diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php index 6589c3f..100c5cf 100644 --- a/app/Http/Controllers/AjaxController.php +++ b/app/Http/Controllers/AjaxController.php @@ -425,7 +425,6 @@ class AjaxController extends Controller return json_encode( $url ); } else { // ---- DEMONSTRATION POUR LES PANIERS - $panier = Panier::where(['panier_campId' => $camp_id, 'panier_contactId' => $demoContact->contact_id, 'panier_pending' => 1])->first(); if($panier){ diff --git a/app/Http/Controllers/MurController.php b/app/Http/Controllers/MurController.php index f50c8be..20fe2dd 100644 --- a/app/Http/Controllers/MurController.php +++ b/app/Http/Controllers/MurController.php @@ -40,7 +40,7 @@ class MurController extends Controller $contact = $request->session()->get('contact'); } - $coupons = $campagne->coupons; + $coupons = $this->getCouponsPublished($campagne); foreach ($coupons as &$coupon){ $coupon->isAvailable = $this->isAvailable($coupon, $campagne, $contact); @@ -289,7 +289,7 @@ class MurController extends Controller ]); } - $coupons = Coupon::where(['coupon_campId' => $refcamp, 'coupon_published' => 1])->get(); + $coupons = $this->getCouponsPublished($campagne); foreach($coupons as &$coupon){ $coupon->isAvailable = $this->isAvailable($coupon, $campagne, $contact); diff --git a/app/Http/Controllers/PanierController.php b/app/Http/Controllers/PanierController.php index 529e3e1..87b3fda 100644 --- a/app/Http/Controllers/PanierController.php +++ b/app/Http/Controllers/PanierController.php @@ -9,12 +9,14 @@ use App\Coupon; use App\Contact; use App\Log; use App\PanierContent; +use App\Traits\CouponTrait; use App\Traits\ThanksTrait; use Response; class PanierController extends Controller { use ThanksTrait; + use CouponTrait; public function showTemplate(Request $request) { @@ -28,7 +30,7 @@ class PanierController extends Controller ]); } session()->put('panier', $panier); - $panierCouponsCount = $panier->panierContents()->sum('panier_content_quantity'); + $panierCouponsCount = $this->getCountCouponsPanierPublished($panier); $contact = Contact::where(['contact_id' => $panier->panier_contactId])->first(); if(!$contact){ return Response::json([ @@ -50,9 +52,9 @@ class PanierController extends Controller session()->put('campagne', $campagne); if($campagne->camp_type == 1) { - $coupons = $campagne->coupons; + $coupons = $this->getCouponsPublished($campagne); } else { - $panierContents = $panier->panierContents()->get(); + $panierContents = $this->getPanierContentPublished($panier); $coupons = array(); foreach ($panierContents as $panierContent) { $cp = Coupon::where(['coupon_id'=> $panierContent->panier_content_couponId])->first(); @@ -67,8 +69,7 @@ class PanierController extends Controller ->with(['ref'=> $panier->panier_campId, 'coupons' => $coupons, 'contact' => $contact, 'nb' => $panierCouponsCount]); echo($view); - - + } public function printPanierSelect(Request $request) @@ -337,10 +338,13 @@ class PanierController extends Controller } //---- Préparation message de log avec coupons - $panierCoupons = array(); - foreach ($coupons as $code => $qty) { - array_push($panierCoupons, ['cp_code' => $code, 'qty' => $qty]); + if(isset($coupons)){ + $panierCoupons = array(); + foreach ($coupons as $code => $qty) { + array_push($panierCoupons, ['cp_code' => $code, 'qty' => $qty]); + } } + if($error){ diff --git a/app/Traits/CouponTrait.php b/app/Traits/CouponTrait.php index c0e4f8f..52da303 100644 --- a/app/Traits/CouponTrait.php +++ b/app/Traits/CouponTrait.php @@ -3,7 +3,7 @@ namespace App\Traits; use App\Campagne; -use app\Coupon; +use App\Coupon; use App\Contact; use App\Calendrier; use App\Mur; @@ -12,13 +12,120 @@ use App\Panier; trait CouponTrait { + public function getCouponsPublished(Campagne $campagne) { + $coupons = $campagne->coupons()->get(); + + foreach ($coupons as $key => $coupon) { + + // ---- Contrôle de la publication + if($coupon->coupon_published == 0) { + $coupons->forget($key); + } + + // ---- Contrôle la date de validité + if($coupon->coupon_maxdatevalidity != null){ + if(strtotime(date('Y-m-d')) > strtotime($coupon->coupon_maxdatevalidity)){ + $coupons->forget($key); + } + } + + // ---- Contrôle des calendriers de diffusion + $calendars = Calendrier::where(['calendrier_couponId' => $coupon->coupon_id])->get(); + if(isset($calendars)){ + $pub = false; + foreach ($calendars as $calendar) { + if(strtotime(date('Y-m-d H:i:s')) > strtotime($calendar->calendrier_startdate) && strtotime(date('Y-m-d H:i:s')) < strtotime($calendar->calendrier_enddate)){ + $pub = true; + } + } + if(!$pub) { + $coupons->forget($key); + } + } + } + + return $coupons; + } + + public function getPanierContentPublished(Panier $panier) { + $panierContents = $panier->panierContents()->get(); + foreach ($panierContents as $key => $panierContent) { + $coupon = Coupon::where(['coupon_id' => $panierContent->panier_content_couponId])->first(); + // ---- Contrôle de la publication + if($coupon->coupon_published == 0) { + $panierContents->forget($key); + } + + // ---- Contrôle la date de validité + if($coupon->coupon_maxdatevalidity != null){ + if(strtotime(date('Y-m-d')) > strtotime($coupon->coupon_maxdatevalidity)){ + $panierContents->forget($key); + } + } + + // ---- Contrôle des calendriers de diffusion + $calendars = Calendrier::where(['calendrier_couponId' => $coupon->coupon_id])->get(); + if(isset($calendars)){ + $pub = false; + foreach ($calendars as $calendar) { + if(strtotime(date('Y-m-d H:i:s')) > strtotime($calendar->calendrier_startdate) && strtotime(date('Y-m-d H:i:s')) < strtotime($calendar->calendrier_enddate)){ + $pub = true; + } + } + if(!$pub) { + $panierContents->forget($key); + } + } + } + + return $panierContents; + } + + public function getCountCouponsPanierPublished(Panier $panier) { + $panierContents = $panier->panierContents()->get(); + foreach ($panierContents as $key => $panierContent) { + $coupon = Coupon::where(['coupon_id' => $panierContent->panier_content_couponId])->first(); + // ---- Contrôle de la publication + if($coupon->coupon_published == 0) { + $panierContents->forget($key); + } + + // ---- Contrôle la date de validité + if($coupon->coupon_maxdatevalidity != null){ + if(strtotime(date('Y-m-d')) > strtotime($coupon->coupon_maxdatevalidity)){ + $panierContents->forget($key); + } + } + + // ---- Contrôle des calendriers de diffusion + $calendars = Calendrier::where(['calendrier_couponId' => $coupon->coupon_id])->get(); + if(isset($calendars)){ + $pub = false; + foreach ($calendars as $calendar) { + if(strtotime(date('Y-m-d H:i:s')) > strtotime($calendar->calendrier_startdate) && strtotime(date('Y-m-d H:i:s')) < strtotime($calendar->calendrier_enddate)){ + $pub = true; + } + } + if(!$pub) { + $panierContents->forget($key); + } + } + } + + $count = 0; + + foreach ($panierContents as $panierContent) { + $count = $count + $panierContent->panier_content_quantity; + } + + return $count; + } + + + public function isAvailable(Coupon $coupon, Campagne $campagne, Contact $contact){ $available = true; - if($coupon->coupon_published == 0) { - $available = false; - } - // ---- Contrôle le nombre limite d'impression if($coupon->coupon_enablelimitquantity == 1){ $stat = Mur::where([ @@ -51,25 +158,6 @@ trait CouponTrait { } } - // ---- Contrôle la date de validité - if($coupon->coupon_maxdatevalidity != null){ - if(date('Y-m-d') > $coupon->coupon_maxdatevalidity){ - $available = false; - } - } - - // ----- Controle le calendrier de diffusion - // $calendars = Calendrier::where(['calendrier_couponId' => $coupon->coupon_id])->get(); - // if(isset($calendars)){ - // $available = false; - // foreach ($calendars as $calendar) { - // if(date('Y-m-d H:i:s') > $calendar->startdate && date('Y-m-d H:i:s') < $calendar->enddate){ - // $available = true; - // } - // } - // } - - return $available; }