140 lines
7.7 KiB
PHP
140 lines
7.7 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_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 5 \* \* \* .*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_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);
|
|
}
|
|
|
|
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));
|
|
}
|
|
}
|