blob: 6d9efb190b64e975c0270e382a222f0aaf81f4eb [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
Rushabh Mehtafc8e5892016-07-20 12:08:47 +053022def notify_status(doc, method=None):
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053023 '''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)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053040 party.flags.ignore_mandatory = True
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053041
42 status = None
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053043 if filters:
44 if evaluate_filters(doc, filters):
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053045 # filters match, passed document is open
46 status = 'Open'
47
48 if status=='Open':
49 if party.status != 'Open':
50 # party not open, make it open
51 party.status = 'Open'
52 party.save(ignore_permissions=True)
53
54 else:
55 if party.status == 'Open':
56 # may be open elsewhere, check
57 # default status
Rushabh Mehta8c9c57c2016-04-13 18:22:06 +053058 update_status(party)
59
60 party.update_modified()
Rushabh Mehtafc8e5892016-07-20 12:08:47 +053061 party.notify_update()
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053062
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053063def get_party_status(doc):
64 '''return party status based on open documents'''
65 status = default_status[doc.doctype]
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053066 for doctype in status_depends_on[doc.doctype]:
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053067 filters = get_filters_for(doctype)
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053068 filters[doc.doctype.lower()] = doc.name
69 if filters:
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053070 open_count = frappe.get_all(doctype, fields='name', filters=filters, limit_page_length=1)
71 if len(open_count) > 0:
72 status = 'Open'
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053073 break
74
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053075 return status
76
77def update_status(doc):
78 '''Set status as open if there is any open notification'''
79 status = get_party_status(doc)
80 if doc.status != status:
81 doc.db_set('status', status)