-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_mobile.py
210 lines (190 loc) · 10.9 KB
/
main_mobile.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import flask
from flask import Flask, request, json,render_template,redirect,url_for,jsonify,json
from dotenv import load_dotenv
import mysql
import mysql.connector
import os
import sys
import pandas as pd
import config.meta as meta
from datetime import datetime
from database import db_connection_string
""" decrypt the database details"""
main_dir = os.getcwd()
os.listdir(os.path.join(main_dir,'auth'))
from decrypt import *
db_auth = {'dbname.txt':'key_dbname.txt',
'db_pass.txt':'key_db_pass.txt',
'host.txt':'key_host.txt',
'dbuser.txt':'key_dbuser.txt'}
filename = {}
for i in db_auth.keys():
with open(r'auth/' +i, 'r') as readfile:
filename['{}'.format(i.split('.')[0])]= json.load(readfile)
file_key = {}
for i in db_auth.keys():
with open(r'auth/' +db_auth[i], 'r') as readfile:
file_key['{}'.format(db_auth[i].split('.')[0])]= json.load(readfile)
db_auth = {}
for i in filename.keys():
db_auth[i] = decrypt(eval(filename[i]),eval(file_key['key_'+i])).decode("utf-8")
app = Flask(__name__)
app.config['DEBUG'] = True
# load the environement variables
load_dotenv('.env')
# set the root and data folder
root_folder = os.getcwd()
data_folder = os.path.join(root_folder,'data')
# set workbook variables
filename = 'monthly_exp.xlsx'
list_sheet_names = meta.run_config['sheetname']
cost_based_items = ['veg','chicken','Grocery','Miscellaneous','credit','Tea','Tea Mom','Amla and giloy']
quantity_based_items = ['rum','cig','netflix','amazon','onedrive','internet','Juice','ParkFee']
""" read the list of users"""
@app.route("/")
def homepage():
# Initiate the database connection
conn = db_connection_string()
df = pd.read_csv(os.path.join(data_folder,'actual_cost_v1.csv'))
commodity_list = df['Commodity'].values.tolist()
cursor = conn.cursor()
query = "Select * from Expense.current_total_expense_v1 order by batchid desc"
cursor.execute(query)
val = cursor.fetchone()
last_cash_withdrawn = int(val[3])
#cursor.close()
#conn.close()
return render_template("user_form_mobile.html",
item_list = commodity_list,
last_cash_withdrawn = last_cash_withdrawn)
@app.route("/expense_data",methods=['GET'])
def expense_data():
expense_df = pd.read_csv(os.path.join(data_folder,'expense.csv'))
columns = ['Commodity','Cost','ColorCode','Unit of Measurement']
item_data = expense_df.columns.values.tolist()
col_data = expense_df.iloc[:1,].values.tolist()
cost_data = expense_df.iloc[:len(expense_df),].values.tolist()[1:]
temp_df = pd.DataFrame(columns = columns)
temp_df['Commodity'] = item_data
temp_df['Cost'] = cost_data[0]
temp_df['ColorCode'] = col_data[0]
temp_df['Unit of Measurement'] = ['Bottle','Units','INR','INR','Bottle','INR','INR','INR','INR']
data = temp_df.iloc[:len(temp_df),].values.tolist()
collection = [dict(zip(columns,data[i])) for i in range(len(data))]
data = {"data": collection}
return jsonify(data)
@app.route("/current_expense_data",methods=['GET'])
def current_expense_data():
conn = db_connection_string()
cursor = conn.cursor()
latest_batchid_query = "Select max(batchid) from Expense.current_total_expense_v1"
cursor.execute(latest_batchid_query)
latest_batchid = cursor.fetchall()
latest_batchid = latest_batchid[0][0]
query = "select * from Expense.current_total_expense_v1 where batchid = {}".format(latest_batchid)
cursor.execute(query)
data = cursor.fetchall()
real_time_data = list(data[0])
current_expense_df = pd.read_csv(os.path.join(data_folder,'current_total_expense_v1.csv'))
column_list = current_expense_df.columns.values.tolist()
collection = dict(zip(column_list,real_time_data[:4]))
val = {"data": [collection]}
return jsonify(val)
@app.route("/addDetails",methods=['POST'])
def addDetails():
# Initiate the database connection
conn = db_connection_string()
cursor = conn.cursor()
data = request.form
if data["cash"] == '':
pass
else:
latest_batchid_query = "Select max(batchid) from Expense.current_total_expense_v1"
cursor.execute(latest_batchid_query)
latest_batchid = cursor.fetchall()
latest_batchid = latest_batchid[0][0]
query = "SET SQL_SAFE_UPDATES = 0;"
cursor.execute(query)
cash_query = """update Expense.current_total_expense_v1 set Cash_Withdrawn = Cash_Withdrawn + {},updated = %s where batchid = {}""".format(int(data["cash"]),latest_batchid)
cursor.execute(cash_query,(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),))
conn.commit()
cash_query_actual_table = "update Expense.actual_cost_v1 set Cost = {},Total = Total + {}, updated = %s where batchid = {} and Commodity = %s".format(data["cash"], data["cash"],latest_batchid)
cursor.execute(cash_query_actual_table,(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),'Cash Withdrawl',))
conn.commit()
if (data["cost"] == "") and (data["quantity"] == ""):
handler = 'empty'
elif (data["cost"] != "") and (data["quantity"] != "") and (data["item_type"] in cost_based_items):
handler = "Database Updated"
latest_batchid_query = "Select max(batchid) from Expense.actual_cost_v1"
cursor.execute(latest_batchid_query)
latest_batchid = cursor.fetchall()
latest_batchid = latest_batchid[0][0]
query = "SET SQL_SAFE_UPDATES = 0;"
cursor.execute(query)
if data['transaction_type'] == 'cash':
cash_query = """update Expense.actual_cost_v1 set Quantity = {}, Cumulative_Quantity = Cumulative_Quantity + {}, Cost = Cost + {},transaction_type = %s,items_not_consider = {},updated = %s where batchid = {} and Commodity = %s""".format(int(data["quantity"]),int(data["quantity"]),int(data["cost"]),int(data["quantity"]),latest_batchid)
cursor.execute(cash_query,(data['transaction_type'],datetime.now().strftime('%Y-%m-%d %H:%M:%S'),data['item_type'],))
conn.commit()
else:
cash_query = """update Expense.actual_cost_v1 set Quantity = {}, Cumulative_Quantity = Cumulative_Quantity + {}, Cost = Cost + {},transaction_type = %s,updated = %s where batchid = {} and Commodity = %s""".format(int(data["quantity"]),int(data["quantity"]),int(data["cost"]),latest_batchid)
cursor.execute(cash_query,('',datetime.now().strftime('%Y-%m-%d %H:%M:%S'),data['item_type'],))
conn.commit()
# check if the transaction type is cash or not?
query = "select transaction_type from Expense.actual_cost_v1 where batchid = {} and Commodity = %s".format(latest_batchid)
cursor.execute(query,(data['item_type'],))
transaction_type = cursor.fetchone()
if transaction_type[0] == '':
update_total_cost_query = "update Expense.actual_cost_v1 set Total = (Cumulative_Quantity * Cost) - (items_not_consider * Cost), updated = %s where batchid = {} and Commodity = %s".format(latest_batchid)
cursor.execute(update_total_cost_query,(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),data['item_type'],))
conn.commit()
else:
update_total_cost_query = "update Expense.actual_cost_v1 set Total = Cumulative_Quantity * Cost, updated = %s where batchid = {} and Commodity = %s".format(latest_batchid)
cursor.execute(update_total_cost_query,(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),data['item_type'],))
conn.commit()
sum_query = "Select sum(Total) from Expense.actual_cost_v1 where batchid = {}".format(latest_batchid)
cursor.execute(sum_query)
total = cursor.fetchone()
total = int(total[0])
update_total_column = """update Expense.actual_cost_v1 set GrandTotal = {}, updated = %s where batchid = {}""".format(total,latest_batchid)
cursor.execute(update_total_column,(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),))
conn.commit()
elif (data["cost"] != "") and (data["quantity"] != "") and (data["item_type"] in quantity_based_items):
handler = "Database Updated"
latest_batchid_query = "Select max(batchid) from Expense.actual_cost_v1"
cursor.execute(latest_batchid_query)
latest_batchid = cursor.fetchall()
latest_batchid = latest_batchid[0][0]
query = "SET SQL_SAFE_UPDATES = 0;"
cursor.execute(query)
if data['transaction_type'] == 'cash':
cash_query = """update Expense.actual_cost_v1 set Quantity = {}, Cumulative_Quantity = Cumulative_Quantity + {}, Cost = {},transaction_type = %s,items_not_consider = items_not_consider + {},updated = %s where batchid = {} and Commodity = %s""".format(int(data["quantity"]),int(data["quantity"]),int(data["cost"]),int(data["quantity"]),latest_batchid)
cursor.execute(cash_query,(data['transaction_type'],datetime.now().strftime('%Y-%m-%d %H:%M:%S'),data['item_type'],))
conn.commit()
else:
cash_query = """update Expense.actual_cost_v1 set Quantity = {}, Cumulative_Quantity = Cumulative_Quantity + {}, Cost = {},transaction_type = %s,updated = %s where batchid = {} and Commodity = %s""".format(int(data["quantity"]),int(data["quantity"]),int(data["cost"]),latest_batchid)
cursor.execute(cash_query,('',datetime.now().strftime('%Y-%m-%d %H:%M:%S'),data['item_type'],))
conn.commit()
# check if the transaction type is cash or not?
query = "select transaction_type from Expense.actual_cost_v1 where batchid = {} and Commodity = %s".format(latest_batchid)
cursor.execute(query,(data['item_type'],))
transaction_type = cursor.fetchone()
if transaction_type[0] == '':
update_total_cost_query = "update Expense.actual_cost_v1 set Total = (Cumulative_Quantity * Cost) - (items_not_consider * Cost), updated = %s where batchid = {} and Commodity = %s".format(latest_batchid)
cursor.execute(update_total_cost_query,(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),data['item_type'],))
conn.commit()
else:
update_total_cost_query = "update Expense.actual_cost_v1 set Total = Cumulative_Quantity * Cost, updated = %s where batchid = {} and Commodity = %s".format(latest_batchid)
cursor.execute(update_total_cost_query,(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),data['item_type'],))
conn.commit()
sum_query = "Select sum(Total) from Expense.actual_cost_v1 where batchid = {}".format(latest_batchid)
cursor.execute(sum_query)
total = cursor.fetchone()
total = int(total[0])
update_total_column = """update Expense.actual_cost_v1 set GrandTotal = {}, updated = %s where batchid = {}""".format(total,latest_batchid)
cursor.execute(update_total_column,(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),))
conn.commit()
else:
handler = "Nothing to Submit"
return render_template('user_form_response.html')
if __name__ == '__main__':
app.run(host = '0.0.0.0',debug=True,port=5003)