From e4f80a6eaa57f6668bc649fb451143b42e8b0ad3 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 17 May 2017 19:41:39 +0530 Subject: [PATCH] Open lost opportunity again if quotation made against it (#8854) --- erpnext/controllers/status_updater.py | 2 +- .../crm/doctype/opportunity/opportunity.py | 21 ++++++++++++++----- .../selling/doctype/quotation/quotation.py | 4 +++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/erpnext/controllers/status_updater.py b/erpnext/controllers/status_updater.py index 403430606f..cede8f4d34 100644 --- a/erpnext/controllers/status_updater.py +++ b/erpnext/controllers/status_updater.py @@ -19,7 +19,7 @@ status_map = { ["Converted", "has_customer"], ], "Opportunity": [ - ["Quotation", "has_quotation"], + ["Quotation", "has_active_quotation"], ["Converted", "has_ordered_quotation"], ["Lost", "eval:self.status=='Lost'"], ["Lost", "has_lost_quotation"], diff --git a/erpnext/crm/doctype/opportunity/opportunity.py b/erpnext/crm/doctype/opportunity/opportunity.py index 3c553a5da8..8a21e7c746 100644 --- a/erpnext/crm/doctype/opportunity/opportunity.py +++ b/erpnext/crm/doctype/opportunity/opportunity.py @@ -84,20 +84,31 @@ class Opportunity(TransactionBase): def on_trash(self): self.delete_events() - def has_quotation(self): - return frappe.db.get_value("Quotation Item", {"prevdoc_docname": self.name, "docstatus": 1}) + def has_active_quotation(self): + return frappe.db.sql(""" + select q.name + from `tabQuotation` q, `tabQuotation Item` qi + where q.name = qi.parent and q.docstatus=1 and qi.prevdoc_docname =%s + and q.status not in ('Lost', 'Closed')""", self.name) def has_ordered_quotation(self): - return frappe.db.sql("""select q.name from `tabQuotation` q, `tabQuotation Item` qi - where q.name = qi.parent and q.docstatus=1 and qi.prevdoc_docname =%s and q.status = 'Ordered'""", self.name) + return frappe.db.sql(""" + select q.name + from `tabQuotation` q, `tabQuotation Item` qi + where q.name = qi.parent and q.docstatus=1 and qi.prevdoc_docname =%s + and q.status = 'Ordered'""", self.name) def has_lost_quotation(self): - return frappe.db.sql(""" + lost_quotation = frappe.db.sql(""" select q.name from `tabQuotation` q, `tabQuotation Item` qi where q.name = qi.parent and q.docstatus=1 and qi.prevdoc_docname =%s and q.status = 'Lost' """, self.name) + if lost_quotation: + if self.has_active_quotation(): + return False + return True def validate_cust_name(self): if self.customer: diff --git a/erpnext/selling/doctype/quotation/quotation.py b/erpnext/selling/doctype/quotation/quotation.py index b5b24f8c75..b102e1d699 100644 --- a/erpnext/selling/doctype/quotation/quotation.py +++ b/erpnext/selling/doctype/quotation/quotation.py @@ -44,7 +44,9 @@ class Quotation(SellingController): def update_opportunity(self): for opportunity in list(set([d.prevdoc_docname for d in self.get("items")])): if opportunity: - frappe.get_doc("Opportunity", opportunity).set_status(update=True) + opp = frappe.get_doc("Opportunity", opportunity) + opp.status = None + opp.set_status(update=True) def declare_order_lost(self, arg): if not self.has_sales_order():