-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputter.py
369 lines (281 loc) · 9.65 KB
/
inputter.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import time
import pyautogui as gui
import numpy as np
import vision
from templates.base_invoice import IDNumType
debug = False
class Inputter:
# Login
user_pt = (642, 543)
# Exit
exit_pt = (1887, 16)
confirm_exit_pt = (905, 601)
# Main menu
nav_bar_air_import_option_pt = (415, 45)
hawb_list_dropdown_option_pt = (480, 108)
# HAWB List
reference_num_column_first_row_pt = (513, 165)
mawb_num_column_first_row_pt = (1011, 165)
# Accounting
account_payable_header_pt = (645, 590)
# Account Payable
vendor_field_pt = (288, 383)
post_date_field_pt = (300, 410)
invoice_date_field_pt = (470, 410)
invoice_num_field_pt = (604, 383)
billing_code_first_row_pt = (220, 500)
price_first_row_pt = (845, 500)
# Close tabs
close_tab_pt = (795, 77)
def __init__(self, data: dict):
"""
Initializes all data needed to be inputted
or checked by Inputter
:param data
dictionary of all data
- vendor, date, and invoice num should be strings
- rows should follow this format
with a list being returned and each
element representing a row with a billing_code
and price:
"rows": {
'billing_code': price
}
- id_num should be a tuple with the first element
being the num type (ex. MAWB, Internal Reference Num)
and the second element being the actual number
ex.
data = {
"vendor": 'MKC CUSTOMS BROKERS',
"date": '072823',
"invoice_num": '0604751',
"rows": {
"AICUSTOM": "23",
"AI-DUTY": "32"
},
"id_num": (IDNumType.MAWB, '15792303271')
}
"""
self.vendor = data['vendor']
self.date = data['date']
self.invoice_num = data['invoice_num']
self.price_rows = data['rows']
self.id_num_type = data['id_num'][0]
self.id_num = data['id_num'][1]
@staticmethod
def login_to_freightstream():
time.sleep(10)
gui.click(*Inputter.user_pt)
with open("credentials.txt", 'r', encoding='utf-8') as f:
lines = f.readlines()
user = lines[3]
pwd = lines[4]
time.sleep(1)
gui.write(user)
time.sleep(1)
gui.press('tab')
time.sleep(1)
gui.write(pwd)
time.sleep(1)
gui.press('enter')
time.sleep(10)
@staticmethod
def exit_freightstream():
time.sleep(10)
gui.click(*Inputter.exit_pt)
time.sleep(2)
gui.click(*Inputter.confirm_exit_pt)
time.sleep(10)
def click_air_import(self):
gui.click(*self.nav_bar_air_import_option_pt)
def click_hawb_list(self):
gui.click(*self.hawb_list_dropdown_option_pt)
def search_id(self):
# click on reference number column
# press F5
# enter search term
# press enter
# top row will be the correct one
# then hit F7
if self.id_num_type == IDNumType.MAWB:
gui.click(*self.mawb_num_column_first_row_pt)
elif self.id_num_type == IDNumType.INTERNAL_REFERENCE:
gui.click(*self.reference_num_column_first_row_pt)
time.sleep(2)
gui.press('f5')
time.sleep(2)
gui.write(self.id_num)
time.sleep(0.5)
gui.press('enter')
def access_accounting(self):
gui.press('f7')
def click_account_payable_header(self):
gui.click(*self.account_payable_header_pt)
def access_account_payable(self):
gui.press('f2')
def vendor_already_exists(self):
import cv2
image = np.array(gui.screenshot())
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# path = r"C:\Users\ianbb\PycharmProjects\FreightStreamTranscriber\freightStreamImages\4.1accountingWithRow.png"
# path = r"C:\Users\ianbb\PycharmProjects\FreightStreamTranscriber\freightStreamImages\4.5accountingWithMKCAtEnd.png"
# path = r"C:\Users\ianbb\PycharmProjects\FreightStreamTranscriber\freightStreamImages\4.6accountingWithNoMKC.png"
# image = cv2.imread(path)
# image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
last_seen = None
row_rect = [622, 648, 289, 485]
y_diff = 22
# while last_seen != seen:
for i in range(4):
text = vision.get_text_from_cropped_rect_of_image(row_rect, image, has_pixel_values=True).strip()
if not text:
return False # should CONTINUE full pipeline function
elif self.vendor[:18] in text: #TODO: fuzzy match
return True # should STOP full pipeline function
else:
seen = i
row_rect[0] += y_diff
row_rect[1] += y_diff
if last_seen == seen:
return False # should CONTINUE full pipeline function
else:
last_seen = seen
# TODO: implement scrolling and rerun the for loop and the last if statement, maybe use a while loop
# Unknown how many vendors are possible
"""
last_seen = None
def check_table:
if grey in first row:
stop
check for vendor name
elif name in first row:
stop entire pipeline
else:
seen = whatever the i in range is
continue
loop until 4th row
if last_seen == seen:
stop
else:
last_seen = seen
gui.scroll OR whatever the way is to move the rows down
screenshot again
run check_table() again
"""
def edit_vendor(self):
gui.click(*self.vendor_field_pt)
gui.write('%' + self.vendor)
time.sleep(1)
gui.press('enter')
time.sleep(4)
# TODO: If needed, OCR the entire column of names and choose from that
# click on the first row
gui.click(*self.reference_num_column_first_row_pt, clicks=2, interval=0.25)
def edit_dates(self):
# input post
gui.click(*self.post_date_field_pt)
gui.write(self.date)
time.sleep(1)
# input invoice date
gui.click(*self.invoice_date_field_pt)
gui.write(self.date)
time.sleep(1)
def edit_invoice_num(self):
gui.click(*self.invoice_num_field_pt)
gui.write(self.invoice_num)
def click_billing_code_field(self):
gui.click(*self.billing_code_first_row_pt)
def edit_billing_code(self, billing_code):
gui.write(billing_code, interval=0.2)
def edit_price(self, price):
gui.write(price, interval=0.2)
def edit_account_payable_row(self):
# TODO: Detect if a row already exists
# Potentially implement this if we are doing a very optimized bot that clicks on existing vendor rows and checks that all things are as they should
self.click_billing_code_field()
for code, price in self.price_rows.items():
self.edit_billing_code(code)
time.sleep(1)
gui.press('tab', presses=2, interval=0.1)
time.sleep(1)
self.edit_price(price)
time.sleep(1)
gui.press('tab', presses=3, interval=0.1)
time.sleep(1)
def close_all_tabs(self):
gui.click(*self.close_tab_pt, clicks=4, interval=1)
time.sleep(1)
def run_full_pipeline(self):
# click air import
# click Master AWB
# search for awb
# Type F7
# Click on Account Payable header
# click on existing row if vendor exists, then F2
# To create new row, F2
# Edit Account Payable Fields
if debug:
print("RUNNING INPUTTER")
print(self.vendor)
print(self.date)
print(self.invoice_num)
print(self.id_num_type)
print(self.id_num)
print(self.price_rows)
time.sleep(3)
# Main screen
self.click_air_import()
time.sleep(1)
self.click_hawb_list()
time.sleep(5)
# HAWB List screen
self.search_id()
time.sleep(5)
# Accounting screen
self.access_accounting()
time.sleep(5)
if self.vendor_already_exists():
return
self.click_account_payable_header()
time.sleep(1)
# Account Payable screen
self.access_account_payable()
time.sleep(3)
self.edit_vendor()
time.sleep(2)
self.edit_dates()
time.sleep(1)
self.edit_invoice_num()
time.sleep(1)
self.edit_account_payable_row()
# TODO: DO NOT PRESS F12 TO SAVE BECAUSE THAT WILL MAKE CHANGES TO DB
# TODO: but eventually you press F12
self.close_all_tabs()
if __name__ == '__main__':
bot = Inputter({})
time.sleep(3)
bot.run_full_pipeline()
if debug:
# Main screen
bot.click_air_import()
time.sleep(1)
bot.click_hawb_list()
time.sleep(5)
# HAWB List screen
bot.search_id()
time.sleep(5)
# Accounting screen
bot.access_accounting()
time.sleep(5)
bot.click_account_payable_header()
time.sleep(1)
# Account Payable screen
bot.access_account_payable()
time.sleep(3)
bot.edit_vendor()
time.sleep(2)
bot.edit_dates()
time.sleep(1)
bot.edit_invoice_num()
time.sleep(1)
bot.edit_account_payable_row()