CaiWuXiTong/app/Support/DropshipFreight/DropshipConfirmation.php

176 lines
8.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Support\DropshipFreight;
use Modules\EcommerceFinance\Services\SupplierPayableOverrideStore;
use RuntimeException;
/**
* 代发运费对账的「核对完成 → 推送厂家确认 → 厂家确认 / 逐单异议」状态。
*
* 和供应商对账一样存在通用数据表 supplier_payable_overrides 里,不加新表:
* - 数据集 dropship-freight-confirm,记录键「月份|厂家ID」:这家这个月的状态、推送版本、推送时的金额快照;
* - 数据集 dropship-freight-dispute:月份|厂家ID,记录键是快递单号:厂家对这一单的异议原因(带推送版本)。
* 每次推送版本号加一,旧链接随之作废;历史异议保留,按版本区分。
*/
final class DropshipConfirmation
{
public const DATASET = 'dropship-freight-confirm';
public const DISPUTES = 'dropship-freight-dispute:';
public const OPEN = 'open';
public const CHECKED = 'checked';
public const SENDING = 'sending';
public const PUSHED = 'pushed';
public const FAILED = 'failed';
public const CONFIRMED = 'confirmed';
public const DISPUTED = 'disputed';
public const LABELS = [
self::OPEN => '未核完', self::CHECKED => '已核完,待推送', self::SENDING => '推送中', self::PUSHED => '已推送,待厂家确认',
self::FAILED => '推送失败', self::CONFIRMED => '厂家已确认', self::DISPUTED => '厂家有异议',
];
public function __construct(private readonly SupplierPayableOverrideStore $store) {}
public static function key(string $month, int $factoryId): string
{
return $month . '|' . $factoryId;
}
public static function label(?array $state): string
{
return self::LABELS[(string) ($state['status'] ?? self::OPEN)] ?? self::LABELS[self::OPEN];
}
public function state(string $month, int $factoryId, int $companyId): ?array
{
return $this->store->findRow(self::DATASET, self::key($month, $factoryId), $companyId);
}
/** @return array<int,array<string,mixed>> 厂家ID => 状态 */
public function states(string $month, int $companyId): array
{
$out = [];
foreach ($this->store->listRows(self::DATASET, $companyId) as $row) {
if (($row['month'] ?? '') === $month) {
$out[(int) ($row['factory_id'] ?? 0)] = $row;
}
}
return $out;
}
/** 财务点「核对完成」/「撤销核对完成」。厂家已确认后不能再撤销,要改就重新推送。 */
public function check(string $month, object $factory, array $summary, bool $checked, int $companyId, ?int $userId, string $by): array
{
$state = $this->state($month, (int) $factory->id, $companyId) ?? [];
$status = (string) ($state['status'] ?? self::OPEN);
if (! $checked && in_array($status, [self::PUSHED, self::CONFIRMED, self::DISPUTED, self::SENDING], true)) {
throw new RuntimeException('已经推送给厂家,不能撤销;有改动请改完后重新推送。');
}
$values = array_merge($this->base($state, $month, $factory), [
'status' => $checked ? ($status === self::OPEN || $status === self::FAILED ? self::CHECKED : $status) : self::OPEN,
'checked_at' => $checked ? now()->toDateTimeString() : null,
'checked_by' => $checked ? $by : null,
'bill' => $summary['bill'], 'settle' => $summary['settle'], 'diff' => $summary['diff'],
]);
$this->store->save(self::DATASET, self::key($month, (int) $factory->id), $values, $companyId, $userId, 'dropship_check');
return $values;
}
/** 开始一次推送:版本号加一并记下这次的金额快照,返回新版本号。 */
public function startPush(string $month, object $factory, array $summary, int $attention, int $companyId, ?int $userId, string $by): int
{
$state = $this->state($month, (int) $factory->id, $companyId) ?? [];
if (($state['status'] ?? self::OPEN) === self::OPEN) {
throw new RuntimeException('先点「核对完成」再推送给厂家。');
}
if (($state['status'] ?? '') === self::SENDING) {
throw new RuntimeException('正在推送,请稍后刷新查看结果。');
}
$version = (int) ($state['version'] ?? 0) + 1;
$values = array_merge($this->base($state, $month, $factory), [
'status' => self::SENDING, 'version' => $version, 'pushed_by' => $by, 'push_requested_at' => now()->toDateTimeString(),
'bill' => $summary['bill'], 'settle' => $summary['settle'], 'diff' => $summary['diff'], 'billed' => $summary['billed'],
'attention' => $attention, 'confirmed_at' => null, 'disputed_count' => 0, 'push_error' => null,
]);
$this->store->save(self::DATASET, self::key($month, (int) $factory->id), $values, $companyId, $userId, 'dropship_push');
return $version;
}
public function markPushed(string $month, int $factoryId, int $companyId): void
{
$this->merge($month, $factoryId, $companyId, ['status' => self::PUSHED, 'pushed_at' => now()->toDateTimeString()], 'dropship_pushed');
}
public function markFailed(string $month, int $factoryId, int $companyId, string $message): void
{
$this->merge($month, $factoryId, $companyId, ['status' => self::FAILED, 'push_error' => mb_strimwidth($message, 0, 300, '…', 'UTF-8')], 'dropship_push_failed');
}
/**
* 厂家在确认页提交:没有异议 = 确认;有异议的单逐单记原因。同一版本只能提交一次。
*
* @param array<string,string> $disputes 快递单号 => 原因
* @return array<string,mixed> 提交后的状态
*/
public function submit(string $month, int $factoryId, int $version, array $disputes, int $companyId): array
{
$state = $this->state($month, $factoryId, $companyId);
if ($state === null || (int) ($state['version'] ?? 0) !== $version) {
throw new RuntimeException('这个链接已经不是最新一次推送,请以群里最新的推送为准。');
}
if (in_array($state['status'] ?? '', [self::CONFIRMED, self::DISPUTED], true)) {
throw new RuntimeException('本次对账已经提交,请查看已提交的结果。');
}
if (($state['status'] ?? '') !== self::PUSHED) {
throw new RuntimeException('本次对账尚未推送成功,请等待财务重新推送。');
}
$now = now()->toDateTimeString();
$this->store->transaction(function () use ($month, $factoryId, $version, $disputes, $companyId, $now): void {
foreach ($disputes as $waybill => $reason) {
$this->store->save(self::DISPUTES . self::key($month, $factoryId), $version . '|' . $waybill,
['waybill' => (string) $waybill, 'reason' => $reason, 'version' => $version, 'submitted_at' => $now],
$companyId, null, 'manufacturer_link');
}
$this->merge($month, $factoryId, $companyId, [
'status' => $disputes === [] ? self::CONFIRMED : self::DISPUTED,
'confirmed_at' => $now, 'disputed_count' => count($disputes),
], 'manufacturer_link');
});
return $this->state($month, $factoryId, $companyId) ?? [];
}
/** @return list<array<string,mixed>> 这家这个月的异议,最新版本在前 */
public function disputes(string $month, int $factoryId, int $companyId, ?int $version = null): array
{
$rows = $this->store->listRows(self::DISPUTES . self::key($month, $factoryId), $companyId);
if ($version !== null) {
$rows = array_values(array_filter($rows, static fn (array $r): bool => (int) ($r['version'] ?? 0) === $version));
}
usort($rows, static fn (array $a, array $b): int => [(int) ($b['version'] ?? 0), (string) ($a['waybill'] ?? '')]
<=> [(int) ($a['version'] ?? 0), (string) ($b['waybill'] ?? '')]);
return $rows;
}
private function base(array $state, string $month, object $factory): array
{
$keep = array_diff_key($state, array_flip(['_action_key']));
return array_merge($keep, ['factory_id' => (int) $factory->id, 'factory' => (string) $factory->name,
'supplier' => (string) $factory->name, 'month' => $month]);
}
private function merge(string $month, int $factoryId, int $companyId, array $values, string $source): void
{
$state = $this->state($month, $factoryId, $companyId) ?? [];
unset($state['_action_key']);
$this->store->save(self::DATASET, self::key($month, $factoryId), array_merge($state, $values), $companyId, null, $source);
}
}