Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions erpnext/controllers/buying_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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']:
Expand Down
3 changes: 2 additions & 1 deletion erpnext/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
erpnext.patches.v13_0.naming_series_subs_2
erpnext.patches.v13_0.update_last_purchase_rate
20 changes: 20 additions & 0 deletions erpnext/patches/v13_0/update_last_purchase_rate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import frappe
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},
"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()
16 changes: 15 additions & 1 deletion erpnext/stock/doctype/item/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading