fetchesTransaction = $fetchesTransaction; $this->fetchesCompany = $fetchesCompany; } /** * @param Request $request * @return string|\Symfony\Component\HttpFoundation\Response * @throws \App\Classes\Exceptions\MalformedRequestException */ public function execute(Request $request) { $pdfTemplateName = 'pages.pdfs.credit_note_v2'; //default since e-invoice implementation $transaction = $this->fetchesTransaction->execute(['id' => $request->route('id')]); if($transaction->type === TransactionType::REFUND){ //Retrieve TransactionType::CREDIT_NOTE $booking = $transaction->owner->booking; $kvp = $transaction->attributesKVP()->latest()->first(); if($kvp){ if($kvp->key === 'App\Models\Transaction'){ $transaction = $this->fetchesTransaction->execute(['id' => $kvp->value ]); } } } else{ //For Old Cases $booking = $transaction->booking; $pdfTemplateName = 'pages.pdfs.credit_note'; //default //For New Cases with e-invoice: Retrieve the refund transaction for this credit note $kvp = KeyValuePair::where('key', 'App\Models\Transaction')->where('value', $transaction->id)->first(); if($kvp){ $kvpOwner = $kvp->owner; if($kvpOwner && $kvpOwner instanceof Transaction && $kvpOwner->type === TransactionType::REFUND){ $booking = $kvpOwner->owner->booking; } } } $date = $transaction->created_at; $supplier = $this->fetchesCompany->execute(['id' => $transaction->receiver]); $brn = $supplier->documents->where('document_type', DocumentType::SSM_REGISTRATION)->first(); $eInvoiceStarted = false; $eInvoiceStartDate = Carbon::parse(env('E_INVOICE_START_DATE', '2025-07-01 00:00:00')); $bookingCreatedDate = Carbon::parse($booking->created_at); if ($bookingCreatedDate->isAfter($eInvoiceStartDate)) { $eInvoiceStarted = true; } // $eInvoiceStarted = false; //cief todo: 90 - for testing if($eInvoiceStarted) { if($supplier->e_invoice === 1){ Log::info('Based on booking created date, E-Credit Note started and company wants e-invoice ' . json_encode($booking)); $date = $booking->updated_at->copy()->endOfMonth(); $pdfTemplateName = 'pages.pdfs.e_credit_note'; } else{ Log::info('Based on booking created date, E-Credit Note started and company do not wants e-invoice'); $pdfTemplateName = 'pages.pdfs.credit_note_v2'; } } else{ Log::info('Based on booking created date, E-Credit Note not yet started'); } $pdf = LaravelMpdf::loadView($pdfTemplateName, ['transaction' => $transaction, 'booking' => $booking, 'supplier' => $supplier, 'date' => $date, 'brn' => $brn,]); $exportFileName = 'CreditNote.pdf'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ $pdfContent = $pdf->output(); return response([ 'src' => AWSS3Helper::S3PDF($exportFileName, $pdfContent) ]); } else{ return $pdf->stream($exportFileName); } } }