快递账单重复导入提示与安全重新读取 (#1546)
This commit is contained in:
parent
a7e7b067eb
commit
ffe97e458a
@ -314,7 +314,7 @@ final class CourierBillController extends Controller
|
||||
{
|
||||
$done = $this->guard(fn () => $this->store->rematch($statement));
|
||||
|
||||
return $this->back($request, [], '已按当前对账分组重新拆分,并按最新发货数据、未发出明细和结算台账重新匹配'
|
||||
return $this->back($request, [], '已按当前规则重新读取账单文件、按当前对账分组重新拆分,并按最新发货数据、未发出明细和结算台账重新匹配'
|
||||
. (count($done) > 1 ? '(同一张账单涉及 ' . count($done) . ' 张对账单,一起更新了)' : '') . '。');
|
||||
}
|
||||
|
||||
|
||||
@ -441,7 +441,7 @@ final class CourierBillStore
|
||||
}
|
||||
|
||||
/** 读文件、写账单行到主分组的对账单,再按快递名称1 拆到同一合并账单等可拆入的分组。 */
|
||||
private function ingest(int $fileId, object $group): void
|
||||
private function ingest(int $fileId, object $group, bool $reread = false): void
|
||||
{
|
||||
$db = $this->db();
|
||||
$file = $db->table('courier_bill_files')->find($fileId);
|
||||
@ -464,12 +464,16 @@ final class CourierBillStore
|
||||
$rows = 0;
|
||||
$amount = 0.0;
|
||||
$buffer = [];
|
||||
$overlap = null;
|
||||
try {
|
||||
$split = $db->transaction(function () use ($db, $path, $extension, $group, $file, $fileId, $statementId, &$sheets, &$rejected, &$rejectedCount, &$rows, &$amount, &$buffer): string {
|
||||
$split = $db->transaction(function () use ($db, $path, $extension, $group, $file, $fileId, $statementId, $reread, &$sheets, &$rejected, &$rejectedCount, &$rows, &$amount, &$buffer, &$overlap): string {
|
||||
if ($db->table('courier_bill_statements')->where('id', $statementId)->lockForUpdate()->value('status') === 'confirmed') {
|
||||
throw new RuntimeException('这家快递本月对账单已确认结算,先撤销确认再上传。');
|
||||
}
|
||||
$db->table('courier_bill_lines')->where('file_id', $fileId)->delete();
|
||||
$previousLastId = $reread ? (int) $db->table('courier_bill_lines')->where('file_id', $fileId)->max('id') : null;
|
||||
if (! $reread) {
|
||||
$db->table('courier_bill_lines')->where('file_id', $fileId)->delete();
|
||||
}
|
||||
$overrides = ['waybill_header' => $group->waybill_header, 'amount_header' => $group->amount_header, 'weight_header' => $group->weight_header];
|
||||
foreach ($this->parser->parse($path, $extension, $overrides) as $item) {
|
||||
if ($item['type'] === 'sheet') {
|
||||
@ -494,17 +498,32 @@ final class CourierBillStore
|
||||
'bill_weight' => $item['weight'], 'raw' => json_encode($item['raw'], JSON_UNESCAPED_UNICODE),
|
||||
];
|
||||
if (count($buffer) >= 500) {
|
||||
$db->table('courier_bill_lines')->insert($buffer);
|
||||
$this->insertLines($buffer, $previousLastId);
|
||||
$buffer = [];
|
||||
}
|
||||
}
|
||||
if ($buffer !== []) {
|
||||
$db->table('courier_bill_lines')->insert($buffer);
|
||||
$this->insertLines($buffer, $previousLastId);
|
||||
}
|
||||
if ($reread && ! array_filter($sheets, static fn (array $s): bool => ! isset($s['skipped']))) {
|
||||
throw new RuntimeException('没有找到含单号和运费列的工作表,已保留原账单。');
|
||||
}
|
||||
if ($reread) {
|
||||
$db->table('courier_bill_lines')->where('file_id', $fileId)->where('id', '<=', $previousLastId)->delete();
|
||||
}
|
||||
$overlap = $this->overlap($fileId, (string) $file->bill_month, $reread);
|
||||
if ($overlap !== null && $overlap[1] * 2 >= $overlap[2]) {
|
||||
$db->table('courier_bill_lines')->where('file_id', $fileId)->delete();
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
return $this->split($fileId, $group, (string) $file->bill_month, $statementId);
|
||||
return $this->split($fileId, $group, (string) $file->bill_month, $statementId, $reread);
|
||||
});
|
||||
} catch (Throwable $error) {
|
||||
if ($reread) {
|
||||
throw new RuntimeException('重新读取「' . $file->original_name . '」失败:' . $error->getMessage(), 0, $error);
|
||||
}
|
||||
report($error);
|
||||
$db->table('courier_bill_files')->where('id', $fileId)->update([
|
||||
'status' => 'failed', 'group_id' => $group->id, 'message' => mb_substr('读取失败:' . $error->getMessage(), 0, 255), 'updated_at' => now(),
|
||||
@ -513,6 +532,17 @@ final class CourierBillStore
|
||||
return;
|
||||
}
|
||||
|
||||
if ($overlap !== null && $overlap[1] * 2 >= $overlap[2]) {
|
||||
$db->table('courier_bill_files')->where('id', $fileId)->update([
|
||||
'status' => 'failed', 'group_id' => $group->id, 'statement_id' => null, 'row_count' => 0, 'amount' => 0,
|
||||
'message' => mb_substr('重复导入:和「' . $overlap[0] . '」有 ' . $overlap[1] . '/' . $overlap[2]
|
||||
. ' 个单号相同(至少一半),这份没有读入,可以删掉。要换成新版本的账单,先删掉旧的那份再上传', 0, 255),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$usable = array_filter($sheets, static fn (array $s): bool => ! isset($s['skipped']));
|
||||
$notes = [];
|
||||
if ($usable === []) {
|
||||
@ -521,6 +551,9 @@ final class CourierBillStore
|
||||
if ($split !== '') {
|
||||
$notes[] = $split;
|
||||
}
|
||||
if ($overlap !== null) {
|
||||
$notes[] = $overlap[1] . ' 个单号在「' . $overlap[0] . '」里已经有了,请在对账明细中核对重复项';
|
||||
}
|
||||
if ($rejectedCount > 0) {
|
||||
$notes[] = '有 ' . $rejectedCount . ' 行单号被拒收';
|
||||
}
|
||||
@ -569,7 +602,7 @@ final class CourierBillStore
|
||||
}
|
||||
}
|
||||
|
||||
$lines = $db->table('courier_bill_lines')->where('file_id', $fileId)->where('statement_id', $primaryStatementId)->get(['id', 'waybill_no']);
|
||||
$lines = $db->table('courier_bill_lines')->where('file_id', $fileId)->where('statement_id', $primaryStatementId)->orderBy('id')->get(['id', 'waybill_no']);
|
||||
$ship = $this->shipping->summaries($lines->pluck('waybill_no')->map(static fn ($w): string => strtoupper((string) $w))->unique()->values()->all());
|
||||
$moves = [];
|
||||
$strangers = [];
|
||||
@ -666,7 +699,91 @@ final class CourierBillStore
|
||||
}
|
||||
|
||||
/**
|
||||
* 「重新匹配」:这张对账单用到的账单文件都按当前分组重新拆分,再把涉及的对账单逐一重新匹配。
|
||||
* 按当前规则重读原始文件。人工处理结果按工作表、原始行号和单号保留,避免同单号的多行互相覆盖。
|
||||
*
|
||||
* @return list<int> 前后涉及的对账单
|
||||
*/
|
||||
public function reread(int $fileId): array
|
||||
{
|
||||
return $this->db()->transaction(fn (): array => $this->rereadFile($fileId));
|
||||
}
|
||||
|
||||
private function rereadFile(int $fileId): array
|
||||
{
|
||||
$db = $this->db();
|
||||
$file = $db->table('courier_bill_files')->lockForUpdate()->find($fileId);
|
||||
$before = $this->statementsOfFiles([$fileId]);
|
||||
if ($file === null || $file->status !== 'parsed' || $file->statement_id === null) {
|
||||
return $before;
|
||||
}
|
||||
$statements = $db->table('courier_bill_statements')->whereIn('id', $before)->orderBy('id')->lockForUpdate()->get(['status']);
|
||||
if ($statements->contains('status', 'confirmed')) {
|
||||
throw new RuntimeException('这个文件涉及的对账单已确认结算,先撤销确认再重新读取。');
|
||||
}
|
||||
$group = collect($this->groups())->firstWhere('id', (int) $file->group_id);
|
||||
if ($group === null || ! Storage::disk(self::DISK)->exists($file->stored_path)) {
|
||||
throw new RuntimeException('原始账单文件或对账分组不存在,无法重新读取,已保留原账单。');
|
||||
}
|
||||
$this->ingest($fileId, $group, true);
|
||||
|
||||
return array_values(array_unique([...$before, ...$this->statementsOfFiles([$fileId])]));
|
||||
}
|
||||
|
||||
private static function lineKey(string $sheet, int $row, string $waybill): string
|
||||
{
|
||||
return json_encode([mb_substr($sheet, 0, 64), $row, $waybill], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/** 每批至多 500 行;旧行留到解析成功后才删除,用于逐批恢复对应原始行的人工处理。 */
|
||||
private function insertLines(array $lines, ?int $previousLastId): void
|
||||
{
|
||||
$db = $this->db();
|
||||
if ($previousLastId !== null) {
|
||||
$kept = $db->table('courier_bill_lines')->where('file_id', $lines[0]['file_id'])
|
||||
->where('id', '<=', $previousLastId)->whereNotNull('resolution')
|
||||
->whereIn('waybill_no', array_unique(array_column($lines, 'waybill_no')))
|
||||
->whereIn('sheet', array_unique(array_column($lines, 'sheet')))
|
||||
->whereIn('row_no', array_unique(array_column($lines, 'row_no')))
|
||||
->get(['sheet', 'row_no', 'waybill_no', 'resolution', 'resolved_by', 'resolved_at'])
|
||||
->keyBy(static fn ($line): string => self::lineKey($line->sheet, $line->row_no, $line->waybill_no));
|
||||
foreach ($lines as &$line) {
|
||||
$old = $kept->get(self::lineKey($line['sheet'], $line['row_no'], $line['waybill_no']));
|
||||
$line += ['resolution' => $old?->resolution, 'resolved_by' => $old?->resolved_by, 'resolved_at' => $old?->resolved_at];
|
||||
}
|
||||
unset($line);
|
||||
}
|
||||
$db->table('courier_bill_lines')->insert($lines);
|
||||
}
|
||||
|
||||
/**
|
||||
* 同月重合最多的文件:[文件名, 重合的唯一单号数, 本文件唯一单号数]。
|
||||
* 数据库聚合避免将整份账单的单号和匹配行加载到 PHP;重读时保留最早上传的那份。
|
||||
*
|
||||
* @return array{0:string,1:int,2:int}|null
|
||||
*/
|
||||
private function overlap(int $fileId, string $month, bool $earlierOnly = false): ?array
|
||||
{
|
||||
$db = $this->db();
|
||||
$total = $db->table('courier_bill_lines')->where('file_id', $fileId)->distinct()->count('waybill_no');
|
||||
if ($total === 0) {
|
||||
return null;
|
||||
}
|
||||
$hit = $db->table('courier_bill_lines as other')
|
||||
->join('courier_bill_files as file', 'file.id', '=', 'other.file_id')
|
||||
->where('file.bill_month', $month)->where('file.id', $earlierOnly ? '<' : '<>', $fileId)
|
||||
->whereExists(function ($query) use ($fileId): void {
|
||||
$query->selectRaw('1')->from('courier_bill_lines as mine')->where('mine.file_id', $fileId)
|
||||
->whereColumn('mine.waybill_no', 'other.waybill_no');
|
||||
})
|
||||
->groupBy('file.id', 'file.original_name')->select('file.original_name')
|
||||
->selectRaw('COUNT(DISTINCT ' . $db->getQueryGrammar()->wrap('other.waybill_no') . ') AS hits')
|
||||
->orderByDesc('hits')->orderBy('file.id')->first();
|
||||
|
||||
return $hit === null ? null : [(string) $hit->original_name, (int) $hit->hits, $total];
|
||||
}
|
||||
|
||||
/**
|
||||
* 「重新匹配」:按当前规则重读文件、按当前分组重新拆分,再把涉及的对账单逐一重新匹配。
|
||||
*
|
||||
* @return list<int> 重新匹配了的对账单
|
||||
*/
|
||||
@ -685,8 +802,9 @@ final class CourierBillStore
|
||||
->merge($db->table('courier_bill_files')->where('statement_id', $statementId)->pluck('id'))
|
||||
->map(static fn ($id): int => (int) $id)->unique()->all();
|
||||
$statements = [$statementId];
|
||||
sort($fileIds);
|
||||
foreach ($fileIds as $fileId) {
|
||||
array_push($statements, ...$this->resplitFile($fileId));
|
||||
array_push($statements, ...$this->rereadFile($fileId));
|
||||
}
|
||||
$done = [];
|
||||
foreach (array_unique($statements) as $id) {
|
||||
|
||||
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::connection('business_mirror')->table('courier_bill_lines', function (Blueprint $table): void {
|
||||
$table->index(['file_id', 'waybill_no'], 'courier_bill_lines_file_waybill');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::connection('business_mirror')->table('courier_bill_lines', function (Blueprint $table): void {
|
||||
$table->dropIndex('courier_bill_lines_file_waybill');
|
||||
});
|
||||
}
|
||||
};
|
||||
@ -7,7 +7,7 @@
|
||||
</div>
|
||||
<label class="cbr-drop">
|
||||
<strong>选择账单文件(可多选,一次可传几十家)</strong>
|
||||
<span class="cbr-muted">支持 xlsx、xls、csv,也可以把多份账单打成一个 zip。同一家一个月分几次发的账单,分次上传会合并到同一张对账单。</span>
|
||||
<span class="cbr-muted">支持 xlsx、xls、csv,也可以把多份账单打成一个 zip。同一家一个月分几次发的账单,分次上传会合并到同一张对账单;与本月已上传的某份文件有一半及以上单号相同,会标成「重复导入」,这份不读入。传错了或要换新版本,在下面列表里删掉旧文件再传;已确认结算的须先撤销确认。</span>
|
||||
<input type="file" name="files[]" multiple required accept=".xlsx,.xls,.csv,.zip">
|
||||
</label>
|
||||
<div><button type="submit" class="cbr-primary">上传并对账</button></div>
|
||||
@ -50,6 +50,7 @@
|
||||
@elseif ($file->statement_id)
|
||||
<a data-cbr-tab href="{{ $pageUrl(['cb_view' => 'statement', 'statement' => $file->statement_id, 'month' => $month]) }}">对账单</a>
|
||||
@endif
|
||||
<form method="POST" action="{{ $action('delete-file', ['file' => $file->id]) }}" onsubmit="return confirm('删除这个文件和它读入的账单行?')" style="display:inline">@csrf<button type="submit" class="cbr-danger">删除</button></form>
|
||||
</td>
|
||||
</tr>
|
||||
@empty
|
||||
|
||||
@ -114,6 +114,32 @@ final class CourierBillPagesTest extends FeatureTestCase
|
||||
$this->assertSame('parsed', $store->db()->table('courier_bill_files')->where('id', $pending->id)->value('status'));
|
||||
}
|
||||
|
||||
public function test_upload_page_deletion_and_reread_feedback_preserve_confirmed_bills(): void
|
||||
{
|
||||
Storage::fake('local');
|
||||
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
|
||||
$this->loginAs()->withExceptionHandling();
|
||||
$store = app(CourierBillStore::class);
|
||||
$group = $store->saveGroup(null, ['name' => '测试快递', 'express_names' => ['测试快递']]);
|
||||
$file = $store->upload([UploadedFile::fake()->createWithContent("账单');alert(1);//.csv", "运单号,运费\nZT100,8\n")], '2026-10', [$group], 'tester')[0];
|
||||
$section = 'courier-payables-final';
|
||||
$page = route('business.pages.show', ['section' => $section, 'page' => 'courier-bill-reconciliation', 'cb_view' => 'upload', 'month' => '2026-10']);
|
||||
$delete = route("business.$section.courier-bills.delete-file", ['file' => $file]);
|
||||
$this->get($page)->assertOk()->assertSeeText('一半及以上单号相同')->assertSeeText('删除')
|
||||
->assertSee($delete)->assertSee("return confirm('删除这个文件和它读入的账单行?')", false)
|
||||
->assertDontSee("confirm('删除「账单", false);
|
||||
$statement = (int) $store->db()->table('courier_bill_files')->where('id', $file)->value('statement_id');
|
||||
$this->post(route("business.$section.courier-bills.rematch", ['statement' => $statement]))
|
||||
->assertRedirect()->assertSessionHas('courier_bills_message', fn ($message) => str_contains($message, '按当前规则重新读取账单文件'));
|
||||
$store->confirm($statement, 'tester');
|
||||
$this->from($page)->post($delete)->assertRedirect($page)->assertSessionHasErrors('courier_bills');
|
||||
$this->assertNotNull($store->db()->table('courier_bill_files')->find($file));
|
||||
$this->assertSame(1, $store->db()->table('courier_bill_lines')->where('file_id', $file)->count());
|
||||
$store->unconfirm($statement);
|
||||
$this->post($delete)->assertRedirect();
|
||||
$this->assertNull($store->db()->table('courier_bill_files')->find($file));
|
||||
}
|
||||
|
||||
private function bill(string $name): UploadedFile
|
||||
{
|
||||
return new UploadedFile($this->xlsx(['Sheet1' => [['运单号', '运费'], ['ZT100', 8], ['ZT999', 5]]]), $name, null, null, true);
|
||||
|
||||
@ -171,7 +171,7 @@ final class CourierBillReconciliationTest extends FeatureTestCase
|
||||
]);
|
||||
$ids = $store->upload([$file], '2026-10', [], 'tester');
|
||||
$fileRow = $store->db()->table('courier_bill_files')->find($ids[0]);
|
||||
$this->assertSame('parsed', $fileRow->status);
|
||||
$this->assertSame('parsed', $fileRow->status, (string) $fileRow->message);
|
||||
$this->assertSame($groupId, (int) $fileRow->group_id);
|
||||
$statementId = (int) $fileRow->statement_id;
|
||||
|
||||
@ -496,6 +496,105 @@ final class CourierBillReconciliationTest extends FeatureTestCase
|
||||
$this->assertSame(['ZT001', 'NEW001'], $waybills($hebei));
|
||||
}
|
||||
|
||||
public function test_duplicate_upload_threshold_counts_unique_waybills_and_is_scoped_to_month(): void
|
||||
{
|
||||
$this->seedShipping();
|
||||
$store = app(CourierBillStore::class);
|
||||
$group = $store->saveGroup(null, ['name' => '测试快递', 'express_names' => ['河北中通-江三']]);
|
||||
$upload = fn (array $rows, string $month = '2026-10') => $store->upload([
|
||||
$this->upload('账单.xlsx', [['运单号', '运费'], ...$rows]),
|
||||
], $month, [$group], 'tester')[0];
|
||||
$first = $upload([['ZT001', 10], ['ZT002', 11]]);
|
||||
$duplicate = $upload([['ZT001', 10], ['ZT001', 10], ['ZT003', 3]]);
|
||||
$file = $store->db()->table('courier_bill_files')->find($duplicate);
|
||||
$this->assertSame('failed', $file->status);
|
||||
$this->assertNull($file->statement_id);
|
||||
$this->assertStringStartsWith('重复导入:和「账单.xlsx」有 1/2 个单号相同', $file->message);
|
||||
$this->assertSame(0, (int) $file->row_count);
|
||||
$this->assertSame(0, $store->db()->table('courier_bill_lines')->where('file_id', $duplicate)->count());
|
||||
|
||||
$partial = $upload([['ZT001', 10], ['ZT006', 6], ['ZT008', 4]]);
|
||||
$this->assertSame('parsed', $store->db()->table('courier_bill_files')->where('id', $partial)->value('status'));
|
||||
$this->assertStringContainsString('1 个单号在「账单.xlsx」里已经有了', $store->db()->table('courier_bill_files')->where('id', $partial)->value('message'));
|
||||
$this->assertSame(Category::DUPLICATE, $store->db()->table('courier_bill_lines')->where('file_id', $partial)->where('waybill_no', 'ZT001')->value('category'));
|
||||
$otherMonth = $upload([['ZT001', 10], ['ZT002', 11]], '2026-11');
|
||||
$this->assertSame('parsed', $store->db()->table('courier_bill_files')->where('id', $otherMonth)->value('status'));
|
||||
$store->deleteFile($duplicate);
|
||||
$this->assertNull($store->db()->table('courier_bill_files')->find($duplicate));
|
||||
$this->assertSame(2, $store->db()->table('courier_bill_lines')->where('file_id', $first)->count());
|
||||
}
|
||||
|
||||
public function test_rematch_rereads_current_rules_and_keeps_each_original_rows_resolution(): void
|
||||
{
|
||||
$this->seedShipping();
|
||||
$store = app(CourierBillStore::class);
|
||||
$group = $store->saveGroup(null, ['name' => '测试快递', 'express_names' => ['河北中通-江三']]);
|
||||
$path = $this->workbook([
|
||||
'账单明细' => [['运单号', '运费'], ['ZT001', 10], ['ZT002', 12], ['ZT002', 5]],
|
||||
'加收&上楼费' => [['运单号', '费用金额'], ['ZT001', 7]],
|
||||
]);
|
||||
$id = $store->upload([new UploadedFile($path, '账单.xlsx', null, null, true)], '2026-10', [$group], 'tester')[0];
|
||||
$db = $store->db();
|
||||
$statement = (int) $db->table('courier_bill_files')->where('id', $id)->value('statement_id');
|
||||
$lines = $db->table('courier_bill_lines')->where('file_id', $id)->orderBy('row_no')->get();
|
||||
$matcher = app(CourierBillMatcher::class);
|
||||
$matcher->resolve($statement, [(int) $lines[1]->id], 'agree', 'tester');
|
||||
$matcher->resolve($statement, [(int) $lines[2]->id], 'exclude', 'tester');
|
||||
$resolvedAt = $db->table('courier_bill_lines')->where('id', $lines[1]->id)->value('resolved_at');
|
||||
$db->table('courier_bill_lines')->insert(['statement_id' => $statement, 'file_id' => $id, 'sheet' => '加收&上楼费',
|
||||
'row_no' => 2, 'waybill_no' => 'ZT001', 'bill_amount' => 7, 'raw' => '{}', 'resolution' => 'defer']);
|
||||
$matcher->match($statement);
|
||||
$store->rematch($statement);
|
||||
$lines = $db->table('courier_bill_lines')->where('statement_id', $statement)->orderBy('row_no')->get();
|
||||
$this->assertSame(['ZT001', 'ZT002', 'ZT002'], $lines->pluck('waybill_no')->all());
|
||||
$this->assertSame(['账单明细', '账单明细', '账单明细'], $lines->pluck('sheet')->all());
|
||||
$this->assertSame([null, 'agree', 'exclude'], $lines->pluck('resolution')->all());
|
||||
$this->assertSame('tester', $lines[1]->resolved_by);
|
||||
$this->assertSame($resolvedAt, $lines[1]->resolved_at);
|
||||
$this->assertSame(3, (int) $db->table('courier_bill_files')->where('id', $id)->value('row_count'));
|
||||
}
|
||||
|
||||
public function test_failed_reread_rolls_back_all_files_and_preserves_existing_results(): void
|
||||
{
|
||||
$store = app(CourierBillStore::class);
|
||||
$group = $store->saveGroup(null, ['name' => '测试快递', 'express_names' => ['测试快递']]);
|
||||
$ids = $store->upload([
|
||||
UploadedFile::fake()->createWithContent('first.csv', "运单号,运费\nZT001,10\n"),
|
||||
UploadedFile::fake()->createWithContent('second.csv', "运单号,运费\nZT002,12\n"),
|
||||
], '2026-10', [$group], 'tester');
|
||||
$db = $store->db();
|
||||
$statement = (int) $db->table('courier_bill_files')->where('id', $ids[0])->value('statement_id');
|
||||
$before = $db->table('courier_bill_lines')->orderBy('id')->get()->toJson();
|
||||
$filesBefore = $db->table('courier_bill_files')->orderBy('id')->get()->toJson();
|
||||
Storage::disk('local')->put($db->table('courier_bill_files')->where('id', $ids[1])->value('stored_path'), "无效表头\n无法读取\n");
|
||||
try {
|
||||
$store->rematch($statement);
|
||||
$this->fail('读取失败必须提示并回滚所有文件。');
|
||||
} catch (\RuntimeException $error) {
|
||||
$this->assertStringContainsString('重新读取「second.csv」失败', $error->getMessage());
|
||||
}
|
||||
$this->assertSame($before, $db->table('courier_bill_lines')->orderBy('id')->get()->toJson());
|
||||
$this->assertSame($filesBefore, $db->table('courier_bill_files')->orderBy('id')->get()->toJson());
|
||||
}
|
||||
|
||||
public function test_reread_keeps_the_earliest_copy_of_a_legacy_duplicate(): void
|
||||
{
|
||||
$store = app(CourierBillStore::class);
|
||||
$group = $store->saveGroup(null, ['name' => '测试快递', 'express_names' => ['测试快递']]);
|
||||
$ids = $store->upload([
|
||||
UploadedFile::fake()->createWithContent('first.csv', "运单号,运费\nZT001,10\n"),
|
||||
UploadedFile::fake()->createWithContent('second.csv', "运单号,运费\nZT002,12\n"),
|
||||
], '2026-10', [$group], 'tester');
|
||||
$db = $store->db();
|
||||
$statement = (int) $db->table('courier_bill_files')->where('id', $ids[0])->value('statement_id');
|
||||
Storage::disk('local')->put($db->table('courier_bill_files')->where('id', $ids[1])->value('stored_path'), "运单号,运费\nZT001,10\n");
|
||||
$db->table('courier_bill_lines')->where('file_id', $ids[1])->update(['waybill_no' => 'ZT001']);
|
||||
$store->rematch($statement);
|
||||
$this->assertSame('parsed', $db->table('courier_bill_files')->where('id', $ids[0])->value('status'));
|
||||
$this->assertSame('failed', $db->table('courier_bill_files')->where('id', $ids[1])->value('status'));
|
||||
$this->assertSame([$ids[0]], $db->table('courier_bill_lines')->pluck('file_id')->map(fn ($id) => (int) $id)->all());
|
||||
}
|
||||
|
||||
public function test_month_express_names_counts_shipping_rows(): void
|
||||
{
|
||||
$this->seedShipping();
|
||||
@ -533,7 +632,11 @@ final class CourierBillReconciliationTest extends FeatureTestCase
|
||||
[' 原始账单 '], [], ['运单号', '运费', '原始编码'],
|
||||
['00012345678901234567', 10.004, '1E5'], ['ZT002', 20.408, '=1+1'], ['合计', 30.412],
|
||||
], '附加费' => [['运单号', '费用金额'], ['ZT002', 99]]]);
|
||||
$files = $store->upload([new UploadedFile($path, 'first.xlsx', null, null, true), new UploadedFile($path, 'second.xlsx', null, null, true)], '2026-10', [$group], 'tester');
|
||||
$secondPath = $this->workbook([$sheetName => [
|
||||
[' 原始账单 '], [], ['运单号', '运费', '原始编码'],
|
||||
['00012345678901234568', 10.004, '1E5'], ['ZT003', 20.408, '=1+1'], ['合计', 30.412],
|
||||
], '附加费' => [['运单号', '费用金额'], ['ZT003', 99]]]);
|
||||
$files = $store->upload([new UploadedFile($path, 'first.xlsx', null, null, true), new UploadedFile($secondPath, 'second.xlsx', null, null, true)], '2026-10', [$group], 'tester');
|
||||
$id = (int) $store->db()->table('courier_bill_files')->where('id', $files[0])->value('statement_id');
|
||||
$this->assertSame(4, $store->db()->table('courier_bill_lines')->count());
|
||||
$this->assertEquals(60.824, $store->db()->table('courier_bill_summary_items')->where('statement_id', $id)->sum('bill_amount'));
|
||||
@ -546,14 +649,14 @@ final class CourierBillReconciliationTest extends FeatureTestCase
|
||||
$names = array_values(array_filter($book->getSheetNames(), fn ($name) => str_starts_with($name, '运费-')));
|
||||
$this->assertCount(2, $names);
|
||||
$this->assertNotSame($names[0], $names[1]);
|
||||
foreach ($names as $name) {
|
||||
foreach ($names as $index => $name) {
|
||||
$this->assertLessThanOrEqual(31, mb_strlen($name));
|
||||
$sheet = $book->getSheetByName($name);
|
||||
$this->assertSame(6, $sheet->getHighestDataRow());
|
||||
$this->assertSame(' 原始账单 ', (string) $sheet->getCell('A1')->getValue());
|
||||
$this->assertNull($sheet->getCell('A2')->getValue());
|
||||
$this->assertSame('运单号', (string) $sheet->getCell('A3')->getValue());
|
||||
$this->assertSame('00012345678901234567', (string) $sheet->getCell('A4')->getValue());
|
||||
$this->assertSame($index === 0 ? '00012345678901234567' : '00012345678901234568', (string) $sheet->getCell('A4')->getValue());
|
||||
$this->assertSame('1E5', (string) $sheet->getCell('C4')->getValue());
|
||||
$this->assertSame('=1+1', (string) $sheet->getCell('C5')->getValue());
|
||||
$this->assertNotSame(DataType::TYPE_FORMULA, $sheet->getCell('C5')->getDataType());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user