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.

106 lines
4.5KB

  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. $this->productIds[$product->getId()] = $product;
  42. $this->addProperty(
  43. $product->getId(),
  44. [
  45. 'label' => $product->getTitle()
  46. ]
  47. );
  48. }
  49. }
  50. // Initialise les valeurs des données pour chaque Interval de date
  51. public function init(SectionInterface $section, DistributionBuilder $distributionBuilder)
  52. {
  53. $distributionArray = $distributionBuilder->getDistributionListFromCurrentOrder($section, $this->nbCycle);
  54. $this->distributionList = $distributionArray->toArray();
  55. // @TODO : à retravailler pour inclure cette logique directement dans getDistributionListFromCurrentOrder()
  56. $distributionCurrentOrder = $distributionBuilder->guessCurrentDistributionOrder($section);
  57. if(!$distributionArray->contains($distributionCurrentOrder)) {
  58. array_unshift($this->distributionList, $distributionCurrentOrder);
  59. }
  60. foreach ($this->distributionList as $distribution){
  61. $this->labels[$this->getKey($distribution->getCycleNumber(),$distribution->getYear())] = $distribution->getCycleNumber();
  62. foreach ($this->getProperties() as $propertyName => $property) {
  63. $this->properties[$propertyName]['data'][$this->getKey($distribution->getCycleNumber(),$distribution->getYear())] = 0;
  64. }
  65. foreach ($this->getAverageProperties() as $propertyName => $property) {
  66. $this->averageProperties[$propertyName]['data'][$this->getKey($distribution->getCycleNumber(),$distribution->getYear())] = 0;
  67. }
  68. }
  69. }
  70. public function populateProperties(OrderShopStore $orderShopStore)
  71. {
  72. $countsOrderedByCyclesAndProducts = $orderShopStore->countValidOrderProductsOfDistributionsByProducts(
  73. $this->distributionList,
  74. $this->productIds
  75. );
  76. foreach ($countsOrderedByCyclesAndProducts as $result) {
  77. $this->setData($result['productId'], $this->getKey($result['cycleNumber'],$result['year']), $result['quantity']);
  78. $product = $this->productIds[$result['productId']];
  79. if ($this->productFamily->getBehaviorDisplaySale() == ProductFamilyModel::BEHAVIOR_DISPLAY_SALE_BY_MEASURE) {
  80. $ratioByMeasure = $this->productSolver->getQuantityInherited($product) / $this->productSolver->getUnitInherited($product)->getCoefficient();
  81. $this->setData('total_sales', $this->getKey($result['cycleNumber'],$result['year']), intval($result['quantity']) * $ratioByMeasure);
  82. } else {
  83. $this->setData('total_sales', $this->getKey($result['cycleNumber'],$result['year']), intval($result['quantity']));
  84. }
  85. }
  86. $this->setAveragePropertiesData();
  87. }
  88. protected function getKey($cycleNumber, $year){
  89. return $cycleNumber.'/'.substr($year,2);
  90. }
  91. }