Beispiel #1
0
def top_accounts():
	accounts = mongo.Accounts
	orders = mongo.Orders

	current_user = g.current_user

	all_accounts = accounts.find({"user_id": str(current_user["_id"])},{"_id":1,"name":1})
	all_accounts = list(all_accounts)
	accounts_id = [i['_id'] for i in all_accounts]
	accounts_id = list(map(str,accounts_id))
	all_accounts_id = set(accounts_id)
	
	try:
		order = orders.aggregate([{"$match": {"stage": 0}},{"$group":{"_id": "$account_id" ,\
		"count": {"$sum" : "$no_of_shares"},"acc_score": { "$sum": {"$multiply": ["$no_of_shares", "$cost_of_share"] }}}}])
	except:
		return ("Incorrect type of cost_of_share present in stage 0 orders.")
	order = list(order)
	order = sorted(order, key = lambda k:k['acc_score'], reverse = True)
	
	top_accounts=[]
	for i in order:
		if i["_id"] in all_accounts_id:
			abc = i["_id"]		
			account = next((sub for sub in all_accounts if sub['_id'] == ObjectId(abc)), None) 
			top_accounts.append({"_id": i["_id"],"name": account["name"],"acc_score":i["acc_score"]})

	
	top_accounts = top_accounts[:3]
	top_accounts = json.dumps(top_accounts, default = myconverter)
	return top_accounts
Beispiel #2
0
def show_all_activities():
	activities = mongo.Activities
	accounts = mongo.Accounts
	leads = mongo.Leads

	current_user = g.current_user
	all_accounts = accounts.find({"user_id": str(current_user["_id"])})
	all_accounts = list(all_accounts)
	accounts_id = [i['_id'] for i in all_accounts]
	accounts_id = list(map(str,accounts_id))

	all_leads = leads.find({"user_id": str(current_user["_id"])})
	all_leads = list(all_leads)
	leads_id = [i['_id'] for i in all_leads]
	leads_id = list(map(str,leads_id))

	users_id = accounts_id+leads_id

	activities.update_many({"activity_type": "future", "date": { "$lte" : datetime.datetime.now()}},{ "$set": { "elapsed": 1 ,"activity_type": "past"}})
	
	all_activities = activities.find({"customer_id":{"$in": users_id}})
	
	all_activities = list(all_activities)

	all_activities = json.dumps(all_activities, default=myconverter)

	return all_activities
Beispiel #3
0
def show_all_accounts():
	accounts = mongo.Accounts
	current_user = g.current_user
	accounts_view = accounts.find({"user_id": str(current_user["_id"])}, {"_id" : 1, "name" : 1, "job_type" : 1, "company" : 1, "city" : 1, "email" : 1, "phone_number" : 1})
	accounts_view = list(accounts_view)
	accounts_view = json.dumps(accounts_view, default = myconverter)
	return accounts_view
Beispiel #4
0
def send_email_after_transaction():
	accounts = mongo.Accounts
	all_orders = request.get_json()
	all_accounts = accounts.find({},{"_id":1,"name":1,"email": 1})
	all_accounts = list(all_accounts)
	accounts_id = [i['_id'] for i in all_accounts]
	all_accounts_id = list(map(str,accounts_id))
	all_accounts_id = set(all_accounts_id)

	current_user = g.current_user
	enc_email_pw = current_user["email_pw"]
	crypt_key = environ.get("crypt_key")
	cipher_key = crypt_key.encode('utf-8')
	cipher_suite = Fernet(cipher_key)
	enc_email_pw = enc_email_pw.encode('utf-8')
	email_pw = cipher_suite.decrypt(enc_email_pw)
	email_pw = email_pw.decode('utf-8')

	for i in all_orders:
		
		if i["account_id"] in all_accounts_id:
			abc = i["account_id"]		
			account = next((sub for sub in all_accounts if sub['_id'] == ObjectId(abc)), None)

		message = """\
Subject: Hi """+str(account["name"])+"""
Hi """+str(account["name"])+""",
Your order to """+str(i["trans_type"]) +""" """+str(i["no_of_shares"])+""" shares of """+str(i["company"])+"""\
for cost Rs."""+str(i["cost_of_share"])+""" has been transacted."""

		send_email(account["email"],message,current_user["username"],email_pw)
	
	return "Emails have been sent."
