validate($this->xlsx([self::H, ['773444000001', '江苏', 3.2, 8, null, 1, 2.1], ['773444000002', null, null, '6.5'], ['合计', null, null, 14.5]]), 'xlsx'); $this->assertSame(['rows' => 2, 'waybills' => 2, 'amount' => 14.5], $ok); foreach ([ [[['快递单号', '运费'], ['1', 2]], '第一行必须依次是'], [[self::H, ['773444000001', '江苏', 3, null]], '第 2 行运费没填'], [[self::H, [null, '江苏', 3, 5]], '第 2 行没有单号'], [[self::H, ['773444000001', '江苏', '三公斤', 5]], '重量「三公斤」不是数字'], [[self::H, ['7.9E+13', '江苏', 3, 5]], '单号格式不对'], [[self::H], '还没有填账单明细'], ] as [$rows, $message]) { try { $t->validate($this->xlsx($rows), 'xlsx'); $this->fail('应当拒收:' . $message); } catch (RuntimeException $e) { $this->assertStringContainsString($message, $e->getMessage()); } } } public function test_dropship_factory_and_courier_upload_their_own_bills_in_the_supplier_portal(): void { Storage::fake('local'); $this->company->makeCurrent(true); $db = CourierBillMatcher::db(); $db->table('dropship_factories')->insert(['name' => '临沂卿德客', 'warehouse' => '工厂-山东卿德客代发仓', 'keywords' => '卿德客', 'created_at' => now(), 'updated_at' => now()]); $db->table('courier_bill_groups')->insert(['name' => '曹县韵达', 'express_names' => json_encode(['曹县韵达'], JSON_UNESCAPED_UNICODE), 'summary_carrier' => '曹县韵达', 'created_at' => now(), 'updated_at' => now()]); $this->assertSame(['曹县韵达'], PortalFreightBills::courierNames(['曹县韵达', '财务中心', '临沂卿德客'])); \Illuminate\Support\Facades\URL::defaults(['company_id' => $this->company->id]); $page = route('business.pages.show', ['section' => 'supplier-reconciliation', 'page' => 'supplier-data']); $upload = route('business.supplier-reconciliation.freight-bill.upload'); $file = fn (array $rows, string $name = '账单.xlsx') => new UploadedFile($this->xlsx($rows), $name, null, null, true); // 代发厂家:厂家门户里多了「上传运费账单」,传代发运费账单 $factoryUser = $this->portalUser('临沂卿德客'); $this->actingAsPortal($factoryUser)->get($page)->assertOk()->assertSeeText('上传运费账单')->assertSeeText('代发运费账单'); $this->get(route('business.supplier-reconciliation.freight-bill.template'))->assertOk(); $this->post($upload, ['kind' => 'dropship', 'month' => '2026-11', 'import' => $file([['快递单号', '运费'], ['773444000001', 8]])]) ->assertSessionHas('success'); $imports = app(PortalFreightBillImports::class); $this->assertSame(0, $db->table('dropship_bill_files')->count()); $this->assertTrue($imports->processNext()); $this->get($page)->assertOk()->assertSeeText('第一行必须依次是'); $this->post($upload, ['kind' => 'dropship', 'month' => '2026-11', 'import' => $file([self::H, ['773444000001', '江苏', 3, 8], ['773444000002', '', '', 6]])]) ->assertSessionHas('success'); $this->assertTrue($imports->processNext()); $f = $db->table('dropship_bill_files')->sole(); $this->assertSame('parsed', $f->status); $this->assertSame(2, (int) $f->waybill_count); $this->assertEquals(14, (float) $f->amount); $this->assertStringContainsString('(门户)', (string) $f->uploaded_by); // 不能替别的厂家传 $this->post($upload, ['kind' => 'dropship', 'supplier' => '别家', 'month' => '2026-11', 'import' => $file([self::H, ['1', '', '', 1]])]) ->assertForbidden(); // 快递:钉钉部门名是承运商名,只看得到上传快递账单,看不到货款对账 $courierUser = $this->portalUser('曹县韵达'); $this->actingAsPortal($courierUser)->get($page)->assertOk()->assertSeeText('快递账单')->assertSeeText('当前快递') ->assertDontSeeText('代发运费账单'); $this->post($upload, ['kind' => 'courier', 'month' => '2026-11', 'import' => $file([self::H, ['YD0001', '山东', 1.2, 4.5, '送货上门', 1, 3]])]) ->assertSessionHas('success'); $this->assertSame(0, $db->table('courier_bill_files')->count()); $this->assertTrue($imports->processNext()); $c = $db->table('courier_bill_files')->sole(); $this->assertSame('parsed', $c->status); $this->assertEquals(4.5, (float) $c->amount); $this->assertSame(1, $db->table('courier_bill_statements')->where('bill_month', '2026-11')->count()); // 快递账号传代发运费账单:不是代发厂家 $this->post($upload, ['kind' => 'dropship', 'month' => '2026-11', 'import' => $file([self::H, ['YD0002', '', '', 4]])]) ->assertSessionHasErrors('freight_bill'); $this->assertFalse($imports->processNext()); } public function test_queued_bill_is_rejected_if_binding_is_revoked_before_processing(): void { Storage::fake('local'); $this->company->makeCurrent(true); $db = CourierBillMatcher::db(); $id = $db->table('dropship_factories')->insertGetId(['name' => '测试厂家', 'warehouse' => '测试仓', 'keywords' => '测试', 'created_at' => now(), 'updated_at' => now()]); $user = $this->portalUser('测试厂家'); $imports = app(PortalFreightBillImports::class); $imports->submit(new UploadedFile($this->xlsx([self::H, ['SF12345678', '', 1, 8]]), '账单.xlsx', null, null, true), [ 'name' => '测试厂家', 'kind' => 'dropship', 'month' => '2026-11', 'target_id' => $id, 'user_id' => (int) $user->id, 'company_id' => (int) $this->company->id, 'uploader' => '测试', ]); $user->update(['enabled' => false]); $this->assertTrue($imports->processNext()); $this->assertSame(0, $db->table('dropship_bill_files')->count()); $this->assertSame('failed', $imports->recent((int) $this->company->id, '测试厂家')[0]['status']); $this->assertSame([], $imports->recent((int) $this->company->id, '别家')); $this->assertSame([], $imports->recent((int) $this->company->id + 1, '测试厂家')); } public function test_multiple_worksheets_are_not_partially_accepted(): void { $path = $this->xlsx([self::H, ['SF12345678', '', 1, 8]]); $book = \PhpOffice\PhpSpreadsheet\IOFactory::load($path); $book->createSheet()->fromArray([self::H, ['SF87654321', '', 1, 99]]); (new Xlsx($book))->save($path); $this->expectException(RuntimeException::class); $this->expectExceptionMessage('只保留模板的一张账单工作表'); app(FreightBillTemplate::class)->validate($path, 'xlsx'); } public function test_portal_filename_cannot_add_another_courier_group(): void { Storage::fake('local'); $db = CourierBillMatcher::db(); $a = $db->table('courier_bill_groups')->insertGetId(['name' => '本家快递', 'express_names' => '["本家快递"]', 'bundle' => '同一合并账单', 'waybill_header' => '旧单号列', 'amount_header' => '旧金额列', 'weight_header' => '旧重量列', 'created_at' => now(), 'updated_at' => now()]); $b = $db->table('courier_bill_groups')->insertGetId(['name' => '另一家快递', 'express_names' => '["另一家快递"]', 'bundle' => '同一合并账单', 'created_at' => now(), 'updated_at' => now()]); $db->table('jst_oms_shipping')->insert(['source_id' => 888001, 'sku_source_id' => 1, 'express_no' => 'SF12345678', 'express_name1' => '另一家快递', 'ship_time' => '2026-11-10']); $file = new UploadedFile($this->xlsx([self::H, ['SF12345678', '江苏', 1, 8]]), '本家快递与另一家快递.xlsx', null, null, true); $id = app(\App\Support\CourierBills\CourierBillStore::class)->uploadFor($file, '2026-11', $a, '测试'); $this->assertSame('parsed', $db->table('courier_bill_files')->find($id)->status); $this->assertSame(0, $db->table('courier_bill_statements')->where('group_id', $b)->count()); $this->assertNull($db->table('courier_bill_files')->find($id)->extra_group_ids); } public function test_finance_can_choose_a_courier_without_supplier_statements(): void { $db = CourierBillMatcher::db(); $db->table('courier_bill_groups')->insert(['name' => '测试财务快递', 'express_names' => '[]', 'created_at' => now(), 'updated_at' => now()]); $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); $this->loginAs()->get(route('business.pages.show', ['section' => 'supplier-reconciliation', 'page' => 'supplier-data', 'supplier' => '测试财务快递'])) ->assertOk()->assertSeeText('上传运费账单')->assertSeeText('快递账单'); } public function test_courier_department_in_dingtalk_resolves_to_a_portal_binding(): void { CourierBillMatcher::db()->table('courier_bill_groups')->insert(['name' => '曹县韵达小件', 'summary_carrier' => '曹县韵达', 'express_names' => '[]', 'created_at' => now(), 'updated_at' => now()]); $query = \Mockery::mock(\Modules\EcommerceFinance\Services\UpstreamReadOnlyQuery::class); $query->shouldReceive('select')->andReturnUsing(static fn (string $sql): array => str_contains($sql, 'hr_employee') ? [['userid' => 'ding-c', 'unionid' => 'union-c', 'active' => 1, 'dept_id_list' => '77']] : [['Id' => 77, 'parent_id' => 70, 'name' => '曹县韵达', 'depth' => 0], ['Id' => 70, 'parent_id' => 1, 'name' => '快递', 'depth' => 1]]); $factory = \Mockery::mock(\Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory::class); $factory->shouldReceive('forSource')->andReturn($query); $suppliers = \Mockery::mock(\Modules\EcommerceFinance\Repositories\SupplierManagementMirrorRepository::class); $suppliers->shouldReceive('suppliersByNames')->andReturn([]); $resolved = (new SupplierPortalAccess($factory, $suppliers))->resolveAll('ding-c', 'union-c'); $this->assertSame([['supplier_name' => '曹县韵达', 'supplier_code' => '', 'dingtalk_group_id' => 77, 'dingtalk_group_name' => '曹县韵达']], $resolved); } private function portalUser(string $name): User { $user = User::create(['name' => $name . '对账员', 'email' => Str::lower(Str::random(12)) . '@portal.test', 'password' => Str::random(32), 'locale' => 'zh-CN', 'enabled' => true, 'created_from' => SupplierPortalAccess::CREATED_FROM, 'landing_page' => 'business.supplier-portal.home', 'created_by' => 0]); $user->roles()->attach(Role::where('name', 'user')->firstOrFail()->id); $user->permissions()->attach(Permission::where('name', 'read-admin-panel')->firstOrFail()->id); $user->companies()->attach($this->company->id); DingTalkIdentity::create(['user_id' => $user->id, 'union_id' => 'union-' . $user->id, 'dingtalk_user_id' => 'ding-' . $user->id]); SupplierPortalIdentity::create(['company_id' => $this->company->id, 'user_id' => $user->id, 'supplier_code' => null, 'supplier_name' => $name, 'dingtalk_group_id' => 1, 'dingtalk_group_name' => $name, 'last_verified_at' => now()]); SupplierPortalSupplier::create(['company_id' => $this->company->id, 'user_id' => $user->id, 'supplier_code' => null, 'supplier_name' => $name, 'dingtalk_group_id' => 1, 'dingtalk_group_name' => $name, 'last_verified_at' => now()]); return $user; } private function actingAsPortal(User $user): self { $identity = DingTalkIdentity::query()->where('user_id', $user->id)->firstOrFail(); return $this->withExceptionHandling()->loginAs($user)->withSession(['dingtalk_identity_id' => $identity->id, 'dingtalk_roster_checked_at' => now()->timestamp]); } /** @param list> $rows */ private function xlsx(array $rows): string { $book = new Spreadsheet(); $sheet = $book->getActiveSheet(); foreach ($rows as $r => $row) { foreach ($row as $c => $value) { if ($value !== null && $value !== '') { $sheet->setCellValueExplicit([$c + 1, $r + 1], $value, is_string($value) ? \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING : \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC); } } } $path = tempnam(sys_get_temp_dir(), 'pfb_') . '.xlsx'; (new Xlsx($book))->save($path); return $path; } }