From 1585dcfa5aea2093b4a464a7eabdc3524c00205c Mon Sep 17 00:00:00 2001 From: omair saleh Date: Thu, 10 Feb 2022 10:28:26 +0800 Subject: [PATCH] clean up wallet --- .../ControllersLogic/CreditWalletLogic.php | 39 ++++++----------- .../ControllersLogic/DebitWalletLogic.php | 11 +++-- app/Http/Resources/WalletResource.php | 12 +----- .../Resources/WalletTransactionResource.php | 43 +++++++++++++++---- .../elements/PaymentVerificationComponent.vue | 2 +- .../CustomerTransactionSectionComponent.vue | 38 ++++++++++------ .../forms/WalletTopUpFormComponent.vue | 42 ++++++++++++------ routes/wallet.php | 1 - 8 files changed, 112 insertions(+), 76 deletions(-) diff --git a/app/Classes/Modules/Wallets/ControllersLogic/CreditWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/CreditWalletLogic.php index 7e93ccb5..1e7b3ceb 100644 --- a/app/Classes/Modules/Wallets/ControllersLogic/CreditWalletLogic.php +++ b/app/Classes/Modules/Wallets/ControllersLogic/CreditWalletLogic.php @@ -82,46 +82,35 @@ class CreditWalletLogic extends AbstractControllerLogic /** * @param Request $request * @return JsonResponse - * @throws ErrorException + * @throws \App\Classes\Exceptions\MalformedRequestException */ public function logic(Request $request) : JsonResponse { $amount = floatval(str_replace(',', '', $request->input('amount'))); + $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]); + + $reference = $request->input('reference'); + + $type = $request->input('transaction_type'); + if (!$company->wallets()->first()) { $object = new WalletObject($company->id, 1, $this->generatesWalletCode->execute()); - $wallet = $this->createsWallet->execute($object, $company); + $this->createsWallet->execute($object, $company); } $wallet = $company->wallets()->first(); - $billNumber = $this->generatesTransactionBillNumber->execute('CREDIT-NOTE-'); + $billNumber = $this->generatesTransactionBillNumber->execute($type === 2 ? 'DEBIT-NOTE-' : 'CREDIT-NOTE-'); - $transaction_object = new TransactionObject( - $billNumber, - TransactionType::CREDIT_NOTE, - 1, - $wallet->company->id, - 1, - PaymentMethodType::WALLET, - $amount, - $amount, - 1, - 1, - 1, - 0, - 0, - null, - ApprovalStatus::PENDING_SUBMISSION, - [] - ); - + $transaction_object = new TransactionObject($billNumber, $type === 2 ? TransactionType::DEBIT_NOTE : TransactionType::CREDIT_NOTE, 1, $wallet->company->id, 1, PaymentMethodType::CASH, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::APPROVED, [], $reference); $transaction = $this->createsTransaction->execute($wallet, $transaction_object); - $updateWalletAmount = $wallet->amount + $transaction->amount; - $walletOject = new WalletObject($wallet->company->id, $wallet->currency_id, $wallet->code, $updateWalletAmount); + $updateWalletAmount = $type === 2 ? ($wallet->amount - $transaction->amount) : ($wallet->amount + $transaction->amount); - $wallet = $this->updatesWallet->execute($wallet, $walletOject); + $walletObject = new WalletObject($wallet->company->id, $wallet->currency_id, $wallet->code, $updateWalletAmount); + + $wallet = $this->updatesWallet->execute($wallet, $walletObject); return $this->resourceResponse(new WalletResource($wallet)); } diff --git a/app/Classes/Modules/Wallets/ControllersLogic/DebitWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/DebitWalletLogic.php index 3085184f..682065c0 100644 --- a/app/Classes/Modules/Wallets/ControllersLogic/DebitWalletLogic.php +++ b/app/Classes/Modules/Wallets/ControllersLogic/DebitWalletLogic.php @@ -68,12 +68,14 @@ class DebitWalletLogic extends AbstractControllerLogic /** * @param Request $request * @return JsonResponse - * @throws ErrorException + * @throws \App\Classes\Exceptions\MalformedRequestException */ public function logic(Request $request) : JsonResponse { $amount = floatval(str_replace(',', '', $request->input('amount'))); $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]); + $reference = $request->input('reference'); + if (!$company->wallets()->first()) { $object = new WalletObject($company->id, 1, $this->generatesWalletCode->execute()); $wallet = $this->createsWallet->execute($object, $company); @@ -89,7 +91,7 @@ class DebitWalletLogic extends AbstractControllerLogic 1, $wallet->company->id, 1, - PaymentMethodType::WALLET, + PaymentMethodType::CASH, $amount, $amount, 1, @@ -98,8 +100,9 @@ class DebitWalletLogic extends AbstractControllerLogic 0, 0, null, - ApprovalStatus::PENDING_SUBMISSION, - [] + ApprovalStatus::APPROVED, + [], + $reference ); $transaction = $this->createsTransaction->execute($wallet, $transaction_object); diff --git a/app/Http/Resources/WalletResource.php b/app/Http/Resources/WalletResource.php index 9123951e..35cdedf2 100644 --- a/app/Http/Resources/WalletResource.php +++ b/app/Http/Resources/WalletResource.php @@ -21,16 +21,8 @@ class WalletResource extends JsonResource 'currency_id' => $this->currency_id, 'amount' => (double) $this->amount, 'company_id' => (int) $this->owner->id, - 'transactions' => $this->transactions()->whereIn('status', [2, 3])->orderBy('id', 'DESC')->get(), - 'topup_transactions' => $this->transactions()->where('type', TransactionType::TOP_UP)->orderBy('id', 'DESC')->get(), - - // 'transaction' => $this->whenLoaded('transactions', function() { - // return [ - // 'topup' => $this->transactions - // ]; - // }), - // 'transaction' // all - // 'top_transaction' // only topup + 'transactions' => WalletTransactionResource::collection($this->transactions()->whereIn('status', [2, 3])->orderBy('id', 'DESC')->get()), + 'top_up_records' => WalletTransactionResource::collection($this->transactions()->where('type', TransactionType::TOP_UP)->orderBy('id', 'DESC')->get()) ]; } } diff --git a/app/Http/Resources/WalletTransactionResource.php b/app/Http/Resources/WalletTransactionResource.php index af634f3b..2384e695 100644 --- a/app/Http/Resources/WalletTransactionResource.php +++ b/app/Http/Resources/WalletTransactionResource.php @@ -2,6 +2,10 @@ namespace App\Http\Resources; +use App\Classes\General\Eloquent\Filters\TransactionServiceId; +use App\Classes\ValueObjects\Constants\TransactionType; +use App\Models\Transaction; +use Carbon\Carbon; use Illuminate\Http\Resources\Json\JsonResource; class WalletTransactionResource extends JsonResource @@ -14,17 +18,38 @@ class WalletTransactionResource extends JsonResource */ public function toArray($request) { + $description = ''; + switch((int) $this->type){ + case 5: + $description = (double) $this->amount.' Credit Top up'; + break; + case 9: + $description = 'Credit Voucher for '.$this->payment_reference; + break; + case 1: + $marking = Transaction::where('payment_reference', $this->bill_no)->first()->owner->marking; + $description = 'Payment For booking refs.'.''.$marking.''; + break; + case 11: + $description = 'Debit Voucher for '.$this->payment_reference; + break; + + } + return [ - 'id' => $this->id, - 'wallet_id' => (int) $this->wallet_id, - 'bill_no' => (int) $this->bill_no, - 'trans_type'=>(int) $this->trans_type, + 'type' => (int) $this->type, + 'bill_no' => $this->bill_no, + 'reference' => $this->payment_reference, + 'payment_method' => (float) $this->payment_method, + 'issuer_name' => $this->issuerCompany->name, 'amount' => (double) $this->amount, - 'currency_id' => (int) $this->currency_id, - 'original_amount' => (double) $this->original_amount, - 'original_currency_id' => (int) $this->original_currency_id, - 'currency_rate' => (double) $this->currency_rate, - 'reference' => $this->payment_reference + 'service_charge' => (double) $this->service_charge, + 'tax' => (double) $this->tax, + 'status' => (int) $this->status, + 'description' => $description, + 'details' => TransactionDetailResource::collection($this->transactionDetails), + 'updated_at' => Carbon::parse($this->updated_at)->format('d-m-Y h:i:s A'), + 'created_at' => Carbon::parse($this->created_at)->format('d-m-Y h:i:s A') ]; } } diff --git a/resources/assets/vue/components/bookings/elements/PaymentVerificationComponent.vue b/resources/assets/vue/components/bookings/elements/PaymentVerificationComponent.vue index d04b50c5..78965cab 100644 --- a/resources/assets/vue/components/bookings/elements/PaymentVerificationComponent.vue +++ b/resources/assets/vue/components/bookings/elements/PaymentVerificationComponent.vue @@ -28,7 +28,7 @@
Amount
- {{(Math.round((item.original_amount + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}} + {{(Math.round((item.amount + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}
diff --git a/resources/assets/vue/components/wallets/elements/CustomerTransactionSectionComponent.vue b/resources/assets/vue/components/wallets/elements/CustomerTransactionSectionComponent.vue index f4f211e2..6e5e370d 100644 --- a/resources/assets/vue/components/wallets/elements/CustomerTransactionSectionComponent.vue +++ b/resources/assets/vue/components/wallets/elements/CustomerTransactionSectionComponent.vue @@ -9,23 +9,22 @@
Transaction History
-
-
Date
+
Date
Description
-
Incoming
-
Outgoing
-
Balance
+
Incoming
+
Outgoing
+
Balance
-
-
{{item.created_at}}
-
{{[5, 9].includes(parseFloat(item.type)) ? 'Top Up' : 'Payment to bill'}}
-
{{[5].includes(parseFloat(item.type)) ? (Math.round((parseFloat(item.amount) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : ''}}
-
{{[1].includes(parseFloat(item.type)) ? '- ' + (Math.round((parseFloat(item.amount) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : ''}}
-
Balance
+
+
{{item.created_at}}
+
+
{{[5, 9].includes(parseFloat(item.type)) ? (Math.round((parseFloat(item.amount) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : ''}}
+
{{[1, 11].includes(parseFloat(item.type)) ? '- ' + (Math.round((parseFloat(item.amount) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : ''}}
+
{{remainingBalance(index)}}
@@ -64,11 +63,11 @@
- +
-
+
@@ -132,6 +131,19 @@ export default { this.isLoading = true; this.submit(route('api.company.show', this.id), 'get', this.section, false, false) }, + remainingBalance(index) { + let tempBalance = 0; + + if(this.company.wallet){ + let transactions = this.company.wallet.transactions.slice().reverse(); + transactions.slice(0, transactions.length - index).map(function(transaction) { + [1, 11].includes(transaction.type) ? tempBalance -= (transaction.amount) : tempBalance += (transaction.amount); + return tempBalance + }, 0); + } + + return (Math.round((tempBalance + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); + }, successHandler(response){ this.isLoading = false; this.company = response.payload.data; diff --git a/resources/assets/vue/components/wallets/forms/WalletTopUpFormComponent.vue b/resources/assets/vue/components/wallets/forms/WalletTopUpFormComponent.vue index ae5f605e..d4d637a2 100644 --- a/resources/assets/vue/components/wallets/forms/WalletTopUpFormComponent.vue +++ b/resources/assets/vue/components/wallets/forms/WalletTopUpFormComponent.vue @@ -2,21 +2,33 @@
-
-
-

Credit

+
+
+
+
+

Credit

+
+
+
+
+

Debit

+
+
-
-
-
-

Debit

+
+
+ + + + +
- +
@@ -75,7 +87,7 @@