blob: 791250cc7e9c243bcc22a24d9ccbc802ee704155 [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
9from erpnext.startup.notifications import get_notification_config
10
11status_depends_on = {
12 'Customer': ('Opportunity', 'Quotation', 'Sales Order', 'Sales Invoice', 'Project', 'Issue'),
13 'Supplier': ('Supplier Quotation', 'Purchase Order', 'Purchase Invoice')
14}
15
16default_status = {
17 'Customer': 'Active',
18 'Supplier': None
19}
20
21def notify_status(doc, method):
22 '''Notify status to customer, supplier'''
23
24 party_type = None
25 for key, doctypes in status_depends_on.iteritems():
26 if doc.doctype in doctypes:
27 party_type = key
28 break
29
30 if not party_type:
31 return
32
Rushabh Mehta2dff9272016-04-08 18:05:12 +053033 name = doc.get(party_type.lower())
34 if not name:
35 return
36
37 party = frappe.get_doc(party_type, name)
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053038 config = get_notification_config().get('for_doctype').get(doc.doctype)
39
40 status = None
41 if config:
42 if evaluate_filters(doc, config):
43 # filters match, passed document is open
44 status = 'Open'
45
46 if status=='Open':
47 if party.status != 'Open':
48 # party not open, make it open
49 party.status = 'Open'
50 party.save(ignore_permissions=True)
51
52 else:
53 if party.status == 'Open':
54 # may be open elsewhere, check
55 # default status
56 party.status = status
57 update_status(party, )
58
59def update_status(doc):
60 '''Set status as open if there is any open notification'''
61 config = get_notification_config()
62
63 original_status = doc.status
64
65 doc.status = default_status[doc.doctype]
66 for doctype in status_depends_on[doc.doctype]:
67 filters = config.get('for_doctype', {}).get(doctype) or {}
68 filters[doc.doctype.lower()] = doc.name
69 if filters:
70 open_count = frappe.get_all(doctype, fields='count(*) as count', filters=filters)
71 if open_count[0].count > 0:
72 doc.status = 'Open'
73 break
74
75 if doc.status != original_status:
76 doc.db_set('status', doc.status)