314 lines
12 KiB
PHP
314 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Panier;
|
|
use App\Campagne;
|
|
use App\Coupon;
|
|
use App\PanierContent;
|
|
|
|
class PanierController extends Controller
|
|
{
|
|
public function showTemplate(Request $request)
|
|
{
|
|
$panier = Panier::find($request->query('baskedid'));
|
|
|
|
session()->put('panier', $panier);
|
|
|
|
var_dump($request->all());
|
|
return;
|
|
}
|
|
|
|
public function createBasket(Request $request)
|
|
{
|
|
// ---- Contrôle des paramètres
|
|
$error = false;
|
|
if(isset($request->refcamp) && $request->refcamp != ""){
|
|
$refcamp = $request->refcamp;
|
|
$campagne = Campagne::where(['camp_id' => $refcamp, 'camp_type' => 1])->first();
|
|
if(!$campagne || $campagne->camp_published == 0 ){
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 103,
|
|
'Message' => 'La campagne n\'existe pas'
|
|
]);
|
|
}
|
|
}else{
|
|
$error = true;
|
|
}
|
|
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;
|
|
}else{
|
|
$error = true;
|
|
}
|
|
if(isset($request->prenom) && $request->prenom != ""){
|
|
$prenom = $request->prenom;
|
|
}else{
|
|
$error = true;
|
|
}
|
|
if(isset($request->email) && $request->email != ""){
|
|
$email = $request->email;
|
|
}else{
|
|
$error = true;
|
|
}
|
|
if(isset($request->date_naissance) && $request->date_naissance != ""){
|
|
$date_naissance = $request->date_naissance;
|
|
}else{
|
|
$date_naissance = "";
|
|
}
|
|
if(isset($request->code_postal) && $request->code_postal != ""){
|
|
$code_postal = $request->code_postal;
|
|
}else{
|
|
$error = true;
|
|
}
|
|
if(isset($request->commune) && $request->commune != ""){
|
|
$commune = $request->commune;
|
|
}else{
|
|
$error = true;
|
|
}
|
|
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;
|
|
}
|
|
|
|
if($error){
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 101,
|
|
'Message' => 'Des paramètres de la requête sont mal renseignés ou non présents'
|
|
]);
|
|
}
|
|
|
|
// ---- Ajout/MAJ du contact
|
|
$contact = Contact::where(['contact_siteId' => $refsite, 'contact_ref' => $refadherent])->first();
|
|
if($contact){
|
|
// ---- Mise à jour du contact
|
|
$contact->contact_prenom = $prenom;
|
|
$contact->contact_nom = $nom;
|
|
$contact->contact_email = $email;
|
|
$contact->contact_cp = $code_postal;
|
|
$contact->contact_ville = $commune;
|
|
$contact->contact_country = $pays;
|
|
$contact->contact_updatedate = date('Y-m-d H:i:s');
|
|
} else {
|
|
// ---- 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;
|
|
} else {
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 102,
|
|
'Message' => 'Le site spécifié n\'existe pas'
|
|
]);
|
|
}
|
|
}
|
|
$contact->save();
|
|
|
|
// ---- Ajout/MAJ du panier
|
|
$panier = Panier::where(['panier_campId' => $refcamp, 'panier_contactId' => $contact->contact_id, 'panier_pending' => 1]);
|
|
if($panier){
|
|
foreach($coupons as $couponCode => $quantity){
|
|
$cp = Campagne::where('camp_id', $panier->panier_campId)->coupons()->where('coupon_code', $couponCode)->first();
|
|
if($cp){
|
|
$panierContent = PanierContent::where(['panier_content_panierId' => $panier->panier_id, 'panier_content_couponId' => $cp->coupon_id]);
|
|
if($panierContent){
|
|
$quantity += $panierContent->panier_content_quantity;
|
|
// MAJ du contenu
|
|
$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
|
|
$panier = panier::addPanier($refcamp, $contactId);
|
|
|
|
$panierRef = strtoupper(md5(uniqid(time(), true)));
|
|
$panier = new Panier;
|
|
$panier->panier_ref = $panierRef;
|
|
$panier->panier_campId = $refcamp;
|
|
$panier->panier_contactId = $contactId;
|
|
$panier->save();
|
|
|
|
foreach($coupons as $couponCode => $quantity){
|
|
$cp = Campagne::where('camp_id', $panier->panier_campId)->coupons()->where('coupon_code', $couponCode)->first();
|
|
if($cp){
|
|
// 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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return Response::json([
|
|
'Statut' => 'OK',
|
|
'Panier' => $panier
|
|
]);
|
|
}
|
|
|
|
public function updateBasket(Request $request)
|
|
{
|
|
if(isset($request->basketid)){
|
|
$basketid = $request->basketid;
|
|
if($basketid != ''){
|
|
$panier = Panier::where('panier_ref', $basketid)->first();
|
|
if(!$panier){
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 106,
|
|
'Message' => 'Le panier n\'existe pas'
|
|
]);
|
|
}
|
|
|
|
if(isset($request->panier) && $request->panier != ""){
|
|
$panier = $request->panier;
|
|
$panierTab = explode(",", $panier);
|
|
$coupons = array();
|
|
foreach($panierTab as $panier){
|
|
$pos = strpos($panier, ")");
|
|
if($pos === false){
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 101,
|
|
'Message' => 'Des paramètres de la requête sont mal renseignés ou non présents'
|
|
]);
|
|
}
|
|
$quantity = substr($panier, 0, $pos);
|
|
$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;
|
|
}
|
|
}
|
|
}
|
|
if(sizeOf($coupons)== 0){
|
|
die();
|
|
}
|
|
} else {
|
|
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();
|
|
if($panier){
|
|
foreach($coupons as $couponCode => $quantity){
|
|
$cp = Campagne::where('camp_id', $panier->panier_campId)->coupons()->where('coupon_code', $couponCode)->first();
|
|
if($cp){
|
|
$panierContent = PanierContent::where(['panier_content_panierId' => $panier->panier_id, 'panier_content_couponId' => $cp->coupon_id]);
|
|
if($panierContent){
|
|
$quantity += $panierContent->panier_content_quantity;
|
|
if($quantity <= 0) $quantity = 0;
|
|
// MAJ du contenu
|
|
$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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// MAJ du status du panier
|
|
$qty = $panier->panierContents()->sum('panier_content_quantity');
|
|
if($qty == 0){
|
|
$panier_pending = 0;
|
|
}else{
|
|
$panier_pending = 1;
|
|
}
|
|
$panier->update('panier_pending', $panier_pending);
|
|
return Response::json([
|
|
'Statut' => 'OK',
|
|
'Panier' => $panier
|
|
]);
|
|
}else{
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 106,
|
|
'Message' => 'Le panier n\'existe pas'
|
|
]);
|
|
}
|
|
|
|
} else {
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 106,
|
|
'Message' => 'Le panier n\'existe pas'
|
|
]);
|
|
}
|
|
} else {
|
|
return $this->createBasket($request);
|
|
}
|
|
}
|
|
|
|
public function showBasket(Request $request)
|
|
{
|
|
var_dump($request->all());
|
|
return;
|
|
}
|
|
}
|