From af531372dad3dda5b78da13502cc8b73ae5b8768 Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Sun, 26 Sep 2021 15:46:13 +0530 Subject: [PATCH 1/9] fix: setting of gain/loss if party account is in company currency (cherry picked from commit 64efe8bf15fb5a0fb61bd152399e069818314b5d) --- erpnext/controllers/accounts_controller.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 821814e7dce..d1f7d485b81 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -690,13 +690,17 @@ class AccountsController(TransactionBase): .format(d.reference_name, d.against_order)) def set_advance_gain_or_loss(self): - if not self.get("advances"): + if self.get('conversion_rate') == 1 or not self.get("advances"): + return + + is_purchase_invoice = self.doctype == 'Purchase Invoice' + party_account = self.credit_to if is_purchase_invoice else self.debit_to + if get_account_currency(party_account) != self.currency: return for d in self.get("advances"): advance_exchange_rate = d.ref_exchange_rate - if (d.allocated_amount and self.conversion_rate != 1 - and self.conversion_rate != advance_exchange_rate): + if (d.allocated_amount and self.conversion_rate != advance_exchange_rate): base_allocated_amount_in_ref_rate = advance_exchange_rate * d.allocated_amount base_allocated_amount_in_inv_rate = self.conversion_rate * d.allocated_amount From 677a59b005df1e7559d592afcb8e622bafff5b57 Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Sun, 26 Sep 2021 15:51:03 +0530 Subject: [PATCH 2/9] chore: hide exchange gain loss if empty (cherry picked from commit dd2d039ca8a83141da2038791628634c816f7718) --- .../payment_entry_reference/payment_entry_reference.json | 2 +- .../purchase_invoice_advance/purchase_invoice_advance.json | 4 +++- .../doctype/sales_invoice_advance/sales_invoice_advance.json | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/doctype/payment_entry_reference/payment_entry_reference.json b/erpnext/accounts/doctype/payment_entry_reference/payment_entry_reference.json index 43eb0b6e2aa..8d8d1a3f20f 100644 --- a/erpnext/accounts/doctype/payment_entry_reference/payment_entry_reference.json +++ b/erpnext/accounts/doctype/payment_entry_reference/payment_entry_reference.json @@ -103,7 +103,7 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2021-04-21 13:30:11.605388", + "modified": "2021-09-26 15:49:28.018334", "modified_by": "Administrator", "module": "Accounts", "name": "Payment Entry Reference", diff --git a/erpnext/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.json b/erpnext/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.json index 63dfff8921f..9fcbf5c6339 100644 --- a/erpnext/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.json +++ b/erpnext/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.json @@ -97,6 +97,7 @@ "width": "100px" }, { + "depends_on": "exchange_gain_loss", "fieldname": "exchange_gain_loss", "fieldtype": "Currency", "label": "Exchange Gain/Loss", @@ -104,6 +105,7 @@ "read_only": 1 }, { + "depends_on": "exchange_gain_loss", "fieldname": "ref_exchange_rate", "fieldtype": "Float", "label": "Reference Exchange Rate", @@ -115,7 +117,7 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2021-04-20 16:26:53.820530", + "modified": "2021-09-26 15:47:28.167371", "modified_by": "Administrator", "module": "Accounts", "name": "Purchase Invoice Advance", diff --git a/erpnext/accounts/doctype/sales_invoice_advance/sales_invoice_advance.json b/erpnext/accounts/doctype/sales_invoice_advance/sales_invoice_advance.json index 29422d68cf6..f92b57a45e1 100644 --- a/erpnext/accounts/doctype/sales_invoice_advance/sales_invoice_advance.json +++ b/erpnext/accounts/doctype/sales_invoice_advance/sales_invoice_advance.json @@ -98,6 +98,7 @@ "width": "120px" }, { + "depends_on": "exchange_gain_loss", "fieldname": "exchange_gain_loss", "fieldtype": "Currency", "label": "Exchange Gain/Loss", @@ -105,6 +106,7 @@ "read_only": 1 }, { + "depends_on": "exchange_gain_loss", "fieldname": "ref_exchange_rate", "fieldtype": "Float", "label": "Reference Exchange Rate", @@ -116,7 +118,7 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2021-06-04 20:25:49.832052", + "modified": "2021-09-26 15:47:46.911595", "modified_by": "Administrator", "module": "Accounts", "name": "Sales Invoice Advance", From 2108bf9f739c1d2602f83b04c7253c0f9c52dbab Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Sun, 26 Sep 2021 16:00:28 +0530 Subject: [PATCH 3/9] fix: cost center in exchange gain loss gl entry (cherry picked from commit 78ad50efc2aeaeaa8900419dba5c2e8533d0ba39) --- erpnext/controllers/accounts_controller.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index d1f7d485b81..835a16f77f6 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -719,7 +719,7 @@ class AccountsController(TransactionBase): gain_loss_account = frappe.db.get_value('Company', self.company, 'exchange_gain_loss_account') if not gain_loss_account: - frappe.throw(_("Please set Default Exchange Gain/Loss Account in Company {}") + frappe.throw(_("Please set default Exchange Gain/Loss Account in Company {}") .format(self.get('company'))) account_currency = get_account_currency(gain_loss_account) if account_currency != self.company_currency: @@ -738,7 +738,7 @@ class AccountsController(TransactionBase): "against": party, dr_or_cr + "_in_account_currency": abs(d.exchange_gain_loss), dr_or_cr: abs(d.exchange_gain_loss), - "cost_center": self.cost_center, + "cost_center": self.cost_center or erpnext.get_default_cost_center(self.company), "project": self.project }, item=d) ) From 19b06b074532bbdc7481cf346b43aeb533bac740 Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Sun, 26 Sep 2021 16:58:55 +0530 Subject: [PATCH 4/9] patch: invalid gain loss gl entry (cherry picked from commit beebfb323ac50c72df20b6c447087fec84b9c80d) --- erpnext/patches.txt | 1 + .../modify_invalid_gain_loss_gl_entries.py | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 46a30f37f72..c83f39708b5 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -315,3 +315,4 @@ erpnext.patches.v13_0.make_homepage_products_website_items erpnext.patches.v13_0.update_dates_in_tax_withholding_category erpnext.patches.v13_0.replace_supplier_item_group_with_party_specific_item erpnext.patches.v13_0.create_accounting_dimensions_in_pos_doctypes +erpnext.patches.v13_0.modify_invalid_gain_loss_gl_entries \ No newline at end of file diff --git a/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py b/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py new file mode 100644 index 00000000000..4781ccb4b2e --- /dev/null +++ b/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py @@ -0,0 +1,40 @@ +from __future__ import unicode_literals + +import frappe + + +def execute(): + purchase_invoices = frappe.db.sql(""" + select + parenttype as type, parent as name + from + `tabPurchase Invoice Advance` + where + ref_exchange_rate = 1 + and docstatus = 1 + and ifnull(exchange_gain_loss, '') != '' + group by + parent + """, as_dict=1) + + sales_invoices = frappe.db.sql(""" + select + parenttype as type, parent as name + from + `tabSales Invoice Advance` + where + ref_exchange_rate = 1 + and docstatus = 1 + and ifnull(exchange_gain_loss, '') != '' + group by + parent + """, as_dict=1) + + for invoice in purchase_invoices + sales_invoices: + doc = frappe.get_doc(invoice.type, invoice.name) + doc.docstatus = 2 + doc.make_gl_entries() + for advance in doc.advances: + advance.db_set('exchange_gain_loss', 0, False) + doc.docstatus = 1 + doc.make_gl_entries() \ No newline at end of file From 705f094a1a943a8bcdc1073d462ea6b1951b826a Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Sun, 26 Sep 2021 17:07:13 +0530 Subject: [PATCH 5/9] chore: hide exchange gain loss if empty (cherry picked from commit e6b4d33f4be5bd359134aa8fd6f7a3bb9edf35ec) --- .../payment_entry_reference/payment_entry_reference.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/payment_entry_reference/payment_entry_reference.json b/erpnext/accounts/doctype/payment_entry_reference/payment_entry_reference.json index 8d8d1a3f20f..8961167f018 100644 --- a/erpnext/accounts/doctype/payment_entry_reference/payment_entry_reference.json +++ b/erpnext/accounts/doctype/payment_entry_reference/payment_entry_reference.json @@ -93,6 +93,7 @@ "options": "Payment Term" }, { + "depends_on": "exchange_gain_loss", "fieldname": "exchange_gain_loss", "fieldtype": "Currency", "label": "Exchange Gain/Loss", @@ -103,7 +104,7 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2021-09-26 15:49:28.018334", + "modified": "2021-09-26 17:06:55.597389", "modified_by": "Administrator", "module": "Accounts", "name": "Payment Entry Reference", From 8a8921fb5b607b7323522f0775126595e84f9740 Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Sun, 26 Sep 2021 17:39:40 +0530 Subject: [PATCH 6/9] fix: unknown column 'ref_exchange_rate' (cherry picked from commit 18e5d59d24dc1981c2e2df2facf8517988e621e0) --- .../patches/v13_0/modify_invalid_gain_loss_gl_entries.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py b/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py index 4781ccb4b2e..81b72cf40fb 100644 --- a/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py +++ b/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py @@ -4,6 +4,9 @@ import frappe def execute(): + frappe.reload_doc('accounts', 'doctype', 'purchase_invoice_advance') + frappe.reload_doc('accounts', 'doctype', 'sales_invoice_advance') + purchase_invoices = frappe.db.sql(""" select parenttype as type, parent as name @@ -35,6 +38,7 @@ def execute(): doc.docstatus = 2 doc.make_gl_entries() for advance in doc.advances: - advance.db_set('exchange_gain_loss', 0, False) + if advance.ref_exchange_rate == 1: + advance.db_set('exchange_gain_loss', 0, False) doc.docstatus = 1 doc.make_gl_entries() \ No newline at end of file From 7a8ff85308aad1178cbad6abbd9ff4a7dfef3779 Mon Sep 17 00:00:00 2001 From: Saqib Date: Mon, 27 Sep 2021 12:20:16 +0530 Subject: [PATCH 7/9] chore: log modified invoices (cherry picked from commit 30f59b09fdc1c44999d996407f4c2a283270a4ab) --- erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py b/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py index 81b72cf40fb..bcd7f13c16f 100644 --- a/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py +++ b/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals +import json import frappe @@ -33,6 +34,9 @@ def execute(): parent """, as_dict=1) +if purchase_invoices + sales_invoices: + frappe.log_error(json.dumps(purchase_invoices + sales_invoices, indent=2), title="Patch Log") + for invoice in purchase_invoices + sales_invoices: doc = frappe.get_doc(invoice.type, invoice.name) doc.docstatus = 2 From 904c592978197dcac010370a37293665d1764f44 Mon Sep 17 00:00:00 2001 From: Saqib Date: Mon, 27 Sep 2021 12:21:06 +0530 Subject: [PATCH 8/9] fix: indentation (cherry picked from commit 1f8ad7241861b16d99d9017335b2720e674d2d81) --- erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py b/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py index bcd7f13c16f..fab8d59884a 100644 --- a/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py +++ b/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py @@ -34,8 +34,8 @@ def execute(): parent """, as_dict=1) -if purchase_invoices + sales_invoices: - frappe.log_error(json.dumps(purchase_invoices + sales_invoices, indent=2), title="Patch Log") + if purchase_invoices + sales_invoices: + frappe.log_error(json.dumps(purchase_invoices + sales_invoices, indent=2), title="Patch Log") for invoice in purchase_invoices + sales_invoices: doc = frappe.get_doc(invoice.type, invoice.name) From 1063ced7cd8c7478cb22011ab9207a267c3551e3 Mon Sep 17 00:00:00 2001 From: Saqib Date: Mon, 27 Sep 2021 12:26:18 +0530 Subject: [PATCH 9/9] fix: linting errors (cherry picked from commit 711395db22617757df612f6bdf179c969674748b) --- erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py | 1 + 1 file changed, 1 insertion(+) diff --git a/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py b/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py index fab8d59884a..fa8a86437d0 100644 --- a/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py +++ b/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py @@ -1,6 +1,7 @@ from __future__ import unicode_literals import json + import frappe