Beispiel #5
0
def get_all_account_names():
	accounts = mongo.Accounts
	current_user = g.current_user
	all_accounts = accounts.find({"user_id": str(current_user["_id"])},{"_id":1,"name":1})
	all_accounts = list(all_accounts)
	all_accounts = json.dumps(all_accounts, default =myconverter)

	return all_accounts
Beispiel #6
0
def get_order_from_email():

	current_user = g.current_user
	enc_email_pw = current_user["email_pw"]
	crypt_key = environ.get("crypt_key")
	cipher_key = crypt_key.encode('utf-8')
	cipher_suite = Fernet(cipher_key)
	enc_email_pw = enc_email_pw.encode('utf-8')
	email_pw = cipher_suite.decrypt(enc_email_pw)
	email_pw = email_pw.decode('utf-8')
	
	
	order_list = fetch_order(current_user["username"],email_pw)

	accounts = mongo.Accounts
	orders = mongo.Orders
	activities = mongo.Activities

	all_accounts = accounts.find({"user_id": str(current_user["_id"])},{"_id": 1, "name": 1, "email": 1})

	all_accounts = list(all_accounts)

	accounts_email = [i['email'] for i in all_accounts]

	accounts_email = set(accounts_email)

	if order_list == []:
		return "No new order"
	else:
		for i in order_list:
			n_email = i["From"]
			n_email = n_email.split('<')[1]
			email = n_email.split('>')[0]
			company = i["company"]
			action = i["action"]
			no_of_shares = i["no_of_shares"]
			amount = i["amount"]

			if email in accounts_email:
				abc = email		
				account = next((i for i in all_accounts if i["email"] == abc), None)

			if account is None:
				continue
			if amount == '':
				price = ' '
			else:
				price = amount
			title = "{} {} shares of {} for desired price:{}?".format(action.upper(),no_of_shares,company.upper(),price)
			body = "Finalize order of {} to {} {} shares of {}. Desired Price:{}".format(account["name"],action,no_of_shares,company.upper(),price)
			date = datetime.datetime.now() + timedelta(hours = 2)
			activities.insert({"title": title, "body": body, "date": date, "activity_type": "future", "customer_id": str(account["_id"]), "elapsed":0, "ai_activity": 1})
			activity = activities.find({}).sort("_id",-1).limit(1)
			orders.insert({ "company": company, "no_of_shares": no_of_shares, "cost_of_share": price, "stage": 1, "account_id":str(account["_id"]), "trans_type": action,\
			"activity_id": str(activity[0]["_id"]), "creation_date":datetime.datetime.now()})

		return "Inserted"
Beispiel #7
0
def get_pie_chart_data():
	accounts = mongo.Accounts
	orders = mongo.Orders

	current_user = g.current_user

	all_accounts = accounts.find({"user_id":str(current_user["_id"])})
	all_accounts = list(all_accounts)
	accounts_id = [i['_id'] for i in all_accounts]
	accounts_id = list(map(str,accounts_id))
	order = orders.find({"account_id":{"$in": accounts_id}},{"stage":1, "cost_of_share":1, "no_of_shares":1})
	order = list(order)

	order = json.dumps(order, default = myconverter)
	return order
Beispiel #8
0
def edit_account():
	req_data = request.get_json()
	#JSON data into variables
	usr_id = req_data["_id"]
	usr_id = ObjectId(usr_id)
	city = req_data["city"]
	company = req_data["company"]
	contact_comm_type = req_data["contact_comm_type"]
	country = req_data["country"]
	demat_accno = req_data["demat_accno"]
	dob = req_data["dob"]
	dob = datetime.datetime.strptime(dob, '%Y-%m-%dT%H:%M:%S.%fZ')
	education = req_data["education"]
	email = req_data["email"]
	job_type = req_data["job_type"]
	last_contact = req_data["last_contact"]
	latest_order_stage = req_data["latest_order_stage"]
	marital_status = req_data["marital_status"]
	name = req_data["name"]
	phone_number = req_data["phone_number"]
	state = req_data["state"]
	trading_accno = req_data["trading_accno"]

	accounts = mongo.Accounts
	account = accounts.find({"_id": usr_id})

	new_values = {"$set":{
	"city": city, 
	"company": company, 
	"contact_comm_type": contact_comm_type, 
	"country": country, 
	"demat_accno": demat_accno, 
	"dob": dob, 
	"education": education, 
	"email": email, 
	"job_type": job_type, 
	"last_contact": last_contact, 
	"latest_order_stage": latest_order_stage, 
	"marital_status": marital_status, 
	"name": name, 
	"phone_number": phone_number, 
	"state": state, 
	"trading_accno": trading_accno
}}

	x = accounts.update({"_id": usr_id},new_values)
	return "Document has been updated"
