From 7803998fce95cf34a128532416bdb3c7b38ef1e4 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Tue, 11 Aug 2026 18:39:52 +0530 Subject: [PATCH] fix: keep PO billed qty in sync when allocating amount to receipts The amount-capped allocation branch reduced the remaining PO-invoiced amount but left the invoiced qty untouched. A later receipt entering the qty-proportional branch then divided by the stale qty and was under-billed: PO 10 x 500, PO-level PI for 5 (2500), PR1 qty 3 with 500 billed directly consumes 1000 (pool 2500 -> 1500, qty stuck at 5), PR2 qty 3 got 1500 * 3/5 = 900 instead of its full 1500. Scale the remaining qty by the consumed fraction so both stay proportional. Follow-up to #58021. --- .../purchase_receipt/services/billing_status.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/erpnext/stock/doctype/purchase_receipt/services/billing_status.py b/erpnext/stock/doctype/purchase_receipt/services/billing_status.py index bdfde3fad21..3f78deb5ff5 100644 --- a/erpnext/stock/doctype/purchase_receipt/services/billing_status.py +++ b/erpnext/stock/doctype/purchase_receipt/services/billing_status.py @@ -72,14 +72,15 @@ def update_billed_amount_based_on_po(po_details: list, update_modified: bool = T ) else: pending_to_bill = flt(pr_item.amount) - billed_amt_against_pr - if pending_to_bill <= billed_amt_against_po: - billed_amt_against_pr += pending_to_bill - billed_amt_against_po -= pending_to_bill - else: - billed_amt_against_pr += billed_amt_against_po - billed_amt_against_po = 0 + consumed_amt_against_po = min(pending_to_bill, billed_amt_against_po) + billed_amt_against_pr += consumed_amt_against_po - po_billed_amt_details[pr_item.purchase_order_item]["billed_amt"] = billed_amt_against_po + po_billed_amt_details[pr_item.purchase_order_item]["billed_amt"] = ( + billed_amt_against_po - consumed_amt_against_po + ) + po_billed_amt_details[pr_item.purchase_order_item]["billed_qty"] = billed_qty_against_po * ( + 1 - consumed_amt_against_po / billed_amt_against_po + ) if pr_item.billed_amt != billed_amt_against_pr: # update existing doc if possible