From d8bc40d7f07dcb7f40f2725d4911ef6ce855970c Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Thu, 22 Oct 2015 17:54:50 +0530 Subject: [PATCH] [fix] party gle currency validation --- erpnext/accounts/doctype/gl_entry/gl_entry.py | 10 ++---- erpnext/accounts/party.py | 10 +++--- erpnext/controllers/accounts_controller.py | 4 ++- .../v6_4/fix_expense_included_in_valuation.py | 36 +++++++++---------- 4 files changed, 29 insertions(+), 31 deletions(-) diff --git a/erpnext/accounts/doctype/gl_entry/gl_entry.py b/erpnext/accounts/doctype/gl_entry/gl_entry.py index d74b9dc7c8..f0f0fa4b41 100644 --- a/erpnext/accounts/doctype/gl_entry/gl_entry.py +++ b/erpnext/accounts/doctype/gl_entry/gl_entry.py @@ -6,7 +6,7 @@ import frappe from frappe import _ from frappe.utils import flt, fmt_money, getdate, formatdate from frappe.model.document import Document -from erpnext.accounts.party import validate_party_gle_currency, get_party_account_currency +from erpnext.accounts.party import validate_party_gle_currency from erpnext.accounts.utils import get_account_currency from erpnext.setup.doctype.company.company import get_company_currency from erpnext.exceptions import InvalidAccountCurrency, CustomerFrozen @@ -114,13 +114,7 @@ class GLEntry(Document): .format(self.account, (account_currency or company_currency)), InvalidAccountCurrency) if self.party_type and self.party: - party_account_currency = get_party_account_currency(self.party_type, self.party, self.company) - - if party_account_currency != self.account_currency: - frappe.throw(_("Accounting Entry for {0}: {1} can only be made in currency: {2}") - .format(self.party_type, self.party, party_account_currency), InvalidAccountCurrency) - - validate_party_gle_currency(self.party_type, self.party, self.company) + validate_party_gle_currency(self.party_type, self.party, self.company, self.account_currency) def validate_balance_type(account, adv_adj=False): if not adv_adj and account: diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py index 516608ff0e..d7b23ac1f4 100644 --- a/erpnext/accounts/party.py +++ b/erpnext/accounts/party.py @@ -203,9 +203,11 @@ def get_party_gle_currency(party_type, party, company): return frappe.local_cache("party_gle_currency", (party_type, party, company), generator) -def validate_party_gle_currency(party_type, party, company): +def validate_party_gle_currency(party_type, party, company, party_account_currency=None): """Validate party account currency with existing GL Entry's currency""" - party_account_currency = get_party_account_currency(party_type, party, company) + if not party_account_currency: + party_account_currency = get_party_account_currency(party_type, party, company) + existing_gle_currency = get_party_gle_currency(party_type, party, company) if existing_gle_currency and party_account_currency != existing_gle_currency: @@ -221,10 +223,10 @@ def validate_party_accounts(doc): .format(doc.doctype, doc.name), DuplicatePartyAccountError) else: companies.append(account.company) - + party_account_currency = frappe.db.get_value("Account", account.account, "account_currency") existing_gle_currency = get_party_gle_currency(doc.doctype, doc.name, account.company) - + if existing_gle_currency and party_account_currency != existing_gle_currency: frappe.throw(_("Accounting entries have already been made in currency {0} for company {1}. Please select a receivable or payable account with currency {0}.").format(existing_gle_currency, account.company)) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 2ed5fa8466..5b06f1c8b7 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -10,7 +10,7 @@ from erpnext.accounts.utils import get_fiscal_year, validate_fiscal_year, get_ac from erpnext.utilities.transaction_base import TransactionBase from erpnext.controllers.recurring_document import convert_to_recurring, validate_recurring_document from erpnext.controllers.sales_and_purchase_return import validate_return -from erpnext.accounts.party import get_party_account_currency, validate_party_gle_currency +from erpnext.accounts.party import get_party_account_currency from erpnext.exceptions import CustomerFrozen, InvalidCurrency force_item_fields = ("item_group", "barcode", "brand", "stock_uom") @@ -435,6 +435,8 @@ class AccountsController(TransactionBase): frappe.throw(_("Accounting Entry for {0}: {1} can only be made in currency: {2}") .format(party_type, party, party_account_currency), InvalidCurrency) + # Note: not validating with gle account because we don't have the account at quotation / sales order level and we shouldn't stop someone from creating a sales invoice if sales order is already created + @frappe.whitelist() def get_tax_rate(account_head): return frappe.db.get_value("Account", account_head, "tax_rate") diff --git a/erpnext/patches/v6_4/fix_expense_included_in_valuation.py b/erpnext/patches/v6_4/fix_expense_included_in_valuation.py index cccfb9bb36..24cf96814a 100644 --- a/erpnext/patches/v6_4/fix_expense_included_in_valuation.py +++ b/erpnext/patches/v6_4/fix_expense_included_in_valuation.py @@ -8,13 +8,13 @@ from frappe.utils import cstr def execute(): for company in frappe.db.sql("select name, expenses_included_in_valuation from tabCompany", as_dict=1): frozen_date = get_frozen_date(company.name, company.expenses_included_in_valuation) - - # Purchase Invoices after frozen date + + # Purchase Invoices after frozen date # which are not against Receipt, but valuation related tax is there pi_list = frappe.db.sql(""" select distinct pi.name from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` pi_item - where + where pi.name = pi_item.parent and pi.company = %s and pi.posting_date > %s @@ -25,40 +25,40 @@ def execute(): and (pi_item.item_code is not null and pi_item.item_code != '') and exists(select name from `tabItem` where name=pi_item.item_code and is_stock_item=1) """, (company.name, frozen_date), as_dict=1) - + for pi in pi_list: # Check whether gle exists for Expenses Included in Valuation account against the PI - gle_for_expenses_included_in_valuation = frappe.db.sql("""select name from `tabGL Entry` - where voucher_type='Purchase Invoice' and voucher_no=%s and account=%s""", + gle_for_expenses_included_in_valuation = frappe.db.sql("""select name from `tabGL Entry` + where voucher_type='Purchase Invoice' and voucher_no=%s and account=%s""", (pi.name, company.expenses_included_in_valuation)) - + if gle_for_expenses_included_in_valuation: frappe.db.sql("""delete from `tabGL Entry` where voucher_type='Purchase Invoice' and voucher_no=%s""", pi.name) + print pi.name + purchase_invoice = frappe.get_doc("Purchase Invoice", pi.name) purchase_invoice.make_gl_entries() - - print pi.name - + def get_frozen_date(company, account): # Accounting frozen upto accounts_frozen_upto = frappe.db.get_single_value("Accounts Settings", "acc_frozen_upto") - + # Last adjustment entry to correct Expenses Included in Valuation account balance - last_adjustment_entry = frappe.db.sql("""select posting_date from `tabGL Entry` + last_adjustment_entry = frappe.db.sql("""select posting_date from `tabGL Entry` where account=%s and company=%s and voucher_type = 'Journal Entry' order by posting_date desc limit 1""", (account, company)) - + last_adjustment_date = cstr(last_adjustment_entry[0][0]) if last_adjustment_entry else None - + # Last period closing voucher - last_closing_entry = frappe.db.sql("""select posting_date from `tabGL Entry` + last_closing_entry = frappe.db.sql("""select posting_date from `tabGL Entry` where company=%s and voucher_type = 'Period Closing Voucher' order by posting_date desc limit 1""", company) - + last_closing_date = cstr(last_closing_entry[0][0]) if last_closing_entry else None frozen_date = max([accounts_frozen_upto, last_adjustment_date, last_closing_date]) - - return frozen_date or '1900-01-01' \ No newline at end of file + + return frozen_date or '1900-01-01'