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, * uploaded:list, * already_sent:list, * sent:list, * previewed:list, * failed:array * } */ 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 */ 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(); } }