72 lines
2.9 KiB
PHP
72 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Services;
|
|
|
|
use Illuminate\Contracts\Filesystem\Factory;
|
|
use InvalidArgumentException;
|
|
use RuntimeException;
|
|
|
|
/** 财务确认的逐单金额修正,保存在持久卷,不改写只读上游及同步镜像。 */
|
|
class PlantingReimbursementCorrections
|
|
{
|
|
public const DIRECTORY = 'planting-reimbursement-corrections';
|
|
|
|
public function __construct(private readonly Factory $filesystems) {}
|
|
|
|
public static function path(int $companyId, string $month): string
|
|
{
|
|
if ($companyId < 1 || preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $month) !== 1) {
|
|
throw new InvalidArgumentException('报销修正的公司或月份无效。');
|
|
}
|
|
|
|
return self::DIRECTORY . '/' . $companyId . '/' . $month . '.json';
|
|
}
|
|
|
|
/**
|
|
* 在镜像缓存读取后应用,确保同步与缓存刷新不会覆盖财务确认的金额。
|
|
* 文档 rows 保留工单、完整线上单号及两项十进制金额,其他审计信息不参与计算。
|
|
*
|
|
* @param list<array<string,mixed>> $rows
|
|
* @return list<array<string,mixed>>
|
|
*/
|
|
public function apply(array $rows, int $companyId, string $month): array
|
|
{
|
|
$disk = $this->filesystems->disk('local');
|
|
$path = self::path($companyId, $month);
|
|
if (! $disk->exists($path)) {
|
|
return $rows;
|
|
}
|
|
|
|
$document = json_decode($disk->get($path), true, 64, JSON_THROW_ON_ERROR);
|
|
if (! is_array($document) || ! isset($document['rows']) || ! is_array($document['rows'])) {
|
|
throw new RuntimeException('报销金额修正文档无效。');
|
|
}
|
|
$corrections = [];
|
|
foreach ($document['rows'] as $correction) {
|
|
$workOrder = (string) ($correction['work_order_no'] ?? '');
|
|
$order = (string) ($correction['online_order_no'] ?? '');
|
|
if ($workOrder === '' || $order === '' || isset($corrections[$workOrder])) {
|
|
throw new RuntimeException('报销金额修正缺少单号或存在重复工单。');
|
|
}
|
|
foreach (['task_payment', 'reimbursement_amount'] as $field) {
|
|
if (! is_string($correction[$field] ?? null) || preg_match('/^-?\d+\.\d{2}$/', $correction[$field]) !== 1) {
|
|
throw new RuntimeException('报销修正金额必须为精确到分的十进制字符串。');
|
|
}
|
|
}
|
|
$corrections[$workOrder] = $correction;
|
|
}
|
|
|
|
foreach ($rows as &$row) {
|
|
$correction = $corrections[(string) ($row['work_order_no'] ?? '')] ?? null;
|
|
if ($correction === null || (string) ($row['online_order_no'] ?? '') !== $correction['online_order_no']) {
|
|
continue;
|
|
}
|
|
$row['task_payment'] = $correction['task_payment'];
|
|
$row['reimbursement_amount'] = $correction['reimbursement_amount'];
|
|
}
|
|
unset($row);
|
|
|
|
return $rows;
|
|
}
|
|
}
|