168 lines
4.8 KiB
PHP
168 lines
4.8 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\Services;
|
||
|
||
/**
|
||
* 为「补映射」查找可复用的同名账单项目规则。
|
||
*
|
||
* 账单项目在多个平台出现时,科目模板通常完全相同(例如京东和京喜的
|
||
* POP商家激励金)。这里只在规范化名称完全一致、并且所有候选规则的
|
||
* 映射字段一致时自动带入;名称相似但口径可能不同的项目不做猜测。
|
||
*/
|
||
class AccountMappingSuggestion
|
||
{
|
||
/** @var list<string> */
|
||
private const COPY_FIELDS = [
|
||
'memo',
|
||
'invoice',
|
||
'category',
|
||
'feeMemo',
|
||
'dAcct',
|
||
'dSub',
|
||
'cAcct',
|
||
'cSub',
|
||
'basis',
|
||
];
|
||
|
||
/** @var list<string> */
|
||
private const REQUIRED_FIELDS = ['category', 'dAcct', 'cAcct', 'basis'];
|
||
|
||
public function __construct(
|
||
private readonly AccountMappingStore $store,
|
||
) {
|
||
}
|
||
|
||
/**
|
||
* @return array{
|
||
* auto_fill:bool,
|
||
* reason:string,
|
||
* matched_count:int,
|
||
* sources:list<array<string,mixed>>,
|
||
* values:array<string,string>
|
||
* }
|
||
*/
|
||
public function suggest(string $platform, string $item): array
|
||
{
|
||
return $this->suggestFromRules($this->store->all(), $platform, $item);
|
||
}
|
||
|
||
/**
|
||
* @param array<int,array<string,mixed>> $rules
|
||
* @return array{
|
||
* auto_fill:bool,
|
||
* reason:string,
|
||
* matched_count:int,
|
||
* sources:list<array<string,mixed>>,
|
||
* values:array<string,string>
|
||
* }
|
||
*/
|
||
public function suggestFromRules(array $rules, string $platform, string $item): array
|
||
{
|
||
$platform = trim($platform);
|
||
$item = trim($item);
|
||
$itemKey = $this->normalizeItem($item);
|
||
|
||
if ($itemKey === '') {
|
||
return $this->emptyResult('invalid_item');
|
||
}
|
||
|
||
$matches = [];
|
||
foreach ($rules as $rule) {
|
||
if (! is_array($rule)
|
||
|| $this->normalizeItem((string) ($rule['item'] ?? '')) !== $itemKey) {
|
||
continue;
|
||
}
|
||
$matches[] = $rule;
|
||
}
|
||
|
||
if ($matches === []) {
|
||
return $this->emptyResult('no_same_item');
|
||
}
|
||
|
||
usort($matches, static fn (array $left, array $right): int => (
|
||
((string) ($left['platform'] ?? '') === $platform ? 0 : 1)
|
||
<=> ((string) ($right['platform'] ?? '') === $platform ? 0 : 1)
|
||
) ?: (((int) ($left['id'] ?? 0)) <=> ((int) ($right['id'] ?? 0))));
|
||
|
||
$templates = [];
|
||
foreach ($matches as $rule) {
|
||
$templates[$this->templateSignature($rule)] = true;
|
||
}
|
||
|
||
$sources = array_map(
|
||
static fn (array $rule): array => [
|
||
'id' => (int) ($rule['id'] ?? 0),
|
||
'platform' => trim((string) ($rule['platform'] ?? '')),
|
||
'item' => trim((string) ($rule['item'] ?? '')),
|
||
'memo' => trim((string) ($rule['memo'] ?? '')),
|
||
],
|
||
array_slice($matches, 0, 8),
|
||
);
|
||
$base = [
|
||
'matched_count' => count($matches),
|
||
'sources' => $sources,
|
||
'values' => [],
|
||
];
|
||
|
||
if (count($templates) > 1) {
|
||
return ['auto_fill' => false, 'reason' => 'ambiguous_templates'] + $base;
|
||
}
|
||
|
||
$template = $matches[0];
|
||
foreach (self::REQUIRED_FIELDS as $field) {
|
||
if (trim((string) ($template[$field] ?? '')) === '') {
|
||
return ['auto_fill' => false, 'reason' => 'incomplete_template'] + $base;
|
||
}
|
||
}
|
||
|
||
$values = [];
|
||
foreach (self::COPY_FIELDS as $field) {
|
||
$values[$field] = trim((string) ($template[$field] ?? ''));
|
||
}
|
||
|
||
return [
|
||
'auto_fill' => true,
|
||
'reason' => 'same_item',
|
||
] + array_replace($base, ['values' => $values]);
|
||
}
|
||
|
||
public function normalizeItem(string $item): string
|
||
{
|
||
$item = mb_strtolower(trim($item));
|
||
$item = preg_replace('/^wx/u', '', $item) ?? $item;
|
||
|
||
return preg_replace('/[\s\x{3000}\p{P}\p{S}]+/u', '', $item) ?? $item;
|
||
}
|
||
|
||
/** @param array<string,mixed> $rule */
|
||
private function templateSignature(array $rule): string
|
||
{
|
||
$values = [];
|
||
foreach (self::COPY_FIELDS as $field) {
|
||
$values[$field] = trim((string) ($rule[$field] ?? ''));
|
||
}
|
||
|
||
return json_encode($values, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
|
||
}
|
||
|
||
/**
|
||
* @return array{
|
||
* auto_fill:bool,
|
||
* reason:string,
|
||
* matched_count:int,
|
||
* sources:list<array<string,mixed>>,
|
||
* values:array<string,string>
|
||
* }
|
||
*/
|
||
private function emptyResult(string $reason): array
|
||
{
|
||
return [
|
||
'auto_fill' => false,
|
||
'reason' => $reason,
|
||
'matched_count' => 0,
|
||
'sources' => [],
|
||
'values' => [],
|
||
];
|
||
}
|
||
}
|