CaiWuXiTong/app/Console/Commands/StoreVipshopJxOpeningBalance.php

78 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Console\Commands;
use Carbon\CarbonImmutable;
use Illuminate\Console\Command;
use Illuminate\Database\DatabaseManager;
use Modules\EcommerceFinance\Repositories\VipshopJxOpeningBalanceRepository;
class StoreVipshopJxOpeningBalance extends Command
{
protected $signature = 'vipshop-jx:store-opening-balance
{period-end : 余额截止日期,格式 YYYY-MM-DD}
{balance : 余额金额}
{--source=唯品会结算平台历史账单累计 : 历史账单净额来源说明}
{--dry-run : 只校验,不写数据库}';
protected $description = '登记唯品会JX账户历史账单净额,作为银行卡结算扣减前的计算基准';
public function handle(DatabaseManager $databases): int
{
$periodEnd = trim((string) $this->argument('period-end'));
$date = CarbonImmutable::createFromFormat('!Y-m-d', $periodEnd);
if ($date === false || $date->format('Y-m-d') !== $periodEnd || $date->format('Y-m-t') !== $periodEnd) {
$this->error('余额截止日期必须是有效月末,格式 YYYY-MM-DD。');
return self::FAILURE;
}
$rawBalance = str_replace([',', ',', '¥', '¥', ' '], '', (string) $this->argument('balance'));
if ($rawBalance === '' || ! is_numeric($rawBalance)) {
$this->error('余额金额无效。');
return self::FAILURE;
}
$connection = $databases->connection(
(string) config('upstream_mirrors.connection', 'business_mirror'),
);
if (! $connection->getSchemaBuilder()->hasTable(VipshopJxOpeningBalanceRepository::TABLE)) {
$this->error('唯品会JX期初余额表尚未完成迁移。');
return self::FAILURE;
}
$balance = number_format((float) $rawBalance, 6, '.', '');
$source = mb_substr(trim((string) $this->option('source')), 0, 255);
if ($source === '') {
$this->error('余额来源说明不能为空。');
return self::FAILURE;
}
if (! $this->option('dry-run')) {
$now = now();
$connection->table(VipshopJxOpeningBalanceRepository::TABLE)->updateOrInsert(
['period_end' => $periodEnd],
[
'balance' => $balance,
'source' => $source,
'imported_at' => $now,
'created_at' => $now,
'updated_at' => $now,
],
);
}
$this->table(['截止日期', '状态', '历史账单净额', '来源'], [[
$periodEnd,
$this->option('dry-run') ? 'validated' : 'succeeded',
$balance,
$source,
]]);
return self::SUCCESS;
}
}