116 lines
5 KiB
PHP
116 lines
5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Fo\SubmitWinningProofRequest;
|
|
use App\Mail\WinningProofMail;
|
|
use App\Models\Participation;
|
|
use App\Models\Settings;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use LDC\LDCCRM\Exceptions\ApiErrorException;
|
|
use LDC\LDCCRM\Exceptions\CrmException;
|
|
use LDC\LDCCRM\Exceptions\ValidationException;
|
|
|
|
class WinningProofController extends Controller
|
|
{
|
|
/**
|
|
* Submit Winning proof
|
|
*
|
|
* @param SubmitWinningProofRequest $request
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
*/
|
|
public function sendWinningProof(SubmitWinningProofRequest $request)
|
|
{
|
|
try {
|
|
return DB::transaction(function () use ($request) {
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = auth('customer')->user();
|
|
|
|
/** @var \LDC\LDCCRM\Models\Customer $crmCustomer */
|
|
$crmCustomer = $customer->apiData;
|
|
|
|
// 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 utilisateur dans la CRM.
|
|
try {
|
|
$crmCustomer->fill(\array_intersect_key($request->validated(), [
|
|
'firstname' => '',
|
|
'lastname' => '',
|
|
'address' => '',
|
|
'postcode' => '',
|
|
'country' => '',
|
|
'city' => '',
|
|
'phone' => '',
|
|
]));
|
|
$crmCustomer->save();
|
|
} catch (ApiErrorException | ValidationException $e) {
|
|
return back()->withErrors($e->getMessage());
|
|
} catch (\Exception | \TypeError | CrmException $e) {
|
|
\report($e);
|
|
} //end try
|
|
|
|
$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);
|
|
// ? Is settings.notification_emails empty ?
|
|
if (!$mails->count()) {
|
|
// ! Send to null (will send to MAIL_ADMIN and print warning in mail object)
|
|
$mails = [null];
|
|
}
|
|
foreach ($mails as $mail) {
|
|
Mail::to($mail)->send(new WinningProofMail(
|
|
$stickerPath,
|
|
$ticketPath,
|
|
$barcodePath,
|
|
$participation,
|
|
$crmCustomer,
|
|
$settings
|
|
));
|
|
}
|
|
return view('front.basic.replay');
|
|
});
|
|
} catch (\Exception $e) {
|
|
report($e);
|
|
return back()->withErrors('Une erreur est survenue, merci de réessayez ultérieurement.');
|
|
}//end try
|
|
}
|
|
}
|