blob: 9ead6d688d5aba824388ce83883f0ea9e47a9329 [file] [log] [blame]
Rushabh Mehta29a75c42011-08-25 19:17:44 +05301dashboards = [
2 {
3 'type': 'account',
4 'account': 'Income',
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +05305 'title': 'Income',
6 'fillColor': '#90EE90'
Rushabh Mehta29a75c42011-08-25 19:17:44 +05307 },
8
9 {
10 'type': 'account',
11 'account': 'Expenses',
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +053012 'title': 'Expenses',
13 'fillColor': '#90EE90'
Rushabh Mehta29a75c42011-08-25 19:17:44 +053014 },
15
16 {
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +053017 'type': 'receivables',
18 'title': 'Receivables',
19 'fillColor': '#FFE4B5'
Rushabh Mehta29a75c42011-08-25 19:17:44 +053020 },
21
22 {
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +053023 'type': 'payables',
24 'title': 'Payables',
25 'fillColor': '#FFE4B5'
Rushabh Mehta29a75c42011-08-25 19:17:44 +053026 },
27
28 {
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +053029 'type': 'collection',
30 'title': 'Collection',
31 'comment':'This info comes from the accounts your have marked as "Bank or Cash"',
32 'fillColor': '#DDA0DD'
Rushabh Mehta29a75c42011-08-25 19:17:44 +053033 },
34
35 {
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +053036 'type': 'payments',
37 'title': 'Payments',
38 'comment':'This info comes from the accounts your have marked as "Bank or Cash"',
39 'fillColor': '#DDA0DD'
Rushabh Mehta29a75c42011-08-25 19:17:44 +053040 },
41
42 {
43 'type': 'creation',
44 'doctype': 'Quotation',
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +053045 'title': 'New Quotations',
46 'fillColor': '#ADD8E6'
Rushabh Mehta29a75c42011-08-25 19:17:44 +053047 },
48
49 {
50 'type': 'creation',
51 'doctype': 'Sales Order',
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +053052 'title': 'New Orders',
53 'fillColor': '#ADD8E6'
Rushabh Mehta29a75c42011-08-25 19:17:44 +053054 }
55]
56
Rushabh Mehta29a75c42011-08-25 19:17:44 +053057class DashboardWidget:
58 def __init__(self, company, start, end, interval):
Rushabh Mehta29a75c42011-08-25 19:17:44 +053059 from webnotes.utils import getdate
60 from webnotes.model.code import get_obj
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +053061 import webnotes
Rushabh Mehta29a75c42011-08-25 19:17:44 +053062
63 self.company = company
64 self.abbr = webnotes.conn.get_value('Company', company, 'abbr')
65 self.start = getdate(start)
66 self.end = getdate(end)
67
68 self.interval = interval
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +053069
Rushabh Mehta29a75c42011-08-25 19:17:44 +053070 self.glc = get_obj('GL Control')
71 self.cash_accounts = [d[0] for d in webnotes.conn.sql("""
72 select name from tabAccount
73 where account_type='Bank or Cash'
74 and company = %s and docstatus = 0
75 """, company)]
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +053076
77 self.receivables_group = webnotes.conn.get_value('Company', company,'receivables_group')
78 self.payables_group = webnotes.conn.get_value('Company', company,'payables_group')
79
80 # list of bank and cash accounts
81 self.bc_list = [s[0] for s in webnotes.conn.sql("select name from tabAccount where account_type='Bank or Cash'")]
82
Rushabh Mehta29a75c42011-08-25 19:17:44 +053083
84 def timeline(self):
85 """
86 get the timeline for the dashboard
87 """
88 import webnotes
89 from webnotes.utils import add_days
90 tl = []
91
92 if self.start > self.end:
93 webnotes.msgprint("Start must be before end", raise_exception=1)
94
95 curr = self.start
96 tl.append(curr)
97
98 while curr < self.end:
99 curr = add_days(curr, self.interval, 'date')
100 tl.append(curr)
101
102 tl.append(self.end)
103
104 return tl
105
106 def generate(self, opts):
107 """
108 Generate the dasboard
109 """
Rushabh Mehtac7fbbba2011-08-30 13:45:57 +0530110 from webnotes.utils import flt
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530111 tl = self.timeline()
112 self.out = []
113
114 for i in range(len(tl)-1):
Rushabh Mehtac7fbbba2011-08-30 13:45:57 +0530115 self.out.append([tl[i+1].strftime('%Y-%m-%d'), flt(self.value(opts, tl[i], tl[i+1])) or 0])
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530116
117 return self.out
118
119 def get_account_balance(self, acc, start):
120 """
121 Get as on account balance
122 """
123 import webnotes
124 # add abbreviation to company
125
126 if not acc.endswith(self.abbr):
127 acc += ' - ' + self.abbr
128
129 # get other reqd parameters
130 try:
131 globals().update(webnotes.conn.sql('select debit_or_credit, lft, rgt from tabAccount where name=%s', acc, as_dict=1)[0])
132 except Exception,e:
133 webnotes.msgprint('Wrongly defined account: ' + acc)
134 print acc
135 raise e
136
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530137 return self.glc.get_as_on_balance(acc, self.get_fiscal_year(start), start, debit_or_credit, lft, rgt)
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530138
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530139 def get_fiscal_year(self, dt):
140 """
141 get fiscal year from date
142 """
143 import webnotes
Rushabh Mehta0b230d12011-08-30 13:55:55 +0530144 return webnotes.conn.sql("""
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530145 select name from `tabFiscal Year`
146 where year_start_date <= %s and
147 DATE_ADD(year_start_date, INTERVAL 1 YEAR) >= %s
148 """, (dt, dt))[0][0]
149
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530150 def get_creation_trend(self, doctype, start, end):
151 """
152 Get creation # of creations in period
153 """
154 import webnotes
155 return int(webnotes.conn.sql("""
156 select count(*) from `tab%s` where creation between %s and %s and docstatus=1
157 """ % (doctype, '%s','%s'), (start, end))[0][0])
158
Rushabh Mehta0a187be2011-08-25 19:28:02 +0530159 def get_account_amt(self, acc, start, end, debit_or_credit):
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530160 """
161 Get debit, credit over a period
162 """
163 import webnotes
164 # add abbreviation to company
165
166 if not acc.endswith(self.abbr):
167 acc += ' - ' + self.abbr
168
169 ret = webnotes.conn.sql("""
170 select ifnull(sum(ifnull(t1.debit,0)),0), ifnull(sum(ifnull(t1.credit,0)),0)
171 from `tabGL Entry` t1, tabAccount t2
172 where t1.account = t2.name
173 and t2.is_pl_account = 'Yes'
174 and t2.debit_or_credit=%s
175 and ifnull(t1.is_cancelled, 'No')='No'
176 and t1.posting_date between %s and %s
Rushabh Mehta0a187be2011-08-25 19:28:02 +0530177 """, (debit_or_credit, start, end))[0]
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530178
Rushabh Mehtac91f88c2011-08-25 19:28:56 +0530179 return debit_or_credit=='Credit' and float(ret[1]-ret[0]) or float(ret[0]-ret[1])
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530180
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530181 def get_bank_amt(self, debit_or_credit, master_type, start, end):
182 """
183 Get collection (reduction in receivables over a period)
184 """
185 import webnotes
186
187 reg = '('+'|'.join(self.bc_list) + ')'
188
189 return webnotes.conn.sql("""
190 select sum(t1.%s)
191 from `tabGL Entry` t1, tabAccount t2
192 where t1.account = t2.name
193 and t2.master_type='%s'
194 and t1.%s > 0
195 and t1.against REGEXP '%s'
196 and ifnull(t1.is_cancelled, 'No')='No'
197 and t1.posting_date between '%s' and '%s'
198 """ % (debit_or_credit, master_type, debit_or_credit, reg, start, end))[0][0]
199
200
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530201 def value(self, opts, start, end):
202 """
203 Value of the series on a particular date
204 """
205 import webnotes
206 if opts['type']=='account':
Rushabh Mehta0a187be2011-08-25 19:28:02 +0530207 debit_or_credit = 'Debit'
208 if opts['account']=='Income':
209 debit_or_credit = 'Credit'
210
211 return self.get_account_amt(opts['account'], start, end, debit_or_credit)
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530212
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530213 elif opts['type']=='receivables':
214 return self.get_account_balance(self.receivables_group, end)[2]
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530215
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530216 elif opts['type']=='payables':
217 return self.get_account_balance(self.payables_group, end)[2]
218
219 elif opts['type']=='collection':
220 return self.get_bank_amt('credit', 'Customer', start, end)
221
222 elif opts['type']=='payments':
Rushabh Mehta0b230d12011-08-30 13:55:55 +0530223 return self.get_bank_amt('debit', 'Supplier', start, end)
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530224
225 elif opts['type']=='creation':
226 return self.get_creation_trend(opts['doctype'], start, end)
227
228
229def load_dashboard(args):
230 """
231 Get dashboard based on
232 1. Company (default company)
233 2. Start Date (last 3 months)
234 3. End Date (today)
235 4. Interval (7 days)
236 """
237 dl = []
238 import json
239 args = json.loads(args)
240 dw = DashboardWidget(args['company'], args['start'], args['end'], int(args['interval']))
241
242 # render the dashboards
243 for d in dashboards:
244 dl.append([d, dw.generate(d)])
245
246 return dl
247
248if __name__=='__main__':
249 import sys
250 sys.path.append('/var/www/webnotes/wnframework/cgi-bin')
251 from webnotes.db import Database
252 import webnotes
253 webnotes.conn = Database(use_default=1)
254 webnotes.session = {'user':'Administrator'}
255 print load_dashboard("""{
256 "company": "My Test",
257 "start": "2011-05-01",
258 "end": "2011-08-01",
259 "interval": "7"
260 }""")