mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
ff88dcecf4
-include the transfer fee calculation -script to update the bill group to include the transfer fee calculation
146 lines
5.8 KiB
PHP
146 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use App\Classes\Modules\Transactions\ControllersLogic\UpdateGroupLogic;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use App\Models\BillGroup;
|
|
use App\Models\Group;
|
|
use App\Models\Transaction;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Route;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Route as FacadesRoute;
|
|
|
|
class UpdateBillGroupAndGroupToIncludeTransferFee extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'updateBillGroupAndGroupToIncludeTransferFee';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Update bill group and group to include transfer fee calculation';
|
|
|
|
/** @var UpdateGroupLogic */
|
|
private $updateGroupLogic;
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @param UpdateGroupLogic $updateGroupLogic
|
|
*/
|
|
public function __construct(UpdateGroupLogic $updateGroupLogic)
|
|
{
|
|
parent::__construct();
|
|
$this->updateGroupLogic = $updateGroupLogic;
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
|
|
// update group to include transfer fee
|
|
$groups = Group::all();
|
|
|
|
foreach ($groups as $group) {
|
|
$originalTransferFees = (float)Transaction::where('type', TransactionType::TRANSFER_FEE)->whereIn('owner_id', $group->transactions->pluck('id'))->sum('service_charge');
|
|
$correctOriginalAmount = $group->transactions()->sum('original_amount');
|
|
$correctOriginalAmount += $originalTransferFees;
|
|
$correctAmount = $group->transactions()->sum('amount');
|
|
$transferFees = $originalTransferFees / $group->currency_rate;
|
|
$correctAmount += $transferFees;
|
|
|
|
if ($group->original_amount != $correctOriginalAmount || $group->amount != $correctAmount) {
|
|
$group->original_amount = $correctOriginalAmount;
|
|
$group->amount = $correctAmount;
|
|
$group->save();
|
|
|
|
$this->info("updated group id: {$group->id}, added transfer fee CNY {$correctOriginalAmount}");
|
|
}
|
|
}
|
|
|
|
// update group calculation to include individual group transfer fee
|
|
$groups = Group::whereHas('morphTransactions', function ($q) {
|
|
$q->where('type', TransactionType::TRANSFER_FEE);
|
|
})->get();
|
|
|
|
foreach ($groups as $group) {
|
|
$route = FacadesRoute::getRoutes()->getByName('api.transaction.group.update');
|
|
$request = Request::create(route('api.transaction.group.update', $group->id));
|
|
$uri = $route->uri;
|
|
$request->setRouteResolver(function () use ($request, $uri) {
|
|
// Associate Route to request so we can access route parameters.
|
|
return (new Route('PUT', $uri, []))->bind($request);
|
|
});
|
|
|
|
$request['rate'] = $group->currency_rate;
|
|
$request['supplier_id'] = $group->issuer;
|
|
$this->updateGroupLogic->execute($request);
|
|
|
|
$group_transfer_fee = $group->morphTransactions()->where('type', TransactionType::TRANSFER_FEE)->first();
|
|
$this->info("updated group id: {$group->id}, added transfer fee to individual white form CNY {$group_transfer_fee->original_amount}");
|
|
}
|
|
|
|
// update bill group calculation to include individual group transfer fee
|
|
$billGroups = BillGroup::all();
|
|
|
|
foreach ($billGroups as $billGroup) {
|
|
// ignore those has bill group refund
|
|
if ($billGroup->billRefunds()->count() > 0) {
|
|
continue;
|
|
}
|
|
|
|
// get the groups
|
|
$payments = $billGroup->groups;
|
|
|
|
$totalOriginalTransferFee = 0;
|
|
$totalTransferFee = 0;
|
|
// for each group
|
|
foreach ($payments as $group) {
|
|
$group_transfer_fee = $group->morphTransactions()->where('type', TransactionType::TRANSFER_FEE)->first();
|
|
|
|
if ($group_transfer_fee) {
|
|
$totalOriginalTransferFee += $group_transfer_fee->original_amount;
|
|
$totalTransferFee = $totalTransferFee + ($group_transfer_fee->original_amount / $group->currency_rate);
|
|
}
|
|
}
|
|
|
|
$totalOriginal = $billGroup->original_amount + $totalOriginalTransferFee;
|
|
$total = $billGroup->amount + $totalTransferFee;
|
|
|
|
// update bill group payment transaction amount if there is only 1 payment transaction
|
|
$payment_transactions = $billGroup->transactions()->whereIn('status', [ApprovalStatus::PENDING_SUBMISSION, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->get();
|
|
|
|
if ($payment_transactions->count() === 1) {
|
|
$payment_transaction = $payment_transactions->first();
|
|
if ($payment_transaction->amount === $billGroup->amount) {
|
|
$payment_transaction->original_amount = $total;
|
|
$payment_transaction->amount = $total;
|
|
$payment_transaction->save();
|
|
$this->info("updated bill group payment transaction id: {$payment_transaction->id}, update original amount to CNY {$totalOriginal}");
|
|
}
|
|
}
|
|
|
|
// update bill group amount and original amount
|
|
$billGroup->original_amount = $totalOriginal;
|
|
$billGroup->amount = $total;
|
|
$billGroup->save();
|
|
|
|
$this->info("updated bill group id: {$billGroup->id}, added transfer fee, final original amount is CNY {$totalOriginal}");
|
|
}
|
|
}
|
|
}
|