139 lines
4.2 KiB
PHP
139 lines
4.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\OpBrCharcut;
|
||
|
|
|
||
|
|
use App\Http\Requests\OpBrCharcut\ParticipateRequest;
|
||
|
|
use App\Mail\NotifAdmin;
|
||
|
|
use App\Mail\ReductionWon;
|
||
|
|
use Illuminate\Support\Facades\Auth;
|
||
|
|
use Illuminate\Support\Facades\Mail;
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
|
||
|
|
class EnterCodeController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Show enter code form
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
||
|
|
*/
|
||
|
|
public function index()
|
||
|
|
{
|
||
|
|
return view('front.opbrcharcut.enter-code');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Participate Method
|
||
|
|
*
|
||
|
|
* @param ParticipateRequest $request
|
||
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
||
|
|
*/
|
||
|
|
public function participate(ParticipateRequest $request)
|
||
|
|
{
|
||
|
|
return $this->serveCouponWin($request->code);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Détermine si un BR doit être servi
|
||
|
|
* renvoi la vue de gain BR
|
||
|
|
*
|
||
|
|
* @param
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
||
|
|
*/
|
||
|
|
private function serveCouponWin($code)
|
||
|
|
{
|
||
|
|
//ici on a besoin que d'un identifiant pas de tout le coupon
|
||
|
|
$crmCoupon = $this->updateCoupon(config('opbrcharcut.MESBR_COUPON_1'), 1);
|
||
|
|
if (!$crmCoupon) {
|
||
|
|
return back()->withErrors("Une erreur est survenue, merci de réessayez ultérieurement.");
|
||
|
|
}
|
||
|
|
$crmCoupon = $this->updateCoupon(config('opbrcharcut.MESBR_COUPON_2'), 2);
|
||
|
|
if (!$crmCoupon) {
|
||
|
|
return back()->withErrors("Une erreur est survenue, merci de réessayez ultérieurement.");
|
||
|
|
}
|
||
|
|
$crmCoupon = $this->updateCoupon(config('opbrcharcut.MESBR_COUPON_3'), 3);
|
||
|
|
if (!$crmCoupon) {
|
||
|
|
return back()->withErrors("Une erreur est survenue, merci de réessayez ultérieurement.");
|
||
|
|
}
|
||
|
|
|
||
|
|
$user = Auth::guard('customer')->user();
|
||
|
|
Mail::to($user->email)->send(new ReductionWon($user->apiData, $crmCoupon));
|
||
|
|
//Marquer le code comme utilisé par le user
|
||
|
|
$code->code_used = 1;
|
||
|
|
$code->customer()->associate($user);
|
||
|
|
$code->save();
|
||
|
|
return view('front.opbrcharcut.reduction', compact('crmCoupon'));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update a coupon
|
||
|
|
*
|
||
|
|
* @param mixed $id_coupon
|
||
|
|
* @return \App\Models\Reduction|null
|
||
|
|
*/
|
||
|
|
private function updateCoupon($id_coupon, $qtty)
|
||
|
|
{
|
||
|
|
/** @var \App\Lib\MesBROld */
|
||
|
|
$mesBRCharc = app('mesBR_Charcut');
|
||
|
|
/** @var \App\Models\Customers $customer */
|
||
|
|
$customer = Auth::guard('customer')->user();
|
||
|
|
/** @var \LDC\LDCCRM\Models\Customer */
|
||
|
|
$crmCustomer = $customer->apiData;
|
||
|
|
|
||
|
|
$updateCustID = $this->updateCustomer(
|
||
|
|
$crmCustomer->hashcode,
|
||
|
|
$crmCustomer->email,
|
||
|
|
$crmCustomer->country
|
||
|
|
);
|
||
|
|
|
||
|
|
if (!$updateCustID) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
$result = $mesBRCharc->updatePanier($crmCustomer, $updateCustID, $id_coupon, $qtty);
|
||
|
|
if ($result["status"] === "OK") {
|
||
|
|
//ici on a besoin que savoir que c'était OK
|
||
|
|
$reduction = $result["link"];
|
||
|
|
} else {
|
||
|
|
Mail::to(config('mail.contactsList.serviceweb'))
|
||
|
|
->send(new NotifAdmin(\sprintf(
|
||
|
|
'MESBR : Une erreur est survenue. Code : %s Message : %s',
|
||
|
|
$result["code"],
|
||
|
|
$result["message"]
|
||
|
|
)));
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $reduction;
|
||
|
|
}
|
||
|
|
|
||
|
|
private function updateCustomer($hascode, $email, $country)
|
||
|
|
{
|
||
|
|
/** @var \App\Lib\MesBR */
|
||
|
|
$mesBR = app('mesBR');
|
||
|
|
|
||
|
|
$updateCust = $mesBR->updateCustomer(
|
||
|
|
$hascode,
|
||
|
|
$email,
|
||
|
|
$country
|
||
|
|
);
|
||
|
|
|
||
|
|
if ($updateCust->status === 'KO') {
|
||
|
|
Mail::to(config('mail.contactsList.serviceweb'))
|
||
|
|
->send(new NotifAdmin(\sprintf(
|
||
|
|
'MESBR : Une erreur est survenue. Code : %s Message : %s',
|
||
|
|
$updateCust->code,
|
||
|
|
$updateCust->message
|
||
|
|
)));
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!isset($updateCust->customer->id)) {
|
||
|
|
return $this->updateCustomer($hascode, $email, $country);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $updateCust->customer->id;
|
||
|
|
}
|
||
|
|
}
|