createsDocument = $createsDocument; $this->createsFile = $createsFile; } /** * @param $transaction * @param $purchaseOrder * @param $supplier * @param $document_type * @return void * @throws \App\Classes\Exceptions\MalformedRequestException */ public function execute($transaction, $purchaseOrder, $supplier, $document_type, $voucherRedemption = null) { // calculate current Paid Amount $booking = $transaction->owner_type == Booking::class ? $transaction->owner : null; $currentPaidAmount = null; $lastPaymentDate = $supplier->segments->whereIn('id', [23])->first() ? \Carbon\Carbon::now() : $purchaseOrder->booking->created_at; $eInvoiceStartDate = Carbon::parse(env('E_INVOICE_START_DATE', '2025-07-01 00:00:00')); if ($booking) { $bookingCreatedDate = Carbon::parse($booking->created_at); if ($bookingCreatedDate->isAfter($eInvoiceStartDate)) { $lastPaymentDate = $booking->transactions()->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::COMPLETED, ApprovalStatus::APPROVED])->latest()->first()->created_at; } $payment = $booking->transactions()->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::COMPLETED, ApprovalStatus::APPROVED])->first(); $refundAmount = $payment->transactions()->refunds()->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->sum('amount'); $paymentAmount = $payment->amount; $currentPaidAmount = $paymentAmount - $refundAmount; } $lowercaseDocumentType = strtolower($document_type); $order_pdf = LaravelMpdf::loadView('pages.pdfs.' . $lowercaseDocumentType, ['transaction' => $transaction, 'po_order_transaction' => $purchaseOrder, 'supplier' => $supplier, 'voucher_redemption' => $voucherRedemption, 'current_paid_amount' => $currentPaidAmount, 'last_payment_date' => $lastPaymentDate ]); if($purchaseOrder && $purchaseOrder->booking->service_id === 4) { $purchaseOrderDocuments = $purchaseOrder->booking->documents()->where('document_type', DocumentType::ECOMMERCE_PURCHASE_ORDER)->get(); $oMerger = PDFMerger::init(); $saveDirectory = storage_path('app/documents'); if (!file_exists($saveDirectory)) { mkdir($saveDirectory, 0755, true); } $order_pdf->save(storage_path('app/documents/temp.pdf')); $oMerger->addPDF(storage_path('app/documents/temp.pdf'), 'all'); $filesystemDriver = Storage::getDefaultDriver(); foreach ($purchaseOrderDocuments as $document){ $currentFile = $document->files()->first()->file->file_info->original->file; if($filesystemDriver === 's3'){ Log::info('CreateInvoiceDocumentProcessor 1: ' . $currentFile); $fileContent = Storage::disk('s3')->get($currentFile); $filepath = storage_path('app/documents/' . $currentFile); $directoryPath = dirname($filepath); if (!file_exists($directoryPath)) { mkdir($directoryPath, 0755, true); } file_put_contents($filepath, $fileContent); } $localFilePath = storage_path('app/documents/' . $currentFile); $oMerger->addPDF($localFilePath, 'all'); } $oMerger->merge(); $order_pdf = $oMerger; if($filesystemDriver === 's3'){ $finalTempPdfPath = storage_path('app/documents/temp.pdf'); $filePathForS3 = 'temp/temp.pdf'; $fileContent = file_get_contents($finalTempPdfPath); Log::info('CreateInvoiceDocumentProcessor 2 filePathForS3: ' . $filePathForS3); Storage::disk('s3')->put($filePathForS3, $fileContent); } } $document_object = new DocumentObject( $document_type, [chunk_split('data:application/pdf;base64,' . base64_encode($order_pdf->output()))], '', ApprovalStatus::COMPLETED, $lowercaseDocumentType . 's' ); /** @var Document $document */ if($document_type === DocumentType::RECEIPT_VOUCHER){ $document = $this->createsDocument->execute($transaction, $document_object); } else{ $document = $this->createsDocument->execute($purchaseOrder->booking, $document_object); } $this->createsFile->execute($document, $document_object); } }