来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
180 lines
6.5 KiB
JavaScript
180 lines
6.5 KiB
JavaScript
/**
|
|
* 生产急救:ORD_MP_1785227388534RMSP 四行漏分配补自采并挂已有待验。
|
|
* 在 API 机 /opt/ai-fresh-delivery/server 下:node prod-fix-rmsp-self-purchase.cjs
|
|
*/
|
|
const { Pool } = require('pg');
|
|
const fs = require('fs');
|
|
|
|
const ORDER = 'ORD_MP_1785227388534RMSP';
|
|
const SKUS = [
|
|
'SKU78659532914', // 丝瓜
|
|
'SKU78667596954', // 青条椒
|
|
'SKU78661943067', // 小葱
|
|
'SKU78668141144', // 香菜
|
|
];
|
|
|
|
async function loadDbUrl() {
|
|
const env = fs.readFileSync('/opt/ai-fresh-delivery/server/.env', 'utf8');
|
|
const m = env.match(/^DATABASE_URL=(.+)$/m);
|
|
if (!m) throw new Error('no DATABASE_URL');
|
|
return m[1].trim().replace(/^["']|["']$/g, '');
|
|
}
|
|
|
|
function purchaseCodeFor(itemId) {
|
|
return `PUR${Date.now()}${String(itemId).slice(-4)}${Math.floor(Math.random() * 1000)}`;
|
|
}
|
|
|
|
(async () => {
|
|
const pool = new Pool({ connectionString: await loadDbUrl() });
|
|
const client = await pool.connect();
|
|
try {
|
|
await client.query('BEGIN');
|
|
await client.query(`SELECT set_config('search_path', 'tenant_br002, public', false)`);
|
|
|
|
const order = await client.query(
|
|
`SELECT order_code, order_status, customer_code, branch_code FROM orders WHERE order_code = $1 FOR UPDATE`,
|
|
[ORDER]
|
|
);
|
|
if (!order.rows[0]) throw new Error('order not found');
|
|
console.log('ORDER', order.rows[0]);
|
|
|
|
const purchaser = await client.query(
|
|
`SELECT purchaser_code, purchaser_name FROM purchase_orders
|
|
WHERE order_code = $1 AND purchaser_code IS NOT NULL AND TRIM(purchaser_code) <> ''
|
|
ORDER BY created_at ASC LIMIT 1`,
|
|
[ORDER]
|
|
);
|
|
const fallbackPurchaser = purchaser.rows[0] || {
|
|
purchaser_code: 'PURCH_FIX',
|
|
purchaser_name: '漏分配急救',
|
|
};
|
|
const { purchaser_code, purchaser_name } = fallbackPurchaser;
|
|
|
|
const created = [];
|
|
for (const sku of SKUS) {
|
|
const oi = await client.query(
|
|
`SELECT id, item_name, quantity, unit, price FROM order_items
|
|
WHERE order_code = $1 AND sku_code = $2 LIMIT 1`,
|
|
[ORDER, sku]
|
|
);
|
|
const item = oi.rows[0];
|
|
if (!item) {
|
|
console.log('SKIP missing item', sku);
|
|
continue;
|
|
}
|
|
|
|
const existingPo = await client.query(
|
|
`SELECT purchase_code, purchase_status FROM purchase_orders
|
|
WHERE order_code = $1 AND sku_code = $2 AND purchase_status < 80
|
|
LIMIT 1`,
|
|
[ORDER, sku]
|
|
);
|
|
let code = existingPo.rows[0]?.purchase_code;
|
|
|
|
// quote cost
|
|
let cost = 0;
|
|
const q = await client.query(
|
|
`SELECT COALESCE(i.cost_price, 0)::float AS cost_price
|
|
FROM customer_quote_sheet_items i
|
|
INNER JOIN customer_quote_sheets qs ON qs.quote_sheet_code = i.quote_sheet_code
|
|
INNER JOIN customer_quote_sheet_customers qsc ON qsc.quote_sheet_code = qs.quote_sheet_code
|
|
WHERE qsc.customer_code = $1 AND i.sku_code = $2
|
|
AND qs.approval_status = 30 AND qs.status = 1
|
|
AND (qs.expires_at IS NULL OR qs.expires_at > NOW())
|
|
AND (qs.branch_code IS NOT DISTINCT FROM $3)
|
|
ORDER BY qs.updated_at DESC NULLS LAST LIMIT 1`,
|
|
[order.rows[0].customer_code, sku, order.rows[0].branch_code]
|
|
);
|
|
cost = Number(q.rows[0]?.cost_price) || 0;
|
|
if (!(cost > 0)) {
|
|
const s = await client.query(`SELECT cost_price FROM skus WHERE sku_code = $1`, [sku]);
|
|
cost = Number(s.rows[0]?.cost_price) || 0;
|
|
}
|
|
|
|
if (!code) {
|
|
code = purchaseCodeFor(item.id);
|
|
await client.query(
|
|
`INSERT INTO purchase_orders (
|
|
purchase_code, order_code, sku_code, supplier_code, purchaser_code, purchaser_name,
|
|
purchase_qty, purchase_price, purchase_status, is_temporary_supplier,
|
|
source_type, source_id, remark, branch_code, actual_qty, actual_price, submitted_at
|
|
) VALUES ($1,$2,$3,NULL,$4,$5,$6,$7,20,false,'order_no_supplier',$8,$9,$10,$6,$7,NOW())`,
|
|
[
|
|
code,
|
|
ORDER,
|
|
sku,
|
|
purchaser_code,
|
|
purchaser_name || purchaser_code,
|
|
Number(item.quantity) || 0,
|
|
cost,
|
|
String(item.id),
|
|
`[${purchaser_name || purchaser_code}] 漏分配急救:转自采`,
|
|
order.rows[0].branch_code,
|
|
]
|
|
);
|
|
} else if (Number(existingPo.rows[0].purchase_status) < 20) {
|
|
await client.query(
|
|
`UPDATE purchase_orders
|
|
SET purchase_status = 20, actual_qty = COALESCE(actual_qty, purchase_qty),
|
|
actual_price = COALESCE(NULLIF(actual_price, 0), purchase_price),
|
|
submitted_at = COALESCE(submitted_at, NOW())
|
|
WHERE purchase_code = $1`,
|
|
[code]
|
|
);
|
|
}
|
|
|
|
await client.query(
|
|
`INSERT INTO order_item_fulfillments (
|
|
order_item_id, order_code, supplier_preparation_id, purchase_code, inventory_cover_qty, updated_at
|
|
) VALUES ($1::uuid, $2, NULL, $3, 0, NOW())
|
|
ON CONFLICT (order_item_id) DO UPDATE SET
|
|
purchase_code = EXCLUDED.purchase_code,
|
|
supplier_preparation_id = NULL,
|
|
updated_at = NOW()`,
|
|
[item.id, ORDER, code]
|
|
);
|
|
|
|
const insp = await client.query(
|
|
`SELECT id, source_type, processing_details, purchase_price
|
|
FROM inspections
|
|
WHERE order_code = $1 AND sku_code = $2
|
|
AND inspection_time IS NULL
|
|
AND COALESCE(inbound_batch_no, '') = ''
|
|
ORDER BY created_at DESC LIMIT 1`,
|
|
[ORDER, sku]
|
|
);
|
|
if (insp.rows[0]) {
|
|
const details =
|
|
insp.rows[0].processing_details && typeof insp.rows[0].processing_details === 'object'
|
|
? { ...insp.rows[0].processing_details }
|
|
: {};
|
|
details.purchase_code = code;
|
|
details.purchase_codes = Array.from(
|
|
new Set([...(Array.isArray(details.purchase_codes) ? details.purchase_codes : []), code])
|
|
);
|
|
await client.query(
|
|
`UPDATE inspections
|
|
SET source_type = 'purchase',
|
|
processing_details = $2::jsonb,
|
|
purchase_price = COALESCE(NULLIF(purchase_price, 0), $3)
|
|
WHERE id = $1`,
|
|
[insp.rows[0].id, JSON.stringify(details), cost > 0 ? cost : null]
|
|
);
|
|
}
|
|
|
|
created.push({ sku, item: item.item_name, purchase_code: code, cost });
|
|
}
|
|
|
|
// order status stays as-is
|
|
await client.query('COMMIT');
|
|
console.log('OK', JSON.stringify(created, null, 2));
|
|
} catch (e) {
|
|
await client.query('ROLLBACK').catch(() => undefined);
|
|
console.error(e);
|
|
process.exitCode = 1;
|
|
} finally {
|
|
client.release();
|
|
await pool.end();
|
|
}
|
|
})();
|