blob: e8f11fbc5cd830699f6856fa4537e027099943e9 [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 Mehtaf17ce7b2012-02-13 16:50:52 +053057import webnotes
58
Rushabh Mehta29a75c42011-08-25 19:17:44 +053059class DashboardWidget:
60 def __init__(self, company, start, end, interval):
Rushabh Mehta29a75c42011-08-25 19:17:44 +053061 from webnotes.utils import getdate
62 from webnotes.model.code import get_obj
63
64 self.company = company
65 self.abbr = webnotes.conn.get_value('Company', company, 'abbr')
66 self.start = getdate(start)
67 self.end = getdate(end)
68
69 self.interval = interval
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +053070
Rushabh Mehta29a75c42011-08-25 19:17:44 +053071 self.glc = get_obj('GL Control')
72 self.cash_accounts = [d[0] for d in webnotes.conn.sql("""
73 select name from tabAccount
74 where account_type='Bank or Cash'
75 and company = %s and docstatus = 0
76 """, company)]
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +053077
78 self.receivables_group = webnotes.conn.get_value('Company', company,'receivables_group')
79 self.payables_group = webnotes.conn.get_value('Company', company,'payables_group')
80
81 # list of bank and cash accounts
82 self.bc_list = [s[0] for s in webnotes.conn.sql("select name from tabAccount where account_type='Bank or Cash'")]
83
Rushabh Mehta29a75c42011-08-25 19:17:44 +053084
85 def timeline(self):
86 """
87 get the timeline for the dashboard
88 """
89 import webnotes
90 from webnotes.utils import add_days
91 tl = []
92
93 if self.start > self.end:
94 webnotes.msgprint("Start must be before end", raise_exception=1)
95
96 curr = self.start
97 tl.append(curr)
98
99 while curr < self.end:
100 curr = add_days(curr, self.interval, 'date')
101 tl.append(curr)
102
103 tl.append(self.end)
104
105 return tl
106
107 def generate(self, opts):
108 """
109 Generate the dasboard
110 """
Rushabh Mehtac7fbbba2011-08-30 13:45:57 +0530111 from webnotes.utils import flt
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530112 tl = self.timeline()
113 self.out = []
114
115 for i in range(len(tl)-1):
Rushabh Mehtac7fbbba2011-08-30 13:45:57 +0530116 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 +0530117
118 return self.out
119
120 def get_account_balance(self, acc, start):
121 """
122 Get as on account balance
123 """
124 import webnotes
125 # add abbreviation to company
126
127 if not acc.endswith(self.abbr):
128 acc += ' - ' + self.abbr
129
130 # get other reqd parameters
131 try:
132 globals().update(webnotes.conn.sql('select debit_or_credit, lft, rgt from tabAccount where name=%s', acc, as_dict=1)[0])
133 except Exception,e:
134 webnotes.msgprint('Wrongly defined account: ' + acc)
135 print acc
136 raise e
Anand Doshi3a23a572011-12-12 19:50:09 +0530137
138 fiscal_year = self.get_fiscal_year(start)
139 if fiscal_year:
140 return self.glc.get_as_on_balance(acc, fiscal_year, start, debit_or_credit, lft, rgt)
141 else:
142 webnotes.msgprint('Please select the START DATE and END DATE such that\
Anand Doshi130ae982011-12-12 20:24:06 +0530143 they fall within <b>fiscal year(s)</b> as defined in\
Anand Doshi3a23a572011-12-12 19:50:09 +0530144 Setup > System > Fiscal Year.', raise_exception=1)
145
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530146
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530147 def get_fiscal_year(self, dt):
148 """
149 get fiscal year from date
150 """
151 import webnotes
Anand Doshi3a23a572011-12-12 19:50:09 +0530152 fiscal_year = webnotes.conn.sql("""
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530153 select name from `tabFiscal Year`
154 where year_start_date <= %s and
155 DATE_ADD(year_start_date, INTERVAL 1 YEAR) >= %s
Anand Doshi3a23a572011-12-12 19:50:09 +0530156 """, (dt, dt))
157 return fiscal_year and (fiscal_year[0] and fiscal_year[0][0]) or None
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530158
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530159 def get_creation_trend(self, doctype, start, end):
160 """
161 Get creation # of creations in period
162 """
163 import webnotes
164 return int(webnotes.conn.sql("""
165 select count(*) from `tab%s` where creation between %s and %s and docstatus=1
166 """ % (doctype, '%s','%s'), (start, end))[0][0])
167
Rushabh Mehta0a187be2011-08-25 19:28:02 +0530168 def get_account_amt(self, acc, start, end, debit_or_credit):
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530169 """
170 Get debit, credit over a period
171 """
172 import webnotes
173 # add abbreviation to company
174
175 if not acc.endswith(self.abbr):
176 acc += ' - ' + self.abbr
177
178 ret = webnotes.conn.sql("""
179 select ifnull(sum(ifnull(t1.debit,0)),0), ifnull(sum(ifnull(t1.credit,0)),0)
180 from `tabGL Entry` t1, tabAccount t2
181 where t1.account = t2.name
182 and t2.is_pl_account = 'Yes'
183 and t2.debit_or_credit=%s
184 and ifnull(t1.is_cancelled, 'No')='No'
185 and t1.posting_date between %s and %s
Rushabh Mehta0a187be2011-08-25 19:28:02 +0530186 """, (debit_or_credit, start, end))[0]
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530187
Rushabh Mehtac91f88c2011-08-25 19:28:56 +0530188 return debit_or_credit=='Credit' and float(ret[1]-ret[0]) or float(ret[0]-ret[1])
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530189
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530190 def get_bank_amt(self, debit_or_credit, master_type, start, end):
191 """
192 Get collection (reduction in receivables over a period)
193 """
194 import webnotes
195
196 reg = '('+'|'.join(self.bc_list) + ')'
197
198 return webnotes.conn.sql("""
199 select sum(t1.%s)
200 from `tabGL Entry` t1, tabAccount t2
201 where t1.account = t2.name
202 and t2.master_type='%s'
203 and t1.%s > 0
204 and t1.against REGEXP '%s'
205 and ifnull(t1.is_cancelled, 'No')='No'
206 and t1.posting_date between '%s' and '%s'
207 """ % (debit_or_credit, master_type, debit_or_credit, reg, start, end))[0][0]
208
209
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530210 def value(self, opts, start, end):
211 """
212 Value of the series on a particular date
213 """
214 import webnotes
215 if opts['type']=='account':
Rushabh Mehta0a187be2011-08-25 19:28:02 +0530216 debit_or_credit = 'Debit'
217 if opts['account']=='Income':
218 debit_or_credit = 'Credit'
219
220 return self.get_account_amt(opts['account'], start, end, debit_or_credit)
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530221
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530222 elif opts['type']=='receivables':
223 return self.get_account_balance(self.receivables_group, end)[2]
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530224
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530225 elif opts['type']=='payables':
226 return self.get_account_balance(self.payables_group, end)[2]
227
228 elif opts['type']=='collection':
229 return self.get_bank_amt('credit', 'Customer', start, end)
230
231 elif opts['type']=='payments':
Rushabh Mehta0b230d12011-08-30 13:55:55 +0530232 return self.get_bank_amt('debit', 'Supplier', start, end)
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530233
234 elif opts['type']=='creation':
235 return self.get_creation_trend(opts['doctype'], start, end)
236
Rushabh Mehtaf17ce7b2012-02-13 16:50:52 +0530237@webnotes.whitelist()
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530238def load_dashboard(args):
239 """
240 Get dashboard based on
241 1. Company (default company)
242 2. Start Date (last 3 months)
243 3. End Date (today)
244 4. Interval (7 days)
245 """
246 dl = []
247 import json
248 args = json.loads(args)
249 dw = DashboardWidget(args['company'], args['start'], args['end'], int(args['interval']))
250
251 # render the dashboards
252 for d in dashboards:
253 dl.append([d, dw.generate(d)])
254
255 return dl
256
257if __name__=='__main__':
258 import sys
259 sys.path.append('/var/www/webnotes/wnframework/cgi-bin')
260 from webnotes.db import Database
261 import webnotes
262 webnotes.conn = Database(use_default=1)
263 webnotes.session = {'user':'Administrator'}
264 print load_dashboard("""{
265 "company": "My Test",
266 "start": "2011-05-01",
267 "end": "2011-08-01",
268 "interval": "7"
Anand Doshi3a23a572011-12-12 19:50:09 +0530269 }""")