Beispiel #9
0
def show_all_companies():
	orders = mongo.Orders

	accounts = mongo.Accounts


	all_accounts = accounts.find({},{"_id":1,"name":1})
	all_accounts = list(all_accounts)
	accounts_id = [i['_id'] for i in all_accounts]
	accounts_id = list(map(str,accounts_id))
	all_accounts_id = set(accounts_id)
	all_companies = orders.find({},{"company":1,"_id":0})
	all_companies = list(all_companies)

	all_companies = json.dumps(all_companies, default =myconverter)

	return all_companies
Beispiel #10
0
def complete_account_orders():
	req_data = request.get_json()
	usr_id = req_data["account_id"]
	company = req_data["company"]
	
	orders = mongo.Orders
	accounts = mongo.Accounts
	activities = mongo.Activities

	all_orders= orders.find({"stage":3, "account_id": usr_id})
	all_orders = list(all_orders)
	activity_id = [i['activity_id'] for i in all_orders]
	activity_id = list(map(ObjectId,activity_id))


	account = accounts.find({"_id": ObjectId(usr_id)})
	account = list(account)


	orders_company = [i["company"].lower() for i in all_orders]
	orders_company = set(orders_company)
	company_keys = set(company)
	company_values = set(company.values())
	companies = orders_company.intersection(company_keys)

	if bool(companies) == False and bool(orders_company) == True:
		return "Send correct company"
	elif bool(companies) == False and bool(orders_company) == False:
		return "No companies to be transacted"
	else:
		#O(n)--> where n is the number of companies in stage 3, ie:better than number of orders
		for i in companies:
			a = re.compile(i, re.IGNORECASE)
			orders.update_many({"company": a, "stage":3},{"$set": {"cost_of_share": company[i]}})

		all_orders_new= orders.find({"stage":3})
		all_orders_new = list(all_orders_new)

		orders.update_many({"stage" : 3, 'account_id' : usr_id},{"$set": {"stage" : 0}})
		
		activities.update({"_id":{"$in": activity_id}},{"$set": {"activity_type": "past"}}, multi = True)

		all_orders_new = json.dumps(all_orders_new, default =myconverter)
		
		return all_orders_new
Beispiel #11
0
def get_line_graph_data():
	orders = mongo.Orders
	accounts = mongo.Accounts
	current_user = g.current_user
	all_accounts = accounts.find({"user_id": str(current_user["_id"])})
	all_accounts = list(all_accounts)
	accounts_id = [i['_id'] for i in all_accounts]
	accounts_id = list(map(str,accounts_id))
	order = orders.find({"stage" : 0, "account_id": {"$in": accounts_id}},{"creation_date": 1, "cost_of_share": 1, "no_of_shares": 1})
	order = list(order)
	final = []
	for i in order:
		i["month"] = i["creation_date"].month
		del i["creation_date"]
		del i["_id"]

	order = json.dumps(order, default = myconverter)
	return order
Beispiel #12
0
def display_account(usr_id):
	accounts = mongo.Accounts
	orders = mongo.Orders
	
	max_stage_order = orders.find({"account_id":usr_id}).sort("stage",-1).limit(1)

	order_count = orders.count_documents({"account_id":usr_id})
	if(order_count==0):
		accounts.update({"_id": ObjectId(usr_id)}, {"$set": {"latest_order_stage": 0}})
	else:
		accounts.update({"_id": ObjectId(usr_id)}, {"$set": {"latest_order_stage": max_stage_order[0]["stage"]}})
	
	account = accounts.find({"_id" : ObjectId(usr_id)})
	account = list(account)
	account = json.dumps(account[0], default=myconverter)



	return account
