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.

102 lines
4.3KB

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