71 lines
3.1 KiB
PHP
71 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class VerifyFinanceLedger extends Command
|
|
{
|
|
protected $signature = 'finance-ledger:verify';
|
|
|
|
protected $description = '校验财务台账数量、金额勾稽和流水平衡(只读)';
|
|
|
|
public function handle(): int
|
|
{
|
|
$db = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror'));
|
|
$accounts = (int) $db->table('fin_accounts')->count();
|
|
$shops = (int) $db->table('fin_shops')->count();
|
|
$companies = (int) $db->table('fin_companies')->count();
|
|
$openingRows = (int) $db->table('fin_account_opening')->count();
|
|
$transactions = (int) $db->table('fin_transactions')->count();
|
|
$flows = (int) $db->table('fin_daily_flows')->count();
|
|
$maps = (int) $db->table('fin_account_map')->count();
|
|
$mapped = (int) $db->table('fin_account_map')->whereNotNull('account_short')->where('account_short', '<>', '')->count();
|
|
|
|
$accountBalance = round((float) $db->table('fin_accounts')->sum('current_balance'), 2);
|
|
$openingSum = round((float) $db->table('fin_account_opening')->sum('opening_balance'), 2);
|
|
$income = round((float) $db->table('fin_transactions')->sum('income'), 2);
|
|
$expense = round((float) $db->table('fin_transactions')->sum('expense'), 2);
|
|
$flowOpening = round((float) $db->table('fin_daily_flows')->sum('opening_balance'), 2);
|
|
$flowInflow = round((float) $db->table('fin_daily_flows')->sum('inflow'), 2);
|
|
$flowOutflow = round((float) $db->table('fin_daily_flows')->sum('outflow'), 2);
|
|
$flowClosing = round((float) $db->table('fin_daily_flows')->sum('closing_balance'), 2);
|
|
$diff = round($flowOpening + $flowInflow - $flowOutflow - $flowClosing, 2);
|
|
|
|
$this->table(['项目', '值'], [
|
|
['账户', $accounts],
|
|
['店铺', $shops],
|
|
['公司', $companies],
|
|
['期初账户', $openingRows],
|
|
['期初合计', number_format($openingSum, 2)],
|
|
['账户余额合计', number_format($accountBalance, 2)],
|
|
['动账明细', $transactions],
|
|
['收入合计', number_format($income, 2)],
|
|
['支出合计', number_format($expense, 2)],
|
|
['每日流水', $flows],
|
|
['流水期末合计', number_format($flowClosing, 2)],
|
|
['流水勾稽差异', number_format($diff, 2)],
|
|
['映射记录', $maps],
|
|
['已映射', $mapped],
|
|
]);
|
|
|
|
$errors = [];
|
|
if (abs($openingSum - $accountBalance) > 0.01) {
|
|
$errors[] = '期初合计与账户余额合计不一致。';
|
|
}
|
|
if (abs($diff) > 0.01) {
|
|
$errors[] = '每日流水期末不等于期初+入账-出账。';
|
|
}
|
|
if ($openingRows !== $flows) {
|
|
$errors[] = '期初账户数与当前流水行数不一致(期初日应一账户一行)。';
|
|
}
|
|
|
|
foreach ($errors as $error) {
|
|
$this->error($error);
|
|
}
|
|
|
|
return $errors === [] ? self::SUCCESS : self::FAILURE;
|
|
}
|
|
}
|