blob: ff095b1d658c47803bfd68ca2769e3a612dcdb7a [file] [log] [blame]
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5
6import frappe
7
8from frappe.utils import evaluate_filters
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +05309from frappe.desk.notifications import get_filters_for
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053010
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053011# NOTE: if you change this also update triggers in erpnext/hooks.py
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053012status_depends_on = {
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053013 'Customer': ('Opportunity', 'Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice', 'Project', 'Issue'),
14 'Supplier': ('Supplier Quotation', 'Purchase Order', 'Purchase Receipt', 'Purchase Invoice')
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053015}
16
17default_status = {
18 'Customer': 'Active',
19 'Supplier': None
20}
21
22def notify_status(doc, method):
23 '''Notify status to customer, supplier'''
24
25 party_type = None
26 for key, doctypes in status_depends_on.iteritems():
27 if doc.doctype in doctypes:
28 party_type = key
29 break
30
31 if not party_type:
32 return
33
Rushabh Mehta2dff9272016-04-08 18:05:12 +053034 name = doc.get(party_type.lower())
35 if not name:
36 return
37
38 party = frappe.get_doc(party_type, name)
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053039 filters = get_filters_for(doc.doctype)
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053040
41 status = None
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053042 if filters:
43 if evaluate_filters(doc, filters):
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053044 # filters match, passed document is open
45 status = 'Open'
46
47 if status=='Open':
48 if party.status != 'Open':
49 # party not open, make it open
50 party.status = 'Open'
51 party.save(ignore_permissions=True)
52
53 else:
54 if party.status == 'Open':
55 # may be open elsewhere, check
56 # default status
57 party.status = status
Rushabh Mehta8c9c57c2016-04-13 18:22:06 +053058 update_status(party)
59
60 party.update_modified()
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053061
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053062def get_party_status(doc):
63 '''return party status based on open documents'''
64 status = default_status[doc.doctype]
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053065 for doctype in status_depends_on[doc.doctype]:
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053066 filters = get_filters_for(doctype)
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053067 filters[doc.doctype.lower()] = doc.name
68 if filters:
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053069 open_count = frappe.get_all(doctype, fields='name', filters=filters, limit_page_length=1)
70 if len(open_count) > 0:
71 status = 'Open'
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053072 break
73
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053074 return status
75
76def update_status(doc):
77 '''Set status as open if there is any open notification'''
78 status = get_party_status(doc)
79 if doc.status != status:
80 doc.db_set('status', status)