96 lines
3.2 KiB
PHP
96 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Fo\MesBr\PrintCouponsRequest;
|
|
use App\Models\Customer;
|
|
use Illuminate\Support\Arr;
|
|
|
|
class DiscountController extends Controller
|
|
{
|
|
/**
|
|
* Fetch PDF BR.
|
|
*
|
|
* @return \Symfony\Component\HttpFoundation\StreamedResponse
|
|
*/
|
|
public function fetchBrPdf(): \Symfony\Component\HttpFoundation\StreamedResponse
|
|
{
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = auth('frontend')->user();
|
|
|
|
$pdf = $this->fetchBrPdfFromMesBr($customer);
|
|
|
|
if ($pdf === null) {
|
|
\back()->withErrors([
|
|
'coupon' => trans('Impossible de récupérer le fichier pdf')
|
|
])->throwResponse();
|
|
}
|
|
|
|
return response()->streamDownload(function () use ($pdf) {
|
|
echo $pdf;
|
|
}, trans('mes-bons-de-reduction-:date.pdf', [
|
|
'date' => now()->isoFormat('DD-MM-Y-HH\hmm')
|
|
]), [
|
|
'Cache-Control' => 'public, must-revalidate, max-age=0',
|
|
'Content-Description' => 'File Transfer',
|
|
'Content-Type' => 'application/pdf',
|
|
'Expires' => 'Sat, 26 Jul 1997 05:00:00 GMT',
|
|
'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT',
|
|
'X-Generator' => 'mPDF'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Fetch coupons PDF.
|
|
*
|
|
* @param \App\Models\Customer $customer
|
|
* @return string|null
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
|
*/
|
|
private function fetchBrPdfFromMesBr(Customer $customer): ?string
|
|
{
|
|
// phpcs:enable
|
|
// * Phase - 1 Recupération des informations CRM
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = auth('frontend')->user();
|
|
/** @var \LDC\LDCCRM\Models\Customer $crmCustomer */
|
|
$crmCustomer = $customer->api_data;
|
|
|
|
try {
|
|
/** @var \LDC\MESBR\MesBr */
|
|
$mesBr = app('mesBrAPI');
|
|
$result = $mesBr->printBasketCustomer($crmCustomer->hashcode);
|
|
$result = \is_object($result) ? (array) $result : $result;
|
|
|
|
if (Arr::get($result, 'status') !== "OK") {
|
|
report(new \RuntimeException(
|
|
\sprintf(
|
|
'MESBR : Une erreur est survenue. Code : %s Message : %s',
|
|
Arr::get($result, 'code') ?? 'Undefined',
|
|
Arr::get($result, 'message') ?? 'Undefined',
|
|
)
|
|
));
|
|
return null;
|
|
}
|
|
|
|
// * La réponse est conforme ?
|
|
$needed_fields = ['pdf'];
|
|
if (!Arr::has($result, $needed_fields)) {
|
|
report(new \RuntimeException(sprintf(
|
|
'MESBR : Il manques des champs dans la réponse, attendu => %s, donné => %s',
|
|
json_encode($needed_fields),
|
|
json_encode($result),
|
|
)));
|
|
return null;
|
|
}
|
|
|
|
// * Tout est OK
|
|
return \base64_decode(Arr::get($result, 'pdf')) ?: null;
|
|
} catch (\RuntimeException $e) {
|
|
report($e);
|
|
// * Retenter ?
|
|
return null;
|
|
} //end try
|
|
}
|
|
}
|