From e0d0dccb4c475aabc56cd18551141f90465428a5 Mon Sep 17 00:00:00 2001 From: edmondlang Date: Tue, 21 Mar 2023 14:11:42 +0800 Subject: [PATCH] multi payment final --- .../ControllersLogic/CallbackBillplzLogic.php | 39 +-- .../ControllersLogic/CreateGroupsLogic.php | 254 ++++++++++++++++++ .../ControllersLogic/DeleteGroupLogic.php | 73 +++++ .../ControllersLogic/ListGroupsLogic.php | 42 +++ .../ControllersLogic/UpdateGroupLogic.php | 185 +++++++++++++ .../Transactions/Services/ListsGroups.php | 31 +++ .../Constants/PaymentMethodType.php | 2 +- .../Transactions/CreateGroupsController.php | 21 ++ .../Transactions/DeleteGroupController.php | 20 ++ .../Transactions/ListGroupsController.php | 21 ++ .../Transactions/UpdateGroupController.php | 20 ++ app/Http/Resources/GroupResource.php | 53 ++++ .../Resources/WalletTransactionResource.php | 16 +- app/Models/Group.php | 86 ++++++ app/Models/GroupTransaction.php | 27 ++ .../2023_03_19_170628_create_groups_table.php | 43 +++ ...170641_create_group_transactions_table.php | 32 +++ .../CustomerPaymentBillingInnerComponent.vue | 19 +- .../forms/GroupPaymentFormComponent.vue | 224 +++++++++++++++ .../elements/CurrenciesListComponent.vue | 6 +- .../CustomerTransactionSectionComponent.vue | 6 +- routes/transaction.php | 9 + 22 files changed, 1191 insertions(+), 38 deletions(-) create mode 100644 app/Classes/Modules/Transactions/ControllersLogic/CreateGroupsLogic.php create mode 100644 app/Classes/Modules/Transactions/ControllersLogic/DeleteGroupLogic.php create mode 100644 app/Classes/Modules/Transactions/ControllersLogic/ListGroupsLogic.php create mode 100644 app/Classes/Modules/Transactions/ControllersLogic/UpdateGroupLogic.php create mode 100644 app/Classes/Modules/Transactions/Services/ListsGroups.php create mode 100644 app/Http/Controllers/Transactions/CreateGroupsController.php create mode 100644 app/Http/Controllers/Transactions/DeleteGroupController.php create mode 100644 app/Http/Controllers/Transactions/ListGroupsController.php create mode 100644 app/Http/Controllers/Transactions/UpdateGroupController.php create mode 100644 app/Http/Resources/GroupResource.php create mode 100644 app/Models/Group.php create mode 100644 app/Models/GroupTransaction.php create mode 100644 database/migrations/2023_03_19_170628_create_groups_table.php create mode 100644 database/migrations/2023_03_19_170641_create_group_transactions_table.php create mode 100644 resources/assets/vue/components/paymentsBilling/forms/GroupPaymentFormComponent.vue diff --git a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php index 18986b1d..2b925682 100644 --- a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php +++ b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php @@ -105,27 +105,36 @@ class CallbackBillplzLogic $token = Auth::fromUser(User::find(1)); $request->headers->set('Authorization', 'Bearer '.$token); - // check if is wallet top up - if($transaction->owner instanceof Wallet && $status === ApprovalStatus::APPROVED) { - $this->updatesWalletBalance->execute($transaction->owner, $transaction->amount); - } - - $this->updatesTransactionStatus->execute($transaction, $status); + if ($status === ApprovalStatus::APPROVED) { + // check if is wallet top up + if($transaction->owner instanceof Wallet && !in_array($transaction->status, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])) { + $this->updatesWalletBalance->execute($transaction->owner, $transaction->amount); + } - if(($invoice->amount - $transaction->amount) < 0.01 && !$transaction->owner instanceof Wallet) { - $this->updatesTransactionStatus->execute($invoice, ApprovalStatus::COMPLETED); - - $packingList->status = ApprovalStatus::APPROVED; - $packingList->save(); - - if(app()->environment('production')){ - $this->updateDoFromVTPortalProcessor->execute($packingList); - $this->updateDoFromYDPortalProcessor->execute($packingList); + if(($invoice->amount - $transaction->amount) < 0.01 && !$transaction->owner instanceof Wallet) { + $this->updatesTransactionStatus->execute($invoice, ApprovalStatus::COMPLETED); + + $packingList->status = ApprovalStatus::APPROVED; + $packingList->save(); + + if(app()->environment('production')){ + $this->updateDoFromVTPortalProcessor->execute($packingList); + $this->updateDoFromYDPortalProcessor->execute($packingList); + } } } + $this->updatesTransactionStatus->execute($transaction, $status); + $company_module_marking = $transaction->owner->owner->connections? $transaction->owner->owner->connections->first()->invitee_reference: null; + if ($transaction->owner instanceof Wallet) { + $company_module_marking = $transaction->owner->owner->connections? $transaction->owner->owner->connections->first()->invitee_reference: null; + } else { + + $company_module_marking = $order->companyModule->connections? $order->companyModule->connections->first()->invitee_reference: null; + } + return $request->method() === 'POST' ? true : view('pages.payments_redirect', ['marking' => $order->reference ?? null, 'company_module_marking' => $company_module_marking ?? null, 'transaction' => $transaction, 'status' => $status]); } } diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreateGroupsLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreateGroupsLogic.php new file mode 100644 index 00000000..e5d46f1b --- /dev/null +++ b/app/Classes/Modules/Transactions/ControllersLogic/CreateGroupsLogic.php @@ -0,0 +1,254 @@ + 'Create Group Transaction', + 'message' => 'You have successfully created a group transaction' + ]; + } + + /** @var FetchesTransaction */ + private $fetchesTransaction; + + + /** @var GeneratesTransactionBillNumber */ + private $generatesTransactionBillNumber; + + /** @var CreatesTransactionableTransaction */ + private $createsTransactionableTransaction; + + /** @var CreatesBillplzBill */ + private $createsBillplzBill; + + /** @var UpdatesWalletBalance */ + private $updatesWalletBalance; + + /** @var CreatesTransaction */ + private $createsTransaction; + + /** @var CreatesPaymentTransaction */ + private $createsPaymentTransaction; + + /** @var UpdateDoFromVTPortalProcessor */ + private $updateDoFromVTPortalProcessor; + + /** @var UpdateDoFromYDPortalProcessor */ + private $updateDoFromYDPortalProcessor ; + + /** @var UpdatesTransactionStatus */ + private $updatesTransactionStatus ; + + public function __construct( + FetchesTransaction $fetchesTransaction, + GeneratesTransactionBillNumber $generatesTransactionBillNumber, + CreatesTransactionableTransaction $createsTransactionableTransaction, + CreatesBillplzBill $createsBillplzBill, + UpdatesWalletBalance $updatesWalletBalance, + CreatesTransaction $createsTransaction, + CreatesPaymentTransaction $createsPaymentTransaction, + UpdateDoFromVTPortalProcessor $updateDoFromVTPortalProcessor, + UpdateDoFromYDPortalProcessor $updateDoFromYDPortalProcessor, + UpdatesTransactionStatus $updatesTransactionStatus + ) + { + $this->fetchesTransaction = $fetchesTransaction; + $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsTransactionableTransaction = $createsTransactionableTransaction; + $this->createsBillplzBill = $createsBillplzBill; + $this->updatesWalletBalance = $updatesWalletBalance; + $this->createsPaymentTransaction = $createsPaymentTransaction; + $this->updateDoFromVTPortalProcessor = $updateDoFromVTPortalProcessor; + $this->updateDoFromYDPortalProcessor = $updateDoFromYDPortalProcessor; + $this->updatesTransactionStatus = $updatesTransactionStatus; + } + + public function logic(Request $request) : JsonResponse + { + $invoice_ids = json_decode($request->route('transaction_ids')); + $billNumber = $this->generatesTransactionBillNumber->execute('PYMT-'); + $approvalStatus = ApprovalStatus::PENDING_SUBMISSION; + $payment_method = PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')]; + + if (count($invoice_ids) > 1) { + dd('more than 1 invoice'); + // creates group transaction + $group = new Group(); + + $issuer = ''; + $receiver = ''; + $amount = 0; + $original_amount = 0; + $currency_id = 0; + $original_currency_id = ''; + $currency_rate = ''; + $tax = 0; + $service_charge = 0; + + // todo-new: check why is this not working + // $invoices = $this->fetchesTransaction->execute(['id_in' => $invoice_ids]); + $invoices = Transaction::whereIn('id', $invoice_ids)->get(); + + foreach ($invoices as $invoice) { + // todo-new: attach transaction to the group id + // create one payment for each, then mark as pending_approval + $issuer = $invoice->issuer; + $receiver = $invoice->receiver; + $amount += $invoice->amount; + $original_amount += $invoice->original_amount; + $currency_id = $invoice->currency_id; + $original_currency_id = $invoice->original_currency_id; + $currency_rate = $invoice->currency_rate; + $tax += $invoice->tax; + $service_charge += $invoice->service_charge; + } + + $group->issuer = $issuer; + $group->receiver = $receiver; + $group->reference = $billNumber; + $group->amount = $amount; + $group->original_amount = $original_amount; + $group->currency_id = $currency_id; + $group->original_currency_id = $original_currency_id; + $group->currency_rate = $currency_rate; + $group->tax = $tax; + $group->service_charge = $service_charge; + + $group->save(); + + + // attach payment on group transaction + + // $invoice_transaction = $this->fetchesTransaction->execute(['id' => $invoice_ids->first()]); + // $amount = $request->input('amount'); + // $company_module = $invoice_transaction->owner()->first()->owner()->first()->companyModule()->first(); + + $invoice_transaction = $group; + $transferDetails = $billNumber; + $company_module = $group->receiverCompany; + } else { + + $invoice_transaction = $this->fetchesTransaction->execute(['id' => $invoice_ids[0]]); + $amount = $request->input('amount'); + $company_module = $invoice_transaction->owner()->first()->owner()->first()->companyModule()->first(); + $transferDetails = $invoice_transaction->bill_no; + } + + $payment_reference = null; + if ($payment_method == PaymentMethodType::PAYMENT_GATEWAY) { + + // create billplz transaction + $payment_method = PaymentMethodType::PAYMENT_GATEWAY; + $billPlzBill = $this->createsBillplzBill->execute( + $company_module->name, + (app()->environment(['production'])) ? $company_module->employees()->first()->email : 'uldvstar@gmail.com', + 'This payment is for the invoice number . ' . $transferDetails, + $amount, + $billNumber, + $request->input('bank_code'), + true + ); + + $payment_reference = $billPlzBill->id; + } + else if ($payment_method === PaymentMethodType::WALLET) { + /** @var Wallet $wallet */ + $wallet = $company_module->wallets()->first(); + + if((float) number_format(($wallet->amount - $amount),2) < 0){ + throw new MalformedRequestException('Insufficient wallet balance. Please Top up your wallet.'); + } + + $walletPaymentBillNumber = $this->generatesTransactionBillNumber->execute('PYMT-'); + + $transaction_object = new TransactionObject($walletPaymentBillNumber, TransactionType::PAYMENT, 1, $company_module->id, 1, PaymentMethodType::WALLET, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::APPROVED, [], ''); + $transaction = $this->createsTransactionableTransaction->execute($wallet, $transaction_object); + + $payment_reference = $walletPaymentBillNumber; + + $this->updatesWalletBalance->execute($wallet, ($amount * -1)); + + if (count($invoice_ids) > 1) { + // do one by one + // dd($invoices); + // todo-new: update all the transaction inside + } else { + // $invoice_transaction; + $packingList = $invoice_transaction->owner; + $order = $packingList->owner; + $packingList->status = ApprovalStatus::APPROVED; + $packingList->save(); + } + + if(app()->environment('production')){ + $this->updateDoFromVTPortalProcessor->execute($packingList); + $this->updateDoFromYDPortalProcessor->execute($packingList); + } + + $approvalStatus = ApprovalStatus::APPROVED; + + // update invoice to completed + $this->updatesTransactionStatus->execute($invoice_transaction, ApprovalStatus::COMPLETED); + } + else { + $payment_method = PaymentMethodType::CASH; + } + + // createsPaymentTransaction + $object = new TransactionObject( + $billNumber, + TransactionType::PAYMENT, + $company_module->id, + 1, + 1, + $payment_method, + $request->input('amount'), + $request->input('amount'), + 1, + 1, + 0, + 0, + 0, + null, + $approvalStatus, + null, + $payment_reference + ); + + $payment_transaction = $this->createsPaymentTransaction->execute($invoice_transaction, $object); + + return $this->resourceResponse(new TransactionResource($payment_transaction)); + } +} diff --git a/app/Classes/Modules/Transactions/ControllersLogic/DeleteGroupLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/DeleteGroupLogic.php new file mode 100644 index 00000000..601a1530 --- /dev/null +++ b/app/Classes/Modules/Transactions/ControllersLogic/DeleteGroupLogic.php @@ -0,0 +1,73 @@ + 'Delete Group Transaction', + 'message' => 'You have successfully deleted this Group Transaction' + ]; + } + + /** @var UpdatesTransactionStatus */ + private $updatesTransactionStatus; + + /** @var FetchesGroup */ + private $fetchesGroup; + + /** @var DeletesTransaction */ + private $deletesTransaction; + + /** + * DeleteGroupLogic constructor. + * @param updatesTransactionStatus $updatesTransactionStatus + * @param FetchesGroup $fetchesGroup + * @param DeletesTransaction $deletesTransaction + */ + public function __construct(updatesTransactionStatus $updatesTransactionStatus, FetchesGroup $fetchesGroup, DeletesTransaction $deletesTransaction) + { + $this->updatesTransactionStatus = $updatesTransactionStatus; + $this->fetchesGroup = $fetchesGroup; + $this->deletesTransaction = $deletesTransaction; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function logic(Request $request) : JsonResponse + { + $group = $this->fetchesGroup->execute(['id' => $request->route('id')]); + + $items = $group->transactions()->get(); + + foreach($items as $item) { + $bill = $item; + $payment = $bill->owner; + $group->transactions()->detach($bill->id); + $this->updatesTransactionStatus->execute($payment, ApprovalStatus::APPROVED); + $this->deletesTransaction->execute($bill); + } + + $group->delete(); + + return $this->resourceResponse(new GroupResource($group)); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Transactions/ControllersLogic/ListGroupsLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/ListGroupsLogic.php new file mode 100644 index 00000000..5cf99bbe --- /dev/null +++ b/app/Classes/Modules/Transactions/ControllersLogic/ListGroupsLogic.php @@ -0,0 +1,42 @@ +listsGroups = $listsGroups; + } + + /** + * @return array + */ + protected function notification():array { + return [ + 'title' => 'Retrieved Groups', + 'message' => 'You have successfully retrieved a list of groups' + ]; + } + + /** @var ListsGroups */ + private $listsGroups; + + public function logic(Request $request) : JsonResponse + { + $query = $this->listsGroups->execute($this->listsGroups->deserializeFilters($request->input('filters'))); + + return $this->collectionResponse(GroupResource::collection($query)); + } + +} diff --git a/app/Classes/Modules/Transactions/ControllersLogic/UpdateGroupLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/UpdateGroupLogic.php new file mode 100644 index 00000000..aa432aea --- /dev/null +++ b/app/Classes/Modules/Transactions/ControllersLogic/UpdateGroupLogic.php @@ -0,0 +1,185 @@ + 'Update Group Transaction', + 'message' => 'You have successfully updated this Group Transaction' + ]; + } + + /** @var FetchesGroup */ + private $fetchesGroup; + + /** @var FetchesCompany */ + private $fetchesCompany; + + /** @var CalculatesTransactionServiceCharge */ + private $calculatesTransactionServiceCharge; + + /** @var UpdatesTransaction */ + private $updatesTransaction; + + /** @var CalculatesTransactionTransferFee */ + private $calculatesTransactionTransferFee; + + /** @var CreatesDocument */ + private $createsDocument; + + /** @var CreatesFiles */ + private $createsFile; + + /** + * UpdateGroupLogic constructor. + * @param FetchesGroup $fetchesGroup + * @param FetchesCompany $fetchesCompany + * @param CalculatesTransactionServiceCharge $calculatesTransactionServiceCharge + * @param UpdatesTransaction $updatesTransaction + * @param CalculatesTransactionTransferFee $calculatesTransactionTransferFee + * @param CreatesDocument $createsDocument + * @param CreatesFiles $createsFile + */ + public function __construct(FetchesGroup $fetchesGroup, FetchesCompany $fetchesCompany, CalculatesTransactionServiceCharge $calculatesTransactionServiceCharge, UpdatesTransaction $updatesTransaction, CalculatesTransactionTransferFee $calculatesTransactionTransferFee, CreatesDocument $createsDocument, CreatesFiles $createsFile) + { + $this->fetchesGroup = $fetchesGroup; + $this->fetchesCompany = $fetchesCompany; + $this->calculatesTransactionServiceCharge = $calculatesTransactionServiceCharge; + $this->updatesTransaction = $updatesTransaction; + $this->calculatesTransactionTransferFee = $calculatesTransactionTransferFee; + $this->createsDocument = $createsDocument; + $this->createsFile = $createsFile; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function logic(Request $request) : JsonResponse + { + $group = $this->fetchesGroup->execute(['id' => $request->route('id')]); + + $transactions = $group->transactions()->get(); + + $rate = $request->input('rate'); + + $supplier = $this->fetchesCompany->execute(['id' => $request->input('supplier_id')]); + +// $group->transactions()->update(['issuer' => $supplier->id, 'currency_rate' => $rate]); +// $group->transactions()->update(['amount' => DB::raw('(original_amount * (1 / currency_rate)) + service_charge + tax')]); + + + foreach($transactions as $transaction) { + + $constant = SegmentConstant::where('reference', SegmentConstants::SERVICE_CHARGE)->where('detail->id', $supplier->id)->first(); + $serviceCharge = $this->calculatesTransactionServiceCharge->execute($transaction->original_amount, $rate, $constant); + + $object = new TransactionObject( + $transaction->bill_no, + TransactionType::BILL, + $supplier->id, + 1, + $supplier->banks()->where('default', true)->first()->id, + PaymentMethodType::CASH, + $transaction->original_amount * (1 / $rate), + $transaction->original_amount, + 1, + $transaction->original_currency_id, + $rate, + 0, + $serviceCharge, + null, + ApprovalStatus::PENDING_VERIFICATION + ); + + $billTransaction = $this->updatesTransaction->execute($transaction, $object); + + $transferTransaction = $transaction->transactions()->where('type', TransactionType::TRANSFER_FEE)->first(); + + $transferFee = $this->calculatesTransactionTransferFee->execute($billTransaction->original_amount, $constant); + + $object = new TransactionObject( + $transferTransaction->bill_no, + TransactionType::TRANSFER_FEE, + $supplier->id, + 1, + $supplier->banks()->where('default', true)->first()->id, + PaymentMethodType::CASH, + $transaction->original_amount, + $transaction->original_amount, + $transaction->original_currency_id, + $transaction->original_currency_id, + 1, + 0, + $transferFee, + null, + ApprovalStatus::PENDING_VERIFICATION + ); + + $this->updatesTransaction->execute($transferTransaction, $object); + } + + $group->issuer = $supplier->id; + $group->amount = $group->transactions()->sum('amount'); + $group->currency_rate = $rate; + $group->tax = $group->transactions()->sum('tax'); + $group->service_charge = $group->transactions()->sum('service_charge'); + + $group->save(); + + $group->documents()->delete(); + + $transferFeeTransactions = $group->transactions()->with([ + 'transactions' => function ($transaction) { + return $transaction->where('type', TransactionType::TRANSFER_FEE); + }])->get()->pluck('transactions')->flatten(); + + $pdf = LaravelMpdf::loadView('pages.pdfs.currency_vendor_order', ['transactions' => $group->transactions, 'transferFeeTransactions' => $transferFeeTransactions, 'supplier' => $supplier]); + + $object = new DocumentObject( + DocumentType::CURRENCY_VENDOR_ORDER, + [chunk_split('data:application/pdf;base64,'.base64_encode($pdf->output()))], + '', + ApprovalStatus::COMPLETED, + 'currency_vendor_order' + ); + + /** @var Document $document */ + $document = $this->createsDocument->execute($group, $object); + $this->createsFile->execute($document, $object); + + return $this->resourceResponse(new GroupResource($group)); + } + +} diff --git a/app/Classes/Modules/Transactions/Services/ListsGroups.php b/app/Classes/Modules/Transactions/Services/ListsGroups.php new file mode 100644 index 00000000..f9126739 --- /dev/null +++ b/app/Classes/Modules/Transactions/Services/ListsGroups.php @@ -0,0 +1,31 @@ +repository = $repository; + } + + /** + * @return Builder + */ + public function getRepository(): Builder + { + return $this->repository->newQuery(); + } +} diff --git a/app/Classes/ValueObjects/Constants/PaymentMethodType.php b/app/Classes/ValueObjects/Constants/PaymentMethodType.php index 4efec990..cb622ae3 100644 --- a/app/Classes/ValueObjects/Constants/PaymentMethodType.php +++ b/app/Classes/ValueObjects/Constants/PaymentMethodType.php @@ -19,7 +19,7 @@ final class PaymentMethodType { 'cheque' => self::CHEQUE, 'ba' => self::BA, 'wallet' => self::WALLET, - 'payment gateway' => self::PAYMENT_GATEWAY, + 'payment_gateway' => self::PAYMENT_GATEWAY, ]; public const PAYMENT_METHODS_ID = [ diff --git a/app/Http/Controllers/Transactions/CreateGroupsController.php b/app/Http/Controllers/Transactions/CreateGroupsController.php new file mode 100644 index 00000000..c0863e7e --- /dev/null +++ b/app/Http/Controllers/Transactions/CreateGroupsController.php @@ -0,0 +1,21 @@ +execute($request); + } +} diff --git a/app/Http/Controllers/Transactions/DeleteGroupController.php b/app/Http/Controllers/Transactions/DeleteGroupController.php new file mode 100644 index 00000000..14ba54f9 --- /dev/null +++ b/app/Http/Controllers/Transactions/DeleteGroupController.php @@ -0,0 +1,20 @@ +execute($request); + } +} diff --git a/app/Http/Controllers/Transactions/ListGroupsController.php b/app/Http/Controllers/Transactions/ListGroupsController.php new file mode 100644 index 00000000..730633e9 --- /dev/null +++ b/app/Http/Controllers/Transactions/ListGroupsController.php @@ -0,0 +1,21 @@ +execute($request); + } +} diff --git a/app/Http/Controllers/Transactions/UpdateGroupController.php b/app/Http/Controllers/Transactions/UpdateGroupController.php new file mode 100644 index 00000000..db0b3a17 --- /dev/null +++ b/app/Http/Controllers/Transactions/UpdateGroupController.php @@ -0,0 +1,20 @@ +execute($request); + } +} diff --git a/app/Http/Resources/GroupResource.php b/app/Http/Resources/GroupResource.php new file mode 100644 index 00000000..abd017ec --- /dev/null +++ b/app/Http/Resources/GroupResource.php @@ -0,0 +1,53 @@ +issuerCompany){ + dd($this->id); + } + return [ + 'id' => $this->id, + 'original_amount' => (float) $this->original_amount, + 'original_currency' => new CurrencyResource($this->original_currency), + 'issuer_name' => $this->issuerCompany->name, + 'issuer_id' => $this->issuerCompany->id, + 'amount' => (float) $this->amount, + 'service_charge' => (float) $this->amount, + 'currency' => new CurrencyResource($this->currency), + 'created_at' => Carbon::parse($this->created_at)->format('d-m-Y h:i:s A'), + 'currency_rate' => (float) $this->currency_rate, + 'transactions' => $this->transactions()->get()->pluck('owner.owner.marking'), + 'complete_transactions' => $this->transactions()->whereHasMorph('owner', [Transaction::class], function($query){ + return $query->whereHas('booking', function($query){ + return $query->whereHas('transactions', function($query){ + return $query->where('type', TransactionType::PURCHASE_ORDER)->where('status', '=', ApprovalStatus::APPROVED); + }); + }); + })->get()->pluck('owner.owner.marking'), + 'documents' => [ + 'currency_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::CURRENCY_VENDOR_ORDER)->first()), + 'purchase_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::BULK_PURCHASE_ORDER)->first()) + ] + ]; + } +} diff --git a/app/Http/Resources/WalletTransactionResource.php b/app/Http/Resources/WalletTransactionResource.php index dbee29eb..c43d2346 100644 --- a/app/Http/Resources/WalletTransactionResource.php +++ b/app/Http/Resources/WalletTransactionResource.php @@ -21,22 +21,22 @@ class WalletTransactionResource extends JsonResource { $description = ''; switch((int) $this->type){ - case 5: + case TransactionType::TOP_UP: $description = (double) $this->amount.' Credit Top up'; break; - case 9: + case TransactionType::CREDIT_NOTE: $description = 'Credit Voucher for '.$this->payment_reference; break; - case 1: - $booking = Transaction::where('payment_reference', $this->bill_no)->first()->owner; + case TransactionType::PAYMENT: + $order = Transaction::where('payment_reference', $this->bill_no)->first()->owner->owner->owner; - if(!$booking) { - $description = 'Payment for unknown booking, please contact tech support.'; + if(!$order) { + $description = 'Payment for unknown order, please contact tech support.'; break; } - $marking = $booking->marking; - $description = 'Payment For booking refs.'.''.$marking.''; + $marking = $order->reference; + $description = 'Payment For order refs.'.''.$marking.''; break; case 11: $description = 'Debit Voucher for '.$this->payment_reference; diff --git a/app/Models/Group.php b/app/Models/Group.php new file mode 100644 index 00000000..c8d02a71 --- /dev/null +++ b/app/Models/Group.php @@ -0,0 +1,86 @@ +belongsToMany(Transaction::class, GroupTransaction::class); + // return $this->MorphMany(Transaction::class, 'owner'); + // } + + /** + * @return MorphMany + */ + public function transactions(): MorphMany + { + return $this->MorphMany(Transaction::class, 'owner'); + } + + /** + * @return MorphMany + */ + public function documents(): morphMany + { + return $this->morphMany(Document::class, 'owner'); + } + + /** + * @return BelongsTo + */ + public function currency(): BelongsTo + { + return $this->BelongsTo(Currency::class, 'currency_id', 'id'); + } + + + /** + * @return HasManyDeep + */ + public function transferFees(): HasManyDeep + { + return $this->HasManyDeep(Transaction::class, [GroupTransaction::class, Transaction::class.' as alias'], ['group_id', ['owner_type', 'owner_id'], ['owner_type', 'owner_id']], ['id', null, null]); + } + + /** + * @return BelongsTo + */ + public function issuerCompany(): BelongsTo + { + return $this->BelongsTo(CompanyModule::class, 'issuer', 'id'); + } + + /** + * @return BelongsTo + */ + public function receiverCompany(): BelongsTo + { + return $this->BelongsTo(CompanyModule::class, 'receiver', 'id'); + } + + /** + * @return BelongsTo + */ + public function original_currency(): BelongsTo + { + return $this->BelongsTo(Currency::class, 'original_currency_id', 'id'); + } +} diff --git a/app/Models/GroupTransaction.php b/app/Models/GroupTransaction.php new file mode 100644 index 00000000..9ab72d18 --- /dev/null +++ b/app/Models/GroupTransaction.php @@ -0,0 +1,27 @@ +BelongsTo(Group::class, 'group_id', 'id'); + } + + /** + * @return BelongsTo + */ + public function transaction(): BelongsTo + { + return $this->BelongsTo(Transaction::class, 'transaction_id', 'id'); + } +} diff --git a/database/migrations/2023_03_19_170628_create_groups_table.php b/database/migrations/2023_03_19_170628_create_groups_table.php new file mode 100644 index 00000000..569627c2 --- /dev/null +++ b/database/migrations/2023_03_19_170628_create_groups_table.php @@ -0,0 +1,43 @@ +id(); + $table->string('reference')->unique(); + $table->foreignId('issuer')->unsigned(); + $table->foreignId('receiver')->unsigned(); + $table->decimal('amount', 14, 5)->default(0.00); + $table->decimal('original_amount', 14, 5)->default(0.00); + $table->foreignId('currency_id')->unsigned(); + $table->foreignId('original_currency_id')->unsigned(); + $table->decimal('currency_rate', 14, 5)->default(0.00); + $table->decimal('tax', 14, 5)->default(0.00); + $table->decimal('service_charge', 14, 5)->default(0.00); + $table->integer('status')->default(ApprovalStatus::PENDING_VERIFICATION); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('groups'); + } +} diff --git a/database/migrations/2023_03_19_170641_create_group_transactions_table.php b/database/migrations/2023_03_19_170641_create_group_transactions_table.php new file mode 100644 index 00000000..53f33356 --- /dev/null +++ b/database/migrations/2023_03_19_170641_create_group_transactions_table.php @@ -0,0 +1,32 @@ +id(); + $table->foreignId('group_id')->unsigned(); + $table->foreignId('transaction_id')->unsigned(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('group_transactions'); + } +} diff --git a/resources/assets/vue/components/companies/elements/CustomerPaymentBillingInnerComponent.vue b/resources/assets/vue/components/companies/elements/CustomerPaymentBillingInnerComponent.vue index 8ec98df2..0eeb1eb9 100644 --- a/resources/assets/vue/components/companies/elements/CustomerPaymentBillingInnerComponent.vue +++ b/resources/assets/vue/components/companies/elements/CustomerPaymentBillingInnerComponent.vue @@ -1,5 +1,5 @@