267 lines
9.4 KiB
PHP
267 lines
9.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Club;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Club\MesBr\PrintCouponsRequest;
|
|
use App\Models\Customer;
|
|
use App\Models\Discount;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class DiscountController extends Controller
|
|
{
|
|
/**
|
|
* Show customer BR list.
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function showCustomerBrList(): \Illuminate\Contracts\View\View
|
|
{
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = auth('club')->user();
|
|
/** @var \LDC\LDCCRM\Models\Customer $crmCustomer */
|
|
$crmCustomer = $customer->api_data;
|
|
|
|
$brList = $this->fetchActiveBr($crmCustomer);
|
|
return view('club.pages.discounts.index', compact('crmCustomer', 'brList'));
|
|
}
|
|
|
|
/**
|
|
* Fetch PDF BR.
|
|
*
|
|
* @param \App\Http\Requests\Club\MesBr\PrintCouponsRequest $request
|
|
* @return \Symfony\Component\HttpFoundation\StreamedResponse
|
|
*/
|
|
public function fetchBrPdf(PrintCouponsRequest $request): \Symfony\Component\HttpFoundation\StreamedResponse
|
|
{
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = auth('club')->user();
|
|
|
|
/** @var array $coupons */
|
|
$coupons = $request->post('coupon');
|
|
|
|
$pdf = $this->fetchBrPdfFromMesBr($customer, $coupons);
|
|
|
|
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
|
|
* @param array $coupons
|
|
* @return string|null
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
|
*/
|
|
private function fetchBrPdfFromMesBr(Customer $customer, array $coupons): ?string
|
|
{
|
|
// phpcs:enable
|
|
// * Phase - 1 Recupération des informations CRM
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = auth('club')->user();
|
|
/** @var \LDC\LDCCRM\Models\Customer $crmCustomer */
|
|
$crmCustomer = $customer->api_data;
|
|
|
|
// * Phase - 2 Mise à jour du coupon dans MesBr
|
|
try {
|
|
/** @var \LDC\MESBR\MesBr */
|
|
$mesBr = app('mesBrAPI');
|
|
|
|
// * MaJ Customer
|
|
retry(3, function () use ($mesBr, $crmCustomer) {
|
|
/**
|
|
* Parce que MesBr n'est pas fiable il faut retenter pour avoir
|
|
* l'id client, des fois cet id sera dans hashcode...
|
|
*
|
|
* Id est necessaire pour update panier..
|
|
*/
|
|
$result = $mesBr->updateCustomer(
|
|
$crmCustomer->hashcode,
|
|
$crmCustomer->email,
|
|
$crmCustomer->country->label()
|
|
);
|
|
if (optional($result)->status !== "OK") {
|
|
throw new \RuntimeException(
|
|
\sprintf(
|
|
'MESBR : Une erreur est survenue. Code : %s Message : %s',
|
|
$result->code ?? 'Undefined',
|
|
$result->message ?? 'Undefined'
|
|
)
|
|
);
|
|
}
|
|
/** @var object|null */
|
|
$crm_customer = optional($result)->customer;
|
|
if (empty(optional($crm_customer)->id)) {
|
|
throw new \RuntimeException(
|
|
'MESBR : Une erreur est survenue. Update Customer n\'as pas retourné le customer id'
|
|
);
|
|
}
|
|
|
|
return intval($crm_customer->id);
|
|
}, 100);
|
|
|
|
// * Maj Panier
|
|
$result = $mesBr->printCoupons($crmCustomer->hashcode, $coupons);
|
|
$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;
|
|
}
|
|
|
|
// Update customer discounts count.
|
|
$cacheKey = "discountsCustomerCount_{$customer->hashcode}";
|
|
Cache::forget($cacheKey);
|
|
Cache::remember($cacheKey, 86400, function () use ($customer) {
|
|
return Discount::getCustomerDiscountsCount($customer->hashcode);
|
|
});
|
|
|
|
// * Tout est OK
|
|
return \base64_decode(Arr::get($result, 'pdf')) ?: null;
|
|
} catch (\RuntimeException $e) {
|
|
report($e);
|
|
// * Retenter ?
|
|
return null;
|
|
} //end try
|
|
}
|
|
|
|
/**
|
|
* Fetch customer BRs.
|
|
*
|
|
* This should be an object like
|
|
*
|
|
* +"status": "OK"
|
|
* +"name": "Test tregalette"
|
|
* +"limited_printing": 0
|
|
* +"total": 15
|
|
* +"available_amount": 10.5
|
|
* +"total_amount": 10.5
|
|
* +"coupons": array{
|
|
* +"code": "2950675055365"
|
|
* +"name": "Webcoupon 1€ Filet de canard Confit"
|
|
* +"value": 1
|
|
* +"used": false
|
|
* +"desktop_image_available": "https://dev.mesbr.eu/coupons/96kjV1ZwGb7Z4m8wyzBCuurEFPlSLHFaAGVWuO8F.png"
|
|
* +"desktop_image_unavailable": "https://dev.mesbr.eu/coupons/K7BSVFOCs2K1XCi78wR2YgEfGyFH6AJthyupTEYz.png"
|
|
* +"mobile_image_available": "https://dev.mesbr.eu/coupons/KE3YodVeDPSG7pVzPQZhPEpVGlxwjTPWCJ8WGN29.png"
|
|
* +"mobile_image_unavailable": "https://dev.mesbr.eu/coupons/FOFhdT0d2X1AURQfYm9O7ac2ca9SGUk3IHCTWJiZ.png"
|
|
* }
|
|
*
|
|
* @param \LDC\LDCCRM\Models\Customer $customer
|
|
* @return object|null
|
|
*/
|
|
private function fetchActiveBr(\LDC\LDCCRM\Models\Customer $customer): ?object
|
|
{
|
|
if (!$this->createOrUpdateMesBrCustomer($customer)) {
|
|
return null;
|
|
}
|
|
|
|
/** @var \LDC\MESBR\MesBr */
|
|
$mesBr = app('mesBrAPI');
|
|
|
|
$message = null;
|
|
|
|
try {
|
|
$res = $mesBr->getMurCustomer($customer->hashcode);
|
|
if (!$res) {
|
|
$message = "ERROR MesBR API : getMurCustomer on api failed {$customer->hashcode}";
|
|
} elseif (Arr::get((array) $res, 'status') === 'KO') {
|
|
$message = Arr::get(
|
|
(array) $res,
|
|
'message',
|
|
"ERROR MesBR API : getMurCustomer on api failed (no message) {$customer->hashcode}"
|
|
);
|
|
}
|
|
|
|
if ($message === null) {
|
|
// * Replace local picture urls for dev Env
|
|
if (config('app.env') === 'local') {
|
|
$res = Discount::replaceCouponsPictureHost($res);
|
|
}
|
|
return $res;
|
|
}
|
|
report(new \RuntimeException($message));
|
|
return null;
|
|
} catch (\Exception $e) {
|
|
report($e);
|
|
return null;
|
|
} //end try
|
|
}
|
|
|
|
/**
|
|
* Report for sure true if the customer is set in mesBr.
|
|
*
|
|
* @param \LDC\LDCCRM\Models\Customer $customer
|
|
* @return boolean
|
|
*/
|
|
private function createOrUpdateMesBrCustomer(\LDC\LDCCRM\Models\Customer $customer): bool
|
|
{
|
|
// * Ajout du customer sur mes BR
|
|
/** @var \LDC\MESBR\MesBr */
|
|
$mesBr = app('mesBrAPI');
|
|
$message = null;
|
|
|
|
/**
|
|
* Observed behavior,
|
|
* calling multiple times createCustomer results in
|
|
* create or update the customer.
|
|
*/
|
|
try {
|
|
// * Create (or update) the customer
|
|
$res = $mesBr->createCustomer($customer->hashcode, $customer->email, 'France');
|
|
if (!$res) {
|
|
$message = "ERROR MesBR API : createCustomer on api failed {$customer->hashcode}";
|
|
} elseif (Arr::get((array) $res, 'status') === 'KO') {
|
|
$message = Arr::get(
|
|
(array) $res,
|
|
'message',
|
|
"ERROR MesBR API : createCustomer on api failed (no message) {$customer->hashcode}"
|
|
);
|
|
}
|
|
|
|
if ($message === null) {
|
|
return true;
|
|
}
|
|
report(new \RuntimeException($message));
|
|
return false;
|
|
} catch (\Exception $e) {
|
|
report($e);
|
|
return false;
|
|
} //end try
|
|
}
|
|
}
|