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.

101 line
3.7KB

  1. <?php
  2. namespace common\logic\Distribution\PointSaleDistribution;
  3. use common\helpers\GlobalParam;
  4. use common\logic\BaseService;
  5. use common\logic\BuilderInterface;
  6. use common\logic\Distribution\Distribution\Distribution;
  7. use common\logic\Distribution\Distribution\DistributionRepository;
  8. use common\logic\PointSale\PointSale\PointSale;
  9. use common\logic\PointSale\PointSale\PointSaleRepository;
  10. class PointSaleDistributionBuilder extends BaseService implements BuilderInterface
  11. {
  12. protected PointSaleDistributionRepository $pointSaleDistributionRepository;
  13. protected PointSaleRepository $pointSaleRepository;
  14. protected DistributionRepository $distributionRepository;
  15. public function __construct()
  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 instanciate(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
  25. {
  26. $pointSaleDistribution = new PointSaleDistribution();
  27. $pointSaleDistribution->id_distribution = $distribution->id;
  28. $pointSaleDistribution->id_point_sale = $pointSale->id;
  29. return $pointSaleDistribution;
  30. }
  31. /**
  32. * Crée un PointSaleDistribution.
  33. */
  34. public function create(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
  35. {
  36. $pointSaleDistribution = $this->instanciate($distribution, $pointSale);
  37. $pointSaleDistribution->save();
  38. return $pointSaleDistribution;
  39. }
  40. public function createIfNotExist(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
  41. {
  42. $pointSaleDistribution = $this->pointSaleDistributionRepository->getOne($distribution, $pointSale);
  43. if (!$pointSaleDistribution) {
  44. $pointSaleDistribution = $this->create($distribution, $pointSale);
  45. }
  46. return $pointSaleDistribution;
  47. }
  48. /**
  49. * Crée les PointSaleDistribution pour une distribution donnée.
  50. */
  51. // setAll
  52. public function createAll(Distribution $distribution, bool $delivery): void
  53. {
  54. $pointSaleArray = $this->pointSaleRepository->getByDistribution($distribution);
  55. foreach ($pointSaleArray as $pointSale) {
  56. if (!$pointSale->pointSaleDistribution) {
  57. $this->create($distribution, $pointSale);
  58. }
  59. }
  60. $pointSaleDistributionArray = $this->pointSaleDistributionRepository->getByDistribution($distribution);
  61. foreach ($pointSaleDistributionArray as $pointSaleDistribution) {
  62. $this->initDelivery($pointSaleDistribution, $delivery);
  63. }
  64. }
  65. public function initDelivery(PointSaleDistribution $pointSaleDistribution, bool $delivery)
  66. {
  67. $day = date('N', strtotime($pointSaleDistribution->distribution->date));
  68. $pointSale = $pointSaleDistribution->pointSale;
  69. if ($delivery &&
  70. (($day == 1 && $pointSale->delivery_monday) ||
  71. ($day == 2 && $pointSale->delivery_tuesday) ||
  72. ($day == 3 && $pointSale->delivery_wednesday) ||
  73. ($day == 4 && $pointSale->delivery_thursday) ||
  74. ($day == 5 && $pointSale->delivery_friday) ||
  75. ($day == 6 && $pointSale->delivery_saturday) ||
  76. ($day == 7 && $pointSale->delivery_sunday)
  77. )) {
  78. $pointSaleDistribution->delivery = 1;
  79. } else {
  80. $pointSaleDistribution->delivery = 0;
  81. }
  82. $pointSaleDistribution->save();
  83. }
  84. }