CaiWuXiTong/app/Console/Commands/SendSupplierStatementReminders.php
苏木 30e6e6f995 fix(供应商对账): 按月度入库数据每日催办
上传入口不设截止时间;系统仅在每月 1-14 日 10:00 提醒未上传厂家,15 日财务出上月终版。
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
2026-11-26 13:26:16 +08:00

100 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 = '在每月 1-14 日提醒上月有入库数据但尚未上传对账单的供应商';
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['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: 入库供应商 %d 家,可提醒 %d 家,%s %d 家,已上传 %d 家,今日已提醒 %d 家,未配置机器人 %d 家,失败 %d 家',
$company->id,
$company->name,
$result['accounting_month'],
$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;
}
}