382 lines
26 KiB
PHP
382 lines
26 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Business;
|
||
|
||
use App\Models\Auth\Permission;
|
||
use App\Support\CourierBills\BillWorkbookReader;
|
||
use App\Support\CourierBills\Category;
|
||
use App\Support\CourierBills\CourierBillMatcher;
|
||
use App\Support\CourierBills\ShippingLookup;
|
||
use App\Support\DropshipFreight\DropshipFreightStore;
|
||
use App\Support\DropshipFreight\DropshipExporter;
|
||
use App\Support\DropshipFreight\DropshipReconciler;
|
||
use App\Support\DropshipFreight\FactoryBillParser;
|
||
use Illuminate\Http\UploadedFile;
|
||
use Illuminate\Support\Facades\Storage;
|
||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
||
use Tests\Feature\FeatureTestCase;
|
||
|
||
final class DropshipFreightTest extends FeatureTestCase
|
||
{
|
||
private const WAREHOUSE = '工厂-山东卿德客代发仓';
|
||
|
||
public function test_factory_bill_parser_reads_freight_sheets_only(): void
|
||
{
|
||
$path = $this->xlsx([
|
||
'运费+货款' => [
|
||
['卿德客10月对账'],
|
||
['快递单号', '内部订单号', '快递公司', '金额', '快递', '偏远'],
|
||
['773444000001', '1', '卿德客申通', 15.1, 4, null],
|
||
['773444000001', '1', '卿德客申通', 11.0, 4, null], // 一单多件:运费相加
|
||
['773444000002', '2', '卿德客申通', 20.0, 5.5, 3], // 偏远列加进运费
|
||
['JDVB0001', '3', '京喜京东快递', 30.0, null, null], // 没运费:不算厂家开了运费
|
||
['合计', null, null, 76.1, 13.5, 3],
|
||
],
|
||
'对账' => [['快递单号', '运费', '结算运费'], ['773444000001', 8, 8]],
|
||
'记账' => [['物流单号', '记账金额'], ['773444000009', 30]],
|
||
'送货' => [['快递单号', '金额'], ['773444000003', 11]],
|
||
]);
|
||
$result = (new FactoryBillParser(new BillWorkbookReader()))->parse($path, 'xlsx');
|
||
|
||
$this->assertSame(['773444000001', '773444000002'], array_map('strval', array_keys($result['lines'])));
|
||
$this->assertEquals(8.0, $result['lines']['773444000001']['amount']);
|
||
$this->assertEquals(8.5, $result['lines']['773444000002']['amount']);
|
||
$read = array_values(array_filter($result['sheets'], static fn (array $s): bool => $s['read']));
|
||
$this->assertCount(1, $read);
|
||
$this->assertStringContainsString('运费列「快递」(加上「偏远」)', $read[0]['note']);
|
||
$skipped = collect($result['sheets'])->where('read', false)->pluck('note', 'sheet');
|
||
$this->assertStringContainsString('对账', $skipped['对账']);
|
||
$this->assertStringContainsString('记账', $skipped['记账']);
|
||
$this->assertStringContainsString('没有同时找到', $skipped['送货']);
|
||
}
|
||
|
||
public function test_generate_upload_reconcile_and_pages(): void
|
||
{
|
||
Storage::fake('local');
|
||
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
|
||
$this->loginAs()->withExceptionHandling();
|
||
$db = CourierBillMatcher::db();
|
||
// 自有快递:快递账单对账分组里的快递名称1
|
||
$db->table('courier_bill_groups')->insert(['name' => '曹县韵达', 'express_names' => json_encode(['曹县韵达'], JSON_UNESCAPED_UNICODE),
|
||
'created_at' => now(), 'updated_at' => now()]);
|
||
$this->ship('773444000001', '卿德客申通', self::WAREHOUSE, '2026-10-03 09:00:00', 1, 8); // 一致
|
||
$this->ship('773444000002', '卿德客申通', self::WAREHOUSE, '2026-10-04 09:00:00', 2, 5); // 厂家多开
|
||
$this->ship('773444000003', '卿德客极兔', self::WAREHOUSE, '2026-10-05 09:00:00', 3, 6); // 厂家少开
|
||
$this->ship('773444000004', '卿德客申通', self::WAREHOUSE, '2026-10-06 09:00:00', 4, 7); // 厂家没开:不付
|
||
$this->ship('YD0001', '曹县韵达', self::WAREHOUSE, '2026-10-06 09:00:00', 5, 9); // 自有快递
|
||
$this->ship('773444000006', '卿德客申通', '简尚-新外贸仓主仓', '2026-10-06 09:00:00', 6, 9); // 别的仓
|
||
$this->ship('773444000007', '卿德客申通', self::WAREHOUSE, '2026-10-07 09:00:00', 7, null); // 未核算
|
||
$this->ship('773444000008', '卿德客申通', self::WAREHOUSE, '2026-09-30 09:00:00', 8, 4); // 前月补提供
|
||
$this->ship('773444000010', '卿德客申通', self::WAREHOUSE, '2026-11-02 09:00:00', 12, 4); // 跨月订单
|
||
$this->ship('JDVB0001', '京喜京东快递', self::WAREHOUSE, '2026-10-08 09:00:00', 9, 3);
|
||
$this->ship('JDVB0002', '京喜京东快递', '工厂-河北丽彪代发仓', '2026-10-08 09:00:00', 10, 3);
|
||
$this->ship('JDVB0003', '京喜京东快递', '简尚-新外贸仓主仓', '2026-10-08 09:00:00', 11, 3);
|
||
$this->ship('773444000020', '卿德客申通', self::WAREHOUSE, '2026-10-09 09:00:00', 13, 6); // 换单后的新单号:代发单
|
||
$this->ship('YD0020', '曹县韵达', self::WAREHOUSE, '2026-10-09 09:00:00', 14, 9); // 换单后的新单号:自有快递
|
||
$this->shipping()->table('jst_oms_shipping')->where('express_no', '773444000020')->update(['front_no' => 'OLD0000001']);
|
||
$this->shipping()->table('jst_oms_shipping')->where('express_no', 'YD0020')->update(['front_no' => 'OLD0000002']);
|
||
$this->shipping()->table('jst_oms_shipping')->where('source_id', '<=', 8)->update(['supplier_name' => '临沂卿德客']);
|
||
|
||
$section = 'courier-payables-final';
|
||
$page = fn (array $q = []) => route('business.pages.show', ['section' => $section, 'page' => 'dropship-freight-reconciliation', ...$q]);
|
||
$this->get(route('business.pages.show', ['section' => $section]))->assertOk()->assertSeeText('代发运费对账');
|
||
foreach (['overview', 'upload', 'factories'] as $view) {
|
||
$this->get($page(['df_view' => $view, 'month' => '2026-10']))->assertOk()->assertSee('data-cbr-root', false);
|
||
}
|
||
|
||
$this->post(route("business.$section.dropship-freight.generate"), ['month' => '2026-10'])->assertRedirect()->assertSessionHas('dropship_message');
|
||
$factory = $db->table('dropship_factories')->sole();
|
||
$this->assertSame('临沂卿德客', $factory->name);
|
||
$this->assertSame(self::WAREHOUSE, $factory->warehouse);
|
||
$this->assertSame('卿德客', $factory->keywords);
|
||
|
||
$bill = new UploadedFile($this->xlsx(['运费+货款' => [
|
||
['快递单号', '快递公司', '运费'],
|
||
['773444000001', '卿德客申通', 8], ['773444000002', '卿德客申通', 6], ['773444000003', '卿德客极兔', 5],
|
||
['YD0001', '曹县韵达', 9], ['773444000006', '卿德客申通', 9], ['773444000007', '卿德客申通', 4],
|
||
['773444000008', '卿德客申通', 4], ['999999999999', '卿德客申通', 3], ['773444000010', '卿德客申通', 4],
|
||
['OLD0000001', '卿德客申通', 6], ['OLD0000002', '卿德客申通', 5],
|
||
]]), '临沂卿德客10月.xlsx', null, null, true);
|
||
$this->post(route("business.$section.dropship-freight.upload"), ['month' => '2026-10', 'files' => [$bill]])
|
||
->assertRedirect()->assertSessionHas('dropship_message');
|
||
$file = $db->table('dropship_bill_files')->sole();
|
||
$this->assertSame('parsed', $file->status);
|
||
$this->assertSame((int) $factory->id, (int) $file->factory_id);
|
||
$this->assertSame(11, (int) $file->waybill_count);
|
||
|
||
$f = collect(app(DropshipFreightStore::class)->factories())->sole();
|
||
$result = app(DropshipReconciler::class)->factoryMonth($f, '2026-10');
|
||
$status = collect($result['rows'])->pluck('status', 'waybill')->all();
|
||
$expected = [
|
||
'773444000001' => Category::MATCH, '773444000002' => Category::DIFF, '773444000003' => Category::DIFF,
|
||
'773444000004' => Category::UNPROVIDED, 'YD0001' => Category::MISMATCH, '773444000006' => Category::MISMATCH,
|
||
'773444000007' => Category::UNCALCULATED, '773444000008' => Category::PRIOR, '773444000010' => Category::LATER,
|
||
'999999999999' => Category::UNMATCHED, 'OLD0000001' => Category::MATCH, 'OLD0000002' => Category::MISMATCH,
|
||
];
|
||
ksort($status);
|
||
ksort($expected);
|
||
$this->assertSame($expected, $status);
|
||
$s = $result['summary'];
|
||
$this->assertSame(11, $s['billed']);
|
||
$this->assertEquals(63, $s['bill']);
|
||
// 按我方核算:一致、运费有差、前月补提供、换单实际发出的代发单;跨月订单留到发货月
|
||
$this->assertEquals(8 + 5 + 6 + 4 + 6, $s['settle']);
|
||
$this->assertEquals(63 - 29, $s['diff']);
|
||
$swap = collect($result['rows'])->firstWhere('waybill', 'OLD0000001');
|
||
$this->assertStringContainsString('换单:实际发出 773444000020(卿德客申通)', $swap['note']);
|
||
$this->assertStringContainsString('自有快递「曹县韵达」', collect($result['rows'])->firstWhere('waybill', 'OLD0000002')['note']);
|
||
$this->assertArrayNotHasKey('773444000020', $status); // 新单号不再算「厂家未提供」
|
||
$this->assertSame([1, 1], [$s['over'], $s['under']]);
|
||
$prior = collect($result['rows'])->firstWhere('waybill', '773444000008');
|
||
$this->assertSame('前月补提供-9月', $prior['label']);
|
||
$own = collect($result['rows'])->firstWhere('waybill', 'YD0001');
|
||
$this->assertStringContainsString('自有快递', (string) $own['note']);
|
||
$this->assertNull($own['settle']);
|
||
|
||
$this->get($page(['df_view' => 'overview', 'month' => '2026-10']))->assertOk()->assertSeeText('临沂卿德客')->assertSeeText('29.00');
|
||
$this->get($page(['df_view' => 'factory', 'factory' => $factory->id, 'month' => '2026-10']))->assertOk()
|
||
->assertSeeText('一致但运费有差')->assertSeeText('773444000004')->assertSeeText('厂家未提供(不付)')->assertSeeText('匹配不到')
|
||
->assertSeeText('自有快递「曹县韵达」');
|
||
$this->get($page(['df_view' => 'factory', 'factory' => $factory->id, 'month' => '2026-10', 'status' => 'attention']))->assertOk()
|
||
->assertDontSeeText('773444000004');
|
||
$this->get(route("business.$section.dropship-freight.export", ['month' => '2026-10']))->assertOk();
|
||
$before = $db->table('dropship_bill_lines')->orderBy('id')->get()->toJson();
|
||
$filesBefore = $db->table('dropship_bill_files')->orderBy('id')->get()->toJson();
|
||
$export = $this->get(route("business.$section.dropship-freight.export", ['month' => '2026-10', 'factory' => $factory->id]))->assertOk();
|
||
$out = $export->baseResponse->getFile()->getPathname();
|
||
$book = \PhpOffice\PhpSpreadsheet\IOFactory::load($out);
|
||
$names = $book->getSheetNames();
|
||
$this->assertSame('运费+货款', $names[0]);
|
||
foreach (['汇总', '对账', '前月补提供-9月', '跨月订单-11月', '快递不符', '匹配不到', '厂家未提供', '说明'] as $sheet) {
|
||
$this->assertContains($sheet, $names);
|
||
}
|
||
$recon = $book->getSheetByName('对账')->toArray(null, false, false);
|
||
$this->assertSame(['快递单号', '快递公司', '运费', '称重重量', '首重', '面单', '快递单号'], array_slice($recon[0], 0, 7));
|
||
$row = collect($recon)->first(fn ($r) => $r[0] === '773444000002');
|
||
$this->assertSame('卿德客申通', $row[1]);
|
||
$this->assertEquals(6, $row[2]);
|
||
$this->assertSame('一致但运费有差', $row[count($row) - 2]);
|
||
$swapRow = collect($recon)->first(fn ($r) => $r[0] === 'OLD0000001');
|
||
$this->assertSame('773444000020', $swapRow[6]);
|
||
$summary = $book->getSheetByName('汇总')->toArray(null, false, false);
|
||
$this->assertSame('10月运费', $summary[1][0]);
|
||
$total = collect($summary)->first(fn ($r) => $r[0] === '合计');
|
||
$this->assertEquals(63, $total[1]);
|
||
$this->assertEquals(29, $total[2]);
|
||
$this->assertSame([3, 1], [count($book->getSheetByName('快递不符')->toArray()) - 1, count($book->getSheetByName('匹配不到')->toArray()) - 1]);
|
||
$book->disconnectWorksheets();
|
||
@unlink($out);
|
||
// 早期账单没有 raw 列也能直接导出;只读原文件,不重读入库,不改金额和文件状态。
|
||
$this->assertSame($before, $db->table('dropship_bill_lines')->orderBy('id')->get()->toJson());
|
||
$this->assertSame($filesBefore, $db->table('dropship_bill_files')->orderBy('id')->get()->toJson());
|
||
|
||
// 同一份账单再传:重复导入,不读入
|
||
$again = new UploadedFile($this->xlsx(['账单' => [['快递单号', '运费'], ['773444000001', 8], ['773444000002', 6]]]), '卿德客10月补.xlsx', null, null, true);
|
||
$this->post(route("business.$section.dropship-freight.upload"), ['month' => '2026-10', 'files' => [$again]])->assertRedirect();
|
||
$this->assertSame('failed', $db->table('dropship_bill_files')->orderByDesc('id')->value('status'));
|
||
// 下个月的账单又开了 10 月已开过的单
|
||
$next = new UploadedFile($this->xlsx(['账单' => [['快递单号', '运费'], ['773444000001', 8]]]), '卿德客11月.xlsx', null, null, true);
|
||
$this->post(route("business.$section.dropship-freight.upload"), ['month' => '2026-11', 'files' => [$next]])->assertRedirect();
|
||
$nov = app(DropshipReconciler::class)->factoryMonth($f, '2026-11');
|
||
$this->assertSame(Category::DUPLICATE, collect($nov['rows'])->firstWhere('waybill', '773444000001')['status']);
|
||
|
||
// 换单跨月:10 月账单开的是旧单号 OLD0000001(已换成 773444000020),11 月账单又开新单号 → 同一包裹,算重复
|
||
$swapNov = new UploadedFile($this->xlsx(['账单' => [['快递单号', '运费'], ['773444000020', 6]]]), '卿德客11月补.xlsx', null, null, true);
|
||
$this->post(route("business.$section.dropship-freight.upload"), ['month' => '2026-11', 'files' => [$swapNov]])->assertRedirect();
|
||
$nov = app(DropshipReconciler::class)->factoryMonth($f, '2026-11');
|
||
$row = collect($nov['rows'])->firstWhere('waybill', '773444000020');
|
||
$this->assertSame(Category::DUPLICATE, $row['status']);
|
||
$this->assertStringContainsString('OLD0000001', (string) $row['note']);
|
||
$this->assertNull($row['settle']);
|
||
|
||
// 认不出厂家的文件:待选厂家,选后读入
|
||
$unknown = new UploadedFile($this->xlsx(['账单' => [['快递单号', '运费'], ['773444000004', 7]]]), '某厂家12月.xlsx', null, null, true);
|
||
$this->post(route("business.$section.dropship-freight.upload"), ['month' => '2026-12', 'files' => [$unknown]])->assertRedirect();
|
||
$pending = $db->table('dropship_bill_files')->where('bill_month', '2026-12')->sole();
|
||
$this->assertSame('pending_factory', $pending->status);
|
||
$this->post(route("business.$section.dropship-freight.assign", ['file' => $pending->id]), ['factory_id' => $factory->id])->assertRedirect();
|
||
$this->assertSame('parsed', $db->table('dropship_bill_files')->where('id', $pending->id)->value('status'));
|
||
|
||
// 厂家设置:有账单不能删;删完账单再删
|
||
$this->post(route("business.$section.dropship-freight.save-factory"), ['id' => $factory->id, 'name' => '临沂卿德客', 'warehouse' => self::WAREHOUSE,
|
||
'keywords' => '卿德客,卿德客申通'])->assertRedirect();
|
||
$this->assertSame('卿德客,卿德客申通', $db->table('dropship_factories')->value('keywords'));
|
||
$this->post(route("business.$section.dropship-freight.delete-factory", ['factory' => $factory->id]))->assertSessionHasErrors('dropship');
|
||
foreach ($db->table('dropship_bill_files')->pluck('id') as $id) {
|
||
$this->post(route("business.$section.dropship-freight.delete-file", ['file' => $id]))->assertRedirect();
|
||
}
|
||
$this->assertSame(0, $db->table('dropship_bill_lines')->count());
|
||
$this->post(route("business.$section.dropship-freight.delete-factory", ['factory' => $factory->id]))->assertRedirect();
|
||
$this->assertSame(0, $db->table('dropship_factories')->count());
|
||
}
|
||
|
||
public function test_partial_duplicate_check_covers_every_waybill_of_a_large_bill(): void
|
||
{
|
||
Storage::fake('local');
|
||
$store = app(DropshipFreightStore::class);
|
||
$store->saveFactory(null, ['name' => '临沂卿德客', 'warehouse' => self::WAREHOUSE, 'keywords' => '卿德客']);
|
||
$first = new UploadedFile($this->xlsx(['账单' => [['快递单号', '运费'], ['773449999999', 5]]]), '卿德客10月.xlsx', null, null, true);
|
||
$store->upload([$first], '2026-10', 't');
|
||
$rows = [['快递单号', '运费']];
|
||
for ($i = 0; $i < 5001; $i++) {
|
||
$rows[] = [(string) (773440000000 + $i), 4];
|
||
}
|
||
$rows[] = ['773449999999', 5]; // 第 5,002 个单号和上一份重复
|
||
$id = $store->upload([new UploadedFile($this->xlsx(['账单' => $rows]), '卿德客10月补.xlsx', null, null, true)], '2026-10', 't')[0];
|
||
$file = $store->db()->table('dropship_bill_files')->find($id);
|
||
$this->assertSame('parsed', $file->status);
|
||
$this->assertSame(5001, (int) $file->waybill_count);
|
||
$this->assertStringContainsString('1 个单号本月别的账单文件里也有', (string) $file->message);
|
||
$this->assertSame(1, $store->db()->table('dropship_bill_lines')->where('waybill_no', '773449999999')->count());
|
||
}
|
||
|
||
public function test_single_export_preserves_original_workbook_and_first_source_row_without_changing_bills(): void
|
||
{
|
||
Storage::fake('local');
|
||
$store = app(DropshipFreightStore::class);
|
||
$id = $store->saveFactory(null, ['name' => '导出测试', 'warehouse' => self::WAREHOUSE, 'keywords' => '卿德客']);
|
||
$path = $this->xlsx([
|
||
'运费' => [['原始账单'], [], ['快递单号', '运费', '备注', '备注'],
|
||
['00012345678901234567', 4, ' 保留空格 ', '=1+1'], ['00012345678901234567', 6, '第二件', '00001']],
|
||
'汇总' => [['厂家自己的汇总']],
|
||
'对账' => [['厂家自己的对账']],
|
||
]);
|
||
$original = \PhpOffice\PhpSpreadsheet\IOFactory::load($path);
|
||
$sheet = $original->getSheetByName('运费');
|
||
$sheet->mergeCells('A1:D1');
|
||
$sheet->getStyle('A1')->getFont()->setBold(true)->setSize(18);
|
||
$sheet->setCellValue('E4', '=B4*2');
|
||
(new Xlsx($original))->save($path);
|
||
$original->disconnectWorksheets();
|
||
$file = $store->upload([new UploadedFile($path, '导出测试.xlsx', null, null, true)], '2026-10', 'tester')[0];
|
||
$db = $store->db();
|
||
// 模拟已核对的金额:导出取当前账单结果,不再次解析入库覆盖。
|
||
$db->table('dropship_bill_lines')->where('file_id', $file)->update(['amount' => 11]);
|
||
$before = $db->table('dropship_bill_lines')->get()->toJson();
|
||
$this->ship('00012345678901234567', '卿德客申通', self::WAREHOUSE, '2026-10-03 09:00:00', 1, 10);
|
||
$out = tempnam(sys_get_temp_dir(), 'dropship_export_');
|
||
$handle = fopen($out, 'wb');
|
||
try {
|
||
app(DropshipExporter::class)->export(collect($store->factories())->firstWhere('id', $id), '2026-10', $handle);
|
||
} finally {
|
||
fclose($handle);
|
||
}
|
||
$book = \PhpOffice\PhpSpreadsheet\IOFactory::load($out);
|
||
$this->assertSame(['运费', '汇总', '对账'], array_slice($book->getSheetNames(), 0, 3));
|
||
$this->assertTrue($book->getSheetByName('运费')->getStyle('A1')->getFont()->getBold());
|
||
$this->assertSame(['A1:D1' => 'A1:D1'], $book->getSheetByName('运费')->getMergeCells());
|
||
$this->assertSame('=B4*2', $book->getSheetByName('运费')->getCell('E4')->getValue());
|
||
$this->assertSame('厂家自己的汇总', $book->getSheetByName('汇总')->getCell('A1')->getValue());
|
||
$data = $book->getSheetByName('对账(系统)')->toArray(null, false, false);
|
||
$this->assertSame(['快递单号', '运费', '备注', '备注(2)'], array_slice($data[0], 0, 4));
|
||
$this->assertSame(['00012345678901234567', 4, ' 保留空格 ', '=1+1'], array_slice($data[1], 0, 4));
|
||
$this->assertEquals(11, $data[1][count($data[0]) - 4]);
|
||
$this->assertContains($book->getSheetByName('对账(系统)')->getCell('D2')->getDataType(), [
|
||
\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_INLINE,
|
||
]);
|
||
$book->disconnectWorksheets();
|
||
$sourceZip = new \ZipArchive();
|
||
$resultZip = new \ZipArchive();
|
||
$sourceZip->open($path);
|
||
$resultZip->open($out);
|
||
$this->assertSame($sourceZip->getFromName('xl/worksheets/sheet1.xml'), $resultZip->getFromName('xl/worksheets/sheet1.xml'));
|
||
$sourceZip->close();
|
||
$resultZip->close();
|
||
$this->assertSame($before, $db->table('dropship_bill_lines')->get()->toJson());
|
||
@unlink($path);
|
||
@unlink($out);
|
||
}
|
||
|
||
public function test_single_export_supports_csv_and_multiple_original_files(): void
|
||
{
|
||
Storage::fake('local');
|
||
$store = app(DropshipFreightStore::class);
|
||
$id = $store->saveFactory(null, ['name' => '导出测试', 'warehouse' => self::WAREHOUSE, 'keywords' => '卿德客']);
|
||
$store->upload([
|
||
UploadedFile::fake()->createWithContent('导出测试.csv', "快递单号,运费,备注\nCSV001,4,00001\n"),
|
||
new UploadedFile($this->xlsx(['明细' => [['快递单号', '运费', '额外'], ['XLSX001', 7, ' 原样 ']]]), '导出测试.xlsx', null, null, true),
|
||
], '2026-10', 'tester');
|
||
$out = tempnam(sys_get_temp_dir(), 'dropship_export_');
|
||
$handle = fopen($out, 'wb');
|
||
try {
|
||
app(DropshipExporter::class)->export(collect($store->factories())->firstWhere('id', $id), '2026-10', $handle);
|
||
} finally {
|
||
fclose($handle);
|
||
}
|
||
$book = \PhpOffice\PhpSpreadsheet\IOFactory::load($out);
|
||
$this->assertSame('明细', $book->getSheetNames()[0]);
|
||
$this->assertContains('文件1-csv', $book->getSheetNames());
|
||
$data = $book->getSheetByName('匹配不到')->toArray(null, false, false);
|
||
$this->assertSame(['快递单号', '运费', '备注', '额外'], array_slice($data[0], 0, 4));
|
||
$csv = collect(array_slice($data, 1))->first(fn ($r) => $r[0] === 'CSV001');
|
||
$xlsx = collect(array_slice($data, 1))->first(fn ($r) => $r[0] === 'XLSX001');
|
||
$this->assertSame('00001', $csv[2]);
|
||
$this->assertSame(' 原样 ', $xlsx[3]);
|
||
$book->disconnectWorksheets();
|
||
@unlink($out);
|
||
}
|
||
|
||
public function test_missing_original_file_returns_an_error_instead_of_a_partial_download(): void
|
||
{
|
||
Storage::fake('local');
|
||
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
|
||
$this->loginAs()->withExceptionHandling();
|
||
$store = app(DropshipFreightStore::class);
|
||
$id = $store->saveFactory(null, ['name' => '导出测试', 'warehouse' => self::WAREHOUSE, 'keywords' => '卿德客']);
|
||
$file = $store->upload([UploadedFile::fake()->createWithContent('导出测试.csv', "快递单号,运费\nCSV001,4\n")], '2026-10', 'tester')[0];
|
||
Storage::disk('local')->delete($store->db()->table('dropship_bill_files')->where('id', $file)->value('stored_path'));
|
||
$page = route('business.pages.show', ['section' => 'courier-payables-final', 'page' => 'dropship-freight-reconciliation']);
|
||
$this->from($page)->get(route('business.courier-payables-final.dropship-freight.export', ['month' => '2026-10', 'factory' => $id]))
|
||
->assertRedirect($page)->assertSessionHasErrors('dropship');
|
||
$this->assertSame(1, $store->db()->table('dropship_bill_lines')->count());
|
||
$this->assertSame('parsed', $store->db()->table('dropship_bill_files')->value('status'));
|
||
}
|
||
|
||
public function test_keyword_helpers(): void
|
||
{
|
||
$this->assertSame('卿德客', DropshipFreightStore::warehouseCore('工厂-山东卿德客代发仓'));
|
||
$this->assertSame('派雅世佳', DropshipFreightStore::warehouseCore('工厂-河北霸州派雅世佳代发仓'));
|
||
$this->assertSame('四强', DropshipFreightStore::commonPart(['四强申通', '四强圆通']));
|
||
$this->assertSame('派雅', DropshipFreightStore::commonPart(['派雅韵达', '派雅世佳']));
|
||
$this->assertSame('', DropshipFreightStore::commonPart(['四强申通', '森卓']));
|
||
$this->assertSame(['卿德客', '极兔'], DropshipFreightStore::splitKeywords('卿德客, 极兔、卿德客'));
|
||
}
|
||
|
||
private function shipping()
|
||
{
|
||
return app(ShippingLookup::class)->connection();
|
||
}
|
||
|
||
private function ship(string $waybill, string $express, string $warehouse, string $time, int $source, ?float $freight): void
|
||
{
|
||
$this->shipping()->table('jst_oms_shipping')->insert(['express_no' => $waybill, 'express_name1' => $express, 'warehouse_name' => $warehouse,
|
||
'ship_time' => $time, 'source_id' => $source]);
|
||
$this->shipping()->table('jst_oms_shipping_freight')->insert(['source_id' => $source, 'freight' => $freight,
|
||
'status' => $freight === null ? '报价管理里没有「' . $express . '」的报价' : 'OK', 'charge_weight' => 1]);
|
||
}
|
||
|
||
/** @param array<string,list<list<mixed>>> $sheets */
|
||
private function xlsx(array $sheets): string
|
||
{
|
||
$book = new Spreadsheet();
|
||
$book->removeSheetByIndex(0);
|
||
foreach ($sheets as $title => $rows) {
|
||
$sheet = $book->createSheet();
|
||
$sheet->setTitle($title);
|
||
foreach ($rows as $r => $row) {
|
||
foreach ($row as $c => $value) {
|
||
if ($value !== null) {
|
||
$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(), 'df_') . '.xlsx';
|
||
(new Xlsx($book))->save($path);
|
||
|
||
return $path;
|
||
}
|
||
}
|