-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1460 lines (1236 loc) · 49.3 KB
/
Copy pathapp.py
File metadata and controls
1460 lines (1236 loc) · 49.3 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import hmac
import os
from datetime import datetime
import pandas as pd
import streamlit as st
from core.email_sender import EmailAttachment, SmtpConfig, send_email_with_attachment
from core.file_io import (
build_output_filenames,
dataframe_to_download_bytes,
get_file_type,
load_input_file,
to_excel_autofit,
to_excel_workbook_autofit,
)
from core.tracking import add_tracking_column_from_labels, extract_label_pages
from core.transform import (
DEFAULT_PRODUCT_NAME_SHORTENING_RULES_TEXT,
DEFAULT_PRICING_AID_RATES,
PRODUCT_NAME_WARNING_LIMIT,
apply_product_name_rules_to_df,
build_excel_breakdown,
build_management_breakdown_sheets,
build_order_item_breakdown,
build_pricing_aid_details,
get_product_name_length_issues,
parse_shortening_rules,
transform_orders,
)
from utils.metrics_logger import HEADERS, get_metrics_worksheet, get_session_id, log_event
st.set_page_config(page_title="Formatter", layout="centered")
APP_NAME = "Formatter"
APP_VERSION = "1.2.0"
# ---------- Local admin-only metrics ----------
def is_local_environment() -> bool:
return os.getenv("STREAMLIT_RUNTIME_ENV") != "cloud"
def load_metrics_df() -> pd.DataFrame:
try:
ws = get_metrics_worksheet()
# Read only the configured metrics columns. Extra blank columns in the
# Google Sheet can make get_all_records() fail due to duplicate blank headers.
values = ws.get(f"A1:W{ws.row_count}")
if len(values) <= 1:
return pd.DataFrame(columns=HEADERS)
rows = values[1:]
normalized_rows = []
for row in rows:
padded = row[: len(HEADERS)] + [""] * max(0, len(HEADERS) - len(row))
if any(str(cell).strip() for cell in padded):
normalized_rows.append(padded)
if not normalized_rows:
return pd.DataFrame(columns=HEADERS)
df = pd.DataFrame(normalized_rows, columns=HEADERS)
numeric_cols = [
"input_rows",
"total_orders",
"total_products",
"lbt_count",
"parcel_count",
"track24_count",
"trackparcel_count",
"tracking_labels_found",
]
for col in numeric_cols:
if col in df.columns:
df[col] = pd.to_numeric(df[col], errors="coerce")
if "success" in df.columns:
df["success"] = df["success"].astype(str)
text_cols = [
"timestamp_utc",
"session_id",
"event_name",
"app_name",
"app_version",
"file_name",
"file_type",
"workflow",
"selected_lot",
"email_tracking_recipient",
"email_labels_recipient",
"email_sent_items",
"skip_pages_without_tracking",
"error_message",
]
for col in text_cols:
if col in df.columns:
df[col] = df[col].fillna("").astype(str)
return df
except Exception:
return pd.DataFrame()
def render_admin_metrics() -> None:
if not is_local_environment():
return
with st.expander("📊 Admin Metrics (Local Only)", expanded=False):
df = load_metrics_df()
if df.empty:
st.info("No metrics logged yet.")
return
process_df = df[df["event_name"] == "process_success"].copy()
total_runs = int(len(process_df))
total_orders = int(pd.to_numeric(process_df["total_orders"], errors="coerce").fillna(0).sum())
total_products = int(pd.to_numeric(process_df["total_products"], errors="coerce").fillna(0).sum())
total_downloads = int(df["event_name"].astype(str).str.startswith("download_").sum())
unique_sessions = int(df["session_id"].astype(str).nunique())
c1, c2, c3, c4, c5 = st.columns(5)
c1.metric("Runs", total_runs)
c2.metric("Orders", total_orders)
c3.metric("Products", total_products)
c4.metric("Downloads", total_downloads)
c5.metric("Sessions", unique_sessions)
if not process_df.empty:
chart_df = process_df.copy()
chart_df["timestamp_utc"] = pd.to_datetime(chart_df["timestamp_utc"], errors="coerce")
chart_df["total_orders"] = pd.to_numeric(chart_df["total_orders"], errors="coerce").fillna(0)
chart_df = chart_df.dropna(subset=["timestamp_utc"]).sort_values("timestamp_utc")
if not chart_df.empty:
st.subheader("Orders Processed Over Time")
st.line_chart(chart_df.set_index("timestamp_utc")["total_orders"])
st.subheader("Recent Events")
st.dataframe(df.tail(20), width="stretch")
def get_email_secret(key: str, default="") -> str:
try:
config = st.secrets.get("email", {})
except Exception:
config = {}
value = ""
if isinstance(config, dict):
value = config.get(key, default)
else:
value = getattr(config, key, default)
env_key = f"EMAIL_{key.upper()}"
return str(value or os.getenv(env_key, default)).strip()
def get_admin_download_password() -> str:
try:
admin_config = st.secrets.get("admin", {})
top_level_value = st.secrets.get("ADMIN_DOWNLOAD_PASSWORD", "")
except Exception:
admin_config = {}
top_level_value = ""
admin_value = ""
if isinstance(admin_config, dict):
admin_value = admin_config.get("download_password", "")
else:
admin_value = getattr(admin_config, "download_password", "")
return str(admin_value or top_level_value or os.getenv("ADMIN_DOWNLOAD_PASSWORD", "")).strip()
def render_admin_excel_download(
*,
df_in: pd.DataFrame,
download_df: pd.DataFrame,
file_name: str,
button_label: str,
key_prefix: str,
) -> bool:
password = get_admin_download_password()
unlocked_key = f"{key_prefix}_admin_excel_unlocked"
if not password:
st.warning("Admin Excel download is not configured.")
with st.expander("Admin password setup", expanded=False):
st.code(
"[admin]\n"
"download_password = \"choose-a-strong-password\"",
language="toml",
)
st.caption("Or set ADMIN_DOWNLOAD_PASSWORD in the environment.")
return False
if st.session_state.get(unlocked_key):
excel_bytes = to_excel_workbook_autofit(
build_management_breakdown_sheets(df_in, download_df)
)
download_clicked = st.download_button(
label=button_label,
data=excel_bytes,
file_name=file_name,
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
key=f"{key_prefix}_download_admin_excel",
use_container_width=True,
)
if st.button(
"Lock admin Excel",
key=f"{key_prefix}_lock_admin_excel",
use_container_width=True,
):
st.session_state[unlocked_key] = False
st.rerun()
return bool(download_clicked)
with st.container(border=True):
st.caption("Admin password required for the management billing workbook.")
entered_password = st.text_input(
"Admin password",
type="password",
key=f"{key_prefix}_admin_excel_password",
)
if st.button(
"Unlock admin Excel",
key=f"{key_prefix}_unlock_admin_excel",
use_container_width=True,
):
if hmac.compare_digest(entered_password, password):
st.session_state[unlocked_key] = True
st.rerun()
st.error("Incorrect admin password.")
return False
def get_smtp_config() -> SmtpConfig | None:
host = get_email_secret("smtp_host")
port_raw = get_email_secret("smtp_port", "587")
username = get_email_secret("username")
password = get_email_secret("password")
from_email = get_email_secret("from_email") or username
use_tls_raw = get_email_secret("use_tls", "true").lower()
if not host or not from_email:
return None
try:
port = int(port_raw)
except Exception:
port = 587
return SmtpConfig(
host=host,
port=port,
username=username,
password=password,
from_email=from_email,
use_tls=use_tls_raw not in {"false", "0", "no"},
)
def render_email_results_section(
*,
tracking_csv_bytes: bytes,
tracking_csv_name: str,
labels_pdf_bytes: bytes | None,
labels_pdf_name: str,
) -> None:
st.markdown("### Email results")
st.caption("Optional: send the tracking CSV and labels PDF as two separate emails.")
smtp_config = get_smtp_config()
if smtp_config is None:
with st.expander("Email setup required", expanded=False):
st.info(
"To enable email sending, add SMTP settings to `.streamlit/secrets.toml` "
"or set EMAIL_* environment variables locally."
)
st.code(
"[email]\n"
"smtp_host = \"smtp.example.com\"\n"
"smtp_port = 587\n"
"username = \"your_email@example.com\"\n"
"password = \"your_app_password\"\n"
"from_email = \"your_email@example.com\"\n"
"use_tls = true",
language="toml",
)
return
tracking_email_options = {
"Lot X": "info@inkstitch.co.uk",
"DPL lot": "teefusion786@gmail.com",
}
tracking_lot = st.selectbox(
"Tracking CSV recipient",
list(tracking_email_options.keys()),
key="email_tracking_csv_lot",
)
tracking_to = tracking_email_options[tracking_lot]
labels_to = "operationsinkstitch@gmail.com"
st.info(f"Tracking CSV will be sent to: {tracking_to}")
st.info(f"Labels PDF will be sent to: {labels_to}")
default_subject_stamp = datetime.now().strftime("%Y-%m-%d")
tracking_subject = st.text_input(
"Tracking CSV email subject",
value=f"Tracking CSV - {tracking_lot} - {default_subject_stamp}",
key="email_tracking_csv_subject",
)
labels_subject = st.text_input(
"Labels PDF email subject",
value=f"Royal Mail Labels PDF - {default_subject_stamp}",
key="email_labels_pdf_subject",
)
tracking_body = st.text_area(
"Tracking CSV email body",
value="Please find the tracking CSV attached.\n\nThanks",
key="email_tracking_csv_body",
)
labels_body = st.text_area(
"Labels PDF email body",
value="Please find the Royal Mail labels PDF attached.\n\nThanks",
key="email_labels_pdf_body",
)
if labels_pdf_bytes is None:
st.warning("Labels PDF is not available in memory. Upload the labels PDF again before sending emails.")
return
if st.button(
"Send separate emails",
type="primary",
key="send_fulfilment_result_emails",
use_container_width=True,
):
if not tracking_to and not labels_to:
st.error("Enter at least one recipient email.")
return
sent = []
try:
if tracking_to:
send_email_with_attachment(
smtp_config=smtp_config,
to_email=tracking_to,
subject=tracking_subject,
body=tracking_body,
attachment=EmailAttachment(
filename=tracking_csv_name,
content=tracking_csv_bytes,
mime_type="text/csv",
),
)
sent.append("tracking CSV")
if labels_to:
send_email_with_attachment(
smtp_config=smtp_config,
to_email=labels_to,
subject=labels_subject,
body=labels_body,
attachment=EmailAttachment(
filename=labels_pdf_name or "labels.pdf",
content=labels_pdf_bytes,
mime_type="application/pdf",
),
)
sent.append("labels PDF")
except Exception as e:
log_event(
"fulfilment_email_failed",
workflow="full_fulfilment",
selected_lot=tracking_lot,
email_tracking_recipient=tracking_to,
email_labels_recipient=labels_to,
success=False,
error_message=str(e),
app_name=APP_NAME,
app_version=APP_VERSION,
)
st.error(f"Email sending failed: {e}")
return
log_event(
"fulfilment_email_sent",
workflow="full_fulfilment",
selected_lot=tracking_lot,
email_tracking_recipient=tracking_to if "tracking CSV" in sent else "",
email_labels_recipient=labels_to if "labels PDF" in sent else "",
email_sent_items=", ".join(sent),
success=True,
app_name=APP_NAME,
app_version=APP_VERSION,
)
st.success("Sent: " + ", ".join(sent))
def render_product_name_safety_section(
df_out: pd.DataFrame,
*,
key_prefix: str,
) -> pd.DataFrame:
if "Product Name" not in df_out.columns:
return df_out
st.subheader("Product Name safety check")
st.caption("Checks Product Name length. Spaces and line breaks count as characters.")
limit = st.number_input(
"Product Name warning limit",
min_value=20,
max_value=250,
value=PRODUCT_NAME_WARNING_LIMIT,
step=1,
key=f"{key_prefix}_product_name_limit",
)
issues_df = get_product_name_length_issues(df_out, int(limit))
lengths = df_out["Product Name"].apply(lambda value: len(str(value)) if pd.notna(value) else 0)
max_length = int(lengths.max()) if not lengths.empty else 0
c1, c2, c3 = st.columns(3)
c1.metric("Rows checked", len(df_out))
c2.metric("Over limit", len(issues_df))
c3.metric("Max length", max_length)
if issues_df.empty:
st.success("All Product Name values are within the current limit.")
return df_out
st.warning(f"{len(issues_df)} Product Name value(s) exceed {limit} characters.")
with st.expander("Rows over Product Name limit", expanded=False):
st.dataframe(issues_df, width="stretch")
with st.expander("Product shortening rules", expanded=False):
st.caption("One rule per line. Format: OLD => NEW")
rules_text = st.text_area(
"Rules",
value=DEFAULT_PRODUCT_NAME_SHORTENING_RULES_TEXT,
height=180,
key=f"{key_prefix}_product_name_rules",
)
rules = parse_shortening_rules(rules_text)
optimized_df = apply_product_name_rules_to_df(df_out, rules)
optimized_issues_df = get_product_name_length_issues(optimized_df, int(limit))
st.markdown("#### After applying rules")
optimized_lengths = optimized_df["Product Name"].apply(
lambda value: len(str(value)) if pd.notna(value) else 0
)
optimized_max_length = int(optimized_lengths.max()) if not optimized_lengths.empty else 0
o1, o2 = st.columns(2)
o1.metric("Rows still over limit", len(optimized_issues_df))
o2.metric("Optimized max length", optimized_max_length)
if not optimized_issues_df.empty:
st.dataframe(optimized_issues_df, width="stretch")
else:
st.success("All Product Name values fit after rules.")
use_rules = st.checkbox(
"Use these shortening rules for downloads",
value=True,
key=f"{key_prefix}_use_product_name_rules",
)
if use_rules:
return optimized_df
return df_out
def render_excel_breakdown_tab(df_in: pd.DataFrame) -> None:
shipment_df, clothing_df, other_df = build_excel_breakdown(df_in)
item_detail_df = build_order_item_breakdown(df_in)
st.subheader("Details")
order_count = int(df_in["order reference"].nunique()) if "order reference" in df_in.columns else len(df_in)
overview_df = pd.DataFrame(
[
{
"Orders": order_count,
"Items": len(item_detail_df),
"Other item types": len(other_df),
"Back add-ons": int(other_df.attrs.get("back_add_on_count", 0)),
}
]
)
st.dataframe(
overview_df,
width="stretch",
hide_index=True,
column_config={
"Orders": st.column_config.NumberColumn("Orders", format="%d"),
"Items": st.column_config.NumberColumn("Items", format="%d"),
"Other item types": st.column_config.NumberColumn("Other item types", format="%d"),
"Back add-ons": st.column_config.NumberColumn("Back add-ons", format="%d"),
},
)
breakdown_left, breakdown_right = st.columns(2)
with breakdown_left:
st.markdown("**Delivery breakdown**")
st.dataframe(
shipment_df,
width="stretch",
hide_index=True,
column_config={
"Category": st.column_config.TextColumn("Delivery type"),
"Count": st.column_config.NumberColumn("Orders", format="%d"),
},
)
with breakdown_right:
st.markdown("**Product breakdown**")
st.dataframe(
clothing_df,
width="stretch",
hide_index=True,
column_config={
"Category": st.column_config.TextColumn("Product type"),
"Count": st.column_config.NumberColumn("Items", format="%d"),
},
)
st.subheader("Pricing aider")
price_cols = st.columns(3)
with price_cols[0]:
adult_shirt_standard = st.number_input(
"Adult Shirt up to 4XL",
min_value=0.0,
value=float(DEFAULT_PRICING_AID_RATES["adult_shirt_standard"]),
step=0.1,
format="%.2f",
key="pricing_aid_adult_shirt_standard",
)
adult_shirt_premium = st.number_input(
"Adult Shirt 5XL/6XL",
min_value=0.0,
value=float(DEFAULT_PRICING_AID_RATES["adult_shirt_premium"]),
step=0.1,
format="%.2f",
key="pricing_aid_adult_shirt_premium",
)
kids_shirt = st.number_input(
"Kids Shirt",
min_value=0.0,
value=float(DEFAULT_PRICING_AID_RATES["kids_shirt"]),
step=0.1,
format="%.2f",
key="pricing_aid_kids_shirt",
)
back_add_on = st.number_input(
"Back add-on",
min_value=0.0,
value=float(DEFAULT_PRICING_AID_RATES["back_add_on"]),
step=0.1,
format="%.2f",
key="pricing_aid_back_add_on",
)
with price_cols[1]:
adult_jumper = st.number_input(
"Adult Jumper/Sweatshirt",
min_value=0.0,
value=float(DEFAULT_PRICING_AID_RATES["adult_jumper"]),
step=0.1,
format="%.2f",
key="pricing_aid_adult_jumper",
)
kids_jumper = st.number_input(
"Kids Jumper/Sweatshirt",
min_value=0.0,
value=float(DEFAULT_PRICING_AID_RATES["kids_jumper"]),
step=0.1,
format="%.2f",
key="pricing_aid_kids_jumper",
)
adult_hoodie = st.number_input(
"Adult Hoodie",
min_value=0.0,
value=float(DEFAULT_PRICING_AID_RATES["adult_hoodie"]),
step=0.1,
format="%.2f",
key="pricing_aid_adult_hoodie",
)
kids_hoodie = st.number_input(
"Kids Hoodie",
min_value=0.0,
value=float(DEFAULT_PRICING_AID_RATES["kids_hoodie"]),
step=0.1,
format="%.2f",
key="pricing_aid_kids_hoodie",
)
with price_cols[2]:
lbt_price = st.number_input(
"LBT",
min_value=0.0,
value=float(DEFAULT_PRICING_AID_RATES["LBT"]),
step=0.1,
format="%.2f",
key="pricing_aid_lbt",
)
parcel_price = st.number_input(
"Parcel",
min_value=0.0,
value=float(DEFAULT_PRICING_AID_RATES["Parcel"]),
step=0.1,
format="%.2f",
key="pricing_aid_parcel",
)
track24_price = st.number_input(
"Track24",
min_value=0.0,
value=float(DEFAULT_PRICING_AID_RATES["Track24"]),
step=0.1,
format="%.2f",
key="pricing_aid_track24",
)
parcel24_price = st.number_input(
"Parcel24",
min_value=0.0,
value=float(DEFAULT_PRICING_AID_RATES["Parcel24"]),
step=0.1,
format="%.2f",
key="pricing_aid_parcel24",
)
other_item_prices = {}
manual_price_df = item_detail_df[
item_detail_df["Product Group"].isin(["Other items", "RL100"])
].copy()
if manual_price_df.empty:
st.success("No manual item prices needed.")
else:
st.markdown("**Manual item prices**")
other_pricing_df = (
manual_price_df.groupby(["Product Group", "Product Item"], dropna=False)
.size()
.reset_index(name="Count")
.sort_values(["Product Group", "Product Item"])
)
other_pricing_df["Unit Price"] = None
edited_other_pricing_df = st.data_editor(
other_pricing_df,
width="stretch",
hide_index=True,
key="pricing_aid_other_item_prices",
disabled=["Product Group", "Product Item", "Count"],
column_config={
"Product Group": st.column_config.TextColumn("Product group"),
"Product Item": st.column_config.TextColumn("Item"),
"Count": st.column_config.NumberColumn("Items", format="%d"),
"Unit Price": st.column_config.NumberColumn("Unit Price", min_value=0.0, step=0.1, format="£%.2f"),
},
)
for row in edited_other_pricing_df.to_dict("records"):
unit_price = row.get("Unit Price")
if pd.notna(unit_price):
other_item_prices[row["Product Item"]] = float(unit_price)
pricing_rates = {
"adult_shirt_standard": adult_shirt_standard,
"adult_shirt_premium": adult_shirt_premium,
"kids_shirt": kids_shirt,
"adult_jumper": adult_jumper,
"kids_jumper": kids_jumper,
"adult_hoodie": adult_hoodie,
"kids_hoodie": kids_hoodie,
"back_add_on": back_add_on,
"LBT": lbt_price,
"Parcel": parcel_price,
"Track24": track24_price,
"Parcel24": parcel24_price,
}
pricing_detail_df, pricing_summary_df = build_pricing_aid_details(
item_detail_df,
rates=pricing_rates,
other_item_prices=other_item_prices,
)
pricing_summary = dict(zip(pricing_summary_df["Category"], pricing_summary_df["Amount"]))
summary_cols = st.columns(4)
summary_cols[0].metric("Total", f"£{pricing_summary.get('Total', 0):,.2f}")
summary_cols[1].metric("Products", f"£{pricing_summary.get('Product subtotal', 0):,.2f}")
summary_cols[2].metric("Back add-ons", f"£{pricing_summary.get('Back add-ons', 0):,.2f}")
summary_cols[3].metric("Delivery", f"£{pricing_summary.get('Delivery', 0):,.2f}")
unpriced_other_items = int(pricing_summary.get("Unpriced manual items", 0))
if unpriced_other_items:
st.warning(f"{unpriced_other_items} manual item(s) still need a price.")
pricing_money_summary_df = pricing_summary_df[pricing_summary_df["Category"] != "Unpriced manual items"]
st.dataframe(
pricing_money_summary_df,
width="stretch",
hide_index=True,
column_config={
"Category": st.column_config.TextColumn("Category"),
"Amount": st.column_config.NumberColumn("Amount", format="£%.2f"),
},
)
st.dataframe(
pricing_detail_df,
width="stretch",
hide_index=True,
column_config={
"Line": st.column_config.NumberColumn("Line", format="%d"),
"Back Add-on": st.column_config.CheckboxColumn("Back Add-on"),
"Item Price": st.column_config.NumberColumn("Item Price", format="£%.2f"),
"Back Add-on Price": st.column_config.NumberColumn("Back Add-on Price", format="£%.2f"),
"Shipping Price": st.column_config.NumberColumn("Shipping Price", format="£%.2f"),
"Line Total": st.column_config.NumberColumn("Line Total", format="£%.2f"),
},
)
st.subheader("Other items")
if other_df.empty:
st.success("No other items found.")
else:
st.dataframe(
other_df,
width="stretch",
hide_index=True,
column_config={
"Item": st.column_config.TextColumn("Item"),
"Count": st.column_config.NumberColumn("Items", format="%d"),
},
)
# ---------- Streamlit pages ----------
def render_full_fulfilment_workflow():
st.caption(
"One-page workflow: upload orders, generate Click & Drop CSV, then return here with the labels PDF to add tracking."
)
if "fulfilment_input_name" not in st.session_state:
st.session_state["fulfilment_input_name"] = ""
if "fulfilment_input_type" not in st.session_state:
st.session_state["fulfilment_input_type"] = ""
if "fulfilment_df_in" not in st.session_state:
st.session_state["fulfilment_df_in"] = None
if "fulfilment_preview_df" not in st.session_state:
st.session_state["fulfilment_preview_df"] = None
if "fulfilment_df_out" not in st.session_state:
st.session_state["fulfilment_df_out"] = None
if "fulfilment_stats" not in st.session_state:
st.session_state["fulfilment_stats"] = None
if "fulfilment_tracking_df" not in st.session_state:
st.session_state["fulfilment_tracking_df"] = None
if "fulfilment_audit_df" not in st.session_state:
st.session_state["fulfilment_audit_df"] = None
if "fulfilment_labels_pdf_name" not in st.session_state:
st.session_state["fulfilment_labels_pdf_name"] = ""
if "fulfilment_labels_pdf_bytes" not in st.session_state:
st.session_state["fulfilment_labels_pdf_bytes"] = None
st.subheader("Step 1 — Upload orders and generate Click & Drop file")
with st.container(border=True):
st.markdown("### 📄 Orders File")
st.caption("Upload the original CSV / Excel orders file once. The app will remember it for the tracking step.")
uploaded_file = st.file_uploader(
"Drop your orders file here (.csv / .xlsx / .xls)",
type=["csv", "xlsx", "xls"],
key="fulfilment_orders_file",
label_visibility="collapsed",
)
if uploaded_file is not None:
st.success(f"Loaded: {uploaded_file.name}")
elif st.session_state["fulfilment_df_in"] is not None:
st.info(f"Using remembered file: {st.session_state['fulfilment_input_name']}")
else:
st.info("Waiting for CSV / Excel file")
if uploaded_file is not None:
file_name = uploaded_file.name
file_type = get_file_type(file_name)
should_process = (
st.session_state["fulfilment_df_in"] is None
or st.session_state["fulfilment_input_name"] != file_name
)
if should_process:
try:
df_in = load_input_file(uploaded_file)
preview_df, df_out, stats = transform_orders(df_in)
st.session_state["fulfilment_input_name"] = file_name
st.session_state["fulfilment_input_type"] = file_type
st.session_state["fulfilment_df_in"] = df_in
st.session_state["fulfilment_preview_df"] = preview_df
st.session_state["fulfilment_df_out"] = df_out
st.session_state["fulfilment_stats"] = stats
# Reset tracking result when a new orders file is uploaded.
st.session_state["fulfilment_tracking_df"] = None
st.session_state["fulfilment_audit_df"] = None
st.session_state["fulfilment_labels_pdf_name"] = ""
st.session_state["fulfilment_labels_pdf_bytes"] = None
log_event(
"fulfilment_file_processed",
workflow="full_fulfilment",
file_name=file_name,
file_type=file_type,
input_rows=len(df_in),
total_orders=stats["total_orders"],
total_products=stats["total_products"],
success=True,
app_name=APP_NAME,
app_version=APP_VERSION,
)
except Exception as e:
log_event(
"fulfilment_process_failed",
workflow="full_fulfilment",
file_name=file_name,
file_type=file_type,
success=False,
error_message=str(e),
app_name=APP_NAME,
app_version=APP_VERSION,
)
st.error(f"Invalid file format: {e}")
return
df_in = st.session_state["fulfilment_df_in"]
preview_df = st.session_state["fulfilment_preview_df"]
df_out = st.session_state["fulfilment_df_out"]
stats = st.session_state["fulfilment_stats"]
if df_in is None or preview_df is None or df_out is None or stats is None:
return
category_counts = (
preview_df["__Category"]
.value_counts()
.reindex(["LBT", "Parcel", "Track24", "TrackParcel"], fill_value=0)
)
st.subheader("Click & Drop summary")
c0, c1, c2 = st.columns(3)
c0.metric("Orders", stats["total_orders"])
c1.metric("Products", stats["total_products"])
c2.metric("LBT", int(category_counts["LBT"]))
c3, c4, c5 = st.columns(3)
c3.metric("Parcel", int(category_counts["Parcel"]))
c4.metric("Track24", int(category_counts["Track24"]))
c5.metric("TrackParcel", int(category_counts["TrackParcel"]))
download_df = render_product_name_safety_section(df_out, key_prefix="fulfilment")
csv_bytes = download_df.to_csv(index=False).encode("utf-8")
csv_name, xlsx_name = build_output_filenames()
st.markdown("### Download Click & Drop file")
col1, col2 = st.columns(2)
with col1:
st.download_button(
label="⬇️ Download Click & Drop CSV",
data=csv_bytes,
file_name=csv_name,
mime="text/csv",
key="download_fulfilment_click_drop_csv",
use_container_width=True,
)
with col2:
render_admin_excel_download(
df_in=df_in,
download_df=download_df,
file_name=xlsx_name,
button_label="⬇️ Download Excel for checking",
key_prefix="fulfilment_click_drop_xlsx",
)
with st.expander("Preview formatted rows", expanded=False):
st.dataframe(preview_df.head(20), width="stretch")
st.divider()
st.subheader("Step 2 — Upload labels PDF and add tracking")
st.caption(
"After you create Royal Mail labels manually, come back here and upload the labels PDF. "
"The original orders file is already remembered."
)
labels_pdf = st.file_uploader(
"Drop Royal Mail labels PDF here",
type=["pdf"],
key="fulfilment_labels_pdf",
)
if labels_pdf is None:
st.info("Waiting for labels PDF")
return
st.session_state["fulfilment_labels_pdf_name"] = labels_pdf.name
st.session_state["fulfilment_labels_pdf_bytes"] = labels_pdf.getvalue()
skip_pages_without_tracking = st.checkbox(
"Skip PDF pages with no tracking number",
value=False,
key="fulfilment_skip_pages_without_tracking",
help="Use this when some labels have extra pages with no tracking number. The app will count only pages that contain tracking numbers.",
)
try:
labels_pdf.seek(0)
labels = extract_label_pages(
labels_pdf,
skip_pages_without_tracking=skip_pages_without_tracking,
)
except Exception as e:
st.error(f"Could not read labels PDF: {e}")
return
st.markdown("### Quick Check")
m1, m2 = st.columns(2)
m1.metric("Order rows remembered", len(df_in))
m2.metric("Tracking labels found" if skip_pages_without_tracking else "Label pages", len(labels))
if len(df_in) != len(labels):
st.error(
f"Row count mismatch: remembered order file has {len(df_in)} rows but labels PDF has {len(labels)} pages"
)
return
if st.button(
"Add Tracking to remembered orders",
type="primary",
key="run_fulfilment_add_tracking",
use_container_width=True,
):
progress_placeholder = st.empty()
status_placeholder = st.empty()
progress_bar = progress_placeholder.progress(0)
status_text = status_placeholder.empty()
try:
labels_pdf.seek(0)
tracking_df, audit_df = add_tracking_column_from_labels(
df_in,
labels_pdf,
progress_bar=progress_bar,
status_text=status_text,
skip_pages_without_tracking=skip_pages_without_tracking,
)