From e99ae3796c00bf36890aa926de3ab4b17f82efbc Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Tue, 11 Aug 2026 19:21:38 +0530 Subject: [PATCH] fix(stock): repair existing underbilled purchase receipts The qty-sync fix corrects allocation going forward, but receipts billed before it can keep understated billed_amt, per_billed, and status. The earlier repair patch only selects over-billed PO items, so it never picks these up. Recompute every candidate PO item (multiple submitted receipts, PO-level invoicing, no invoice-created receipts). update_billed_amount_based_on_po only writes rows whose recomputed value differs, so already-correct items are untouched and the patch stays idempotent. This also converges receipts left with direct-only billed_amt by last-event-wins overwrites. --- erpnext/patches.txt | 1 + ...e_mixed_purchase_receipt_billing_status.py | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 erpnext/patches/v16_0/recalculate_mixed_purchase_receipt_billing_status.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index fd62d28a0d9..251e7eb83b7 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -511,3 +511,4 @@ erpnext.patches.v16_0.set_stock_uom_in_job_card erpnext.patches.v16_0.set_work_order_requested_and_picked_qty erpnext.patches.v16_0.rename_italy_customer_name_fields erpnext.patches.v16_0.recalculate_purchase_receipt_billing_status +erpnext.patches.v16_0.recalculate_mixed_purchase_receipt_billing_status diff --git a/erpnext/patches/v16_0/recalculate_mixed_purchase_receipt_billing_status.py b/erpnext/patches/v16_0/recalculate_mixed_purchase_receipt_billing_status.py new file mode 100644 index 00000000000..fc76d20588e --- /dev/null +++ b/erpnext/patches/v16_0/recalculate_mixed_purchase_receipt_billing_status.py @@ -0,0 +1,25 @@ +import frappe + +from erpnext.patches.v16_0.recalculate_purchase_receipt_billing_status import ( + exclude_purchase_order_items_with_invoice_created_receipts, + get_candidate_purchase_order_items, +) +from erpnext.stock.doctype.purchase_receipt.services.billing_status import ( + update_billed_amount_based_on_po, + update_billing_percentage, +) + + +def execute(): + purchase_order_items = get_candidate_purchase_order_items() + if purchase_order_items: + purchase_order_items = exclude_purchase_order_items_with_invoice_created_receipts( + purchase_order_items + ) + + if not purchase_order_items: + return + + updated_purchase_receipts = update_billed_amount_based_on_po(purchase_order_items) + for purchase_receipt in set(updated_purchase_receipts): + update_billing_percentage(frappe.get_doc("Purchase Receipt", purchase_receipt))