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

642 lines
25 KiB
PHP
Raw Normal View History

2019-01-15 14:17:15 +01:00
<?php
namespace App\Http\Controllers;
2021-04-27 17:29:47 +02:00
2019-01-15 14:17:15 +01:00
use App\Campagne;
use App\Coupon;
2019-02-13 16:37:14 +01:00
use App\Contact;
use App\Panier;
use App\PanierContent;
2019-03-21 09:22:01 +01:00
use App\Statistique;
2019-02-13 16:37:14 +01:00
use App\Mur;
2019-02-20 16:26:32 +01:00
use App\Log;
2019-01-15 14:17:15 +01:00
use Illuminate\Http\Request;
2019-02-13 16:37:14 +01:00
use App\Traits\GenerateBR;
use App\Traits\CouponTrait;
2021-04-27 17:29:47 +02:00
use App\Http\Controllers;
2019-02-13 16:37:14 +01:00
use Carbon\Carbon;
2019-01-15 14:17:15 +01:00
class AjaxController extends Controller
{
2019-02-13 16:37:14 +01:00
use GenerateBR;
use CouponTrait;
// ---- Ajoute des coupons à une campagne
2021-04-27 17:29:47 +02:00
public function add_coupons_ajax(Request $request)
{
2025-04-03 09:30:16 +02:00
$camp_id = $request->input('campid');
2019-01-15 14:17:15 +01:00
$campagne = Campagne::where('camp_id', $camp_id)->first();
2025-04-03 09:30:16 +02:00
$ids = $request->input('ids');
2019-01-15 14:17:15 +01:00
$coupons = Coupon::find($ids);
$campagne->coupons()->attach($coupons);
return json_encode($coupons);
}
2019-02-13 16:37:14 +01:00
// ---- Efface des coupons d'une campagne
2021-04-27 17:29:47 +02:00
public function remove_coupons_ajax(Request $request)
{
2025-04-03 09:30:16 +02:00
$camp_id = $request->input('campid');
$campagne = Campagne::where('camp_id', $camp_id)->first();
2021-04-27 17:29:47 +02:00
$coupon_id = $request->input('couponid');
2025-04-03 09:30:16 +02:00
$coupon = Coupon::find($coupon_id);
2019-01-15 14:17:15 +01:00
$campagne->coupons()->detach($coupon_id);
return json_encode($coupon);
}
2019-02-13 16:37:14 +01:00
// ---- Dépublie un coupon
2021-04-27 17:29:47 +02:00
public function unpublish_coupons_ajax(Request $request)
{
2025-04-03 09:30:16 +02:00
$coupon_id = $request->input('couponid');
$coupon = Coupon::find($coupon_id);
2019-02-13 16:37:14 +01:00
$coupon->coupon_published = 0;
$coupon->save();
return json_encode($coupon);
}
// ---- Publie un coupon
2021-04-27 17:29:47 +02:00
public function publish_coupons_ajax(Request $request)
{
2025-04-03 09:30:16 +02:00
$coupon_id = $request->input('couponid');
$coupon = Coupon::find($coupon_id);
2019-02-13 16:37:14 +01:00
$coupon->coupon_published = 1;
$coupon->save();
return json_encode($coupon);
}
// ---- Dépublie une campagne
2021-04-27 17:29:47 +02:00
public function unpublish_camp_ajax(Request $request)
{
2025-04-03 09:30:16 +02:00
$campid = $request->input('campid');
$campagne = Campagne::find($campid);
$campagne->camp_published = 0;
$campagne->save();
return json_encode($campagne);
}
// ---- Publie une campagne
2021-04-27 17:29:47 +02:00
public function publish_camp_ajax(Request $request)
{
2025-04-03 09:30:16 +02:00
$campid = $request->input('campid');
$campagne = Campagne::find($campid);
$campagne->camp_published = 1;
$campagne->save();
return json_encode($campagne);
}
2019-02-13 16:37:14 +01:00
// ---- Impression des BR
2021-04-27 17:29:47 +02:00
public function impression_ajax(Request $request)
{
2019-02-20 16:26:32 +01:00
2021-04-27 17:29:47 +02:00
if ($request->session()->has('coupons')) {
$couponsToPrint = $request->session()->get('coupons');
}
if ($request->session()->has('campagne')) {
$campagne = $request->session()->get('campagne');
2019-02-13 16:37:14 +01:00
} else {
die();
}
2021-04-27 17:29:47 +02:00
if ($request->session()->has('contact')) {
$contact = $request->session()->get('contact');
2019-02-13 16:37:14 +01:00
} else {
die();
}
2021-04-27 17:29:47 +02:00
if ($request->session()->has('panier')) {
$panier = $request->session()->get('panier');
2019-02-13 16:37:14 +01:00
}
2019-02-20 16:26:32 +01:00
2021-04-27 17:29:47 +02:00
if ($contact->contact_ref == "DEMO") {
$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
2025-04-03 09:30:16 +02:00
$log = new Log();
$log->log_action = config('constants.impression');
$log->log_source = $source;
2019-02-20 16:26:32 +01:00
$log->log_contactId = $contact->contact_id;
2025-04-03 09:30:16 +02:00
$log->log_refsite = $campagne->camp_siteId;
$log->log_params = json_encode(['refcamp' => $campagne->camp_id]);
2019-02-20 16:26:32 +01:00
$couponsPrinted = array();
2025-04-03 09:30:16 +02:00
$coupons = '<TABLE width="100%" align="center" cellpadding="0" cellspacing="0" border="0">';
2021-04-27 17:29:47 +02:00
$coupons .= '<TR><TD width="100%" align="center"><div>';
2019-02-13 16:37:14 +01:00
2025-04-03 09:30:16 +02:00
if ($campagne->camp_type == 0) {
// ---- POUR LES MURS
2021-04-27 17:29:47 +02:00
if (!isset($couponsToPrint)) {
die();
}
2019-02-13 16:37:14 +01:00
$nbCoupon = 0;
2021-04-27 17:29:47 +02:00
foreach ($couponsToPrint as $key => $coupon_code) {
2019-02-13 16:37:14 +01:00
$cp = Coupon::where(['coupon_code' => $coupon_code])->first();
2021-04-27 17:29:47 +02:00
if ($this->isAvailable($cp, $campagne, $contact) && $this->isAvailableToPrint($cp, $campagne)) {
2019-02-20 16:26:32 +01:00
array_push($couponsPrinted, ['cp_code' => $cp->coupon_code, 'qty' => 1]);
2019-02-13 16:37:14 +01:00
$imgCoupon = $this->printCp($cp, $contact);
2025-04-03 09:30:16 +02:00
$coupons .= '<img src="' . $imgCoupon . '" border="0" style="display:block;width:165mm;"/>';
2019-02-13 16:37:14 +01:00
$nbCoupon++;
2021-04-27 17:29:47 +02:00
if ($nbCoupon % 3 == 0) {
$coupons .= '</div><div>';
}
2019-02-13 16:37:14 +01:00
2025-04-03 09:30:16 +02:00
// ----- mémorise l'impression
$mur = new Mur();
$mur->mur_campid = $campagne->camp_id;
2019-02-13 16:37:14 +01:00
$mur->mur_contactId = $contact->contact_id;
2025-04-03 09:30:16 +02:00
$mur->mur_couponId = $cp->coupon_id;
2019-02-13 16:37:14 +01:00
$mur->save();
2025-04-03 09:30:16 +02:00
// Sauvegarde de stat
$stat = new Statistique();
$stat->stat_campId = $campagne->camp_id;
$stat->stat_couponId = $cp->coupon_id;
$stat->stat_couponPrinted = 1;
$stat->save();
2025-04-03 09:30:16 +02:00
}//end if
}//end foreach
} elseif ($campagne->camp_type == 1) {
// ---- POUR LES PANIERS
2021-04-27 17:29:47 +02:00
if (!isset($panier)) {
die();
}
2019-02-13 16:37:14 +01:00
$allPanierContent = $panier->panierContents()->get();
2021-04-27 17:29:47 +02:00
if (count($allPanierContent) == 0) {
die("error");
}
2019-02-13 16:37:14 +01:00
$nbCoupon = 0;
2021-04-27 17:29:47 +02:00
2019-02-13 16:37:14 +01:00
foreach ($allPanierContent as $panierContent) {
2021-04-27 17:29:47 +02:00
$cp = Coupon::where(['coupon_id' => $panierContent->panier_content_couponId])->first();
2025-04-03 09:30:16 +02:00
if ($cp && $this->isAvailableToPrint($cp, $campagne)) {
2019-02-13 16:37:14 +01:00
$imgCoupon = $this->printCp($cp, $contact);
// ---- Création du html du contenu à imprimer
2021-04-27 17:29:47 +02:00
if ($campagne->camp_limitedPrinting == 0) {
for ($i = 0; $i < $panierContent->panier_content_quantity; $i++) {
$coupons .= '<img src="' . $imgCoupon . '" border="0" style="display:block;width:165mm;"/>';
2019-02-13 16:37:14 +01:00
$nbCoupon++;
2021-04-27 17:29:47 +02:00
if ($nbCoupon % 3 == 0) {
$coupons .= '</div><div>';
}
2019-02-13 16:37:14 +01:00
}
$newQtyAvailable = 0;
2025-04-03 09:30:16 +02:00
$newQtyPrinted = $panierContent->panier_content_quantity + $panierContent->panier_content_printed;
2019-02-20 16:26:32 +01:00
array_push($couponsPrinted, ['cp_code' => $cp->coupon_code, 'qty' => $panierContent->panier_content_quantity]);
2025-04-03 09:30:16 +02:00
// Sauvegarde de stat
$stat = new Statistique();
$stat->stat_campId = $campagne->camp_id;
$stat->stat_couponId = $cp->coupon_id;
$stat->stat_couponPrinted = $panierContent->panier_content_quantity;
$stat->save();
2021-04-27 17:29:47 +02:00
} else {
if ($panierContent->panier_content_quantity > 0) {
$coupons .= '<img src="' . $imgCoupon . '" border="0" style="display:block;width:165mm;"/>';
2019-02-13 16:37:14 +01:00
$nbCoupon++;
2021-04-27 17:29:47 +02:00
if ($nbCoupon % 3 == 0) {
$coupons .= '</div><div>';
}
$newQtyAvailable = $panierContent->panier_content_quantity - 1;
2025-04-03 09:30:16 +02:00
$newQtyPrinted = $panierContent->panier_content_printed + 1;
2019-02-20 16:26:32 +01:00
array_push($couponsPrinted, ['cp_code' => $cp->coupon_code, 'qty' => 1]);
2021-04-27 17:29:47 +02:00
2025-04-03 09:30:16 +02:00
// Sauvegarde de stat
$stat = new Statistique();
$stat->stat_campId = $campagne->camp_id;
$stat->stat_couponId = $cp->coupon_id;
$stat->stat_couponPrinted = 1;
$stat->save();
2019-02-13 16:37:14 +01:00
}
2025-04-03 09:30:16 +02:00
}//end if
2019-02-13 16:37:14 +01:00
// ---- Mémorise l'impression
$panierContent->panier_content_quantity = $newQtyAvailable;
2025-04-03 09:30:16 +02:00
$panierContent->panier_content_printed = $newQtyPrinted;
2019-02-13 16:37:14 +01:00
$panierContent->save();
2025-04-03 09:30:16 +02:00
}//end if
}//end foreach
2019-02-13 16:37:14 +01:00
// ---- Update du status du panier
$qty = $panier->panierContents()->sum('panier_content_quantity');
2021-04-27 17:29:47 +02:00
if ($qty == 0) {
2019-02-13 16:37:14 +01:00
$panier->panier_pending = 0;
2021-04-27 17:29:47 +02:00
} else {
2019-02-13 16:37:14 +01:00
$panier->panier_pending = 1;
}
$panier->save();
2025-04-03 09:30:16 +02:00
} elseif ($campagne->camp_type == 2) {
// ---- POUR LES MURS SANS SELECTION
2019-02-13 16:37:14 +01:00
$nbCoupon = 0;
2025-04-03 09:30:16 +02:00
$cps = $campagne->coupons()->get();
2019-02-13 16:37:14 +01:00
2021-04-27 17:29:47 +02:00
foreach ($cps as $cp) {
2019-02-13 16:37:14 +01:00
// ---- Contrôle si le BR a déjà fait l'objet d'une impression
2021-04-27 17:29:47 +02:00
if ($this->isAvailable($cp, $campagne, $contact) && $this->isAvailableToPrint($cp, $campagne)) {
2019-02-13 16:37:14 +01:00
// ---- Génère l'image du BR à imprimer
$imgCoupon = $this->printCp($cp, $contact);
2025-04-03 09:30:16 +02:00
$coupons .= '<img src="' . $imgCoupon . '" border="0" style="display:block;width:165mm;"/>';
2019-02-13 16:37:14 +01:00
$nbCoupon++;
2021-04-27 17:29:47 +02:00
if ($nbCoupon % 3 == 0) {
$coupons .= '</div><div>';
}
2019-02-20 16:26:32 +01:00
array_push($couponsPrinted, ['cp_code' => $cp->coupon_code, 'qty' => 1]);
2025-04-03 09:30:16 +02:00
// ----- mémorise l'impression
$mur = new Mur();
$mur->mur_campid = $campagne->camp_id;
2019-02-13 16:37:14 +01:00
$mur->mur_contactId = $contact->contact_id;
2025-04-03 09:30:16 +02:00
$mur->mur_couponId = $cp->coupon_id;
2019-02-13 16:37:14 +01:00
$mur->save();
2025-04-03 09:30:16 +02:00
// Sauvegarde de stat
$stat = new Statistique();
$stat->stat_campId = $campagne->camp_id;
$stat->stat_couponId = $cp->coupon_id;
$stat->stat_couponPrinted = 1;
$stat->save();
2025-04-03 09:30:16 +02:00
}//end if
}//end foreach
} elseif ($campagne->camp_type == 3) {
// ---- POUR LES PANIERS AVEC SELECTION
2021-04-27 17:29:47 +02:00
if (!isset($couponsToPrint)) {
die();
}
2019-02-13 16:37:14 +01:00
$nbCoupon = 0;
2021-04-27 17:29:47 +02:00
if (!isset($panier)) {
die();
}
foreach ($couponsToPrint as $key => $coupon_code) {
2019-02-13 16:37:14 +01:00
$cp = Coupon::where(['coupon_code' => $coupon_code])->first();
2021-04-27 17:29:47 +02:00
if ($this->isAvailable($cp, $campagne, $contact) && $this->isAvailableToPrint($cp, $campagne)) {
2025-04-03 09:30:16 +02:00
$imgCoupon = $this->printCp($cp, $contact);
2019-02-13 16:37:14 +01:00
$panierContent = PanierContent::where(['panier_content_couponId' => $cp->coupon_id, 'panier_content_panierId' => $panier->panier_id])->first();
2021-04-27 17:29:47 +02:00
if ($panierContent->panier_content_quantity > 0) {
$coupons .= '<img src="' . $imgCoupon . '" border="0" style="display:block;width:165mm;"/>';
2019-02-13 16:37:14 +01:00
$nbCoupon++;
2021-04-27 17:29:47 +02:00
if ($nbCoupon % 3 == 0) {
$coupons .= '</div><div>';
}
$newQtyAvailable = $panierContent->panier_content_quantity - 1;
2025-04-03 09:30:16 +02:00
$newQtyPrinted = $panierContent->panier_content_printed + 1;
2025-04-03 09:30:16 +02:00
// Sauvegarde de stat
$stat = new Statistique();
$stat->stat_campId = $campagne->camp_id;
$stat->stat_couponId = $cp->coupon_id;
$stat->stat_couponPrinted = 1;
$stat->save();
2019-02-13 16:37:14 +01:00
}
2019-02-20 16:26:32 +01:00
array_push($couponsPrinted, ['cp_code' => $cp->coupon_code, 'qty' => 1]);
2019-02-13 16:37:14 +01:00
// ---- Mémorise l'impression
$panierContent->panier_content_quantity = $newQtyAvailable;
2025-04-03 09:30:16 +02:00
$panierContent->panier_content_printed = $newQtyPrinted;
2019-02-13 16:37:14 +01:00
$panierContent->save();
2025-04-03 09:30:16 +02:00
}//end if
}//end foreach
2019-02-20 16:26:32 +01:00
// ---- Update 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->panier_pending = 0;
2021-04-27 17:29:47 +02:00
} else {
2019-02-20 16:26:32 +01:00
$panier->panier_pending = 1;
}
$panier->save();
2025-04-03 09:30:16 +02:00
}//end if
2019-02-13 16:37:14 +01:00
$coupons .= '</div></TD></TR>';
2021-04-27 17:29:47 +02:00
$coupons .= '</TABLE>';
2019-02-13 16:37:14 +01:00
2025-04-03 09:30:16 +02:00
$log->log_statut = 'OK';
2019-02-20 16:26:32 +01:00
$log->log_message = json_encode(['Message' => 'Impressions réussies', 'Coupons' => $couponsPrinted], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$log->save();
2019-02-13 16:37:14 +01:00
return $coupons;
}
// ---- Retour au bon index une fois l'impression faite
2021-04-27 17:29:47 +02:00
public function backToIndex(Request $request)
{
if ($request->session()->has('campagne')) {
$campagne = $request->session()->get('campagne');
2019-02-13 16:37:14 +01:00
} else {
die();
}
2021-04-27 17:29:47 +02:00
if ($request->session()->has('contact')) {
$contact = $request->session()->get('contact');
}
2019-02-13 16:37:14 +01:00
2021-04-27 17:29:47 +02:00
if ($campagne->camp_type == 0 || $campagne->camp_type == 2) {
if (!isset($contact)) {
die();
}
return redirect(url('/') . '/?refsite=' . $campagne->camp_siteId . '&refcamp=' . $campagne->camp_id . '&hash=' . $contact->contact_hash);
2019-02-13 16:37:14 +01:00
} elseif ($campagne->camp_type == 1 || $campagne->camp_type == 3) {
2021-04-27 17:29:47 +02:00
if ($request->session()->has('panier')) {
$panier = $request->session()->get('panier');
2019-02-13 16:37:14 +01:00
} else {
die();
}
2021-04-27 17:29:47 +02:00
return redirect(url('/') . '/?basketid=' . $panier->panier_ref);
}
2019-02-13 16:37:14 +01:00
}
// ---- Charge les widget d'assistance
2021-04-27 17:29:47 +02:00
public function load_widget(Request $request)
{
2019-02-13 16:37:14 +01:00
if (isset($request->camp_id)) {
$camp_id = $request->camp_id;
2021-04-27 17:29:47 +02:00
} else {
die;
}
2019-02-13 16:37:14 +01:00
if (isset($request->contact_hash)) {
$contact_hash = $request->contact_hash;
2021-04-27 17:29:47 +02:00
} else {
die;
}
2019-02-13 16:37:14 +01:00
$campagne = Campagne::where(['camp_id' => $camp_id])->first();
2025-04-03 09:30:16 +02:00
$contact = Contact::where(['contact_hash' => $contact_hash])->first();
$coupons = $campagne->coupons;
2019-02-13 16:37:14 +01:00
foreach ($coupons as $key => $coupon) {
2021-04-27 17:29:47 +02:00
if (!$this->isAvailableToPrint($coupon, $campagne)) {
$coupons->forget($key);
}
}
2019-02-28 09:40:26 +01:00
$allLogs = Log::where(['log_refsite' => $campagne->camp_siteId, 'log_contactId' => $contact->contact_id, 'log_statut' => 'OK'])->orderBy('created_at', 'DESC')->get();
2025-04-03 09:30:16 +02:00
$logs = array();
2019-02-20 16:26:32 +01:00
foreach ($allLogs as &$l) {
$params = json_decode($l->log_params);
2021-04-27 17:29:47 +02:00
if ($l->log_action == 3 || $l->log_action == 1) {
2019-02-20 16:26:32 +01:00
$l->log_message = json_decode($l->log_message, true);
}
2021-04-27 17:29:47 +02:00
if (intval($params->refcamp) == $camp_id) {
array_push($logs, $l);
2019-02-20 16:26:32 +01:00
}
}
2025-04-03 09:30:16 +02:00
if ($campagne->camp_type == 0 || $campagne->camp_type == 2) {
// ---- Pour les types murs
2021-04-27 17:29:47 +02:00
foreach ($coupons as &$coupon) {
2019-02-13 16:37:14 +01:00
$coupon->isAvailable = $this->isAvailable($coupon, $campagne, $contact);
}
unset($coupon);
// Montant total et Nombre réductions disponible
$montantDispo = 0;
$montantTotal = 0;
2025-04-03 09:30:16 +02:00
$nbCoupon = 0;
2021-04-27 17:29:47 +02:00
foreach ($coupons as $coupon) {
2019-02-13 16:37:14 +01:00
$montantTotal += $coupon->coupon_value;
2021-04-27 17:29:47 +02:00
if ($coupon->isAvailable == true) {
2019-02-13 16:37:14 +01:00
$montantDispo += $coupon->coupon_value;
$nbCoupon++;
}
}
2021-04-27 17:29:47 +02:00
if (sizeof($coupons) == $nbCoupon) {
2019-02-13 16:37:14 +01:00
$reset = false;
} else {
$reset = true;
}
2021-04-27 17:29:47 +02:00
$campagne->url = url('/') . "/?refsite=" . $campagne->camp_siteId . "&refcamp=" . $camp_id . "&hash=" . $contact->contact_hash;
2019-02-13 16:37:14 +01:00
$returnHTML = view('widgets.mur')
->with([
2021-04-27 17:29:47 +02:00
'campagne' => $campagne,
'reset' => $reset,
'montantTotal' => $montantTotal,
'montantDispo' => $montantDispo,
2019-02-20 16:26:32 +01:00
'logs' => $logs,
2019-02-13 16:37:14 +01:00
'contact' => $contact])
->renderSections()['widget'];
2021-04-27 17:29:47 +02:00
return json_encode(array('success' => true, 'html' => $returnHTML));
2025-04-03 09:30:16 +02:00
} else {
// ---- Pour les types paniers
2019-02-13 16:37:14 +01:00
$panier = Panier::where(['panier_campId' => $campagne->camp_id, 'panier_contactId' => $contact->contact_id, 'panier_pending' => 1])->first();
2021-04-27 17:29:47 +02:00
if ($panier) {
2019-02-13 16:37:14 +01:00
$panier_ref = $panier->panier_ref;
foreach ($coupons as &$coupon) {
$paniercontent = PanierContent::where(['panier_content_couponId' => $coupon->coupon_id, 'panier_content_panierId' => $panier->panier_id])->first();
2021-04-27 17:29:47 +02:00
if ($paniercontent) {
2019-11-18 11:05:13 +01:00
$coupon->qty = $paniercontent->panier_content_quantity;
} else {
$coupon->qty = 0;
}
2019-02-13 16:37:14 +01:00
}
2021-04-27 17:29:47 +02:00
$campagne->url = url('/') . "/?basketid=" . $panier_ref;
2019-02-13 16:37:14 +01:00
unset($coupon);
} else {
2021-04-27 17:29:47 +02:00
$panier_ref = "";
2019-02-13 16:37:14 +01:00
foreach ($coupons as &$coupon) {
$coupon->qty = 0;
}
$campagne->url = "Pas d'url disponible (panier non créé)";
}
2021-04-27 17:29:47 +02:00
2019-02-13 16:37:14 +01:00
$returnHTML = view('widgets.panier')
->with([
2021-04-27 17:29:47 +02:00
'campagne' => $campagne,
2019-02-13 16:37:14 +01:00
'contact' => $contact,
2021-04-27 17:29:47 +02:00
'panier' => $panier_ref,
2019-02-20 16:26:32 +01:00
'logs' => $logs,
2019-02-13 16:37:14 +01:00
'coupons' => $coupons
])
->renderSections()['widget'];
2021-04-27 17:29:47 +02:00
return json_encode(array('success' => true, 'html' => $returnHTML));
2025-04-03 09:30:16 +02:00
}//end if
2019-02-13 16:37:14 +01:00
}
// ---- Montre les campagnes avec un compte de démo
2021-04-27 17:29:47 +02:00
public function show_demo(Request $request)
{
2019-02-13 16:37:14 +01:00
if (isset($request->camp_id)) {
$camp_id = $request->camp_id;
2021-04-27 17:29:47 +02:00
} else {
die;
}
2019-02-13 16:37:14 +01:00
2025-04-03 09:30:16 +02:00
$campagne = Campagne::where(['camp_id' => $camp_id])->first();
$siteId = $campagne->camp_siteId;
2019-02-13 16:37:14 +01:00
$demoContact = Contact::where(['contact_siteId' => $siteId, 'contact_ref' => 'DEMO'])->first();
2021-04-27 17:29:47 +02:00
if (!isset($demoContact)) {
2019-02-26 16:31:42 +01:00
$req = new \Illuminate\Http\Request();
$req->setMethod('POST');
$req->request->add([
'refsite' => $siteId,
'refadherent' => 'DEMO',
'prenom' => 'Compte',
'nom' => 'Démonstration',
'email' => 'demo@demo.fr',
'code_postal' => '72000',
'commune' => 'Le Mans',
'pays' => 250
]);
2025-04-03 09:30:16 +02:00
$contact = new Controllers\ContactController();
$res = $contact->updateContact($req);
2019-02-26 16:31:42 +01:00
$demoContact = Contact::where(['contact_siteId' => $siteId, 'contact_ref' => 'DEMO'])->first();
}
2021-04-27 17:29:47 +02:00
if ($campagne->camp_type == 0 || $campagne->camp_type == 2) {
2019-02-13 16:37:14 +01:00
// ---- DEMONSTRATION POUR LES MURS
$myRequest = new \Illuminate\Http\Request();
$myRequest->setMethod('POST');
2019-02-20 16:26:32 +01:00
$myRequest->request->add(['refcamp' => $camp_id, 'refadherent' => 'DEMO', 'admin' => 1]);
2019-02-13 16:37:14 +01:00
// ---- EFFACER LES CONTROLES SI EXISTANT
2021-04-27 17:29:47 +02:00
$mur = new Controllers\MurController();
2019-02-13 16:37:14 +01:00
$res = $mur->deleteControlContact($myRequest);
// ---- RETOURNE l'URL
2021-04-27 17:29:47 +02:00
$url = "/?refsite=" . $siteId . "&refcamp=" . $camp_id . "&hash=" . $demoContact->contact_hash;
return json_encode($url);
2019-02-13 16:37:14 +01:00
} else {
// ---- DEMONSTRATION POUR LES PANIERS
$panier = Panier::where(['panier_campId' => $camp_id, 'panier_contactId' => $demoContact->contact_id, 'panier_pending' => 1])->first();
2021-04-27 17:29:47 +02:00
if ($panier) {
$url = "/?basketid=" . $panier->panier_ref;
2019-02-13 16:37:14 +01:00
} else {
// --- Pas de panier Création d'un nouveau
// --- Récuperation des coupons de la campagne
$coupons = $campagne->coupons()->get();
foreach ($coupons as $key => $coupon) {
2021-04-27 17:29:47 +02:00
if (!$this->isAvailableToPrint($coupon, $campagne)) {
$coupons->forget($key);
}
}
2025-04-03 09:30:16 +02:00
$couponsTotal = sizeof($coupons);
$couponsAdded = 0;
2019-02-13 16:37:14 +01:00
$basketContent = "";
2021-04-27 17:29:47 +02:00
2019-02-13 16:37:14 +01:00
foreach ($coupons as $coupon) {
2021-04-27 17:29:47 +02:00
$basketContent .= "(1)" . $coupon->coupon_code;
2019-02-13 16:37:14 +01:00
$couponsAdded++;
2021-04-27 17:29:47 +02:00
if ($couponsAdded != $couponsTotal) {
2019-02-13 16:37:14 +01:00
$basketContent .= ",";
}
}
// --- création de l'objet request
$req = new \Illuminate\Http\Request();
$req->setMethod('POST');
$req->request->add([
'refsite' => $siteId,
2021-04-27 17:29:47 +02:00
'refcamp' => $camp_id,
2019-02-13 16:37:14 +01:00
'refadherent' => $demoContact->contact_ref,
'nom' => $demoContact->contact_nom,
'prenom' => $demoContact->contact_prenom,
'email' => $demoContact->contact_email,
'code_postal' => $demoContact->contact_cp,
'commune' => $demoContact->contact_ville,
'pays' => $demoContact->contact_country,
2019-02-20 16:26:32 +01:00
'panier' => $basketContent,
'admin' => 1
2019-02-13 16:37:14 +01:00
]);
2021-04-27 17:29:47 +02:00
$panier = new Controllers\PanierController();
2025-04-03 09:30:16 +02:00
$res = $panier->updateBasket($req);
$url = "/?basketid=" . $res->original['Panier']->Panier;
}//end if
2021-04-27 17:29:47 +02:00
return json_encode($url);
2025-04-03 09:30:16 +02:00
}//end if
2019-02-13 16:37:14 +01:00
}
2019-02-20 16:26:32 +01:00
// ---- MAINTENANCE : CLEAN DE LA BDD
// ---- Fonction de récap des contacts que l'on va effacer
2021-04-27 17:29:47 +02:00
public function get_clean_contacts_detail(Request $request)
{
2025-04-03 09:30:16 +02:00
$contacts = Contact::where('contact_updatedate', '<', Carbon::now()->subMonth())->get();
$date = Carbon::now();
2019-02-20 16:26:32 +01:00
$contact_count = 0;
foreach ($contacts as $contact) {
2025-04-03 09:30:16 +02:00
$paniers = $contact->paniers()->get();
2019-02-20 16:26:32 +01:00
$canbeDeleted = true;
foreach ($paniers as $panier) {
2021-04-27 17:29:47 +02:00
if ($panier->panier_pending == 1) {
2019-02-20 16:26:32 +01:00
$canbeDeleted = false;
}
}
if ($canbeDeleted) {
$contact_count++;
2025-04-03 09:30:16 +02:00
// $testcontact .= $contact->contact_id.", ";
2019-02-20 16:26:32 +01:00
}
}
return json_encode(['date' => $date, 'contact_count' => $contact_count]);
}
2019-02-13 16:37:14 +01:00
// ---- Fonction de nettoyage de la base contact
2021-04-27 17:29:47 +02:00
public function clean_contacts(Request $request)
{
2025-04-03 09:30:16 +02:00
$contacts = Contact::where('contact_updatedate', '<', Carbon::now()->subMonth())->get();
2019-02-13 16:37:14 +01:00
$testcontact = "";
foreach ($contacts as $contact) {
2025-04-03 09:30:16 +02:00
$paniers = $contact->paniers()->get();
2019-02-13 16:37:14 +01:00
$canbeDeleted = true;
foreach ($paniers as $panier) {
2021-04-27 17:29:47 +02:00
if ($panier->panier_pending == 1) {
2019-02-13 16:37:14 +01:00
$canbeDeleted = false;
}
}
2019-02-20 16:26:32 +01:00
$murs = $contact->murs()->get();
2021-04-27 17:29:47 +02:00
if (isset($murs) && !empty($murs)) {
2019-02-20 16:26:32 +01:00
$canbeDeleted = false;
}
2019-02-13 16:37:14 +01:00
if ($canbeDeleted) {
2019-02-20 16:26:32 +01:00
$contact->delete();
2021-04-27 17:29:47 +02:00
$testcontact .= $contact->contact_id . ", ";
2019-02-13 16:37:14 +01:00
}
}
return json_encode($testcontact);
}
// ---- Fonction de nettoyage de la base mur
2021-04-27 17:29:47 +02:00
public function clean_walls(Request $request)
{
2025-04-03 09:30:16 +02:00
$murs = Mur::where('mur_createdate', '<', Carbon::now()->subYear())->where(['mur_deleted' => 1])->get();
2019-02-13 16:37:14 +01:00
$testmur = "";
foreach ($murs as $mur) {
2019-02-20 16:26:32 +01:00
$mur->delete();
2021-04-27 17:29:47 +02:00
$testmur .= $mur->mur_id . ", ";
2019-02-13 16:37:14 +01:00
}
return json_encode($testmur);
}
// ---- Fonction de nettoyage de la base panier et panierContent
2021-04-27 17:29:47 +02:00
public function clean_baskets(Request $request)
{
2025-04-03 09:30:16 +02:00
$paniers = Panier::where('panier_createdate', '<', Carbon::now()->subMonth(2))->where(['panier_pending' => 0])->get();
2019-02-13 16:37:14 +01:00
$testpanier = "";
foreach ($paniers as $panier) {
2025-04-03 09:30:16 +02:00
$testpanier .= $panier->panier_id . ": ";
2019-02-13 16:37:14 +01:00
$panierContents = $panier->panierContents()->get();
foreach ($panierContents as $panierContent) {
2021-04-27 17:29:47 +02:00
$testpanier .= $panierContent->panier_content_id . ', ';
2019-02-20 16:26:32 +01:00
$panierContent->delete();
2019-02-13 16:37:14 +01:00
}
2019-02-20 16:26:32 +01:00
$panier->delete();
2019-02-13 16:37:14 +01:00
}
return json_encode($testpanier);
}
2019-01-15 14:17:15 +01:00
}