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) <noreply@anthropic.com>
This commit is contained in:
Ali 2026-06-25 10:04:07 +00:00
parent bd61196c84
commit 70cb6da547
1 changed files with 14 additions and 1 deletions

View File

@ -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