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.

118 lines
3.6KB

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