enhance the code in ApproveDuplicateBankStatementDetailsStatusLogic class to dynamic for update status to approve and revert back pending

This commit is contained in:
Steve Ng
2023-10-03 15:20:11 +08:00
parent 5f7df8ab36
commit 9938615fe4
5 changed files with 96 additions and 64 deletions
@@ -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);
}
}
+6
View File
@@ -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);
}
}
@@ -11,6 +11,36 @@
<div class="col"><a :href="item.owners.pending_verification[0].reference_link" target="_blank">{{item.owners.pending_verification[0].reference}}</a></div>
</div>
</div>
<!-- Pending Export tab -->
<div class="col-5" v-if="item.owners.approved.length === 1">
<div class="row">
<div class="col">{{item.owners.approved[0].system}}</div>
<div class="col">{{ typeString(item.owners.approved[0].type) }}</div>
<div class="col">
<div class="col d-flex justify-content-between">
<a :href="item.owners.approved[0].reference_link" target="_blank">{{item.owners.approved[0].reference}}</a>
<div v-if="stage === 4">
<button class="btn btn-xs btn-outline-danger b-rad-none m-r-5 requestModal" data-type="approveCorrectMappingTransaction">
<i class="fa fa-times fa-fw"></i>
</button>
<modal-component class="animate__animated animate__fast animate__fadeIn" type="approveCorrectMappingTransaction">
<general-confirmation-form-component
:contentText="returnTextRevert(item.owners.approved[0].reference)"
modalType="confirm"
class="text-center"
:apiRoute="route('api.accounting.bankStatement.details.status.update', item.owners.approved[0].id, 'pending_verification')"
apiMethod="post"
:section="section"
>
</general-confirmation-form-component>
</modal-component>
</div>
</div>
</div>
</div>
</div>
<div class="col-5" v-if="item.owners.pending_verification.length > 1">
<div class="row">
<div class="col">
@@ -52,7 +82,8 @@
</div>
</div>
</div>
<div class="col-5" v-if="!item.owners.pending_verification.length">
<div class="col-5" v-if="!item.owners.pending_verification.length && !item.owners.approved.length">
<div class="row">
<div class="col">
<button class="btn btn-xs btn-outline-success b-rad-none m-r-5 requestModal" data-type="updateOwner">
@@ -81,7 +112,7 @@
</div>
<div class="col-1">{{ item.amount }}</div>
<div class="col-1 text-success" v-if="item.owners.approved.length">{{ [6, 7, 8, 9, 10, 11, 12, 13, 14].include(item.owners.approved[0].type) ? 'Miscellaneous' : 'Approved' }}</div>
<div class="col-1 text-success" v-if="item.owners.approved.length">{{ [6, 7, 8, 9, 10, 11, 12, 13, 14].includes(item.owners.approved[0].type) ? 'Miscellaneous' : 'Approved' }}</div>
<div class="col-1 text-danger" v-if="!item.owners.approved.length">Pending...</div>
<div class="col-1" v-if="stage === 1">
<button class="btn btn-xs btn-outline-danger b-rad-none m-r-5 requestModal" data-type="deleteMappingTransaction">
@@ -154,6 +185,9 @@
},
returnTextWithVariable(variable) {
return "Are you sure you want to choose this mapping " + variable + "?";
},
returnTextRevert(variable) {
return "Are you sure you want to revert this mapping " + variable + "?";
}
},
mixins: [componentHandler]
@@ -55,7 +55,7 @@
<div class="col"></div>
<div class="col-auto pointer bold text-danger" @click="step=0">X</div>
</div>
<div class="row parentContainer" v-if="step > 0 && stage !== 4">
<div class="row parentContainer" v-if="step > 0">
<div class="col">
<div class="row">
<div class="col">
+1 -1
View File
@@ -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');