CaiWuXiTong/app/Console/Commands/PushSupplierZeroDifferenceConfirmations.php
苏木 d7b664ad53 fix(供应商对账): 批量推送命令切换到指定公司上下文 (#1710)
批量推送命令先激活指定公司上下文,确保读取网页中已配置的钉钉机器人和对账设置。
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
2026-12-03 18:20:14 +08:00

111 lines
4.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\SupplierStatementStore;
use Modules\EcommerceFinance\Services\SupplierZeroDifferenceConfirmation;
class PushSupplierZeroDifferenceConfirmations extends Command
{
protected $signature = 'supplier-reconciliation:push-zero-difference-confirmations
{--month= : 账期 YYYY-MM,默认上月}
{--company=1 : 公司 ID}
{--supplier=* : 只处理指定厂家,可重复传入}
{--dry-run : 只检查,不发送}
{--force : 已有待确认或已确认记录时仍重新推送}';
protected $description = '扫描厂家上传的账单,双方完全一致时自动推送货款确认';
public function handle(
SupplierStatementStore $statements,
SupplierZeroDifferenceConfirmation $zeroDifferenceConfirmations,
): int {
$companyId = max(1, (int) $this->option('company'));
$month = trim((string) $this->option('month'));
if ($month === '') {
$month = now('Asia/Shanghai')->subMonthNoOverflow()->format('Y-m');
}
if (preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $month) !== 1) {
$this->error('账期格式必须为 YYYY-MM。');
return self::FAILURE;
}
Company::findOrFail($companyId)->makeCurrent();
$suppliers = array_values(array_filter(array_map(
static fn (mixed $supplier): string => trim((string) $supplier),
(array) $this->option('supplier'),
)));
if ($suppliers === []) {
$suppliers = $statements->uploadedSuppliers($companyId);
}
$suppliers = array_values(array_unique($suppliers));
sort($suppliers, SORT_NATURAL | SORT_FLAG_CASE);
if ($suppliers === []) {
$this->info('没有找到已上传对账单的厂家。');
return self::SUCCESS;
}
$dryRun = (bool) $this->option('dry-run');
$force = (bool) $this->option('force');
$rows = [];
$pushed = 0;
$failed = 0;
$skipped = 0;
foreach ($suppliers as $supplier) {
if ($dryRun) {
$assessment = $zeroDifferenceConfirmations->assess($supplier, $companyId, $month);
$status = $assessment['eligible'] ? '可推送' : '不推送';
$reason = $assessment['reason'];
$qty = $assessment['qty'];
$amount = $assessment['amount'];
} else {
$result = $zeroDifferenceConfirmations->pushMonth(
$supplier,
$companyId,
$month,
null,
! $force,
);
$status = match ($result['status']) {
'pushed' => '已推送',
'failed' => '失败',
default => '跳过',
};
$reason = $result['reason'];
$qty = $result['qty'];
$amount = $result['amount'];
if ($result['status'] === 'pushed') {
$pushed++;
} elseif ($result['status'] === 'failed') {
$failed++;
} else {
$skipped++;
}
}
$rows[] = [
$supplier,
$status,
rtrim(rtrim(number_format($qty, 2, '.', ''), '0'), '.'),
rtrim(rtrim(number_format($amount, 2, '.', ''), '0'), '.'),
$reason,
];
}
$this->table(['厂家', '状态', '件数', '货款金额', '说明'], $rows);
if ($dryRun) {
$this->info("{$month} 检查完成,共 " . count($suppliers) . ' 家;未发送任何消息。');
} else {
$this->info("{$month} 处理完成:已推送 {$pushed} 家,跳过 {$skipped} 家,失败 {$failed} 家。");
}
return $failed > 0 ? self::FAILURE : self::SUCCESS;
}
}