Beispiel #13
0
def complete_all_orders():	
	accounts = mongo.Accounts
	orders = mongo.Orders
	activities = mongo.Activities
	req_data = request.get_json()
	company = req_data["company"]

	current_user = g.current_user

	all_accounts = accounts.find({"user_id": str(current_user["_id"])})
	all_accounts = list(all_accounts)
	account_id = [i['_id'] for i in all_accounts]
	account_id = list(map(str,account_id))
	all_orders= orders.find({"stage":3,"account_id":{"$in": account_id}})
	all_orders = list(all_orders)
	activity_id = [i['activity_id'] for i in all_orders]
	activity_id = list(map(ObjectId,activity_id))
	
	orders_company = [i["company"].lower() for i in all_orders]
	orders_company = set(orders_company)
	company_keys = set(company)
	company_values = set(company.values())
	companies = orders_company.intersection(company_keys)
	if bool(companies) == False and bool(orders_company) == True:
		return "Send correct company"
	elif bool(companies) == False and bool(orders_company) == False:
		return "No companies to be transacted"
	else:
		for i in companies:
			a = re.compile(i, re.IGNORECASE)
			orders.update_many({"company": a, "stage":3,"account_id": {"$in":account_id}},{"$set": {"cost_of_share": company[i]}})

		all_orders_new= orders.find({"stage":3})
		all_orders_new = list(all_orders_new)

		orders.update_many({"stage" : 3,"account_id": {"$in":account_id}},{"$set": {"stage" : 0}})
		
		activities.update({"_id":{"$in": activity_id}},{"$set": {"activity_type": "past"}}, multi = True)

		all_orders_new = json.dumps(all_orders_new, default =myconverter)

		return all_orders_new
Beispiel #14
0
def show_all_orders():
	orders = mongo.Orders

	accounts = mongo.Accounts

	current_user = g.current_user

	all_accounts = accounts.find({"user_id": str(current_user["_id"])},{"_id":1,"name":1})
	all_accounts = list(all_accounts)
	accounts_id = [i['_id'] for i in all_accounts]
	accounts_id = list(map(str,accounts_id))
	all_accounts_id = set(accounts_id)
	all_orders = orders.find({"account_id": {"$in": accounts_id}})
	all_orders = list(all_orders)

	for i in all_orders:
		if i["account_id"] in all_accounts_id:
			abc = i["account_id"]		
			account = next((sub for sub in all_accounts if sub['_id'] == ObjectId(abc)), None) 
			i["name"] = account["name"]	

	all_orders = json.dumps(all_orders, default =myconverter)

	return all_orders
Beispiel #15
0
def convert_finalized_orders():
	
	req_data = request.get_json()
	company = req_data["company"]

	accounts = mongo.Accounts
	orders = mongo.Orders
	activities = mongo.Activities

	current_user = g.current_user

	all_accounts = accounts.find({"user_id":str(current_user["_id"])})
	all_accounts = list(all_accounts)
	accounts_id = [i['_id'] for i in all_accounts]
	accounts_id = list(map(str,accounts_id))
	all_accounts_id = set(accounts_id)

	finalized_orders = orders.find({"stage": 2,"account_id":{"$in": accounts_id}}) #1
	finalized_orders = list(finalized_orders)

	order_ids = []
	values = []
	
	if finalized_orders != []:
		for i in finalized_orders:
			if i["account_id"] in all_accounts_id:
				abc = i["account_id"]
				account = next((sub for sub in all_accounts if sub['_id'] == ObjectId(abc)), None) 
				i["name"] = account["name"]

			order_company = i["company"].lower()
			action = i["trans_type"].lower()
			cost_of_share = get_cost_from_text(i["cost_of_share"])
			if cost_of_share == 'undefined':
				cost_of_share = company[order_company]

			if (action == 'buy' and cost_of_share >= company[order_company]) or (action == 'sell' and cost_of_share <= company[order_company]):
				order_ids.append(i["_id"])
				
				title = "Transact order for {}ing {} {} shares.".format(i["trans_type"],i["no_of_shares"],i["company"])
				body = "Transact order of {} to {} {} shares of {}. Desired Price:{}".format(i["name"],i["trans_type"],i["no_of_shares"],\
				i["company"],i["cost_of_share"])
				date = datetime.datetime.now() + timedelta(hours = 10)
				values.append({"title": title, "body":body, "date":date, "activity_type": "future",\
				"customer_id": i["account_id"], "elapsed":0, "ai_activity": 1})

		
		
		len_order_ids = len(order_ids)
		if len_order_ids > 0:
			activities.insert(values)
			inserted_activities = activities.find({}).sort("_id",-1).limit(len_order_ids)
			inserted_activities = list(inserted_activities)
			inserted_activities = inserted_activities[::-1]
			activity_id = [i["_id"] for i in inserted_activities]
			activity_id = list(map(str,activity_id))
			
			for i in range(len_order_ids):
				orders.update({"_id": order_ids[i]},{"$set":{"stage":3, "activity_id": activity_id[i]}})

	return "Success"