blob: 0c5d4c50e2668be416252f53087d6588284c8c6e [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
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +05308from frappe import _
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +05309from frappe.utils import evaluate_filters
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053010from frappe.desk.notifications import get_filters_for
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053011
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053012# NOTE: if you change this also update triggers in erpnext/hooks.py
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053013status_depends_on = {
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053014 'Customer': ('Opportunity', 'Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice', 'Project', 'Issue'),
15 'Supplier': ('Supplier Quotation', 'Purchase Order', 'Purchase Receipt', 'Purchase Invoice')
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053016}
17
18default_status = {
19 'Customer': 'Active',
20 'Supplier': None
21}
22
23def notify_status(doc, method):
24 '''Notify status to customer, supplier'''
25
26 party_type = None
27 for key, doctypes in status_depends_on.iteritems():
28 if doc.doctype in doctypes:
29 party_type = key
30 break
31
32 if not party_type:
33 return
34
Rushabh Mehta2dff9272016-04-08 18:05:12 +053035 name = doc.get(party_type.lower())
36 if not name:
37 return
38
39 party = frappe.get_doc(party_type, name)
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053040 filters = get_filters_for(doc.doctype)
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
58 party.status = status
59 update_status(party, )
60
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053061def get_party_status(doc):
62 '''return party status based on open documents'''
63 status = default_status[doc.doctype]
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053064 for doctype in status_depends_on[doc.doctype]:
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053065 filters = get_filters_for(doctype)
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053066 filters[doc.doctype.lower()] = doc.name
67 if filters:
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053068 open_count = frappe.get_all(doctype, fields='name', filters=filters, limit_page_length=1)
69 if len(open_count) > 0:
70 status = 'Open'
Rushabh Mehtaf16f9c52016-04-08 17:20:50 +053071 break
72
Rushabh Mehta5d0e8de2016-04-11 17:34:25 +053073 return status
74
75def update_status(doc):
76 '''Set status as open if there is any open notification'''
77 status = get_party_status(doc)
78 if doc.status != status:
79 doc.db_set('status', status)
80
81def get_transaction_count(doc):
82 '''Return list of open documents given party doc'''
83 out = []
84 for doctype in status_depends_on[doc.doctype]:
85 filters = get_filters_for(doctype)
86 filters[doc.doctype.lower()] = doc.name
87 if filters:
88 open_count = frappe.get_all(doctype, fields='count(*) as count', filters=filters)[0].count
89 out.append({'name': doctype, 'count': open_count})
90
91 return out
92
93@frappe.whitelist()
94def get_transaction_info(party_type, party_name):
95 doc = frappe.get_doc(party_type, party_name)
96 if not doc.has_permission('read'):
97 frappe.msgprint(_("Not permitted"), raise_exception=True)
98
99 out = {}
100 out['transaction_count'] = get_transaction_count(doc)
101
102 return out