api_organisation.py 13.2 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 import eng, enumdict
35
from gkcore.models import gkdb
36
from sqlalchemy.sql import select, distinct
37
import json
38
from sqlalchemy.engine.base import Connection
39
from sqlalchemy import and_
40
41
import jwt
import gkcore
42
43
44
con = Connection
con = eng.connect()

45
@view_defaults(route_name='organisations')
46
47
48
class api_organisation(object):
	def __init__(self,request):
		self.request = request
49

50
51
	@view_config(request_method='GET', renderer ='json')
	def getOrgs(self):
52
		try:
53
			result = con.execute(select([gkdb.organisation.c.orgname, gkdb.organisation.c.orgtype]).order_by(gkdb.organisation.c.orgname).distinct())
54
55
56
57
58
59
			orgs = []
			for row in result:
				orgs.append({"orgname":row["orgname"], "orgtype":row["orgtype"]})
			return {"gkstatus":enumdict["Success"], "gkdata":orgs}
		except:
			return {"gkstatus":enumdict["ConnectionFailed"]}
60

61
62
	@view_config(route_name='orgyears', request_method='GET', renderer ='json')
	def getYears(self):
63
64
65
66
67
68
69
70
		try:
			result = 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"])))
			years = []
			for row in result:
				years.append({"yearstart":str(row["yearstart"]), "yearend":str(row["yearend"]),"orgcode":row["orgcode"]})
			return {"gkstatus":enumdict["Success"],"gkdata":years}
		except:
			return {"gkstatus":enumdict["ConnectionFailed"]}
71

72
73
	@view_config(request_method='POST',renderer='json')
	def postOrg(self):
74

75
76
		try:
			dataset = self.request.json_body
77
78
79
80
			orgdata = dataset["orgdetails"]
			userdata = dataset["userdetails"]
			result = con.execute(gkdb.organisation.insert(),[orgdata])
			if result.rowcount==1:
81
82
				code = 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"])))
				orgcode = code.fetchone()
83
				try:
84

85
86
87
88
89
					currentassets= {"groupname":"Current Assets","orgcode":orgcode["orgcode"]}
					result = con.execute(gkdb.groupsubgroups.insert(),currentassets)
					result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Current Assets",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
					grpcode = result.fetchone()
					result = 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"]} ])
90

91
					currentliability= {"groupname":"Current Liabilities","orgcode":orgcode["orgcode"]}
92
					result = con.execute(gkdb.groupsubgroups.insert(),currentliability)
93
					result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Current Liabilities",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
94
95
					grpcode = result.fetchone()
					result = 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"]}])
96

97
98
					directexpense= {"groupname":"Direct Expense","orgcode":orgcode["orgcode"]}
					result = con.execute(gkdb.groupsubgroups.insert(),directexpense)
99

100
101
					directincome= {"groupname":"Direct Income","orgcode":orgcode["orgcode"]}
					result = con.execute(gkdb.groupsubgroups.insert(),directincome)
102

103
104
105
106
107
					fixedassets= {"groupname":"Fixed Assets","orgcode":orgcode["orgcode"]}
					result = con.execute(gkdb.groupsubgroups.insert(),fixedassets)
					result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Fixed Assets",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
					grpcode = result.fetchone()
					result = 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"]} ])
108

109
110
					indirectexpense= {"groupname":"Indirect Expense","orgcode":orgcode["orgcode"]}
					result = con.execute(gkdb.groupsubgroups.insert(),indirectexpense)
111

112
113
					indirectincome= {"groupname":"Indirect Income","orgcode":orgcode["orgcode"]}
					result = con.execute(gkdb.groupsubgroups.insert(),indirectincome)
114

115
					investment= {"groupname":"Investments","orgcode":orgcode["orgcode"]}
116
					result = con.execute(gkdb.groupsubgroups.insert(),investment)
117
					result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Investments",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
118
119
					grpcode = result.fetchone()
					result = 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"]}, ])
120

121
122
					loansasset= {"groupname":"Loans(Asset)","orgcode":orgcode["orgcode"]}
					result = con.execute(gkdb.groupsubgroups.insert(),loansasset)
123

124
125
126
127
128
					loansliab= {"groupname":"Loans(Liability)","orgcode":orgcode["orgcode"]}
					result = con.execute(gkdb.groupsubgroups.insert(),loansliab)
					result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Loans(Liability)",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
					grpcode = result.fetchone()
					result = con.execute(gkdb.groupsubgroups.insert(),[{"groupname":"Secured","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]},{"groupname":"Unsecured","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]} ])
129

130
131
					reserves= {"groupname":"Reserves","orgcode":orgcode["orgcode"]}
					result = con.execute(gkdb.groupsubgroups.insert(),reserves)
132

133
134
135
					result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Direct Income",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
					grpcode = result.fetchone()
					if orgdata["orgtype"] == "Profit Making":
136
137
						result = con.execute(gkdb.groupsubgroups.insert(),[{"groupname":"Capital","orgcode":orgcode["orgcode"]},{"groupname":"Miscellaneous Expenses(Asset)","orgcode":orgcode["orgcode"]}])

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

140
141
					else:
						result = con.execute(gkdb.groupsubgroups.insert(),{"groupname":"Corpus","orgcode":orgcode["orgcode"]})
142

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

145
146
147
					result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Inventory",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
					grpcode = result.fetchone()
					result = 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"]}])
148

