createsTransaction = $createsTransaction; $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; $this->calculatesBookingPayableAmount = $calculatesBookingPayableAmount; $this->calculatesBookingCurrencyAverageRate = $calculatesBookingCurrencyAverageRate; $this->fetchesCompany = $fetchesCompany; $this->createsDocument = $createsDocument; $this->createsFile = $createsFile; $this->calculatesBookingOutstanding = $calculatesBookingOutstanding; $this->generatesBookingQuotation = $generatesBookingQuotation; $this->fetchesBookingQuotation = $fetchesBookingQuotation; $this->fetchesCompanyPaymentAttemptLimit = $fetchesCompanyPaymentAttemptLimit; } /** * @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::PENDING_VERIFICATION, ApprovalStatus::APPROVED]) ->first(); $outstanding = $this->calculatesBookingOutstanding->execute($booking); $conversionObject = new CurrencyConversionObject(floatval(str_replace(',', '', $outstanding)), $booking->convertible_currency_id, $booking->service_id, $booking->fix_currency_id === 1 ? 0:1, PaymentMethodType::CASH); $configurations = $this->fetchesBookingQuotation->execute($booking->company, $conversionObject); $paymentAttemptLimit = $this->fetchesCompanyPaymentAttemptLimit->execute($booking->company); $billNumber = $this->generatesTransactionBillNumber->execute('PYMT-'); $object = new TransactionObject($billNumber, TransactionType::PAYMENT, 1, $booking->company->id, $configurations->getConfigurations()->getBankId(), $configurations->getConversionObject()->getPaymentMethod(), $configurations->getTotal(), $configurations->getForeignTotal(), 1, $configurations->getConversionObject()->getCurrencyId(), $configurations->getConfigurations()->getRate(), $configurations->getTax(), $configurations->getServiceCharge(), Carbon::now()->addMinutes($paymentAttemptLimit), ApprovalStatus::PENDING_SUBMISSION, [], isset($billPlzBill) ? $billPlzBill->id : NULL); $this->createsTransaction->execute($booking, $object); $billNumber = $this->generatesTransactionBillNumber->execute('PROFORMA-'); $payable_amount = $booking->transactions()->whereNotIn('status', [ApprovalStatus::REJECTED, ApprovalStatus::SUSPENDED])->payments()->sum('amount'); $booking_amount = $booking->fix_amount; $transaction = $booking->transactions() ->where('type', TransactionType::PAYMENT) ->first(); $booking_currency_average_rate = $booking_amount / $booking->transactions()->whereNotIn('status', [ApprovalStatus::REJECTED, ApprovalStatus::SUSPENDED])->payments()->selectRaw('sum(amount - service_charge - tax) as sub_total')->get()->sum('sub_total'); $total_service_charge = $booking->transactions() ->where('type', TransactionType::PAYMENT) ->whereNotIn('status', [ApprovalStatus::REJECTED, ApprovalStatus::SUSPENDED]) ->sum('service_charge'); $total_tax = $booking->transactions() ->where('type', TransactionType::PAYMENT) ->whereIn('status', [ApprovalStatus::REJECTED, ApprovalStatus::SUSPENDED]) ->sum('tax'); $transaction_object = new TransactionObject( $billNumber, TransactionType::PROFORMA, $transaction->issuer, $transaction->receiver, $transaction->recipient_bank_account_id, $transaction->payment_method, $payable_amount, $booking_amount, $transaction->currency_id, $transaction->original_currency_id, $booking_currency_average_rate, $total_tax, $total_service_charge, null, ApprovalStatus::APPROVED ); $perofrma_transaction = $this->createsTransaction->execute($po_order_transaction->booking, $transaction_object); $supplier = $this->fetchesCompany->execute(['id' => $transaction->receiver]); $purchase_order_pdf = LaravelMpdf::loadView('pages.pdfs.proforma_invoice', ['invoice_transaction' => $perofrma_transaction, 'po_order_transaction' => $po_order_transaction, 'supplier' => $supplier]); $document_object = new DocumentObject( DocumentType::PROFORMA_INVOICE, [chunk_split('data:application/pdf;base64,'.base64_encode($purchase_order_pdf->output()))], '', ApprovalStatus::COMPLETED, 'proforma_invoices' ); /** @var Document $document */ $document = $this->createsDocument->execute($po_order_transaction->booking, $document_object); $this->createsFile->execute($document, $document_object); } }