contents->sum(function ($content) { return $content->quantity - $content->printed; }); return view('fo.basket.show', compact('basket', 'totalQuantity')); } // FIXME: Docblock public function print(PrintRequest $request, Basket $basket) { $campagne = $basket->campagne; // FIXME: Bump to CrmApiV4 $crm = new CrmApiV2(config('services.crm.endpoint'), $campagne->site->crm_token, $campagne->site->crm_password); /** * @var \LDC\LDCCRM\Models\Customer */ $crmInfos = $crm->getCustomer($basket->customer->hashcode); if (is_null($crmInfos)) { throw new \Exception('Customer not found.'); } $coupons = []; $currentDateTime = date('Y-m-d H:i:s'); foreach ($basket->contents as $content) { $coupon = $content->coupon()->whereRaw( "coupon_published = 1 AND (coupon_mindatediffusion IS NULL OR coupon_mindatediffusion <= NOW()) AND (coupon_maxdatediffusion IS NULL OR coupon_maxdatediffusion >= NOW()) AND (coupon_maxdatevalidity IS NULL OR coupon_maxdatevalidity >= NOW())" )->first(); if ($coupon !== null) { $calendars = Calendrier::where('calendrier_couponId', $coupon->coupon_id)->get(); $published = true; if (!$calendars->isEmpty()) { $published = false; foreach ($calendars as $calendar) { if ($currentDateTime > $calendar->calendrier_startdate && $currentDateTime < $calendar->calendrier_enddate) { $published = true; break; // Exit loop early as condition is met } } } if ($published) { $qtyToPrint = $content->quantity - $content->printed; for ($i = 0; $i < $qtyToPrint; $i++) { $coupons[] = $this->printCoupon($coupon, $basket->customer, $crmInfos, $campagne); } $content->printed = $content->quantity; $content->save(); } } } // $coupons contains all printed coupons $pdf = PDF::loadView('pdf.coupons', ['coupons' => $coupons]); return $pdf->download(sprintf("coupons_%s_%s_%s.pdf", $crmInfos->firstname, $crmInfos->lastname, date('d-m-Y'))); } }