ldc-mesbr-app/app/Http/Controllers/Api/v1/CouponsController.php

90 lines
3.3 KiB
PHP
Raw Normal View History

2021-04-27 17:29:47 +02:00
<?php
namespace App\Http\Controllers\Api\v1;
use App\Campagne;
use App\Coupon;
use App\Http\Controllers\Controller;
use App\Http\Requests\Coupons\PrintRequest;
use App\Http\Resources\StatusResource;
use App\Models\Customers;
use App\Models\PrintedWalls;
use App\Statistique;
use App\Traits\CouponTrait;
use App\Traits\GenerateBR;
use PDF;
2021-08-13 11:48:06 +02:00
use LDC\LDCCRM\CrmApiV2;
2021-06-29 16:04:22 +02:00
2021-04-27 17:29:47 +02:00
class CouponsController extends Controller
{
use CouponTrait;
use GenerateBR;
public function print(PrintRequest $request)
{
2025-04-03 09:30:16 +02:00
$datas = $request->validated();
$campagne = Campagne::find($datas['refcamp']);
$customer = Customers::find($datas['hashcode']);
2021-04-27 17:29:47 +02:00
$couponsToPrint = explode(",", $datas['coupons']);
2021-06-29 16:04:22 +02:00
if ($campagne->site->crm_token === null) {
2025-04-03 09:30:16 +02:00
return new StatusResource(['status' => 'KO', 'code' => 500, 'message' => 'CRM Token is missing for site_id : ' . $campagne->site->site_id]);
2021-08-20 11:24:10 +02:00
}
if ($campagne->site->crm_password === null) {
2025-04-03 09:30:16 +02:00
return new StatusResource(['status' => 'KO', 'code' => 500, 'message' => 'CRM Password is missing for site_id : ' . $campagne->site->site_id]);
2021-06-29 16:04:22 +02:00
}
2021-04-27 17:29:47 +02:00
if (empty($couponsToPrint)) {
return new StatusResource(['status' => 'KO', 'code' => 422, 'message' => 'Coupons not found.']);
}
2021-08-13 11:48:06 +02:00
$crm = new CrmApiV2(config('crm.CRM_ENDPOINT'), $campagne->site->crm_token, $campagne->site->crm_password);
try {
/**
* @var \LDC\LDCCRM\Models\Customer
*/
$crmInfos = $crm->getCustomer($customer->hashcode);
2025-04-03 09:30:16 +02:00
} catch (\Exception $e) {
2021-08-13 11:48:06 +02:00
// TODO : Update error message
2021-04-27 17:29:47 +02:00
return new StatusResource(['status' => 'KO', 'code' => 422, 'message' => 'Customer not found.']);
}
2021-08-20 11:46:17 +02:00
if (is_null($crmInfos)) {
return new StatusResource(['status' => 'KO', 'code' => 422, 'message' => 'Customer not found.']);
}
2021-04-27 17:29:47 +02:00
// TODO : Vérifier si les coupons sont dans la campagne
// TODO : Logs
$coupons = [];
2025-04-03 09:30:16 +02:00
if ($campagne->camp_type == 0) {
// ---- POUR LES MURS
2021-04-27 17:29:47 +02:00
foreach ($couponsToPrint as $key => $coupon_code) {
$cp = Coupon::where(['coupon_code' => $coupon_code])->first();
if (!$this->isUsed($cp, $campagne, $customer)) {
$coupons[] = $this->printCoupon($cp, $customer, $crmInfos);
2025-04-03 09:30:16 +02:00
// ----- mémorise l'impression
$mur = new PrintedWalls();
$mur->camp_id = $campagne->camp_id;
2021-04-27 17:29:47 +02:00
$mur->customer_id = $customer->id;
2025-04-03 09:30:16 +02:00
$mur->coupon_id = $cp->coupon_id;
2021-04-27 17:29:47 +02:00
$mur->save();
2025-04-03 09:30:16 +02:00
// Sauvegarde de stat
$stat = new Statistique();
$stat->stat_campId = $campagne->camp_id;
$stat->stat_couponId = $cp->coupon_id;
2021-04-27 17:29:47 +02:00
$stat->stat_couponPrinted = 1;
$stat->save();
}
2025-04-03 09:30:16 +02:00
}//end foreach
}//end if
2021-04-27 17:29:47 +02:00
$pdf = PDF::loadView('pdf.coupons', ['coupons' => $coupons]);
return new StatusResource(['status' => 'OK', 'pdf' => base64_encode($pdf->stream(sprintf("coupons_%s_%s_%s.pdf", $crmInfos->firstname, $crmInfos->lastname, date('d-m-Y'))))]);
}
}