123 lines
4.4 KiB
PHP
123 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Fo\SubmitWinningProof;
|
|
use App\Mail\NotifAdmin;
|
|
use App\Mail\WinningProofMail;
|
|
use App\Models\Participation;
|
|
use App\Models\Settings;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class WinningProofController extends Controller
|
|
{
|
|
/**
|
|
* @var \LDCCRM\ApiV1
|
|
*/
|
|
private $apiCrm;
|
|
|
|
/**
|
|
* WinningProofController
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->apiCrm = new \LDCCRM\ApiV1(config('crm.endpoint'), config('crm.token'));
|
|
}
|
|
|
|
/**
|
|
* Submit Winning proof
|
|
*
|
|
* @param SubmitWinningProof $request
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
*/
|
|
public function sendWinningProof(SubmitWinningProof $request)
|
|
{
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = auth('customer')->user();
|
|
|
|
// On récupère la participation gagnante en attente des infos.
|
|
/** @var \App\Models\Participation|null $participation */
|
|
$participation = Participation::whereHas('customer', function ($q) use ($customer) {
|
|
$q->whereId($customer->id);
|
|
})->whereHas('instantWin')->where([
|
|
['sticker', null],
|
|
['ticket', null],
|
|
['barcode', null]
|
|
])->first();
|
|
|
|
if (!$participation) {
|
|
return redirect()->route('home');
|
|
}
|
|
|
|
/** @var \Illuminate\Http\UploadedFile $sticker */
|
|
$sticker = $request->sticker;
|
|
/** @var \Illuminate\Http\UploadedFile $ticket */
|
|
$ticket = $request->ticket;
|
|
/** @var \Illuminate\Http\UploadedFile $barcode */
|
|
$barcode = $request->barcode;
|
|
|
|
// * Mise à jour de l'utilisateur en local.
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer->fill($request->validated());
|
|
$customer->saveOrFail();
|
|
|
|
// Mise à jour utilisateur dans la CRM.
|
|
$update = $this->apiCrm->updateCustomer(
|
|
$customer->crm_id,
|
|
$customer->firstname,
|
|
$customer->lastname,
|
|
null,
|
|
null,
|
|
$customer->phone,
|
|
$customer->address,
|
|
$customer->address2,
|
|
$customer->town,
|
|
$customer->postal_code,
|
|
ucfirst($request->country)
|
|
);
|
|
if ($update->status !== 'OK') {
|
|
Log::warning('Erreur lors de la mise a jour du user (crm_id : ' . $customer->crm_id .
|
|
') - code : ' . $update->code . ' - message : ' . $update->message);
|
|
Mail::to(config('mail.MAIL_ADMIN'))->send(new NotifAdmin(
|
|
'CRM : Erreur lors de la mise a jour du user (crm_id : ' .
|
|
$customer->crm_id . ') - code : ' . $update->code . ' - message : ' . $update->message
|
|
));
|
|
}
|
|
|
|
$participationId = $participation->id;
|
|
$stickerFileName = $participationId . '-sticker.' . $sticker->extension();
|
|
$sticker->move(public_path('fichiers/preuve-de-gain/' . $participationId . '/'), $stickerFileName);
|
|
$stickerPath = public_path() . '/fichiers/preuve-de-gain/' . $participationId . '/' . $stickerFileName;
|
|
|
|
$ticketFileName = $participationId . '-ticket.' . $ticket->extension();
|
|
$ticket->move(public_path('fichiers/preuve-de-gain/' . $participationId . '/'), $ticketFileName);
|
|
$ticketPath = public_path() . '/fichiers/preuve-de-gain/' . $participationId . '/' . $ticketFileName;
|
|
|
|
$barcodeFileName = $participationId . '-code-barre.' . $barcode->extension();
|
|
$barcode->move(public_path('fichiers/preuve-de-gain/' . $participationId . '/'), $barcodeFileName);
|
|
$barcodePath = public_path() . '/fichiers/preuve-de-gain/' . $participationId . '/' . $barcodeFileName;
|
|
|
|
$participation->sticker = $stickerFileName;
|
|
$participation->ticket = $ticketFileName;
|
|
$participation->barcode = $barcodeFileName;
|
|
$participation->saveOrFail();
|
|
|
|
/** @var \App\Models\Settings $settings */
|
|
$settings = app(Settings::class);
|
|
$mails = \collect($settings->notification_emails);
|
|
foreach ($mails as $mail) {
|
|
Mail::to($mail)->send(new WinningProofMail(
|
|
$stickerPath,
|
|
$ticketPath,
|
|
$barcodePath,
|
|
$participation,
|
|
$customer,
|
|
$settings
|
|
));
|
|
}
|
|
|
|
return view('front.basic.replay');
|
|
}
|
|
}
|