60 lines
2.2 KiB
PHP
60 lines
2.2 KiB
PHP
<?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;
|
||
}
|
||
}
|
||
}
|