From 56bc26aeccf8a208dd7adf4607c364acd1e2fb48 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Tue, 18 Mar 2025 11:22:46 +0530 Subject: [PATCH] fix: set landed cost based on purchase invoice rate (cherry picked from commit 75ab5f2bd01bdac8ef14755fb9645495dacd0175) # Conflicts: # erpnext/patches.txt --- erpnext/patches.txt | 4 +++ .../purchase_receipt/purchase_receipt.py | 25 ++++++------------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 5e657df44dc..dda1b11859d 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -395,9 +395,13 @@ execute:frappe.db.set_single_value("Accounts Settings", "exchange_gain_loss_post erpnext.patches.v14_0.disable_add_row_in_gross_profit erpnext.patches.v15_0.set_difference_amount_in_asset_value_adjustment erpnext.patches.v14_0.update_posting_datetime +<<<<<<< HEAD erpnext.stock.doctype.stock_ledger_entry.patches.ensure_sle_indexes erpnext.patches.v15_0.update_query_report erpnext.patches.v15_0.rename_field_from_rate_difference_to_amount_difference +======= +erpnext.patches.v15_0.rename_field_from_rate_difference_to_amount_difference #2025-03-18 +>>>>>>> 75ab5f2bd0 (fix: set landed cost based on purchase invoice rate) erpnext.patches.v15_0.recalculate_amount_difference_field erpnext.patches.v15_0.rename_sla_fields #2025-03-12 erpnext.patches.v15_0.set_purchase_receipt_row_item_to_capitalization_stock_item diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index 7f2c04316e9..168713a66da 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -1102,16 +1102,10 @@ def update_billing_percentage(pr_doc, update_modified=True, adjust_incoming_rate if adjust_incoming_rate: adjusted_amt = 0.0 - item_wise_billed_qty = get_billed_qty_against_purchase_receipt(pr_doc) + billed_qty = get_billed_qty_against_item(item.name) - if ( - item.billed_amt is not None - and item.amount is not None - and item_wise_billed_qty.get(item.name) - ): - adjusted_amt = ( - flt(item.billed_amt / item_wise_billed_qty.get(item.name)) - flt(item.rate) - ) * item.qty + if item.billed_amt is not None and item.amount is not None and billed_qty: + adjusted_amt = (flt(item.billed_amt / billed_qty) - flt(item.rate)) * item.qty adjusted_amt = flt(adjusted_amt * flt(pr_doc.conversion_rate), item.precision("amount")) item.db_set("amount_difference_with_purchase_invoice", adjusted_amt, update_modified=False) @@ -1127,19 +1121,14 @@ def update_billing_percentage(pr_doc, update_modified=True, adjust_incoming_rate adjust_incoming_rate_for_pr(pr_doc) -def get_billed_qty_against_purchase_receipt(pr_doc): - pr_names = [d.name for d in pr_doc.items] +def get_billed_qty_against_item(name): table = frappe.qb.DocType("Purchase Invoice Item") query = ( frappe.qb.from_(table) - .select(table.pr_detail, fn.Sum(table.qty).as_("qty")) - .where((table.pr_detail.isin(pr_names)) & (table.docstatus == 1)) + .select(fn.Sum(table.qty).as_("qty")) + .where((table.pr_detail == name) & (table.docstatus == 1)) ) - invoice_data = query.run(as_list=1) - - if not invoice_data: - return frappe._dict() - return frappe._dict(invoice_data) + return query.run(as_dict=True)[0].get("qty", 0) def adjust_incoming_rate_for_pr(doc):