diff --git a/app/Classes/Modules/Accounting/ControllersLogic/ApproveDuplicateBankStatementDetailsStatusLogic.php b/app/Classes/Modules/Accounting/ControllersLogic/ApproveDuplicateBankStatementDetailsStatusLogic.php index e7d1a7c9..3872039b 100644 --- a/app/Classes/Modules/Accounting/ControllersLogic/ApproveDuplicateBankStatementDetailsStatusLogic.php +++ b/app/Classes/Modules/Accounting/ControllersLogic/ApproveDuplicateBankStatementDetailsStatusLogic.php @@ -135,7 +135,7 @@ class ApproveDuplicateBankStatementDetailsStatusLogic extends AbstractController public function logic(Request $request): JsonResponse { // Determine the approval status - $status = $this->getApprovalStatus($request); + $status = $this->getConstantStatus($request->route('status')); // Find the statement transaction owner $owner = $this->getOwner($request); @@ -143,21 +143,33 @@ class ApproveDuplicateBankStatementDetailsStatusLogic extends AbstractController // Update owner status $this->updateOwnerStatus($owner, $status); - // If the status is 'approved', handle the approval process - if ($status === ApprovalStatus::APPROVED) { - $this->handleApprovedStatus($owner); - } + // handle the siblings process + $this->handleSiblingsStatus($owner, $this->getSiblingsStatus($status)); // Check and approve remaining matches if any - $this->checkAndApproveRemainingMatches($owner); + $this->checkAndApproveRemainingMatches($owner, $status); // Return an empty response return $this->response([]); } - private function getApprovalStatus(Request $request): int + private function getConstantStatus(String $statusName=null): int { - return $request->route('status') == 'approve' ? ApprovalStatus::APPROVED : ApprovalStatus::REJECTED; + switch ($statusName) { + case 'approve': + return ApprovalStatus::APPROVED; + + case 'pending_verification': + return ApprovalStatus::PENDING_VERIFICATION; + + default: + return ApprovalStatus::REJECTED; + } + } + + private function getSiblingsStatus(int $status): int + { + return $status == ApprovalStatus::APPROVED ? ApprovalStatus::REJECTED : ApprovalStatus::PENDING_VERIFICATION; } private function getOwner(Request $request): StatementTransactionOwner @@ -170,23 +182,24 @@ class ApproveDuplicateBankStatementDetailsStatusLogic extends AbstractController $this->updatesBankStatementTransactionOwnerStatus->execute($owner, $status); } - private function handleApprovedStatus(StatementTransactionOwner $owner): void + private function handleSiblingsStatus(StatementTransactionOwner $owner, int $siblingStatus): void { - // Reject all other owners with the same system, owner type, and owner ID - $this->rejectOtherOwners($owner); + // update all other owners with the same system, owner type, and owner ID + $this->updateOtherOwners($owner, $siblingStatus); // Find all siblings and process them $siblings = $this->getSiblings($owner); - $this->processSiblings($siblings); + + foreach ($siblings as $sibling) { + $this->processSibling($sibling, $siblingStatus); + } } - private function rejectOtherOwners(StatementTransactionOwner $owner): void + private function updateOtherOwners(StatementTransactionOwner $owner, int $status): void { - StatementTransactionOwner::where('system', $owner->system) - ->where('owner_type', $owner->owner_type) - ->where('owner_id', $owner->owner_id) + StatementTransactionOwner::getSiblingsOwner() ->where('id', '!=', $owner->id) - ->update(['status' => ApprovalStatus::REJECTED]); + ->update(['status' => $status]); } private function getSiblings(StatementTransactionOwner $owner): Collection @@ -196,86 +209,65 @@ class ApproveDuplicateBankStatementDetailsStatusLogic extends AbstractController ->get(); } - private function processSiblings(Collection $siblings): void - { - foreach ($siblings as $sibling) { - $this->processSibling($sibling); - } - } - - private function processSibling(StatementTransactionOwner $sibling): void + private function processSibling(StatementTransactionOwner $sibling, int $siblingStatus): void { // Reject the sibling and save the changes - $sibling->status = ApprovalStatus::REJECTED; + $sibling->status = $siblingStatus; $sibling->save(); - + // Find all twins and process them $twins = $this->getTwins($sibling); - $this->processTwins($twins); - } - private function getTwins(StatementTransactionOwner $sibling): Collection - { - return StatementTransactionOwner::where('system', $sibling->system) - ->where('owner_type', $sibling->owner_type) - ->where('owner_id', $sibling->owner_id) - ->where('id', '!=', $sibling->id) - ->get(); - } - - private function processTwins(Collection $twins): void - { foreach ($twins as $twin) { $this->processTwin($twin); } } + private function getTwins(StatementTransactionOwner $sibling): Collection + { + return StatementTransactionOwner::getSiblingsOwner() + ->where('id', '!=', $sibling->id) + ->get(); + } + private function processTwin(StatementTransactionOwner $twin): void { // Find all owners with the same statement transaction ID as the twin - $owners = $this->getOwners($twin); + $owners = $this->getSiblings($twin); // If there is only one owner (the twin itself), approve it if ($owners->count() === 1) { - $this->updateOwnerStatus($twin, ApprovalStatus::APPROVED); + $this->updateOwnerStatus($twin, $this->getConstantStatus(request()->route('status'))); } } - private function getOwners(StatementTransactionOwner $transactionOwner): Collection - { - return StatementTransactionOwner::where('statement_transaction_id', $transactionOwner->statement_transaction_id) - ->where('id', '!=', $transactionOwner->id) - ->get(); - } - - private function checkAndApproveRemainingMatches(StatementTransactionOwner $owner): void + private function checkAndApproveRemainingMatches(StatementTransactionOwner $owner, int $status): void { // Find all remaining matching owners for the related transaction - $remainingMatches = $this->getRemainingMatches($owner); + $checkStatus = ($status == ApprovalStatus::APPROVED ? ApprovalStatus::PENDING_VERIFICATION : ApprovalStatus::APPROVED); + $remainingMatches = $this->getRemainingMatches($owner, $checkStatus); // Process each remaining match foreach ($remainingMatches as $match) { - $this->processRemainingMatch($match); + $this->processRemainingMatch($match, $status); } } - private function getRemainingMatches(StatementTransactionOwner $owner): Collection + private function getRemainingMatches(StatementTransactionOwner $owner, int $checkStatus): Collection { - return StatementTransactionOwner::where('system', $owner->system) - ->where('owner_type', $owner->owner_type) - ->where('owner_id', $owner->owner_id) - ->where('status', ApprovalStatus::PENDING_VERIFICATION) + return StatementTransactionOwner::getSiblingsOwner() + ->where('status', $checkStatus) ->get(); } - private function processRemainingMatch(StatementTransactionOwner $match): void + private function processRemainingMatch(StatementTransactionOwner $match, int $status): void { // Find all owners with the same statement transaction ID as the match - $owners = $this->getOwners($match); + $owners = $this->getSiblings($match); // If there is only one owner (the match itself), approve it if ($owners->count() === 1) { - $this->updateOwnerStatus($match, ApprovalStatus::APPROVED); + $this->updateOwnerStatus($match, $status); } } diff --git a/app/Models/StatementTransactionOwner.php b/app/Models/StatementTransactionOwner.php index 52e8d1fc..5bfe6f82 100644 --- a/app/Models/StatementTransactionOwner.php +++ b/app/Models/StatementTransactionOwner.php @@ -26,4 +26,10 @@ class StatementTransactionOwner extends Model { return $this->belongsTo(StatementTransaction::class, 'statement_transaction_id', 'id'); } + + public function scopeGetSiblingsOwner($query) { + $query->where('system', $this->system) + ->where('owner_type', $this->owner_type) + ->where('owner_id', $this->owner_id); + } } diff --git a/resources/assets/vue/components/accounting/elements/StatementTransactionComponent.vue b/resources/assets/vue/components/accounting/elements/StatementTransactionComponent.vue index db2fc252..d36c149f 100644 --- a/resources/assets/vue/components/accounting/elements/StatementTransactionComponent.vue +++ b/resources/assets/vue/components/accounting/elements/StatementTransactionComponent.vue @@ -11,6 +11,36 @@
{{item.owners.pending_verification[0].reference}}
+ + +
+
+
{{item.owners.approved[0].system}}
+
{{ typeString(item.owners.approved[0].type) }}
+
+ +
+
+
+
@@ -52,7 +82,8 @@
-
+ +
{{ item.amount }}
-
{{ [6, 7, 8, 9, 10, 11, 12, 13, 14].include(item.owners.approved[0].type) ? 'Miscellaneous' : 'Approved' }}
+
{{ [6, 7, 8, 9, 10, 11, 12, 13, 14].includes(item.owners.approved[0].type) ? 'Miscellaneous' : 'Approved' }}
Pending...
-
+
diff --git a/routes/accounting.php b/routes/accounting.php index c7112947..4fe0a1ad 100644 --- a/routes/accounting.php +++ b/routes/accounting.php @@ -10,7 +10,7 @@ Route::group(['prefix' => 'accounting', 'as' => 'accounting.', 'namespace' => 'A Route::put('/details/update', 'BankStatementController@update')->name('details.update'); }); - Route::post('bankStatement/{id}/details/{status}', 'ApproveDuplicateBankStatementDetailsStatusController@update')->where('status', 'approve|reject')->name('bankStatement.details.status.update'); + Route::post('bankStatement/{id}/details/{status}', 'ApproveDuplicateBankStatementDetailsStatusController@update')->where('status', 'approve|reject|pending_verification')->name('bankStatement.details.status.update'); Route::group(['prefix' => 'statement_transaction', 'as' => 'statement_transaction.'], function () { Route::post('/owner/group-approve', 'GroupApproveStatementTransactionController@approve')->name('owner.groupApprove');