-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.py
More file actions
348 lines (316 loc) · 14.7 KB
/
interface.py
File metadata and controls
348 lines (316 loc) · 14.7 KB
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
from inventory_main import InterfaceActions
from database import *
from order_form import *
from item import Product, StaffMember, ProductInventory
from order_form import *
#import PySimpleGUI as psg
from typing import TYPE_CHECKING, Any
from typing import Union, List, Callable
if TYPE_CHECKING:
psg = Any
else:
import PySimpleGUI as psg
#type check alas
TableData = list[list[str | int]]
StockRecord = tuple[str, str, str, str, str, int, str, int, int]
class Display:
"""Takes StockItems and holds them in a dictionary, has function to update the inventory,
update details, create a CSV file and print out the inventory."""
def __init__(self) -> None:
#Instantuate
self.stock = InStock()
self.product = ProductDB()
self.inventory = InterfaceActions()
self.order_form = Order()
#field attributes for the main window
self.headers = self.stock.get_header_names()
self.window_stock = None
self.stock_table = None
#Order information
self.prod_id_list_for_orders:List[str] = []
def table_template(self,data_list:List[List[str]],headers:List[str],a_key:str) -> psg.Table:
"""@returns a pysimpleGUI table.
@args data_list all the data you require in the table.
@args headers is a stringlist of the column headers you require.
@args key is a string key linked to the table."""
table = psg.Table(
values=data_list,
headings=headers,
max_col_width=30,
auto_size_columns=False,
display_row_numbers=False,
selected_row_colors=('light blue', 'dark blue'),
justification="left",
enable_events='True',
num_rows=10,
key=a_key,
row_height=35,)
return table
def create_table_data(self, data_source:Callable[[], list[tuple[str]]]) -> TableData:
"""
Collects all the records from the data_source, refines a list to hold the fields
needed for the display menus in the frontend.
@args function that returns all the records from a choosen database.
@return list of list containing strings and integers.
"""
data = data_source()
new_data:List[List[Union[str,int]]] = []
for record in data:
record_object = ProductInventory(*record)
display_data = record_object.display_data()
new_data.append(display_data)
return new_data
def main(self) -> None:
"""Shows all the current stock in the Pysimplegui application."""
#list of list holding data records
new_data = self.create_table_data(self.stock.get_all_records)
#creates the table
psg_table = self.table_template(new_data,self.headers,'-TABLE-')
#create the window layout built up in rows.
layout = [
[psg.Button("Refresh", size=(10,0)),psg.Push(),psg.Text("***STOCK MAIN MENU***",size=(60,1), justification='centre'),psg.Push()],
#Table displayed at this row of the layout
[psg_table],
[psg.Push(),
psg.Button("Add To Stock", size=(20,1),pad=(35,10)),
psg.Button("Check Low Stock", size=(20,1),pad=(35,10)),
psg.Button("View All Products", size=(20,1),pad=(35,10)),
psg.Push()]
]
self.window_stock = psg.Window("Products in Stock", layout)
while True:
event, values = self.window_stock.read()
if event == psg.WIN_CLOSED:
break
if event == 'Refresh':
# data = self.stock.get_all_records()
# new_data:List[List[Union[str,int]]] = [list(record) for record in data]
new_data = self.create_table_data(self.stock.get_all_records)
layout[1][0].update(values=new_data)
if event == '-TABLE-':
if values['-TABLE-']:
try:
index:str = values['-TABLE-'][0]
record:str = new_data[index][0]
#print(type(record))
self.crud_window(record)
except IndexError:
print(values['-TABLE-'])
if event == 'View All Products':
self.show_all_products()
if event == 'Check Low Stock':
self.show_low_stock()
if event == 'Add To Stock':
empty_list=['','','','','','','']
self.crud_window(empty_list)
self.window_stock.close()
def crud_window(self, record_id:str) -> None:
"""
Allows the user to update the selected record, create a new record, edit or
delete a record.
@args the product ID of the selected product as a string.
"""
#locate the record from the database
record_data = self.stock.get_record(record_id)
#create a product object with the record information
my_record = ProductInventory(*record_data[0])
my_record_display_details = my_record.display_data()
#create the window layout built up in rows.
layout = [
[psg.Text("***AMEND STOCK ITEM***",size=(40,1), justification='centre')],
]
for index, header in enumerate(self.headers):
new_row = [psg.Text(f'{header}',size=(20,1)),
psg.Input(default_text=f"{my_record_display_details[index]}", key='TEXT', size=(20,1),disabled=True,),
]
layout.append(new_row)
layout.append([
psg.Push(),
psg.Button("Create record", size=(10,1), button_color='blue'),
psg.Button("Update Record", size=(15,1),button_color='blue'),
psg.Button("edit", size=(10,1), button_color="grey"),
psg.Push()
])
layout.append([psg.Push(),
psg.Button("Delete", size=(10,1), button_color="red"),
psg.Push()
])
window_record = psg.Window(f"Record for {my_record.name}", layout)
while True:
event, values = window_record.read()
if event == psg.WIN_CLOSED:
break
if event == 'Create record':
for i in range(1,8):
layout[i][1].update(value='')
if event == 'edit':
for i in range(1,8):
layout[i][1].update(disabled=False)
if event == 'Update Record':
updated_record = my_record.update_display_fields(*values.values())
self.inventory.create_or_update_record(my_record)
break
if event == 'Delete':
self.inventory.delete_record(my_record)
break
window_record.close()
def show_low_stock (self) -> None:
"""Checks what items are below the set limits and displays the
items in a GUI table."""
#List of list holding the data records.
new_data = self.create_table_data(self.stock.need_ordering)
#Creates the table
psg_table = self.table_template(new_data,self.headers,'-TABLE-')
#create the window layout built up in rows.
layout = [
[psg.Push(),
(psg.Text("***LOW SOCK***",size=(80,1), justification='centre')),
psg.Push()],
[psg_table],
[psg.Push(),
psg.Button("Close Window", size=(20,1), key='-Close-'),psg.Button("Add To Order", size=(20,1), key='-Order-'),
psg.Push()
],
]
window_ordering = psg.Window("Need ordering", layout)
while True:
event, values = window_ordering.read()
if event == psg.WIN_CLOSED or event == "-Close-":
break
if event == '-Order-':
v:List[int] = values['-TABLE-']
if len(v) > 0:
selected_prods = [rec for i, rec in enumerate(new_data) if i in v]
for x in selected_prods:
self.prod_id_list_for_orders.append(x[0])
self.create_order(self.prod_id_list_for_orders)
break
window_ordering.close()
def show_all_products(self) -> None:
"""Checks with the database what products are available. Diaplays
items in a GUI table."""
#List of lists holding data records.
new_data = self.create_table_data(self.stock.get_all_records)
#top row
row1 = [psg.Push(),psg.Frame('Prduct Main Menu:',
[[psg.Text(f"* * * "*29, text_color=('yellow'))],
[psg.Text("*WHEN CREATING AN ORDER PRESS CRTL KEY TO SELECT MULTIPLE PRODUCTS, THEN PRESS ADD TO ORDER BUTTON *", text_color='yellow')],
[psg.Text(f"* * * "*29,text_color=('yellow'))],
],),psg.Push()]
#row2 - frame 1
row2 = [psg.Frame('Products:',
[[psg.Table(
values=new_data,
headings=self.headers,
max_col_width=25,
auto_size_columns=True,
display_row_numbers=False,
justification="left",
selected_row_colors=('light blue', 'dark blue'),
enable_events='True',
num_rows=10,
key="-TABLE-",
row_height=25,)]])]
#row3
row3 = [psg.Push(),
psg.Button("Close Window", size=(20,1), key='-Close-'),
psg.Button("Add To Order", size=(15,1)),
psg.Push()]
#Create the layout
layout = [[row1],[row2],[row3]]
window_product = psg.Window("Availabe products", layout)
while True:
event, values = window_product.read()
if event == psg.WIN_CLOSED:
break
if event == 'Add To Order':
v:List[int] = values['-TABLE-']
if len(v) > 0:
#list containing all the product ids from the selected records.
selected_prods = [str(rec[0]) for i, rec in enumerate(new_data) if i in v]
self.create_order(selected_prods)
break
if event == '-Close-':
break
window_product.close()
def create_order(self, list_of_prod_ids:List[str]):
"""Shows a table of the selected products from the previous screen.
Products can be added and removed."""
#Checks if an item already exists in the current order list
def item_check( name:str) -> bool:
for x in new_data:
if name in x:
return True
return False
#creates a list of tuples each being a product record
data = self.product.get_selection_of_records(list_of_prod_ids)
#Creates a list of lists each list is a string of product fields
new_data:List[List[str]] = []
for record in data:
#casts to a list removes the product version held at index 1, appends to the data list
record = list(record)
record.pop(1)
new_data.append(record)
#Creates the table
psg_table = self.table_template(new_data,self.product.get_header_names(),'-TABLE-')
#creates a dictionary key is the product name as a string the value is a product object.
product_objects = self.inventory.get_product_objects()
#creates a dictionary key is the staff name as a string the value is a staff object.
staff_objects = self.inventory.get_staff_objects()
#column 1
col1 = psg.Column([[psg.Frame('Products:',
[[psg_table]]
)
]])
#column 2
col2 = psg.Column([[psg.Frame('Order Details:',
[[psg.Listbox([f'{product_objects[prod].name}' for prod in product_objects],expand_y=True,size=(20,10),key='-PROD-' )]]
)],
[psg.Frame('Staff Details',
[[psg.Listbox([f'{staff_objects[staff].staff_name}' for staff in staff_objects],expand_y=True,size=(15,10),key='-STAFF-')]]
)
]])
#row2
bottom_row = [psg.Push(),
psg.Button("Remove Product", size=(15,1)),
psg.Button("Add Another Product", size=(15,1)),
psg.Button("Create Spread Sheet", size=(15,1)),psg.Push()]
#Create window layout
layout = [psg.vtop([col1,col2]),[bottom_row]]
window_order = psg.Window("Order Form", layout)
while True:
event, values = window_order.read()
if event == psg.WIN_CLOSED:
break
if event == 'Add Another Product':
if len(values['-PROD-'])>0:
name = values['-PROD-'][0]
obj = product_objects[name]
if not item_check(name):
new_data.append(obj.display_data())
layout[0][0].get_next_focus().update(values=new_data)
if event == 'Remove Product':
if len(values['-TABLE-'])>0:
order_item_index:int = values['-TABLE-'][0]
row = new_data[order_item_index]
del_name = row[1]
if item_check(del_name):
new_data.pop(order_item_index)
#access the pysimplegui table element
layout[0][0].get_next_focus().update(values=new_data)
if event == 'Create Spread Sheet':
#empty dictionary holding products and staff members
prod_order_list:dict[str,list[Product|StaffMember]] = {'products':[],'staff':[]}
for x in new_data:
obj = product_objects[x[1]]
prod_order_list['products'].append(obj)
obj = staff_objects[values['-STAFF-'][0]]
prod_order_list['staff'].append(obj)
#creates the order form adds the order, saves and closes file.
self.order_form.write_order_to_file(prod_order_list)
new_data = []
layout[0][0].get_next_focus().update(values=new_data)
window_order.close()
if __name__== "__main__":
dis = Display()
dis.main()