CaiWuXiTong/app/Console/Commands/PlantingReimbursementAutoConfirm.php

73 lines
3.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 App\Models\Common\Company;
use Illuminate\Console\Command;
use Modules\EcommerceFinance\Repositories\PlantingReimbursementRepository as Repository;
use Modules\EcommerceFinance\Services\PlantingReimbursementBatches as Batches;
use Modules\EcommerceFinance\Services\PlantingReimbursementGate as Gate;
use Modules\EcommerceFinance\Services\PlantingReimbursementWorkbench as Workbench;
use Throwable;
/**
* 每天 10:30 锁定当天种菜报销批次(确认本批):放行检查通过就把待处理 + 处理中的校验结果固定下来,
* 之后在聚工单整批锁单、取消、标记打款都按这份来。没通过(比如同步不新鲜)时 10:40、10:50 再试;
* 已确认的跳过。不依赖钉钉群机器人,不改聚工单。11:20 的汇总推送仍会兜底确认一次。
*/
class PlantingReimbursementAutoConfirm extends Command
{
protected $signature = 'planting-reimbursement:auto-confirm
{--date= : 报销日期 Y-m-d,默认今天}
{--dry-run : 只检查,不确认}';
protected $description = '放行检查通过后自动确认(锁定)当天种菜报销批次';
public function handle(Workbench $workbench, Batches $batches, Repository $repository): int
{
$date = trim((string) $this->option('date')) ?: Workbench::defaultDate();
try {
Workbench::parseDate($date);
} catch (Throwable $exception) {
$this->error($exception->getMessage());
return self::FAILURE;
}
$failed = false;
foreach (Company::enabled()->orderBy('id')->get() as $company) {
$company->makeCurrent();
$companyId = (int) $company->id;
if ($batches->get($companyId, $date) !== null) {
$this->line("#{$companyId}: {$date} 已确认,跳过");
continue;
}
if (! in_array($repository->syncStatus()['code'], ['ready', 'failed'], true)) {
$this->line("#{$companyId}: 聚工单镜像未就绪,跳过");
continue;
}
try {
$result = $workbench->build($companyId, $date);
$gate = Gate::check($result);
if (! $gate['ok']) {
$this->warn("#{$companyId}: {$date} 放行检查没通过,暂不确认:" . implode(';', $gate['reasons']));
$failed = true;
continue;
}
if ($this->option('dry-run')) {
$this->line("#{$companyId}: {$date} 可以确认:" . $result['summary']['source']['count'] . ' 单');
continue;
}
$batch = $batches->confirm($companyId, $date, $result, ['paid_from' => Workbench::defaultPaidFrom($date), 'scope' => Workbench::DEFAULT_SCOPE], null, 'auto');
$this->info("#{$companyId}: 已确认 {$date} 批次 " . $batch['summary']['source']['count'] . ' 单');
} catch (Throwable $exception) {
report($exception);
$this->error("#{$companyId}: 确认失败:" . $exception->getMessage());
$failed = true;
}
}
return $failed ? self::FAILURE : self::SUCCESS;
}
}