blob: 0dc2d6ddd4dccfe87384b02edac7cfe534290c33 [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
Anand Doshi3a23a572011-12-12 19:50:09 +0530136
137 fiscal_year = self.get_fiscal_year(start)
138 if fiscal_year:
139 return self.glc.get_as_on_balance(acc, fiscal_year, start, debit_or_credit, lft, rgt)
140 else:
141 webnotes.msgprint('Please select the START DATE and END DATE such that\
142 they fall within the <b>same fiscal year</b> as defined in\
143 Setup > System > Fiscal Year.', raise_exception=1)
144
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530145
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530146 def get_fiscal_year(self, dt):
147 """
148 get fiscal year from date
149 """
150 import webnotes
Anand Doshi3a23a572011-12-12 19:50:09 +0530151 fiscal_year = webnotes.conn.sql("""
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530152 select name from `tabFiscal Year`
153 where year_start_date <= %s and
154 DATE_ADD(year_start_date, INTERVAL 1 YEAR) >= %s
Anand Doshi3a23a572011-12-12 19:50:09 +0530155 """, (dt, dt))
156 return fiscal_year and (fiscal_year[0] and fiscal_year[0][0]) or None
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530157
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530158 def get_creation_trend(self, doctype, start, end):
159 """
160 Get creation # of creations in period
161 """
162 import webnotes
163 return int(webnotes.conn.sql("""
164 select count(*) from `tab%s` where creation between %s and %s and docstatus=1
165 """ % (doctype, '%s','%s'), (start, end))[0][0])
166
Rushabh Mehta0a187be2011-08-25 19:28:02 +0530167 def get_account_amt(self, acc, start, end, debit_or_credit):
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530168 """
169 Get debit, credit over a period
170 """
171 import webnotes
172 # add abbreviation to company
173
174 if not acc.endswith(self.abbr):
175 acc += ' - ' + self.abbr
176
177 ret = webnotes.conn.sql("""
178 select ifnull(sum(ifnull(t1.debit,0)),0), ifnull(sum(ifnull(t1.credit,0)),0)
179 from `tabGL Entry` t1, tabAccount t2
180 where t1.account = t2.name
181 and t2.is_pl_account = 'Yes'
182 and t2.debit_or_credit=%s
183 and ifnull(t1.is_cancelled, 'No')='No'
184 and t1.posting_date between %s and %s
Rushabh Mehta0a187be2011-08-25 19:28:02 +0530185 """, (debit_or_credit, start, end))[0]
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530186
Rushabh Mehtac91f88c2011-08-25 19:28:56 +0530187 return debit_or_credit=='Credit' and float(ret[1]-ret[0]) or float(ret[0]-ret[1])
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530188
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530189 def get_bank_amt(self, debit_or_credit, master_type, start, end):
190 """
191 Get collection (reduction in receivables over a period)
192 """
193 import webnotes
194
195 reg = '('+'|'.join(self.bc_list) + ')'
196
197 return webnotes.conn.sql("""
198 select sum(t1.%s)
199 from `tabGL Entry` t1, tabAccount t2
200 where t1.account = t2.name
201 and t2.master_type='%s'
202 and t1.%s > 0
203 and t1.against REGEXP '%s'
204 and ifnull(t1.is_cancelled, 'No')='No'
205 and t1.posting_date between '%s' and '%s'
206 """ % (debit_or_credit, master_type, debit_or_credit, reg, start, end))[0][0]
207
208
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530209 def value(self, opts, start, end):
210 """
211 Value of the series on a particular date
212 """
213 import webnotes
214 if opts['type']=='account':
Rushabh Mehta0a187be2011-08-25 19:28:02 +0530215 debit_or_credit = 'Debit'
216 if opts['account']=='Income':
217 debit_or_credit = 'Credit'
218
219 return self.get_account_amt(opts['account'], start, end, debit_or_credit)
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530220
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530221 elif opts['type']=='receivables':
222 return self.get_account_balance(self.receivables_group, end)[2]
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530223
Rushabh Mehtaae5cdeb2011-08-30 13:24:49 +0530224 elif opts['type']=='payables':
225 return self.get_account_balance(self.payables_group, end)[2]
226
227 elif opts['type']=='collection':
228 return self.get_bank_amt('credit', 'Customer', start, end)
229
230 elif opts['type']=='payments':
Rushabh Mehta0b230d12011-08-30 13:55:55 +0530231 return self.get_bank_amt('debit', 'Supplier', start, end)
Rushabh Mehta29a75c42011-08-25 19:17:44 +0530232
233 elif opts['type']=='creation':
234 return self.get_creation_trend(opts['doctype'], start, end)
235
236
237def load_dashboard(args):
238 """
239 Get dashboard based on
240 1. Company (default company)
241 2. Start Date (last 3 months)
242 3. End Date (today)
243 4. Interval (7 days)
244 """
245 dl = []
246 import json
247 args = json.loads(args)
248 dw = DashboardWidget(args['company'], args['start'], args['end'], int(args['interval']))
249
250 # render the dashboards
251 for d in dashboards:
252 dl.append([d, dw.generate(d)])
253
254 return dl
255
256if __name__=='__main__':
257 import sys
258 sys.path.append('/var/www/webnotes/wnframework/cgi-bin')
259 from webnotes.db import Database
260 import webnotes
261 webnotes.conn = Database(use_default=1)
262 webnotes.session = {'user':'Administrator'}
263 print load_dashboard("""{
264 "company": "My Test",
265 "start": "2011-05-01",
266 "end": "2011-08-01",
267 "interval": "7"
Anand Doshi3a23a572011-12-12 19:50:09 +0530268 }""")