ldc-legaulois/app/Http/Controllers/Club/DiscountController.php
2026-09-25 16:25:31 +02:00

150 lines
4.6 KiB
PHP

<?php
namespace App\Http\Controllers\Club;
use App\Http\Controllers\Controller;
use App\Http\Requests\Club\MesBr\PrintCouponsRequest;
use App\Models\Discount;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Arr;
use LDC\LDCCRM\Models\Customer;
class DiscountController extends Controller
{
/**
* Show customer BR list.
*/
public function showCustomerBrList(): 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.
*/
public function fetchBrPdf(PrintCouponsRequest $request): RedirectResponse
{
/** @var \LDC\MESBR\MesBr */
$mesBr = app('mesBrAPI');
/** @var \App\Models\Customer $customer */
$customer = auth('club')->user();
/** @var array $coupons */
$coupons = $request->post('coupon');
return redirect()->to($mesBr->getPrintMurCouponsUrl($customer->hashcode, $coupons));
}
/**
* 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"
* }
*/
private function fetchActiveBr(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.
*/
private function createOrUpdateMesBrCustomer(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
}
}