Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

135 lines
4.3KB

  1. <?php
  2. namespace domain\PointSale\PointSale;
  3. use common\helpers\GlobalParam;
  4. use domain\Distribution\Distribution\Distribution;
  5. use domain\_\AbstractService;
  6. use domain\_\SolverInterface;
  7. use yii\helpers\Html;
  8. class PointSaleSolver extends AbstractService implements SolverInterface
  9. {
  10. public function filterUserPointSalesByDistribution(array $userPointSaleArray, Distribution $distribution): array
  11. {
  12. foreach($userPointSaleArray as $key => $userPointSale) {
  13. if(!$this->isDelivered($userPointSale->pointSale, $distribution)) {
  14. unset($userPointSaleArray[$key]);
  15. }
  16. }
  17. return $userPointSaleArray;
  18. }
  19. public function isDelivered(PointSale $pointSale, Distribution $distribution): bool
  20. {
  21. foreach($distribution->pointSaleDistribution as $pointSaleDistribution) {
  22. if($pointSaleDistribution->pointSale->id == $pointSale->id && $pointSaleDistribution->delivery) {
  23. return true;
  24. }
  25. }
  26. return false;
  27. }
  28. /**
  29. * Retourne le commentaire de l'utilisateur courant lié au point de vente.
  30. */
  31. public function getComment(PointSale $pointSale): ?string
  32. {
  33. if (isset($pointSale->userPointSale)) {
  34. foreach ($pointSale->userPointSale as $userPointSale) {
  35. if ($userPointSale->id_user == GlobalParam::getCurrentUserId()) {
  36. return $userPointSale->comment;
  37. }
  38. }
  39. }
  40. return null;
  41. }
  42. /**
  43. * Vérifie le code d'accès à un point de vente.
  44. */
  45. public function validateCode(PointSale $pointSale, string $code): bool
  46. {
  47. if (strlen($pointSale->code)) {
  48. if (trim(strtolower($code)) == trim(strtolower($pointSale->code))) {
  49. return true;
  50. }
  51. else {
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57. /**
  58. * Retourne les jours de livraison du point de vente sous forme d'une chaine
  59. * de caractères.
  60. */
  61. public function getStrDeliveryDays(PointSale $pointSale): string
  62. {
  63. $str = '';
  64. if ($pointSale->delivery_monday) $str .= 'lundi, ';
  65. if ($pointSale->delivery_tuesday) $str .= 'mardi, ';
  66. if ($pointSale->delivery_wednesday) $str .= 'mercredi, ';
  67. if ($pointSale->delivery_thursday) $str .= 'jeudi, ';
  68. if ($pointSale->delivery_friday) $str .= 'vendredi, ';
  69. if ($pointSale->delivery_saturday) $str .= 'samedi, ';
  70. if ($pointSale->delivery_sunday) $str .= 'dimanche, ';
  71. if (strlen($str)) {
  72. return substr($str, 0, strlen($str) - 2);
  73. } else {
  74. return '';
  75. }
  76. }
  77. /**
  78. * Retourne un commentaire informant l'utilisateur sur les détails de
  79. * livraison d'un point de vente et pour un jour donné.
  80. */
  81. public function getStrInfos(PointSale $pointSale, string $day): string
  82. {
  83. $str = '';
  84. $field = 'infos_' . $day;
  85. if (strlen($pointSale->$field)) {
  86. $str = nl2br(Html::encode($pointSale->$field));
  87. $str = preg_replace('/\[select_previous_day\](.*?)\[\/select_previous_day\]/', '<a href="javascript:void(0);" class="select-previous-day">$1</a>', $str);
  88. $str = preg_replace('@([^>"])(https?://[a-z0-9\./+,%#_-]+)@i', '$1<a href="$2" target="_blank">$2</a>', $str);
  89. }
  90. return $str;
  91. }
  92. public function getStrInfosByDistribution(PointSale $pointSale, Distribution $distribution): string
  93. {
  94. return $this->getStrInfos($pointSale, strtolower(date('l', strtotime($distribution->date))));
  95. }
  96. public function getLocalityWithAddressTooltip(PointSale $pointSale): string
  97. {
  98. $html = '<span class="locality">';
  99. if($pointSale->address && strlen($pointSale->address) > 0) {
  100. $html .= '<span data-bs-toggle="tooltip" data-bs-placement="bottom" title="'.Html::encode($pointSale->address).'">'.Html::encode($pointSale->locality).'</span>';
  101. }
  102. else {
  103. $html .= Html::encode($pointSale->locality);
  104. }
  105. $html .= '</span>';
  106. return $html;
  107. }
  108. public function isPublic(PointSale $pointSale): bool
  109. {
  110. if($pointSale->restricted_access || ($pointSale->code && strlen($pointSale->code))) {
  111. return false;
  112. }
  113. return true;
  114. }
  115. }