/** * 单订单纠正并打印 pending 判定细节 */ const path = require('path'); const serverRoot = process.env.SERVER_ROOT || process.cwd(); process.chdir(serverRoot); const { pool } = require(path.join(serverRoot, 'dist/db')); const { reprocessOrderAllocation } = require(path.join(serverRoot, 'dist/services/orderAllocationService')); const { resolveItemAllocation, DEFAULT_ALLOCATION_RULES } = require( path.join(serverRoot, 'dist/services/allocationRules') ); const { isOrderItemFulfillmentValid } = require( path.join(serverRoot, 'dist/services/orderItemFulfillmentService') ); const { fetchCategoryRows } = require(path.join(serverRoot, 'dist/services/categoryCatalogService')); function parseArg(name, fallback) { const hit = process.argv.find((a) => a.startsWith(`--${name}=`)); return hit ? hit.slice(name.length + 3) : fallback; } async function main() { const orderCode = parseArg('order', 'ORD_MP_1784184988527TF2W'); const order = ( await pool.query( `SELECT order_code, customer_code, branch_code, order_status FROM orders WHERE order_code = $1`, [orderCode] ) ).rows[0]; const items = ( await pool.query( `SELECT oi.*, s.sku_name AS joined_sku_name, s.category_code AS joined_category_code, s.unit AS joined_unit FROM order_items oi LEFT JOIN skus s ON s.sku_code = oi.sku_code WHERE oi.order_code = $1`, [orderCode] ) ).rows.map((row) => ({ ...row, sku_name: row.joined_sku_name || row.item_name, category_code: row.category_code || row.joined_category_code || null, unit: row.unit || row.joined_unit || '斤', })); const [suppliers, supplierCategories, skuSuppliers, categoryRows] = await Promise.all([ pool.query(`SELECT supplier_code, company_name, rating, status FROM suppliers WHERE status IN (10,20,30,40)`), pool.query(`SELECT supplier_code, category_code, is_primary FROM supplier_categories`), pool.query( `SELECT sku_code, supplier_code, supply_price, is_preferred, in_stock, status, quality_rating FROM sku_suppliers WHERE status = 1` ), fetchCategoryRows(order.branch_code), ]); console.log('--- pending decision ---'); for (const item of items) { const fulfilled = await isOrderItemFulfillmentValid(pool, String(item.id)); const openPo = await pool.query( `SELECT purchase_code FROM purchase_orders WHERE order_code = $1 AND source_id = $2 AND purchase_status IN (10, 32) LIMIT 1`, [orderCode, String(item.id)] ); const hasOpenPo = Boolean(openPo.rowCount); const resolved = resolveItemAllocation({ orderCode, item: { id: String(item.id), sku_code: String(item.sku_code || ''), sku_name: String(item.sku_name || item.item_name || ''), category_code: item.category_code || undefined, category_name: item.category_name || undefined, quantity: Number(item.quantity) || 0, price: Number(item.price) || 0, unit: String(item.unit || '斤'), }, rules: DEFAULT_ALLOCATION_RULES, supplierCategories: supplierCategories.rows, suppliers: suppliers.rows, skuSuppliers: skuSuppliers.rows, categoryRows, }); const wouldPending = !fulfilled || (hasOpenPo && resolved?.target === 'supplier'); if (wouldPending || hasOpenPo) { console.log( JSON.stringify({ name: item.item_name || item.sku_name, category: item.category_code, fulfilled, hasOpenPo, target: resolved?.target, source: resolved?.allocation_source, supplier: resolved?.supplier_code, wouldPending, }) ); } } console.log('--- reprocess ---'); const result = await reprocessOrderAllocation(orderCode, order.customer_code, order.branch_code); console.log(JSON.stringify(result, null, 2)); const after = await pool.query( `SELECT purchase_code, sku_code, purchase_status, source_id FROM purchase_orders WHERE order_code = $1 AND purchase_status IN (10,32,40) ORDER BY purchase_status, purchase_code`, [orderCode] ); console.log('--- purchase after ---'); console.table(after.rows); const preps = await pool.query( `SELECT id, sku_code, supplier_code, prepare_status, prepare_qty FROM supplier_preparations WHERE order_code = $1`, [orderCode] ); console.log('--- preparations ---'); console.table(preps.rows); await pool.end(); } main().catch(async (e) => { console.error(e); try { await pool.end(); } catch (_) {} process.exit(1); });