248 lines
7.7 KiB
PHP
248 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Campagne;
|
|
use app\Coupon;
|
|
use App\Contact;
|
|
use App\Mur;
|
|
use Response;
|
|
|
|
class MurController extends Controller
|
|
{
|
|
public function show(Request $request)
|
|
{
|
|
$datas = $request->all();
|
|
$ref = $datas['refcamp'];
|
|
$campagne = Campagne::find(intval($ref));
|
|
$contact = Contact::where([
|
|
'contact_email' => $datas['email'], 'contact_siteId' => $campagne->site->site_id
|
|
])->first();
|
|
|
|
|
|
session()->put('campagne', $campagne);
|
|
session()->put('site', $campagne->site);
|
|
session()->put('contact', $contact);
|
|
|
|
$view = view('landings::' . $ref . '.index')
|
|
->with(['ref'=> $ref, 'coupons' => $campagne->coupons]);
|
|
|
|
echo($view);
|
|
}
|
|
|
|
public function deleteControlContact(Request $request)
|
|
{
|
|
$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){
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 101,
|
|
'Message' => 'Des paramètres de la requête sont mal renseignés ou non présents'
|
|
]);
|
|
}
|
|
|
|
//Check du contact
|
|
$contact = Contact::where(['contact_ref' => $refadherent])->first();
|
|
|
|
if(!$contact){
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 104,
|
|
'Message' => 'Le contact n\'existe pas'
|
|
]);
|
|
}
|
|
|
|
//Check de la campagne
|
|
$campagne = Campagne::where('camp_id', $refcamp)->first();
|
|
|
|
if($campagne){
|
|
//Update
|
|
$mur = Mur::where('mur_campId', $refcamp)->update(['mur_deleted' => '1']);
|
|
return Response::json([
|
|
'Statut' => 'OK',
|
|
'Message' => $mur." enregistrements ont été mis à jour."
|
|
]);
|
|
} else {
|
|
return Response::json([
|
|
'Statut' => 'KO',
|
|
'Code' => 103,
|
|
'Message' => 'La campagne n\'existe pas'
|
|
]);
|
|
}
|
|
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 = Coupon::where(['coupon_campId' => $refcamp, 'coupon_published' => 1])->get();
|
|
|
|
foreach($coupons as &$coupon){
|
|
$available = true;
|
|
|
|
// ---- Contrôle le nombre limite d'impression
|
|
if($coupon->coupon_enablelimitquantity == 1){
|
|
$stat = Mur::where([
|
|
'mur_campId' => $campagne->camp_id,
|
|
'mur_contactId' => $contact->contact_id,
|
|
'mur_couponId' => $coupons->coupon_id,
|
|
'mur_deleted' => 0 ])
|
|
->count();
|
|
|
|
if($stat >= $coupon->coupon_limitquantity){
|
|
$available = false;
|
|
}
|
|
}
|
|
|
|
// ---- Contrôle la limite de période de diffusion
|
|
if($coupon->coupon_enablelimitperiod == 1 && $coupon->coupon_limitperiod > 0){
|
|
$stat = Mur::where([
|
|
'mur_campId' => $campagne->camp_id,
|
|
'mur_contactId' => $contact->contact_id,
|
|
'mur_couponId' => $coupons->coupon_id,
|
|
'mur_deleted' => 0 ])
|
|
->orderBy('mur_createdate', 'DESC')
|
|
->first();
|
|
|
|
if ($stat->count > 0){
|
|
$dateDisponibilite = date('Y-m-d',strtotime($stat->mur_createdate." +".$coupon->coupon_limitperiod." days"));
|
|
if($dateDisponibilite > date('Y-m-d')){
|
|
$available = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---- Contrôle la date de validité
|
|
if($coupon->coupon_maxdatevalidity != null){
|
|
if(date('Y-m-d') > $coupon->coupon_maxdatevalidity){
|
|
$available = false;
|
|
}
|
|
}
|
|
|
|
$coupon->isAvailable = $available;
|
|
}
|
|
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
|
|
]);
|
|
}
|
|
}
|