Files
exchange-2.0/app/Console/Commands/AuditAndUpdateWallletBalance.php
T
2023-05-17 23:49:32 +08:00

80 lines
2.6 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\SeasonalSegment;
use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Classes\Modules\Companies\Services\RemovesCompanyFromSegment;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Wallet;
class AuditAndUpdateWallletBalance extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'auditWalletBalance';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Audit and Update Wallet Balance';
/** @var RemovesCompanyFromSegment */
private $removesCompanyFromSegment;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(RemovesCompanyFromSegment $removesCompanyFromSegment)
{
parent::__construct();
$this->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);
Wallet::where('id', $wallet->id)->update(['amount' => $auditBalance]);
}
}
}