api_organisation.py 16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25



"""
Copyright (C) 2014 2015 2016 Digital Freedom Foundation


  This file is part of GNUKhata:A modular,robust and Free Accounting System.

  GNUKhata is Free Software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as
  published by the Free Software Foundation; either version 3 of
  the License, or (at your option) any later version.and old.stockflag = 's'

  GNUKhata is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public
  License along with GNUKhata (COPYING); if not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA  02110-1301  USA59 Temple Place, Suite 330,


26
Contributor:
27
28
29
30
31
32
"Krishnakant Mane" <kk@gmail.com>
"Ishan Masdekar " <imasdekar@dff.org.in>
"Navin Karkera" <navin@dff.org.in>
"""


33
from pyramid.view import view_defaults,  view_config
34
from gkcore.views.api_login import authCheck
35
from gkcore import eng, enumdict
36
from pyramid.request import Request
37
from gkcore.models import gkdb
38
from sqlalchemy.sql import select, distinct
39
import json
40
from sqlalchemy.engine.base import Connection
41
from sqlalchemy import and_
42
43
import jwt
import gkcore
44
from gkcore.models.meta import dbconnect
45

46

47
@view_defaults(route_name='organisations')
48
49
class api_organisation(object):
	def __init__(self,request):
50
		self.request = Request
51
		self.request = request
52
53
		self.con = Connection
		print "Organisation initialized"
54

55
56
	@view_config(request_method='GET', renderer ='json')
	def getOrgs(self):
57
		try:
58
			self.con=eng.connect()
59
			result = self.con.execute(select([gkdb.organisation.c.orgname, gkdb.organisation.c.orgtype]).order_by(gkdb.organisation.c.orgname).distinct())
60
61
62
			orgs = []
			for row in result:
				orgs.append({"orgname":row["orgname"], "orgtype":row["orgtype"]})
63
			self.con.close()
64
65
			return {"gkstatus":enumdict["Success"], "gkdata":orgs}
		except:
66
			self.con.close()
67
			return {"gkstatus":enumdict["ConnectionFailed"]}
68

69
70
	@view_config(route_name='orgyears', request_method='GET', renderer ='json')
	def getYears(self):
71
		try:
72
			self.con = eng.connect()
73
			result = self.con.execute(select([gkdb.organisation.c.yearstart, gkdb.organisation.c.yearend,gkdb.organisation.c.orgcode]).where(and_(gkdb.organisation.c.orgname==self.request.matchdict["orgname"], gkdb.organisation.c.orgtype == self.request.matchdict["orgtype"])))
74
75
76
			years = []
			for row in result:
				years.append({"yearstart":str(row["yearstart"]), "yearend":str(row["yearend"]),"orgcode":row["orgcode"]})
77
			self.con.close()
78
79
			return {"gkstatus":enumdict["Success"],"gkdata":years}
		except:
80
			self.con.close()
81
			return {"gkstatus":enumdict["ConnectionFailed"]}
82

83
84
	@view_config(request_method='POST',renderer='json')
	def postOrg(self):
85

86
		try:
87
			self.con = eng.connect()
88
			dataset = self.request.json_body
89
90
			orgdata = dataset["orgdetails"]
			userdata = dataset["userdetails"]
91
			result = self.con.execute(gkdb.organisation.insert(),[orgdata])
92
			if result.rowcount==1:
93
				code = self.con.execute(select([gkdb.organisation.c.orgcode]).where(and_(gkdb.organisation.c.orgname==orgdata["orgname"], gkdb.organisation.c.orgtype==orgdata["orgtype"], gkdb.organisation.c.yearstart==orgdata["yearstart"], gkdb.organisation.c.yearend==orgdata["yearend"])))
94
				orgcode = code.fetchone()
95
				try:
96
					currentassets= {"groupname":"Current Assets","orgcode":orgcode["orgcode"]}
97
98
					result = self.con.execute(gkdb.groupsubgroups.insert(),currentassets)
					result = self.con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Current Assets",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
99
					grpcode = result.fetchone()
