api_organisation.py 12.6 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
35
from gkcore import eng
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
		result = con.execute(select([gkdb.organisation.c.orgname, gkdb.organisation.c.orgtype]).distinct())
53
54
		orgs = []
		for row in result:
55
			orgs.append({"orgname":row["orgname"], "orgtype":row["orgtype"]})
56
57
		print orgs
		return orgs
58

59
60
	@view_config(route_name='orgyears', request_method='GET', renderer ='json')
	def getYears(self):
61
		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"])))
62
63
		years = []
		for row in result:
64
			years.append({"yearstart":str(row["yearstart"]), "yearend":str(row["yearend"]),"orgcode":row["orgcode"]})
65
66
		print years
		return years
67

68
69
	@view_config(request_method='POST',renderer='json')
	def postOrg(self):
70

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

83
84
85
86
87
					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"]} ])
88

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

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

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

101
102
103
104
105
					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"]} ])
106

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

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

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

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

122
123
124
125
126
					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"]} ])
127

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

131
132
133
					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":
134
135
						result = con.execute(gkdb.groupsubgroups.insert(),[{"groupname":"Capital","orgcode":orgcode["orgcode"]},{"groupname":"Miscellaneous Expenses(Asset)","orgcode":orgcode["orgcode"]}])

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

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

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

143
144
145
					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"]}])
146

147
148
149
					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"]})
150
151
152



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

161
162
163
164
165
166
167
							token = jwt.encode({"orgcode":userdata["orgcode"],"userid":record["userid"]},gkcore.secret,algorithm='HS256')
							return {"status":"ok","token":token }
						else:
							return {"status":"invalid"}
					else:
							return False
				except:
ishan masdekar's avatar
ishan masdekar committed
168
					result = con.execute(gkdb.organisation.delete().where(gkdb.organisation.c.orgcode==orgcode["orgcode"]))
169
170
			else:
				return False
171
172
		except:
			return False
173

174
175
	@view_config(route_name='organisation', request_method='GET',renderer='json')
	def getOrg(self):
176
		result = con.execute(select([gkdb.organisation]).where(gkdb.organisation.c.orgcode==self.request.matchdict["orgcode"]))
177
		row = result.fetchone()
178
		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"]	}
179
		return orgDetails
180
181
	@view_config(request_method='PUT', renderer='json')
	def putOrg(self):
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
		token = self.request.headers['gktoken']
		authDetails = authCheck(token)
		if authDetails["auth"]==False:
			return {"gkstatus":enumdict["UnauthorisedAccess"]}
		else:
		    try:
				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))
					print result.rowcount
					return {"gkstatus":enumdict["Success"]}
				else:
					{"gkstatus":  enumdict["BadPrivilege"]}
		    except:
				return {"gkstatus":  enumdict["ConnectionFailed"]}
199
200
	@view_config(request_method='DELETE', renderer='json')
	def deleteOrg(self):
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
		token = self.request.headers['gktoken']
		authDetails = authCheck(token)
		if authDetails["auth"]==False:
			return {"gkstatus":enumdict["UnauthorisedAccess"]}
		else:
		    try:
				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"]))
					print result.rowcount
					return {"gkstatus":enumdict["Success"]}
				else:
					{"gkstatus":  enumdict["BadPrivilege"]}
		    except:
				return {"gkstatus":  enumdict["ConnectionFailed"]}
217

218
219
220
221
222
	@view_config(route_name='orgid', request_method='GET',renderer='json')
	def getOrgCode(self):
		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"]}
223
		return orgcode