Rushabh Mehta | f16f9c5 | 2016-04-08 17:20:50 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | |
| 6 | import frappe |
| 7 | |
| 8 | from frappe.utils import evaluate_filters |
Rushabh Mehta | 5d0e8de | 2016-04-11 17:34:25 +0530 | [diff] [blame] | 9 | from frappe.desk.notifications import get_filters_for |
Rushabh Mehta | f16f9c5 | 2016-04-08 17:20:50 +0530 | [diff] [blame] | 10 | |
Rushabh Mehta | 5d0e8de | 2016-04-11 17:34:25 +0530 | [diff] [blame] | 11 | # NOTE: if you change this also update triggers in erpnext/hooks.py |
Rushabh Mehta | f16f9c5 | 2016-04-08 17:20:50 +0530 | [diff] [blame] | 12 | status_depends_on = { |
Rushabh Mehta | 5d0e8de | 2016-04-11 17:34:25 +0530 | [diff] [blame] | 13 | 'Customer': ('Opportunity', 'Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice', 'Project', 'Issue'), |
| 14 | 'Supplier': ('Supplier Quotation', 'Purchase Order', 'Purchase Receipt', 'Purchase Invoice') |
Rushabh Mehta | f16f9c5 | 2016-04-08 17:20:50 +0530 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | default_status = { |
| 18 | 'Customer': 'Active', |
| 19 | 'Supplier': None |
| 20 | } |
| 21 | |
| 22 | def 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 Mehta | 2dff927 | 2016-04-08 18:05:12 +0530 | [diff] [blame] | 34 | name = doc.get(party_type.lower()) |
| 35 | if not name: |
| 36 | return |
| 37 | |
| 38 | party = frappe.get_doc(party_type, name) |
Rushabh Mehta | 5d0e8de | 2016-04-11 17:34:25 +0530 | [diff] [blame] | 39 | filters = get_filters_for(doc.doctype) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 40 | party.flags.ignore_mandatory = True |
Rushabh Mehta | f16f9c5 | 2016-04-08 17:20:50 +0530 | [diff] [blame] | 41 | |
| 42 | status = None |
Rushabh Mehta | 5d0e8de | 2016-04-11 17:34:25 +0530 | [diff] [blame] | 43 | if filters: |
| 44 | if evaluate_filters(doc, filters): |
Rushabh Mehta | f16f9c5 | 2016-04-08 17:20:50 +0530 | [diff] [blame] | 45 | # 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 |
Rushabh Mehta | 8c9c57c | 2016-04-13 18:22:06 +0530 | [diff] [blame] | 59 | update_status(party) |
| 60 | |
| 61 | party.update_modified() |
Rushabh Mehta | f16f9c5 | 2016-04-08 17:20:50 +0530 | [diff] [blame] | 62 | |
Rushabh Mehta | 5d0e8de | 2016-04-11 17:34:25 +0530 | [diff] [blame] | 63 | def get_party_status(doc): |
| 64 | '''return party status based on open documents''' |
| 65 | status = default_status[doc.doctype] |
Rushabh Mehta | f16f9c5 | 2016-04-08 17:20:50 +0530 | [diff] [blame] | 66 | for doctype in status_depends_on[doc.doctype]: |
Rushabh Mehta | 5d0e8de | 2016-04-11 17:34:25 +0530 | [diff] [blame] | 67 | filters = get_filters_for(doctype) |
Rushabh Mehta | f16f9c5 | 2016-04-08 17:20:50 +0530 | [diff] [blame] | 68 | filters[doc.doctype.lower()] = doc.name |
| 69 | if filters: |
Rushabh Mehta | 5d0e8de | 2016-04-11 17:34:25 +0530 | [diff] [blame] | 70 | open_count = frappe.get_all(doctype, fields='name', filters=filters, limit_page_length=1) |
| 71 | if len(open_count) > 0: |
| 72 | status = 'Open' |
Rushabh Mehta | f16f9c5 | 2016-04-08 17:20:50 +0530 | [diff] [blame] | 73 | break |
| 74 | |
Rushabh Mehta | 5d0e8de | 2016-04-11 17:34:25 +0530 | [diff] [blame] | 75 | return status |
| 76 | |
| 77 | def 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) |