100
					result = self.con.execute(gkdb.groupsubgroups.insert(),[{"groupname":"Bank","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]},{"groupname":"Cash","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]},{"groupname":"Inventory","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]},{"groupname":"Loans & Advance","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]},{"groupname":"Sundry Debtors","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]} ])
101

102
					currentliability= {"groupname":"Current Liabilities","orgcode":orgcode["orgcode"]}
103
104
					result = self.con.execute(gkdb.groupsubgroups.insert(),currentliability)
					result = self.con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Current Liabilities",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
105
					grpcode = result.fetchone()
106
					result = self.con.execute(gkdb.groupsubgroups.insert(),[{"groupname":"Provisions","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]},{"groupname":"Sundry Creditors for Expense","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]},{"groupname":"Sundry Creditors for Purchase","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]}])
107

108
					directexpense= {"groupname":"Direct Expense","orgcode":orgcode["orgcode"]}
109
					result = self.con.execute(gkdb.groupsubgroups.insert(),directexpense)
110

111
					directincome= {"groupname":"Direct Income","orgcode":orgcode["orgcode"]}
112
					result = self.con.execute(gkdb.groupsubgroups.insert(),directincome)
113

114
					fixedassets= {"groupname":"Fixed Assets","orgcode":orgcode["orgcode"]}
115
116
					result = self.con.execute(gkdb.groupsubgroups.insert(),fixedassets)
					result = self.con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Fixed Assets",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
117
					grpcode = result.fetchone()
118
					result = self.con.execute(gkdb.groupsubgroups.insert(),[{"groupname":"Building","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]},{"groupname":"Furniture","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]},{"groupname":"Land","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]},{"groupname":"Plant & Machinery","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]} ])
119

120
					indirectexpense= {"groupname":"Indirect Expense","orgcode":orgcode["orgcode"]}
121
					result = self.con.execute(gkdb.groupsubgroups.insert(),indirectexpense)
122

123
					indirectincome= {"groupname":"Indirect Income","orgcode":orgcode["orgcode"]}
124
					result = self.con.execute(gkdb.groupsubgroups.insert(),indirectincome)
125

126
					investment= {"groupname":"Investments","orgcode":orgcode["orgcode"]}
127
128
					result = self.con.execute(gkdb.groupsubgroups.insert(),investment)
					result = self.con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Investments",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
129
					grpcode = result.fetchone()
130
					result = self.con.execute(gkdb.groupsubgroups.insert(),[{"groupname":"Investment in Bank Deposits","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]},{"groupname":"Investment in Shares & Debentures","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]}, ])
131

132
					loansasset= {"groupname":"Loans(Asset)","orgcode":orgcode["orgcode"]}
133
					result = self.con.execute(gkdb.groupsubgroups.insert(),loansasset)
134

135
					loansliab= {"groupname":"Loans(Liability)","orgcode":orgcode["orgcode"]}
136
137
					result = self.con.execute(gkdb.groupsubgroups.insert(),loansliab)
					result = self.con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Loans(Liability)",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
138
					grpcode = result.fetchone()
139
					result = self.con.execute(gkdb.groupsubgroups.insert(),[{"groupname":"Secured","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]},{"groupname":"Unsecured","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]} ])
140

141
					reserves= {"groupname":"Reserves","orgcode":orgcode["orgcode"]}
142
					result = self.con.execute(gkdb.groupsubgroups.insert(),reserves)
143

144
					result = self.con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Direct Income",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
145
146
					grpcode = result.fetchone()
					if orgdata["orgtype"] == "Profit Making":
147
						result = self.con.execute(gkdb.groupsubgroups.insert(),[{"groupname":"Capital","orgcode":orgcode["orgcode"]},{"groupname":"Miscellaneous Expenses(Asset)","orgcode":orgcode["orgcode"]}])
148

149
						result = self.con.execute(gkdb.accounts.insert(),{"accountname":"Profit & Loss","groupcode":grpcode["groupcode"],"orgcode":orgcode["orgcode"]})
150

151
					else:
152
						result = self.con.execute(gkdb.groupsubgroups.insert(),{"groupname":"Corpus","orgcode":orgcode["orgcode"]})
153

154
						result = self.con.execute(gkdb.accounts.insert(),{"accountname":"Income & Expenditure","groupcode":grpcode["groupcode"],"orgcode":orgcode["orgcode"]})
155

156
					result = self.con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Inventory",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
157
					grpcode = result.fetchone()
158
					result = self.con.execute(gkdb.accounts.insert(),[{"accountname":"Closing Stock","groupcode":grpcode["groupcode"],"orgcode":orgcode["orgcode"]},{"accountname":"Stock at the Beginning","groupcode":grpcode["groupcode"],"orgcode":orgcode["orgcode"]}])
