fix(未发出明细): 排除外部仓占位单号 #1791

Closed
limeng wants to merge 1 commits from fix/limeng-unshipped-outerwms into main
2 changed files with 38 additions and 3 deletions

View File

@ -53,6 +53,12 @@ final class UnshippedWaybills
return ltrim(preg_replace('/\s+/u', '', $number ?? '') ?? '', '@');
}
private static function isPlaceholder(?string $number): bool
{
// 外部仓占位标记不是运单号,不能作为未发出或换单依据。
return strcasecmp(self::normalize($number), 'OuterWMS') === 0;
}
public static function parseRemark(string $remark): ?array
{
if (!preg_match('/^\s*([^::]+)[::]([^::]+?)\s*->\s*([^::]+)[::]([^::]+)\s*$/u', $remark, $parts)) {
@ -61,7 +67,7 @@ final class UnshippedWaybills
$old = self::normalize($parts[2]);
$new = self::normalize($parts[4]);
foreach ([$old, $new] as $number) {
if (!preg_match('/^[A-Za-z0-9-]{1,96}$/', $number) || preg_match('/^\d+(?:\.\d+)?e[+-]?\d+$/i', $number)) return null;
if (self::isPlaceholder($number) || !preg_match('/^[A-Za-z0-9-]{1,96}$/', $number) || preg_match('/^\d+(?:\.\d+)?e[+-]?\d+$/i', $number)) return null;
}
return ['old_waybill' => $old, 'new_waybill' => $new, 'old_courier' => trim($parts[1]), 'new_courier' => trim($parts[3])];
@ -75,7 +81,8 @@ final class UnshippedWaybills
public function unshippedSources(array $waybills): array
{
$result = [];
foreach (array_chunk(array_unique(array_filter(array_map(self::normalize(...), $waybills))), 500) as $chunk) {
foreach (array_chunk(array_unique(array_filter(array_map(self::normalize(...), $waybills),
fn ($number) => $number !== '' && !self::isPlaceholder($number))), 500) as $chunk) {
foreach ($this->results()->whereIn('waybill_no', $chunk)->distinct()->get(['waybill_no', 'source']) as $row) {
$label = self::label($row->source);
$result[$row->waybill_no][$label] = $label;
@ -191,6 +198,7 @@ final class UnshippedWaybills
$currentOrder = null;
$finalNumber = null;
foreach ($logs as $log) {
if (self::isPlaceholder($log->old_waybill) || self::isPlaceholder($log->new_waybill)) continue;
if ($currentOrder !== $log->order_no) {
$currentOrder = $log->order_no;
$finalNumber = $log->new_waybill;
@ -211,7 +219,7 @@ final class UnshippedWaybills
->select(['id', 'source_id', 'front_no', 'express_no', 'trade_no', 'ship_time', 'express_name1'])->lazyById(500);
foreach ($ships as $ship) {
$old = self::normalize($ship->front_no); $new = self::normalize($ship->express_no);
if ($old === '' || $new === '' || $old === $new) continue;
if ($old === '' || $new === '' || $old === $new || self::isPlaceholder($old) || self::isPlaceholder($new)) continue;
// 旧号码仍是现有发货包裹时,不因另一条换单记录把它排除。
if ($this->connection()->table('jst_oms_shipping')->whereIn('express_no', [$old, '@'.$old])->exists()) continue;
$rows['shipping:'.$ship->source_id.'|'.$old] = [

View File

@ -43,6 +43,33 @@ final class UnshippedWaybillsTest extends FeatureTestCase
$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);