Enhance Order Summary Query Logic
- Added checks to ensure that only orders with a positive quantity are included in the order summary by using COALESCE to handle null values. - Updated the query logic to improve data integrity and accuracy in the order summary results. (TASK: ERP-XXX)
This commit is contained in:
@@ -86,6 +86,7 @@ export async function getOrderSummary(
|
||||
FROM sales_order_mng so
|
||||
WHERE ${whereClause}
|
||||
AND so.part_code IS NOT NULL AND so.part_code != ''
|
||||
AND COALESCE(so.order_qty::numeric, 0) > 0
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM sales_order_detail sd
|
||||
WHERE sd.order_no = so.order_no AND sd.company_code = so.company_code
|
||||
@@ -103,6 +104,7 @@ export async function getOrderSummary(
|
||||
INNER JOIN sales_order_mng so ON sd.order_no = so.order_no AND sd.company_code = so.company_code
|
||||
WHERE sd.company_code = $1
|
||||
AND sd.part_code IS NOT NULL AND sd.part_code != ''
|
||||
AND COALESCE(sd.qty::numeric, 0) > 0
|
||||
),
|
||||
item_info_dedup AS (
|
||||
SELECT DISTINCT ON (item_number)
|
||||
@@ -161,12 +163,12 @@ export async function getOrderSummary(
|
||||
LEFT JOIN plan_info pi ON os.item_code = pi.item_code
|
||||
LEFT JOIN item_info_dedup ilt ON os.item_code = ilt.item_number
|
||||
${options?.excludePlanned
|
||||
? ""
|
||||
: `WHERE GREATEST(
|
||||
? `WHERE GREATEST(
|
||||
os.total_balance_qty + COALESCE(si.safety_stock, 0) - COALESCE(si.current_stock, 0)
|
||||
- COALESCE(pi.existing_plan_qty, 0) - COALESCE(pi.in_progress_qty, 0),
|
||||
0
|
||||
) > 0`}
|
||||
) > 0`
|
||||
: ""}
|
||||
ORDER BY os.item_code
|
||||
`;
|
||||
|
||||
@@ -184,6 +186,7 @@ export async function getOrderSummary(
|
||||
FROM sales_order_mng so
|
||||
WHERE ${whereClause}
|
||||
AND so.part_code IS NOT NULL AND so.part_code != ''
|
||||
AND COALESCE(so.order_qty::numeric, 0) > 0
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM sales_order_detail sd
|
||||
WHERE sd.order_no = so.order_no AND sd.company_code = so.company_code
|
||||
@@ -194,6 +197,7 @@ export async function getOrderSummary(
|
||||
INNER JOIN sales_order_mng so ON sd.order_no = so.order_no AND sd.company_code = so.company_code
|
||||
WHERE sd.company_code = $1
|
||||
AND sd.part_code IS NOT NULL AND sd.part_code != ''
|
||||
AND COALESCE(sd.qty::numeric, 0) > 0
|
||||
),
|
||||
os AS (
|
||||
SELECT part_code AS item_code, SUM(balance_qty) AS total_balance_qty
|
||||
@@ -220,12 +224,12 @@ export async function getOrderSummary(
|
||||
LEFT JOIN si ON os.item_code = si.item_code
|
||||
LEFT JOIN pi ON os.item_code = pi.item_code
|
||||
${options?.excludePlanned
|
||||
? ""
|
||||
: `WHERE GREATEST(
|
||||
? `WHERE GREATEST(
|
||||
os.total_balance_qty + COALESCE(si.safety_stock, 0) - COALESCE(si.current_stock, 0)
|
||||
- COALESCE(pi.existing_plan_qty, 0) - COALESCE(pi.in_progress_qty, 0),
|
||||
0
|
||||
) > 0`};
|
||||
) > 0`
|
||||
: ""};
|
||||
`;
|
||||
const countRes = await pool.query(countQuery, params);
|
||||
total = countRes.rows[0]?.total ?? 0;
|
||||
|
||||
Reference in New Issue
Block a user