diff --git a/erpnext/accounts/doctype/budget/budget.py b/erpnext/accounts/doctype/budget/budget.py index f99d0bb5f9..d025583b51 100644 --- a/erpnext/accounts/doctype/budget/budget.py +++ b/erpnext/accounts/doctype/budget/budget.py @@ -27,13 +27,21 @@ class Budget(Document): def validate_duplicate(self): budget_against_field = frappe.scrub(self.budget_against) budget_against = self.get(budget_against_field) - existing_budget = frappe.db.get_value("Budget", {budget_against_field: budget_against, - "fiscal_year": self.fiscal_year, "company": self.company, - "name": ["!=", self.name], "docstatus": ["!=", 2]}) - if existing_budget: - frappe.throw(_("Another Budget record '{0}' already exists against {1} '{2}' for fiscal year {3}") - .format(existing_budget, self.budget_against, budget_against, self.fiscal_year), DuplicateBudgetError) - + + accounts = [d.account for d in self.accounts] or [] + existing_budget = frappe.db.sql(""" + select + b.name, ba.account from `tabBudget` b, `tabBudget Account` ba + where + ba.parent = b.name and b.company = %s and %s=%s and + b.fiscal_year=%s and b.name != %sand ba.account in (%s) """ + % ('%s', budget_against_field, '%s', '%s', '%s', ','.join(['%s'] * len(accounts))), + (self.company, budget_against, self.fiscal_year, self.name) + tuple(accounts), as_dict=1) + + for d in existing_budget: + frappe.throw(_("Another Budget record '{0}' already exists against {1} '{2}' and account '{3}' for fiscal year {4}") + .format(d.name, self.budget_against, budget_against, d.account, self.fiscal_year), DuplicateBudgetError) + def validate_accounts(self): account_list = [] for d in self.get('accounts'):