diff --git a/app/Classes/Modules/Bookings/ControllersLogic/DownloadBookingDocumentLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/DownloadBookingDocumentLogic.php index d8fdb0df..de4b59f1 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/DownloadBookingDocumentLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/DownloadBookingDocumentLogic.php @@ -14,6 +14,7 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; use App\Classes\Exceptions\MalformedRequestException; use App\Classes\ValueObjects\Constants\ApprovalStatus; +use App\Classes\ValueObjects\Constants\DocumentType; class DownloadBookingDocumentLogic { @@ -26,9 +27,9 @@ class DownloadBookingDocumentLogic */ public function execute(Request $request) { - Auth::login(User::findOrFail(1)); - $zip_file = $request->input('type').'.zip'; + $document_type = str_replace(' ', '', $request->input('type')); + $zip_file = $document_type.'.zip'; $attachment = storage_path().'/app/documents/collections/' . $zip_file; $zip = new ZipArchive(); @@ -36,19 +37,40 @@ class DownloadBookingDocumentLogic $bookings = Booking::where('status', ApprovalStatus::COMPLETED) ->whereDate('created_at', '>=', Carbon::parse($request->input('startDate'))) - ->whereDate('created_at', '<=', Carbon::parse($request->input('endDate')))->whereHas('transactions', function ($query) use ($request){ + ->whereDate('created_at', '<=', Carbon::parse($request->input('endDate'))) + ->whereHas('transactions', function ($query) use ($request){ return $query->where('type', TransactionType::PAYMENT)->whereHas('transactions', function ($query) use ($request){ return $query->where('type', TransactionType::BILL)->where('issuer', $request->input('supplier')); }); })->get(); + if (!count($bookings)) throw new MalformedRequestException('No available file to download'); foreach ($bookings as $booking) { - $file = $booking->documents()->where('document_type', $request->input('type'))->first()->files()->first(); - $zip->addFile(Storage::disk('documents')->path($file->file->file_info->original->file), $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); - } + if ($document_type == 'INVOICEPODO' || $document_type == 'INVOICEPODOSDO') { + $invoice_file = $booking->documents()->where('document_type', DocumentType::INVOICE)->first()->files()->first(); + $purchase_file = $booking->documents()->where('document_type', DocumentType::PURCHASE_ORDER)->first()->files()->first(); + $deliver_file = $booking->documents()->where('document_type', DocumentType::DELIVER_ORDER)->first()->files()->first(); + if ($document_type == 'INVOICEPODOSDO') { + $supplier_deliver_order_file = $booking->documents()->where('document_type', DocumentType::SUPPLIER_DELIVER_ORDER)->first()->files()->first(); + } + + $zip->addFile(Storage::disk('documents')->path($invoice_file->file->file_info->original->file), 'invoice-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); + + $zip->addFile(Storage::disk('documents')->path($purchase_file->file->file_info->original->file), 'purchase-order-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); + + $zip->addFile(Storage::disk('documents')->path($deliver_file->file->file_info->original->file), 'deliver-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); + + $zip->addFile(Storage::disk('documents')->path($supplier_deliver_order_file->file->file_info->original->file), 'supplier-deliver-order-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); + } + else { + $file = $booking->documents()->where('document_type', $document_type)->first()->files()->first(); + $zip->addFile(Storage::disk('documents')->path($file->file->file_info->original->file), $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); + } + + } $zip->close(); while (ob_get_level()) { ob_end_clean(); diff --git a/app/Classes/Modules/Transactions/Processors/CreateInvoiceDocumentProcessor.php b/app/Classes/Modules/Transactions/Processors/CreateInvoiceDocumentProcessor.php new file mode 100644 index 00000000..95aa933d --- /dev/null +++ b/app/Classes/Modules/Transactions/Processors/CreateInvoiceDocumentProcessor.php @@ -0,0 +1,69 @@ +createsDocument = $createsDocument; + $this->createsFile = $createsFile; + } + + /** + * @param Booking $booking + * @return void + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function execute($transaction, $po_order_transaction, $supplier, $document_type) + { + $lowercaseDocumentType = strtolower($document_type); + + if ($document_type !== DocumentType::SUPPLIER_DELIVER_ORDER) { + + $order_pdf = LaravelMpdf::loadView('pages.pdfs.' . $lowercaseDocumentType, ['invoice_transaction' => $transaction, 'po_order_transaction' => $po_order_transaction, 'supplier' => $supplier]); + $document_object = new DocumentObject( + $document_type, + [chunk_split('data:application/pdf;base64,' . base64_encode($order_pdf->output()))], + '', + ApprovalStatus::COMPLETED, + $lowercaseDocumentType . 's' + ); + + } + else { + + $order_pdf = LaravelMpdf::loadView('pages.pdfs.supplier_deliver_order', ['supplier_deliver_order_transaction' => $transaction, 'po_order_transaction' => $po_order_transaction, 'supplier' => $supplier]); + $document_object = new DocumentObject( + $document_type, + [chunk_split('data:application/pdf;base64,'.base64_encode($order_pdf->output()))], + '', + ApprovalStatus::COMPLETED, + $lowercaseDocumentType . 's' + ); + + } + + $document = $this->createsDocument->execute($po_order_transaction->booking, $document_object); + $this->createsFile->execute($document, $document_object); + } +} diff --git a/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php b/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php index 5a29a1a2..3f98af4e 100644 --- a/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php +++ b/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php @@ -55,15 +55,12 @@ class CreateInvoiceTransactionProcessor /** @var FetchesCompany */ private $fetchesCompany; - /** @var CreatesDocument */ - private $createsDocument; - - /** @var CreatesFiles */ - private $createsFile; - /** @var UpdatesBookingStatus */ private $updatesBookingStatus; + /** @var CreateInvoiceDocumentProcessor */ + private $invoiceDocumentProcessor; + /** * CreateInvoiceTransactionProcessor constructor. * @param ListsTransactions $listsTransactions @@ -75,11 +72,10 @@ class CreateInvoiceTransactionProcessor * @param FetchesServiceConfigurations $fetchesServiceConfigurations * @param CalculatesBookingCurrencyAverageRate $calculatesBookingCurrencyAverageRate * @param FetchesCompany $fetchesCompany - * @param CreatesDocument $createsDocument - * @param CreatesFiles $createsFile * @param UpdatesBookingStatus $updatesBookingStatus + * @param CreateInvoiceDocumentProcessor $invoiceDocumentProcessor */ - public function __construct(ListsTransactions $listsTransactions, CreatesTransaction $createsTransaction, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CalculatesBookingPaidAmount $calculatesBookingPaidAmount, CalculatesBookingPayableAmount $calculatesBookingPayableAmount, CalculatesBookingTransferredAmount $calculatesBookingTransferredAmount, FetchesServiceConfigurations $fetchesServiceConfigurations, CalculatesBookingCurrencyAverageRate $calculatesBookingCurrencyAverageRate, FetchesCompany $fetchesCompany, CreatesDocument $createsDocument, CreatesFiles $createsFile, UpdatesBookingStatus $updatesBookingStatus) + public function __construct(ListsTransactions $listsTransactions, CreatesTransaction $createsTransaction, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CalculatesBookingPaidAmount $calculatesBookingPaidAmount, CalculatesBookingPayableAmount $calculatesBookingPayableAmount, CalculatesBookingTransferredAmount $calculatesBookingTransferredAmount, FetchesServiceConfigurations $fetchesServiceConfigurations, CalculatesBookingCurrencyAverageRate $calculatesBookingCurrencyAverageRate, FetchesCompany $fetchesCompany, UpdatesBookingStatus $updatesBookingStatus, CreateInvoiceTransactionProcessor $invoiceDocumentProcessor) { $this->listsTransactions = $listsTransactions; $this->createsTransaction = $createsTransaction; @@ -90,9 +86,8 @@ class CreateInvoiceTransactionProcessor $this->fetchesServiceConfigurations = $fetchesServiceConfigurations; $this->calculatesBookingCurrencyAverageRate = $calculatesBookingCurrencyAverageRate; $this->fetchesCompany = $fetchesCompany; - $this->createsDocument = $createsDocument; - $this->createsFile = $createsFile; $this->updatesBookingStatus = $updatesBookingStatus; + $this->invoiceDocumentProcessor = $invoiceDocumentProcessor; } @@ -101,7 +96,7 @@ class CreateInvoiceTransactionProcessor * @return void * @throws \App\Classes\Exceptions\MalformedRequestException */ - public function execute(Booking $booking) + public function execute(Booking $booking) { if ($booking->status === ApprovalStatus::COMPLETED) { @@ -116,7 +111,7 @@ class CreateInvoiceTransactionProcessor return; } // confirm that all payments has been transferred - if($this->calculatesBookingTransferredAmount->execute($booking) !== $this->calculatesBookingPaidAmount->execute($booking)){ + if ($this->calculatesBookingTransferredAmount->execute($booking) !== $this->calculatesBookingPaidAmount->execute($booking)) { return; } @@ -127,7 +122,7 @@ class CreateInvoiceTransactionProcessor $constants = SegmentConstant::where('reference', SegmentConstants::SERVICE_TYPE)->where('detail->id', $booking->service->id)->first(); - if($constants->detail->is_billable && !$po_order_transaction) { + if ($constants->detail->is_billable && !$po_order_transaction) { return; } @@ -170,42 +165,14 @@ class CreateInvoiceTransactionProcessor $supplier = $this->fetchesCompany->execute(['id' => $transaction->receiver]); - $purchase_order_pdf = LaravelMpdf::loadView('pages.pdfs.purchase_order', ['invoice_transaction' => $invoice_transaction, 'po_order_transaction' => $po_order_transaction, 'supplier' => $supplier]); - $document_object = new DocumentObject( - DocumentType::PURCHASE_ORDER, - [chunk_split('data:application/pdf;base64,'.base64_encode($purchase_order_pdf->output()))], - '', - ApprovalStatus::COMPLETED, - 'purchase_orders' - ); - /** @var Document $document */ - $document = $this->createsDocument->execute($po_order_transaction->booking, $document_object); - $this->createsFile->execute($document, $document_object); + // purchase order + $this->invoiceDocumentProcessor->execute($invoice_transaction, $po_order_transaction, $supplier, DocumentType::PURCHASE_ORDER); - $deliver_order_pdf = LaravelMpdf::loadView('pages.pdfs.deliver_order', ['invoice_transaction' => $invoice_transaction, 'po_order_transaction' => $po_order_transaction, 'supplier' => $supplier]); - $document_object = new DocumentObject( - DocumentType::DELIVER_ORDER, - [chunk_split('data:application/pdf;base64,'.base64_encode($deliver_order_pdf->output()))], - '', - ApprovalStatus::COMPLETED, - 'delivery_orders' - ); + // deliver order + $this->invoiceDocumentProcessor->execute($invoice_transaction, $po_order_transaction, $supplier, DocumentType::DELIVER_ORDER); - /** @var Document $document */ - $document = $this->createsDocument->execute($po_order_transaction->booking, $document_object); - $this->createsFile->execute($document, $document_object); - - - $invoice_pdf = LaravelMpdf::loadView('pages.pdfs.invoice', ['invoice_transaction' => $invoice_transaction, 'po_order_transaction' => $po_order_transaction, 'supplier' => $supplier]); - $document_object = new DocumentObject( - DocumentType::INVOICE, - [chunk_split('data:application/pdf;base64,'.base64_encode($invoice_pdf->output()))], - '', - ApprovalStatus::COMPLETED, - 'invoices' - ); - $document = $this->createsDocument->execute($po_order_transaction->booking, $document_object); - $this->createsFile->execute($document, $document_object); + // invoice + $this->invoiceDocumentProcessor->execute($invoice_transaction, $po_order_transaction, $supplier, DocumentType::INVOICE); $billNumber = $this->generatesTransactionBillNumber->execute('SPDO-'); @@ -233,16 +200,8 @@ class CreateInvoiceTransactionProcessor ); $supplier_deliver_order_transaction = $this->createsTransaction->execute($po_order_transaction->booking, $transaction_object); - $supplier_order_pdf = LaravelMpdf::loadView('pages.pdfs.supplier_deliver_order', ['supplier_deliver_order_transaction' => $supplier_deliver_order_transaction, 'po_order_transaction' => $po_order_transaction, 'supplier' => $supplier]); - $document_object = new DocumentObject( - DocumentType::SUPPLIER_DELIVER_ORDER, - [chunk_split('data:application/pdf;base64,'.base64_encode($supplier_order_pdf->output()))], - '', - ApprovalStatus::COMPLETED, - 'supplier_delivery_orders' - ); - $document = $this->createsDocument->execute($po_order_transaction->booking, $document_object); - $this->createsFile->execute($document, $document_object); + // supply deliver order + $this->invoiceDocumentProcessor->execute($supplier_deliver_order_transaction, $po_order_transaction, $supplier, DocumentType::SUPPLIER_DELIVER_ORDER); $this->updatesBookingStatus->execute($booking, ApprovalStatus::COMPLETED); } diff --git a/resources/assets/vue/components/bookings/elements/BillingComponent.vue b/resources/assets/vue/components/bookings/elements/BillingComponent.vue index 70f511ed..f24078ed 100644 --- a/resources/assets/vue/components/bookings/elements/BillingComponent.vue +++ b/resources/assets/vue/components/bookings/elements/BillingComponent.vue @@ -84,7 +84,14 @@ export default { type: 'INVOICE', supplier: null }, - documents: ['INVOICE', 'PURCHASE_ORDER', 'DELIVER_ORDER', 'SUPPLIER_DELIVER_ORDER'], + documents: [ + 'INVOICE', + 'PURCHASE_ORDER', + 'DELIVER_ORDER', + 'SUPPLIER_DELIVER_ORDER', + 'INVOICE + PO + DO', + 'INVOICE + PO + DO + SDO' + ], selectedDocumentStatus: false } },