快递账单上传逐文件按文件名识别分组 (#1542)

This commit is contained in:
limeng 2026-11-28 10:48:01 +08:00
parent 801264b4b3
commit 6b1ff05fee
3 changed files with 39 additions and 9 deletions

View File

@ -272,8 +272,7 @@ final class CourierBillController extends Controller
public function upload(Request $request)
{
$data = $request->validate([
'month' => ['required', 'date_format:Y-m'], 'group_id' => ['nullable', 'integer'],
'group_ids' => ['nullable', 'array', 'max:20'], 'group_ids.*' => ['integer'],
'month' => ['required', 'date_format:Y-m'],
'files' => ['required', 'array', 'max:60'], 'files.*' => ['file', 'max:51200'],
], [], ['files' => '账单文件', 'files.*' => '账单文件']);
foreach ($data['files'] as $file) {
@ -281,8 +280,7 @@ final class CourierBillController extends Controller
throw ValidationException::withMessages(['files' => '只支持 xlsx、xls、csv、zip:' . $file->getClientOriginalName()]);
}
}
$groupIds = $data['group_ids'] ?? (isset($data['group_id']) ? [$data['group_id']] : []);
$ids = $this->guard(fn () => $this->store->upload($data['files'], $data['month'], array_map('intval', $groupIds), self::user()));
$ids = $this->guard(fn () => $this->store->upload($data['files'], $data['month'], [], self::user()));
$files = $this->store->db()->table('courier_bill_files')->whereIn('id', $ids)->get();
$split = $files->filter(fn ($f) => str_contains((string) $f->message, '合并账单已拆分'))->count();

View File

@ -4,9 +4,6 @@
@csrf
<div class="cbr-form">
<label>账单月份<input type="month" name="month" value="{{ $month }}" required></label>
<label>对账分组(不选 = 按文件名自动识别;可多选,第一个为主分组)
<select name="group_ids[]" multiple size="4" style="min-width:260px;height:auto;padding:4px 8px">@foreach ($cbGroups as $group)<option value="{{ $group->id }}">{{ $group->name }}{{ $group->bundle ? '(合并账单:' . $group->bundle . ')' : '' }}</option>@endforeach</select>
</label>
</div>
<label class="cbr-drop">
<strong>选择账单文件(可多选,一次可传几十家)</strong>
@ -14,9 +11,9 @@
<input type="file" name="files[]" multiple required accept=".xlsx,.xls,.csv,.zip">
</label>
<div><button type="submit" class="cbr-primary">上传并对账</button></div>
<p class="cbr-muted">识别规则:文件名里出现分组名称、承运商、分组里的快递名称1或文件名关键字,只命中一个分组才算识别成功;识别不出的文件会列在下面让你选分组。
<p class="cbr-muted">按文件名自动识别是哪家:文件名里出现分组名称、承运商、分组里的快递名称1或文件名关键字,取最长命中的那一家;认不出的文件不会乱归,会在下面列为「待选分组」,在那一行单独选分组后读入。
表头在前 15 行里找「运单号 / 运单编号 / 快递单号」所在行;被 Excel 截断成科学计数法(如 7.91508E+13)的单号整行拒收,下面会列出来。</p>
<p class="cbr-muted"><strong>合并账单:</strong>几家快递的账单放在一张表里发来的(如河北中通-江三和霸州百世快运-雅柏),不用拆开传。在「对账分组」里给它们填同一个「合并账单」名称,上传后系统按每个单号在发货数据里的快递名称1 自动拆到各自分组;也可以在上面多选分组,或者文件名里写上几家的名字。拆不进去的单号留在主分组,照常判「匹配不到」「快递不符」;其中属于别的分组或还没建分组的快递,会在文件说明里提示出来。合并账单里新加了一家:把它加进合并账单后,在对账单上点「重新匹配」就会重新拆分,不用重传。</p>
<p class="cbr-muted"><strong>合并账单:</strong>几家快递的账单放在一张表里发来的(如河北中通-江三和霸州百世快运-雅柏),不用拆开传。在「对账分组」里给它们填同一个「合并账单」名称,上传后系统按每个单号在发货数据里的快递名称1 自动拆到各自分组;文件名里写上几家的名字也会一起拆。拆不进去的单号留在主分组,照常判「匹配不到」「快递不符」;其中属于别的分组或还没建分组的快递,会在文件说明里提示出来。合并账单里新加了一家:把它加进合并账单后,在对账单上点「重新匹配」就会重新拆分,不用重传。</p>
</form>
</section>

View File

@ -4,6 +4,7 @@ namespace Tests\Feature\Business;
use App\Models\Auth\Permission;
use App\Support\CourierBills\CourierBillMatcher;
use App\Support\CourierBills\CourierBillStore;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
@ -76,6 +77,40 @@ final class CourierBillPagesTest extends FeatureTestCase
$this->assertFalse($db->table('courier_bill_groups')->where('name', '新分组')->exists());
}
public function test_upload_identifies_each_file_by_name_and_allows_an_unrecognized_file_to_be_assigned(): void
{
Storage::fake('local');
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
$this->loginAs()->withExceptionHandling();
$store = app(CourierBillStore::class);
$first = $store->saveGroup(null, ['name' => '河北中通', 'express_names' => ['河北中通']]);
$second = $store->saveGroup(null, ['name' => '北京韵达', 'express_names' => ['北京韵达']]);
$section = 'courier-payables-final';
$uploadPage = route('business.pages.show', ['section' => $section, 'page' => 'courier-bill-reconciliation', 'cb_view' => 'upload', 'month' => '2026-10']);
$this->get($uploadPage)->assertOk()->assertDontSee('name="group_ids[]"', false);
$this->post(route("business.$section.courier-bills.upload"), [
'month' => '2026-10', 'group_id' => $first, 'group_ids' => [$first],
'files' => [
UploadedFile::fake()->createWithContent('河北中通10月.csv', "运单号,运费\nZT100,8\n"),
UploadedFile::fake()->createWithContent('北京韵达10月.csv', "运单号,运费\nYD200,9\n"),
UploadedFile::fake()->createWithContent('月结账单.csv', "运单号,运费\nYD300,10\n"),
],
])->assertRedirect($uploadPage);
$files = $store->db()->table('courier_bill_files')->get()->keyBy('original_name');
$this->assertEquals($first, $files['河北中通10月.csv']->group_id);
$this->assertEquals($second, $files['北京韵达10月.csv']->group_id);
$pending = $files['月结账单.csv'];
$this->assertSame('pending_group', $pending->status);
$this->assertNull($pending->group_id);
$this->get($uploadPage)->assertOk()->assertSeeText('待选分组')->assertSee('name="group_id"', false);
$this->from($uploadPage)->post(route("business.$section.courier-bills.assign", ['file' => $pending->id]), [
'group_id' => $second,
])->assertRedirect($uploadPage);
$this->assertEquals($second, $store->db()->table('courier_bill_files')->where('id', $pending->id)->value('group_id'));
$this->assertSame('parsed', $store->db()->table('courier_bill_files')->where('id', $pending->id)->value('status'));
}
private function bill(string $name): UploadedFile
{
return new UploadedFile($this->xlsx(['Sheet1' => [['运单号', '运费'], ['ZT100', 8], ['ZT999', 5]]]), $name, null, null, true);