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

78 lines
2.7 KiB
PHP

<?php
namespace Modules\EcommerceFinance\Services;
/**
* 厂家对账单的存放:整份替换(以最新一版为准)+ 上传记录。
* 数据放在与「终版」目录同一张按表数据表里,数据集键带 statement: 前缀。
*/
class SupplierStatementStore
{
public const PREFIX = 'statement:';
public const LOG_PREFIX = 'statement-log:';
public function __construct(
private readonly SupplierPayableOverrideStore $store,
) {
}
/** 保存厂家对账单(整份替换)并记录一次上传。 */
public function save(string $supplier, array $records, string $filename, int $companyId, ?int $userId): int
{
$rows = [];
foreach ($records as $index => $record) {
$rows[sprintf('line:%06d', $index + 1)] = $record;
}
$this->store->replaceAll(self::PREFIX . $supplier, $rows, $companyId, $userId);
$this->store->save(self::LOG_PREFIX . $supplier, 'upload:' . now()->format('YmdHis'), [
'uploaded_at' => now()->toDateTimeString(),
'filename' => $filename,
'rows' => count($records),
], $companyId, $userId, 'import');
return count($records);
}
/** @return list<array<string,mixed>> 最新的对账单行 */
public function rows(string $supplier, int $companyId): array
{
return $this->store->listRows(self::PREFIX . $supplier, $companyId);
}
/** @return list<array<string,mixed>> 最近的上传记录(新到旧) */
public function uploads(string $supplier, int $companyId, int $limit = 10): array
{
$uploads = $this->store->listRows(self::LOG_PREFIX . $supplier, $companyId);
usort($uploads, static fn (array $a, array $b): int => strcmp((string) ($b['uploaded_at'] ?? ''), (string) ($a['uploaded_at'] ?? '')));
return array_slice($uploads, 0, $limit);
}
public function hasUploadInMonth(string $supplier, string $month, int $companyId): bool
{
$month = substr(trim($month), 0, 7);
if ($supplier === '' || preg_match('/^\d{4}-\d{2}$/', $month) !== 1) {
return false;
}
foreach ($this->store->listRows(self::LOG_PREFIX . $supplier, $companyId) as $upload) {
if (substr((string) ($upload['uploaded_at'] ?? ''), 0, 7) === $month) {
return true;
}
}
return false;
}
/** @return list<string> 已经上传过对账单的厂家名称 */
public function uploadedSuppliers(int $companyId): array
{
return array_values(array_map(
static fn (string $dataset): string => substr($dataset, strlen(self::LOG_PREFIX)),
$this->store->datasetsWithPrefix(self::LOG_PREFIX, $companyId),
));
}
}