Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

99 lines
3.8KB

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