31 lines
1.2 KiB
JavaScript
31 lines
1.2 KiB
JavaScript
const { Client } = require('pg');
|
|
const client = new Client('postgres://postgres:csh%402018@localhost:5432/ai_pharmacy');
|
|
|
|
client.connect().then(async () => {
|
|
try {
|
|
// 1. Update the pack_size of Losar 50 to 10
|
|
await client.query("UPDATE products SET pack_size = 10 WHERE id = 'prd_4410'");
|
|
|
|
// 2. Fix all old dispensing records that were calculated with pack_size=1
|
|
// For every dispensing record, we recalculate the total amount.
|
|
const res = await client.query(`
|
|
SELECT d.id,
|
|
SUM((p.mrp / COALESCE(NULLIF(p.pack_size, 0), 1)) * ABS(sm.qty_delta)) as new_total
|
|
FROM dispensing_records d
|
|
JOIN stock_movements sm ON sm.reference_id = d.id
|
|
JOIN batches b ON b.batch_no = sm.batch_no
|
|
JOIN products p ON p.id = b.product_id
|
|
GROUP BY d.id
|
|
`);
|
|
|
|
for (const row of res.rows) {
|
|
await client.query("UPDATE dispensing_records SET total_amount = $1 WHERE id = $2", [row.new_total, row.id]);
|
|
}
|
|
|
|
console.log("Updated pack_size to 10 for prd_4410 and corrected all historical total amounts.");
|
|
} catch (err) {
|
|
console.error('Error fixing history:', err);
|
|
}
|
|
process.exit(0);
|
|
}).catch(console.error);
|