const pool = require("./database");

async function checkUndelivered() {
  try {
    const res = await pool.query(`
      SELECT 
        s.sub_order_id,
        s.order_id,
        s.design_id,
        s.design_style,
        s.print_type,
        s.inward_weight,
        s.delivered_weight,
        s.suborder_dept_flow_status,
        s.dept_status,
        om.order_status,
        om.order_date,
        s.suborder_date,
        s.expected_delivery_date
      FROM sub_order s
      JOIN order_master om ON s.order_id = om.order_id
      ORDER BY s.suborder_date DESC NULLS LAST
    `);
    console.log("ALL SUBORDERS IN DB (Total:", res.rows.length, "):");
    res.rows.forEach(r => {
      console.log(`SUB: ${r.sub_order_id} | ORD: ${r.order_id} | Inward: ${r.inward_weight} | Delivered: ${r.delivered_weight} | flow_status: ${r.suborder_dept_flow_status} | dept_status: ${r.dept_status} | om_status: ${r.order_status}`);
    });
  } catch (err) {
    console.error(err);
  } finally {
    await pool.end();
  }
}

checkUndelivered();
