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