来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
117 lines
3.7 KiB
JavaScript
117 lines
3.7 KiB
JavaScript
/**
|
|
* Drill into a few MOBILE orders: order amount vs cost vs items.
|
|
*/
|
|
const { Pool } = require('pg');
|
|
|
|
const CODES = [
|
|
'ORD_MP_17839241302808VMY',
|
|
'ORD_MP_1783927512218OVB8',
|
|
'ORD_MP_1783923824069PWT2',
|
|
'ORD_MP_17839244756069KIY',
|
|
];
|
|
|
|
async function main() {
|
|
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
|
|
for (const code of CODES) {
|
|
const order = await pool.query(`SELECT * FROM orders WHERE order_code = $1`, [code]);
|
|
const items2 = await pool.query(`SELECT * FROM order_items WHERE order_code = $1 LIMIT 30`, [code]);
|
|
const costs = await pool.query(`SELECT * FROM cost_details WHERE order_code = $1 ORDER BY created_at`, [
|
|
code,
|
|
]);
|
|
|
|
const itemSum = items2.rows.reduce((s, r) => {
|
|
const amt = Number(r.amount ?? r.line_amount ?? r.total_amount ?? 0);
|
|
const qty = Number(r.quantity || 0);
|
|
const price = Number(r.unit_price ?? r.price ?? 0);
|
|
return s + (amt || qty * price);
|
|
}, 0);
|
|
|
|
console.log('\n====', code, '====');
|
|
console.log('order keys sample:', {
|
|
order_code: order.rows[0]?.order_code,
|
|
cycle_code: order.rows[0]?.cycle_code,
|
|
order_status: order.rows[0]?.order_status,
|
|
total_amount: order.rows[0]?.total_amount,
|
|
customer_code: order.rows[0]?.customer_code,
|
|
branch_code: order.rows[0]?.branch_code,
|
|
updated_at: order.rows[0]?.updated_at,
|
|
});
|
|
console.log(
|
|
'items sample:',
|
|
items2.rows.slice(0, 6).map((r) => ({
|
|
name: r.sku_name || r.product_name || r.name || r.sku_code,
|
|
qty: r.quantity,
|
|
unit_price: r.unit_price ?? r.price,
|
|
amount: r.amount ?? r.line_amount,
|
|
purchase_price: r.purchase_price ?? r.cost_price,
|
|
}))
|
|
);
|
|
console.log('items sum (approx):', itemSum);
|
|
console.log(
|
|
'costs:',
|
|
costs.rows.map((r) => ({
|
|
type: r.cost_type,
|
|
amount: r.amount,
|
|
allocated: r.allocated_amount,
|
|
cycle: r.cycle_code,
|
|
sku: r.sku_code,
|
|
qty: r.quantity,
|
|
unit_price: r.unit_price,
|
|
note: r.remark || r.notes || r.description,
|
|
}))
|
|
);
|
|
console.log(
|
|
'cost sum:',
|
|
costs.rows.reduce((s, r) => s + Number(r.allocated_amount ?? r.amount ?? 0), 0)
|
|
);
|
|
}
|
|
|
|
// Week totals excluding MOBILE vs only 202607
|
|
const weekStart = new Date();
|
|
weekStart.setDate(weekStart.getDate() - 7);
|
|
const weekStartIso = weekStart.toISOString();
|
|
const split = await pool.query(
|
|
`WITH week_orders AS (
|
|
SELECT order_code, cycle_code, total_amount
|
|
FROM orders
|
|
WHERE order_status >= 90 AND updated_at >= $1
|
|
),
|
|
costs AS (
|
|
SELECT wo.cycle_code,
|
|
wo.order_code,
|
|
wo.total_amount,
|
|
COALESCE(SUM(
|
|
CASE WHEN LOWER(COALESCE(cd.cost_type,'')) = 'purchase_applied' THEN 0
|
|
ELSE COALESCE(cd.allocated_amount, cd.amount, 0) END
|
|
), 0) AS cost
|
|
FROM week_orders wo
|
|
LEFT JOIN cost_details cd ON cd.order_code = wo.order_code
|
|
GROUP BY wo.cycle_code, wo.order_code, wo.total_amount
|
|
)
|
|
SELECT cycle_code,
|
|
COUNT(*) AS orders,
|
|
ROUND(SUM(total_amount)::numeric, 2) AS revenue,
|
|
ROUND(SUM(cost)::numeric, 2) AS cost,
|
|
ROUND((SUM(total_amount) - SUM(cost))::numeric, 2) AS gross,
|
|
ROUND(
|
|
CASE WHEN SUM(total_amount) > 0
|
|
THEN ((SUM(total_amount) - SUM(cost)) / SUM(total_amount) * 100)
|
|
ELSE 0 END
|
|
, 1) AS margin_pct
|
|
FROM costs
|
|
GROUP BY cycle_code
|
|
ORDER BY cycle_code`,
|
|
[weekStartIso]
|
|
);
|
|
console.log('\n==== week split by cycle ====');
|
|
console.log(split.rows);
|
|
|
|
await pool.end();
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|