From 041335f318c510dac7f56d2f7998135ff559fd58 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Thu, 6 Mar 2025 12:58:53 +0530 Subject: [PATCH 1/3] fix: recalculate_amount_difference_field patch (cherry picked from commit 0492b941ffc576b81116b486c7553c2bfe729391) # Conflicts: # erpnext/patches/v15_0/recalculate_amount_difference_field.py (cherry picked from commit f247f02e49e5bb22585e81e3c687e052ff441361) --- .../recalculate_amount_difference_field.py | 101 ++++++++++++++++-- 1 file changed, 90 insertions(+), 11 deletions(-) diff --git a/erpnext/patches/v15_0/recalculate_amount_difference_field.py b/erpnext/patches/v15_0/recalculate_amount_difference_field.py index fa45211be94..dda92ecf1e8 100644 --- a/erpnext/patches/v15_0/recalculate_amount_difference_field.py +++ b/erpnext/patches/v15_0/recalculate_amount_difference_field.py @@ -1,12 +1,13 @@ import frappe from frappe.query_builder.functions import Sum -from frappe.utils import flt +from frappe.utils import flt, getdate from erpnext.accounts.utils import get_fiscal_year from erpnext.stock.doctype.purchase_receipt.purchase_receipt import adjust_incoming_rate_for_pr def execute(): +<<<<<<< HEAD table = frappe.qb.DocType("Purchase Receipt Item") parent = frappe.qb.DocType("Purchase Receipt") query = ( @@ -50,20 +51,98 @@ def execute(): adjusted_amt = flt( adjusted_amt * flt(item.conversion_rate), precision, +======= + for company in frappe.get_all("Company", pluck="name"): + table = frappe.qb.DocType("Purchase Receipt Item") + parent = frappe.qb.DocType("Purchase Receipt") + query = ( + frappe.qb.from_(table) + .join(parent) + .on(table.parent == parent.name) + .select( + table.parent, + table.name, + table.amount, + table.billed_amt, + table.amount_difference_with_purchase_invoice, + table.rate, + table.qty, + parent.conversion_rate, +>>>>>>> 0492b941ff (fix: recalculate_amount_difference_field patch) ) + .where( + (table.amount_difference_with_purchase_invoice != 0) + & (table.docstatus == 1) + & (parent.company == company) + ) + ) - if adjusted_amt != item.amount_difference_with_purchase_invoice: - frappe.db.set_value( - "Purchase Receipt Item", - item.name, - "amount_difference_with_purchase_invoice", - adjusted_amt, - update_modified=False, + posting_date = "2024-04-01" + + # Get the last accounting period end date + accounting_period = frappe.get_all( + "Accounting Period", {"company": company}, ["end_date"], order_by="end_date desc", limit=1 + ) + if ( + accounting_period + and accounting_period[0].end_date + and getdate(accounting_period[0].end_date) > getdate(posting_date) + ): + posting_date = accounting_period[0].end_date + + # Get the last period closing voucher end date + period_closing_voucher = frappe.get_all( + "Period Closing Voucher", + {"company": company, "docstatus": 1}, + ["period_end_date"], + order_by="period_end_date desc", + limit=1, + ) + if ( + period_closing_voucher + and period_closing_voucher[0].period_end_date + and getdate(period_closing_voucher[0].period_end_date) > getdate(posting_date) + ): + posting_date = period_closing_voucher[0].period_end_date + + fiscal_year = get_fiscal_year(frappe.utils.datetime.date.today(), raise_on_missing=False) + if fiscal_year and getdate(fiscal_year[1]) > getdate(posting_date): + posting_date = fiscal_year[1] + query = query.where(parent.posting_date > posting_date) + + if result := query.run(as_dict=True): + item_wise_billed_qty = get_billed_qty_against_purchase_receipt([item.name for item in result]) + + purchase_receipts = set() + precision = frappe.get_precision("Purchase Receipt Item", "amount") + for item in result: + adjusted_amt = 0.0 + + 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(item.conversion_rate), + precision, ) - purchase_receipts.add(item.parent) - for pr in purchase_receipts: - adjust_incoming_rate_for_pr(frappe.get_doc("Purchase Receipt", pr)) + if adjusted_amt != item.amount_difference_with_purchase_invoice: + frappe.db.set_value( + "Purchase Receipt Item", + item.name, + "amount_difference_with_purchase_invoice", + adjusted_amt, + update_modified=False, + ) + purchase_receipts.add(item.parent) + + for pr in purchase_receipts: + adjust_incoming_rate_for_pr(frappe.get_doc("Purchase Receipt", pr)) def get_billed_qty_against_purchase_receipt(pr_names): From 12bf31df87c9b09f7c1acf31102705faebf125b1 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Thu, 6 Mar 2025 13:11:31 +0530 Subject: [PATCH 2/3] fix: check if set_landed_cost_based_on_purchase_invoice_rate is enabled before running patch (cherry picked from commit 95d197693151610bda2926db46f70e1cf9f6838c) # Conflicts: # erpnext/patches/v15_0/recalculate_amount_difference_field.py (cherry picked from commit 7047fe26812fbb48d80e3a24f438a9c1c5d8683f) --- .../patches/v15_0/recalculate_amount_difference_field.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/erpnext/patches/v15_0/recalculate_amount_difference_field.py b/erpnext/patches/v15_0/recalculate_amount_difference_field.py index dda92ecf1e8..d0a42e7cc5e 100644 --- a/erpnext/patches/v15_0/recalculate_amount_difference_field.py +++ b/erpnext/patches/v15_0/recalculate_amount_difference_field.py @@ -7,6 +7,7 @@ from erpnext.stock.doctype.purchase_receipt.purchase_receipt import adjust_incom def execute(): +<<<<<<< HEAD <<<<<<< HEAD table = frappe.qb.DocType("Purchase Receipt Item") parent = frappe.qb.DocType("Purchase Receipt") @@ -52,6 +53,11 @@ def execute(): adjusted_amt * flt(item.conversion_rate), precision, ======= +======= + if not frappe.db.get_single_value("Buying Settings", "set_landed_cost_based_on_purchase_invoice_rate"): + return + +>>>>>>> 95d1976931 (fix: check if set_landed_cost_based_on_purchase_invoice_rate is enabled before running patch) for company in frappe.get_all("Company", pluck="name"): table = frappe.qb.DocType("Purchase Receipt Item") parent = frappe.qb.DocType("Purchase Receipt") From 525780645a0edf5b5ea76c9a9faecf4368e0866e Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Thu, 6 Mar 2025 14:13:55 +0530 Subject: [PATCH 3/3] chore: resolve conflicts (cherry picked from commit 8e65b0ec0c0bb8e00ecc2e3b104bf4694883926c) --- .../recalculate_amount_difference_field.py | 59 +++---------------- 1 file changed, 7 insertions(+), 52 deletions(-) diff --git a/erpnext/patches/v15_0/recalculate_amount_difference_field.py b/erpnext/patches/v15_0/recalculate_amount_difference_field.py index d0a42e7cc5e..7d3d957fe62 100644 --- a/erpnext/patches/v15_0/recalculate_amount_difference_field.py +++ b/erpnext/patches/v15_0/recalculate_amount_difference_field.py @@ -7,57 +7,9 @@ from erpnext.stock.doctype.purchase_receipt.purchase_receipt import adjust_incom def execute(): -<<<<<<< HEAD -<<<<<<< HEAD - table = frappe.qb.DocType("Purchase Receipt Item") - parent = frappe.qb.DocType("Purchase Receipt") - query = ( - frappe.qb.from_(table) - .join(parent) - .on(table.parent == parent.name) - .select( - table.parent, - table.name, - table.amount, - table.billed_amt, - table.amount_difference_with_purchase_invoice, - table.rate, - table.qty, - parent.conversion_rate, - ) - .where((table.amount_difference_with_purchase_invoice != 0) & (table.docstatus == 1)) - ) - try: - if fiscal_year_dates := get_fiscal_year(frappe.utils.datetime.date.today()): - query.where(parent.posting_date.between(fiscal_year_dates[1], fiscal_year_dates[2])) - except Exception: - return - - if result := query.run(as_dict=True): - item_wise_billed_qty = get_billed_qty_against_purchase_receipt([item.name for item in result]) - - purchase_receipts = set() - precision = frappe.get_precision("Purchase Receipt Item", "amount") - for item in result: - adjusted_amt = 0.0 - - 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(item.conversion_rate), - precision, -======= -======= if not frappe.db.get_single_value("Buying Settings", "set_landed_cost_based_on_purchase_invoice_rate"): return ->>>>>>> 95d1976931 (fix: check if set_landed_cost_based_on_purchase_invoice_rate is enabled before running patch) for company in frappe.get_all("Company", pluck="name"): table = frappe.qb.DocType("Purchase Receipt Item") parent = frappe.qb.DocType("Purchase Receipt") @@ -74,7 +26,6 @@ def execute(): table.rate, table.qty, parent.conversion_rate, ->>>>>>> 0492b941ff (fix: recalculate_amount_difference_field patch) ) .where( (table.amount_difference_with_purchase_invoice != 0) @@ -111,9 +62,13 @@ def execute(): ): posting_date = period_closing_voucher[0].period_end_date - fiscal_year = get_fiscal_year(frappe.utils.datetime.date.today(), raise_on_missing=False) - if fiscal_year and getdate(fiscal_year[1]) > getdate(posting_date): - posting_date = fiscal_year[1] + try: + fiscal_year = get_fiscal_year(frappe.utils.datetime.date.today()) + except Exception: + return + else: + if fiscal_year and getdate(fiscal_year[1]) > getdate(posting_date): + posting_date = fiscal_year[1] query = query.where(parent.posting_date > posting_date) if result := query.run(as_dict=True):