Rushabh Mehta | 95e4e14 | 2012-09-13 19:40:56 +0530 | [diff] [blame] | 1 | # ERPNext - web based ERP (http://erpnext.com) |
| 2 | # Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
Rushabh Mehta | 2ad0d42 | 2012-09-18 18:52:05 +0530 | [diff] [blame] | 17 | from __future__ import unicode_literals |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 18 | import webnotes |
| 19 | |
| 20 | def get_unread_messages(): |
| 21 | "returns unread (docstatus-0 messages for a user)" |
| 22 | return webnotes.conn.sql("""\ |
Rushabh Mehta | ec40686 | 2012-11-13 10:59:11 +0530 | [diff] [blame] | 23 | SELECT count(*) |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 24 | FROM `tabComment` |
| 25 | WHERE comment_doctype IN ('My Company', 'Message') |
| 26 | AND comment_docname = %s |
| 27 | AND ifnull(docstatus,0)=0 |
Rushabh Mehta | ec40686 | 2012-11-13 10:59:11 +0530 | [diff] [blame] | 28 | """, webnotes.user.name)[0][0] |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 29 | |
| 30 | def get_open_support_tickets(): |
Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 31 | """Returns a count of open support tickets""" |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 32 | open_support_tickets = webnotes.conn.sql("""\ |
| 33 | SELECT COUNT(*) FROM `tabSupport Ticket` |
| 34 | WHERE status = 'Open'""") |
Rushabh Mehta | 2e5db35 | 2013-01-16 11:34:26 +0530 | [diff] [blame] | 35 | return open_support_tickets[0][0] |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 36 | |
Rushabh Mehta | 07c94ce | 2012-06-04 12:57:23 +0530 | [diff] [blame] | 37 | def get_open_tasks(): |
Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 38 | """Returns a count of open tasks""" |
Rushabh Mehta | 07c94ce | 2012-06-04 12:57:23 +0530 | [diff] [blame] | 39 | return webnotes.conn.sql("""\ |
| 40 | SELECT COUNT(*) FROM `tabTask` |
| 41 | WHERE status = 'Open'""")[0][0] |
| 42 | |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 43 | def get_things_todo(): |
Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 44 | """Returns a count of incomplete todos""" |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 45 | incomplete_todos = webnotes.conn.sql("""\ |
| 46 | SELECT COUNT(*) FROM `tabToDo` |
| 47 | WHERE IFNULL(checked, 0) = 0 |
Rushabh Mehta | 9e12af4 | 2012-12-12 16:13:29 +0530 | [diff] [blame] | 48 | AND (owner = %s or assigned_by=%s)""", (webnotes.session.user, webnotes.session.user)) |
Rushabh Mehta | 2e5db35 | 2013-01-16 11:34:26 +0530 | [diff] [blame] | 49 | return incomplete_todos[0][0] |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 50 | |
| 51 | def get_todays_events(): |
Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 52 | """Returns a count of todays events in calendar""" |
Rushabh Mehta | 2e5db35 | 2013-01-16 11:34:26 +0530 | [diff] [blame] | 53 | from webnotes.utils import nowdate |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 54 | todays_events = webnotes.conn.sql("""\ |
| 55 | SELECT COUNT(*) FROM `tabEvent` |
| 56 | WHERE owner = %s |
| 57 | AND event_type != 'Cancel' |
Anand Doshi | a6acdda | 2013-03-01 14:41:32 +0530 | [diff] [blame] | 58 | AND %s between date(starts_on) and date(ends_on)""", ( |
Rushabh Mehta | 2e5db35 | 2013-01-16 11:34:26 +0530 | [diff] [blame] | 59 | webnotes.session.user, nowdate())) |
| 60 | return todays_events[0][0] |
| 61 | |
| 62 | def get_open_leads(): |
| 63 | return webnotes.conn.sql("""select count(*) from tabLead |
| 64 | where status='Open'""")[0][0] |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 65 | |
Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 66 | def get_unanswered_questions(): |
| 67 | return len(filter(lambda d: d[0]==0, |
| 68 | webnotes.conn.sql("""select (select count(*) from tabAnswer |
| 69 | where tabAnswer.question = tabQuestion.name) as answers from tabQuestion"""))) |
| 70 | |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 71 | @webnotes.whitelist() |
| 72 | def get_global_status_messages(arg=None): |
| 73 | return { |
| 74 | 'unread_messages': get_unread_messages(), |
| 75 | 'open_support_tickets': get_open_support_tickets(), |
| 76 | 'things_todo': get_things_todo(), |
| 77 | 'todays_events': get_todays_events(), |
Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 78 | 'open_tasks': get_open_tasks(), |
Rushabh Mehta | 2e5db35 | 2013-01-16 11:34:26 +0530 | [diff] [blame] | 79 | 'unanswered_questions': get_unanswered_questions(), |
| 80 | 'open_leads': get_open_leads() |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 81 | } |