CaiWuXiTong/app/Console/Commands/SendSupplierStatementReminders.php
苏木 bd2ca10afc feat: 供应商对账单每日早晚催办
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
2026-12-02 14:53:17 +08:00

124 lines
4.8 KiB
PHP
Raw Permalink 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 Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Modules\EcommerceFinance\Services\SupplierStatementReminder;
use Throwable;
class SendSupplierStatementReminders extends Command
{
protected $signature = 'supplier-statement:send-reminders
{--date= : 执行日期,格式 Y-m-d,默认今天}
{--slot= : 催办时段,10 或 16;默认按当前小时判断}
{--supplier= : 仅检查指定供应商}
{--dry-run : 只预览,不发送也不记录}';
protected $description = '在每月 1-14 日 10:00、16:00 提醒上月有入库数据但尚未上传对账单的供应商';
public function handle(SupplierStatementReminder $reminder): int
{
$timezone = (string) config('app.platform_timezone', 'Asia/Shanghai');
$dateOption = trim((string) $this->option('date'));
$dateValue = $dateOption !== '' ? $dateOption : now($timezone)->toDateString();
try {
$date = Carbon::createFromFormat('!Y-m-d', $dateValue, $timezone)->startOfDay();
} catch (Throwable) {
$this->error('执行日期格式无效。');
return self::FAILURE;
}
if ($date->format('Y-m-d') !== $dateValue) {
$this->error('执行日期格式无效。');
return self::FAILURE;
}
$failed = false;
$dryRun = (bool) $this->option('dry-run');
$slot = trim((string) $this->option('slot'));
if ($slot === '') {
$slot = now($timezone)->hour < 16 ? '10' : '16';
}
if (! in_array($slot, ['10', '16'], true)) {
$this->error('催办时段只能是 10 或 16。');
return self::FAILURE;
}
$startsAt = Carbon::parse(
(string) config('supplier_reconciliation.reminder.starts_at', '2026-12-03 00:00:00'),
$timezone,
)->startOfDay();
if ($dateOption === '' && $date->lt($startsAt)) {
$this->line('对账单双向催办尚未到启用日期,将从 ' . $startsAt->toDateString() . ' 开始。');
return self::SUCCESS;
}
foreach (Company::enabled()->orderBy('id')->get() as $company) {
$company->makeCurrent();
$lock = Cache::lock(sprintf(
'supplier-statement-reminder:%d:%s:%s',
$company->id,
$date->toDateString(),
$slot,
), 900);
if (! $dryRun && ! $lock->get()) {
$this->warn("#{$company->id} {$company->name}: 已有催办任务运行");
continue;
}
try {
$result = $reminder->send(
$date,
(int) $company->id,
$dryRun,
trim((string) $this->option('supplier')),
$slot,
);
if (! $result['reminder_day']) {
$this->line("#{$company->id} {$company->name}: 今天不在 1-14 日催办窗口");
continue;
}
if ($result['source_suppliers'] === 0) {
$this->warn("#{$company->id} {$company->name}: 上月对账入库数据尚未导入或没有入库供应商");
continue;
}
$this->line(sprintf(
'#%d %s %s %s:00: 入库供应商 %d 家,可提醒 %d 家,%s %d 家,已上传 %d 家,本时段已提醒 %d 家,未配置机器人 %d 家,失败 %d 家',
$company->id,
$company->name,
$result['accounting_month'],
$slot,
$result['source_suppliers'],
$result['configured'],
$dryRun ? '将提醒' : '已发送',
count($dryRun ? $result['previewed'] : $result['sent']),
count($result['uploaded']),
count($result['already_sent']),
count($result['missing_robot']),
count($result['failed']),
));
foreach ($result['failed'] as $supplier => $message) {
$this->error("{$supplier}: {$message}");
$failed = true;
}
} catch (Throwable $exception) {
report($exception);
$this->error("#{$company->id} {$company->name}: 催办任务失败:" . $exception->getMessage());
$failed = true;
} finally {
if (! $dryRun) {
$lock->release();
}
}
}
return $failed ? self::FAILURE : self::SUCCESS;
}
}