diff --git a/app/Classes/Modules/Bookings/ControllersLogic/ApprovePaymentVerificationLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/ApprovePaymentVerificationLogic.php index ae05c651..ebe4ce62 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/ApprovePaymentVerificationLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/ApprovePaymentVerificationLogic.php @@ -5,7 +5,7 @@ namespace App\Classes\Modules\Bookings\ControllersLogic; use App\Classes\General\Abstracts\AbstractControllerLogic; -use App\Classes\Modules\Bookings\ControllersLogic\CreateInvoiceTransactionLogic; +use App\Classes\Modules\Transactions\Processors\CreateInvoiceTransactionProcessor; use App\Classes\Modules\Documents\Services\ApprovesDocument; use App\Classes\Modules\Documents\Services\FetchesDocument; @@ -27,15 +27,15 @@ class ApprovePaymentVerificationLogic extends AbstractControllerLogic * @param ApprovesDocument $approvesDocument * @param RejectsDocument $rejectsDocument * @param FetchesDocument $fetchesDocument + * @param CreateInvoiceTransactionProcessor $createInvoiceTransactionProcessor */ - public function __construct(FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, ApprovesDocument $approvesDocument, RejectsDocument $rejectsDocument, FetchesDocument $fetchesDocument, CreateInvoiceTransactionLogic $createInvoiceTransactionLogic) + public function __construct(FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, ApprovesDocument $approvesDocument, RejectsDocument $rejectsDocument, FetchesDocument $fetchesDocument, CreateInvoiceTransactionProcessor $createInvoiceTransactionProcessor) { $this->fetchesTransaction = $fetchesTransaction; $this->updatesTransactionStatus = $updatesTransactionStatus; $this->approvesDocument = $approvesDocument; $this->rejectsDocument = $rejectsDocument; - $this->createInvoiceTransactionLogic = $createInvoiceTransactionLogic; - + $this->createInvoiceTransactionProcessor = $createInvoiceTransactionProcessor; } /** @@ -60,6 +60,9 @@ class ApprovePaymentVerificationLogic extends AbstractControllerLogic /** @var RejectsDocument */ private $rejectsDocument; + /** @var CreateInvoiceTransactionProcessor */ + private $createInvoiceTransactionProcessor; + /** * @param Request $request * @return JsonResponse @@ -76,12 +79,7 @@ class ApprovePaymentVerificationLogic extends AbstractControllerLogic $this->updatesTransactionStatus->execute($transaction, $status === 'approve' ? ApprovalStatus::APPROVED : ApprovalStatus::REJECTED); - if ($status == 'approve') { - $request->request->add([ - 'booking_id' => $transaction->booking_id, - ]); - $this->createInvoiceTransactionLogic->execute($request); - } + $this->createInvoiceTransactionProcessor->execute($transaction->booking); return $this->response([]); } diff --git a/app/Classes/Modules/Bookings/Services/CalculatesBookingCurrencyAverageRate.php b/app/Classes/Modules/Bookings/Services/CalculatesBookingCurrencyAverageRate.php new file mode 100644 index 00000000..996c8248 --- /dev/null +++ b/app/Classes/Modules/Bookings/Services/CalculatesBookingCurrencyAverageRate.php @@ -0,0 +1,19 @@ +transactions() + ->where('type', TransactionType::PAYMENT) + ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) + ->avg('currency_rate'); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreateInvoiceTransactionLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreateInvoiceTransactionLogic.php deleted file mode 100644 index 51ed24cd..00000000 --- a/app/Classes/Modules/Transactions/ControllersLogic/CreateInvoiceTransactionLogic.php +++ /dev/null @@ -1,107 +0,0 @@ -listsTransactions = $listsTransactions; - $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; - $this->createsTransaction = $createsTransaction; - } - - /** - * @return array - */ - protected function notification():array { - return [ - 'title' => 'Created InvoiceTransaction', - 'message' => 'You have successfully created a invoice transaction' - ]; - } - - /** @var ListsTransactions */ - private $listsTransactions; - - /** @var CreatesTransaction */ - private $createsTransaction; - - /** @var GeneratesTransactionBillNumber */ - private $generatesTransactionBillNumber; - - /** - * @param Request $request - * @return JsonResponse - * @throws \App\Classes\Exceptions\MalformedRequestException - */ - public function logic(Request $request) : JsonResponse - { - $booking_id = $request->input('booking_id'); - - $transaction = $this->listsTransactions->execute([ - 'booking_id' => $booking_id - ]); - - $allow = true; - $total_currency_rate = 0; - - foreach ($transaction as $key => $row) { - $total_currency_rate += $row->currency_rate; - - if ($row->status != ApprovalStatus::APPROVED ) { - $allow = false; - } - } - - $total_average_currency_rate = $total_currency_rate / count($transaction); - - if ($allow) { - $transaction = $transaction[0]; - - $billNumber = $this->generatesTransactionBillNumber->execute('INV-'); - $object = new TransactionObject( - $billNumber, - TransactionType::INVOICE, - $transaction->issuer, - $transaction->receiver, - $transaction->recipient_bank_account_id, - $transaction->payment_method, - $transaction->amount, - $transaction->original_amount, - $transaction->currency_id, - $transaction->original_currency_id, - $total_average_currency_rate, - $transaction->tax, - $transaction->service_charge, - null, - ApprovalStatus::APPROVED - ); - - $transaction = $this->createsTransaction->execute($transaction->booking, $object); - } - - return $this->response([]); - } - -} \ No newline at end of file diff --git a/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php b/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php new file mode 100644 index 00000000..70c2476a --- /dev/null +++ b/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php @@ -0,0 +1,135 @@ +listsTransactions = $listsTransactions; + $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsTransaction = $createsTransaction; + $this->calculatesBookingPaidAmount = $calculatesBookingPaidAmount; + $this->calculatesBookingCurrencyAverageRate = $calculatesBookingCurrencyAverageRate; + $this->fetchesCompany = $fetchesCompany; + $this->createsDocument = $createsDocument; + $this->createsFile = $createsFile; + $this->pdf = $pdf; + } + + /** + * @param Booking $booking + * @return Model + * @throws \App\Classes\Exceptions\AccessForbiddenException + * @throws \App\Classes\Exceptions\MalformedRequestException + * @throws \App\Classes\Exceptions\RequestValidationException + */ + 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]); + + $pdf = $this->pdf->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($pdf->output()))], + '', + ApprovalStatus::COMPLETED, + 'purchase_order' + ); + $document = $this->createsDocument->execute($po_order_transaction, $document_object); + $this->createsFile->execute($document, $document_object); + + $transaction = $this->createsTransaction->execute($po_order_transaction->booking, $transaction_object); + } + } + } +} \ No newline at end of file diff --git a/app/Classes/ValueObjects/Constants/DocumentType.php b/app/Classes/ValueObjects/Constants/DocumentType.php index 6a9a348c..0b12f257 100644 --- a/app/Classes/ValueObjects/Constants/DocumentType.php +++ b/app/Classes/ValueObjects/Constants/DocumentType.php @@ -16,4 +16,6 @@ final class DocumentType { public const WALLET_TOP_UP_PAYMENT_PROOF = 'WALLET_TOP_UP_PAYMENT_PROOF'; public const WALLET_REFUND_PAYMENT_PROOF = 'WALLET_REFUND_PAYMENT_PROOF'; + public const PURCHASE_ORDER = 'PURCHASE_ORDER'; + } diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 51597ba2..d77eda88 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -52,4 +52,12 @@ class Transaction extends AbstractModel implements Documentable return $this->HasMany(TransactionDetail::class, 'transaction_id', 'id'); } + public function convert_original_amount() + { + if($this->booking()->first()->fix_currency_id !== 1) { + $currency_rate = $this->currency()->first()->rates()->where('payment_method_type', $this->payment_method)->first(); + return number_format($this->original_amount / $currency_rate->selling, 2); + } + return $this->original_amount; + } } diff --git a/app/Models/TransactionDetail.php b/app/Models/TransactionDetail.php index 49494327..8e0a1613 100644 --- a/app/Models/TransactionDetail.php +++ b/app/Models/TransactionDetail.php @@ -2,16 +2,36 @@ namespace App\Models; -use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Database\Eloquent\Relations\BelongsTo; class TransactionDetail extends AbstractModel { protected $table = 'transaction_detail'; - public function transaction(): HasOne + /** + * @return BelongsTo + */ + public function transaction(): BelongsTo { - return $this->hasOne(Transaction::class, 'transaction_id', 'id'); + return $this->BelongsTo( Transaction::class, 'transaction_id', 'id'); } + public function convert_original_price() + { + if($this->transaction()->first()->booking()->first()->fix_currency_id !== 1) { + $currency_rate = $this->transaction()->first()->currency()->first()->rates()->where('payment_method_type', $this->transaction()->first()->payment_method)->first(); + return number_format($this->price / $currency_rate->selling, 2); + } + return $this->price; + } + + public function convert_original_amount() + { + if($this->transaction()->first()->booking()->first()->fix_currency_id !== 1) { + $currency_rate = $this->transaction()->first()->currency()->first()->rates()->where('payment_method_type', $this->transaction()->first()->payment_method)->first(); + return number_format($this->amount / $currency_rate->selling, 2); + } + return $this->amount; + } } diff --git a/resources/views/pages/pdfs/purchase_order.blade.php b/resources/views/pages/pdfs/purchase_order.blade.php new file mode 100644 index 00000000..ad8d8e63 --- /dev/null +++ b/resources/views/pages/pdfs/purchase_order.blade.php @@ -0,0 +1,56 @@ +@extends('layouts.base_pdf') +@section('inner_content') + +
| {{$supplier->name}} | +{{\Carbon\Carbon::now('Asia/Singapore')->format('d-m-Y h:s')}} | +
| Reference | +Amount | +Bank in Details | +
| {{$transaction->booking->marking}} | +MYR {{ $transaction->convert_original_amount() }} | +Account Holder Name: {{$transaction->booking->bank->holder_name}} {{$transaction->booking->bank->bank_name}}: {{$transaction->booking->bank->account_no}} + Branch: 首都 Bank in Amount: {{$transaction->original_amount}} |
+
| Product Code | +Product Name | +Quantity | +Price | +Amount | +
| {{ $transaction_detail->product_code }} | +{{ $transaction_detail->product_name }} | +{{ $transaction_detail->quantity }} | +MYR {{ $transaction_detail->convert_original_price() }} | +MYR {{ $transaction_detail->convert_original_amount() }} | +