From 63118efc0b21c1c8e92f4ff05cb13463bc101721 Mon Sep 17 00:00:00 2001 From: Jia Sheng Date: Sun, 14 Jul 2024 11:00:33 +0800 Subject: [PATCH] delete duplicated 1688 bank account --- .../Constants/BankAccountType.php | 2 +- .../DeleteDuplicate1688BankAccount.php | 75 +++++++++++++++++++ app/Http/Resources/CompanyResource.php | 2 +- .../elements/BookingConfirmationComponent.vue | 22 ++++-- 4 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 app/Console/Commands/DeleteDuplicate1688BankAccount.php diff --git a/app/Classes/ValueObjects/Constants/BankAccountType.php b/app/Classes/ValueObjects/Constants/BankAccountType.php index f6ef447b..0393091f 100644 --- a/app/Classes/ValueObjects/Constants/BankAccountType.php +++ b/app/Classes/ValueObjects/Constants/BankAccountType.php @@ -8,7 +8,7 @@ final class BankAccountType { public const EXTERNAL = 2; - public const ALIPAY= 3; + public const ALIPAY_1688 = 3; public const ALIPAY_RECIPIENT = 4; diff --git a/app/Console/Commands/DeleteDuplicate1688BankAccount.php b/app/Console/Commands/DeleteDuplicate1688BankAccount.php new file mode 100644 index 00000000..57b65733 --- /dev/null +++ b/app/Console/Commands/DeleteDuplicate1688BankAccount.php @@ -0,0 +1,75 @@ +get(); + + $groupedBanks = $records->groupBy(function($item, $key) { + return $item['company_id'] . '-' . $item['account_no']; + }); + + foreach ($groupedBanks as $key => $banksWithSameUserAndAccountNo) { + // Sort banks by created_at or updated_at to find the latest one + $sortedBanks = $banksWithSameUserAndAccountNo->sortByDesc('created_at'); + + // Retain the latest bank + $latestBank = $sortedBanks->first(); + + // Get all IDs except the latest one + $idsToDelete = $sortedBanks->pluck('id')->slice(1); + + foreach ($idsToDelete as $id) { + Booking::where('bank_id', $id)->update([ + 'bank_id' => $latestBank->id + ]); + } + + $this->info('for company id: ' . $latestBank->company_id . ', account no: ' . $latestBank->account_no . ', duplicated id: ' . $idsToDelete); + // Delete the rest + Bank::whereIn('id', $idsToDelete)->delete(); + } + } +} diff --git a/app/Http/Resources/CompanyResource.php b/app/Http/Resources/CompanyResource.php index bbafbefc..a8103e8b 100644 --- a/app/Http/Resources/CompanyResource.php +++ b/app/Http/Resources/CompanyResource.php @@ -56,7 +56,7 @@ class CompanyResource extends JsonResource 'last_payment' => $lastPayment ? $lastPayment->created_at->diffForHumans() : 'No Payments', 'personal_banks' => BankResource::collection($this->banks->where('type', BankAccountType::PERSONAL)), 'recipient_banks' => [ - 'accounts' => BankResource::collection($this->banks->whereIn('type', [BankAccountType::EXTERNAL, BankAccountType::ALIPAY_RECIPIENT])), + 'accounts' => BankResource::collection($this->banks->whereIn('type', [BankAccountType::EXTERNAL, BankAccountType::ALIPAY_1688, BankAccountType::ALIPAY_RECIPIENT])), 'default' => new BankResource($this->banks->where('type', BankAccountType::EXTERNAL)->where('default', true)->first()) ], 'segments' => SegmentResource::collection($this->segments), diff --git a/resources/assets/vue/components/bookings/elements/BookingConfirmationComponent.vue b/resources/assets/vue/components/bookings/elements/BookingConfirmationComponent.vue index 29e664f0..3e19e8e3 100644 --- a/resources/assets/vue/components/bookings/elements/BookingConfirmationComponent.vue +++ b/resources/assets/vue/components/bookings/elements/BookingConfirmationComponent.vue @@ -100,7 +100,7 @@ -
+
@@ -144,13 +144,13 @@
-
-
+
@@ -318,6 +318,7 @@ data(){ return { createBank: false, + existingBank: false, recipientBanks: [], dropdownStatus: false, account_no: '', @@ -326,9 +327,14 @@ oneSixEightEightServiceIds: [4, 10], } }, + watch: { + 'data.serviceType.id': function() { + this.clearAccount(); + } + }, computed: { formDisabled(){ - return !!Object.keys(this.parameters.bankAccount).length; + return this.existingBank; }, isOneSizEightEightOrder() { return this.oneSixEightEightServiceIds.includes(this.data.serviceType.id); @@ -341,6 +347,10 @@ return [4]; } + if (this.isOneSizEightEightOrder) { + return [3]; + } + if (!this.isAlipayOrder && !this.isOneSizEightEightOrder) { return [1, 2]; } @@ -363,12 +373,14 @@ }, selectBank(bank){ this.account_no = bank.account_no; - this.createBank = true; + this.createBank = false; + this.existingBank = true; this.parameters.bankAccount = bank; this.dropdownStatus = false; }, clearAccount(){ this.createBank = false; + this.existingBank = false; this.account_no = ''; this.parameters.bankAccount = {}; }