119 lines
5.9 KiB
PHP
119 lines
5.9 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\Services;
|
||
|
||
use Illuminate\Support\Facades\Storage;
|
||
use RuntimeException;
|
||
|
||
/**
|
||
* 确认本批:把某个报销日的校验结果固定下来(每天 11:20 自动确认,也可在工作台手动确认)。
|
||
*
|
||
* 确认之后,工作台、导出、发通知、提交钉钉流程都读这份结果,不再按聚工单当前状态重新取单——
|
||
* 否则在聚工单里锁单 / 标记打款后,这批单会从「待处理」里消失。
|
||
* 存为 storage/app/planting-reimbursement-references/<公司>/batches/<报销日>.json;钉钉流程提交后不能再撤销确认。
|
||
*/
|
||
class PlantingReimbursementBatches
|
||
{
|
||
/** @return array<string,mixed>|null 已确认的批次结果(结构同 PlantingReimbursementWorkbench::build(),另带 batch) */
|
||
public function get(int $companyId, string $date): ?array
|
||
{
|
||
$disk = Storage::disk('local');
|
||
$path = $this->path($companyId, $date);
|
||
$batch = $disk->exists($path) ? json_decode((string) $disk->get($path), true) : null;
|
||
|
||
return is_array($batch) && isset($batch['summary'], $batch['rows']) ? $batch : null;
|
||
}
|
||
|
||
/**
|
||
* @param array<string,mixed> $result 刚生成、放行检查已通过的批次结果
|
||
* @return array<string,mixed>
|
||
*/
|
||
public function confirm(int $companyId, string $date, array $result, array $filters, ?int $userId, string $by = 'manual'): array
|
||
{
|
||
if ($this->get($companyId, $date) !== null) {
|
||
throw new RuntimeException($date . ' 这一批已经确认过了。');
|
||
}
|
||
$gate = PlantingReimbursementGate::check($result);
|
||
if (! $gate['ok']) {
|
||
throw new RuntimeException('放行检查没通过,不能确认:' . implode(';', $gate['reasons']) . '。');
|
||
}
|
||
$batch = $result + ['batch' => [
|
||
'confirmed_at' => now()->format('Y-m-d H:i:s'),
|
||
'confirmed_by' => $userId,
|
||
'confirmed_via' => $by,
|
||
'paid_from' => $filters['paid_from'] ?? null,
|
||
'scope' => $filters['scope'] ?? null,
|
||
]];
|
||
unset($batch['uploads']);
|
||
if (! Storage::disk('local')->put($this->path($companyId, $date), json_encode($batch, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR))) {
|
||
throw new RuntimeException('批次保存失败。');
|
||
}
|
||
|
||
return $batch;
|
||
}
|
||
|
||
/** 撤销确认(重新取单)。钉钉流程已提交的批次不能撤销。 */
|
||
public function discard(int $companyId, string $date, PlantingDingTalkSubmission $dingTalk): void
|
||
{
|
||
$batch = $this->get($companyId, $date);
|
||
if ($batch === null) {
|
||
throw new RuntimeException('这一天还没有确认批次。');
|
||
}
|
||
$record = $dingTalk->record($companyId, (string) $batch['summary']['bill_date']);
|
||
if ($record !== null) {
|
||
throw new RuntimeException('这一批已经提交过钉钉流程(' . ($record['status'] === 'submitted' ? '审批编号 ' . $record['instance_id'] : '结果待核查') . '),不能撤销确认。');
|
||
}
|
||
Storage::disk('local')->delete($this->path($companyId, $date));
|
||
}
|
||
|
||
/**
|
||
* 聚工单操作清单(按手工报销顺序,2026-12-01 财务确认):
|
||
* 1. 锁单:本批全部工单「待处理 → 处理中」;
|
||
* 2. 取消:处理中里判为取消的;
|
||
* 3. 标记打款:可报销的,出纳打款后标记,工单变为已完成。
|
||
* 待复查的单锁上后留在处理中,下次批次(默认取待处理 + 处理中)会再带进来。
|
||
*
|
||
* @param array<string,mixed> $batch
|
||
* @param array<string,string> $statuses 工单号 → 当前状态编码
|
||
* @return array<string,array<string,mixed>>
|
||
*/
|
||
public static function operations(array $batch, array $statuses): array
|
||
{
|
||
$groups = [
|
||
'lock' => ['label' => '① 锁单(标处理中)', 'action' => '更多操作 → 标处理中', 'decision' => null, 'done' => ['CONFIRMING', 'CONFIRMED', 'DISABLED']],
|
||
'cancel' => ['label' => '② 取消工单', 'action' => '取消工单', 'decision' => PlantingReimbursementAuditor::DECISION_CANCEL, 'done' => ['DISABLED']],
|
||
'paid' => ['label' => '③ 标记打款(变为已完成)', 'action' => '标记打款', 'decision' => PlantingReimbursementAuditor::DECISION_PASS, 'done' => ['CONFIRMED']],
|
||
];
|
||
$result = [];
|
||
foreach ($groups as $key => $group) {
|
||
$items = [];
|
||
foreach ($batch['rows'] as $row) {
|
||
if ($group['decision'] !== null && $row['decision'] !== $group['decision']) {
|
||
continue;
|
||
}
|
||
$status = $statuses[(string) $row['work_order_no']] ?? '';
|
||
$items[] = [
|
||
'work_order_no' => (string) $row['work_order_no'],
|
||
'online_order_no' => (string) $row['online_order_no'],
|
||
'status' => \Modules\EcommerceFinance\Repositories\PlantingReimbursementRepository::STATUS_LABELS[$status] ?? ($status === '' ? '未同步' : $status),
|
||
'done' => in_array($status, $group['done'], true),
|
||
];
|
||
}
|
||
$done = count(array_filter($items, static fn (array $item): bool => $item['done']));
|
||
$result[$key] = $group + [
|
||
'items' => $items,
|
||
'done_count' => $done,
|
||
'pending' => array_values(array_filter($items, static fn (array $item): bool => ! $item['done'])),
|
||
'online_orders' => implode(',', array_values(array_unique(array_column(array_filter($items, static fn (array $item): bool => ! $item['done']), 'online_order_no')))),
|
||
];
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
|
||
private function path(int $companyId, string $date): string
|
||
{
|
||
return PlantingReimbursementReferences::DIRECTORY . '/' . $companyId . '/batches/' . preg_replace('/[^0-9-]/', '', $date) . '.json';
|
||
}
|
||
}
|