201 lines
6.2 KiB
PHP
201 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\ParticipationRequest;
|
|
use App\Http\Requests\WinnerRequest;
|
|
use App\Mail\GagneEmail;
|
|
use App\Mail\UnUserAGagne;
|
|
use App\Models\EanCode;
|
|
use App\Models\InstantGagnant;
|
|
use App\Models\Magasin;
|
|
use App\Models\Participation;
|
|
use App\Models\User;
|
|
use App\Models\Winner;
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use setasign\Fpdi\Fpdi;
|
|
|
|
class GameController extends Controller
|
|
{
|
|
public function participation()
|
|
{
|
|
$magasins = Magasin::all();
|
|
try {
|
|
$this->authorize('participate');
|
|
} catch (AuthorizationException $e) {
|
|
return view('participation', compact('magasins'))
|
|
->with('maxParticipation', true);
|
|
}
|
|
return view('participation', compact('magasins'));
|
|
}
|
|
|
|
public function participationSubmit(ParticipationRequest $request)
|
|
{
|
|
$this->authorize('participate');
|
|
if (is_null($request->getClientIp())) {
|
|
return back()->withErrors([
|
|
'ipError' => trans('Impossible d\'identifier l\'IP du participant')
|
|
]);
|
|
}
|
|
|
|
/** @var \App\Models\User */
|
|
$user = auth()->user();
|
|
|
|
$instantGagnant = InstantGagnant::where('magasin_id', $request->magasin)
|
|
->where('user_id', null)
|
|
->where('date', '<', Carbon::now())
|
|
->first();
|
|
|
|
// Save participation
|
|
$magasin = Magasin::where('id', $request->magasin)
|
|
->firstOrFail();
|
|
$eanCode = EanCode::where('ean', $request->ean)
|
|
->firstOrFail();
|
|
|
|
$participation = new Participation();
|
|
$participation->user_id = $user->id;
|
|
$participation->ean_code_id = $eanCode->id;
|
|
$participation->magasin_id = $magasin->id;
|
|
$participation->buydate = $request->buydate;
|
|
$participation->ip = $request->getClientIp();
|
|
$participation->saveOrFail();
|
|
|
|
if ($instantGagnant) {
|
|
$instantGagnant->user()->associate(auth()->user());
|
|
$instantGagnant->saveOrFail();
|
|
// pas de mail template, pas de mail
|
|
// Mail::to(auth()->user()->email)->send(new GagneEmail(auth()->user()));
|
|
Mail::to([
|
|
config('mail.contactsList.serviceweb')
|
|
])->send(new UnUserAGagne($user, $eanCode, $magasin));
|
|
return view('result', ['result' => true]);
|
|
} else {
|
|
return view('result', ['result' => false]);
|
|
}
|
|
}
|
|
|
|
public function winner()
|
|
{
|
|
/** @var \App\Models\User */
|
|
$user = auth()->user();
|
|
if (!$user->hasWon()->exists()) {
|
|
abort(404);
|
|
}
|
|
|
|
$unclaimedWins = $user->unclaimedInstantGagnants();
|
|
|
|
if (!count($unclaimedWins)) {
|
|
return view('winner', ['alreadySaved' => true]);
|
|
}
|
|
return view('winner', compact('user', 'unclaimedWins'));
|
|
}
|
|
|
|
public function winnerSubmit(WinnerRequest $request)
|
|
{
|
|
$this->authorize('participate');
|
|
/** @var \App\Models\User */
|
|
$user = auth()->user();
|
|
if (
|
|
Winner::where('instant_gagnant_id', $request->unclaimedWin)
|
|
->where('user_id', $user->id)
|
|
->exists()
|
|
) {
|
|
return back()->withErrors('Vous avez déjà validé votre participation', 'validated');
|
|
}
|
|
$data = $request->validated();
|
|
unset($data['file']);
|
|
$winner = new Winner([
|
|
'submitedDataForm' => $data,
|
|
'filename' => sprintf('preuve.%s', $request->file->extension()),
|
|
'file' => $request->file
|
|
]);
|
|
|
|
$winner->instant_gagnant_id = $request->unclaimedWin;
|
|
$winner->user_id = $user->id;
|
|
|
|
$winner->saveOrFail();
|
|
|
|
return view('winner', ['saved' => true]);
|
|
}
|
|
|
|
public function winnerPdf(Request $request)
|
|
{
|
|
/** @var \App\Models\User */
|
|
$user = auth()->user();
|
|
$instantGagnant = InstantGagnant::where('id', $request->unclaimedWin)
|
|
->firstOrFail();
|
|
if (!in_array($instantGagnant->id, $user->unclaimedInstantGagnants()->pluck('id')->all())) {
|
|
abort(400);
|
|
}
|
|
|
|
return response()->streamDownload(function () use ($user, $instantGagnant) {
|
|
$this->genPdf($user, $instantGagnant, 'preuve_de_gains_a_fournir.pdf');
|
|
});
|
|
}
|
|
|
|
private function genPdf(User $user, InstantGagnant $iG, string $outFileName)
|
|
{
|
|
// initiate FPDI
|
|
$pdf = new Fpdi();
|
|
$pdf->AddPage();
|
|
$pdf->setSourceFile(\resource_path('assets/documents/preuve_de_gain.pdf'));
|
|
|
|
$tplId = $pdf->importPage(1);
|
|
// use the imported page and place it at point 5,5 with your preferred width in mm
|
|
$pdf->useTemplate($tplId, 5, 5, 210);
|
|
|
|
$pdf->SetFont('Arial', '', 10); // Font Name, Font Style (eg. 'B' for Bold), Font Size
|
|
$pdf->SetTextColor(0, 0, 0); // RGB
|
|
|
|
$pdf->SetXY(105, 54); // X start, Y start in mm
|
|
$text = sprintf(
|
|
"#%d - %s %s",
|
|
$user->id,
|
|
$user->lastname,
|
|
$user->firstname
|
|
);
|
|
$pdf->Write(0, utf8_decode($text));
|
|
|
|
$pdf->SetXY(105, 58);
|
|
$text = $user->address;
|
|
$pdf->Write(0, utf8_decode($text));
|
|
|
|
$pdf->SetXY(105, 62);
|
|
$text = $user->address2;
|
|
$pdf->Write(0, utf8_decode($text));
|
|
|
|
$pdf->SetXY(105, 66);
|
|
$text = sprintf(
|
|
"%d %s",
|
|
$user->postcode,
|
|
$user->city,
|
|
);
|
|
$pdf->Write(0, utf8_decode($text));
|
|
|
|
$pdf->SetXY(105, 70);
|
|
$text = sprintf(
|
|
"Magasin : %s",
|
|
$iG->magasin->name
|
|
);
|
|
$pdf->Write(0, utf8_decode($text));
|
|
|
|
$pdf->SetXY(105, 74);
|
|
$text = sprintf(
|
|
"Code EAN : %d",
|
|
$iG->participation()->eanCode->ean
|
|
);
|
|
$pdf->Write(0, utf8_decode($text));
|
|
$pdf->SetXY(105, 78);
|
|
$text = sprintf(
|
|
"Date d'achat : %s",
|
|
$iG->participation()->buydate->format('d/m/Y')
|
|
);
|
|
$pdf->Write(0, utf8_decode($text));
|
|
|
|
return $pdf->Output('', $outFileName);
|
|
}
|
|
}
|