159

160
					result = self.con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Direct Expense",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
161
					grpcode = result.fetchone()
162
					result = self.con.execute(gkdb.accounts.insert(),{"accountname":"Opening Stock","groupcode":grpcode["groupcode"],"orgcode":orgcode["orgcode"]})
163
164
165



166
167
					userdata["orgcode"] = orgcode["orgcode"]
					userdata["userrole"] = -1
168
					result = self.con.execute(gkdb.users.insert(),[userdata])
169
					if result.rowcount==1:
170
						result = self.con.execute(select([gkdb.users.c.userid]).where(and_(gkdb.users.c.username==userdata["username"], gkdb.users.c.userpassword== userdata["userpassword"], gkdb.users.c.orgcode==userdata["orgcode"])) )
171
172
						if result.rowcount == 1:
							record = result.fetchone()
173

174
							token = jwt.encode({"orgcode":userdata["orgcode"],"userid":record["userid"]},gkcore.secret,algorithm='HS256')
175
							self.con.close()
176
							return {"gkstatus":enumdict["Success"],"token":token }
177
						else:
178
							self.con.close()
179
							return {"gkstatus":enumdict["ConnectionFailed"]}
180
					else:
181
							self.con.close()
182
							return {"gkstatus":enumdict["ConnectionFailed"]}
183
				except:
184
185
					result = self.con.execute(gkdb.organisation.delete().where(gkdb.organisation.c.orgcode==orgcode["orgcode"]))
					self.con.close()
186
					return {"gkstatus":enumdict["ConnectionFailed"]}
187
			else:
188
				self.con.close()
189
				return {"gkstatus":enumdict["ConnectionFailed"]}
190
		except:
191
			self.con.close()
192
			return {"gkstatus":enumdict["ConnectionFailed"]}
193

ishan masdekar's avatar
ishan masdekar committed
194
	@view_config(route_name='organisation', request_method='GET',renderer='json')
195
	def getOrg(self):
196
		try:
197
			token = self.request.headers["gktoken"]
198
		except:
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
			return  {"gkstatus":  enumdict["UnauthorisedAccess"]}
		authDetails = authCheck(token)
		if authDetails["auth"]==False:
			return {"gkstatus":enumdict["UnauthorisedAccess"]}
		else:
			try:
				self.con = eng.connect()
				result = self.con.execute(select([gkdb.organisation]).where(gkdb.organisation.c.orgcode==authDetails["orgcode"]))
				row = result.fetchone()
				if(row["orgcity"]==None):
					orgcity=""
				else:
					orgcity=row["orgcity"]

				if(row["orgaddr"]==None):
					orgaddr=""
				else:
					orgaddr=row["orgaddr"]

				if(row["orgpincode"]==None):
					orgpincode=""
				else:
					orgpincode=row["orgpincode"]

				if(row["orgstate"]==None):
					orgstate=""
				else:
					orgstate=row["orgstate"]

				if(row["orgcountry"]==None):
					orgcountry=""
				else:
					orgcountry=row["orgcountry"]

				if(row["orgtelno"]==None):
					orgtelno=""
				else:
					orgtelno=row["orgtelno"]

				if(row["orgfax"]==None):
					orgfax=""
				else:
					orgfax=row["orgfax"]

				if(row["orgwebsite"]==None):
					orgwebsite=""
				else:
					orgwebsite=row["orgwebsite"]

				if(row["orgemail"]==None):
					orgemail=""
				else:
					orgemail=row["orgemail"]

				if(row["orgpan"]==None):
					orgpan=""
				else:
					orgpan=row["orgpan"]

				if(row["orgmvat"]==None):
					orgmvat=""
				else:
					orgmvat=row["orgmvat"]

				if(row["orgstax"]==None):
					orgstax=""
				else:
					orgstax=row["orgstax"]

				if(row["orgregno"]==None):
					orgregno=""
				else:
					orgregno=row["orgregno"]

				if(row["orgregdate"]==None):
					orgregdate=""
				else:
					orgregdate=row["orgregdate"]

				if(row["orgfcrano"]==None):
					orgfcrano=""
				else:
					orgfcrano=row["orgfcrano"]

				if(row["orgfcradate"]==None):
					orgfcradate=""
				else:
					orgfcradate=row["orgfcradate"]


				orgDetails={"orgname":row["orgname"], "orgtype":row["orgtype"], "yearstart":str(row["yearstart"]), "yearend":str(row["yearend"]),"orgcity":orgcity, "orgaddr":orgaddr, "orgpincode":orgpincode, "orgstate":orgstate, "orgcountry":orgcountry, "orgtelno":orgtelno, "orgfax":orgfax, "orgwebsite":orgwebsite, "orgemail":orgemail, "orgpan":orgpan, "orgmvat":orgmvat, "orgstax":orgstax, "orgregno":orgregno, "orgregdate":orgregdate, "orgfcrano":orgfcrano, "orgfcradate":orgfcradate, "roflag":row["roflag"], "booksclosedflag":row["booksclosedflag"]}
				self.con.close()
				print orgDetails
				return {"gkstatus":enumdict["Success"],"gkdata":orgDetails}
			except:
				self.con.close()
				return {"gkstatus":enumdict["ConnectionFailed"]}




