ldc-gen-game-web/app/Http/Controllers/WinningProofController.php
Jérémy Munsch 1af96897f3
- Squash migration
- Normalize Model names and sql tables
- PHPCS clean up
- Change analytics form + field
2022-01-11 12:46:10 +01:00

133 lines
5.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Mail\NotifAdmin;
use App\Mail\WinningProof;
use App\Models\Participation;
use App\Models\Settings;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
class WinningProofController extends Controller
{
private $apiCrm;
public function __construct()
{
$this->apiCrm = new \LDCCRM\ApiV1(config('crm.endpoint'), config('crm.token'));
}
public function sendWinningProof(Request $request)
{
// On récupère la participation gagnante en attente des infos
$participation = Participation::where([
['user_id', Auth::user()->id],
['sticker', null],
['ticket', null],
['barcode', null]
])->has('instantWin')->first();
if (!$participation) {
return redirect()->route('home');
}
$request->validate([
'lastname' => 'required|string|max:255',
'firstname' => 'required|string|max:255',
'address' => 'required|string|max:255',
'address2' => 'max:255',
'town' => 'required|string|max:255',
'postal_code' => 'required|max:255',
'country' => 'required|string|max:255',
'phone' => 'required|string|max:50',
'sticker' => 'required|mimes:jpeg,png,jpg,pdf|max:2048',
'ticket' => 'required|mimes:jpeg,png,jpg,pdf|max:2048',
'barcode' => 'required|mimes:jpeg,png,jpg,pdf|max:2048',
]);
$lastname = filter_var($request->lastname, FILTER_SANITIZE_STRING);
$firstname = filter_var($request->firstname, FILTER_SANITIZE_STRING);
$address = filter_var($request->address, FILTER_SANITIZE_STRING);
$address2 = $request->address2 !== null ? filter_var($request->address2, FILTER_SANITIZE_STRING) : null;
$town = filter_var($request->town, FILTER_SANITIZE_STRING);
$postalCode = filter_var($request->postal_code, FILTER_SANITIZE_STRING);
$country = filter_var($request->country, FILTER_SANITIZE_STRING);
$phone = filter_var($request->phone, FILTER_SANITIZE_STRING);
$sticker = $request->sticker;
$ticket = $request->ticket;
$barcode = $request->barcode;
// Mise à jour de l'utilisateur en local.
$user = User::find(Auth::user()->id);
$user->lastname = $lastname;
$user->firstname = $firstname;
$user->address = $address;
$user->address2 = $address2;
$user->town = $town;
$user->postal_code = $postalCode;
$user->country = $country;
$user->phone = $phone;
$user->save();
// Mise à jour utilisateur dans la CRM.
$update = $this->apiCrm->updateCustomer(
$user->crm_id,
$firstname,
$lastname,
null,
null,
$phone,
$address,
$address2,
$town,
$postalCode,
ucfirst($country)
);
if ($update->status !== 'OK') {
Log::warning('Erreur lors de la mise a jour du user (crm_id : ' . $user->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 : ' .
$user->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->save();
/** @var \App\Models\Settings $settings */
$settings = app(Settings::class);
$mails = json_decode($settings->notification_emails);
foreach ($mails as $mail) {
Mail::to($mail->email)->send(new WinningProof(
$stickerPath,
$ticketPath,
$barcodePath,
$participation,
$user,
$settings
));
}
return view('front.basic.replay');
}
}