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.

183 lines
5.7KB

  1. <?php
  2. namespace domain\Subscription\Subscription;
  3. use domain\Product\Product\Product;
  4. use domain\Product\Product\ProductSolver;
  5. use domain\User\User\UserSolver;
  6. use domain\_\AbstractService;
  7. use domain\_\SolverInterface;
  8. use yii\helpers\Html;
  9. class SubscriptionSolver extends AbstractService implements SolverInterface
  10. {
  11. protected UserSolver $userSolver;
  12. protected ProductSolver $productSolver;
  13. public function loadDependencies(): void
  14. {
  15. $this->userSolver = $this->loadService(UserSolver::class);
  16. $this->productSolver = $this->loadService(ProductSolver::class);
  17. }
  18. /**
  19. * Informe s'il existe une commande correspond à l'abonnement courant.
  20. */
  21. public function hasOrderAlreadyExist(Subscription $subscription, array $orderArray): bool
  22. {
  23. if (is_array($orderArray) && count($orderArray) > 0) {
  24. foreach ($orderArray as $order) {
  25. if ((($order->id_user > 0 && $order->id_user == $subscription->id_user) ||
  26. (!$order->id_user && $order->username == $subscription->username)) &&
  27. $order->id_point_sale == $subscription->id_point_sale) {
  28. return true;
  29. }
  30. }
  31. }
  32. return false;
  33. }
  34. public function getUsername(Subscription $subscription): string
  35. {
  36. if ($subscription->user) {
  37. return $this->userSolver->getUsername($subscription->user);
  38. }
  39. return $subscription->username;
  40. }
  41. /**
  42. * Valide le fait qu'un abonnement est bien compatible avec une date donnée.
  43. */
  44. public function isSubscriptionMatchWith(Subscription $subscription, string $date): bool
  45. {
  46. $arrayDays = [
  47. 1 => 'monday',
  48. 2 => 'tuesday',
  49. 3 => 'wednesday',
  50. 4 => 'thursday',
  51. 5 => 'friday',
  52. 6 => 'saturday',
  53. 7 => 'sunday'
  54. ];
  55. $nbDays = (strtotime($date) - strtotime($subscription->date_begin)) / (24 * 60 * 60);
  56. if (round($nbDays) % ($subscription->week_frequency * 7) < 7) {
  57. $numDay = date('N', strtotime($date));
  58. $day = $arrayDays[$numDay];
  59. if ($subscription->$day) {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. public function getProductsListAsHtml(Subscription $subscription): string
  66. {
  67. $html = '' ;
  68. foreach($subscription->productSubscription as $productSubscription)
  69. {
  70. if(isset($productSubscription->product) && $productSubscription->product) {
  71. $html .= '<strong>'.($productSubscription->quantity * Product::$unitsArray[$productSubscription->product->unit]['coefficient']) . '&nbsp'. $this->productSolver->strUnit($productSubscription->product, 'wording_short') . '</strong> ' . Html::encode($productSubscription->product->name). '<br />' ;
  72. }
  73. else {
  74. $html .= 'Produit non défini<br />' ;
  75. }
  76. }
  77. if(!count($subscription->productSubscription))
  78. {
  79. $html .= '<i class="bi bi-exclamation-triangle"></i> Aucun produit' ;
  80. }
  81. return $html ;
  82. }
  83. public function getPeriodAsHtml(Subscription $subscription): string
  84. {
  85. $html = '<small>' ;
  86. if($subscription->date_end) {
  87. $html .= 'Du&nbsp;' ;
  88. }
  89. else {
  90. $html .= 'À partir du ' ;
  91. }
  92. $html .= '</small>' ;
  93. $html .= date('d/m/Y',strtotime($subscription->date_begin)) ;
  94. if($subscription->date_end) {
  95. $html .= '<br />au&nbsp;'.date('d/m/Y',strtotime($subscription->date_end)) ;
  96. if(date('Y-m-d') > $subscription->date_end) {
  97. $html .= ' <span class="label label-danger">Terminé</span>' ;
  98. }
  99. }
  100. return $html ;
  101. }
  102. public function getDaysAsHtml(Subscription $subscription): string
  103. {
  104. $html = '' ;
  105. if($subscription->monday) {
  106. $html .= 'lundi, ' ;
  107. }
  108. if($subscription->tuesday) {
  109. $html .= 'mardi, ' ;
  110. }
  111. if($subscription->wednesday) {
  112. $html .= 'mercredi, ' ;
  113. }
  114. if($subscription->thursday) {
  115. $html .= 'jeudi, ' ;
  116. }
  117. if($subscription->friday) {
  118. $html .= 'vendredi, ' ;
  119. }
  120. if($subscription->saturday) {
  121. $html .= 'samedi, ' ;
  122. }
  123. if($subscription->sunday) {
  124. $html .= 'dimanche, ' ;
  125. }
  126. if(strlen($html)) {
  127. return substr ($html, 0, strlen($html) - 2) ;
  128. }
  129. else {
  130. return '<span class="glyphicon glyphicon-warning-sign"></span> Aucun jour' ;
  131. }
  132. }
  133. public function getWeekFrequencyAsString(Subscription $subscription): string
  134. {
  135. if($subscription->week_frequency == 1) {
  136. return 'Toutes les semaines' ;
  137. }
  138. else {
  139. return 'Toutes les '.$subscription->week_frequency.' semaines' ;
  140. }
  141. }
  142. public function containProduct(Subscription $subscription, Product $product): bool
  143. {
  144. foreach($subscription->productSubscription as $productSubscription) {
  145. if($productSubscription->id_product == $product->id) {
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. public function getSubscriptionsListAsHtml(array $subscriptionsArray): string
  152. {
  153. $subscriptionSolver = $this;
  154. return implode(
  155. ', ',
  156. array_map(function($subscription) use ($subscriptionSolver) {
  157. return Html::a($subscriptionSolver->getUsername($subscription), ['subscription/update', 'id' => $subscription->id]);
  158. }, $subscriptionsArray)
  159. );
  160. }
  161. }