157 lines
5.1 KiB
PHP
157 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Services;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
use Throwable;
|
|
|
|
class SupplierStatementReminder
|
|
{
|
|
private const SETTING_KEY = 'supplier_reconciliation.statement_reminders';
|
|
|
|
public function __construct(
|
|
private readonly SupplierStatementStore $statements,
|
|
private readonly SupplierDingTalkNotifier $notifier,
|
|
private readonly SupplierStatementReminderTargets $targets,
|
|
) {
|
|
}
|
|
|
|
public function stageFor(Carbon $date): ?string
|
|
{
|
|
$deadlineDay = (int) config('supplier_reconciliation.upload_deadline_day', 5);
|
|
$dueSoonDay = (int) config('supplier_reconciliation.reminder.due_soon_day', 3);
|
|
$overdueDay = (int) config('supplier_reconciliation.reminder.overdue_day', 6);
|
|
|
|
if ($date->day >= $overdueDay) {
|
|
return 'overdue';
|
|
}
|
|
if ($date->day === $deadlineDay) {
|
|
return 'due_today';
|
|
}
|
|
if ($date->day >= $dueSoonDay) {
|
|
return 'due_soon';
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @return array{
|
|
* stage:?string,
|
|
* accounting_month:string,
|
|
* deadline:string,
|
|
* target_count: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 {
|
|
$stage = $this->stageFor($date);
|
|
$accountingMonth = $date->copy()->startOfMonth()->subMonthNoOverflow()->format('Y-m');
|
|
$deadline = $date->copy()->startOfMonth()
|
|
->addDays((int) config('supplier_reconciliation.upload_deadline_day', 5) - 1)
|
|
->setTime(23, 59, 59);
|
|
$result = [
|
|
'stage' => $stage,
|
|
'accounting_month' => $accountingMonth,
|
|
'deadline' => $deadline->toDateTimeString(),
|
|
'target_count' => 0,
|
|
'configured' => 0,
|
|
'missing_robot' => [],
|
|
'uploaded' => [],
|
|
'already_sent' => [],
|
|
'sent' => [],
|
|
'previewed' => [],
|
|
'failed' => [],
|
|
];
|
|
if ($stage === null) {
|
|
return $result;
|
|
}
|
|
|
|
$targets = $this->targets->names();
|
|
$onlySupplier = trim($onlySupplier);
|
|
if ($onlySupplier !== '') {
|
|
$targets = array_values(array_filter(
|
|
$targets,
|
|
static fn (string $supplier): bool => $supplier === $onlySupplier,
|
|
));
|
|
}
|
|
$configured = array_fill_keys($this->notifier->configuredSuppliers(), true);
|
|
$suppliers = array_values(array_filter(
|
|
$targets,
|
|
static fn (string $supplier): bool => isset($configured[$supplier]),
|
|
));
|
|
$result['target_count'] = count($targets);
|
|
$result['configured'] = count($suppliers);
|
|
$result['missing_robot'] = array_values(array_diff($targets, $suppliers));
|
|
$state = $this->state();
|
|
|
|
foreach (array_values(array_unique($suppliers)) as $supplier) {
|
|
if ($this->statements->hasUploadInMonth($supplier, $date->format('Y-m'), $companyId)) {
|
|
$result['uploaded'][] = $supplier;
|
|
continue;
|
|
}
|
|
if (isset($state[$accountingMonth][$stage][$supplier])) {
|
|
$result['already_sent'][] = $supplier;
|
|
continue;
|
|
}
|
|
if ($dryRun) {
|
|
$result['previewed'][] = $supplier;
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
$this->notifier->sendStatementReminder(
|
|
$supplier,
|
|
$accountingMonth,
|
|
$deadline,
|
|
$date,
|
|
$stage,
|
|
$companyId,
|
|
);
|
|
$this->recordSent($accountingMonth, $stage, $supplier, $deadline);
|
|
$result['sent'][] = $supplier;
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
$result['failed'][$supplier] = $exception->getMessage();
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
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 $stage, string $supplier, Carbon $deadline): void
|
|
{
|
|
$state = $this->state();
|
|
$state[$accountingMonth][$stage][$supplier] = [
|
|
'sent_at' => now()->toDateTimeString(),
|
|
'deadline' => $deadline->toDateTimeString(),
|
|
];
|
|
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();
|
|
}
|
|
}
|