You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProductsSalesStatistic.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Lc\CaracoleBundle\Statistic\Product;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\CaracoleBundle\Builder\Distribution\DistributionBuilder;
  6. use Lc\CaracoleBundle\Container\Order\OrderShopContainer;
  7. use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
  8. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  9. use Lc\CaracoleBundle\Repository\Order\OrderProductStore;
  10. use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
  11. use Lc\CaracoleBundle\Resolver\OpeningResolver;
  12. use Lc\CaracoleBundle\Solver\Distribution\DistributionSolver;
  13. use Lc\CaracoleBundle\Solver\Order\OrderShopSolver;
  14. use Lc\CaracoleBundle\Solver\Product\ProductSolver;
  15. use Lc\CaracoleBundle\Statistic\Statistic;
  16. use function Symfony\Component\Translation\t;
  17. class ProductsSalesStatistic extends Statistic
  18. {
  19. protected int $nbCycle;
  20. protected $productFamily;
  21. protected $distributionList = array();
  22. protected $productIds = array();
  23. protected ProductSolver $productSolver;
  24. public function __construct(EntityManagerInterface $entityManager, $productFamily, $nbCycle, ProductSolver $productSolver)
  25. {
  26. parent::__construct($entityManager);
  27. $this->nbCycle = $nbCycle;
  28. $this->productFamily = $productFamily;
  29. $this->productSolver = $productSolver;
  30. $this->createProperties();
  31. }
  32. public function createProperties()
  33. {
  34. $this->addProperty(
  35. 'total_sales',
  36. [
  37. 'label' => 'Total ventes'
  38. ]
  39. );
  40. foreach ($this->productFamily->getProducts() as $product) {
  41. if($product->getId()) {
  42. $this->productIds[$product->getId()] = $product;
  43. $this->addProperty(
  44. $product->getId(),
  45. [
  46. 'label' => $product->getTitle()
  47. ]
  48. );
  49. }
  50. }
  51. }
  52. // Initialise les valeurs des données pour chaque Interval de date
  53. public function init(SectionInterface $section, DistributionBuilder $distributionBuilder)
  54. {
  55. $distributionArray = $distributionBuilder->getDistributionListFromCurrentOrder($section, $this->nbCycle);
  56. $this->distributionList = $distributionArray->toArray();
  57. // @TODO : à retravailler pour inclure cette logique directement dans getDistributionListFromCurrentOrder()
  58. $distributionCurrentOrder = $distributionBuilder->guessCurrentDistributionOrder($section);
  59. if(!$distributionArray->contains($distributionCurrentOrder)) {
  60. array_unshift($this->distributionList, $distributionCurrentOrder);
  61. }
  62. foreach ($this->distributionList as $distribution){
  63. $this->labels[$this->getKey($distribution->getCycleNumber(),$distribution->getYear())] = $distribution->getCycleNumber();
  64. foreach ($this->getProperties() as $propertyName => $property) {
  65. $this->properties[$propertyName]['data'][$this->getKey($distribution->getCycleNumber(),$distribution->getYear())] = 0;
  66. }
  67. foreach ($this->getAverageProperties() as $propertyName => $property) {
  68. $this->averageProperties[$propertyName]['data'][$this->getKey($distribution->getCycleNumber(),$distribution->getYear())] = 0;
  69. }
  70. }
  71. }
  72. public function populateProperties(OrderShopStore $orderShopStore)
  73. {
  74. $countsOrderedByCyclesAndProducts = $orderShopStore->countValidOrderProductsOfDistributionsByProducts(
  75. $this->distributionList,
  76. $this->productIds,
  77. $this->productFamily
  78. );
  79. foreach ($countsOrderedByCyclesAndProducts as $result) {
  80. $this->setData($result['productId'], $this->getKey($result['cycleNumber'],$result['year']), $result['quantity']);
  81. $product = $this->productIds[$result['productId']];
  82. if ($this->productFamily->getBehaviorDisplaySale() == ProductFamilyModel::BEHAVIOR_DISPLAY_SALE_BY_MEASURE) {
  83. $ratioByMeasure = $this->productSolver->getQuantityInherited($product) / $this->productSolver->getUnitInherited($product)->getCoefficient();
  84. $this->setData('total_sales', $this->getKey($result['cycleNumber'],$result['year']), intval($result['quantity']) * $ratioByMeasure);
  85. } else {
  86. $this->setData('total_sales', $this->getKey($result['cycleNumber'],$result['year']), intval($result['quantity']));
  87. }
  88. }
  89. $this->setAveragePropertiesData();
  90. }
  91. protected function getKey($cycleNumber, $year){
  92. return $cycleNumber.'/'.substr($year,2);
  93. }
  94. }