blob: c17e974df9381f42acdd3f25b33faa38bb077a63 [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 """
110 tl = self.timeline()
111 self.out = []
112
113 for i in range(len(tl)-1):
114 self.out.append([tl[i+1].strftime('%Y-%m-%d'), self.value(opts, tl[i], tl[i+1]) or 0])
115
116 return self.out
117
118 def get_account_balance(self, acc, start):
119 """
120 Get as on account balance
121 """
122 import webnotes
123 # add abbreviation to company
124
125 if not acc.endswith(self.abbr):
126 acc += ' - ' + self.abbr
127
128 # get other reqd parameters
129 try:
130 globals().update(webnotes.conn.sql('select debit_or_credit, lft, rgt from tabAccount where name=%s', acc, as_dict=1)[0])
131 except Exception,e:
132 webnotes.msgprint('Wrongly defined account: ' + acc)
133 print acc
134 raise e
135
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530136 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 +0530137
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530138 def get_fiscal_year(self, dt):
139 """
140 get fiscal year from date
141 """
142 import webnotes
143 self.fiscal_year = webnotes.conn.sql("""
144 select name from `tabFiscal Year`
145 where year_start_date <= %s and
146 DATE_ADD(year_start_date, INTERVAL 1 YEAR) >= %s
147 """, (dt, dt))[0][0]
148
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530149 def get_creation_trend(self, doctype, start, end):
150 """
151 Get creation # of creations in period
152 """
153 import webnotes
154 return int(webnotes.conn.sql("""
155 select count(*) from `tab%s` where creation between %s and %s and docstatus=1
156 """ % (doctype, '%s','%s'), (start, end))[0][0])
157
Rushabh Mehta0a187be2011-08-25 19:28:02 +0530158 def get_account_amt(self, acc, start, end, debit_or_credit):
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530159 """
160 Get debit, credit over a period
161 """
162 import webnotes
163 # add abbreviation to company
164
165 if not acc.endswith(self.abbr):
166 acc += ' - ' + self.abbr
167
168 ret = webnotes.conn.sql("""
169 select ifnull(sum(ifnull(t1.debit,0)),0), ifnull(sum(ifnull(t1.credit,0)),0)
170 from `tabGL Entry` t1, tabAccount t2
171 where t1.account = t2.name
172 and t2.is_pl_account = 'Yes'
173 and t2.debit_or_credit=%s
174 and ifnull(t1.is_cancelled, 'No')='No'
175 and t1.posting_date between %s and %s
Rushabh Mehta0a187be2011-08-25 19:28:02 +0530176 """, (debit_or_credit, start, end))[0]
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530177
Rushabh Mehtac91f88c2011-08-25 19:28:56 +0530178 return debit_or_credit=='Credit' and float(ret[1]-ret[0]) or float(ret[0]-ret[1])
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530179
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530180 def get_bank_amt(self, debit_or_credit, master_type, start, end):
181 """
182 Get collection (reduction in receivables over a period)
183 """
184 import webnotes
185
186 reg = '('+'|'.join(self.bc_list) + ')'
187
188 return webnotes.conn.sql("""
189 select sum(t1.%s)
190 from `tabGL Entry` t1, tabAccount t2
191 where t1.account = t2.name
192 and t2.master_type='%s'
193 and t1.%s > 0
194 and t1.against REGEXP '%s'
195 and ifnull(t1.is_cancelled, 'No')='No'
196 and t1.posting_date between '%s' and '%s'
197 """ % (debit_or_credit, master_type, debit_or_credit, reg, start, end))[0][0]
198
199
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530200 def value(self, opts, start, end):
201 """
202 Value of the series on a particular date
203 """
204 import webnotes
205 if opts['type']=='account':
Rushabh Mehta0a187be2011-08-25 19:28:02 +0530206 debit_or_credit = 'Debit'
207 if opts['account']=='Income':
208 debit_or_credit = 'Credit'
209
210 return self.get_account_amt(opts['account'], start, end, debit_or_credit)
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530211
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530212 elif opts['type']=='receivables':
213 return self.get_account_balance(self.receivables_group, end)[2]
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530214
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530215 elif opts['type']=='payables':
216 return self.get_account_balance(self.payables_group, end)[2]
217
218 elif opts['type']=='collection':
219 return self.get_bank_amt('credit', 'Customer', start, end)
220
221 elif opts['type']=='payments':
222 return self.get_bank_amt('credit', 'Supplier', start, end)
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530223
224 elif opts['type']=='creation':
225 return self.get_creation_trend(opts['doctype'], start, end)
226
227
228def load_dashboard(args):
229 """
230 Get dashboard based on
231 1. Company (default company)
232 2. Start Date (last 3 months)
233 3. End Date (today)
234 4. Interval (7 days)
235 """
236 dl = []
237 import json
238 args = json.loads(args)
239 dw = DashboardWidget(args['company'], args['start'], args['end'], int(args['interval']))
240
241 # render the dashboards
242 for d in dashboards:
243 dl.append([d, dw.generate(d)])
244
245 return dl
246
247if __name__=='__main__':
248 import sys
249 sys.path.append('/var/www/webnotes/wnframework/cgi-bin')
250 from webnotes.db import Database
251 import webnotes
252 webnotes.conn = Database(use_default=1)
253 webnotes.session = {'user':'Administrator'}
254 print load_dashboard("""{
255 "company": "My Test",
256 "start": "2011-05-01",
257 "end": "2011-08-01",
258 "interval": "7"
259 }""")