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ů.

PointSaleDistributionBuilder.php 3.7KB

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