From 74fda7550b4246cce49e53538638c8c8f022a69a Mon Sep 17 00:00:00 2001 From: fproldan Date: Tue, 10 Mar 2026 14:10:29 +0000 Subject: [PATCH 1/4] feat: precio de la ultima compra en FV --- erpnext/controllers/buying_controller.py | 4 ++-- erpnext/stock/doctype/item/item.py | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py index 6ea06aeae1fb..c3501b7549b6 100644 --- a/erpnext/controllers/buying_controller.py +++ b/erpnext/controllers/buying_controller.py @@ -503,7 +503,7 @@ def on_submit(self): self.process_fixed_asset() self.update_fixed_asset(field) - if self.doctype in ['Purchase Order', 'Purchase Receipt']: + if self.doctype in ['Purchase Order', 'Purchase Receipt', 'Purchase Invoice']: update_last_purchase_rate(self, is_submit = 1) def on_cancel(self): @@ -512,7 +512,7 @@ def on_cancel(self): if self.get('is_return'): return - if self.doctype in ['Purchase Order', 'Purchase Receipt']: + if self.doctype in ['Purchase Order', 'Purchase Receipt', 'Purchase Invoice']: update_last_purchase_rate(self, is_submit = 0) if self.doctype in ['Purchase Receipt', 'Purchase Invoice']: diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index fd87403f98d2..3fa9b4798fb1 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -859,7 +859,21 @@ def get_last_purchase_details(item_code, doc_name=None, conversion_rate=1.0): purchase_date = purchase_receipt_date else: - return frappe._dict() + last_purchase_invoice = frappe.db.sql("""\ + select pi.name, pi.posting_date, pi.posting_time, pi.conversion_rate, + pi_item.conversion_factor, pi_item.base_price_list_rate, pi_item.discount_percentage, + pi_item.base_rate, pi_item.base_net_rate + from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` pi_item + where pi.docstatus = 1 and pi_item.item_code = %s and pi.name != %s and + pi.name = pi_item.parent + order by pi.posting_date desc, pi.posting_time desc, pi.name desc + limit 1""", (item_code, cstr(doc_name)), as_dict=1) + + if last_purchase_invoice: + last_purchase = last_purchase_invoice[0] + purchase_date = getdate(last_purchase.posting_date) + else: + return frappe._dict() conversion_factor = flt(last_purchase.conversion_factor) out = frappe._dict({ From 48898f5ac28ba1a335f0e38a1c61e8fc59edd27d Mon Sep 17 00:00:00 2001 From: fproldan Date: Tue, 10 Mar 2026 14:42:58 +0000 Subject: [PATCH 2/4] patch --- erpnext/patches.txt | 3 ++- erpnext/patches/v13_0/update_last_purchase_rate.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 erpnext/patches/v13_0/update_last_purchase_rate.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index cf4b4be25b97..78b7e60b3725 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -367,4 +367,5 @@ erpnext.patches.v13_0.remove_old_hr_domains erpnext.patches.v13_0.release_1_8 erpnext.patches.v13_0.release_1_9 erpnext.patches.v13_0.naming_series_subs -erpnext.patches.v13_0.naming_series_subs_2 \ No newline at end of file +erpnext.patches.v13_0.naming_series_subs_2 +erpnext.patches.v13_0.update_last_purchase_rate \ No newline at end of file diff --git a/erpnext/patches/v13_0/update_last_purchase_rate.py b/erpnext/patches/v13_0/update_last_purchase_rate.py new file mode 100644 index 000000000000..a01284f37ddf --- /dev/null +++ b/erpnext/patches/v13_0/update_last_purchase_rate.py @@ -0,0 +1,11 @@ +import frappe +from erpnext.buying.utils import update_last_purchase_rate + + +def execute(): + invoices = frappe.get_all("Purchase Invoice", {"docstatus": 1}, pluck="name") + for invoice in invoices: + doc = frappe.get_doc("Purchase Invoice", invoice) + update_last_purchase_rate(doc, 1) + + frappe.db.commit() \ No newline at end of file From e230f1d04c81e6fcd780dc61c7d3be0bc4bc54fa Mon Sep 17 00:00:00 2001 From: fproldan Date: Tue, 10 Mar 2026 16:43:38 +0000 Subject: [PATCH 3/4] fix --- .../patches/v13_0/update_last_purchase_rate.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/erpnext/patches/v13_0/update_last_purchase_rate.py b/erpnext/patches/v13_0/update_last_purchase_rate.py index a01284f37ddf..cb384ada2e5f 100644 --- a/erpnext/patches/v13_0/update_last_purchase_rate.py +++ b/erpnext/patches/v13_0/update_last_purchase_rate.py @@ -3,9 +3,14 @@ def execute(): - invoices = frappe.get_all("Purchase Invoice", {"docstatus": 1}, pluck="name") - for invoice in invoices: - doc = frappe.get_doc("Purchase Invoice", invoice) - update_last_purchase_rate(doc, 1) - + for item in frappe.get_all("Item", pluck="name"): + last_purchase_invoice = frappe.db.get_value( + "Purchase Invoice Item", + {"item_code": item}, + "parent", + order_by="creation desc", + ) + if last_purchase_invoice: + doc = frappe.get_doc("Purchase Invoice", last_purchase_invoice) + update_last_purchase_rate(doc, 1) frappe.db.commit() \ No newline at end of file From 021c81745f05f06dc9733ffeb027df19bd381a2f Mon Sep 17 00:00:00 2001 From: fproldan Date: Tue, 10 Mar 2026 18:13:35 +0000 Subject: [PATCH 4/4] fix: patch --- .../v13_0/update_last_purchase_rate.py | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/erpnext/patches/v13_0/update_last_purchase_rate.py b/erpnext/patches/v13_0/update_last_purchase_rate.py index cb384ada2e5f..825a6049e770 100644 --- a/erpnext/patches/v13_0/update_last_purchase_rate.py +++ b/erpnext/patches/v13_0/update_last_purchase_rate.py @@ -1,16 +1,20 @@ import frappe -from erpnext.buying.utils import update_last_purchase_rate +from frappe.utils import flt def execute(): - for item in frappe.get_all("Item", pluck="name"): - last_purchase_invoice = frappe.db.get_value( - "Purchase Invoice Item", - {"item_code": item}, - "parent", - order_by="creation desc", - ) - if last_purchase_invoice: - doc = frappe.get_doc("Purchase Invoice", last_purchase_invoice) - update_last_purchase_rate(doc, 1) - frappe.db.commit() \ No newline at end of file + for item in frappe.get_all("Item", pluck="name"): + last_purchase_invoice = frappe.db.get_value( + "Purchase Invoice Item", + {"item_code": item}, + "name", + order_by="creation desc", + ) + last_purchase_rate = None + if last_purchase_invoice: + d = frappe.get_doc("Purchase Invoice Item", last_purchase_invoice) + if flt(d.conversion_factor): + last_purchase_rate = flt(d.base_net_rate) / flt(d.conversion_factor) + + frappe.db.set_value('Item', d.item_code, 'last_purchase_rate', flt(last_purchase_rate)) + frappe.db.commit() \ No newline at end of file