CaiWuXiTong/app/Console/Commands/SendSupplierStatementReminders.php
苏木 671b6b81a8 feat(供应商对账): 月度催办未上传对账单厂家 (#1445)
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
2026-11-26 11:44:34 +08:00

101 lines
3.8 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 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,默认今天}
{--supplier= : 仅检查指定供应商}
{--dry-run : 只预览,不发送也不记录}';
protected $description = '按月度上传截止时间提醒尚未上传对账单的供应商';
public function handle(SupplierStatementReminder $reminder): int
{
$timezone = (string) config('app.platform_timezone', 'Asia/Shanghai');
$dateValue = trim((string) $this->option('date')) ?: 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');
foreach (Company::enabled()->orderBy('id')->get() as $company) {
$company->makeCurrent();
$lock = Cache::lock(sprintf(
'supplier-statement-reminder:%d:%s',
$company->id,
$date->toDateString(),
), 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')),
);
if ($result['stage'] === null) {
$this->line("#{$company->id} {$company->name}: 今天不在催办阶段");
continue;
}
if ($result['target_count'] === 0) {
$this->warn("#{$company->id} {$company->name}: 当前催办名单未配置");
continue;
}
$this->line(sprintf(
'#%d %s %s %s: 名单 %d 家,可提醒 %d 家,%s %d 家,已上传 %d 家,已提醒 %d 家,未配置机器人 %d 家,失败 %d 家',
$company->id,
$company->name,
$result['accounting_month'],
$result['stage'],
$result['target_count'],
$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;
}
}