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.

96 line
4.0KB

  1. <?php
  2. namespace common\logic\Distribution\PointSaleDistribution\Service;
  3. use common\logic\AbstractBuilder;
  4. use common\logic\Distribution\Distribution\Model\Distribution;
  5. use common\logic\Distribution\Distribution\Repository\DistributionRepository;
  6. use common\logic\Distribution\PointSaleDistribution\Model\PointSaleDistribution;
  7. use common\logic\Distribution\PointSaleDistribution\Repository\PointSaleDistributionRepository;
  8. use common\logic\PointSale\PointSale\Model\PointSale;
  9. use common\logic\PointSale\PointSale\Repository\PointSaleRepository;
  10. class PointSaleDistributionBuilder extends AbstractBuilder
  11. {
  12. protected PointSaleDistributionRepository $pointSaleDistributionRepository;
  13. protected PointSaleRepository $pointSaleRepository;
  14. protected DistributionRepository $distributionRepository;
  15. public function loadDependencies(): void
  16. {
  17. $this->pointSaleDistributionRepository = $this->loadService(PointSaleDistributionRepository::class);
  18. $this->pointSaleRepository = $this->loadService(PointSaleRepository::class);
  19. $this->distributionRepository = $this->loadService(DistributionRepository::class);
  20. }
  21. /**
  22. * Instancie un PointSaleDistribution.
  23. */
  24. public function instanciatePointSaleDistribution(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
  25. {
  26. $pointSaleDistribution = new PointSaleDistribution();
  27. $pointSaleDistribution->populateFieldObject('id_distribution', 'distribution', $distribution);
  28. $pointSaleDistribution->populateFieldObject('id_point_sale', 'pointSale', $pointSale);
  29. return $pointSaleDistribution;
  30. }
  31. /**
  32. * Crée un PointSaleDistribution.
  33. */
  34. public function createPointSaleDistribution(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
  35. {
  36. $pointSaleDistribution = $this->instanciatePointSaleDistribution($distribution, $pointSale);
  37. $this->saveCreate($pointSaleDistribution);
  38. return $pointSaleDistribution;
  39. }
  40. public function createPointSaleDistributionIfNotExist(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
  41. {
  42. return $this->pointSaleDistributionRepository->findOnePointSaleDistribution($distribution, $pointSale)
  43. ?? $this->createPointSaleDistribution($distribution, $pointSale);
  44. }
  45. /**
  46. * Crée les PointSaleDistribution pour une distribution donnée.
  47. */
  48. // setAll
  49. public function createAllPointSaleDistributions(Distribution $distribution, bool $delivery): void
  50. {
  51. $pointSaleArray = $this->pointSaleRepository->findPointSalesByDistribution($distribution);
  52. foreach ($pointSaleArray as $pointSale) {
  53. if (!$pointSale->pointSaleDistribution) {
  54. $this->createPointSaleDistribution($distribution, $pointSale);
  55. }
  56. }
  57. $pointSaleDistributionArray = $this->pointSaleDistributionRepository->findPointSaleDistributionsByDistribution($distribution);
  58. foreach ($pointSaleDistributionArray as $pointSaleDistribution) {
  59. $this->updateDelivery($pointSaleDistribution, $delivery);
  60. }
  61. }
  62. public function updateDelivery(PointSaleDistribution $pointSaleDistribution, bool $delivery): void
  63. {
  64. $day = date('N', strtotime($pointSaleDistribution->distribution->date));
  65. $pointSale = $pointSaleDistribution->pointSale;
  66. if ($delivery &&
  67. (($day == 1 && $pointSale->delivery_monday) ||
  68. ($day == 2 && $pointSale->delivery_tuesday) ||
  69. ($day == 3 && $pointSale->delivery_wednesday) ||
  70. ($day == 4 && $pointSale->delivery_thursday) ||
  71. ($day == 5 && $pointSale->delivery_friday) ||
  72. ($day == 6 && $pointSale->delivery_saturday) ||
  73. ($day == 7 && $pointSale->delivery_sunday)
  74. )) {
  75. $pointSaleDistribution->delivery = 1;
  76. } else {
  77. $pointSaleDistribution->delivery = 0;
  78. }
  79. $this->saveUpdate($pointSaleDistribution);
  80. }
  81. }