251 lines
17 KiB
PHP
251 lines
17 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\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();
|
||
$this->get(route("business.$section.dropship-freight.export", ['month' => '2026-10', 'factory' => $factory->id]))->assertOk();
|
||
|
||
// 同一份账单再传:重复导入,不读入
|
||
$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_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;
|
||
}
|
||
}
|