diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py index 43ef287854e..e29e695f48c 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.py +++ b/erpnext/buying/doctype/purchase_order/purchase_order.py @@ -807,11 +807,19 @@ def get_mapped_purchase_invoice(source_name, target_doc=None, ignore_permissions target.credit_to = get_party_account("Supplier", source.supplier, source.company) def update_item(obj, target, source_parent): - target.amount = flt(obj.amount) - flt(obj.billed_amt) - target.base_amount = target.amount * flt(source_parent.conversion_rate) - target.qty = ( - target.amount / flt(obj.rate) if (flt(obj.rate) and flt(obj.billed_amt)) else flt(obj.qty) - ) + def get_billed_qty(po_item_name): + from frappe.query_builder.functions import Sum + + table = frappe.qb.DocType("Purchase Invoice Item") + query = ( + frappe.qb.from_(table) + .select(Sum(table.qty).as_("qty")) + .where((table.docstatus == 1) & (table.po_detail == po_item_name)) + ) + return query.run(pluck="qty")[0] or 0 + + billed_qty = flt(get_billed_qty(obj.name)) + target.qty = flt(obj.qty) - billed_qty item = get_item_defaults(target.item_code, source_parent.company) item_group = get_item_group_defaults(target.item_code, source_parent.company) diff --git a/erpnext/buying/doctype/purchase_order/test_purchase_order.py b/erpnext/buying/doctype/purchase_order/test_purchase_order.py index c6ee35d6090..206cbc0000e 100644 --- a/erpnext/buying/doctype/purchase_order/test_purchase_order.py +++ b/erpnext/buying/doctype/purchase_order/test_purchase_order.py @@ -1286,6 +1286,25 @@ class TestPurchaseOrder(FrappeTestCase): self.assertFalse(po.per_billed) self.assertEqual(po.status, "To Receive and Bill") + @change_settings("Buying Settings", {"maintain_same_rate": 0}) + def test_purchase_invoice_creation_with_partial_qty(self): + po = create_purchase_order(qty=100, rate=10) + + pi = make_pi_from_po(po.name) + pi.items[0].qty = 42 + pi.items[0].rate = 7.5 + pi.submit() + + pi = make_pi_from_po(po.name) + self.assertEqual(pi.items[0].qty, 58) + self.assertEqual(pi.items[0].rate, 10) + pi.items[0].qty = 8 + pi.items[0].rate = 5 + pi.submit() + + pi = make_pi_from_po(po.name) + self.assertEqual(pi.items[0].qty, 50) + def create_po_for_sc_testing(): from erpnext.controllers.tests.test_subcontracting_controller import (