Monolista: svuotamento corsie per stato ERP e ricerca UDC nello storico biennale

This commit is contained in:
2026-10-22 11:47:03 +02:00
parent 85b11834b6
commit e4b227e82c
11 changed files with 980 additions and 36 deletions

View File

@@ -20,6 +20,7 @@ from busy_overlay import InlineBusyOverlay
from gestione_aree import AsyncRunner
from gestione_scarico import move_pallet_async
from locale_text import load_locale_catalog, text as loc_text
from pickinglist_history_search import SQL_UDC_HISTORY, detail_request
from ui_theme import theme_color, theme_font, theme_section, theme_value
from ui_tables import merge_tags, style_treeview, zebra_tag
from version_info import module_version, versioned_title
@@ -175,6 +176,11 @@ class StoricoPickingListWindow(ctk.CTkToplevel):
self._async = AsyncRunner(self)
self._busy = InlineBusyOverlay(self, self._theme)
self.var_documento = tk.StringVar()
self.var_udc = tk.StringVar()
self._master_rows: dict[str, dict[str, Any]] = {}
self._selected_doc_id: int | None = None
self._master_request = 0
self._detail_request = 0
self._selected_documento: str | None = None
self._selected_stato_operativo: str = ""
self._detail_rows: list[dict[str, Any]] = []
@@ -201,23 +207,28 @@ class StoricoPickingListWindow(ctk.CTkToplevel):
fg_color=theme_color(self._theme, "toolbar_frame_fg_color", ("#d7d7d7", "#3b3b3b")),
)
top.grid(row=0, column=0, sticky="ew", padx=8, pady=8)
top.grid_columnconfigure(4, weight=1)
top.grid_columnconfigure(6, weight=1)
label_font = theme_font(self._theme, "toolbar_label_font", ("Segoe UI", 10))
entry_font = theme_font(self._theme, "entry_font", ("Segoe UI", 10))
button_font = theme_font(self._theme, "toolbar_button_font", ("Segoe UI", 10, "bold"))
ctk.CTkLabel(top, text="Documento:", font=label_font).grid(row=0, column=0, sticky="w")
ctk.CTkEntry(top, textvariable=self.var_documento, width=140, font=entry_font).grid(
row=0, column=1, sticky="w", padx=(4, 12)
)
ctk.CTkLabel(top, text=loc_text("history.picking.document", catalog=self._locale_catalog),
font=label_font).grid(row=0, column=0, sticky="w")
self.entry_documento = ctk.CTkEntry(top, textvariable=self.var_documento, width=120, font=entry_font)
self.entry_documento.grid(row=0, column=1, sticky="w", padx=(4, 12))
self.entry_documento.bind("<Return>", lambda _e: self._load_master())
ctk.CTkLabel(top, text="UDC:", font=label_font).grid(row=0, column=2, sticky="w")
self.entry_udc = ctk.CTkEntry(top, textvariable=self.var_udc, width=110, font=entry_font)
self.entry_udc.grid(row=0, column=3, sticky="w", padx=(4, 12))
self.entry_udc.bind("<Return>", lambda _e: self._load_master())
ctk.CTkButton(
top,
text=loc_text("history.picking.button.reload", catalog=self._locale_catalog, default="Ricarica"),
command=self._load_master,
font=button_font,
).grid(
row=0, column=2, sticky="w"
row=0, column=4, sticky="w"
)
self.btn_ship_residuals = ctk.CTkButton(
top,
@@ -230,11 +241,13 @@ class StoricoPickingListWindow(ctk.CTkToplevel):
state="disabled",
font=button_font,
)
self.btn_ship_residuals.grid(row=0, column=3, sticky="w", padx=(12, 0))
self.btn_ship_residuals.grid(row=0, column=5, sticky="w", padx=(12, 0))
ctk.CTkLabel(top, text=loc_text("history.picking.udc_hint", catalog=self._locale_catalog),
font=label_font, anchor="w").grid(row=1, column=0, columnspan=7, sticky="ew", pady=(4, 0))
self.master_tree = self._make_tree(
row=1,
columns=("Documento", "Data", "StatoDoc", "NAZIONE", "TotUDC", "Residue", "Spedite", "Stato", "IDStato"),
columns=("Documento", "Data", "StatoDoc", "NAZIONE", "TotUDC", "Residue", "Spedite", "Stato", "IDStato", "Anno"),
widths={
"Documento": 100,
"Data": 110,
@@ -245,9 +258,13 @@ class StoricoPickingListWindow(ctk.CTkToplevel):
"Spedite": 90,
"Stato": 120,
"IDStato": 80,
"Anno": 70,
},
)
self.master_tree.bind("<<TreeviewSelect>>", self._on_master_select)
self.master_tree.configure(displaycolumns=("Documento", "Anno", "Data", "StatoDoc", "NAZIONE",
"TotUDC", "Residue", "Spedite", "Stato", "IDStato"))
self.master_tree.heading("Anno", text=loc_text("history.picking.year", catalog=self._locale_catalog))
ctk.CTkLabel(
self,
@@ -297,17 +314,35 @@ class StoricoPickingListWindow(ctk.CTkToplevel):
def _load_master(self) -> None:
params = {"documento": str(self.var_documento.get() or "").strip() or None}
udc = self.var_udc.get().strip()
if udc and (not udc.isascii() or not udc.isdigit() or len(udc) > 6):
messagebox.showwarning(loc_text("history.picking.msg.title", catalog=self._locale_catalog),
loc_text("history.picking.udc_invalid", catalog=self._locale_catalog), parent=self)
return
sql = SQL_STORICO_PL
if udc:
sql = SQL_UDC_HISTORY
params.update(pallet=udc, id_documento=None)
previous_documento = self._selected_documento
previous_id = self._selected_doc_id
self._master_request += 1
request = self._master_request
self._detail_request += 1
self._fill_master([])
async def _job():
return await self.db_client.query_json(SQL_STORICO_PL, params)
return await self.db_client.query_json(sql, params)
def _ok(res):
if request != self._master_request:
return
self._fill_master(_rows_to_dicts(res))
if previous_documento:
self._restore_master_selection(previous_documento)
self._restore_master_selection(previous_documento, previous_id)
def _err(ex):
if request != self._master_request:
return
messagebox.showerror(
loc_text("history.picking.msg.title", catalog=self._locale_catalog, default="Storico Picking List"),
loc_text(
@@ -334,10 +369,14 @@ class StoricoPickingListWindow(ctk.CTkToplevel):
self.master_tree.delete(*self.master_tree.get_children(""))
self.detail_tree.delete(*self.detail_tree.get_children(""))
self._selected_documento = None
self._selected_doc_id = None
self._master_rows = {}
self._selected_stato_operativo = ""
self._detail_rows = []
self._update_residual_button()
for index, row in enumerate(rows):
iid = f"doc_{row.get('Documento')}_{index}"
self._master_rows[iid] = row
stato = str(row.get("StatoOperativo") or "")
is_open_with_shipped = (
str(row.get("StatoDocumento") or "") == "P"
@@ -355,7 +394,7 @@ class StoricoPickingListWindow(ctk.CTkToplevel):
self.master_tree.insert(
"",
"end",
iid=f"doc_{row.get('Documento')}_{index}",
iid=iid,
values=(
row.get("Documento", ""),
_format_date(row.get("DataDocumento", "")),
@@ -366,14 +405,18 @@ class StoricoPickingListWindow(ctk.CTkToplevel):
row.get("RigheSpedite", ""),
stato,
row.get("IDStato", ""),
row.get("AnnoDocumento", datetime.now().year),
),
tags=merge_tags(zebra_tag(index), tag),
)
def _on_master_select(self, _event=None) -> None:
self._detail_request += 1
request = self._detail_request
selected = self.master_tree.selection()
if not selected:
self._selected_documento = None
self._selected_doc_id = None
self._selected_stato_operativo = ""
self._detail_rows = []
self._update_residual_button()
@@ -382,19 +425,26 @@ class StoricoPickingListWindow(ctk.CTkToplevel):
if not values:
return
documento = values[0]
self._selected_doc_id = self._master_rows.get(selected[0], {}).get("IDDocumento")
self._selected_documento = str(documento)
self._selected_stato_operativo = str(values[7] if len(values) > 7 else "")
self._detail_rows = []
self.detail_tree.delete(*self.detail_tree.get_children(""))
self._update_residual_button()
sql, params = detail_request(documento, self._selected_doc_id, SQL_STORICO_PL_DETAILS)
async def _job():
return await self.db_client.query_json(SQL_STORICO_PL_DETAILS, {"documento": documento})
return await self.db_client.query_json(sql, params)
def _ok(res):
if request != self._detail_request:
return
self._fill_detail(_rows_to_dicts(res))
self._update_residual_button()
def _err(ex):
if request != self._detail_request:
return
messagebox.showerror(
loc_text("history.picking.msg.title", catalog=self._locale_catalog, default="Storico Picking List"),
loc_text(
@@ -451,12 +501,15 @@ class StoricoPickingListWindow(ctk.CTkToplevel):
except Exception:
pass
def _restore_master_selection(self, documento: str) -> None:
def _restore_master_selection(self, documento: str, document_id: int | None = None) -> None:
"""Re-select a document after a reload, when it is still visible."""
for iid in self.master_tree.get_children(""):
values = self.master_tree.item(iid, "values")
if values and str(values[0]) == str(documento):
row = self._master_rows.get(iid, {})
matches = (row.get("IDDocumento") == document_id if document_id is not None
else row.get("IDDocumento") is None and values and str(values[0]) == str(documento))
if matches:
self.master_tree.selection_set(iid)
self.master_tree.focus(iid)
self.master_tree.see(iid)
@@ -512,14 +565,18 @@ class StoricoPickingListWindow(ctk.CTkToplevel):
return
documento = self._selected_documento
document_id = self._selected_doc_id
utente = self._operator_login()
sql, params = detail_request(documento, document_id, SQL_STORICO_PL_DETAILS)
async def _job():
# Re-read the detail inside the async job to avoid moving stale UI
# rows if another operator or barcode client changed the list after
# the user selected it.
detail_res = await self.db_client.query_json(SQL_STORICO_PL_DETAILS, {"documento": documento})
detail_res = await self.db_client.query_json(sql, params)
detail_rows = _rows_to_dicts(detail_res)
if document_id is not None and any(row.get("StatoDocumento") != "D" for row in detail_rows):
raise RuntimeError("Documento non piu' definitivo. Ricaricare lo storico.")
pallets = self._residual_pallets_from_rows(detail_rows)
results: list[dict[str, Any]] = []
for pallet in pallets:
@@ -546,6 +603,7 @@ class StoricoPickingListWindow(ctk.CTkToplevel):
parent=self,
)
self._selected_documento = str(documento)
self._selected_doc_id = document_id
self._load_master()
def _err(ex):