320 lines
10 KiB
PHP
320 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Campagne;
|
|
use app\Coupon;
|
|
use App\Contact;
|
|
use App\Mur;
|
|
use App\Log;
|
|
use App\Traits\CouponTrait;
|
|
use App\Traits\ThanksTrait;
|
|
use Response;
|
|
|
|
class MurController extends Controller
|
|
{
|
|
use CouponTrait;
|
|
use ThanksTrait;
|
|
|
|
public function show(Request $request)
|
|
{
|
|
$datas = $request->all();
|
|
if(isset( $datas['refcamp'])){
|
|
$ref = intval($datas['refcamp']);
|
|
$campagne = Campagne::where(['camp_id'=> $ref])->first();
|
|
} else {
|
|
abort(404);
|
|
}
|
|
if(!isset( $datas['refsite'])){
|
|
abort(404);
|
|
}
|
|
if(!isset($campagne) || $campagne->camp_published == 0){
|
|
abort(404);
|
|
}
|
|
|
|
if(isset($datas['hash'])){
|
|
$contact = Contact::where([
|
|
'contact_hash' => $datas['hash'], 'contact_siteId' => $campagne->camp_siteId
|
|
])->first();
|
|
} else {
|
|
abort(404);
|
|
}
|
|
|
|
$coupons = $this->getCouponsPublished($campagne);
|
|
|
|
foreach ($coupons as &$coupon){
|
|
$coupon->isAvailable = $this->isAvailable($coupon, $campagne, $contact);
|
|
$coupon->isAvailableToPrint = $this->isAvailableToPrint($coupon, $campagne);
|
|
}
|
|
unset($coupon);
|
|
|
|
// Montant total et Nombre réductions disponible
|
|
$montantDispo = 0;
|
|
$montantTotal = 0;
|
|
$nbCoupon = 0;
|
|
$nbCouponToPrint = 0;
|
|
foreach($coupons as $coupon){
|
|
$montantTotal += $coupon->coupon_value;
|
|
if($coupon->isAvailable == true){
|
|
$montantDispo += $coupon->coupon_value;
|
|
$nbCoupon++;
|
|
}
|
|
if($coupon->isAvailableToPrint == true){
|
|
$nbCouponToPrint++;
|
|
}
|
|
}
|
|
|
|
session()->put('campagne', $campagne);
|
|
session()->put('site', $campagne->site);
|
|
session()->put('contact', $contact);
|
|
|
|
$view = view('landings::' . $ref . '.index')
|
|
->with(['ref'=> $ref, 'coupons' => $coupons, 'contact' => $contact, 'nb' => $nbCoupon, 'nbAvailableToPrint' => $nbCouponToPrint]);
|
|
|
|
echo($view);
|
|
}
|
|
|
|
public function printMur(Request $request)
|
|
{
|
|
if(isset($request->coupons)){
|
|
session()->put('coupons', $request->coupons);
|
|
$coupons = $request->coupons;
|
|
}
|
|
|
|
if($request->session()->has('campagne')) {
|
|
$campagne = $request->session()->get('campagne');
|
|
}
|
|
|
|
$ref = $campagne->camp_id;
|
|
|
|
$view = view('landings::' . $ref . '.imprimer')
|
|
->with(['ref'=> $ref, 'coupons' => $coupons, 'contact' => $request->session()->get('contact')]);
|
|
|
|
echo($view);
|
|
}
|
|
|
|
public function deleteControlContact(Request $request)
|
|
{
|
|
if(isset($request->admin)){
|
|
$source = 'ADMIN';
|
|
} else {
|
|
$source = 'API';
|
|
}
|
|
|
|
// ---- INIT du log
|
|
$log = new Log();
|
|
$log->log_action = config('constants.delctrlcontact');
|
|
$log->log_source = $source;
|
|
|
|
$error = false;
|
|
// ---- Contrôle des paramètres
|
|
if(isset($request->refcamp) && $request->refcamp != ""){
|
|
$refcamp = $request->refcamp;
|
|
}else{
|
|
$error = true;
|
|
}
|
|
if(isset($request->refadherent) && $request->refadherent != ""){
|
|
$refadherent = $request->refadherent;
|
|
}else{
|
|
$error = true;
|
|
}
|
|
// Erreur si paramètres mal renseignés
|
|
if($error){
|
|
|
|
$log->log_params = json_encode(['refcamp' => $request->refcamp, 'refadherent' => $request->refadherent]);
|
|
$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'
|
|
]);
|
|
}
|
|
|
|
//Check de la campagne
|
|
$campagne = Campagne::where('camp_id', $refcamp)->first();
|
|
|
|
if(!$campagne){
|
|
$log->log_params = json_encode(['refcamp' => $request->refcamp, 'refadherent' => $request->refadherent]);
|
|
$log->log_statut = 'NOK';
|
|
$log->log_message = "La campagne n'existe pas";
|
|
$log->save();
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 103,
|
|
'Message' => 'La campagne n\'existe pas'
|
|
]);
|
|
}
|
|
|
|
|
|
//Check du contact
|
|
$contact = Contact::where(['contact_ref' => $refadherent, 'contact_siteId' => $campagne->camp_siteId])->first();
|
|
|
|
if(!$contact){
|
|
$log->log_params = json_encode(['refcamp' => $request->refcamp, 'refadherent' => $request->refadherent]);
|
|
$log->log_refsite = $campagne->camp_siteId;
|
|
$log->log_statut = 'NOK';
|
|
$log->log_message = "Le contact n'existe pas";
|
|
$log->save();
|
|
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 104,
|
|
'Message' => 'Le contact n\'existe pas'
|
|
]);
|
|
}
|
|
|
|
if($campagne){
|
|
//Update
|
|
$mur = Mur::where(['mur_campId' => $refcamp, 'mur_contactId' => $contact->contact_id])->update(['mur_deleted' => '1']);
|
|
if($mur != 0){
|
|
$log->log_params = json_encode(['refcamp' => $request->refcamp, 'refadherent' => $request->refadherent]);
|
|
$log->log_refsite = $campagne->camp_siteId;
|
|
$log->log_contactId = $contact->contact_id;
|
|
$log->log_statut = 'OK';
|
|
$log->log_message = $mur." enregistrements ont été mis à jour.";
|
|
$log->save();
|
|
return Response::json([
|
|
'Statut' => 'OK',
|
|
'Message' => $mur." enregistrements ont été mis à jour."
|
|
]);
|
|
} else {
|
|
$log->log_params = json_encode(['refcamp' => $request->refcamp, 'refadherent' => $request->refadherent]);
|
|
$log->log_refsite = $campagne->camp_siteId;
|
|
$log->log_contactId = $contact->contact_id;
|
|
$log->log_statut = 'OK';
|
|
$log->log_message = 'Aucun contrôle à effacer';
|
|
$log->save();
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 110,
|
|
'Message' => 'Aucun contrôle à effacer'
|
|
]);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
public function montantDispoCampagne(Request $request)
|
|
{
|
|
|
|
// ---- Contrôle des paramètres
|
|
if(isset($request->refcamp) && $request->refcamp != ""){
|
|
$refcamp = $request->refcamp;
|
|
}else{
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 101,
|
|
'Message' => 'Des paramètres de la requête sont mal renseignés ou non présents'
|
|
]);
|
|
}
|
|
|
|
// ------ Recuperation de la campagne
|
|
$camp = Campagne::where(['camp_id' => $refcamp])->first();
|
|
if(!$camp){
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 103,
|
|
'Message' => 'La campagne n\'existe pas'
|
|
]);
|
|
}
|
|
|
|
// ------ Récupération des coupons de la campagne
|
|
$coupons = $camp->coupons()->where(['coupon_published' => 1])->get();
|
|
if(count($coupons) == 0){
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 105,
|
|
'Message' => 'La campagne est vide'
|
|
]);
|
|
}
|
|
|
|
// ---- Montant total et Nombre réductions disponible
|
|
$montantTotal = 0;
|
|
foreach($coupons as $coupon){
|
|
$montantTotal += $coupon->coupon_value;
|
|
}
|
|
|
|
$montantTotal = number_format($montantTotal, 2, ",", " ");
|
|
$montantTotal = str_pad($montantTotal, 6, "0", STR_PAD_LEFT);
|
|
|
|
return Response::json([
|
|
'Statut' => 'OK',
|
|
'Message' => $montantTotal
|
|
]);
|
|
}
|
|
|
|
public function montantDispoContact(Request $request)
|
|
{
|
|
$error = false;
|
|
if(isset($request->refcamp) && $request->refcamp != ""){
|
|
$refcamp = $request->refcamp;
|
|
}else{
|
|
$error = true;
|
|
}
|
|
|
|
if(isset($request->refsite) && $request->refsite != ""){
|
|
$refsite = $request->refsite;
|
|
}else{
|
|
$error = true;
|
|
}
|
|
|
|
if(isset($request->refadherent) && $request->refadherent != ""){
|
|
$refadherent = $request->refadherent;
|
|
}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'
|
|
]);
|
|
}
|
|
|
|
$campagne = Campagne::where(['camp_id' => $refcamp])->where('camp_type', 'IN', array(0,2))->first();
|
|
|
|
if (!$campagne) {
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 103,
|
|
'Message' => 'La campagne n\'existe pas'
|
|
]);
|
|
}
|
|
|
|
$contact = Contact::where(['contact_siteId' => $refsite, 'contact_ref' => $refadherent])->first();
|
|
if (!$contact) {
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 104,
|
|
'Message' => 'Le contact n\'existe pas'
|
|
]);
|
|
}
|
|
|
|
$coupons = $this->getCouponsPublished($campagne);
|
|
|
|
foreach($coupons as &$coupon){
|
|
$coupon->isAvailable = $this->isAvailable($coupon, $campagne, $contact);
|
|
}
|
|
unset($coupon);
|
|
|
|
$montantDispo = 0;
|
|
foreach($coupons as $coupon){
|
|
if($coupon->isAvailable == true){
|
|
$montantDispo += $coupon->coupon_value;
|
|
}
|
|
}
|
|
|
|
$montantDispo = number_format($montantDispo, 2, ",", " ");
|
|
$montantDispo = str_pad($montantDispo, 6, "0", STR_PAD_LEFT);
|
|
|
|
return Response::json([
|
|
'Statut' => 'OK',
|
|
'Message' => $montantDispo
|
|
]);
|
|
}
|
|
}
|