CaiWuXiTong/modules/EcommerceFinance/Services/SupplierStatementReminder.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

180 lines
6.0 KiB
PHP

<?php
namespace Modules\EcommerceFinance\Services;
use Illuminate\Support\Carbon;
use Throwable;
class SupplierStatementReminder
{
private const SETTING_KEY = 'supplier_reconciliation.statement_reminders';
private const PAYMENT_RECONCILIATION_DATASET = 'final:supplier:payment-reconciliation';
public function __construct(
private readonly SupplierStatementStore $statements,
private readonly SupplierDingTalkNotifier $notifier,
private readonly SupplierPayableOverrideStore $payables,
) {
}
public function isReminderDay(Carbon $date): bool
{
$reminderEndDay = (int) config('supplier_reconciliation.reminder_end_day', 14);
return $date->day >= 1 && $date->day <= $reminderEndDay;
}
/**
* @return array{
* reminder_day:bool,
* accounting_month:string,
* reminder_end:string,
* final_version:string,
* source_suppliers:int,
* configured:int,
* missing_robot:list<string>,
* uploaded:list<string>,
* already_sent:list<string>,
* sent:list<string>,
* previewed:list<string>,
* failed:array<string,string>
* }
*/
public function send(
Carbon $date,
int $companyId,
bool $dryRun = false,
string $onlySupplier = '',
): array {
$accountingMonth = $date->copy()->startOfMonth()->subMonthNoOverflow()->format('Y-m');
$reminderEnd = $date->copy()->startOfMonth()
->addDays((int) config('supplier_reconciliation.reminder_end_day', 14) - 1)
->setTime(23, 59, 59);
$finalVersion = $date->copy()->startOfMonth()
->addDays((int) config('supplier_reconciliation.final_version_day', 15) - 1)
->startOfDay();
$result = [
'reminder_day' => $this->isReminderDay($date),
'accounting_month' => $accountingMonth,
'reminder_end' => $reminderEnd->toDateTimeString(),
'final_version' => $finalVersion->toDateString(),
'source_suppliers' => 0,
'configured' => 0,
'missing_robot' => [],
'uploaded' => [],
'already_sent' => [],
'sent' => [],
'previewed' => [],
'failed' => [],
];
if (! $result['reminder_day']) {
return $result;
}
$suppliers = $this->sourceSuppliersForMonth($accountingMonth, $companyId);
$onlySupplier = trim($onlySupplier);
if ($onlySupplier !== '') {
$suppliers = array_values(array_filter(
$suppliers,
static fn (string $supplier): bool => $supplier === $onlySupplier,
));
}
$configured = array_fill_keys($this->notifier->configuredSuppliers(), true);
$result['source_suppliers'] = count($suppliers);
$result['missing_robot'] = array_values(array_filter(
$suppliers,
static fn (string $supplier): bool => ! isset($configured[$supplier]),
));
$result['configured'] = count($suppliers) - count($result['missing_robot']);
$state = $this->state();
$dateKey = $date->toDateString();
foreach ($suppliers as $supplier) {
if (! isset($configured[$supplier])) {
continue;
}
if ($this->statements->hasUploadInMonth($supplier, $date->format('Y-m'), $companyId)) {
$result['uploaded'][] = $supplier;
continue;
}
if (isset($state[$accountingMonth][$dateKey][$supplier])) {
$result['already_sent'][] = $supplier;
continue;
}
if ($dryRun) {
$result['previewed'][] = $supplier;
continue;
}
try {
$this->notifier->sendStatementReminder(
$supplier,
$accountingMonth,
$finalVersion,
$date,
$companyId,
);
$this->recordSent($accountingMonth, $dateKey, $supplier, $reminderEnd, $finalVersion);
$result['sent'][] = $supplier;
} catch (Throwable $exception) {
report($exception);
$result['failed'][$supplier] = $exception->getMessage();
}
}
return $result;
}
/** @return list<string> */
private function sourceSuppliersForMonth(string $month, int $companyId): array
{
$suppliers = [];
foreach ($this->payables->listRows(self::PAYMENT_RECONCILIATION_DATASET, $companyId) as $row) {
if (substr(trim((string) ($row['summary_month'] ?? '')), 0, 7) !== $month) {
continue;
}
$supplier = trim((string) ($row['supplier'] ?? ''));
if ($supplier !== '') {
$suppliers[$supplier] = true;
}
}
$suppliers = array_keys($suppliers);
natcasesort($suppliers);
return array_values($suppliers);
}
private function state(): array
{
$state = setting(self::SETTING_KEY, []);
if (is_string($state)) {
$state = json_decode($state, true) ?: [];
}
return is_array($state) ? $state : [];
}
private function recordSent(
string $accountingMonth,
string $dateKey,
string $supplier,
Carbon $reminderEnd,
Carbon $finalVersion,
): void
{
$state = $this->state();
$state[$accountingMonth][$dateKey][$supplier] = [
'sent_at' => now()->toDateTimeString(),
'reminder_end' => $reminderEnd->toDateTimeString(),
'final_version' => $finalVersion->toDateString(),
];
uksort($state, static fn (string $left, string $right): int => strcmp($right, $left));
$state = array_slice($state, 0, 24, true);
setting()->set(self::SETTING_KEY, json_encode($state, JSON_UNESCAPED_UNICODE));
setting()->save();
}
}