CaiWuXiTong/app/Console/Commands/RunPayablesUnpricedCycle.php
苏木 ae52465900 feat: 每月2日自动生成未定价转本月定价
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
2026-12-02 18:57:47 +08:00

60 lines
2.2 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 Modules\EcommerceFinance\Services\PayablesUnpricedCycleProcessor;
use Modules\EcommerceFinance\Services\SupplierPayableOverrideStore;
use Throwable;
class RunPayablesUnpricedCycle extends Command
{
protected $signature = 'payables:unpriced-cycle:auto
{--period= : 货款账期,格式 YYYY-MM,默认上月}
{--cutoff= : 工作平台价格截止日,格式 YYYY-MM-DD,默认为本月1日}
{--company=1 : 公司 ID}
{--user= : 操作用户 ID}';
protected $description = '每月2日使用工作平台价格镜像自动生成上月未定价转本月定价数据';
public function handle(
PayablesUnpricedCycleProcessor $processor,
SupplierPayableOverrideStore $store,
): int {
$timezone = (string) config('app.platform_timezone', 'Asia/Shanghai');
$now = now($timezone);
$period = trim((string) $this->option('period')) ?: $now->copy()->subMonthNoOverflow()->format('Y-m');
$cutoff = trim((string) $this->option('cutoff')) ?: $now->copy()->startOfMonth()->toDateString();
$companyId = max(1, (int) $this->option('company'));
$userId = $this->option('user') !== null ? (int) $this->option('user') : null;
try {
$completed = $store->findLatestRow(
PayablesUnpricedCycleProcessor::RUN_DATASET,
['summary_month' => $period, 'status' => 'completed'],
$companyId,
);
if ($completed !== null) {
$this->info("账期 {$period} 已完成,本次自动任务跳过。");
return self::SUCCESS;
}
$result = $processor->processFromPlatformPrices(
$period,
$cutoff,
$companyId,
$userId,
);
$this->line(json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
return self::SUCCESS;
} catch (Throwable $exception) {
report($exception);
$this->error('自动定价失败:' . $exception->getMessage());
return self::FAILURE;
}
}
}