PageRenderTime 66ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/gkcore/views/api_organisation.py

https://gitlab.com/Ashutoshrox/gkcore
Python | 232 lines | 200 code | 7 blank | 25 comment | 4 complexity | a57927bd30c98d2259380efc8724c820 MD5 | raw file
  1. """
  2. Copyright (C) 2014 2015 2016 Digital Freedom Foundation
  3. This file is part of GNUKhata:A modular,robust and Free Accounting System.
  4. GNUKhata is Free Software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 3 of
  7. the License, or (at your option) any later version.and old.stockflag = 's'
  8. GNUKhata is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public
  13. License along with GNUKhata (COPYING); if not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301 USA59 Temple Place, Suite 330,
  16. Contributor:
  17. "Krishnakant Mane" <kk@gmail.com>
  18. "Ishan Masdekar " <imasdekar@dff.org.in>
  19. "Navin Karkera" <navin@dff.org.in>
  20. """
  21. from pyramid.view import view_defaults, view_config
  22. from gkcore import eng, enumdict
  23. from gkcore.models import gkdb
  24. from sqlalchemy.sql import select, distinct
  25. import json
  26. from sqlalchemy.engine.base import Connection
  27. from sqlalchemy import and_
  28. import jwt
  29. import gkcore
  30. con = Connection
  31. con = eng.connect()
  32. @view_defaults(route_name='organisations')
  33. class api_organisation(object):
  34. def __init__(self,request):
  35. self.request = request
  36. @view_config(request_method='GET', renderer ='json')
  37. def getOrgs(self):
  38. try:
  39. result = con.execute(select([gkdb.organisation.c.orgname, gkdb.organisation.c.orgtype]).distinct())
  40. orgs = []
  41. for row in result:
  42. orgs.append({"orgname":row["orgname"], "orgtype":row["orgtype"]})
  43. return {"gkstatus":enumdict["Success"], "gkdata":orgs}
  44. except:
  45. return {"gkstatus":enumdict["ConnectionFailed"]}
  46. @view_config(route_name='orgyears', request_method='GET', renderer ='json')
  47. def getYears(self):
  48. try:
  49. 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"])))
  50. years = []
  51. for row in result:
  52. years.append({"yearstart":str(row["yearstart"]), "yearend":str(row["yearend"]),"orgcode":row["orgcode"]})
  53. return {"gkstatus":enumdict["Success"],"gkdata":years}
  54. except:
  55. return {"gkstatus":enumdict["ConnectionFailed"]}
  56. @view_config(request_method='POST',renderer='json')
  57. def postOrg(self):
  58. try:
  59. dataset = self.request.json_body
  60. orgdata = dataset["orgdetails"]
  61. userdata = dataset["userdetails"]
  62. result = con.execute(gkdb.organisation.insert(),[orgdata])
  63. if result.rowcount==1:
  64. 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"])))
  65. orgcode = code.fetchone()
  66. try:
  67. currentassets= {"groupname":"Current Assets","orgcode":orgcode["orgcode"]}
  68. result = con.execute(gkdb.groupsubgroups.insert(),currentassets)
  69. result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Current Assets",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
  70. grpcode = result.fetchone()
  71. 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"]} ])
  72. currentliability= {"groupname":"Current Liabilities","orgcode":orgcode["orgcode"]}
  73. result = con.execute(gkdb.groupsubgroups.insert(),currentliability)
  74. result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Current Liabilities",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
  75. grpcode = result.fetchone()
  76. 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"]}])
  77. directexpense= {"groupname":"Direct Expense","orgcode":orgcode["orgcode"]}
  78. result = con.execute(gkdb.groupsubgroups.insert(),directexpense)
  79. directincome= {"groupname":"Direct Income","orgcode":orgcode["orgcode"]}
  80. result = con.execute(gkdb.groupsubgroups.insert(),directincome)
  81. fixedassets= {"groupname":"Fixed Assets","orgcode":orgcode["orgcode"]}
  82. result = con.execute(gkdb.groupsubgroups.insert(),fixedassets)
  83. result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Fixed Assets",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
  84. grpcode = result.fetchone()
  85. 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"]} ])
  86. indirectexpense= {"groupname":"Indirect Expense","orgcode":orgcode["orgcode"]}
  87. result = con.execute(gkdb.groupsubgroups.insert(),indirectexpense)
  88. indirectincome= {"groupname":"Indirect Income","orgcode":orgcode["orgcode"]}
  89. result = con.execute(gkdb.groupsubgroups.insert(),indirectincome)
  90. investment= {"groupname":"Investments","orgcode":orgcode["orgcode"]}
  91. result = con.execute(gkdb.groupsubgroups.insert(),investment)
  92. result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Investments",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
  93. grpcode = result.fetchone()
  94. 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"]}, ])
  95. loansasset= {"groupname":"Loans(Asset)","orgcode":orgcode["orgcode"]}
  96. result = con.execute(gkdb.groupsubgroups.insert(),loansasset)
  97. loansliab= {"groupname":"Loans(Liability)","orgcode":orgcode["orgcode"]}
  98. result = con.execute(gkdb.groupsubgroups.insert(),loansliab)
  99. result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Loans(Liability)",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
  100. grpcode = result.fetchone()
  101. result = con.execute(gkdb.groupsubgroups.insert(),[{"groupname":"Secured","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]},{"groupname":"Unsecured","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]} ])
  102. reserves= {"groupname":"Reserves","orgcode":orgcode["orgcode"]}
  103. result = con.execute(gkdb.groupsubgroups.insert(),reserves)
  104. result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Direct Income",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
  105. grpcode = result.fetchone()
  106. if orgdata["orgtype"] == "Profit Making":
  107. result = con.execute(gkdb.groupsubgroups.insert(),[{"groupname":"Capital","orgcode":orgcode["orgcode"]},{"groupname":"Miscellaneous Expenses(Asset)","orgcode":orgcode["orgcode"]}])
  108. result = con.execute(gkdb.accounts.insert(),{"accountname":"Profit & Loss","groupcode":grpcode["groupcode"],"orgcode":orgcode["orgcode"]})
  109. else:
  110. result = con.execute(gkdb.groupsubgroups.insert(),{"groupname":"Corpus","orgcode":orgcode["orgcode"]})
  111. result = con.execute(gkdb.accounts.insert(),{"accountname":"Income & Expenditure","groupcode":grpcode["groupcode"],"orgcode":orgcode["orgcode"]})
  112. result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Inventory",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
  113. grpcode = result.fetchone()
  114. 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"]}])
  115. result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Direct Expense",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
  116. grpcode = result.fetchone()
  117. result = con.execute(gkdb.accounts.insert(),{"accountname":"Opening Stock","groupcode":grpcode["groupcode"],"orgcode":orgcode["orgcode"]})
  118. userdata["orgcode"] = orgcode["orgcode"]
  119. userdata["userrole"] = -1
  120. result = con.execute(gkdb.users.insert(),[userdata])
  121. if result.rowcount==1:
  122. 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"])) )
  123. if result.rowcount == 1:
  124. record = result.fetchone()
  125. token = jwt.encode({"orgcode":userdata["orgcode"],"userid":record["userid"]},gkcore.secret,algorithm='HS256')
  126. return {"gkstatus":enumdict["Success"],"token":token }
  127. else:
  128. return {"gkstatus":enumdict["ConnectionFailed"]}
  129. else:
  130. return {"gkstatus":enumdict["ConnectionFailed"]}
  131. except:
  132. result = con.execute(gkdb.organisation.delete().where(gkdb.organisation.c.orgcode==orgcode["orgcode"]))
  133. return {"gkstatus":enumdict["ConnectionFailed"]}
  134. else:
  135. return {"gkstatus":enumdict["ConnectionFailed"]}
  136. except:
  137. return {"gkstatus":enumdict["ConnectionFailed"]}
  138. @view_config(route_name='organisation', request_method='GET',renderer='json')
  139. def getOrg(self):
  140. try:
  141. result = con.execute(select([gkdb.organisation]).where(gkdb.organisation.c.orgcode==self.request.matchdict["orgcode"]))
  142. row = result.fetchone()
  143. 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"] }
  144. return {"gkstatus":enumdict["Success"],"gkdata":orgDetails}
  145. except:
  146. return {"gkstatus":enumdict["ConnectionFailed"]}
  147. @view_config(request_method='PUT', renderer='json')
  148. def putOrg(self):
  149. token = self.request.headers['gktoken']
  150. authDetails = authCheck(token)
  151. if authDetails["auth"]==False:
  152. return {"gkstatus":enumdict["UnauthorisedAccess"]}
  153. else:
  154. try:
  155. user=con.execute(select([gkdb.users.c.userrole]).where(gkdb.users.c.userid == authDetails["userid"] ))
  156. userRole = user.fetchone()
  157. dataset = self.request.json_body
  158. if userRole[0]==-1:
  159. result = con.execute(gkdb.organisation.update().where(gkdb.organisation.c.orgcode==authDetails["orgcode"]).values(dataset))
  160. return {"gkstatus":enumdict["Success"]}
  161. else:
  162. {"gkstatus": enumdict["BadPrivilege"]}
  163. except:
  164. return {"gkstatus": enumdict["ConnectionFailed"]}
  165. @view_config(request_method='DELETE', renderer='json')
  166. def deleteOrg(self):
  167. token = self.request.headers['gktoken']
  168. authDetails = authCheck(token)
  169. if authDetails["auth"]==False:
  170. return {"gkstatus":enumdict["UnauthorisedAccess"]}
  171. else:
  172. try:
  173. user=con.execute(select([gkdb.users.c.userrole]).where(gkdb.users.c.userid == authDetails["userid"] ))
  174. userRole = user.fetchone()
  175. if userRole[0]==-1:
  176. result = con.execute(gkdb.organisation.delete().where(gkdb.organisation.c.orgcode==authDetails["orgcode"]))
  177. return {"gkstatus":enumdict["Success"]}
  178. else:
  179. {"gkstatus": enumdict["BadPrivilege"]}
  180. except:
  181. return {"gkstatus": enumdict["ConnectionFailed"]}
  182. @view_config(route_name='orgid', request_method='GET',renderer='json')
  183. def getOrgCode(self):
  184. try:
  185. 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"])))
  186. row = result.fetchone()
  187. orgcode={"orgcode":row["orgcode"]}
  188. return {"gkstatus":enumdict["Success"],"gkdata":orgcode}
  189. except:
  190. return {"gkstatus":enumdict["ConnectionFailed"]}