From 0be737914a7aaf70a55415419e31c0356b402d96 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Wed, 9 Jul 2025 14:38:56 +0530 Subject: [PATCH] fix: store currency exchange in deal & organization --- crm/fcrm/doctype/crm_deal/crm_deal.json | 9 +- crm/fcrm/doctype/crm_deal/crm_deal.py | 23 ++++ .../crm_organization/crm_organization.json | 12 +- .../crm_organization/crm_organization.py | 117 +++++++++++------- frontend/src/stores/users.js | 2 +- 5 files changed, 113 insertions(+), 50 deletions(-) diff --git a/crm/fcrm/doctype/crm_deal/crm_deal.json b/crm/fcrm/doctype/crm_deal/crm_deal.json index 599d82a8..85e516c2 100644 --- a/crm/fcrm/doctype/crm_deal/crm_deal.json +++ b/crm/fcrm/doctype/crm_deal/crm_deal.json @@ -38,6 +38,7 @@ "column_break_xbyf", "territory", "currency", + "currency_exchange", "annual_revenue", "industry", "person_section", @@ -415,12 +416,18 @@ "fieldname": "closed_on", "fieldtype": "Datetime", "label": "Closed On" + }, + { + "fieldname": "currency_exchange", + "fieldtype": "Link", + "label": "Currency Exchange", + "options": "CRM Currency Exchange" } ], "grid_page_length": 50, "index_web_pages_for_search": 1, "links": [], - "modified": "2025-07-06 15:16:37.673699", + "modified": "2025-07-09 14:26:53.986118", "modified_by": "Administrator", "module": "FCRM", "name": "CRM Deal", diff --git a/crm/fcrm/doctype/crm_deal/crm_deal.py b/crm/fcrm/doctype/crm_deal/crm_deal.py index b0011054..51bb4280 100644 --- a/crm/fcrm/doctype/crm_deal/crm_deal.py +++ b/crm/fcrm/doctype/crm_deal/crm_deal.py @@ -28,6 +28,7 @@ class CRMDeal(Document): self.closed_on = frappe.utils.now_datetime() self.validate_forcasting_fields() self.validate_lost_reason() + self.update_currency_exchange() def after_insert(self): if self.deal_owner: @@ -170,6 +171,28 @@ class CRMDeal(Document): elif self.lost_reason == "Other" and not self.lost_notes: frappe.throw(_("Please specify the reason for losing the deal."), frappe.ValidationError) + def update_currency_exchange(self): + if self.has_value_changed("currency") or not self.currency_exchange: + system_currency = frappe.db.get_single_value("System Settings", "currency") + currency_exchange = None + if self.currency and self.currency != system_currency: + if not frappe.db.exists( + "CRM Currency Exchange", {"from_currency": self.currency, "to_currency": system_currency} + ): + new_er = frappe.new_doc("CRM Currency Exchange") + new_er.from_currency = self.currency + new_er.to_currency = system_currency + new_er.insert(ignore_permissions=True) + currency_exchange = new_er.name + else: + currency_exchange = frappe.db.get_value( + "CRM Currency Exchange", + {"from_currency": self.currency, "to_currency": system_currency}, + "name", + ) + + currency_exchange and self.db_set("currency_exchange", currency_exchange) + @staticmethod def default_list_data(): columns = [ diff --git a/crm/fcrm/doctype/crm_organization/crm_organization.json b/crm/fcrm/doctype/crm_organization/crm_organization.json index 34252d1c..53b5c826 100644 --- a/crm/fcrm/doctype/crm_organization/crm_organization.json +++ b/crm/fcrm/doctype/crm_organization/crm_organization.json @@ -10,6 +10,7 @@ "organization_name", "no_of_employees", "currency", + "currency_exchange", "annual_revenue", "organization_logo", "column_break_pnpp", @@ -74,12 +75,18 @@ "fieldtype": "Link", "label": "Address", "options": "Address" + }, + { + "fieldname": "currency_exchange", + "fieldtype": "Link", + "label": "Currency Exchange", + "options": "CRM Currency Exchange" } ], "image_field": "organization_logo", "index_web_pages_for_search": 1, "links": [], - "modified": "2024-09-17 18:37:10.341062", + "modified": "2025-07-09 14:32:23.893267", "modified_by": "Administrator", "module": "FCRM", "name": "CRM Organization", @@ -111,7 +118,8 @@ "write": 1 } ], + "row_format": "Dynamic", "sort_field": "modified", "sort_order": "DESC", "states": [] -} \ No newline at end of file +} diff --git a/crm/fcrm/doctype/crm_organization/crm_organization.py b/crm/fcrm/doctype/crm_organization/crm_organization.py index 471d0f51..0da504e2 100644 --- a/crm/fcrm/doctype/crm_organization/crm_organization.py +++ b/crm/fcrm/doctype/crm_organization/crm_organization.py @@ -6,49 +6,74 @@ from frappe.model.document import Document class CRMOrganization(Document): - @staticmethod - def default_list_data(): - columns = [ - { - 'label': 'Organization', - 'type': 'Data', - 'key': 'organization_name', - 'width': '16rem', - }, - { - 'label': 'Website', - 'type': 'Data', - 'key': 'website', - 'width': '14rem', - }, - { - 'label': 'Industry', - 'type': 'Link', - 'key': 'industry', - 'options': 'CRM Industry', - 'width': '14rem', - }, - { - 'label': 'Annual Revenue', - 'type': 'Currency', - 'key': 'annual_revenue', - 'width': '14rem', - }, - { - 'label': 'Last Modified', - 'type': 'Datetime', - 'key': 'modified', - 'width': '8rem', - }, - ] - rows = [ - "name", - "organization_name", - "organization_logo", - "website", - "industry", - "currency", - "annual_revenue", - "modified", - ] - return {'columns': columns, 'rows': rows} + def validate(self): + self.update_currency_exchange() + + def update_currency_exchange(self): + if self.has_value_changed("currency") or not self.currency_exchange: + system_currency = frappe.db.get_single_value("System Settings", "currency") + currency_exchange = None + if self.currency and self.currency != system_currency: + if not frappe.db.exists( + "CRM Currency Exchange", {"from_currency": self.currency, "to_currency": system_currency} + ): + new_er = frappe.new_doc("CRM Currency Exchange") + new_er.from_currency = self.currency + new_er.to_currency = system_currency + new_er.insert(ignore_permissions=True) + currency_exchange = new_er.name + else: + currency_exchange = frappe.db.get_value( + "CRM Currency Exchange", + {"from_currency": self.currency, "to_currency": system_currency}, + "name", + ) + + currency_exchange and self.db_set("currency_exchange", currency_exchange) + + @staticmethod + def default_list_data(): + columns = [ + { + "label": "Organization", + "type": "Data", + "key": "organization_name", + "width": "16rem", + }, + { + "label": "Website", + "type": "Data", + "key": "website", + "width": "14rem", + }, + { + "label": "Industry", + "type": "Link", + "key": "industry", + "options": "CRM Industry", + "width": "14rem", + }, + { + "label": "Annual Revenue", + "type": "Currency", + "key": "annual_revenue", + "width": "14rem", + }, + { + "label": "Last Modified", + "type": "Datetime", + "key": "modified", + "width": "8rem", + }, + ] + rows = [ + "name", + "organization_name", + "organization_logo", + "website", + "industry", + "currency", + "annual_revenue", + "modified", + ] + return {"columns": columns, "rows": rows} diff --git a/frontend/src/stores/users.js b/frontend/src/stores/users.js index 8ef3fd6d..d1630c3b 100644 --- a/frontend/src/stores/users.js +++ b/frontend/src/stores/users.js @@ -54,7 +54,7 @@ export const usersStore = defineStore('crm-users', () => { } function isManager(email) { - return getUser(email).role === 'Sales Manager' + return getUser(email).role === 'Sales Manager' || isAdmin(email) } function isSalesUser(email) {