300

301
302
	@view_config(request_method='PUT', renderer='json')
	def putOrg(self):
303
304
305
306
307
		token = self.request.headers['gktoken']
		authDetails = authCheck(token)
		if authDetails["auth"]==False:
			return {"gkstatus":enumdict["UnauthorisedAccess"]}
		else:
308
			try:
309
				self.con = eng.connect()
310
				user=self.con.execute(select([gkdb.users.c.userrole]).where(gkdb.users.c.userid == authDetails["userid"] ))
311
312
313
				userRole = user.fetchone()
				dataset = self.request.json_body
				if userRole[0]==-1:
314
315
					result = self.con.execute(gkdb.organisation.update().where(gkdb.organisation.c.orgcode==authDetails["orgcode"]).values(dataset))
					self.con.close()
316
317
318
					return {"gkstatus":enumdict["Success"]}
				else:
					{"gkstatus":  enumdict["BadPrivilege"]}
319
			except:
320
				self.con.close()
321
				return {"gkstatus":  enumdict["ConnectionFailed"]}
322

323
324
	@view_config(request_method='DELETE', renderer='json')
	def deleteOrg(self):
325
326
327
328
329
		token = self.request.headers['gktoken']
		authDetails = authCheck(token)
		if authDetails["auth"]==False:
			return {"gkstatus":enumdict["UnauthorisedAccess"]}
		else:
330
			try:
331
				self.con = eng.connect()
332
				user=self.con.execute(select([gkdb.users.c.userrole]).where(gkdb.users.c.userid == authDetails["userid"] ))
333
334
				userRole = user.fetchone()
				if userRole[0]==-1:
335
336
					result = self.con.execute(gkdb.organisation.delete().where(gkdb.organisation.c.orgcode==authDetails["orgcode"]))
					self.con.close()
337
338
339
					return {"gkstatus":enumdict["Success"]}
				else:
					{"gkstatus":  enumdict["BadPrivilege"]}
340
			except:
341
				self.con.close()
342
				return {"gkstatus":  enumdict["ConnectionFailed"]}
343

344
	@view_config(request_param='orgcode', request_method='GET',renderer='json')
345
	def getOrgcode(self):
346
		try:
ishan masdekar's avatar
ishan masdekar committed
347
			token = self.request.headers["gktoken"]
348
		except:
ishan masdekar's avatar
ishan masdekar committed
349
350
351
352
353
354
			return  {"gkstatus":  enumdict["UnauthorisedAccess"]}
		authDetails = authCheck(token)
		if authDetails["auth"]==False:
			return {"gkstatus":enumdict["UnauthorisedAccess"]}
		else:
			return {"gkstatus":enumdict["Success"],"gkdata":authDetails["orgcode"]}
355
356
357
358
359
360
361
362
363
364


	@view_config(request_method='PUT', request_param="type=editorganisation", renderer='json')
	def editOrg(self):
		token = self.request.headers['gktoken']
		authDetails = authCheck(token)
		if authDetails["auth"]==False:
			return {"gkstatus":enumdict["UnauthorisedAccess"]}
		else:
			try:
365
				self.con =eng.connect()
366
367
368
369
370
371
372
				dataset = self.request.json_body
				result = self.con.execute(gkdb.organisation.update().where(gkdb.organisation.c.orgcode==dataset["orgcode"]).values(dataset))
				self.con.close()
				return {"gkstatus":enumdict["Success"]}
			except:
				self.con.close()
				return {"gkstatus":enumdict["ConnectionFailed"]}