From 70cb6da547abde3ad493f54f678e86c6f69380bc Mon Sep 17 00:00:00 2001 From: Ali <010109ali@gmail.com> Date: Thu, 25 Jun 2026 10:04:07 +0000 Subject: [PATCH] fix(report): count sale price only for stock-moving agricultural sales Sold in period (sale price) now sums Sales Invoice items only when the sale produced an outgoing Stock Ledger entry, so it stays consistent with COGS/closing. A sale that does not update stock contributes 0 instead of inflating the sale figure with no matching cost. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../agricultural_goods_stock_report.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/taxes_az/taxes_az/report/agricultural_goods_stock_report/agricultural_goods_stock_report.py b/taxes_az/taxes_az/report/agricultural_goods_stock_report/agricultural_goods_stock_report.py index 39e9dfa..e46f82c 100644 --- a/taxes_az/taxes_az/report/agricultural_goods_stock_report/agricultural_goods_stock_report.py +++ b/taxes_az/taxes_az/report/agricultural_goods_stock_report/agricultural_goods_stock_report.py @@ -159,7 +159,12 @@ def _movements(company, from_date, to_date): def _sold_sale_price(company, from_date, to_date): - """Net sale value (purchase price excluded) of agricultural items sold.""" + """Net sale value of agricultural items that actually moved stock. + + Only Sales Invoice item rows that produced an outgoing Stock Ledger entry + are counted, so the figure stays consistent with COGS / closing: a sale + that did not update stock contributes 0 (just like it contributes no COGS). + """ params = {} conditions = "si.docstatus = 1 AND it.agricultural_goods = 1" conditions += _company_cond(company, params, "si") @@ -177,6 +182,14 @@ def _sold_sale_price(company, from_date, to_date): INNER JOIN `tabSales Invoice` si ON si.name = sii.parent INNER JOIN `tabItem` it ON it.name = sii.item_code WHERE {conditions} + AND EXISTS ( + SELECT 1 FROM `tabStock Ledger Entry` sle + WHERE sle.voucher_type = 'Sales Invoice' + AND sle.voucher_no = si.name + AND sle.item_code = sii.item_code + AND sle.is_cancelled = 0 + AND sle.stock_value_difference < 0 + ) """, params) return flt(result[0][0]) if result else 0.0