149
150
151
					result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Direct Expense",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
					grpcode = result.fetchone()
					result = con.execute(gkdb.accounts.insert(),{"accountname":"Opening Stock","groupcode":grpcode["groupcode"],"orgcode":orgcode["orgcode"]})
152
153
154



155
156
157
158
					userdata["orgcode"] = orgcode["orgcode"]
					userdata["userrole"] = -1
					result = con.execute(gkdb.users.insert(),[userdata])
					if result.rowcount==1:
159
						result = 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"])) )
160
161
						if result.rowcount == 1:
							record = result.fetchone()
162

163
							token = jwt.encode({"orgcode":userdata["orgcode"],"userid":record["userid"]},gkcore.secret,algorithm='HS256')
164
							return {"gkstatus":enumdict["Success"],"token":token }
165
						else:
166
							return {"gkstatus":enumdict["ConnectionFailed"]}
167
					else:
168
							return {"gkstatus":enumdict["ConnectionFailed"]}
169
				except:
ishan masdekar's avatar
ishan masdekar committed
170
					result = con.execute(gkdb.organisation.delete().where(gkdb.organisation.c.orgcode==orgcode["orgcode"]))
171
					return {"gkstatus":enumdict["ConnectionFailed"]}
172
			else:
173
				return {"gkstatus":enumdict["ConnectionFailed"]}
174
		except:
175
			return {"gkstatus":enumdict["ConnectionFailed"]}
176

177
178
	@view_config(route_name='organisation', request_method='GET',renderer='json')
	def getOrg(self):
179
180
181
182
183
184
185
186
		try:
			result = con.execute(select([gkdb.organisation]).where(gkdb.organisation.c.orgcode==self.request.matchdict["orgcode"]))
			row = result.fetchone()
			orgDetails={"orgname":row["orgname"], "orgtype":row["orgtype"], "yearstart":str(row["yearstart"]), "yearend":str(row["yearend"]),"orgcity":row["orgcity"], "orgaddr":row["orgaddr"], "orgpincode":row["orgpincode"], "orgstate":row["orgstate"], "orgcountry":row["orgcountry"], "orgtelno":row["orgtelno"], "orgfax":row["orgfax"], "orgwebsite":row["orgwebsite"], "orgemail":row["orgemail"], "orgpan":row["orgpan"], "orgmvat":row["orgmvat"], "orgstax":row["orgstax"], "orgregno":row["orgregno"], "orgregdate":row["orgregdate"], "orgfcrano":row["orgfcrano"], "orgfcradate":row["orgfcradate"], "roflag":row["roflag"], "booksclosedflag":row["booksclosedflag"]	}
			return {"gkstatus":enumdict["Success"],"gkdata":orgDetails}
		except:
			return {"gkstatus":enumdict["ConnectionFailed"]}

187
188
	@view_config(request_method='PUT', renderer='json')
	def putOrg(self):
189
190
191
192
193
		token = self.request.headers['gktoken']
		authDetails = authCheck(token)
		if authDetails["auth"]==False:
			return {"gkstatus":enumdict["UnauthorisedAccess"]}
		else:
194
			try:
195
196
197
198
199
200
201
202
				user=con.execute(select([gkdb.users.c.userrole]).where(gkdb.users.c.userid == authDetails["userid"] ))
				userRole = user.fetchone()
				dataset = self.request.json_body
				if userRole[0]==-1:
					result = con.execute(gkdb.organisation.update().where(gkdb.organisation.c.orgcode==authDetails["orgcode"]).values(dataset))
					return {"gkstatus":enumdict["Success"]}
				else:
					{"gkstatus":  enumdict["BadPrivilege"]}
203
			except:
204
				return {"gkstatus":  enumdict["ConnectionFailed"]}
205

206
207
	@view_config(request_method='DELETE', renderer='json')
	def deleteOrg(self):
208
209
210
211
212
		token = self.request.headers['gktoken']
		authDetails = authCheck(token)
		if authDetails["auth"]==False:
			return {"gkstatus":enumdict["UnauthorisedAccess"]}
		else:
213
			try:
214
215
216
217
218
219
220
				user=con.execute(select([gkdb.users.c.userrole]).where(gkdb.users.c.userid == authDetails["userid"] ))
				userRole = user.fetchone()
				if userRole[0]==-1:
					result = con.execute(gkdb.organisation.delete().where(gkdb.organisation.c.orgcode==authDetails["orgcode"]))
					return {"gkstatus":enumdict["Success"]}
				else:
					{"gkstatus":  enumdict["BadPrivilege"]}
221
			except:
222
				return {"gkstatus":  enumdict["ConnectionFailed"]}
223

224
225
	@view_config(route_name='orgid', request_method='GET',renderer='json')
	def getOrgCode(self):
226
227
228
229
230
231
232
		try:
			result = con.execute(select([gkdb.organisation.c.orgcode]).where(and_(gkdb.organisation.c.orgname==self.request.matchdict["orgname"], gkdb.organisation.c.orgtype==self.request.matchdict["orgtype"], gkdb.organisation.c.yearstart==self.request.matchdict["yearstart"], gkdb.organisation.c.yearend==self.request.matchdict["yearend"])))
			row = result.fetchone()
			orgcode={"orgcode":row["orgcode"]}
			return {"gkstatus":enumdict["Success"],"gkdata":orgcode}
		except:
			return {"gkstatus":enumdict["ConnectionFailed"]}