CaiWuXiTong/tests/Feature/Business/UnshippedWaybillsTest.php

282 lines
17 KiB
PHP

<?php
namespace Tests\Feature\Business;
use App\Http\Controllers\Business\UnshippedWaybillController;
use App\Support\UnshippedWaybills;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\Request;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Tests\Feature\FeatureTestCase;
final class UnshippedWaybillsTest extends FeatureTestCase
{
public function test_exact_unshipped_match_precedes_format_and_t_prefix_fallback(): void
{
$service=app(UnshippedWaybills::class);
foreach ([['123456789012','warehouse'],['T123456789012','replacement'],["'T223456789012",'replacement'],
[' @DPK777 ','warehouse'],['123456789013','warehouse'],['T123456789013','replacement']] as $i=>[$number,$source]) {
$service->results()->insert(['waybill_no'=>$number,'source'=>$source,'evidence_no'=>'fuzzy-'.$i,'evidence_time'=>'2026-12-01']);
}
$result=$service->unshippedSources(['123456789012','223456789012','DPK777',' @123456789013.0 ','123456789014']);
$this->assertSame(['仓内未发'],$result['123456789012']);
$this->assertSame(['换单未发出'],$result['223456789012']);
$this->assertSame(['仓内未发'],$result['DPK777']);
$this->assertArrayNotHasKey('123456789013.0',$result);
$this->assertArrayNotHasKey('123456789014',$result);
}
public function test_remark_parser_and_daily_production_cron(): void
{
$this->assertSame([
'old_waybill' => 'DPK001', 'new_waybill' => 'DPK002', 'old_courier' => '曹县-德邦', 'new_courier' => '德邦',
], UnshippedWaybills::parseRemark('曹县-德邦: @DPK001 -> 德邦: @DPK002'));
$this->assertNull(UnshippedWaybills::parseRemark('德邦:7.91508E+13->德邦:DPK002'));
$this->assertNull(UnshippedWaybills::parseRemark('没有新单号'));
foreach (['activate-release.sh', 'activate-incremental-release.sh'] as $file) {
$this->assertMatchesRegularExpression('/^10 7 \* \* \* .*run-finance-batch\.sh courier:sync-unshipped-waybills\b/m',
file_get_contents(base_path('deploy/'.$file)));
}
}
public function test_replacement_excludes_final_number_and_failure_preserves_previous_results(): void
{
$this->travelTo(now()->setDate(2026, 11, 27));
$service = app(UnshippedWaybills::class);
$db = $service->connection();
foreach ([['A', 'B', '09:00:00'], ['B', 'A', '10:00:00']] as [$old, $new, $hour]) {
$remark = '德邦:'.$old.'->圆通:'.$new;
$db->table('courier_unshipped_order_logs')->insert(UnshippedWaybills::parseRemark($remark) + [
'order_no' => '100', 'operated_at' => '2026-11-25 '.$hour, 'remark' => $remark,
]);
}
$this->assertTrue($service->sync('replacement'));
$this->assertSame(['B' => ['换单未发出']], $service->unshippedSources(['A', ' @B ']));
$db->table('courier_unshipped_order_logs')->delete();
$this->assertFalse($service->sync('replacement'));
$this->assertSame(['B' => ['换单未发出']], $service->unshippedSources(['B']));
$this->assertSame(1, (int) $db->table('courier_unshipped_sync')->where('source', 'replacement')->value('row_count'));
}
public function test_outerwms_placeholder_is_not_a_waybill_or_replacement(): void
{
foreach (['OuterWMS', ' @outerwms ', 'OUTERWMS'] as $marker) {
$this->assertNull(UnshippedWaybills::parseRemark('外部仓:'.$marker.'->圆通:YT001'));
$this->assertNull(UnshippedWaybills::parseRemark('圆通:YT001->外部仓:'.$marker));
}
$service = app(UnshippedWaybills::class);
$service->connection()->table('courier_unshipped_order_logs')->insert([
'order_no'=>'OLD-LOG','operated_at'=>'2026-11-28 08:00:00',
'old_waybill'=>'OuterWMS','new_waybill'=>'YT001','old_courier'=>'外部仓',
'new_courier'=>'圆通','remark'=>'外部仓:OuterWMS->圆通:YT001',
]);
$service->connection()->table('jst_oms_shipping')->insert([
['source_id'=>81,'front_no'=>'OuterWMS','express_no'=>'YT001','trade_no'=>'T81','ship_time'=>'2026-11-28','express_name1'=>'圆通'],
['source_id'=>82,'front_no'=>' @outerwms ','express_no'=>'JT002','trade_no'=>'T82','ship_time'=>'2026-11-28','express_name1'=>'极兔'],
['source_id'=>83,'front_no'=>'REAL-OLD','express_no'=>'REAL-NEW','trade_no'=>'T83','ship_time'=>'2026-11-28','express_name1'=>'真实快递'],
['source_id'=>84,'front_no'=>'INVALID-NEW','express_no'=>'OUTERWMS','trade_no'=>'T84','ship_time'=>'2026-11-28','express_name1'=>'外部仓'],
]);
$this->assertTrue($service->sync('replacement'));
$this->assertSame(['REAL-OLD'], $service->results()->pluck('waybill_no')->all());
$this->assertSame('REAL-NEW', $service->results()->value('new_waybill_no'));
$this->assertSame(['REAL-OLD'=>['换单未发出']], $service->unshippedSources(['OuterWMS','REAL-OLD','YT001','JT002']));
$this->loginAs();
$url = route('business.pages.show', ['section'=>'courier-payables','page'=>'unshipped-waybills','waybills'=>'OuterWMS']);
$this->get($url)->assertOk()->assertSeeText('没有符合条件的明细')->assertDontSeeText('REAL-OLD');
}
public function test_intercept_uses_earliest_qualifying_item_and_original_waybill(): void
{
$service = app(UnshippedWaybills::class);
$db = $service->connection();
foreach ([
['ops_jst_return_refund_info', 'ops_jst_return_refund_item', 'ops_jst_orders'],
['ops_jst_qmcust_refunds', 'ops_jst_qmcust_refund_items', 'ops_jst_qmcust_orders'],
] as [$refund, $item, $order]) {
$this->mirrorRows($refund, ['as_id', 'o_id', 'so_id', 'shop_name', 'question_type', 'l_id'], [
['a', '1', 't', '店铺', '现场拦截入库签收', 'RETURN'],
['b', '1', 't', '店铺', '现场拦截入库签收', 'RETURN'],
['c', '1', 't', '店铺', '其他', 'RETURN'],
]);
$this->mirrorRows($item, ['as_id', 'receive_date'], [
['a', null], ['a', '2026-09-05'], ['a', '2026-09-03'], ['b', '2026-08-31'], ['c', '2026-09-03'],
]);
$this->mirrorRows($order, ['o_id', 'l_id', 'logistics_company'], [['1', ' @ORIGINAL ', '圆通']]);
}
$this->assertTrue($service->sync('intercept_regular'));
$this->assertSame(['ORIGINAL' => ['现场拦截入库签收']], $service->unshippedSources(['ORIGINAL', 'RETURN']));
$this->assertSame('2026-09-03', $service->results()->value('evidence_time'));
$this->assertSame(1, $service->results()->count());
$db->getSchemaBuilder()->drop('ops_jst_qmcust_orders');
$this->assertFalse($service->sync('intercept_taobao'));
$this->assertSame(['ORIGINAL' => ['现场拦截入库签收']], $service->unshippedSources(['ORIGINAL']));
}
public function test_warehouse_filters_status_and_exports_waybill_as_text(): void
{
$this->mirrorRows('ops_work_order_form_config', ['work_type', 'work_type_name'], [['type-a', '仓内未发']]);
$this->mirrorRows('ops_work_order_main', ['work_type', 'order_no', 'status', 'is_deleted', 'title', 'create_time'], [
['type-a', 'W1', '待处理', 0, '标题', '2026-10-02'],
['type-a', 'W2', '已拒绝', 0, '', '2026-10-02'],
['type-a', 'W3', '已取消', 0, '', '2026-10-02'],
['type-a', 'W4', '处理中', 0, '', '2026-10-02'],
]);
$this->mirrorRows('ops_work_order_form_value', ['main_order_no', 'field_code', 'field_name', 'field_value', 'is_deleted'], [
['W1', 'lIdField', '快递单号', '000000001234567890', 0],
['W1', 'carrier', '快递名称', '圆通', 0],
['W2', 'lIdField', '快递单号', 'NO2', 0],
['W3', 'lIdField', '快递单号', 'NO3', 0],
['W4', 'lIdField', '快递单号', '', 0],
]);
$service = app(UnshippedWaybills::class);
$this->assertTrue($service->sync('warehouse'));
$this->assertSame(1, $service->results()->count());
$this->loginAs();
$controller = app(UnshippedWaybillController::class);
$page = $controller->page(Request::create('/'));
$this->assertStringContainsString('000000001234567890', view($page['view'], $page)->render());
$response = $controller->export(Request::create('/'));
$file = tempnam(sys_get_temp_dir(), 'unshipped-xlsx');
try {
ob_start();
$response->sendContent();
file_put_contents($file, ob_get_clean());
$cell = IOFactory::createReader('Xlsx')->load($file)->getActiveSheet()->getCell('A2');
$this->assertSame('000000001234567890', $cell->getValue());
$this->assertSame('s', $cell->getDataType());
} finally {
unlink($file);
}
}
public function test_warehouse_reads_verified_work_type_and_json_waybill_when_new_work_order_config_has_no_type(): void
{
$this->mirrorRows('ops_work_order_form_config', ['work_type', 'work_type_name'], [['unrelated', '查件']]);
$this->mirrorRows('ops_work_order_main', ['order_no'], [['other']]);
$this->mirrorRows('ops_work_order_form_value', ['main_order_no'], [['other']]);
$type = '78FC156B524645E886F21A32ED67133F';
$form = json_encode([['fieldId' => 'lIdField', 'value' => '@DPK001'], ['fieldId' => 'o_id', 'value' => '345']]);
$this->mirrorRows('js_jst_work_order_info', ['woTypeId', 'woNo', 'status', 'title', 'createTime', 'businessFieldList'], [
[$type, 'GD1', 'WAIT_AUDIT', '仓内未发', '2026-11-15', $form],
[$type, 'GD2', 'REJECT_AUDIT', '仓内未发', '2026-11-16', $form],
[$type, 'GD3', 'DISABLED', '仓内未发', '2026-11-16', $form],
['other', 'GD4', 'WAIT_AUDIT', '其他', '2026-11-16', $form],
]);
$service = app(UnshippedWaybills::class);
$this->assertTrue($service->sync('warehouse'));
$this->assertSame(['DPK001' => ['仓内未发']], $service->unshippedSources(['DPK001']));
$this->assertSame('GD1', $service->results()->sole()->evidence_no);
}
public function test_daily_sync_keeps_seeded_history_and_adds_only_new_days(): void
{
$this->travelTo(now()->setDate(2026, 11, 29));
$service = app(UnshippedWaybills::class);
$service->results()->insert([
'source' => 'replacement', 'waybill_no' => 'HISTORY1',
'evidence_no' => 'history:2026-11-26', 'evidence_time' => '2026-11-26 00:00:00',
]);
foreach (['2026-11-26' => 'OLD26', '2026-11-27' => 'NEW27'] as $date => $old) {
$remark = '德邦:'.$old.'->圆通:FINAL'.$old;
$service->connection()->table('courier_unshipped_order_logs')->insert(UnshippedWaybills::parseRemark($remark) + [
'order_no' => $old, 'operated_at' => $date.' 10:00:00', 'remark' => $remark,
]);
}
$this->assertTrue($service->sync('replacement'));
$this->assertSame(['HISTORY1', 'NEW27'], $service->results()->orderBy('waybill_no')->pluck('waybill_no')->all());
}
public function test_shipping_record_supplies_replacement_evidence_without_operation_log(): void
{
$service=app(UnshippedWaybills::class);$db=$service->connection();
$db->table('jst_oms_shipping')->insert([
['source_id'=>71,'front_no'=>'@OLD71','express_no'=>'NEW71','trade_no'=>'T71','ship_time'=>'2026-11-28','express_name1'=>'测试快递'],
['source_id'=>72,'front_no'=>'OLD72','express_no'=>'NEW72','trade_no'=>'T72','ship_time'=>'2026-11-28','express_name1'=>'测试快递'],
['source_id'=>73,'front_no'=>null,'express_no'=>'OLD72','trade_no'=>'T73','ship_time'=>'2026-11-28','express_name1'=>'测试快递'],
]);
$this->assertTrue($service->sync('replacement'));
$this->assertSame(['OLD71'=>['换单未发出']],$service->unshippedSources(['OLD71','OLD72','NEW71']));
$this->assertStringContainsString('非换单操作时间',$service->results()->sole()->evidence_text);
}
public function test_staged_publish_preserves_history_and_does_not_expose_partial_rows(): void
{
$service=app(UnshippedWaybills::class);$db=$service->connection();
$service->results()->insert(['source'=>'warehouse','waybill_no'=>'HISTORY','evidence_no'=>'history:2026-11-26','evidence_time'=>'2026-11-26']);
foreach(['one','two'] as $generation)$db->table('courier_unshipped_stage')->insert(['source'=>'warehouse','row_key'=>hash('sha256',$generation),'generation'=>$generation,'waybill_no'=>strtoupper($generation),'evidence_no'=>$generation,'evidence_time'=>'2026-11-28']);
$this->assertSame(1,$service->results()->count());
$this->assertSame(1,$service->publishStage('warehouse','two',0));
$this->assertSame(['HISTORY','TWO'],$service->results()->orderBy('waybill_no')->pluck('waybill_no')->all());
}
public function test_page_keeps_filter_export_scope_and_displays_sync_gaps(): void
{
$this->loginAs();
$service = app(UnshippedWaybills::class);
$service->results()->insert(['source'=>'warehouse','waybill_no'=>'KEEP-001','evidence_no'=>'W1',
'evidence_time'=>'2026-11-28 10:00:00','courier_name'=>'测试快递']);
$service->connection()->table('courier_unshipped_sync')->insert(['source'=>'intercept_regular',
'last_success_at'=>'2026-12-06 07:10:00','row_count'=>138,'missing_count'=>11]);
$url = route('business.pages.show', ['section'=>'courier-payables','page'=>'unshipped-waybills',
'sources'=>['warehouse'],'courier'=>'测试快递','from'=>'2026-11-01','to'=>'2026-11-30']);
$response = $this->get($url)->assertOk()->assertSeeText('KEEP-001')->assertSeeText('缺原订单单号 11 条')
->assertSeeText('导出筛选结果')->assertSee('data-uw-filter', false)->assertSee('data-date-picker', false);
$this->assertStringContainsString('sources%5B0%5D=warehouse', $response->getContent());
$this->get($url.'&waybills=NOT-FOUND')->assertOk()->assertSeeText('没有符合条件的明细')
->assertDontSeeText('KEEP-001')->assertSeeText('清空筛选');
}
public function test_replacement_page_and_export_keep_both_waybills_and_courier_names(): void
{
$this->loginAs();
app(UnshippedWaybills::class)->results()->insert([
['source'=>'replacement','waybill_no'=>'000000001234567890','courier_name'=>'原快递',
'new_waybill_no'=>'000000009876543210','new_courier_name'=>'新快递',
'evidence_no'=>'REPLACED-1','evidence_time'=>'2026-11-28 10:00:00'],
['source'=>'replacement','waybill_no'=>'MISSING-DETAILS','courier_name'=>null,
'new_waybill_no'=>null,'new_courier_name'=>null,
'evidence_no'=>'history:2026-11-26','evidence_time'=>'2026-11-26 00:00:00'],
]);
$url = route('business.pages.show', ['section'=>'courier-payables','page'=>'unshipped-waybills',
'sources'=>['replacement']]);
$this->get($url)->assertOk()->assertSeeText('换单前单号')->assertSeeText('换单前快递名称')
->assertSeeText('换单后单号')->assertSeeText('换单后快递名称')
->assertSeeText('000000001234567890')->assertSeeText('000000009876543210')
->assertSeeText('原快递')->assertSeeText('新快递')->assertSeeText('未提供');
$response = app(UnshippedWaybillController::class)->export(Request::create('/?sources[]=replacement'));
$file = tempnam(sys_get_temp_dir(), 'replacement-xlsx');
$book = null;
try {
ob_start();
$response->sendContent();
file_put_contents($file, ob_get_clean());
$book = IOFactory::createReader('Xlsx')->load($file);
$sheet = $book->getActiveSheet();
$this->assertSame(['换单前单号','来源','换单前快递名称','换单后单号','换单后快递名称','依据时间'],
$sheet->rangeToArray('A1:F1')[0]);
foreach (['A2'=>'000000001234567890','C2'=>'原快递','D2'=>'000000009876543210','E2'=>'新快递'] as $cell=>$value) {
$this->assertSame($value, $sheet->getCell($cell)->getValue());
$this->assertSame('s', $sheet->getCell($cell)->getDataType());
}
$this->assertNull($sheet->getCell('D3')->getValue());
$this->assertNull($sheet->getCell('E3')->getValue());
} finally {
$book?->disconnectWorksheets();
unlink($file);
}
}
private function mirrorRows(string $table, array $fields, array $rows): void
{
$connection = app(UnshippedWaybills::class)->connection();
$connection->getSchemaBuilder()->dropIfExists($table);
$connection->getSchemaBuilder()->create($table, function (Blueprint $blueprint) use ($fields): void {
$blueprint->id();
foreach ($fields as $field) $blueprint->string($field)->nullable();
});
$connection->table($table)->insert(array_map(fn ($row) => array_combine($fields, $row), $rows));
}
}