From 781d23fe83f8bd06486ceb06067c7b257aa28375 Mon Sep 17 00:00:00 2001 From: Guillaume Pratte Date: Sat, 24 Feb 2024 19:51:48 -0500 Subject: [PATCH] Add food preparation table in kitchen count (#93) --- souschef/delivery/views.py | 65 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/souschef/delivery/views.py b/souschef/delivery/views.py index 5f2f211d..f8b3083d 100755 --- a/souschef/delivery/views.py +++ b/souschef/delivery/views.py @@ -1112,12 +1112,14 @@ def get(self, request, *args, **kwargs): kitchen_list[client_id] = kitchen_item component_lines, meal_lines = kcr_make_lines(kitchen_list, delivery_date) + preparation_lines = kcr_get_preparation_lines(kitchen_list) if component_lines: # we have orders on that date num_pages = kcr_make_pages( # kitchen count as PDF delivery_date, component_lines, meal_lines, # summary + preparation_lines, ) # detail num_labels = kcr_make_labels( # meal labels as PDF delivery_date, @@ -1190,7 +1192,7 @@ def meal_line(kititm): A MealLine object """ return MealLine( - client=kititm.lastname + ", " + kititm.firstname[0:2] + ".", + client=format_client_name(kititm.firstname, kititm.lastname), rqty=(str(kititm.meal_qty) if kititm.meal_size == SIZE_CHOICES_REGULAR else ""), lqty=(str(kititm.meal_qty) if kititm.meal_size == SIZE_CHOICES_LARGE else ""), ingr_clash="", @@ -1371,7 +1373,30 @@ def kcr_make_lines(kitchen_list, kcr_date): return (component_lines_sorted, meal_lines) -def kcr_make_pages(kcr_date, component_lines, meal_lines): +def format_client_name(firstname, lastname): + return f"{lastname}, {firstname[:2]}." + + +def kcr_get_preparation_lines(kitchen_list): + items_per_preparation = collections.defaultdict(list) + # Group KitchenItem per preparation method + for item in kitchen_list.values(): + for prep in item.preparation: + items_per_preparation[prep].append(item) + # Return [(preparation_method, [formatted_client_name, ...]), ...] + return [ + ( + prep, + sorted( + format_client_name(item.firstname, item.lastname) + for item in items_per_preparation[prep] + ), + ) + for prep in sorted(items_per_preparation.keys()) + ] + + +def kcr_make_pages(kcr_date, component_lines, meal_lines, preperation_lines): """Generate the kitchen count report pages as a PDF file. Uses ReportLab see http://www.reportlab.com/documentation/faq/ @@ -1442,6 +1467,39 @@ def myLaterPages(canvas, doc): drawHeader(canvas, doc) canvas.restoreState() + def get_food_preperation_table(): + rows = [] + line = 0 + tab_style = RLTableStyle([("VALIGN", (0, 0), (-1, -1), "TOP")]) + rows.append( + [ + RLParagraph("Food Preparation", styles["NormalLeft"]), + RLParagraph("Quantity", styles["NormalRight"]), + "", + RLParagraph("Clients", styles["NormalLeft"]), + ] + ) + tab_style.add("LINEABOVE", (0, line), (-1, line), 1, rl_colors.black) + tab_style.add("LINEBELOW", (0, line), (-1, line), 1, rl_colors.black) + tab_style.add("LINEBEFORE", (0, line), (0, line), 1, rl_colors.black) + tab_style.add("LINEAFTER", (-1, line), (-1, line), 1, rl_colors.black) + line += 1 + + for prepline in preperation_lines: + rows.append( + [ + RLParagraph(prepline[0], styles["LargeBoldLeft"]), + RLParagraph(str(len(prepline[1])), styles["NormalRightBold"]), + "", + RLParagraph( + ";   ".join(prepline[1]), styles["NormalLeft"] + ), + ] + ) + tab = RLTable(rows, colWidths=(150, 50, 10, 310), repeatRows=1) + tab.setStyle(tab_style) + return tab + def go(): """Generate the pages. @@ -1590,6 +1648,9 @@ def go(): story.append(RLSpacer(1, 1 * rl_inch)) # end Detail section + story.append(get_food_preperation_table()) + story.append(RLSpacer(1, 1 * rl_inch)) + # build full document doc.build(story, onFirstPage=myFirstPage, onLaterPages=myLaterPages) return doc.page