listsTransactions = $listsTransactions; $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; $this->createsTransaction = $createsTransaction; $this->calculatesBookingPaidAmount = $calculatesBookingPaidAmount; $this->calculatesBookingCurrencyAverageRate = $calculatesBookingCurrencyAverageRate; $this->fetchesCompany = $fetchesCompany; $this->createsDocument = $createsDocument; $this->createsFile = $createsFile; $this->updatesBookingStatus = $updatesBookingStatus; } /** * @param Booking $booking * @return void * @throws \App\Classes\Exceptions\MalformedRequestException */ public function execute(Booking $booking) { $po_order_transaction = $booking->transactions() ->where('type', TransactionType::PURCHASE_ORDER) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) ->first(); if ($po_order_transaction) { $paymant_amount = $this->calculatesBookingPaidAmount->execute($booking, $booking->fix_currency_id); $booking_amount = $booking->fix_amount; if ((float) $booking_amount === (float) $paymant_amount) { $transaction = $booking->transactions() ->where('type', TransactionType::PAYMENT) ->first(); $billNumber = $this->generatesTransactionBillNumber->execute('INV-'); $booking_currency_average_rate = $this->calculatesBookingCurrencyAverageRate->execute($booking); $transaction_object = new TransactionObject( $billNumber, TransactionType::INVOICE, $transaction->issuer, $transaction->receiver, $transaction->recipient_bank_account_id, $transaction->payment_method, $paymant_amount, $booking_amount, $transaction->currency_id, $transaction->original_currency_id, $booking_currency_average_rate, $transaction->tax, $transaction->service_charge, null, ApprovalStatus::APPROVED ); $supplier = $this->fetchesCompany->execute(['id' => $transaction->receiver]); $purchase_order_pdf = LaravelMpdf::loadView('pages.pdfs.purchase_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' ); $document = $this->createsDocument->execute($po_order_transaction->booking, $document_object); $this->createsFile->execute($document, $document_object); $deliver_order_pdf = LaravelMpdf::loadView('pages.pdfs.deliver_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, 'deliver_orders' ); $document = $this->createsDocument->execute($po_order_transaction->booking, $document_object); $this->createsFile->execute($document, $document_object); $payment_order_pdf = LaravelMpdf::loadView('pages.pdfs.payment_order', ['transaction' => $po_order_transaction, 'supplier' => $supplier]); $document_object = new DocumentObject( DocumentType::PAYMENT_ORDER, [chunk_split('data:application/pdf;base64,'.base64_encode($payment_order_pdf->output()))], '', ApprovalStatus::COMPLETED, 'payment_orders' ); $document = $this->createsDocument->execute($po_order_transaction->booking, $document_object); $this->createsFile->execute($document, $document_object); $supplier_order_pdf = LaravelMpdf::loadView('pages.pdfs.supplier_deliver_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_deliver_orders' ); $document = $this->createsDocument->execute($po_order_transaction->booking, $document_object); $this->createsFile->execute($document, $document_object); $transaction = $this->createsTransaction->execute($po_order_transaction->booking, $transaction_object); $this->updatesBookingStatus->execute($booking, ApprovalStatus::COMPLETED); } } } }