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 3.7KB

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