-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew_Order_Window.py
366 lines (313 loc) · 15 KB
/
New_Order_Window.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
import difflib as dl
import random
from datetime import datetime
import tinydb
from guizero import CheckBox, Combo, ListBox, PushButton, Text, TextBox, Window
import Common
import Inventory_Management_Window
import PackingSlip
def PriceUpdate():
global PurchaseName, address, address2, city, state, ZipCode, PricingOptionButton, item1, item2, item3, item4, item5
global ItemQuantity1, ItemQuantity2, ItemQuantity3, ItemQuantity4, ItemQuantity5, ItemPrice1, ItemPrice2, ItemPrice3, ItemPrice4
global ItemPrice5, Total
global window2, product_names, styles, products
# Get the closest match to the item name
try:
autofill1 = dl.get_close_matches(item1.value, product_names)
item1.value = autofill1[0] # Set the item name to the closest match
item1_data = products.search(
(tinydb.Query().product_name == item1.value)
& (tinydb.Query().process_status == "UTILIZE")
) # Get the data for the item
# Set the price to the price of the item times the quantity
ItemPrice1.value = "$" + str(
Common.MonetaryMultiply(
item1_data[0][PricingOptionButton.value.replace(" ", "_")],
ItemQuantity1.value,
)
)
except:
ItemPrice1.value = "NA"
try:
autofill2 = dl.get_close_matches(item2.value, product_names)
item2.value = autofill2[0] # Set the item name to the closest match
item2_data = products.search(
(tinydb.Query().product_name == item2.value)
& (tinydb.Query().process_status == "UTILIZE")
) # Get the data for the item
# Set the price to the price of the item times the quantity
ItemPrice2.value = "$" + str(
Common.MonetaryMultiply(
item2_data[0][PricingOptionButton.value.replace(" ", "_")],
ItemQuantity2.value,
)
)
except:
ItemPrice2.value = "NA"
try:
autofill3 = dl.get_close_matches(item3.value, product_names)
item3.value = autofill3[0] # Set the item name to the closest match
item3_data = products.search(
(tinydb.Query().product_name == item3.value)
& (tinydb.Query().process_status == "UTILIZE")
) # Get the data for the item
# Set the price to the price of the item times the quantity
ItemPrice3.value = "$" + str(
Common.MonetaryMultiply(
item3_data[0][PricingOptionButton.value.replace(" ", "_")],
ItemQuantity3.value,
)
)
except:
ItemPrice3.value = "NA"
try:
autofill4 = dl.get_close_matches(item4.value, product_names)
item4.value = autofill4[0] # Set the item name to the closest match
item4_data = products.search(
(tinydb.Query().product_name == item4.value)
& (tinydb.Query().process_status == "UTILIZE")
) # Get the data for the item
# Set the price to the price of the item times the quantity
ItemPrice4.value = "$" + str(
Common.MonetaryMultiply(
item4_data[0][PricingOptionButton.value.replace(" ", "_")],
ItemQuantity4.value,
)
)
except:
ItemPrice4.value = "NA"
try:
autofill5 = dl.get_close_matches(item5.value, product_names)
item5.value = autofill5[0] # Set the item name to the closest match
item5_data = products.search(
(tinydb.Query().product_name == item5.value)
& (tinydb.Query().process_status == "UTILIZE")
) # Get the data for the item
# Set the price to the price of the item times the quantity
ItemPrice5.value = "$" + str(
Common.MonetaryMultiply(
item5_data[0][PricingOptionButton.value.replace(" ", "_")],
ItemQuantity5.value,
)
)
except:
ItemPrice5.value = "NA"
# Calculate the total
Total.value = "Total: $" + str(
Common.MonetarySummation(
[
ItemPrice1.value,
ItemPrice2.value,
ItemPrice3.value,
ItemPrice4.value,
ItemPrice5.value,
]
)
)
def export():
"""Export the order to the database and optionally generate a packing slip and/or ship the order"""
global PurchaseName, address, address2, city, state, ZipCode, PricingOptionButton, item1, item2, item3, item4, item5
global ItemQuantity1, ItemQuantity2, ItemQuantity3, ItemQuantity4, ItemQuantity5, ItemPrice1, ItemPrice2, ItemPrice3, ItemPrice4
global ItemPrice5, Total, ChooseExportCheckBox, ChooseShippingCheckBox, finish
global window2, styles, product_names, products, orders, order_items, DateField, ForwardDataBase
global doModifyInventory, InventorySelection
global orderNotes
if doModifyInventory.value:
inventory = []
for item, quantity in zip(
[item1, item2, item3, item4, item5],
[ItemQuantity1, ItemQuantity2, ItemQuantity3, ItemQuantity4, ItemQuantity5],
):
if item.value != "Empty" and item.value != "" and quantity.value != "0":
inventory.append([item.value, int(quantity.value)])
success = Inventory_Management_Window.OrderModifyInventory(
window2, ForwardDataBase, InventorySelection.value, inventory
)
if not success:
window2.warn("Error", "Inventory Modification Failed. Export Order Canceled.")
return
OrderNumber = Common.MakeOrderID(orders) # Make a unique order ID
ItemCount = 0
for item in [item1, item2, item3, item4, item5]: # Count the number of items
if item.value != "Empty" and item.value != "": # Count the number of items
ItemCount += 1
# Make a list of UIDs for the order items
itemsUIDs = Common.MakeUIDs(order_items, ItemCount)
# Replace the / with a - to clean up the date
DateField.value = DateField.value.replace("/", "-")
orders.insert(
{
"order_number": str(OrderNumber),
"order_name": PurchaseName.value,
"order_address": address.value,
"order_address2": address2.value,
"order_city": city.value,
"order_state": state.value,
"order_zip": ZipCode.value,
"order_items_UID": itemsUIDs,
"order_date": DateField.value,
"order_pricing_style": PricingOptionButton.value.replace(" ", "_"),
"order_notes": orderNotes.value,
"order_status": "OPEN",
"process_status": "UTILIZE",
}
) # Insert the order into the database
PriceUpdate()
ItemQuantities = [
ItemQuantity1.value,
ItemQuantity2.value,
ItemQuantity3.value,
ItemQuantity4.value,
ItemQuantity5.value,
] # Make a list of the quantities
ItemIncrement = 0
UIDIncrement = 0
# Insert the order items into the database
for item in [item1, item2, item3, item4, item5]:
if item.value != "Empty" and item.value != "": # Insert the order items into the database
# Get the data for the item
product = products.search(tinydb.Query().product_name == item.value)
order_items.insert(
{
"item_UID": itemsUIDs[UIDIncrement],
"item_name": product[0]["product_name"],
"item_quantity": int(ItemQuantities[ItemIncrement]),
"item_unit_price": int(product[0][PricingOptionButton.value.replace(" ", "_")]),
"process_status": "UTILIZE",
"product_snapshot": product[0],
}
) # Insert the order item into the database
UIDIncrement += 1
ItemIncrement += 1
if ChooseExportCheckBox.value == 1: # If the user wants to export the order
PackingSlip.PrintPackingSlip(
window2, ForwardDataBase, str(OrderNumber)
) # tf why is order number a string? too much work to change it now
if ChooseShippingCheckBox.value == 1: # If the user wants to ship the order
# ShippingHandler.ShipOrder(order)
pass
window2.destroy() # Close the window
def SelectedProductFill(event): # Fill the product name in the widget
global widget, window3
ItemsList = event.widget # Get the listbox
widget.value = ItemsList.value # Set the widget to the selected item
window3.destroy() # Close the window
PriceUpdate() # Update the prices
def DropDownSelection(event): # Select a product from the dropdown
global window2, product_names, widget, window3
widget = event.widget # Get the widget
window3 = Window(
window2, title="Select Product", layout="grid", width=400, height=400
) # Create a new window
title = Text(window3, text="Select Product", grid=[0, 0]) # Create a title
ItemsList = ListBox(
window3, items=product_names, grid=[0, 1], scrollbar=True, width=350, height=350
) # Create a listbox
# When the user double clicks an item, run the SelectedProductFill function
ItemsList.when_double_clicked = SelectedProductFill
def InventoryComboVisibility():
global doModifyInventory, InventorySelection
if doModifyInventory.value == 1:
InventorySelection.show()
else:
InventorySelection.hide()
def NewOrder(main_window, database):
global PurchaseName, address, address2, city, state, ZipCode, PricingOptionButton, item1, item2, item3, item4, item5
global ItemQuantity1, ItemQuantity2, ItemQuantity3, ItemQuantity4, ItemQuantity5, ItemPrice1, ItemPrice2, ItemPrice3, ItemPrice4
global ItemPrice5, Total, ChooseExportCheckBox, ChooseShippingCheckBox, finish
global window2, styles, product_names, products, orders, order_items, DateField, ForwardDataBase
global doModifyInventory, InventorySelection
global orderNotes
products = database.table("Products") # Get the products table
ForwardDataBase = database # Set the forward database to the database
# Get the product pricing styles table
product_pricing_styles = database.table("Product_Pricing_Styles")
orders = database.table("Orders") # Get the orders table
order_items = database.table("Order_Items") # Get the order items table
active_products = products.search(
tinydb.Query().process_status == "UTILIZE"
) # Get all the active products
product_names = []
for product in active_products: # Get all the active product names
# Add the product name to the list
product_names.append(product["product_name"])
styles = []
ActiveStyles = product_pricing_styles.search(
tinydb.Query().process_status == "UTILIZE"
) # Get all the active styles
for style in ActiveStyles:
styles.append(style["style_name"])
inventories = Inventory_Management_Window.GetInventoryGroups(
database
) # Get the inventory groups
window2 = Window(
main_window, title="New Order", layout="grid", width=1100, height=700
) # Create a new window
WelcomeMessage = Text(
window2,
text="Enter New Order Information",
size=18,
font="Times New Roman",
grid=[1, 0],
) # Create a welcome message
PurchaseNameText = Text(
window2, text="Buyer Name", size=15, font="Times New Roman", grid=[0, 1]
) # Buyer name
PurchaseName = TextBox(window2, grid=[1, 1], width=30)
# shipping info
AddressText = Text(window2, text="Address", size=15, font="Times New Roman", grid=[0, 3])
address = TextBox(window2, grid=[1, 3], width=60)
AddressText2 = Text(window2, text="Line 2", size=15, font="Times New Roman", grid=[0, 4])
address2 = TextBox(window2, grid=[1, 4], width=60)
CityText = Text(window2, text="City", size=15, font="Times New Roman", grid=[0, 5])
city = TextBox(window2, grid=[1, 5], width=30)
StateText = Text(window2, text="State", size=15, font="Times New Roman", grid=[0, 6])
state = TextBox(window2, grid=[1, 6], width=10)
ZipText = Text(window2, text="Zip Code", size=15, font="Times New Roman", grid=[0, 7])
ZipCode = TextBox(window2, grid=[1, 7], width=10)
# items header
ItemsMessage = Text(window2, text="Include Items", size=18, font="Times New Roman", grid=[1, 8])
PricingOptionButton = Combo(window2, options=styles, command=PriceUpdate, grid=[2, 7])
# items
item1 = TextBox(window2, width=30, grid=[0, 9], text="Empty")
item1.when_double_clicked = DropDownSelection
ItemQuantity1 = TextBox(window2, grid=[1, 9], width=10, command=PriceUpdate, text="0")
ItemPrice1 = Text(window2, text="0", size=15, font="Times New Roman", grid=[2, 9])
item2 = TextBox(window2, width=30, grid=[0, 10], text="Empty")
item2.when_double_clicked = DropDownSelection
ItemQuantity2 = TextBox(window2, grid=[1, 10], width=10, command=PriceUpdate, text="0")
ItemPrice2 = Text(window2, text="0", size=15, font="Times New Roman", grid=[2, 10])
item3 = TextBox(window2, width=30, grid=[0, 11], text="Empty")
item3.when_double_clicked = DropDownSelection
ItemQuantity3 = TextBox(window2, grid=[1, 11], width=10, command=PriceUpdate, text="0")
ItemPrice3 = Text(window2, text="0", size=15, font="Times New Roman", grid=[2, 11])
item4 = TextBox(window2, width=30, grid=[0, 12], text="Empty")
item4.when_double_clicked = DropDownSelection
ItemQuantity4 = TextBox(window2, grid=[1, 12], width=10, command=PriceUpdate, text="0")
ItemPrice4 = Text(window2, text="0", size=15, font="Times New Roman", grid=[2, 12])
item5 = TextBox(window2, width=30, grid=[0, 13], text="Empty")
item5.when_double_clicked = DropDownSelection
ItemQuantity5 = TextBox(window2, grid=[1, 13], width=10, command=PriceUpdate, text="0")
ItemPrice5 = Text(window2, text="0", size=15, font="Times New Roman", grid=[2, 13])
# Total
Total = Text(window2, text="Total: $0", size=18, font="Times New Roman", grid=[2, 19])
DateText = Text(window2, text="Order Date: ", size=15, font="Times New Roman", grid=[0, 18])
DateField = TextBox(window2, grid=[1, 18], width=15, text=datetime.today().strftime("%m-%d-%Y"))
# Export Options
ChooseExportCheckBox = CheckBox(window2, text="Generate Packing Slip", grid=[1, 19])
ChooseShippingCheckBox = CheckBox(window2, text="Ship Order", grid=[1, 20])
finish = PushButton(window2, command=export, text="Save", grid=[0, 19])
doModifyInventory = CheckBox(
window2, text="Modify Inventory", grid=[0, 20], command=InventoryComboVisibility
)
# window2.repeat(
# 500, InventoryComboVisibility
# ) # Check if the checkbox is checked, and show/hide the inventory selection combo box
# this is a hacky way to do it, but it works, and I don't know how to do it better
InventorySelection = Combo(window2, options=inventories, grid=[0, 21])
orderNotesText = Text(
window2, text="Order Notes", size=15, font="Times New Roman", grid=[0, 22]
)
orderNotes = TextBox(
window2, grid=[0, 23, 4, 3], width=60, height=10, multiline=True, scrollbar=True
)