From 526c5694fabf23060cf217b7d2d54d847dff1dc2 Mon Sep 17 00:00:00 2001 From: edmondlang Date: Tue, 9 May 2023 16:01:17 +0800 Subject: [PATCH 01/14] bulk import honey trap --- .../elements/UploadHoneyTrapCsvComponent.vue | 69 +++++++++++++++++++ resources/views/pages/settings.blade.php | 5 ++ 2 files changed, 74 insertions(+) create mode 100644 resources/assets/vue/components/companies/elements/UploadHoneyTrapCsvComponent.vue diff --git a/resources/assets/vue/components/companies/elements/UploadHoneyTrapCsvComponent.vue b/resources/assets/vue/components/companies/elements/UploadHoneyTrapCsvComponent.vue new file mode 100644 index 00000000..9daf132c --- /dev/null +++ b/resources/assets/vue/components/companies/elements/UploadHoneyTrapCsvComponent.vue @@ -0,0 +1,69 @@ + + + \ No newline at end of file diff --git a/resources/views/pages/settings.blade.php b/resources/views/pages/settings.blade.php index 100fb380..a7f735cb 100644 --- a/resources/views/pages/settings.blade.php +++ b/resources/views/pages/settings.blade.php @@ -321,6 +321,11 @@ +
+
+ +
+
From d315c47bcac9e540521ec62a40f29f592e51e493 Mon Sep 17 00:00:00 2001 From: edmondlang Date: Wed, 10 May 2023 01:41:30 +0800 Subject: [PATCH 02/14] upload-honey-trap --- .../Imports/Services/GenericImport.php | 23 ++++ .../Imports/ImportHoneyTrapController.php | 106 ++++++++++++++++++ .../elements/UploadHoneyTrapCsvComponent.vue | 35 +++++- resources/views/pages/honey_trap.blade.php | 12 ++ resources/views/pages/settings.blade.php | 5 - routes/api.php | 1 + routes/web.php | 3 + 7 files changed, 175 insertions(+), 10 deletions(-) create mode 100644 app/Classes/Modules/Imports/Services/GenericImport.php create mode 100644 app/Http/Controllers/Imports/ImportHoneyTrapController.php create mode 100644 resources/views/pages/honey_trap.blade.php diff --git a/app/Classes/Modules/Imports/Services/GenericImport.php b/app/Classes/Modules/Imports/Services/GenericImport.php new file mode 100644 index 00000000..b2ffdb8f --- /dev/null +++ b/app/Classes/Modules/Imports/Services/GenericImport.php @@ -0,0 +1,23 @@ +rows = $collection; + } +} diff --git a/app/Http/Controllers/Imports/ImportHoneyTrapController.php b/app/Http/Controllers/Imports/ImportHoneyTrapController.php new file mode 100644 index 00000000..a4dbc858 --- /dev/null +++ b/app/Http/Controllers/Imports/ImportHoneyTrapController.php @@ -0,0 +1,106 @@ +input('files'), '', ApprovalStatus::APPROVED, 'imports'); + $file = json_decode($object->getFiles()[0])->file_info->original->file; + + $import = new GenericImport(); + Excel::import($import, $file); + $excelRows = $import->rows; + $excelRows = $excelRows->toArray(); + + $returnArray = []; + + $segment_id = Segment::where('name', 'honey trap')->first()->id; + + foreach ($excelRows as $row) { + + if (is_null($row['email']) || empty($row['email'])) { + continue; + } + + if (is_null($row['end_date']) || empty($row['end_date'])) { + // use csv import must have an end_date + $row['status'] = 'failed'; + $row['message'] = 'End Date is required'; + $returnArray[] = $row; + continue; + } + + $row['end_date'] = $end_date = $this->changeExcelDate($row['end_date']); + $start_date = $row['start_date'] ? $this->changeExcelDate($row['start_date']) : Carbon::now(); + $row['start_date'] = $start_date; + + $end_date = Carbon::parse($end_date); + // Check if the end_date is in the past + if ($end_date->isPast()) { + // end_date must be after today's date + $row['status'] = 'failed'; + $row['message'] = "End Date must be after today's date"; + $returnArray[] = $row; + continue; + } + + $user = User::where('email', $row['email'])->first(); + if (!$user) { + // if user not found + $row['status'] = 'failed'; + $row['message'] = 'Email not found'; + $returnArray[] = $row; + continue; + } + + $company = $user->company->first(); + if (!$company) { + // if company not found + $row['status'] = 'failed'; + $row['message'] = 'Company not found'; + $returnArray[] = $row; + continue; + } + + $seasonalSegmentObject = new SeasonalSegmentObject($company->id, $segment_id, $start_date, $end_date ?? null); + + (App()->make(createsSeasonalSegment::class))->execute($seasonalSegmentObject); + (App()->make(assignSegmentProcessor::class))->execute($company, $segment_id); + + // success added honey trap seasonal segment + $row['status'] = 'success'; + $row['message'] = ''; + $returnArray[] = $row; + continue; + } + + return response()->json($returnArray); + } + + public function changeExcelDate($date) + { + $unixTime = (($date - 25569) * 86400); + $date = new DateTime("@$unixTime"); + return $date->format('d/m/Y'); + } +} diff --git a/resources/assets/vue/components/companies/elements/UploadHoneyTrapCsvComponent.vue b/resources/assets/vue/components/companies/elements/UploadHoneyTrapCsvComponent.vue index 9daf132c..6b3a6569 100644 --- a/resources/assets/vue/components/companies/elements/UploadHoneyTrapCsvComponent.vue +++ b/resources/assets/vue/components/companies/elements/UploadHoneyTrapCsvComponent.vue @@ -4,7 +4,7 @@
-
+
@@ -32,6 +32,19 @@
+
+
Uploaded Customer
+ + + + + + + +
{{ key }}
+ {{ col }} +
+
@@ -45,7 +58,8 @@ data(){ return { files: [], - parameters: {} + parameters: {}, + returnData: null } }, validations: { @@ -53,14 +67,25 @@ required } }, + computed: { + returnDataKeys() { + return Object.keys(Object.assign({}, this.returnData[0])) + }, + }, methods: { submitForm(){ - this.parameters = { files: this.files }; - - this.submit(this.route('api.debtor.import'), 'post', this.section, true, false) + this.submit(this.route('api.honey_trap.upload'), 'post', this.section, true, false); + }, + errorHandler(error){ + console.log(error); + }, + successHandler(response) { + console.log(response); + this.returnData = response; + console.log(this.returnDataKeys); }, }, mixins: [ModalFromHandler] diff --git a/resources/views/pages/honey_trap.blade.php b/resources/views/pages/honey_trap.blade.php new file mode 100644 index 00000000..69c28f15 --- /dev/null +++ b/resources/views/pages/honey_trap.blade.php @@ -0,0 +1,12 @@ +@extends('layouts.base_portal') +@section('inner_content') +
+
+
+
+ +
+
+
+
+@endsection \ No newline at end of file diff --git a/resources/views/pages/settings.blade.php b/resources/views/pages/settings.blade.php index a7f735cb..100fb380 100644 --- a/resources/views/pages/settings.blade.php +++ b/resources/views/pages/settings.blade.php @@ -321,11 +321,6 @@
-
-
- -
-
diff --git a/routes/api.php b/routes/api.php index 6c17a107..b3e0002c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -29,6 +29,7 @@ Route::group(['middleware' => 'api', 'prefix' => 'v1', 'as' => 'api.'], function Route::get('/storage/{fileName}/fetch', 'Documents\RenderDocumentController@fileStorageServe')->where(['fileName' => '.*'])->name('storage.document.file'); Route::post('/import/update-debtor/f614e339d7058904a831aad742e24d55', 'Imports\ImportUpdateDebtorController@import')->name('debtor.import'); + Route::post('/import/upload-honey-trap', 'Imports\ImportHoneyTrapController@import')->name('honey_trap.upload'); require __DIR__ . '/company.php'; diff --git a/routes/web.php b/routes/web.php index 1e9c3d00..2547d087 100644 --- a/routes/web.php +++ b/routes/web.php @@ -598,3 +598,6 @@ Route::get('/po/outsource/check', function(){ }); +Route::get('/upload-honey-trap', function () { + return view('pages.honey_trap'); +})->name('upload_honey_trap'); \ No newline at end of file From d586e1124936bde8c6fa6f502aba0a47b154c153 Mon Sep 17 00:00:00 2001 From: edmondlang Date: Wed, 10 May 2023 01:46:35 +0800 Subject: [PATCH 03/14] upload-honey-trap --- .../Controllers/Imports/ImportHoneyTrapController.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/Http/Controllers/Imports/ImportHoneyTrapController.php b/app/Http/Controllers/Imports/ImportHoneyTrapController.php index a4dbc858..01261356 100644 --- a/app/Http/Controllers/Imports/ImportHoneyTrapController.php +++ b/app/Http/Controllers/Imports/ImportHoneyTrapController.php @@ -14,6 +14,7 @@ use Illuminate\Http\Request; use Maatwebsite\Excel\Facades\Excel; use App\Classes\Modules\Segments\Services\CreatesSeasonalSegment; use App\Classes\Modules\Companies\Processors\AssignSegmentProcessor; +use App\Models\SeasonalSegment; class ImportHoneyTrapController { @@ -82,6 +83,15 @@ class ImportHoneyTrapController continue; } + $company_seasonal_honey_trap_count = SeasonalSegment::where('company_id', $company->id)->where('segment_id', $segment_id)->get(); + if (count($company_seasonal_honey_trap_count)) { + // seasonal segment already exists + $row['status'] = 'failed'; + $row['message'] = 'Company is already in the Honey Trap Segment'; + $returnArray[] = $row; + continue; + } + $seasonalSegmentObject = new SeasonalSegmentObject($company->id, $segment_id, $start_date, $end_date ?? null); (App()->make(createsSeasonalSegment::class))->execute($seasonalSegmentObject); From d058d10e4975caa64bfd12e9dfc75a344bcf8088 Mon Sep 17 00:00:00 2001 From: edmondlang Date: Wed, 10 May 2023 10:38:34 +0800 Subject: [PATCH 04/14] change honey trap to honey trap platinum --- app/Http/Controllers/Imports/ImportHoneyTrapController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Imports/ImportHoneyTrapController.php b/app/Http/Controllers/Imports/ImportHoneyTrapController.php index 01261356..81ef237b 100644 --- a/app/Http/Controllers/Imports/ImportHoneyTrapController.php +++ b/app/Http/Controllers/Imports/ImportHoneyTrapController.php @@ -35,7 +35,7 @@ class ImportHoneyTrapController $returnArray = []; - $segment_id = Segment::where('name', 'honey trap')->first()->id; + $segment_id = Segment::where('name', 'honey trap platinum')->first()->id; foreach ($excelRows as $row) { From 2033842d6cc6bdda6641a85cb5b26d1e65d61062 Mon Sep 17 00:00:00 2001 From: edmondlang Date: Wed, 10 May 2023 10:43:48 +0800 Subject: [PATCH 05/14] only admin and upload honey trap csv --- resources/views/pages/honey_trap.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/pages/honey_trap.blade.php b/resources/views/pages/honey_trap.blade.php index 69c28f15..f0de7afe 100644 --- a/resources/views/pages/honey_trap.blade.php +++ b/resources/views/pages/honey_trap.blade.php @@ -1,6 +1,6 @@ @extends('layouts.base_portal') @section('inner_content') -
+
From 455d1d83faeb608ee3016b3a75de247664727da5 Mon Sep 17 00:00:00 2001 From: edmondlang Date: Wed, 10 May 2023 13:38:11 +0800 Subject: [PATCH 06/14] fix sending billplz email to customer on development environment --- app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php b/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php index 1bed083c..a2947e82 100644 --- a/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php +++ b/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php @@ -4,6 +4,7 @@ namespace App\Classes\Modules\Billplzs\Services; use Illuminate\Support\Facades\Http; use App\Classes\Exceptions\MalformedRequestException; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Log; class CreatesBillplzBill @@ -26,7 +27,7 @@ class CreatesBillplzBill $response = Http::withBasicAuth(config('billplz.api_key').':', '')->post(config('billplz.base_url').'/api/v3/bills', [ 'collection_id' => $wallet ? config('billplz.wallet_collection_id') : config('billplz.collection_id'), 'name' => $name, - 'email' => $email, + 'email' => App::environment('production') ? $email : 'development@cief-malaysia.com', 'description' => $description, 'amount' => $this->finalizeAmount($amount), 'redirect_url' => route('online_payment.redirect'), From bc7c0026bd062a11aa15c07b653c7f116e8f623e Mon Sep 17 00:00:00 2001 From: edmondlang Date: Wed, 10 May 2023 13:38:39 +0800 Subject: [PATCH 07/14] fix wallet bug --- .../Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php index a6c0b7fd..a9beb2d0 100644 --- a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php +++ b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php @@ -91,7 +91,7 @@ class CallbackBillplzLogic $status = $billplzXSignatureObject->getStatus() === 'failed' ? ApprovalStatus::REJECTED : ApprovalStatus::PENDING_VERIFICATION; } - if($transaction->status !== ApprovalStatus::COMPLETED){ + if($transaction->status !== ApprovalStatus::COMPLETED && $transaction->status !== ApprovalStatus::APPROVED){ if($transaction->owner instanceof Wallet && $transaction->status !== ApprovalStatus::APPROVED && $status === ApprovalStatus::APPROVED) { $this->updatesWalletBalance->execute($transaction->owner, $transaction->amount); From e84f288d39d2fcef864dd8c459f08c27e0dd3c83 Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Mon, 15 May 2023 10:44:04 +0800 Subject: [PATCH 08/14] xpo jan & feb --- .../Bookings/ControllersLogic/AutoPurchaseOrderFillLogic.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Classes/Modules/Bookings/ControllersLogic/AutoPurchaseOrderFillLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/AutoPurchaseOrderFillLogic.php index 1983952e..bc837878 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/AutoPurchaseOrderFillLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/AutoPurchaseOrderFillLogic.php @@ -65,7 +65,7 @@ class AutoPurchaseOrderFillLogic extends AbstractControllerLogic public function logic(Request $request) : JsonResponse { $bookings = Booking::where('service_id', '!=', 4)->where(function($query){ - return $query->whereMonth('created_at', '>', 07)->whereYear('created_at', 2022); + return $query->whereMonth('created_at', '<', 03)->whereYear('created_at', 2023); })->whereDoesntHave('transactions', function($q){ $q->where('type', TransactionType::PURCHASE_ORDER); $q->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED]); From 96193a2880d36be14b43f7c01ae934a26385e0fa Mon Sep 17 00:00:00 2001 From: edmondlang Date: Wed, 17 May 2023 23:36:32 +0800 Subject: [PATCH 09/14] recalculate wallet balance --- .../ControllersLogic/CallbackBillplzLogic.php | 22 ++++++++- .../Services/RecalculatesWalletBalance.php | 48 +++++++++++++++++++ .../Wallets/Services/UpdatesWalletBalance.php | 11 ++++- 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 app/Classes/Modules/Wallets/Services/RecalculatesWalletBalance.php diff --git a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php index a9beb2d0..7c7ed52c 100644 --- a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php +++ b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php @@ -25,6 +25,7 @@ use App\Classes\Modules\Transactions\Services\FetchesTransaction; use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; +use App\Classes\Modules\Wallets\Services\RecalculatesWalletBalance; class CallbackBillplzLogic { @@ -45,20 +46,25 @@ class CallbackBillplzLogic /** @var CreateCashBackTransactionProcessor */ private $createCashBackTransactionProcessor; + /** @var RecalculatesWalletBalance */ + private $recalculatesWalletBalance; + /** * CallbackBillplzLogic constructor. * @param GetBillplzBill $getBillplzBill * @param FetchesTransaction $fetchesTransaction * @param UpdatesTransactionStatus $updatesTransactionStatus * @param UpdatesWalletBalance $updatesWalletBalance + * @param RecalculatesWalletBalance $recalculatesWalletBalance */ - public function __construct(GetBillplzBill $getBillplzBill, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, UpdatesWalletBalance $updatesWalletBalance, CreateCashBackTransactionProcessor $createCashBackTransactionProcessor) + public function __construct(GetBillplzBill $getBillplzBill, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, UpdatesWalletBalance $updatesWalletBalance, CreateCashBackTransactionProcessor $createCashBackTransactionProcessor, RecalculatesWalletBalance $recalculatesWalletBalance) { $this->getBillplzBill = $getBillplzBill; $this->fetchesTransaction = $fetchesTransaction; $this->updatesTransactionStatus = $updatesTransactionStatus; $this->updatesWalletBalance = $updatesWalletBalance; $this->createCashBackTransactionProcessor = $createCashBackTransactionProcessor; + $this->recalculatesWalletBalance = $recalculatesWalletBalance; } @@ -104,6 +110,20 @@ class CallbackBillplzLogic // $cash_back_transaction = $this->createCashBackTransactionProcessor->execute($transaction); // } + + $wallet = null; + if ($transaction->owner instanceof Wallet) { + $wallet = $transaction->owner; + } + + if ($transaction->owner->company->wallets()->first() instanceof Wallet) { + $wallet = $transaction->owner->company->wallets()->first(); + } + + if ($wallet != null) { + $this->recalculatesWalletBalance->execute($wallet); + } + $token = Auth::fromUser(User::find(1)); $request->headers->set('Authorization', 'Bearer '.$token); diff --git a/app/Classes/Modules/Wallets/Services/RecalculatesWalletBalance.php b/app/Classes/Modules/Wallets/Services/RecalculatesWalletBalance.php new file mode 100644 index 00000000..651bee4a --- /dev/null +++ b/app/Classes/Modules/Wallets/Services/RecalculatesWalletBalance.php @@ -0,0 +1,48 @@ +updatesWalletBalance = $updatesWalletBalance; + } + /** + * @param Wallet $model + * @param $amount + * @return \Illuminate\Database\Eloquent\Model + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function execute(Wallet $wallet) + { + + $i = 0; + $topups = 0; + $credit = 0; + $payments = 0; + $debit = 0; + + foreach ($wallet->transactions as $transaction) { + if (!in_array((int) $transaction->status, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])) continue; + if ((int) $transaction->type === TransactionType::TOP_UP) { + $topups += (float) $transaction->amount; + } + if ((int) $transaction->type === TransactionType::CREDIT_NOTE) $credit += (float) $transaction->amount; + if ((int) $transaction->type === TransactionType::PAYMENT) $payments += (float) $transaction->amount; + if ((int) $transaction->type === TransactionType::DEBIT_NOTE) $debit += (float) $transaction->amount; + } + $auditBalance = ($topups + $credit) - ($payments + $debit); + + return $auditBalance; + } +} diff --git a/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php b/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php index a724cf24..a94fd9a8 100644 --- a/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php +++ b/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php @@ -10,6 +10,13 @@ use App\Models\Company; class UpdatesWalletBalance extends AbstractUpdateRecord { + private $recalculatesWalletBalance; + + public function __construct(RecalculatesWalletBalance $recalculatesWalletBalance) + { + $this->recalculatesWalletBalance = $recalculatesWalletBalance; + } + /** * @param Wallet $model * @param $amount @@ -18,7 +25,9 @@ class UpdatesWalletBalance extends AbstractUpdateRecord */ public function execute(Wallet $model, $amount) { - $model->amount = $model->amount + $amount; + $auditBalance = $this->recalculatesWalletBalance->execute($model); + + $model->amount = $auditBalance; return $this->handler($model); } From d5b04b9b8111f34f25344f8f37c6baedf89cfcc2 Mon Sep 17 00:00:00 2001 From: edmondlang Date: Wed, 17 May 2023 23:44:49 +0800 Subject: [PATCH 10/14] AuditAndUpdateWallletBalance --- .../Commands/AuditAndUpdateWallletBalance.php | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 app/Console/Commands/AuditAndUpdateWallletBalance.php diff --git a/app/Console/Commands/AuditAndUpdateWallletBalance.php b/app/Console/Commands/AuditAndUpdateWallletBalance.php new file mode 100644 index 00000000..453c39be --- /dev/null +++ b/app/Console/Commands/AuditAndUpdateWallletBalance.php @@ -0,0 +1,77 @@ +removesCompanyFromSegment = $removesCompanyFromSegment; + } + + /** + * Execute the console command. + * + * @return int + */ + public function handle() + { + $wallets = Wallet::all(); + + $i = 0; + foreach ($wallets as $wallet) { + $topups = 0; + $credit = 0; + $payments = 0; + $debit = 0; + + foreach ($wallet->transactions as $transaction) { + if (!in_array((int) $transaction->status, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])) continue; + if ((int) $transaction->type === TransactionType::TOP_UP) { + $topups += (float) $transaction->amount; + } + if ((int) $transaction->type === TransactionType::CREDIT_NOTE) $credit += (float) $transaction->amount; + if ((int) $transaction->type === TransactionType::PAYMENT) $payments += (float) $transaction->amount; + if ((int) $transaction->type === TransactionType::DEBIT_NOTE) $debit += (float) $transaction->amount; + } + $auditBalance = ($topups + $credit) - ($payments + $debit); + $diffenrence = round((float) $wallet->amount - (($topups + $credit) - ($payments + $debit)), 2); + if ((($diffenrence == 0) || ($diffenrence == -0)) and $wallet->amount > -0.01) continue; + + $i++; + + $this->info($i . ". Marking: " . $wallet->owner->reference . "(" . $wallet->id . ")" . PHP_EOL . "Current Balance: " . $wallet->amount . PHP_EOL . "Audit Balance: " . ($auditBalance) . PHP_EOL . "Difference: " . $diffenrence . PHP_EOL); + } + } +} From 43a9bf6742418a4f9f505b3fb869086b589d955f Mon Sep 17 00:00:00 2001 From: edmondlang Date: Wed, 17 May 2023 23:49:32 +0800 Subject: [PATCH 11/14] AuditAndUpdateWallletBalance --- app/Console/Commands/AuditAndUpdateWallletBalance.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/Console/Commands/AuditAndUpdateWallletBalance.php b/app/Console/Commands/AuditAndUpdateWallletBalance.php index 453c39be..0dd1be05 100644 --- a/app/Console/Commands/AuditAndUpdateWallletBalance.php +++ b/app/Console/Commands/AuditAndUpdateWallletBalance.php @@ -72,6 +72,8 @@ class AuditAndUpdateWallletBalance extends Command $i++; $this->info($i . ". Marking: " . $wallet->owner->reference . "(" . $wallet->id . ")" . PHP_EOL . "Current Balance: " . $wallet->amount . PHP_EOL . "Audit Balance: " . ($auditBalance) . PHP_EOL . "Difference: " . $diffenrence . PHP_EOL); + + Wallet::where('id', $wallet->id)->update(['amount' => $auditBalance]); } } } From 35edc807a8adf2731f04fd23a8df102c4219e4fc Mon Sep 17 00:00:00 2001 From: edmondlang Date: Thu, 18 May 2023 00:07:20 +0800 Subject: [PATCH 12/14] recalculate wallet balance --- .../ControllersLogic/CallbackBillplzLogic.php | 8 ++++---- .../ControllersLogic/CreateBookingPaymentLogic.php | 11 +++++++++-- .../Modules/Wallets/Services/UpdatesWalletBalance.php | 11 +---------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php index 7c7ed52c..bcaa57a7 100644 --- a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php +++ b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php @@ -114,14 +114,14 @@ class CallbackBillplzLogic $wallet = null; if ($transaction->owner instanceof Wallet) { $wallet = $transaction->owner; - } - - if ($transaction->owner->company->wallets()->first() instanceof Wallet) { + } else if ($transaction->owner->company->wallets()->first() instanceof Wallet) { $wallet = $transaction->owner->company->wallets()->first(); } if ($wallet != null) { - $this->recalculatesWalletBalance->execute($wallet); + $walletBalance = $this->recalculatesWalletBalance->execute($wallet); + + $this->updatesWalletBalance->execute($wallet, $walletBalance); } $token = Auth::fromUser(User::find(1)); diff --git a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php index 1140ecd9..77d8252d 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php @@ -29,6 +29,7 @@ use App\Models\Wallet; use Carbon\Carbon; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use App\Classes\Modules\Wallets\Services\RecalculatesWalletBalance; class CreateBookingPaymentLogic extends AbstractControllerLogic { @@ -70,6 +71,9 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic /** @var CreateCashBackTransactionProcessor */ private $createCashBackTransactionProcessor; + /** @var RecalculatesWalletBalance */ + private $recalculatesWalletBalance; + /** * CreateBookingPaymentLogic constructor. * @param FetchesBookingQuotation $fetchBookingQuotation @@ -81,8 +85,9 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic * @param UpdatesWalletBalance $updatesWalletBalance * @param UpdatesTransactionStatus $updatesTransactionStatus * @param CreateCashBackTransactionProcessor $createCashBackTransactionProcessor + * @param RecalculatesWalletBalance $recalculatesWalletBalance */ - public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction, CalculatesBookingOutstanding $calculatesBookingOutstanding, CreatesBillplzBill $createsBillplzBill, UpdatesWalletBalance $updatesWalletBalance, UpdatesTransactionStatus $updatesTransactionStatus, CreateCashBackTransactionProcessor $createCashBackTransactionProcessor) + public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction, CalculatesBookingOutstanding $calculatesBookingOutstanding, CreatesBillplzBill $createsBillplzBill, UpdatesWalletBalance $updatesWalletBalance, UpdatesTransactionStatus $updatesTransactionStatus, CreateCashBackTransactionProcessor $createCashBackTransactionProcessor, RecalculatesWalletBalance $recalculatesWalletBalance) { $this->fetchBookingQuotation = $fetchBookingQuotation; $this->fetchesCompanyPaymentAttemptLimit = $fetchesCompanyPaymentAttemptLimit; @@ -93,6 +98,7 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic $this->updatesWalletBalance = $updatesWalletBalance; $this->updatesTransactionStatus = $updatesTransactionStatus; $this->createCashBackTransactionProcessor = $createCashBackTransactionProcessor; + $this->recalculatesWalletBalance = $recalculatesWalletBalance; } /** @@ -138,7 +144,8 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic $paymentReference = $billNumber; - $this->updatesWalletBalance->execute($wallet, ($amount * -1)); + $walletBalance = $this->recalculatesWalletBalance->execute($wallet); + $this->updatesWalletBalance->execute($wallet, $walletBalance); } $billNumber = $this->generatesTransactionBillNumber->execute('PYMT-'); diff --git a/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php b/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php index a94fd9a8..a724cf24 100644 --- a/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php +++ b/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php @@ -10,13 +10,6 @@ use App\Models\Company; class UpdatesWalletBalance extends AbstractUpdateRecord { - private $recalculatesWalletBalance; - - public function __construct(RecalculatesWalletBalance $recalculatesWalletBalance) - { - $this->recalculatesWalletBalance = $recalculatesWalletBalance; - } - /** * @param Wallet $model * @param $amount @@ -25,9 +18,7 @@ class UpdatesWalletBalance extends AbstractUpdateRecord */ public function execute(Wallet $model, $amount) { - $auditBalance = $this->recalculatesWalletBalance->execute($model); - - $model->amount = $auditBalance; + $model->amount = $model->amount + $amount; return $this->handler($model); } From b452cf0027e30f93937ffa548a222d7e3456fb91 Mon Sep 17 00:00:00 2001 From: edmondlang Date: Thu, 18 May 2023 19:22:58 +0800 Subject: [PATCH 13/14] recalculate wallet balance --- .../ControllersLogic/CallbackBillplzLogic.php | 21 +++------------ .../Wallets/Services/UpdatesWalletBalance.php | 27 ++++++++++++++----- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php index bcaa57a7..186ef3b6 100644 --- a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php +++ b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php @@ -98,32 +98,17 @@ class CallbackBillplzLogic } if($transaction->status !== ApprovalStatus::COMPLETED && $transaction->status !== ApprovalStatus::APPROVED){ - - if($transaction->owner instanceof Wallet && $transaction->status !== ApprovalStatus::APPROVED && $status === ApprovalStatus::APPROVED) { - $this->updatesWalletBalance->execute($transaction->owner, $transaction->amount); - } $this->updatesTransactionStatus->execute($transaction, $status); + } + if($transaction->owner instanceof Wallet) { + $this->updatesWalletBalance->execute($transaction->owner, $transaction->amount); } // if ($transaction->type == TransactionType::PAYMENT) { // $cash_back_transaction = $this->createCashBackTransactionProcessor->execute($transaction); // } - - $wallet = null; - if ($transaction->owner instanceof Wallet) { - $wallet = $transaction->owner; - } else if ($transaction->owner->company->wallets()->first() instanceof Wallet) { - $wallet = $transaction->owner->company->wallets()->first(); - } - - if ($wallet != null) { - $walletBalance = $this->recalculatesWalletBalance->execute($wallet); - - $this->updatesWalletBalance->execute($wallet, $walletBalance); - } - $token = Auth::fromUser(User::find(1)); $request->headers->set('Authorization', 'Bearer '.$token); diff --git a/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php b/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php index a724cf24..5b384272 100644 --- a/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php +++ b/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php @@ -3,10 +3,10 @@ namespace App\Classes\Modules\Wallets\Services; use App\Classes\General\Eloquent\AbstractUpdateRecord; -use App\Classes\General\Eloquent\AbstractUpdateRelationshipRecord; -use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject; +use App\Classes\ValueObjects\Constants\ApprovalStatus; +use App\Classes\ValueObjects\Constants\TransactionType; use App\Models\Wallet; -use App\Models\Company; +use Illuminate\Support\Facades\Log; class UpdatesWalletBalance extends AbstractUpdateRecord { @@ -16,10 +16,25 @@ class UpdatesWalletBalance extends AbstractUpdateRecord * @return \Illuminate\Database\Eloquent\Model * @throws \App\Classes\Exceptions\MalformedRequestException */ - public function execute(Wallet $model, $amount) { + public function execute(Wallet $model, $amount) + { + $i = 0; + $topups = 0; + $credit = 0; + $payments = 0; + $debit = 0; - $model->amount = $model->amount + $amount; + foreach ($model->transactions as $transaction) { + Log::debug($transaction); + if (!in_array((int) $transaction->status, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])) continue; + if ((int) $transaction->type === TransactionType::TOP_UP) $topups += (float) $transaction->amount; + if ((int) $transaction->type === TransactionType::CREDIT_NOTE) $credit += (float) $transaction->amount; + if ((int) $transaction->type === TransactionType::PAYMENT) $payments += (float) $transaction->amount; + if ((int) $transaction->type === TransactionType::DEBIT_NOTE) $debit += (float) $transaction->amount; + } + $auditBalance = ($topups + $credit) - ($payments + $debit); + + $model->amount = $auditBalance; return $this->handler($model); - } } From 27509f59371b8c5528303dc772851e7c4359f7dd Mon Sep 17 00:00:00 2001 From: edmondlang Date: Thu, 18 May 2023 19:31:51 +0800 Subject: [PATCH 14/14] recalculate wallet balance --- app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php b/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php index 5b384272..4812660a 100644 --- a/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php +++ b/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php @@ -6,7 +6,6 @@ use App\Classes\General\Eloquent\AbstractUpdateRecord; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\ValueObjects\Constants\TransactionType; use App\Models\Wallet; -use Illuminate\Support\Facades\Log; class UpdatesWalletBalance extends AbstractUpdateRecord { @@ -25,7 +24,6 @@ class UpdatesWalletBalance extends AbstractUpdateRecord $debit = 0; foreach ($model->transactions as $transaction) { - Log::debug($transaction); if (!in_array((int) $transaction->status, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])) continue; if ((int) $transaction->type === TransactionType::TOP_UP) $topups += (float) $transaction->amount; if ((int) $transaction->type === TransactionType::CREDIT_NOTE) $credit += (float) $transaction->amount;