diff --git a/app.py b/app.py index fba31a9..5ff6f63 100644 --- a/app.py +++ b/app.py @@ -1102,7 +1102,11 @@ def get_stock_ledger(): s.name as supplier_name, CASE WHEN sl.event_type = 'PURCHASE' OR sl.event_type = 'RECEIPT' THEN pr.receipt_no - WHEN sl.event_type = 'CONSUME' THEN COALESCE(c.prescription_no, '조제#' || c.compound_id) + WHEN sl.event_type = 'CONSUME' THEN + CASE + WHEN c.prescription_no IS NOT NULL THEN c.prescription_no || ' (' || COALESCE(f.formula_name, '직접조제') || ')' + ELSE '조제#' || c.compound_id || ' (' || COALESCE(f.formula_name, '직접조제') || ')' + END ELSE NULL END as reference_no, CASE @@ -1116,6 +1120,7 @@ def get_stock_ledger(): LEFT JOIN purchase_receipts pr ON sl.reference_table = 'purchase_receipts' AND sl.reference_id = pr.receipt_id LEFT JOIN compounds c ON sl.reference_table = 'compounds' AND sl.reference_id = c.compound_id LEFT JOIN patients p ON c.patient_id = p.patient_id + LEFT JOIN formulas f ON c.formula_id = f.formula_id WHERE sl.herb_item_id = ? ORDER BY sl.event_time DESC LIMIT ? @@ -1136,7 +1141,11 @@ def get_stock_ledger(): s.name as supplier_name, CASE WHEN sl.event_type = 'PURCHASE' OR sl.event_type = 'RECEIPT' THEN pr.receipt_no - WHEN sl.event_type = 'CONSUME' THEN COALESCE(c.prescription_no, '조제#' || c.compound_id) + WHEN sl.event_type = 'CONSUME' THEN + CASE + WHEN c.prescription_no IS NOT NULL THEN c.prescription_no || ' (' || COALESCE(f.formula_name, '직접조제') || ')' + ELSE '조제#' || c.compound_id || ' (' || COALESCE(f.formula_name, '직접조제') || ')' + END ELSE NULL END as reference_no, CASE @@ -1150,6 +1159,7 @@ def get_stock_ledger(): LEFT JOIN purchase_receipts pr ON sl.reference_table = 'purchase_receipts' AND sl.reference_id = pr.receipt_id LEFT JOIN compounds c ON sl.reference_table = 'compounds' AND sl.reference_id = c.compound_id LEFT JOIN patients p ON c.patient_id = p.patient_id + LEFT JOIN formulas f ON c.formula_id = f.formula_id ORDER BY sl.event_time DESC LIMIT ? """, (limit,))