|
- <?php
-
- namespace Lc\CaracoleBundle\Statistic\Product;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Builder\Distribution\DistributionBuilder;
- use Lc\CaracoleBundle\Container\Order\OrderShopContainer;
- use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
- use Lc\CaracoleBundle\Model\Section\SectionInterface;
- use Lc\CaracoleBundle\Repository\Order\OrderProductStore;
- use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
- use Lc\CaracoleBundle\Resolver\OpeningResolver;
- use Lc\CaracoleBundle\Solver\Distribution\DistributionSolver;
- use Lc\CaracoleBundle\Solver\Order\OrderShopSolver;
- use Lc\CaracoleBundle\Solver\Product\ProductSolver;
- use Lc\CaracoleBundle\Statistic\Statistic;
- use function Symfony\Component\Translation\t;
-
- class ProductsSalesStatistic extends Statistic
- {
- protected int $nbCycle;
- protected $productFamily;
- protected $distributionList = array();
- protected $productIds = array();
- protected ProductSolver $productSolver;
-
- public function __construct(EntityManagerInterface $entityManager, $productFamily, $nbCycle, ProductSolver $productSolver)
- {
- parent::__construct($entityManager);
- $this->nbCycle = $nbCycle;
- $this->productFamily = $productFamily;
- $this->productSolver = $productSolver;
-
- $this->createProperties();
- }
-
- public function createProperties()
- {
- $this->addProperty(
- 'total_sales',
- [
- 'label' => 'Total ventes'
- ]
- );
- foreach ($this->productFamily->getProducts() as $product) {
- $this->productIds[$product->getId()] = $product;
- $this->addProperty(
- $product->getId(),
- [
- 'label' => $product->getTitle()
- ]
- );
- }
- }
-
- // Initialise les valeurs des données pour chaque Interval de date
- public function init(SectionInterface $section, DistributionBuilder $distributionBuilder)
- {
- $this->distributionList = $distributionBuilder->getDistributionListFromCurrentOrder($section, $this->nbCycle);
-
- // if ($openingResolver->isOpenSale($section, null,OpeningResolver::OPENING_CONTEXT_BACKEND) == false && date('w') > 2) {
- // $currentCycleNumber = $currentCycleNumber - 1;
- // }
-
- foreach ($this->distributionList as $distribution){
- $this->labels[$this->getKey($distribution->getCycleNumber(),$distribution->getYear())] = $distribution->getCycleNumber();
- foreach ($this->getProperties() as $propertyName => $property) {
- $this->properties[$propertyName]['data'][$this->getKey($distribution->getCycleNumber(),$distribution->getYear())] = 0;
- }
- foreach ($this->getAverageProperties() as $propertyName => $property) {
- $this->averageProperties[$propertyName]['data'][$this->getKey($distribution->getCycleNumber(),$distribution->getYear())] = 0;
- }
- }
- }
-
- public function populateProperties(OrderShopStore $orderShopStore)
- {
- $countsOrderedByCyclesAndProducts = $orderShopStore->countValidOrderProductsOfDistributionsByProducts(
- $this->distributionList,
- $this->productIds
- );
-
- foreach ($countsOrderedByCyclesAndProducts as $result) {
- $this->setData($result['productId'], $this->getKey($result['cycleNumber'],$result['year']), $result['quantity']);
- $product = $this->productIds[$result['productId']];
- if ($this->productFamily->getBehaviorDisplaySale() == ProductFamilyModel::BEHAVIOR_DISPLAY_SALE_BY_MEASURE) {
-
- $ratioByMeasure = $this->productSolver->getQuantityInherited($product) / $this->productSolver->getUnitInherited($product)->getCoefficient();
- $this->setData('total_sales', $this->getKey($result['cycleNumber'],$result['year']), intval($result['quantity']) * $ratioByMeasure);
- } else {
- $this->setData('total_sales', $this->getKey($result['cycleNumber'],$result['year']), intval($result['quantity']));
- }
- }
- $this->setAveragePropertiesData();
- }
-
- protected function getKey($cycleNumber, $year){
- return $cycleNumber.'/'.substr($year,2);
- }
-
- }
|