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.

136 lines
4.1KB

  1. <?php
  2. namespace domain\Distribution\Distribution;
  3. use domain\PointSale\PointSale\PointSale;
  4. use domain\Producer\Producer\ProducerSolver;
  5. use domain\Product\Product\Product;
  6. use domain\_\AbstractService;
  7. use domain\_\SolverInterface;
  8. class DistributionSolver extends AbstractService implements SolverInterface
  9. {
  10. public function validateDistributionDate(string $date = null): bool
  11. {
  12. $format = 'Y-m-d';
  13. $dateObject = \DateTime::createFromFormat($format, $date);
  14. if ($dateObject && $dateObject->format($format) === $date) {
  15. return true;
  16. }
  17. return false;
  18. }
  19. public function getDistributionDayAsString(Distribution $distribution): string
  20. {
  21. $dayDistribution = date('N', strtotime($distribution->date));
  22. $daysArray = [
  23. 1 => 'monday',
  24. 2 => 'tuesday',
  25. 3 => 'wednesday',
  26. 4 => 'thursday',
  27. 5 => 'friday',
  28. 6 => 'saturday',
  29. 7 => 'sunday',
  30. ];
  31. return $daysArray[$dayDistribution];
  32. }
  33. /**
  34. * Déduit si un produit est actif ou non
  35. */
  36. public function isActiveProduct(Distribution $distribution, Product $product): bool
  37. {
  38. if ($product->id
  39. && isset($distribution->productDistribution)
  40. && count($distribution->productDistribution) > 0) {
  41. foreach ($distribution->productDistribution as $productDistribution) {
  42. if ($productDistribution['id_product'] == $product->id
  43. && $productDistribution['active']) {
  44. return true;
  45. }
  46. }
  47. }
  48. return false;
  49. }
  50. // isAvailable
  51. public function isDistributionAvailable(Distribution $distribution): bool
  52. {
  53. $dateToday = date('Y-m-d');
  54. $producer = $distribution->producer;
  55. $dateDistribution = $distribution->date;
  56. $dayDistribution = strtolower(date('l', strtotime($dateDistribution)));
  57. $fieldDayDistributionDelay = 'order_delay_' . $dayDistribution;
  58. $fieldDayDistributionDeadline = 'order_deadline_' . $dayDistribution;
  59. $delay = $producer->order_delay;
  60. $deadline = $producer->order_deadline;
  61. if ($producer->$fieldDayDistributionDelay) {
  62. $delay = $producer->$fieldDayDistributionDelay;
  63. }
  64. if ($producer->$fieldDayDistributionDeadline) {
  65. $deadline = $producer->$fieldDayDistributionDeadline;
  66. }
  67. $countDaysTodayDistribution = number_format((strtotime($dateDistribution) - strtotime($dateToday)) / (24 * 60 * 60), 0);
  68. if(date('H') >= $deadline) {
  69. $countDaysTodayDistribution--;
  70. }
  71. if ($countDaysTodayDistribution < $delay) {
  72. return false;
  73. }
  74. return true;
  75. }
  76. /**
  77. * Filtre les distributions par délai de commande.
  78. */
  79. public function filterDistributionsByDateDelay(array $distributionsArray): array
  80. {
  81. foreach ($distributionsArray as $keyDistribution => $distribution) {
  82. if(!$this->isDistributionAvailable($distribution)) {
  83. unset($distributionsArray[$keyDistribution]);
  84. }
  85. }
  86. $newDistributionsArray = [];
  87. foreach ($distributionsArray as $distribution) {
  88. $newDistributionsArray[] = $distribution;
  89. }
  90. return $newDistributionsArray;
  91. }
  92. public function isPointSaleActive(Distribution $distribution, PointSale $pointSale): bool
  93. {
  94. if ($distribution->pointSaleDistribution) {
  95. foreach ($distribution->pointSaleDistribution as $pointSaleDistribution) {
  96. if ($pointSaleDistribution->id_point_sale == $pointSale->id && $pointSaleDistribution->delivery) {
  97. return true;
  98. }
  99. }
  100. }
  101. return false;
  102. }
  103. public function getLinkOrder(Distribution $distribution): string
  104. {
  105. return \Yii::$app->urlManagerProducer->createAbsoluteUrl([
  106. 'order/order',
  107. 'slug_producer' => $this->getProducerContext()->slug,
  108. 'date' => $distribution->date
  109. ]) ;
  110. }
  111. }