CaiWuXiTong/app/Console/Commands/SyncSupplierPayableForecastClose.php
苏木 bda9f5868e feat(供应商货款预估): 增加日结与月初差异复核 (#1556)
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
2026-11-30 11:39:09 +08:00

65 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 Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Modules\EcommerceFinance\Services\SupplierPayableForecastSyncService;
class SyncSupplierPayableForecastClose extends Command
{
protected $signature = 'supplier-payable-forecast:sync
{--date= : 日结业务日期 YYYY-MM-DD,默认昨天}
{--month= : 月结重跑月份 YYYY-MM,默认上月}
{--monthly : 月初重跑上月;可代替 --month}';
protected $description = '日结供应商货款预估前一天数据;月初重跑上月并记录差异';
public function handle(SupplierPayableForecastSyncService $service): int
{
$timezone = 'Asia/Shanghai';
$dateOption = trim((string) $this->option('date'));
$monthOption = trim((string) $this->option('month'));
$monthly = (bool) $this->option('monthly');
if ($dateOption !== '' && ($monthOption !== '' || $monthly)) {
$this->error('--date 与 --month/--monthly 不能同时使用。');
return self::FAILURE;
}
try {
if ($monthOption !== '' || $monthly) {
$previousMonth = now($timezone)->subMonthNoOverflow()->startOfMonth();
if ($monthOption !== '' && preg_match('/^\d{4}-\d{2}$/D', $monthOption) !== 1) {
throw new \InvalidArgumentException('月份格式必须为 YYYY-MM。');
}
$month = $monthOption === ''
? $previousMonth->copy()
: Carbon::createFromFormat('!Y-m', $monthOption, $timezone);
if ($month->gt($previousMonth)) {
throw new \InvalidArgumentException('只能重跑已完成月份。');
}
$result = $service->reconcileMonth($month);
} else {
$date = $dateOption === ''
? now($timezone)->subDay()->startOfDay()
: Carbon::createFromFormat('!Y-m-d', $dateOption, $timezone);
if ($date->format('Y-m-d') !== ($dateOption === '' ? $date->format('Y-m-d') : $dateOption)) {
throw new \InvalidArgumentException('日期格式必须为 YYYY-MM-DD。');
}
if ($date->gte(now($timezone)->startOfDay())) {
throw new \InvalidArgumentException('日结日期必须早于今天。');
}
$result = $service->closeDay($date);
}
} catch (\Throwable $exception) {
$this->error($exception->getMessage());
return self::FAILURE;
}
$this->line(json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_PRESERVE_ZERO_FRACTION));
return self::SUCCESS;
}
}