jashdoshi77 commited on
Commit
2e7546f
Β·
1 Parent(s): 600d2a0

status closed added

Browse files
Files changed (1) hide show
  1. ai/signatures.py +39 -8
ai/signatures.py CHANGED
@@ -88,11 +88,29 @@ class AnalyzeAndPlan(dspy.Signature):
88
  β†’ NEVER sum gold_amount + diamond_amount from PO line tables β€” that misses labour.
89
 
90
  ══════════════════════════════════════════════════════════════
91
- RULE 2 β€” STATUS FILTERING
92
  ══════════════════════════════════════════════════════════════
93
- For ALL revenue, sales, AOV, and financial metrics:
94
- β†’ WHERE status = 'closed' on sales_table_v2_sales_order
95
- For product catalog or inventory questions: no status filter needed.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
  ══════════════════════════════════════════════════════════════
98
  RULE 1.5 β€” AGGREGATION GRANULARITY (CRITICAL)
@@ -249,10 +267,23 @@ class SQLGeneration(dspy.Signature):
249
  - NEVER add gold_amount + diamond_amount or any component columns β€”
250
  that always gives the WRONG answer (misses labour, taxes, etc.)
251
 
252
- 6. CORRECT FORMULAS:
253
- - Revenue: SELECT SUM(total_amount) FROM sales_table_v2_sales_order WHERE status = 'closed'
254
- - AOV: SELECT AVG(total_amount) FROM sales_table_v2_sales_order WHERE status = 'closed'
255
- - Per-product revenue: SUM(line_total) FROM sales_order_line_pricing (no extra JOIN needed)
 
 
 
 
 
 
 
 
 
 
 
 
 
256
 
257
  7. DATE FILTERING (order_date is TEXT 'YYYY-MM-DD'):
258
  - Use the EXACT year values from the [CONTEXT] block in the question.
 
88
  β†’ NEVER sum gold_amount + diamond_amount from PO line tables β€” that misses labour.
89
 
90
  ══════════════════════════════════════════════════════════════
91
+ RULE 2 β€” STATUS FILTERING (DEFAULT = 'closed', ALWAYS)
92
  ══════════════════════════════════════════════════════════════
93
+ For ANY query that touches sales_table_v2_sales_order, the DEFAULT
94
+ is to always filter WHERE status = 'closed'.
95
+
96
+ ONLY skip or change this filter when the question EXPLICITLY mentions
97
+ a different status by name β€” e.g. "pending orders", "open orders",
98
+ "cancelled orders", "all orders regardless of status".
99
+ If the question does not mention any status word, use status = 'closed'.
100
+
101
+ This applies to every type of sales_order query:
102
+ β€’ Revenue, AOV, total sales, order value
103
+ β€’ Order count ("how many orders", "number of orders")
104
+ β€’ Top customers, top products, top SKUs by any metric
105
+ β€’ Any SUM, AVG, COUNT on total_amount, line_total, or component costs
106
+ β€’ Any JOIN that starts from or passes through sales_order
107
+ β€’ Component cost queries from sales_order_line_pricing
108
+ (join back to sales_order and apply status = 'closed')
109
+
110
+ NEVER apply status = 'closed' to:
111
+ β€’ Purchase order tables (they have a separate status column)
112
+ β€’ Inventory tables
113
+ β€’ Customer master / product lookup tables (no status column)
114
 
115
  ══════════════════════════════════════════════════════════════
116
  RULE 1.5 β€” AGGREGATION GRANULARITY (CRITICAL)
 
267
  - NEVER add gold_amount + diamond_amount or any component columns β€”
268
  that always gives the WRONG answer (misses labour, taxes, etc.)
269
 
270
+ 6. STATUS = 'closed' IS THE DEFAULT FOR ALL SALES ORDER QUERIES:
271
+ Unless the question explicitly mentions a different status (e.g. "pending",
272
+ "open", "cancelled", "all orders"), ALWAYS add WHERE so.status = 'closed'.
273
+ This is not optional β€” omitting it returns incomplete/incorrect data.
274
+
275
+ Correct formulas:
276
+ - Revenue: SUM(so.total_amount) ... WHERE so.status = 'closed'
277
+ - AOV: AVG(so.total_amount) ... WHERE so.status = 'closed'
278
+ - Order count: COUNT(DISTINCT so.so_id) ... WHERE so.status = 'closed'
279
+ - Per-product rev: SUM(lp.line_total) FROM sales_order_line_pricing lp
280
+ JOIN sales_table_v2_sales_order so ON so.so_id = sol.so_id
281
+ WHERE so.status = 'closed'
282
+ - Component costs: SUM(lp.diamond_amount_per_unit * lp.quantity)
283
+ FROM sales_order_line_pricing lp
284
+ JOIN sales_table_v2_sales_order_line sol ON lp.sol_id = sol.sol_id
285
+ JOIN sales_table_v2_sales_order so ON sol.so_id = so.so_id
286
+ WHERE so.status = 'closed'
287
 
288
  7. DATE FILTERING (order_date is TEXT 'YYYY-MM-DD'):
289
  - Use the EXACT year values from the [CONTEXT] block in the question.