-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
290 lines (231 loc) · 7.57 KB
/
main.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
#!/usr/bin/env python3
# Author: Daniel Rode
# Dependencies:
# Python 3.10+
# fpdf2 (via pip)
# pyyaml (via pip)
# qrcode (via pip)
# typst (via Winget)
# Init: 15 Aug 2023
# Updated: 30 Apr 2024
import os
import sys
import pathlib as pl
import subprocess as sp
from itertools import cycle
import re
import csv
import yaml
import qrcode
from fpdf import FPDF
# Constants
labels_A4_export_path = './export_labels_A4.pdf'
labels_4x3_export_path = './export_labels_4x3.pdf'
bol_export_path = './export_bol.pdf'
return_address = """\
Boulder Valley Meals
3645 Wazee St
Denver, CO 80216"""
meal_code_map = {
'Chorizo, Egg, Potato and Rice Burrito Bowl': 'BV-BG',
'Potato, Chorizo and Egg Burrito Bowl': 'BV-BR',
'Potato, Egg, Sausage, Cheese and Bacon Burrito Bowl': 'BV-BY',
'Potato, Sausage and Egg Burrito Bowl': 'BV-BO',
'Texas-Inspired Bacon Wrapped Chicken Breasts': 'BV-GT',
'Three Cheese Bacon Wrapped Chicken Breasts': 'BV-GC',
}
# Functions
def ceiling_div(numer, denom):
return -(numer // -denom)
def get_next_label(label_info):
global boxes
midX = 14.4
midY = 11.75
iter_pos = cycle([
(margin, margin),
(midX, margin),
(margin, midY),
(midX, midY),
])
for order_id in label_info:
i = label_info[order_id]
item_count, name, street, city_state_zip, orderID = i
# Calculate the number of boxes/labels needed for this order
labels_needed_count = ceiling_div(item_count, 6)
for _ in range(labels_needed_count):
x, y = next(iter_pos)
recipient = "\n".join([name, street, city_state_zip])
destinations.add(', '.join([name, street, city_state_zip]))
boxes += 1
yield recipient, orderID, x, y
def get_multi_cell_width(str):
return max([ pdf.get_string_width(i) for i in str.split('\n') ])
def render_A4_label(recipient, orderID, x, y):
# x,y is the positional anchor point for the label
l = 11.8 # Label length cm (this is the content right bound)
d = 8.5 # Label depth cm (this is the content lower bound)
# Render logo
pdf.set_xy(x,y)
pdf.image('./bvm_logo.png', w=5)
# Render return address
pdf.set_font(size=14)
w = get_multi_cell_width(return_address)
pdf.set_xy(x+l-w,y)
pdf.multi_cell(text=return_address, w=99)
# Render recipient address
pdf.set_xy(x,y+6)
pdf.set_font(size=16)
pdf.multi_cell(text=recipient, w=99)
# Render QR code and order number
w = 2
qr = qrcode.make(orderID, border = 0)
pdf.set_xy(x+l-w-0.1,y+d-w-1)
pdf.image(qr.get_image(), w=2)
pdf.set_font(size=16)
pdf.set_xy(x+l-w-0.1,pdf.get_y()+0.1)
pdf.cell(w=2, text=f'#{orderID}', align='C')
def gen_A4_labels_pdf():
# Compile/render labels and save to PDF
print("Generating A4 labels PDF...")
global pdf
global margin
pdf = FPDF(orientation='landscape', format='letter', unit='cm')
margin = 1.6
pdf.set_margin(margin)
pdf.set_font('Times', style='', size=16)
# pdf.add_font(
# 'DejaVuSerif',
# fname='/usr/share/fonts/TTF/DejaVuSerifCondensed.ttf'
# )
# pdf.set_font('DejaVuSerif', style='', size=16)
for recipient, orderID, x, y in get_next_label(label_info):
if (x, y) == (margin, margin):
pdf.add_page()
render_A4_label(recipient, orderID, x, y)
pdf.output(labels_A4_export_path)
def render_4x3_label(recipient, orderID, x = 0.3, y = 0.3):
# x,y is the positional anchor point for the label
l = 9.2 # Label length cm (this is the content right bound)
d = 7 # Label depth cm (this is the content lower bound)
# Render logo
pdf.set_xy(x,y)
pdf.image('./bvm_logo.png', w=4)
# Render return address
pdf.set_font(size=14)
w = get_multi_cell_width(return_address)
pdf.set_xy(x+l-w,y)
pdf.multi_cell(text=return_address, w=99)
# Render recipient address
pdf.set_xy(x,y+5)
pdf.set_font(size=12)
pdf.multi_cell(text=recipient, w=99)
# Render QR code and order number
w = 2
qr = qrcode.make(orderID, border = 0)
pdf.set_xy(x+l-w-0.1,y+d-w-1)
pdf.image(qr.get_image(), w=2)
pdf.set_font(size=12)
pdf.set_xy(x+l-w-0.1,pdf.get_y()+0.1)
pdf.cell(w=2, text=f'#{orderID}', align='C')
def gen_4x3_labels_pdf():
# Compile/render labels and save to PDF
print("Generating 4x3 labels PDF...")
global pdf
global margin
pdf = FPDF(orientation='portrait', format=(10.16,7.62), unit='cm')
margin = 0.5
pdf.set_margin(margin)
pdf.set_font('Times', style='', size=12)
for recipient, orderID, x, y in get_next_label(label_info):
pdf.add_page()
render_4x3_label(recipient, orderID)
pdf.output(labels_4x3_export_path)
def gen_bol_pdf():
# Sum quantity of specific items per order
cargo = dict()
for o in orders:
meal_name = o['Lineitem name']
order_id = o['Name'].strip('#')
quantity = int(o['Lineitem quantity'])
if (meal_name, order_id) not in cargo:
cargo[(meal_name, order_id)] = quantity
else:
cargo[(meal_name, order_id)] += quantity
# Format cargo list for BOL
cargo_list = list()
for meal_name, order_id in cargo:
quantity = cargo[(meal_name, order_id)]
# Order ID, Item Code, Item Description, Quantity
try:
meal_code = meal_code_map[meal_name]
except KeyError:
continue
cargo_list.append([
order_id,
meal_code,
"Frozen meal: " + meal_name.replace(',', '')[:32],
str(quantity),
])
# Save BOL info to YAML file for Typst to read and render
with open('./tmp-bol.yaml', 'w') as f:
data = {
'boxes': boxes,
'cargo': cargo_list,
'order_ids': ', '.join(set([i[0] for i in cargo_list])),
'destinations': list(destinations),
}
yaml.dump(data, f)
# Render BOL PDF with Typst
sp.run(['typst', 'compile', './bol-template.typ', bol_export_path])
def get_newest_file(pth, glob):
paths = list(pth.glob(glob))
if len(paths) == 0:
return None
newest = paths[0]
for p in paths:
newest.stat() < p.stat()
newest = p
return newest
# Main
# Init vars
boxes = 0
destinations = set()
# Parse command line arguments
try:
orders_csv_path = pl.Path(sys.argv[1])
except IndexError:
orders_csv_path = get_newest_file(
pl.Path.home() / "downloads", "orders_export*.csv"
)
if (orders_csv_path is None) or (not orders_csv_path.is_file()):
print("Usage: this-script ORDERS_CSV_PATH")
exit(1)
if not orders_csv_path.is_file():
print("error: File does not exist:", orders_csv_path)
exit(1)
# Import order information from orders CSV
with open(orders_csv_path, newline='') as csvfile:
reader = csv.DictReader(csvfile)
orders = list(reader)
label_info = dict()
for i in orders:
order_id = i['Name'].strip('#')
if order_id in label_info:
label_info[order_id][0] += int(i['Lineitem quantity'])
continue
city_state_zip = f"{i['Shipping City']}, "
city_state_zip += f"{i['Shipping Province']} {i['Shipping Zip']}"
label_info[order_id] = [
int(i['Lineitem quantity']), # Num of items in order
i['Shipping Name'],
i['Shipping Street'],
city_state_zip,
order_id,
]
# CD to dir where this script resides
os.chdir(os.path.dirname(os.path.realpath(__file__)))
# Generate labels PDF
gen_A4_labels_pdf()
gen_4x3_labels_pdf()
# Generate BOL PDF
gen_bol_pdf()