ldc-mesbr-app/app/Http/Controllers/PanierController.php

658 lines
30 KiB
PHP
Raw Normal View History

2019-01-15 14:17:15 +01:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Panier;
2019-01-18 09:00:42 +01:00
use App\Campagne;
use App\Coupon;
2019-01-29 12:26:00 +01:00
use App\Contact;
2019-02-28 13:58:25 +01:00
use App\Site;
2019-02-20 16:26:32 +01:00
use App\Log;
2019-01-18 09:00:42 +01:00
use App\PanierContent;
use App\Traits\CouponTrait;
2019-02-13 16:37:14 +01:00
use App\Traits\ThanksTrait;
2019-01-29 12:26:00 +01:00
use Response;
2019-01-15 14:17:15 +01:00
class PanierController extends Controller
{
2019-02-13 16:37:14 +01:00
use ThanksTrait;
use CouponTrait;
2019-02-13 16:37:14 +01:00
2019-01-15 14:17:15 +01:00
public function showTemplate(Request $request)
{
2019-01-29 12:26:00 +01:00
//$panier = Panier::find($request->query('basketid'));
$panier = Panier::where(['panier_ref' => $request->basketid])->first();
2021-04-27 17:29:47 +02:00
if (!$panier) {
2019-02-28 17:05:24 +01:00
abort(404);
2019-01-29 12:26:00 +01:00
}
2019-01-15 14:17:15 +01:00
session()->put('panier', $panier);
$panierCouponsCount = $this->getCountCouponsPanierPublished($panier);
2019-01-29 12:26:00 +01:00
$contact = Contact::where(['contact_id' => $panier->panier_contactId])->first();
2021-04-27 17:29:47 +02:00
if (!$contact) {
2019-02-28 17:05:24 +01:00
abort(404);
2019-01-29 12:26:00 +01:00
}
session()->put('contact', $contact);
2019-02-28 17:05:24 +01:00
$campagne = Campagne::where(['camp_id' => $panier->panier_campId, 'camp_published' => 1])->first();
2021-04-27 17:29:47 +02:00
if (!$campagne) {
2019-02-28 17:05:24 +01:00
abort(404);
2019-01-29 12:26:00 +01:00
}
session()->put('campagne', $campagne);
2021-04-27 17:29:47 +02:00
if ($campagne->camp_type == 1) {
$coupons = $this->getCouponsPublished($campagne);
2019-02-13 16:37:14 +01:00
} else {
$panierContents = $this->getPanierContentPublished($panier);
2019-02-13 16:37:14 +01:00
$coupons = array();
foreach ($panierContents as $panierContent) {
2021-04-27 17:29:47 +02:00
$cp = Coupon::where(['coupon_id' => $panierContent->panier_content_couponId])->first();
2019-02-13 16:37:14 +01:00
$qty = $panierContent->panier_content_quantity;
2021-04-27 17:29:47 +02:00
for ($i = 0; $i < $qty; $i++) {
2019-03-12 12:30:11 +01:00
$coupons[] = $cp;
2019-02-13 16:37:14 +01:00
}
}
}
session()->put('coupons', $coupons);
2019-05-06 10:33:32 +02:00
return view('landings::' . $panier->panier_campId . '.index')
2021-04-27 17:29:47 +02:00
->with(['ref' => $panier->panier_campId, 'coupons' => $coupons, 'contact' => $contact, 'nb' => $panierCouponsCount]);
2019-01-29 12:26:00 +01:00
}
2019-02-13 16:37:14 +01:00
public function printPanierSelect(Request $request)
2019-01-29 12:26:00 +01:00
{
2021-04-27 17:29:47 +02:00
if (isset($request->coupons)) {
2019-02-13 16:37:14 +01:00
session()->put('coupons', $request->coupons);
$coupons = $request->coupons;
}
2021-04-27 17:29:47 +02:00
if ($request->session()->has('campagne')) {
$campagne = $request->session()->get('campagne');
}
2019-02-13 16:37:14 +01:00
$ref = $campagne->camp_id;
2019-05-06 10:33:32 +02:00
return view('landings::' . $ref . '.imprimer')
2021-04-27 17:29:47 +02:00
->with(['ref' => $ref, 'coupons' => $coupons, 'contact' => $request->session()->get('contact')]);
2019-01-15 14:17:15 +01:00
}
2019-02-20 16:26:32 +01:00
public function updateBasket(Request $request)
2019-01-15 14:17:15 +01:00
{
2021-04-27 17:29:47 +02:00
if (isset($request->admin)) {
$source = 'ADMIN';
2019-02-20 16:26:32 +01:00
} else {
2021-04-27 17:29:47 +02:00
$source = 'API';
2019-02-20 16:26:32 +01:00
}
// ---- INIT du log
$log = new Log();
$log->log_action = config('constants.updatebasket');
$log->log_source = $source;
2021-04-27 17:29:47 +02:00
if (isset($request->basketid)) {
2019-02-20 16:26:32 +01:00
$basketid = $request->basketid;
2021-04-27 17:29:47 +02:00
if ($basketid != '') {
2019-02-20 16:26:32 +01:00
$panier = Panier::where('panier_ref', $basketid)->first();
2021-04-27 17:29:47 +02:00
if (!$panier) {
2019-02-20 16:26:32 +01:00
$log->log_params = json_encode(['basketid' => $basketid]);
$log->log_statut = 'NOK';
$log->log_message = "Le panier n'existe pas";
$log->save();
return Response::json([
'Statut' => 'KO',
'Code' => 106,
'Message' => 'Le panier n\'existe pas'
2021-04-27 17:29:47 +02:00
]);
2019-02-20 16:26:32 +01:00
}
2021-04-27 17:29:47 +02:00
if (isset($request->panier) && $request->panier != "") {
2019-02-20 16:26:32 +01:00
$panier = $request->panier;
$panierTab = explode(",", $panier);
$coupons = array();
2021-04-27 17:29:47 +02:00
foreach ($panierTab as $panier) {
2019-02-20 16:26:32 +01:00
$pos = strpos($panier, ")");
2021-04-27 17:29:47 +02:00
if ($pos === false || substr_count($panier, ")") != 1) {
2019-02-20 16:26:32 +01:00
$log->log_params = json_encode(['basketid' => $basketid, 'panier' => $panier]);
$log->log_statut = 'NOK';
$log->log_message = "Des paramètres de la requête sont mal renseignés ou non présents";
$log->save();
return Response::json([
'Statut' => 'KO',
'Code' => 101,
'Message' => 'Des paramètres de la requête sont mal renseignés ou non présents'
2021-04-27 17:29:47 +02:00
]);
2019-02-20 16:26:32 +01:00
}
$quantity = substr($panier, 0, $pos);
2021-04-27 17:29:47 +02:00
$quantity = str_replace("(", "", $quantity);
$couponCode = substr($panier, $pos + 1);
if (is_numeric($quantity) && is_numeric($couponCode)) {
if (isset($coupons[$couponCode])) {
$coupons[$couponCode] = $coupons[$couponCode] + $quantity;
} else {
$coupons[$couponCode] = $quantity;
2019-02-20 16:26:32 +01:00
}
2019-02-28 17:05:24 +01:00
} else {
$log->log_params = json_encode(['basketid' => $basketid, 'panier' => $panier]);
$log->log_statut = 'NOK';
$log->log_message = "Des paramètres de la requête sont mal renseignés ou non présents";
$log->save();
return Response::json([
'Statut' => 'KO',
'Code' => 101,
'Message' => 'Des paramètres de la requête sont mal renseignés ou non présents'
]);
2019-02-20 16:26:32 +01:00
}
}
//---- Préparation message de log avec coupons
$panierCoupons = array();
foreach ($coupons as $code => $qty) {
array_push($panierCoupons, ['cp_code' => $code, 'qty' => $qty]);
}
2021-04-27 17:29:47 +02:00
if (sizeof($coupons) == 0) {
2019-02-20 16:26:32 +01:00
die();
}
} else {
$log->log_params = json_encode(['basketid' => $basketid, 'panier' => $panier]);
$log->log_statut = 'NOK';
$log->log_message = "Des paramètres de la requête sont mal renseignés ou non présents";
$log->save();
return Response::json([
'Statut' => 'KO',
'Code' => 101,
'Message' => 'Des paramètres de la requête sont mal renseignés ou non présents'
]);
}
// ---- MAJ du panier
$panier = Panier::where('panier_ref', $basketid)->first();
2021-04-27 17:29:47 +02:00
if ($panier) {
foreach ($coupons as $couponCode => $quantity) {
2019-02-20 16:26:32 +01:00
$cp = Coupon::where('coupon_code', $couponCode)->first();
2019-02-28 17:05:24 +01:00
$exists = $cp->campagnes->contains($panier->panier_campId);
2021-04-27 17:29:47 +02:00
if ($cp && $exists) {
2019-02-20 16:26:32 +01:00
$site = $cp->coupon_siteId;
$panierContent = PanierContent::where(['panier_content_panierId' => $panier->panier_id, 'panier_content_couponId' => $cp->coupon_id])->first();
2021-04-27 17:29:47 +02:00
if ($panierContent) {
2019-02-20 16:26:32 +01:00
$quantity += $panierContent->panier_content_quantity;
2021-04-27 17:29:47 +02:00
if ($quantity <= 0) {
$quantity = 0;
}
2019-02-20 16:26:32 +01:00
// MAJ du contenu
$panierContent->panier_content_quantity = $quantity;
$panierContent->save();
2021-04-27 17:29:47 +02:00
} else {
2019-02-20 16:26:32 +01:00
// Ajout du contenu
2021-04-27 17:29:47 +02:00
if ($quantity > 0) {
$panierNewContent = new PanierContent();
2019-02-20 16:26:32 +01:00
$panierNewContent->panier_content_panierId = $panier->panier_id;
$panierNewContent->panier_content_couponId = $cp->coupon_id;
$panierNewContent->panier_content_quantity = $quantity;
$panierNewContent->save();
}
}
2021-04-27 17:29:47 +02:00
} else {
$log->log_params = json_encode(['refcamp' => $request->refcamp, 'refadherent' => $request->refadherent, 'refsite' => $request->refsite, 'nom' => $request->nom, 'prenom' => $request->prenom, 'email' => $request->email, 'code postal' => $request->code_postal, 'commune' => $request->commune, 'panier' => $request->panier]);
2019-02-28 17:05:24 +01:00
$log->log_statut = 'NOK';
$log->log_message = "Le coupon ne correspond pas aux coupons liés a la campagne";
$log->save();
2021-04-27 17:29:47 +02:00
2019-02-28 17:05:24 +01:00
return Response::json([
'Statut' => 'KO',
'Code' => 110,
'Message' => 'Le coupon ne correspond pas aux coupons liés a la campagne'
]);
2019-02-20 16:26:32 +01:00
}
}
// MAJ du status du panier
$qty = $panier->panierContents()->sum('panier_content_quantity');
2021-04-27 17:29:47 +02:00
if ($qty == 0) {
2019-02-20 16:26:32 +01:00
$panier_pending = 0;
2021-04-27 17:29:47 +02:00
} else {
2019-02-20 16:26:32 +01:00
$panier_pending = 1;
}
2021-04-27 17:29:47 +02:00
$panier->panier_pending = $panier_pending;
2019-02-20 16:26:32 +01:00
$panier->save();
$campagne = Campagne::where(['camp_id' => $panier->panier_campId])->first();
//var_dump($panier->panier_campId);
$log->log_refsite = $campagne->camp_siteId;
$log->log_contactId = $panier->panier_contactId;
$log->log_params = json_encode(['refcamp' => $panier->panier_campId, 'basketid' => $basketid, 'panier' => $panier]);
$log->log_statut = 'OK';
$log->log_message = json_encode(['Message' => 'Le panier a bien été créé/mis à jour', 'Coupons' => $panierCoupons], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$log->save();
2019-02-28 17:05:24 +01:00
$contact = Contact::where(['contact_id' => $panier->panier_contactId])->first();
$site = Site::where(['site_id' => $campagne->camp_siteId])->first();
if (isset($contact->contact_nom) && isset($site->site_nom)) {
2021-04-27 17:29:47 +02:00
if ($panier->panier_pending == 1) {
2019-02-28 17:05:24 +01:00
$encours = "oui";
2021-04-27 17:29:47 +02:00
} else {
2019-02-28 17:05:24 +01:00
$encours = "non";
}
$response = new \stdClass();
2021-04-27 17:29:47 +02:00
$response->Site = $site->site_nom . " (" . $site->site_id . ")";
$response->Campagne = $campagne->camp_name . " (" . $campagne->camp_id . ")";
2019-02-28 17:05:24 +01:00
$response->Panier = $panier->panier_ref;
$response->DateCreation = date("d/m/Y", strtotime($panier->panier_createdate));
$response->EnCours = $encours;
$response->Nom = $contact->contact_nom;
$response->Prenom = $contact->contact_prenom;
$response->Email = $contact->contact_email;
$response->CodePostal = $contact->contact_cp;
$response->Commune = $contact->contact_ville;
2021-04-27 17:29:47 +02:00
2019-02-28 17:05:24 +01:00
$allPanierContent = $panier->panierContents()->get();
2021-04-27 17:29:47 +02:00
foreach ($allPanierContent as $panierContent) {
2019-02-28 17:05:24 +01:00
$panCont = [];
$coupon = Coupon::where(['coupon_id' => $panierContent->panier_content_couponId])->first();
2021-04-27 17:29:47 +02:00
if ($coupon) {
array_push($panCont, $coupon->coupon_code . " - " . $coupon->coupon_name . " (x" . $panierContent->panier_content_quantity . ")");
2019-02-28 17:05:24 +01:00
}
}
2021-04-27 17:29:47 +02:00
$response->panierContent = $panCont;
2019-02-28 17:05:24 +01:00
return Response::json([
'Statut' => 'OK',
'Panier' => $response
]);
} else {
return Response::json([
'Statut' => 'OK',
'Panier' => $panier
]);
}
2021-04-27 17:29:47 +02:00
} else {
2019-02-20 16:26:32 +01:00
$log->log_params = json_encode(['basketid' => $basketid, 'panier' => $panier]);
$log->log_statut = 'NOK';
$log->log_message = "Le panier n'existe pas";
$log->save();
return Response::json([
'Statut' => 'KO',
'Code' => 106,
'Message' => 'Le panier n\'existe pas'
]);
}
} else {
$log->log_params = json_encode(['basketid' => $basketid, 'panier' => $panier]);
$log->log_statut = 'NOK';
$log->log_message = "Le panier n'existe pas";
$log->save();
return Response::json([
'Statut' => 'KO',
'Code' => 106,
'Message' => 'Le panier n\'existe pas'
]);
}
} else {
// ---- Contrôle des paramètres
2021-04-27 17:29:47 +02:00
$error = false;
if (isset($request->refcamp) && $request->refcamp != "") {
$refcamp = $request->refcamp;
$campagne = Campagne::where(['camp_id' => $refcamp])->first();
if (!$campagne || $campagne->camp_published == 0 || ($campagne->camp_type != 1 && $campagne->camp_type != 3)) {
$log->log_params = json_encode(['refcamp' => $refcamp]);
$log->log_statut = 'NOK';
$log->log_message = "La campagne n'existe pas ou n'est pas un panier";
$log->save();
2019-02-20 16:26:32 +01:00
2021-04-27 17:29:47 +02:00
return Response::json([
2019-01-18 09:00:42 +01:00
'Statut' => 'KO',
'Code' => 103,
'Message' => 'La campagne n\'existe pas ou n\'est pas un panier'
2021-04-27 17:29:47 +02:00
]);
}
} else {
$error = true;
2019-01-18 09:00:42 +01:00
}
2021-04-27 17:29:47 +02:00
if (isset($request->refadherent) && $request->refadherent != "") {
$refadherent = $request->refadherent;
} else {
$error = true;
}
if (isset($request->nom) && $request->nom != "") {
$nom = $request->nom;
} else {
$error = true;
}
if (isset($request->refsite) && $request->refsite != "") {
$refsite = $request->refsite;
if ($refsite != $campagne->camp_siteId) {
return Response::json([
'Statut' => 'KO',
'Code' => 103,
'Message' => 'La campagne ne correspond à aucune campagnes du site'
2021-04-27 17:29:47 +02:00
]);
}
} else {
$error = true;
}
if (isset($request->prenom) && $request->prenom != "") {
$prenom = $request->prenom;
} else {
$error = true;
}
if (isset($request->email) && $request->email != "" && filter_var($request->email, FILTER_VALIDATE_EMAIL)) {
$email = $request->email;
} else {
$error = true;
}
2019-02-13 16:37:14 +01:00
2021-04-27 17:29:47 +02:00
if (isset($request->code_postal) && $request->code_postal != "") {
$code_postal = $request->code_postal;
2019-02-28 13:58:25 +01:00
} else {
$error = true;
}
2021-04-27 17:29:47 +02:00
if (isset($request->commune) && $request->commune != "") {
$commune = $request->commune;
} else {
$error = true;
}
if (isset($request->pays) && $request->pays != "") {
if (filter_var($request->pays, FILTER_VALIDATE_INT)) {
$pays = $request->pays;
} else {
$error = true;
2019-01-18 09:00:42 +01:00
}
2021-04-27 17:29:47 +02:00
} else {
$pays = 250;
2019-01-18 09:00:42 +01:00
}
2021-04-27 17:29:47 +02:00
if (isset($request->panier) && $request->panier != "") {
$panier = $request->panier;
$panierTab = explode(",", $panier);
$coupons = array();
foreach ($panierTab as $panier) {
$pos = strpos($panier, ")");
if ($pos === false) {
$error = true;
}
$quantity = substr($panier, 0, $pos);
$quantity = str_replace("(", "", $quantity);
$couponCode = substr($panier, $pos + 1);
if (is_numeric($quantity) && is_numeric($couponCode) && $quantity > 0) {
if (isset($coupons[$couponCode])) {
$coupons[$couponCode] = $coupons[$couponCode] + $quantity;
} else {
$coupons[$couponCode] = $quantity;
}
}
}
if (sizeof($coupons) == 0) {
die();
}
} else {
$error = true;
2019-01-18 09:00:42 +01:00
}
2019-02-20 16:26:32 +01:00
//---- Préparation message de log avec coupons
2021-04-27 17:29:47 +02:00
if (isset($coupons)) {
$panierCoupons = array();
foreach ($coupons as $code => $qty) {
array_push($panierCoupons, ['cp_code' => $code, 'qty' => $qty]);
}
}
2019-02-20 16:26:32 +01:00
2021-04-27 17:29:47 +02:00
if ($error) {
$log->log_params = json_encode(['refcamp' => $request->refcamp, 'refadherent' => $request->refadherent, 'refsite' => $request->refsite, 'nom' => $request->nom, 'prenom' => $request->prenom, 'email' => $request->email, 'code postal' => $request->code_postal, 'commune' => $request->commune, 'panier' => $request->panier]);
$log->log_statut = 'NOK';
$log->log_message = "Des paramètres de la requête sont mal renseignés ou non présents";
$log->save();
return Response::json([
2019-01-18 09:00:42 +01:00
'Statut' => 'KO',
'Code' => 101,
'Message' => 'Des paramètres de la requête sont mal renseignés ou non présents'
2021-04-27 17:29:47 +02:00
]);
}
2019-01-18 09:00:42 +01:00
// ---- Ajout/MAJ du contact
2021-04-27 17:29:47 +02:00
$contact = Contact::where(['contact_siteId' => $refsite, 'contact_ref' => $refadherent])->first();
if ($contact) {
// ---- Mise à jour du contact
2019-01-18 09:00:42 +01:00
$contact->contact_prenom = $prenom;
2021-04-27 17:29:47 +02:00
$contact->contact_nom = $nom;
2019-01-18 09:00:42 +01:00
$contact->contact_email = $email;
$contact->contact_cp = $code_postal;
$contact->contact_ville = $commune;
$contact->contact_country = $pays;
2021-04-27 17:29:47 +02:00
$contact->contact_updatedate = date('Y-m-d H:i:s');
2019-01-18 09:00:42 +01:00
} else {
2021-04-27 17:29:47 +02:00
// ---- Création du contact
$site = Site::where('site_id', $refsite)->first();
if ($site) {
$contact = new Contact();
$contact->contact_nom = $nom;
$contact->contact_prenom = $prenom;
$contact->contact_ref = $refadherent;
$contact->contact_email = $email;
$contact->contact_cp = $code_postal;
$contact->contact_ville = $commune;
$contact->contact_country = $pays;
$contact->contact_siteId = $refsite;
$contact->contact_hash = strtoupper(md5(uniqid(time(), true)));
} else {
$log->log_params = json_encode(['refcamp' => $request->refcamp, 'refadherent' => $request->refadherent, 'refsite' => $request->refsite, 'nom' => $request->nom, 'prenom' => $request->prenom, 'email' => $request->email, 'code postal' => $request->code_postal, 'commune' => $request->commune, 'panier' => $request->panier]);
$log->log_statut = 'NOK';
$log->log_message = "Le site spécifié n'existe pas";
$log->save();
2019-02-20 16:26:32 +01:00
2021-04-27 17:29:47 +02:00
return Response::json([
2019-01-18 09:00:42 +01:00
'Statut' => 'KO',
'Code' => 102,
'Message' => 'Le site spécifié n\'existe pas'
2021-04-27 17:29:47 +02:00
]);
}
2019-01-18 09:00:42 +01:00
}
2021-04-27 17:29:47 +02:00
if (!$contact->save()) {
$log->log_params = json_encode(['refcamp' => $request->refcamp, 'refadherent' => $request->refadherent, 'refsite' => $request->refsite, 'nom' => $request->nom, 'prenom' => $request->prenom, 'email' => $request->email, 'code postal' => $request->code_postal, 'commune' => $request->commune, 'panier' => $request->panier]);
$log->log_statut = 'NOK';
$log->log_message = "Une erreur est survenue";
$log->save();
return Response::json([
2019-02-20 16:26:32 +01:00
'Statut' => 'KO',
'Code' => 109,
'Message' => 'Une erreur est survenue'
2021-04-27 17:29:47 +02:00
]);
}
2019-01-18 09:00:42 +01:00
// ---- Ajout/MAJ du panier
2021-04-27 17:29:47 +02:00
$panier = Panier::where(['panier_campId' => $refcamp, 'panier_contactId' => $contact->contact_id, 'panier_pending' => 1])->first();
if ($panier) {
foreach ($coupons as $couponCode => $quantity) {
$camp = Campagne::where('camp_id', $panier->panier_campId)->first();
$cp = $camp->coupons()->where('coupon_code', $couponCode)->first();
if ($cp) {
$panierContent = PanierContent::where(['panier_content_panierId' => $panier->panier_id, 'panier_content_couponId' => $cp->coupon_id])->first();
if ($panierContent) {
//$quantity += $panierContent->panier_content_quantity;
$panierContent->panier_content_quantity = $quantity + $panierContent->panier_content_quantity;
// MAJ du contenu
$panierContent->save();
//$panierContent->update(['panier_content_quantity', $quantity]);
} else {
// Ajout du contenu
if ($quantity > 0) {
$panierNewContent = new PanierContent();
$panierNewContent->panier_content_panierId = $panier->panier_id;
$panierNewContent->panier_content_couponId = $cp->coupon_id;
$panierNewContent->panier_content_quantity = $quantity;
$panierNewContent->save();
}
}
}
}
} else {
// Ajout du panier et du contenu
$panierRef = strtoupper(md5(uniqid(time(), true)));
$panier = new Panier();
$panier->panier_ref = $panierRef;
$panier->panier_campId = $refcamp;
$panier->panier_contactId = $contact->contact_id;
$panier->panier_createdate = date('Y-m-d H:i:s');
$panier->save();
foreach ($coupons as $couponCode => $quantity) {
$cp = $campagne->coupons()->where('coupon_code', $couponCode)->first();
if ($cp) {
2019-01-18 09:00:42 +01:00
// Ajout du contenu
2021-04-27 17:29:47 +02:00
if ($quantity > 0) {
$panierNewContent = new PanierContent();
2019-01-18 09:00:42 +01:00
$panierNewContent->panier_content_panierId = $panier->panier_id;
$panierNewContent->panier_content_couponId = $cp->coupon_id;
$panierNewContent->panier_content_quantity = $quantity;
$panierNewContent->save();
}
}
}
}
2019-02-20 16:26:32 +01:00
2021-04-27 17:29:47 +02:00
$log->log_params = json_encode(['refcamp' => $request->refcamp, 'refadherent' => $request->refadherent, 'refsite' => $request->refsite, 'nom' => $request->nom, 'prenom' => $request->prenom, 'email' => $request->email, 'code postal' => $request->code_postal, 'commune' => $request->commune, 'panier' => $request->panier]);
$log->log_contactId = $panier->panier_contactId;
$log->log_refsite = $request->refsite;
$log->log_statut = 'OK';
$log->log_message = json_encode(['Message' => 'Le panier a bien été créé/mis à jour', 'Coupons' => $panierCoupons], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$log->save();
2019-02-20 16:26:32 +01:00
2019-02-28 17:05:24 +01:00
$contact = Contact::where(['contact_id' => $panier->panier_contactId])->first();
$site = Site::where(['site_id' => $campagne->camp_siteId])->first();
2021-04-27 17:29:47 +02:00
2019-02-28 17:05:24 +01:00
if (isset($contact->contact_nom) && isset($site->site_nom)) {
2021-04-27 17:29:47 +02:00
if ($panier->panier_pending == 1) {
2019-02-28 17:05:24 +01:00
$encours = "oui";
2021-04-27 17:29:47 +02:00
} else {
2019-02-28 17:05:24 +01:00
$encours = "non";
}
$response = new \stdClass();
2021-04-27 17:29:47 +02:00
$response->Site = $site->site_nom . " (" . $site->site_id . ")";
$response->Campagne = $campagne->camp_name . " (" . $campagne->camp_id . ")";
2019-02-28 17:05:24 +01:00
$response->Panier = $panier->panier_ref;
$response->DateCreation = date("d/m/Y", strtotime($panier->panier_createdate));
$response->EnCours = $encours;
$response->Nom = $contact->contact_nom;
$response->Prenom = $contact->contact_prenom;
$response->Email = $contact->contact_email;
$response->CodePostal = $contact->contact_cp;
$response->Commune = $contact->contact_ville;
$allPanierContent = $panier->panierContents()->get();
$panCont = [];
2021-04-27 17:29:47 +02:00
foreach ($allPanierContent as $panierContent) {
2019-02-28 17:05:24 +01:00
$coupon = Coupon::where(['coupon_id' => $panierContent->panier_content_couponId])->first();
2021-04-27 17:29:47 +02:00
if ($coupon) {
array_push($panCont, $coupon->coupon_code . " - " . $coupon->coupon_name . " (x" . $panierContent->panier_content_quantity . ")");
2019-02-28 17:05:24 +01:00
}
}
2021-04-27 17:29:47 +02:00
$response->panierContent = $panCont;
2019-02-28 17:05:24 +01:00
return Response::json([
'Statut' => 'OK',
'Panier' => $response
]);
} else {
return Response::json([
'Statut' => 'OK',
'Panier' => $panier
]);
}
2019-01-18 09:00:42 +01:00
}
2019-01-15 14:17:15 +01:00
}
public function showBasket(Request $request)
{
2019-01-29 12:26:00 +01:00
// ---- Contrôle des paramètres
2021-04-27 17:29:47 +02:00
if (isset($request->basketid) && $request->basketid != "") {
$basketid = $request->basketid;
} else {
return Response::json([
'Statut' => 'KO',
'Code' => 106,
'Message' => 'Le panier n\'existe pas'
]);
}
2019-01-29 12:26:00 +01:00
2021-04-27 17:29:47 +02:00
$panier = Panier::where('panier_ref', $basketid)->first();
if (!$panier) {
return Response::json([
'Statut' => 'KO',
'Code' => 106,
'Message' => 'Le panier n\'existe pas'
]);
}
2019-01-29 12:26:00 +01:00
2021-04-27 17:29:47 +02:00
if ($panier->panier_pending == 1) {
$encours = "oui";
} else {
$encours = "non";
}
2019-01-29 12:26:00 +01:00
$contact = $panier->contact()->where(['contact_id' => $panier->panier_contactId])->first();
2021-04-27 17:29:47 +02:00
if (!$contact) {
return Response::json([
'Statut' => 'KO',
'Code' => 104,
'Message' => 'Le contact n\'existe pas'
]);
}
2019-01-29 12:26:00 +01:00
$campagne = $panier->campagne()->where(['camp_id' => $panier->panier_campId])->first();
2021-04-27 17:29:47 +02:00
if (!$campagne) {
return Response::json([
'Statut' => 'KO',
'Code' => 103,
'Message' => 'La campagne n\'existe pas'
]);
}
2019-01-29 12:26:00 +01:00
2021-04-27 17:29:47 +02:00
$site = $campagne->site()->first();
if ($site === null) {
return Response::json([
'Statut' => 'KO',
'Code' => 102,
'Message' => 'Le site n\'existe pas'
]);
}
2019-01-29 12:26:00 +01:00
$allPanierContent = $panier->panierContents()->get();
2021-04-27 17:29:47 +02:00
if ($allPanierContent->count() == 0) {
return Response::json([
'Statut' => 'KO',
'Code' => 108,
'Message' => 'Le panier n\'a pas de coupons liés'
]);
}
2019-01-29 12:26:00 +01:00
$response = new \stdClass();
2021-04-27 17:29:47 +02:00
$response->Site = $site->site_nom . " (" . $site->site_id . ")";
$response->Campagne = $campagne->camp_name . " (" . $campagne->camp_id . ")";
2019-01-29 12:26:00 +01:00
$response->Panier = $panier->panier_ref;
$response->DateCreation = date("d/m/Y", strtotime($panier->panier_createdate));
$response->EnCours = $encours;
$response->Nom = $contact->contact_nom;
$response->Prenom = $contact->contact_prenom;
$response->Email = $contact->contact_email;
$response->CodePostal = $contact->contact_cp;
$response->Commune = $contact->contact_ville;
$panCont = [];
2021-04-27 17:29:47 +02:00
foreach ($allPanierContent as $panierContent) {
$coupon = Coupon::where(['coupon_id' => $panierContent->panier_content_couponId])->first();
if ($coupon) {
array_push($panCont, $coupon->coupon_code . " - " . $coupon->coupon_name . " (x" . $panierContent->panier_content_quantity . ")");
2019-01-29 12:26:00 +01:00
}
2021-04-27 17:29:47 +02:00
}
2019-01-29 12:26:00 +01:00
2021-04-27 17:29:47 +02:00
$response->panierContent = $panCont;
2019-01-29 12:26:00 +01:00
return Response::json([
'Statut' => 'OK',
'Informations Panier' => $response
]);
2019-01-15 14:17:15 +01:00
}
}