From 5cd58dc38737fdadc642c6f7f0ff0d1ae2a0286f Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Thu, 1 Aug 2013 18:58:35 +0530 Subject: [PATCH] [usability] [issue] webnotes/erpnext#606 - disable default currency after a transaction is created for a company --- setup/doctype/company/company.js | 4 ++++ setup/doctype/company/company.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/setup/doctype/company/company.js b/setup/doctype/company/company.js index dfe8e2ae87..4859464309 100644 --- a/setup/doctype/company/company.js +++ b/setup/doctype/company/company.js @@ -17,6 +17,10 @@ cur_frm.cscript.refresh = function(doc, cdt, cdn) { if(doc.abbr && !doc.__islocal) cur_frm.set_df_property("abbr", "read_only", 1) + + if(!doc.__islocal) { + cur_frm.toggle_enable("default_currency", !cur_frm.doc.__transactions_exist); + } } cur_frm.cscript.has_special_chars = function(t) { diff --git a/setup/doctype/company/company.py b/setup/doctype/company/company.py index 8a10587a1a..ee432c00e4 100644 --- a/setup/doctype/company/company.py +++ b/setup/doctype/company/company.py @@ -29,10 +29,30 @@ class DocType: def __init__(self,d,dl): self.doc, self.doclist = d,dl + def onload(self): + self.doc.fields["__transactions_exist"] = self.check_if_transactions_exist() + + def check_if_transactions_exist(self): + exists = False + for doctype in ["Sales Invoice", "Delivery Note", "Sales Order", "Quotation", + "Purchase Invoice", "Purchase Receipt", "Purchase Order", "Supplier Quotation"]: + if webnotes.conn.sql("""select name from `tab%s` where company=%s and docstatus=1 + limit 1""" % (doctype, "%s"), self.doc.name): + exists = True + break + + return exists + def validate(self): if self.doc.fields.get('__islocal') and len(self.doc.abbr) > 5: webnotes.msgprint("Abbreviation cannot have more than 5 characters", raise_exception=1) + + self.previous_default_currency = webnotes.conn.get_value("Company", self.doc.name, "default_currency") + if self.doc.default_currency and self.previous_default_currency and \ + self.doc.default_currency != self.previous_default_currency and \ + self.check_if_transactions_exist(): + msgprint(_("Sorry! You cannot change company's default currency, because there are existing transactions against it. You will need to cancel those transactions if you want to change the default currency."), raise_exception=True) def on_update(self): if not webnotes.conn.sql("""select name from tabAccount