Commit 2d7a6302 authored by Vanita Rajpurohit's avatar Vanita Rajpurohit
Browse files

Core logic for edit organisation is written. Also the optimasation for ap_organisation is done

parent 0a547b3d
......@@ -31,7 +31,9 @@ Contributor:
from pyramid.view import view_defaults, view_config
from gkcore.views.api_login import authCheck
from gkcore import eng, enumdict
from pyramid.request import Request
from gkcore.models import gkdb
from sqlalchemy.sql import select, distinct
import json
......@@ -39,149 +41,169 @@ from sqlalchemy.engine.base import Connection
from sqlalchemy import and_
import jwt
import gkcore
con = Connection
con = eng.connect()
from gkcore.models.meta import dbconnect
#con = Connection
#con = eng.connect()
@view_defaults(route_name='organisations')
class api_organisation(object):
def __init__(self,request):
self.request = Request
self.request = request
self.myeng = dbconnect()
self.con = Connection
print "Organisation initialized"
@view_config(request_method='GET', renderer ='json')
def getOrgs(self):
try:
result = con.execute(select([gkdb.organisation.c.orgname, gkdb.organisation.c.orgtype]).order_by(gkdb.organisation.c.orgname).distinct())
self.con=self.myeng.connect()
result = self.con.execute(select([gkdb.organisation.c.orgname, gkdb.organisation.c.orgtype]).order_by(gkdb.organisation.c.orgname).distinct())
orgs = []
for row in result:
orgs.append({"orgname":row["orgname"], "orgtype":row["orgtype"]})
self.con.close()
return {"gkstatus":enumdict["Success"], "gkdata":orgs}
except:
self.con.close()
return {"gkstatus":enumdict["ConnectionFailed"]}
@view_config(route_name='orgyears', request_method='GET', renderer ='json')
def getYears(self):
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"])))
self.con = self.myeng.connect()
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"])))
years = []
for row in result:
years.append({"yearstart":str(row["yearstart"]), "yearend":str(row["yearend"]),"orgcode":row["orgcode"]})
self.con.close()
return {"gkstatus":enumdict["Success"],"gkdata":years}
except:
self.con.close()
return {"gkstatus":enumdict["ConnectionFailed"]}
@view_config(request_method='POST',renderer='json')
def postOrg(self):
try:
self.con = self.myeng.connect()
dataset = self.request.json_body
orgdata = dataset["orgdetails"]
userdata = dataset["userdetails"]
result = con.execute(gkdb.organisation.insert(),[orgdata])
result = self.con.execute(gkdb.organisation.insert(),[orgdata])
if result.rowcount==1:
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"])))
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"])))
orgcode = code.fetchone()
try:
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"])))
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"])))
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"]} ])
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"]} ])
currentliability= {"groupname":"Current Liabilities","orgcode":orgcode["orgcode"]}
result = con.execute(gkdb.groupsubgroups.insert(),currentliability)
result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Current Liabilities",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
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"])))
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"]}])
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"]}])
directexpense= {"groupname":"Direct Expense","orgcode":orgcode["orgcode"]}
result = con.execute(gkdb.groupsubgroups.insert(),directexpense)
result = self.con.execute(gkdb.groupsubgroups.insert(),directexpense)
directincome= {"groupname":"Direct Income","orgcode":orgcode["orgcode"]}
result = con.execute(gkdb.groupsubgroups.insert(),directincome)
result = self.con.execute(gkdb.groupsubgroups.insert(),directincome)
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"])))
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"])))
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"]} ])
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"]} ])
indirectexpense= {"groupname":"Indirect Expense","orgcode":orgcode["orgcode"]}
result = con.execute(gkdb.groupsubgroups.insert(),indirectexpense)
result = self.con.execute(gkdb.groupsubgroups.insert(),indirectexpense)
indirectincome= {"groupname":"Indirect Income","orgcode":orgcode["orgcode"]}
result = con.execute(gkdb.groupsubgroups.insert(),indirectincome)
result = self.con.execute(gkdb.groupsubgroups.insert(),indirectincome)
investment= {"groupname":"Investments","orgcode":orgcode["orgcode"]}
result = con.execute(gkdb.groupsubgroups.insert(),investment)
result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Investments",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
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"])))
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"]}, ])
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"]}, ])
loansasset= {"groupname":"Loans(Asset)","orgcode":orgcode["orgcode"]}
result = con.execute(gkdb.groupsubgroups.insert(),loansasset)
result = self.con.execute(gkdb.groupsubgroups.insert(),loansasset)
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"])))
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"])))
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"]} ])
result = self.con.execute(gkdb.groupsubgroups.insert(),[{"groupname":"Secured","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]},{"groupname":"Unsecured","orgcode":orgcode["orgcode"],"subgroupof":grpcode["groupcode"]} ])
reserves= {"groupname":"Reserves","orgcode":orgcode["orgcode"]}
result = con.execute(gkdb.groupsubgroups.insert(),reserves)
result = self.con.execute(gkdb.groupsubgroups.insert(),reserves)
result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Direct Income",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
result = self.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":
result = con.execute(gkdb.groupsubgroups.insert(),[{"groupname":"Capital","orgcode":orgcode["orgcode"]},{"groupname":"Miscellaneous Expenses(Asset)","orgcode":orgcode["orgcode"]}])
result = self.con.execute(gkdb.groupsubgroups.insert(),[{"groupname":"Capital","orgcode":orgcode["orgcode"]},{"groupname":"Miscellaneous Expenses(Asset)","orgcode":orgcode["orgcode"]}])
result = con.execute(gkdb.accounts.insert(),{"accountname":"Profit & Loss","groupcode":grpcode["groupcode"],"orgcode":orgcode["orgcode"]})
result = self.con.execute(gkdb.accounts.insert(),{"accountname":"Profit & Loss","groupcode":grpcode["groupcode"],"orgcode":orgcode["orgcode"]})
else:
result = con.execute(gkdb.groupsubgroups.insert(),{"groupname":"Corpus","orgcode":orgcode["orgcode"]})
result = self.con.execute(gkdb.groupsubgroups.insert(),{"groupname":"Corpus","orgcode":orgcode["orgcode"]})
result = con.execute(gkdb.accounts.insert(),{"accountname":"Income & Expenditure","groupcode":grpcode["groupcode"],"orgcode":orgcode["orgcode"]})
result = self.con.execute(gkdb.accounts.insert(),{"accountname":"Income & Expenditure","groupcode":grpcode["groupcode"],"orgcode":orgcode["orgcode"]})
result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Inventory",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
result = self.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"]}])
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"]}])
result = con.execute(select([gkdb.groupsubgroups.c.groupcode]).where(and_(gkdb.groupsubgroups.c.groupname=="Direct Expense",gkdb.groupsubgroups.c.orgcode==orgcode["orgcode"])))
result = self.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"]})
result = self.con.execute(gkdb.accounts.insert(),{"accountname":"Opening Stock","groupcode":grpcode["groupcode"],"orgcode":orgcode["orgcode"]})
userdata["orgcode"] = orgcode["orgcode"]
userdata["userrole"] = -1
result = con.execute(gkdb.users.insert(),[userdata])
result = self.con.execute(gkdb.users.insert(),[userdata])
if result.rowcount==1:
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"])) )
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"])) )
if result.rowcount == 1:
record = result.fetchone()
token = jwt.encode({"orgcode":userdata["orgcode"],"userid":record["userid"]},gkcore.secret,algorithm='HS256')
self.con.close()
return {"gkstatus":enumdict["Success"],"token":token }
else:
self.con.close()
return {"gkstatus":enumdict["ConnectionFailed"]}
else:
self.con.close()
return {"gkstatus":enumdict["ConnectionFailed"]}
except:
result = con.execute(gkdb.organisation.delete().where(gkdb.organisation.c.orgcode==orgcode["orgcode"]))
result = self.con.execute(gkdb.organisation.delete().where(gkdb.organisation.c.orgcode==orgcode["orgcode"]))
self.con.close()
return {"gkstatus":enumdict["ConnectionFailed"]}
else:
self.con.close()
return {"gkstatus":enumdict["ConnectionFailed"]}
except:
self.con.close()
return {"gkstatus":enumdict["ConnectionFailed"]}
@view_config(route_name='organisation', request_method='GET',renderer='json')
def getOrg(self):
try:
result = con.execute(select([gkdb.organisation]).where(gkdb.organisation.c.orgcode==self.request.matchdict["orgcode"]))
self.con = self.myeng.connect()
result = self.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"] }
self.con.close()
return {"gkstatus":enumdict["Success"],"gkdata":orgDetails}
except:
self.con.close()
return {"gkstatus":enumdict["ConnectionFailed"]}
@view_config(request_method='PUT', renderer='json')
......@@ -192,15 +214,18 @@ class api_organisation(object):
return {"gkstatus":enumdict["UnauthorisedAccess"]}
else:
try:
user=con.execute(select([gkdb.users.c.userrole]).where(gkdb.users.c.userid == authDetails["userid"] ))
self.con = self.myeng.connect()
user=self.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))
result = self.con.execute(gkdb.organisation.update().where(gkdb.organisation.c.orgcode==authDetails["orgcode"]).values(dataset))
self.con.close()
return {"gkstatus":enumdict["Success"]}
else:
{"gkstatus": enumdict["BadPrivilege"]}
except:
self.con.close()
return {"gkstatus": enumdict["ConnectionFailed"]}
@view_config(request_method='DELETE', renderer='json')
......@@ -211,22 +236,46 @@ class api_organisation(object):
return {"gkstatus":enumdict["UnauthorisedAccess"]}
else:
try:
user=con.execute(select([gkdb.users.c.userrole]).where(gkdb.users.c.userid == authDetails["userid"] ))
self.con = self.myeng.connect()
user=self.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"]))
result = self.con.execute(gkdb.organisation.delete().where(gkdb.organisation.c.orgcode==authDetails["orgcode"]))
self.con.close()
return {"gkstatus":enumdict["Success"]}
else:
{"gkstatus": enumdict["BadPrivilege"]}
except:
self.con.close()
return {"gkstatus": enumdict["ConnectionFailed"]}
@view_config(route_name='orgid', request_method='GET',renderer='json')
def getOrgCode(self):
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"])))
self.con = self.myeng.connect()
result = self.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"]}
self.con.close()
return {"gkstatus":enumdict["Success"],"gkdata":orgcode}
except:
self.con.close()
return {"gkstatus":enumdict["ConnectionFailed"]}
@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:
self.con =self.myeng.connect()
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"]}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment