264 lines
11 KiB
PHP
264 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Mail\CourrierArcadiaError;
|
|
use App\Mail\NotifAdmin;
|
|
use App\Mail\NotifWinAdmin;
|
|
use App\Mail\PrizeWon;
|
|
use App\Mail\ReductionWon;
|
|
use App\Models\BannedIP;
|
|
use App\Models\Codes;
|
|
use App\Models\CodesEAN;
|
|
use App\Models\InstantWin;
|
|
use App\Models\Participation;
|
|
use App\Models\Reductions;
|
|
use LDCWeb\MesBR;
|
|
use App\Models\Settings;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class EnterCodeController extends Controller
|
|
{
|
|
private $apiCrm;
|
|
|
|
public function __construct() {
|
|
$this->apiCrm = new \LDCCRM\ApiV1(config('crm.endpoint'), config('crm.token'));
|
|
}
|
|
|
|
public function index() {
|
|
$nbParticipations = Participation::where('user_id', Auth::user()->id)->count();
|
|
$settings = Settings::find(1)->select('use_ean_codes', 'nb_participation_player')->first();
|
|
if ($nbParticipations >= $settings->nb_participation_player)
|
|
return view('front.basic.participation-exhausted', compact('settings'));
|
|
return view('front.basic.enter-code', compact('settings'));
|
|
}
|
|
|
|
public function participate(Request $request) {
|
|
// TODO : VALIDATE avec / sans code ean
|
|
// Récupération des données à utilisées
|
|
$code = strtoupper($request->code);
|
|
$codeBase = Codes::where('code_unique', $code)->first();
|
|
$settings = Settings::find(1);
|
|
|
|
$rules = $request->accept_rules === null ? false : true;
|
|
if (!$rules)
|
|
return back()->withErrors("Pour participer, vous devez accepter le règlement du jeu.");
|
|
|
|
// Cas IP bannie
|
|
if (config('app.env') == "local") {
|
|
$ipBanned = BannedIP::where('ip', $_SERVER['REMOTE_ADDR'])->first() === null ? 0 : 1;
|
|
} else {
|
|
$ipBanned = BannedIP::where('ip', $_SERVER['HTTP_X_FORWARDED_FOR'])->first() === null ? 0 : 1;
|
|
}
|
|
if ($ipBanned)
|
|
return view('front.basic.participation-exhausted', compact('settings'));
|
|
|
|
// Cas nombre de participation maximale atteint
|
|
$nbParticipations = Participation::where('user_id', Auth::user()->id)->count();
|
|
if ($nbParticipations >= $settings->nb_participation_player)
|
|
return view('front.basic.participation-exhausted', compact('settings'));
|
|
|
|
// Check le code pour savoir s'il est encore utilisable
|
|
if (!$codeBase)
|
|
return back()->withErrors("Code incorrect, veuillez réessayer.");
|
|
if ($codeBase->code_used === 1)
|
|
return back()->withErrors("Ce code n'est plus utilisable.");
|
|
|
|
// Check si l'utilisateur n'a pas déjà utiliser ce code
|
|
$participUser = Participation::where([
|
|
['code_unique_id', $codeBase->id],
|
|
['user_id', Auth::user()->id]
|
|
])->first();
|
|
if ($participUser)
|
|
return back()->withErrors("Vous avez déjà utilisé ce code.");
|
|
|
|
// Check si les codes ean sont utilisés, auquel cas on test celui envoyé pour savoir s'il est correct
|
|
$codeEan = null;
|
|
// TODO : Task Algo Sebastien
|
|
if ($settings->use_ean_codes === 1) {
|
|
$codeEan = $request->code_ean;
|
|
$codeEanBase = CodesEAN::where('code', $codeEan)->first();
|
|
if (!$codeEanBase)
|
|
$codeEanBase = CodesEAN::where('code', 'like', substr($codeEan, 0, 6) . "%")->first();
|
|
if (!$codeEanBase || empty($codeEan))
|
|
return back()->withErrors('Le code barre entré est incorrect, veuillez réessayer.');
|
|
}
|
|
|
|
$codeNbUsage = Participation::where('code_unique_id', $codeBase->id)->count();
|
|
|
|
// Gestion du nombre d'utilisation d'un code
|
|
if ($codeNbUsage >= $settings->nb_unique_code_use) {
|
|
$codeBase->code_used = 1;
|
|
$codeBase->save();
|
|
return back()->withErrors("Ce code n'est plus utilisable.");
|
|
} else {
|
|
if ($codeNbUsage + 1 >= $settings->nb_unique_code_use)
|
|
$codeBase->code_used = 1;
|
|
}
|
|
|
|
// Tous les cas "bloquants" sont passé, l'utilisateur participe à partir d'ici
|
|
$participation = new Participation();
|
|
$participation->user_id = Auth::user()->id;
|
|
$participation->code_unique_id = $codeBase->id;
|
|
$participation->code_ean_id = isset($codeEanBase) ? $codeEanBase->id : null;
|
|
if (config('app.env') === "local") {
|
|
$participation->ip = $_SERVER['REMOTE_ADDR'];
|
|
} else {
|
|
$participation->ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
}
|
|
$participation->save();
|
|
|
|
// Gestion des instants gagnants
|
|
$userAlreadyWon = Participation::where([
|
|
['user_id', Auth::user()->id],
|
|
['instant_win_id', '<>', null]
|
|
])->first();
|
|
|
|
// Si l'utilisateur actuel à déjà gagné un gros lot, pas d'instant gagnant
|
|
if ($userAlreadyWon) {
|
|
// Si le jeu est 100% gagnant
|
|
if ($settings->reductions_activated === 1) {
|
|
$coupon = $this->getRandomCoupon($settings);
|
|
$crmCoupon = $this->updateCoupon($settings, $coupon->web_coupon_identifiant, $participation);
|
|
if (!$crmCoupon) {
|
|
$participation->delete();
|
|
return back()->withErrors("Une erreur est survenue, merci de réessayez ultérieurement.");
|
|
}
|
|
Mail::to(Auth::user()->email)->send(new ReductionWon(Auth::user(), $coupon, $settings, $crmCoupon));
|
|
return view('front.basic.reduction', compact('crmCoupon', 'coupon'));
|
|
} else {
|
|
return view('front.basic.loose');
|
|
}
|
|
} else {
|
|
// Gain d'un gros lot
|
|
$instantWin = InstantWin::where([['appear_date', '<', now()], ['won', 0]])->orderBy('appear_date', 'ASC')->first();
|
|
if ($instantWin) {
|
|
$instantWin->won = 1;
|
|
$instantWin->participation_id = $participation->id;
|
|
$participation->instant_win_id = $instantWin->id;
|
|
// Envoi du courrier Arcadia
|
|
if ($instantWin->prize->arcadia_operation_code !== 0 && $instantWin->prize->arcadia_support_code !== 0) {
|
|
|
|
$crmId = Auth::user()->crm_id;
|
|
|
|
$fields = [
|
|
'root' => [
|
|
[
|
|
'fieldName' => 'order_date',
|
|
'fieldValue' => date('d/m/Y')
|
|
],
|
|
[
|
|
'fieldName' => 'operation',
|
|
'fieldValue' => $instantWin->prize->arcadia_operation_code
|
|
]
|
|
],
|
|
'orderLine' => [
|
|
[
|
|
'fieldName' => 'code_unique',
|
|
'fieldValue' => "$codeBase->code_unique"
|
|
],
|
|
[
|
|
'fieldName' => 'support_code',
|
|
'fieldValue' => $instantWin->prize->arcadia_support_code
|
|
],
|
|
[
|
|
'fieldName' => 'label_type_support',
|
|
'fieldValue' => 'Gain du lot : ' . $instantWin->prize->label_one
|
|
]
|
|
]
|
|
];
|
|
|
|
if ($settings->use_ean_codes === 1) {
|
|
$codeEan = $request->code_ean;
|
|
$fields['orderLine'][] = [
|
|
'fieldName' => 'code_on_pack',
|
|
'fieldValue' => "$codeEan"
|
|
];
|
|
}
|
|
|
|
$arcadiaMail = $this->apiCrm->ArcadiaOutgoingMail($crmId, $fields, 'game');
|
|
if ($arcadiaMail->status === 'NOK'){
|
|
Mail::to(config('mail.MAIL_ADMIN'))->send(new CourrierArcadiaError(Auth::user()));
|
|
}
|
|
}
|
|
|
|
$instantWin->save();
|
|
Mail::to(Auth::user()->email)->send(new PrizeWon(Auth::user(), $instantWin->prize, $settings));
|
|
$mails = json_decode($settings->notification_emails);
|
|
foreach ($mails as $mail)
|
|
Mail::to($mail->email)->send(new NotifWinAdmin($settings, $participation));
|
|
$participation->save();
|
|
return view("front.basic." . $instantWin->prize->gain_view->file_name, compact('settings'))->with('prize', $instantWin->prize);
|
|
}
|
|
// Si le jeu est 100% gagnant
|
|
if ($settings->reductions_activated === 1) {
|
|
$coupon = $this->getRandomCoupon($settings);
|
|
$crmCoupon = $this->updateCoupon($settings, $coupon->web_coupon_identifiant, $participation);
|
|
if (!$crmCoupon) {
|
|
$participation->delete();
|
|
return back()->withErrors("Une erreur est survenue, merci de réessayez ultérieurement.");
|
|
}
|
|
Mail::to(Auth::user()->email)->send(new ReductionWon(Auth::user(), $coupon, $settings, $crmCoupon));
|
|
return view('front.basic.reduction', compact('crmCoupon', 'coupon'));
|
|
} else {
|
|
return view('front.basic.loose');
|
|
}
|
|
}
|
|
}
|
|
|
|
private function getRandomCoupon($settings) {
|
|
$webCoupons = json_decode($settings->web_coupons);
|
|
if (count($webCoupons) > 1) {
|
|
$lots = [];
|
|
foreach($webCoupons as $webCoupon)
|
|
$lots[] = $webCoupon;
|
|
foreach ($lots as $lot)
|
|
for ($i = 0; $i < $lot->percentage; $i++)
|
|
$tmp[] = $lot;
|
|
shuffle($tmp);
|
|
return $tmp[0];
|
|
} else {
|
|
return $webCoupons[0];
|
|
}
|
|
}
|
|
|
|
private function updateCoupon($settings, $coupon, $participation) {
|
|
$reduction = null;
|
|
$mesBR = new MesBR(config('mesbr.endpoint'), $settings->reductions_site, $settings->reductions_campaign);
|
|
|
|
$crmUser = $this->apiCrm->getCustomer(Auth::user()->crm_id);
|
|
if ($crmUser->status === 'OK'){
|
|
$mesBR->updateContact($crmUser->user);
|
|
$result = $mesBR->updatePanier($crmUser->user, $coupon);
|
|
if ($result["status"] === "OK") {
|
|
$reduction = new Reductions();
|
|
$reduction->participation_id = $participation->id;
|
|
$reduction->link = $result["link"];
|
|
$reduction->verification = $result["verif"];
|
|
$reduction->basket_id = $result["basketId"];
|
|
$reduction->save();
|
|
$participation->reduction_id = $reduction->id;
|
|
$participation->save();
|
|
}else{
|
|
Mail::to(config('mail.MAIL_ADMIN'))->send(new NotifAdmin("MESBR : Une erreur est survenue. Code : " . $result["code"] . " Message : ". $result["message"]));
|
|
}
|
|
}
|
|
|
|
return $reduction;
|
|
}
|
|
|
|
public function formWinningProof() {
|
|
$participation = Participation::where([
|
|
['user_id', Auth::user()->id],
|
|
['instant_win_id', '<>', null],
|
|
['sticker', null],
|
|
['ticket', null],
|
|
['barcode', null]
|
|
])->first();
|
|
if ($participation)
|
|
return view('front.basic.winning-proof', compact('participation'));
|
|
return redirect()->route('home');
|
|
}
|
|
}
|