From 56bc26aeccf8a208dd7adf4607c364acd1e2fb48 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Tue, 18 Mar 2025 11:22:46 +0530 Subject: [PATCH 1/4] 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): From 36ffc2ee67096d726933b6f54ee3f6448593ee69 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Tue, 18 Mar 2025 11:43:16 +0530 Subject: [PATCH 2/4] fix: patch (cherry picked from commit 7e669c07281a1275a2c254072009af31b626ea86) --- .../patches/v15_0/recalculate_amount_difference_field.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/erpnext/patches/v15_0/recalculate_amount_difference_field.py b/erpnext/patches/v15_0/recalculate_amount_difference_field.py index 2891ac16c44..0b7ab2ec4fe 100644 --- a/erpnext/patches/v15_0/recalculate_amount_difference_field.py +++ b/erpnext/patches/v15_0/recalculate_amount_difference_field.py @@ -27,11 +27,7 @@ def execute(): table.qty, parent.conversion_rate, ) - .where( - (table.amount_difference_with_purchase_invoice != 0) - & (table.docstatus == 1) - & (parent.company == company) - ) + .where((table.docstatus == 1) & (parent.company == company)) ) posting_date = "2024-04-01" @@ -121,6 +117,7 @@ def get_billed_qty_against_purchase_receipt(pr_names): frappe.qb.from_(table) .select(table.pr_detail, Sum(table.qty).as_("qty")) .where((table.pr_detail.isin(pr_names)) & (table.docstatus == 1)) + .groupby(table.pr_detail) ) invoice_data = query.run(as_list=1) From ec1a3a1e6b4fcecda6e89abb46dca065cc33fdef Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Wed, 19 Mar 2025 13:04:08 +0530 Subject: [PATCH 3/4] fix: take function call outside loop (cherry picked from commit b3c400f998ca030b8d3e705bbd0a676603e133fa) --- .../purchase_receipt/purchase_receipt.py | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index 168713a66da..48e161980db 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -1083,6 +1083,9 @@ def update_billing_percentage(pr_doc, update_modified=True, adjust_incoming_rate total_amount, total_billed_amount = 0, 0 item_wise_returned_qty = get_item_wise_returned_qty(pr_doc) + if adjust_incoming_rate: + item_wise_billed_qty = get_billed_qty_against_purchase_receipt(pr_doc) + for item in pr_doc.items: returned_qty = flt(item_wise_returned_qty.get(item.name)) returned_amount = flt(returned_qty) * flt(item.rate) @@ -1102,10 +1105,15 @@ def update_billing_percentage(pr_doc, update_modified=True, adjust_incoming_rate if adjust_incoming_rate: adjusted_amt = 0.0 - billed_qty = get_billed_qty_against_item(item.name) - 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 + 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 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) @@ -1121,14 +1129,20 @@ def update_billing_percentage(pr_doc, update_modified=True, adjust_incoming_rate adjust_incoming_rate_for_pr(pr_doc) -def get_billed_qty_against_item(name): +def get_billed_qty_against_purchase_receipt(pr_doc): + pr_names = [d.name for d in pr_doc.items] table = frappe.qb.DocType("Purchase Invoice Item") query = ( frappe.qb.from_(table) - .select(fn.Sum(table.qty).as_("qty")) - .where((table.pr_detail == name) & (table.docstatus == 1)) + .select(table.pr_detail, fn.Sum(table.qty).as_("qty")) + .where((table.pr_detail.isin(pr_names)) & (table.docstatus == 1)) + .groupby(table.pr_detail) ) - return query.run(as_dict=True)[0].get("qty", 0) + invoice_data = query.run(as_list=1) + + if not invoice_data: + return frappe._dict() + return frappe._dict(invoice_data) def adjust_incoming_rate_for_pr(doc): From 5b802ae527e8e1fe70b893be9d4c5de068ce6833 Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Wed, 19 Mar 2025 15:35:26 +0530 Subject: [PATCH 4/4] chore: fix conflicts --- erpnext/patches.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index dda1b11859d..835ee5b6133 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -395,13 +395,9 @@ 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