|
- <?php
-
- namespace domain\Subscription\Subscription;
-
- use domain\Product\Product\Product;
- use domain\Product\Product\ProductSolver;
- use domain\User\User\UserSolver;
- use domain\_\AbstractService;
- use domain\_\SolverInterface;
- use yii\helpers\Html;
-
- class SubscriptionSolver extends AbstractService implements SolverInterface
- {
- protected UserSolver $userSolver;
- protected ProductSolver $productSolver;
-
- public function loadDependencies(): void
- {
- $this->userSolver = $this->loadService(UserSolver::class);
- $this->productSolver = $this->loadService(ProductSolver::class);
- }
-
- /**
- * Informe s'il existe une commande correspond à l'abonnement courant.
- */
- public function hasOrderAlreadyExist(Subscription $subscription, array $orderArray): bool
- {
- if (is_array($orderArray) && count($orderArray) > 0) {
- foreach ($orderArray as $order) {
- if ((($order->id_user > 0 && $order->id_user == $subscription->id_user) ||
- (!$order->id_user && $order->username == $subscription->username)) &&
- $order->id_point_sale == $subscription->id_point_sale) {
- return true;
- }
- }
- }
-
- return false;
- }
-
- public function getUsername(Subscription $subscription): string
- {
- if ($subscription->user) {
- return $this->userSolver->getUsername($subscription->user);
- }
-
- return $subscription->username;
- }
-
- /**
- * Valide le fait qu'un abonnement est bien compatible avec une date donnée.
- */
- public function isSubscriptionMatchWith(Subscription $subscription, string $date): bool
- {
- $arrayDays = [
- 1 => 'monday',
- 2 => 'tuesday',
- 3 => 'wednesday',
- 4 => 'thursday',
- 5 => 'friday',
- 6 => 'saturday',
- 7 => 'sunday'
- ];
-
- $nbDays = (strtotime($date) - strtotime($subscription->date_begin)) / (24 * 60 * 60);
- if (round($nbDays) % ($subscription->week_frequency * 7) < 7) {
- $numDay = date('N', strtotime($date));
- $day = $arrayDays[$numDay];
- if ($subscription->$day) {
- return true;
- }
- }
-
- return false;
- }
-
- public function getProductsListAsHtml(Subscription $subscription): string
- {
- $html = '' ;
- foreach($subscription->productSubscription as $productSubscription)
- {
- if(isset($productSubscription->product) && $productSubscription->product) {
- $html .= '<strong>'.($productSubscription->quantity * Product::$unitsArray[$productSubscription->product->unit]['coefficient']) . ' '. $this->productSolver->strUnit($productSubscription->product, 'wording_short') . '</strong> ' . Html::encode($productSubscription->product->name). '<br />' ;
- }
- else {
- $html .= 'Produit non défini<br />' ;
- }
- }
-
- if(!count($subscription->productSubscription))
- {
- $html .= '<i class="bi bi-exclamation-triangle"></i> Aucun produit' ;
- }
-
- return $html ;
- }
-
- public function getPeriodAsHtml(Subscription $subscription): string
- {
- $html = '<small>' ;
- if($subscription->date_end) {
- $html .= 'Du ' ;
- }
- else {
- $html .= 'À partir du ' ;
- }
- $html .= '</small>' ;
- $html .= date('d/m/Y',strtotime($subscription->date_begin)) ;
- if($subscription->date_end) {
- $html .= '<br />au '.date('d/m/Y',strtotime($subscription->date_end)) ;
- if(date('Y-m-d') > $subscription->date_end) {
- $html .= ' <span class="label label-danger">Terminé</span>' ;
- }
- }
-
- return $html ;
- }
-
- public function getDaysAsHtml(Subscription $subscription): string
- {
- $html = '' ;
- if($subscription->monday) {
- $html .= 'lundi, ' ;
- }
- if($subscription->tuesday) {
- $html .= 'mardi, ' ;
- }
- if($subscription->wednesday) {
- $html .= 'mercredi, ' ;
- }
- if($subscription->thursday) {
- $html .= 'jeudi, ' ;
- }
- if($subscription->friday) {
- $html .= 'vendredi, ' ;
- }
- if($subscription->saturday) {
- $html .= 'samedi, ' ;
- }
- if($subscription->sunday) {
- $html .= 'dimanche, ' ;
- }
-
- if(strlen($html)) {
- return substr ($html, 0, strlen($html) - 2) ;
- }
- else {
- return '<span class="glyphicon glyphicon-warning-sign"></span> Aucun jour' ;
- }
- }
-
- public function getWeekFrequencyAsString(Subscription $subscription): string
- {
- if($subscription->week_frequency == 1) {
- return 'Toutes les semaines' ;
- }
- else {
- return 'Toutes les '.$subscription->week_frequency.' semaines' ;
- }
- }
-
- public function containProduct(Subscription $subscription, Product $product): bool
- {
- foreach($subscription->productSubscription as $productSubscription) {
- if($productSubscription->id_product == $product->id) {
- return true;
- }
- }
-
- return false;
- }
-
- public function getSubscriptionsListAsHtml(array $subscriptionsArray): string
- {
- $subscriptionSolver = $this;
- return implode(
- ', ',
- array_map(function($subscription) use ($subscriptionSolver) {
- return Html::a($subscriptionSolver->getUsername($subscription), ['subscription/update', 'id' => $subscription->id]);
- }, $subscriptionsArray)
- );
- }
- }
|