160 lines
5.7 KiB
PHP
160 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Fo\SubmitWinningProofRequest;
|
|
use App\Mail\Won\WinningProofAdminMail;
|
|
use App\Models\Participation;
|
|
use App\Models\Settings;
|
|
use App\Notifications\Customer\HasSubmitedWinProof;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Validation\ValidationException as IlluminateValidationException;
|
|
use LDC\LDCCRM\Enums\ApiError;
|
|
use LDC\LDCCRM\Exceptions\ApiErrorException;
|
|
use LDC\LDCCRM\Exceptions\ValidationException as CrmValidationException;
|
|
|
|
class WinningProofController extends Controller
|
|
{
|
|
/**
|
|
* WinningProofController
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->middleware(function (Request $request, callable $next) {
|
|
$this->authorize('fo.gameEnabled');
|
|
$this->authorize('submitProof', $request->route('participation'));
|
|
return $next($request);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Display winning proof form
|
|
*
|
|
* @param \App\Models\Participation $participation
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function displayForm(Participation $participation): View
|
|
{
|
|
$participation->load('instantWin.prize');
|
|
|
|
/** @var \App\Models\Customer */
|
|
$customer = auth('frontend')->user();
|
|
|
|
$crmCustomer = $customer->api_data;
|
|
|
|
return view('theme::pages.participation.won-form-only', [
|
|
'participationModel' => $participation,
|
|
'prizeModel' => $participation->instantWin->prize,
|
|
'crmCustomer' => $crmCustomer
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Submit winning proofs,
|
|
* - 1 Met à jour le profil du customer (sans lui en faire part),
|
|
* - 2 Save les pictures de preuve de gains
|
|
* - 3 Envoi un mail au abonnés (settings notification_emails) avec les infos client
|
|
*
|
|
* @param \App\Http\Requests\Fo\SubmitWinningProofRequest $request
|
|
* @param \App\Models\Participation $participation
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
* @throws IlluminateValidationException In case of failure.
|
|
* @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
|
|
*/
|
|
public function displayFormSubmit(
|
|
SubmitWinningProofRequest $request,
|
|
Participation $participation
|
|
): RedirectResponse {
|
|
// phpcs:enable
|
|
try {
|
|
return DB::transaction(function () use ($request, $participation) {
|
|
$participation->load([
|
|
'instantWin.prize',
|
|
'code',
|
|
'product'
|
|
]);
|
|
|
|
/** @var \App\Models\Settings */
|
|
$settings = app(Settings::class);
|
|
|
|
// * --- PHASE 1 Update Crm Customer ---
|
|
/** @var \App\Models\Customer */
|
|
$customer = auth('frontend')->user();
|
|
|
|
$crmCustomer = $customer->api_data;
|
|
/** @var array */
|
|
$data = $request->validated();
|
|
|
|
// * This field is not standard in crm
|
|
$data['postcode'] = Arr::get($data, 'postal_code');
|
|
Arr::forget($data, 'postal_code');
|
|
|
|
$customer->updateApiData(Arr::only(
|
|
$data,
|
|
[
|
|
'firstname',
|
|
'lastname',
|
|
'birth_date',
|
|
'address',
|
|
'address2',
|
|
'postcode',
|
|
'country',
|
|
'city',
|
|
'phone',
|
|
'email',
|
|
]
|
|
));
|
|
|
|
// * --- PHASE 2 Update participation ---
|
|
$participation->fill(Arr::only($request->validated(), [
|
|
'barcode',
|
|
'ticket',
|
|
'sticker'
|
|
]));
|
|
$participation->save();
|
|
|
|
$customer->notify(new HasSubmitedWinProof($request->input('firstname')));
|
|
|
|
// * Dispatch notification to mail list
|
|
Mail::to($settings->notification_emails)->send(new WinningProofAdminMail(
|
|
$settings->name,
|
|
$participation->created_at,
|
|
$participation->instantWin->prize->label,
|
|
$crmCustomer,
|
|
optional($participation->code)->code_unique,
|
|
optional($participation->product)->ean_code,
|
|
$participation->sticker,
|
|
$participation->ticket,
|
|
$participation->barcode,
|
|
));
|
|
|
|
return response()->redirectToRoute('fo.homepage')
|
|
->with('success', trans(
|
|
'Nous avons bien pris en compte vos preuve de gains, vous serez contactés prochainement'
|
|
));
|
|
});
|
|
} catch (ApiErrorException $e) {
|
|
if ($e->getApiError()->equals(ApiError::validationFailed)) {
|
|
throw IlluminateValidationException::withMessages([
|
|
'error' => $e->getMessage()
|
|
]);
|
|
}
|
|
report($e);
|
|
Log::error(__CLASS__ . '::create reached end of possibilities.');
|
|
throw new \RuntimeException(__CLASS__ . '::create reached end of possibilities.');
|
|
} catch (CrmValidationException $e) {
|
|
throw IlluminateValidationException::withMessages([
|
|
'error' => $e->getMessage()
|
|
]);
|
|
} //end try
|
|
}
|
|
}
|