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

97 lines
3.0 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\Services\SupplierDingTalkNotifier;
use Modules\EcommerceFinance\Services\SupplierStatementReminderTargets;
use Throwable;
class SetSupplierStatementReminderTargets extends Command
{
protected $signature = 'supplier-statement:targets:set
{file=- : UTF-8 文本文件路径,每行一个供应商名称;- 表示从 stdin 读取}
{--company= : 公司 ID,默认当前公司或 1}';
protected $description = '设置月度对账催办的当前供应商名单';
public function handle(
SupplierStatementReminderTargets $targets,
SupplierDingTalkNotifier $notifier,
): int {
$companyId = (int) ($this->option('company') ?: (company_id() ?: 1));
$company = Company::query()->whereKey($companyId)->first();
if (! $company) {
$this->error("公司 #{$companyId} 不存在。");
return self::FAILURE;
}
$company->makeCurrent();
$path = trim((string) $this->argument('file')) ?: '-';
$handle = $path === '-' ? STDIN : @fopen($path, 'rb');
if (! is_resource($handle)) {
$this->error('无法读取供应商名单文件。');
return self::FAILURE;
}
$names = [];
$lineNumber = 0;
try {
while (($line = fgets($handle)) !== false) {
$lineNumber++;
if ($lineNumber === 1) {
$line = ltrim($line, "\xEF\xBB\xBF");
}
$name = trim($line);
if ($lineNumber === 1 && in_array($name, ['供应商', '供应商名称'], true)) {
continue;
}
if ($name !== '') {
$names[] = $name;
}
}
} finally {
if ($path !== '-') {
fclose($handle);
}
}
if ($lineNumber > 5000) {
$this->error('催办名单不能超过 5000 行。');
return self::FAILURE;
}
if ($names === []) {
$this->error('供应商名单为空,未修改现有名单。');
return self::FAILURE;
}
try {
$saved = $targets->replace($names);
} catch (Throwable $exception) {
$this->error($exception->getMessage());
return self::FAILURE;
}
$missingRobots = array_values(array_diff($saved, $notifier->configuredSuppliers()));
$this->info(sprintf(
'#%d %s: 已保存 %d 家当前供应商;其中 %d 家可自动催办,%d 家未配置钉钉群机器人。',
$company->id,
$company->name,
count($saved),
count($saved) - count($missingRobots),
count($missingRobots),
));
if ($missingRobots !== []) {
$this->warn('未配置机器人:' . implode('、', $missingRobots));
}
return self::SUCCESS;
}
}