diff --git a/app/Support/UnshippedWaybills.php b/app/Support/UnshippedWaybills.php index bba72601..3ef59436 100644 --- a/app/Support/UnshippedWaybills.php +++ b/app/Support/UnshippedWaybills.php @@ -11,6 +11,11 @@ final class UnshippedWaybills public const START = '2026-09-01'; public const SOURCES = ['intercept' => '现场拦截入库签收', 'replacement' => '换单未发出', 'warehouse' => '仓内未发']; + private const WAREHOUSE_WORK_TYPE = '78FC156B524645E886F21A32ED67133F'; + private const WAREHOUSE_EXCLUDED_STATUSES = [ + 'REJECT_AUDIT', 'DISABLED', 'REJECTED', 'REFUSED', 'CANCELED', 'CANCELLED', '已拒绝', '已取消', + ]; + private const INTERCEPT_TABLES = [ 'intercept_regular' => ['ops_jst_return_refund_info', 'ops_jst_return_refund_item', 'ops_jst_orders'], 'intercept_taobao' => ['ops_jst_qmcust_refunds', 'ops_jst_qmcust_refund_items', 'ops_jst_qmcust_orders'], @@ -203,7 +208,7 @@ final class UnshippedWaybills $types = $db->table('ops_work_order_form_config as c')->joinSub($latest, 'latest', 'latest.id', '=', 'c.id') ->where('c.work_type_name', self::SOURCES['warehouse'])->pluck('c.work_type'); if ($types->isEmpty()) { - throw new RuntimeException('未找到仓内未发工单类型或单号字段。'); + return [$this->withWarehouseCouriers($this->warehouseFromJiansun()), 0]; } $workFields = $db->table('ops_work_order_main as w')->join('ops_work_order_form_value as v', 'v.main_order_no', '=', 'w.order_no') @@ -229,8 +234,47 @@ final class UnshippedWaybills 'evidence_text' => trim(($order->status ?? '').' '.$order->title), ]; } + return [$this->withWarehouseCouriers($rows), 0]; + } + + private function warehouseFromJiansun(): array + { + // The verified warehouse work orders are mirrored from jst_workOrderInfo. + $table = 'js_jst_work_order_info'; + $this->assertMirrorReady([$table]); + $workOrders = $this->connection()->table($table)->where('woTypeId', self::WAREHOUSE_WORK_TYPE) + ->where('createTime', '>=', self::START) + ->where(fn ($q) => $q->whereNull('status')->orWhereNotIn('status', self::WAREHOUSE_EXCLUDED_STATUSES)) + ->select(['Id', 'woNo', 'status', 'title', 'createTime', 'businessFieldList']); + + $rows = []; + foreach ($workOrders->lazyById(500, 'Id') as $order) { + $fields = []; + foreach (json_decode((string) $order->businessFieldList, true) ?? [] as $field) { + if (is_array($field) && isset($field['fieldId']) && is_scalar($field['value'] ?? null)) { + $fields[$field['fieldId']] = (string) $field['value']; + } + } + $number = self::normalize($fields['lIdField'] ?? null); + if ($number === '') continue; + $rows[$order->woNo.'|'.$number] = [ + 'source' => 'warehouse', 'waybill_no' => $number, 'courier_name' => '', + 'evidence_no' => $order->woNo, 'evidence_time' => $order->createTime, + 'o_id' => $fields['o_id'] ?? null, 'evidence_text' => trim(($order->status ?? '').' '.$order->title), + ]; + } + + return $rows; + } + + private function withWarehouseCouriers(array $rows): array + { + $db = $this->connection(); $numbers = array_unique(array_column(array_filter($rows, fn ($row) => $row['courier_name'] === ''), 'waybill_no')); $shipping = []; + if ($numbers !== [] && !$db->getSchemaBuilder()->hasTable('jst_oms_shipping')) { + return $rows; + } foreach (array_chunk($numbers, 500) as $chunk) { foreach ($db->table('jst_oms_shipping')->whereIn('express_no', $chunk)->get(['express_no', 'express_name1', 'express_name2']) as $item) { $shipping[$item->express_no] = $item->express_name1 ?: $item->express_name2; @@ -240,6 +284,6 @@ final class UnshippedWaybills $row['courier_name'] = $row['courier_name'] ?: ($shipping[$row['waybill_no']] ?? null); } - return [$rows, 0]; + return $rows; } } diff --git a/tests/Feature/Business/UnshippedWaybillsTest.php b/tests/Feature/Business/UnshippedWaybillsTest.php index 8721779a..ec141a15 100644 --- a/tests/Feature/Business/UnshippedWaybillsTest.php +++ b/tests/Feature/Business/UnshippedWaybillsTest.php @@ -107,6 +107,25 @@ final class UnshippedWaybillsTest extends FeatureTestCase } } + 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();