-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackingSlip.py
400 lines (350 loc) · 14.9 KB
/
PackingSlip.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import copy
import os
import platform
import subprocess
import traceback
import tinydb
from PIL import Image, ImageDraw, ImageFont
import Common
def TextWrap(text, rowLength):
"""Wraps text to fit within a given row length. Also hydrates escape sequences if allowed.
Args:
text (Str): The text to wrap.
rowLength (Int): The maximum length of each row.
Returns:
Str: The wrapped text.
"""
wrappedText = copy.copy(text) # copy text to avoid modifying original as it will be pull apart
i = 0 # index of the current character
length = 0 # length of the current line
while i < len(wrappedText):
if wrappedText[i] == "\n": # if a newline is found, reset the length
length = 0
else:
length += 1
if length >= rowLength: # if the length is greater than the row length
# find the last space before the row length
for j in range(
i, i - rowLength, -1
): # search backwards from the current character to the row length
if (
wrappedText[j] == " " # break at spaces in sentence
): # if a space is found, break the loop and wrap the text at that space
wrappedText = wrappedText[:j] + "\n" + wrappedText[j + 1 :]
break
elif wrappedText[j] == "/": # break at slashes in URLs
wrappedText = (
wrappedText[: j + 1] + "\n" + wrappedText[j + 1 :]
) # include the slash in the next line
break
length = 0
i += 1
return wrappedText
def PrintPackingSlip(app, database, OrderNumber):
"""Prints a packing slip for the given order number.
Args:
app (GuiZero Window): The main window of the program.
database (TindyDb): The database to use.
OrderNumber (Str): The order number to print the packing slip for.
"""
if type(OrderNumber) != str:
OrderNumber = str(OrderNumber)
app.warn("Order Number Error", "The order number must be a string. Casting to string.")
try:
settings = database.table("Settings")
orders = database.table("Orders")
order_items = database.table("Order_Items")
# Formatting
Path = settings.search(
(tinydb.Query().setting_name == "Packing_Slip_Background_Path")
& (tinydb.Query()["process_status"] == "UTILIZE")
)[0]["setting_value"]
if Path == "":
Path = os.path.join(os.path.realpath(os.path.dirname(__file__)), "Order_Slip.png")
TextColor = settings.search(
(tinydb.Query().setting_name == "Packing_Slip_Text_Color")
& (tinydb.Query()["process_status"] == "UTILIZE")
)[0]["setting_value"]
if TextColor == "":
TextColor = "#000000"
R = TextColor[1:3]
G = TextColor[3:5]
B = TextColor[5:7]
# map to 0-255
R = int(R, 16)
G = int(G, 16)
B = int(B, 16)
TextColor = (R, G, B)
IncludePrices = settings.search(
(tinydb.Query().setting_name == "Packing_Slip_Include_Prices")
& (tinydb.Query()["process_status"] == "UTILIZE")
)[0]["setting_value"]
if IncludePrices == "": # if blank
IncludePrices = True
elif IncludePrices == "True": # if true
IncludePrices = True
elif IncludePrices == "False": # if false
IncludePrices = False
ImagesFolderPath = settings.search(
(tinydb.Query().setting_name == "Images_Folder_Path")
& (tinydb.Query()["process_status"] == "UTILIZE")
)[0]["setting_value"]
IncludeOrderNotes = settings.search(
(tinydb.Query().setting_name == "Packing_Slip_Include_Notes")
& (tinydb.Query()["process_status"] == "UTILIZE")
)[0]["setting_value"]
CompanyName = settings.search(
(tinydb.Query().setting_name == "Company_Name")
& (tinydb.Query()["process_status"] == "UTILIZE")
)[0]["setting_value"]
CompanyAddress1 = settings.search(
(tinydb.Query().setting_name == "Company_Address_Ln1")
& (tinydb.Query()["process_status"] == "UTILIZE")
)[0]["setting_value"]
CompanyAddress2 = settings.search(
(tinydb.Query().setting_name == "Company_Address_Ln2")
& (tinydb.Query()["process_status"] == "UTILIZE")
)[0]["setting_value"]
CompanyCity = settings.search(
(tinydb.Query().setting_name == "Company_City")
& (tinydb.Query()["process_status"] == "UTILIZE")
)[0]["setting_value"]
CompanyState = settings.search(
(tinydb.Query().setting_name == "Company_State")
& (tinydb.Query()["process_status"] == "UTILIZE")
)[0]["setting_value"]
CompanyZip = settings.search(
(tinydb.Query().setting_name == "Company_Zip")
& (tinydb.Query()["process_status"] == "UTILIZE")
)[0]["setting_value"]
# Order
try:
order = orders.search(
(tinydb.Query().order_number == OrderNumber)
& (tinydb.Query().process_status == "UTILIZE")
)[0]
except IndexError:
app.warn(
"Order Error",
"The order could not be found. Please check the order number and try again.",
)
return
# Order Items
try:
UIDs = order["order_items_UID"]
items = []
for UID in UIDs:
items.append(
order_items.search(
(tinydb.Query().item_UID == UID)
& (tinydb.Query().process_status == "UTILIZE")
)[0]
)
except IndexError:
app.warn(
"Order Items Error",
"The order items could not be found. Please check the order number and try again.",
)
return
# open image
try:
with Image.open(Path) as im:
Canvas = ImageDraw.Draw(im)
except OSError:
app.warn(
"Open Image Error",
"The image file could not be opened. Please check the path and try again.",
)
return
# Fonts
SmallFont = ImageFont.truetype(
os.path.join(os.path.realpath(os.path.dirname(__file__)), "Bright.TTF"), 15
)
NormalFont = ImageFont.truetype(
os.path.join(os.path.realpath(os.path.dirname(__file__)), "Bright.TTF"), 30
)
# Dividing Line
Canvas.line((400, 600, 400, 1800), fill=(TextColor))
# Ship To
Name = order["order_name"]
Address1 = order["order_address"]
Address2 = order["order_address2"]
City = order["order_city"]
State = order["order_state"]
Zip = order["order_zip"]
Canvas.text((20, 640), "Ship To:", font=NormalFont, fill=(TextColor))
Canvas.text((20, 690), Name, font=NormalFont, fill=(TextColor))
if Address1 != "": # if address available
Canvas.text((20, 730), Address1, font=NormalFont, fill=(TextColor))
if Address2 != "": # if address2 available
Canvas.text((20, 770), Address2, font=NormalFont, fill=(TextColor))
if City != "" and State != "" and Zip != "":
Canvas.text(
(20, 810),
City + ", " + State + " " + Zip,
font=NormalFont,
fill=(TextColor),
)
elif City != "" and State != "":
Canvas.text(
(20, 810),
City + ", " + State,
font=NormalFont,
fill=(TextColor),
)
elif State != "" and Zip != "":
Canvas.text((20, 810), State + " " + Zip, font=NormalFont, fill=(TextColor))
# if address2 not available but city, state, or zip is
elif Address2 == "" and (City != "" or State != "" or Zip != ""):
if City != "" and State != "" and Zip != "":
Canvas.text(
(20, 770),
City + ", " + State + " " + Zip,
font=NormalFont,
fill=(TextColor),
)
elif City != "" and State != "":
Canvas.text(
(20, 770),
City + ", " + State,
font=NormalFont,
fill=(TextColor),
)
elif State != "" and Zip != "":
Canvas.text((20, 770), State + " " + Zip, font=NormalFont, fill=(TextColor))
# From (100 between)
Canvas.text((20, 870), "Ship From:", font=NormalFont, fill=(TextColor))
Canvas.text((20, 920), CompanyName, font=NormalFont, fill=(TextColor))
Canvas.text((20, 960), CompanyAddress1, font=NormalFont, fill=(TextColor))
if CompanyAddress2 != "": # if address2 available
Canvas.text((20, 1000), CompanyAddress2, font=NormalFont, fill=(TextColor))
Canvas.text(
(20, 1040),
CompanyCity + ", " + CompanyState + " " + CompanyZip,
font=NormalFont,
fill=(TextColor),
)
else:
Canvas.text(
(20, 1000),
CompanyCity + ", " + CompanyState + " " + CompanyZip,
font=NormalFont,
fill=(TextColor),
)
# Order Number (100 between)
OrderNumber = order["order_number"]
Canvas.text((20, 1100), "Order Number:", font=NormalFont, fill=(TextColor))
Canvas.text((20, 1150), str(OrderNumber), font=NormalFont, fill=(TextColor))
# Order Date (100 between)
OrderDate = order["order_date"]
# if there is a space in the date then there is a time (don't include time)
if " " in OrderDate:
OrderDate = OrderDate.split(" ")[0]
Canvas.text((20, 1250), "Order Date:", font=NormalFont, fill=(TextColor))
Canvas.text((20, 1300), OrderDate, font=NormalFont, fill=(TextColor))
# Purchase Name (100 between)
Canvas.text((20, 1400), "Buyer:", font=NormalFont, fill=(TextColor))
Canvas.text((20, 1450), order["order_name"], font=NormalFont, fill=(TextColor))
# Quantity
Quantity = 0
for item in items:
Quantity += int(item["item_quantity"])
Canvas.text((500, 650), str(Quantity) + " Items", font=NormalFont, fill=(TextColor))
# Grid Top
if IncludePrices:
Canvas.line((500, 700, 1400, 700), fill=(TextColor))
else:
Canvas.line((500, 700, 1030, 700), fill=(TextColor))
# Header
Canvas.text((520, 710), "QTY:", font=NormalFont, fill=(TextColor))
Canvas.text((650, 710), "Item:", font=NormalFont, fill=(TextColor))
if IncludePrices:
Canvas.text((1050, 710), "Price:", font=NormalFont, fill=(TextColor))
Canvas.text((1200, 710), "Sub-Total:", font=NormalFont, fill=(TextColor))
Canvas.line((500, 760, 1400, 760), fill=(TextColor))
else:
Canvas.line((500, 760, 1030, 760), fill=(TextColor))
# Side Bars
SeparateItems = len(order["order_items_UID"])
# 50 is the height of each item + 1 extra bar for titles
length = 710 + ((SeparateItems + 1) * 50)
Canvas.line((500, 700, 500, length), fill=(TextColor))
Canvas.line((630, 700, 630, length), fill=(TextColor))
Canvas.line((1030, 700, 1030, length), fill=(TextColor))
if IncludePrices:
Canvas.line((1180, 700, 1180, length), fill=(TextColor))
Canvas.line((1400, 700, 1400, length), fill=(TextColor))
# Item Fill
for i in range(len(items)):
Canvas.text(
(520, 720 + (i + 1) * 50),
str(items[i]["item_quantity"]),
font=NormalFont,
fill=(TextColor),
)
if len(items[i]["item_name"]) > 20:
NameFont = SmallFont
else:
NameFont = NormalFont
Canvas.text(
(650, 720 + (i + 1) * 50),
str(items[i]["item_name"]),
font=NameFont,
fill=(TextColor),
)
if IncludePrices:
Canvas.text(
(1050, 720 + (i + 1) * 50),
"$" + str(items[i]["item_unit_price"]),
font=NormalFont,
fill=(TextColor),
)
Subtotal = Common.Decimal(items[i]["item_quantity"])
Subtotal.multiply(items[i]["item_unit_price"])
Canvas.text(
(1200, 720 + (i + 1) * 50),
"$" + str(Subtotal),
font=NormalFont,
fill=(TextColor),
)
Canvas.line(
(500, 760 + (i + 1) * 50, 1400, 760 + (i + 1) * 50),
fill=(TextColor),
)
else:
Canvas.line(
(500, 760 + (i + 1) * 50, 1030, 760 + (i + 1) * 50),
fill=(TextColor),
)
# Total
if IncludePrices:
Total = Common.Decimal(0)
for item in items:
Subtotal = Common.Decimal(item["item_quantity"])
Subtotal.multiply(item["item_unit_price"])
Total.add(Subtotal)
Canvas.text((520, 1600), "Total: $" + str(Total), font=NormalFont, fill=(TextColor))
# Add Notes
if IncludeOrderNotes:
notes = ""
try:
notes = order["order_notes"]
except KeyError:
pass # no notes to add
if notes != "":
# wrap notes to fit within the packing slip
wrappedNotes = TextWrap(notes, 60)
Canvas.text((520, 1700), "Order Notes:", font=NormalFont, fill=(TextColor))
Canvas.text((520, 1750), wrappedNotes, font=NormalFont, fill=(TextColor))
# Save Image
im.save(os.path.join(ImagesFolderPath, str(order["order_number"]) + ".png"), "PNG")
path = os.path.join(ImagesFolderPath, str(order["order_number"]) + ".png")
# open the file in the default browser
if platform.system() == "Darwin": # macOS
subprocess.call(("open", path))
elif platform.system() == "Windows": # Windows
os.startfile(path)
else: # linux variants
subprocess.call(("xdg-open", path))
except:
app.warn("Packing Slip Error", traceback.format_exc())