-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmit_entered_data.py
287 lines (200 loc) · 14.6 KB
/
transmit_entered_data.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
############### IMPORT NECESSARY RECOURSES ###############
# Import psycopg2 in order to get needed functions to #
# establish link between Python and SQL. #
##########################################################
import psycopg2 as pg2 # import psycopg2 to communicate with 'Receipt_Project_v3.0' database
############### TRANSMIT DATA FUNCTIONALITY ###############
# The transmitEnteredData function is utilized within the #
# home_page.py central file and is used to transmit data #
# from the gui to the 'Receipt_Project_v3.0' database. #
###########################################################
def transmitEnteredData (
establishmentName, purchaseDate, purchaseTime, purchaseAmount, purchaseTax, purchaseCurrency, employeeName,
establishmentNumAndStreet, establishmentCity, establishmentState, establishmentZIP, purchaseType, purchasePresent,
purchaseCount, purchaseTotal
):
########## CREATE LINK BETWEEN PYTHON AND SQL SERVER ##########
# The following code is used to create a cursor in order to #
# communicate with the 'Receipt_Project_v3.0' database. #
###############################################################
receipt_project = pg2.connect( # connect python to 'Receipt_Project_v3.0' database
host='localhost',
database='Receipt_Project_v3.0',
user='',
password=''
)
receiptProjectCursor = receipt_project.cursor() # create cursor to input commands
########## FILL NEW FIELDS FUNCTIONALITY ##########
# The fillNewFields function is used to transmit #
# information to the 'Receipt_Project_v3.0' #
# database in the case that there is no duplicate #
# dates. #
###################################################
def fillNewFields(): # function to fill fields provided no duplicate 'purchase_date' found
##### FILL 'purchase_date', 'purchase_present', 'purchase_count' #####
receiptProjectCursor.execute( # insert new 'purchaseDate' into 'Receipt_Project_v3.0' database
'insert into base_fields_table(purchase_date, purchase_present, purchase_count, total_spent) values (%s, %s, %s, %s)',
(purchaseDate, purchasePresent, purchaseCount, purchaseTotal)
)
receipt_project.commit() # submit query to 'Receipt_Project_v3.0'
##### GRAB 'day_id' FROM 'base_fields_table' #####
receiptProjectCursor.execute( # select 'day_id' correlated with 'purchase_date' and store into 'dayID'
'select day_id from base_fields_table where purchase_date = (%s)',
((purchaseDate,)) # <---- this one tuple brought me so much pain :)
)
dayID = str(receiptProjectCursor.fetchall()[0][0]) # assign contents to 'dayID'
##### FILL 'day_id' IN 'purchase_table' #####
receiptProjectCursor.execute( # create new 'day_id' in 'purchase_table' based on 'dayID'
'insert into purchase_table(day_id) values (%s)',
((dayID),)
)
receipt_project.commit() # submit query to 'Receipt_Project_v3.0'
##### FILL 'purchase_time', 'purchase_amount', 'purchase_tax', 'purchase_currency', 'purchase_type', 'employee_name' INTO 'purchase_table' #####
receiptProjectCursor.execute( # insert new 'purchaseTime', 'purchaseAmount', 'purchaseTax', 'purchaseCurrency', 'purchaseType', 'employeeName' into 'Receipt_Project_v3.0' database
'update purchase_table set purchase_time = (%s), purchase_amount = (%s), purchase_tax = (%s), purchase_currency = (%s), purchase_type = (%s), employee_name = (%s) where day_id = (%s)',
(purchaseTime, purchaseAmount, purchaseTax, purchaseCurrency, purchaseType, employeeName,
dayID)
)
receipt_project.commit() # submit query to 'Receipt_Project_v3.0'
##### GRAB 'purchase_id' FROM 'purchase_table' #####
receiptProjectCursor.execute( # select 'purchase_id' correlated with 'day_id' and store into 'purchaseID'
'select purchase_id from purchase_table where day_id = (%s)',
((dayID),)
)
purchaseID = str(receiptProjectCursor.fetchall()[0][0]) # assign contents to 'purchaseID'
##### FILL 'day_id' AND 'purchase_id' INTO 'location_table' #####
receiptProjectCursor.execute( # create new 'purchase_table' day id based on 'dayID'
'insert into location_table(day_id, purchase_id) values (%s, %s)',
(dayID, purchaseID)
)
receipt_project.commit() # submit query to 'Receipt_Project_v3.0'
##### FILL 'establishment_name', 'establishment_address', 'establishment_city', 'establishment_region', 'establishment_zip' INTO 'location_table' #####
receiptProjectCursor.execute( # insert new 'establishmentName', 'establishmentNumAndStreet', 'establishmentCity', 'establishmentState', 'establishmentZIP' into 'Receipt_Project_v3.0' database
'update location_table set establishment_name = (%s), establishment_address = (%s), establishment_city = (%s), establishment_region = (%s), establishment_zip = (%s) where purchase_id = (%s)',
(establishmentName, establishmentNumAndStreet, establishmentCity, establishmentState, establishmentZIP, purchaseID)
)
receipt_project.commit() # submit query to 'Receipt_Project_v3.0'
########## IF DATA DETECTED ##########
# If the 'Receipt_Project_v3.0' #
# database is not empty, proceed. #
######################################
if (purchasePresent == True): # if all fields correctly filled in, transmit data to 'Receipt_Project_v3.0'
##### ATTEMPT TO FIND DATE DUPLICATES #####
try: # try to create a list of all dates
##### ALTER 'purchaseDate' TO COMPARE WITH 'allDates' VALUES #####
receiptProjectCursor.execute( # grab list of all dates in 'Receipt_Project_v3.0' database
'select purchase_date from base_fields_table where extract(year from purchase_date) = (%s)',
((purchaseDate.split('/')[0]),)
)
allDates = str(receiptProjectCursor.fetchall()) # assign contents to 'allDates'
if (purchaseDate.split('/')[1][0] == '0'): # if month date begins with 0, remove 0
monthDate = purchaseDate.split('/')[1][1] # store value into 'monthDate'
elif (purchaseDate.split('/')[1][0] != '0'): # if month date does not begin with 0, change nothing
monthDate = purchaseDate.split('/')[1] # store value into 'monthDate'
if (purchaseDate.split('/')[2][0] == '0'): # if day date begins with 0, remove 0
dayDate = purchaseDate.split('/')[2][1] # store value into 'dayDate'
elif (purchaseDate.split('/')[2][0] != '0'): # if day date does not begin with 0, change nothing
dayDate = purchaseDate.split('/')[2] # store value into 'dayDate'
compareDate = str( # concatenate 'purchaseDate' values to make a comparison date
'(datetime.date(' + purchaseDate.split('/')[0] + ', ' + monthDate + ', ' + dayDate + '),)'
)
########## IF 'compareDate' IN 'allDates' ##########
# If there is a past instance of a date, proceed. #
####################################################
if (compareDate in allDates): # if not first instance of 'purchaseDate'
##### UPDATE 'purchase_count' IN 'base_fields_table' #####
receiptProjectCursor.execute( # find previous 'purchase_count', index by +1, and store into 'purchaseCount'
'select purchase_count from base_fields_table where purchase_date = (%s)',
((purchaseDate,))
)
purchaseCount = str(int(receiptProjectCursor.fetchall()[0][0]) + 1) # assign contents to 'purchaseCount'
receiptProjectCursor.execute( # update 'purchase_count' if 'purchase_date' day already has a purchase
'update base_fields_table set purchase_count = (%s) where purchase_date = (%s)',
(purchaseCount, (purchaseDate,))
)
receipt_project.commit() # submit query to 'Receipt_Project_v3.0'
##### GRAB ID FROM 'base_fields_table' #####
receiptProjectCursor.execute( # select 'day_id' correlated with 'purchase_date' and store into 'dayID'
'select day_id from base_fields_table where purchase_date = (%s)',
((purchaseDate,))
)
dayID = str(receiptProjectCursor.fetchall()[0][0]) # assign contents to 'dayID'
##### FILL 'day_id' IN 'purchase_table' #####
receiptProjectCursor.execute( # create new 'purchase_table' day id based on 'dayID'
'insert into purchase_table(day_id) values (%s)',
((dayID),)
)
receipt_project.commit() # submit query to 'Receipt_Project_v3.0'
##### GRAB ID FROM 'purchase_table' #####
receiptProjectCursor.execute( # select 'day_id' correlated with 'purchase_date' and store into 'dayID'
'select max(purchase_id) from purchase_table where day_id = (%s)',
((dayID),)
)
purchaseID = str(receiptProjectCursor.fetchall()[0][0]) # assign contents to 'purchaseID'
##### FILL 'purchase_time', 'purchase_amount', 'purchase_tax', 'purchase_currency', 'purchase_type', 'employee_name' INTO 'purchase_table' #####
receiptProjectCursor.execute( # insert 'purchaseTime' into 'Receipt_Project_v3.0' database
'update purchase_table set purchase_time = (%s), purchase_amount = (%s), purchase_tax = (%s), purchase_currency = (%s), purchase_type = (%s), employee_name = (%s) where purchase_id = (%s)',
(purchaseTime, purchaseAmount, purchaseTax, purchaseCurrency, purchaseType, employeeName,
purchaseID)
)
receipt_project.commit() # submit query to 'Receipt_Project_v3.0'
##### FILL 'day_id' AND 'purchase_id' INTO 'location_table' #####
receiptProjectCursor.execute( # create new 'purchase_table' day id based on 'dayID'
'insert into location_table(day_id, purchase_id) values (%s, %s)',
(dayID, purchaseID)
)
receipt_project.commit() # submit query to 'Receipt_Project_v3.0'
##### FILL 'establishment_name', 'establishment_address', 'establishment_city', 'establishment_region', 'establishment_zip' INTO 'location_table' #####
receiptProjectCursor.execute( # insert 'establishmentName' into 'Receipt_Project_v3.0' database
'update location_table set establishment_name = (%s), establishment_address = (%s), establishment_city = (%s), establishment_region = (%s), establishment_zip = (%s) where purchase_id = (%s)',
(establishmentName, establishmentNumAndStreet, establishmentCity, establishmentState, establishmentZIP, purchaseID)
)
receipt_project.commit() # submit query to 'Receipt_Project_v3.0'
##### GRAB 'total_spent' FROM 'base_fields_table' AND UPDATE 'total_spent' #####
receiptProjectCursor.execute( # select 'day_id' correlated with 'purchase_date' and store into 'dayID'
'select total_spent from base_fields_table where day_id = (%s)',
((dayID),)
)
oldTotal = receiptProjectCursor.fetchall()[0][0] # assign contents to 'oldTotal'
newTotal = float(purchaseAmount) + float(oldTotal) # assign contents to 'newTotal'
receiptProjectCursor.execute( # assign value of 'newTotal' to 'total_spent' in 'base_fields_table'
'update base_fields_table set total_spent = (%s) where day_id = (%s)',
(newTotal, dayID)
)
receipt_project.commit() # submit query to 'Receipt_Project_v3.0'
##### FILL 'establishment_name', 'establishment_address', 'establishment_city', 'establishment_region', 'establishment_zip' INTO 'location_table' #####
receiptProjectCursor.execute( # insert 'establishmentName' into 'Receipt_Project_v3.0' database
'update location_table set establishment_name = (%s), establishment_address = (%s), establishment_city = (%s), establishment_region = (%s), establishment_zip = (%s) where purchase_id = (%s)',
(establishmentName, establishmentNumAndStreet, establishmentCity, establishmentState, establishmentZIP, purchaseID)
)
receipt_project.commit() # submit query to 'Receipt_Project_v3.0'
########## IF 'compareDate' NOT IN 'allDates' ##########
# If no past instance of a date found, proceed. #
########################################################
else: # if first instance of 'purchaseDate'
fillNewFields() # call 'fillNewFields' function provided no duplicate 'purchase_date'
########## IF NO DATA DETECTED ##########
# If the 'Receipt_Project_v3.0' #
# database is empty, proceed. #
#########################################
except: # if list failure (no data found), 'purchase_date' duplicates impossible
fillNewFields() # call 'fillNewFields' function provided no duplicate 'purchase_date'
########## IF ENTERED DATA EMPTY ##########
# If day entered in home_page.py has no #
# data, proceed. #
###########################################
elif (purchasePresent == False):
##### FILL 'purchase_date', 'purchase_present', 'purchase_count' #####
receiptProjectCursor.execute( # insert new 'purchaseDate' into 'Receipt_Project_v3.0' database
'insert into base_fields_table(purchase_date, purchase_present, purchase_count, total_spent) values (%s, %s, %s, %s)',
(purchaseDate, False, None, None)
)
receipt_project.commit() # submit query to 'Receipt_Project_v3.0'
########## CLOSE CONNECTIONS ##########
# Close the connections to #
# 'Receipt_Project_v3.0' database in #
# order to prevent data leaks. #
#######################################
# IF THESE TWO COMMANDS BELOW CAUSE A PROBLEM, DELETE THEM
receiptProjectCursor.close() # close the cursor to 'Receipt_Project_v3.0' database
receipt_project.close() # close the connection to 'Receipt_Project_v3.0' database