Merge pull request #49315 from frappe/mergify/bp/version-15-hotfix/pr-48875

fix: incorrect pending qty when creating sales invoice from sales order (backport #48875)
This commit is contained in:
ruthra kumar
2025-08-26 13:12:58 +05:30
committed by GitHub
2 changed files with 37 additions and 2 deletions

View File

@@ -1144,6 +1144,17 @@ def make_sales_invoice(source_name, target_doc=None, ignore_permissions=False, a
target.debit_to = get_party_account("Customer", source.customer, source.company)
def update_item(source, target, source_parent):
def get_billed_qty(so_item_name):
from frappe.query_builder.functions import Sum
table = frappe.qb.DocType("Sales Invoice Item")
query = (
frappe.qb.from_(table)
.select(Sum(table.qty).as_("qty"))
.where((table.docstatus == 1) & (table.so_detail == so_item_name))
)
return query.run(pluck="qty")[0] or 0
if source_parent.has_unit_price_items:
# 0 Amount rows (as seen in Unit Price Items) should be mapped as it is
pending_amount = flt(source.amount) - flt(source.billed_amt)
@@ -1153,8 +1164,8 @@ def make_sales_invoice(source_name, target_doc=None, ignore_permissions=False, a
target.base_amount = target.amount * flt(source_parent.conversion_rate)
target.qty = (
target.amount / flt(source.rate)
if (source.rate and source.billed_amt)
source.qty - get_billed_qty(source.name)
if (source.qty and source.billed_amt)
else (source.qty if is_unit_price_row(source) else source.qty - source.returned_qty)
)

View File

@@ -2396,6 +2396,30 @@ class TestSalesOrder(AccountsTestMixin, FrappeTestCase):
self.assertFalse(so.per_billed)
self.assertEqual(so.status, "To Deliver and Bill")
def test_pending_quantity_after_update_item_during_invoice_creation(self):
so = make_sales_order(qty=30, rate=100)
si1 = make_sales_invoice(so.name)
si1.get("items")[0].qty = 10
si1.insert()
si1.submit()
first_item_of_so = so.get("items")[0]
trans_item = json.dumps(
[
{
"item_code": first_item_of_so.item_code,
"rate": 1000,
"qty": first_item_of_so.qty,
"docname": first_item_of_so.name,
},
]
)
update_child_qty_rate("Sales Order", trans_item, so.name)
si2 = make_sales_invoice(so.name)
self.assertEqual(si2.items[0].qty, 20)
def automatically_fetch_payment_terms(enable=1):
accounts_settings = frappe.get_doc("Accounts Settings")