fix: Create common function for fetching conversion rate details of linked docs
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index 05d6322..c164868 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -517,7 +517,7 @@
if d.category in ('Valuation', 'Total and Valuation')
and flt(d.base_tax_amount_after_discount_amount)]
- exchange_rate_map, net_rate_map = self.get_purchase_receipt_details()
+ exchange_rate_map, net_rate_map = get_pr_or_pi_details(self)
for item in self.get("items"):
if flt(item.base_net_amount):
@@ -717,24 +717,6 @@
self.negative_expense_to_be_booked += flt(item.item_tax_amount, \
item.precision("item_tax_amount"))
- def get_purchase_receipt_details(self):
- purchase_receipts = []
- pr_items = []
-
- for item in self.get('items'):
- if item.get('purchase_receipt'):
- purchase_receipts.append(item.purchase_receipt)
- if item.get('pr_detail'):
- pr_items.append(item.pr_detail)
-
- exchange_rate_map = frappe._dict(frappe.get_all('Purchase Receipt', filters={'name': ('in',
- purchase_receipts)}, fields=['name', 'conversion_rate'], as_list=1))
-
- net_rate_map = frappe._dict(frappe.get_all('Purchase Receipt Item', filters={'name': ('in',
- pr_items)}, fields=['item_code', 'net_rate'], as_list=1))
-
- return exchange_rate_map, net_rate_map
-
def get_asset_gl_entry(self, gl_entries):
arbnb_account = self.get_company_default("asset_received_but_not_billed")
eiiav_account = self.get_company_default("expenses_included_in_asset_valuation")
@@ -1189,6 +1171,36 @@
if update:
self.db_set('status', self.status, update_modified = update_modified)
+# to get details of purchase invoice/receipt from which this doc was created for exchange rate difference handling
+def get_pr_or_pi_details(doc):
+ if doc.doctype == 'Purchase Invoice':
+ pr_or_pi = 'purchase_receipt'
+ items_reference = 'pr_detail'
+ pr_or_pi_doctype = 'Purchase Receipt'
+ pr_or_pi_items_table = 'Purchase Receipt Item'
+ else:
+ pr_or_pi = 'purchase_invoice'
+ items_reference = 'purchase_invoice_item'
+ pr_or_pi_doctype = 'Purchase Invoice'
+ pr_or_pi_items_table = 'Purchase Invoice Item'
+
+ purchase_receipts_or_invoices = []
+ pr_or_pi_items = []
+
+ for item in doc.get('items'):
+ if item.get(pr_or_pi):
+ purchase_receipts_or_invoices.append(item.get(pr_or_pi))
+ if item.get(items_reference):
+ pr_or_pi_items.append(item.get(items_reference))
+
+ exchange_rate_map = frappe._dict(frappe.get_all(pr_or_pi_doctype, filters={'name': ('in',
+ purchase_receipts_or_invoices)}, fields=['name', 'conversion_rate'], as_list=1))
+
+ net_rate_map = frappe._dict(frappe.get_all(pr_or_pi_items_table, filters={'name': ('in',
+ pr_or_pi_items)}, fields=['item_code', 'net_rate'], as_list=1))
+
+ return exchange_rate_map, net_rate_map
+
def get_list_context(context=None):
from erpnext.controllers.website_list_for_contact import get_list_context
list_context = get_list_context(context)
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
index 7b002b0..12fa530 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -254,6 +254,8 @@
return process_gl_map(gl_entries)
def make_item_gl_entries(self, gl_entries, warehouse_account=None):
+ from erpnext.accounts.doctype.purchase_invoice.purchase_invoice import get_pr_or_pi_details
+
stock_rbnb = self.get_company_default("stock_received_but_not_billed")
landed_cost_entries = get_item_account_wise_additional_cost(self.name)
expenses_included_in_valuation = self.get_company_default("expenses_included_in_valuation")
@@ -262,7 +264,7 @@
warehouse_with_no_account = []
stock_items = self.get_stock_items()
- exchange_rate_map, net_rate_map = self.get_purchase_invoice_details()
+ exchange_rate_map, net_rate_map = get_pr_or_pi_details(self)
for d in self.get("items"):
if d.item_code in stock_items and flt(d.valuation_rate) and flt(d.qty):
@@ -501,28 +503,6 @@
self.add_gl_entry(gl_entries, asset_account, item.cost_center, 0.0, flt(item.landed_cost_voucher_amount),
remarks, expenses_included_in_asset_valuation, project=item.project, item=item)
- def get_purchase_invoice_details(self):
- purchase_invoices = []
- pi_items = []
-
- for item in self.get('items'):
- if item.get('purchase_invoice'):
- purchase_invoices.append(item.purchase_invoice)
- if item.get('purchase_invoice_item'):
- pi_items.append(item.purchase_invoice_item)
-
- exchange_rate_map = frappe._dict(frappe.get_all('Purchase Invoice', filters={'name': ('in',
- purchase_invoices)}, fields=['name', 'conversion_rate'], as_list=1))
- print("*"*50)
- print("In get_purchase_invoice_details:")
- print("exchange_rate_map: ", exchange_rate_map)
-
- net_rate_map = frappe._dict(frappe.get_all('Purchase Invoice Item', filters={'name': ('in',
- pi_items)}, fields=['item_code', 'net_rate'], as_list=1))
- print("net_rate_map: ", net_rate_map)
-
- return exchange_rate_map, net_rate_map
-
def update_assets(self, item, valuation_rate):
assets = frappe.db.get_all('Asset',
filters={ 'purchase_receipt': self.name, 'item_code': item.item_code }