diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py index e29169604e9..18bd4432bdb 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -69,6 +69,7 @@ class Customer(TransactionBase): self.flags.old_lead = self.lead_name validate_party_accounts(self) self.status = get_party_status(self) + self.validate_credit_limit_on_change() def on_update(self): self.validate_name_with_customer_group() @@ -125,6 +126,15 @@ class Customer(TransactionBase): if frappe.db.exists("Customer Group", self.name): frappe.throw(_("A Customer Group exists with same name please change the Customer name or rename the Customer Group"), frappe.NameError) + def validate_credit_limit_on_change(self): + customer = frappe.get_doc('Customer', self.name) + companies = [c.name for c in frappe.get_all("Company")] + + for company in companies: + outstanding_amt = get_customer_outstanding(customer.name, company) + if flt(self.credit_limit) < outstanding_amt: + frappe.throw(_("New credit limit is less than current outstanding amount for the customer. Credit limit has to be atleast {0}").format(outstanding_amt)) + def delete_customer_address(self): addresses = frappe.db.sql("""select name, lead from `tabAddress` where customer=%s""", (self.name,))