Implementa picking bilista 2.1.0 con test, patch SQL e rollback
This commit is contained in:
@@ -332,8 +332,11 @@ DET_COLS: List[ColSpec] = [
|
||||
]
|
||||
|
||||
ROW_H = 28
|
||||
RESERVED_IDSTATO_BG = "#dc2626"
|
||||
RESERVED_IDSTATO_FG = "#ffffff"
|
||||
from pickinglist_pair import PAIR_SLOT_COLORS
|
||||
from pickinglist_pair_repository import PickingPairRepository
|
||||
|
||||
RESERVED_IDSTATO_BG = PAIR_SLOT_COLORS[1]
|
||||
RESERVED_IDSTATO_FG = "#111827"
|
||||
|
||||
|
||||
# -------------------- Micro spinner (toolbar) --------------------
|
||||
@@ -651,6 +654,8 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
pass
|
||||
|
||||
self.rows_models: list[PLRow] = []
|
||||
self._selection_order: list[str] = []
|
||||
self._reserved_pair_documents: set[str] = set()
|
||||
self._detail_cache: Dict[Any, list] = {}
|
||||
self.detail_doc = None
|
||||
self.days_var = tk.StringVar(value="10")
|
||||
@@ -707,12 +712,15 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
(loc_text("picking.button.sequence", catalog=self._locale_catalog, default="Sequenza di prelievo"), self.on_sequence),
|
||||
(loc_text("picking.button.export", catalog=self._locale_catalog, default="Esporta XLSX"), self.on_export)
|
||||
]):
|
||||
ctk.CTkButton(
|
||||
button = ctk.CTkButton(
|
||||
top,
|
||||
text=text,
|
||||
command=cmd,
|
||||
font=theme_font(self._theme, "toolbar_button_font", ("Segoe UI", 10, "bold")),
|
||||
).grid(row=0, column=i, padx=6)
|
||||
)
|
||||
button.grid(row=0, column=i, padx=6)
|
||||
if cmd == self.on_sequence:
|
||||
self.sequence_button = button
|
||||
|
||||
ctk.CTkLabel(
|
||||
top,
|
||||
@@ -728,6 +736,10 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
font=theme_font(self._theme, "toolbar_button_font", ("Segoe UI", 10, "bold")),
|
||||
).grid(row=0, column=6, padx=(0, 6), sticky="w")
|
||||
|
||||
self.selection_hint = ctk.CTkLabel(top, text="", anchor="w")
|
||||
self.selection_hint.grid(row=1, column=0, columnspan=7, padx=6, pady=(4, 2), sticky="w")
|
||||
self._update_sequence_button()
|
||||
|
||||
# --- micro spinner a destra della toolbar ---
|
||||
self.spinner = ToolbarSpinner(top)
|
||||
self.spinner.widget().grid(row=0, column=10, padx=(8,0)) # largo spazio a destra
|
||||
@@ -913,8 +925,9 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
row_children = row_widget.winfo_children()
|
||||
if len(row_children) >= 5:
|
||||
holder = row_children[4]
|
||||
is_reserved = int(d.get("IDStato") or 0) == 1
|
||||
holder_bg = RESERVED_IDSTATO_BG if is_reserved else row_bg
|
||||
slot = int(d.get("IDStato") or 0)
|
||||
is_reserved = slot in PAIR_SLOT_COLORS
|
||||
holder_bg = PAIR_SLOT_COLORS.get(slot, row_bg)
|
||||
holder.configure(fg_color=holder_bg)
|
||||
if holder.winfo_children():
|
||||
lbl = holder.winfo_children()[0]
|
||||
@@ -951,8 +964,8 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
row_index=r,
|
||||
anchors=[c.anchor for c in PL_COLS],
|
||||
checkbox_builder=model.build_checkbox,
|
||||
cell_bg={"IDStato": RESERVED_IDSTATO_BG} if int(d.get("IDStato") or 0) == 1 else None,
|
||||
cell_fg={"IDStato": RESERVED_IDSTATO_FG} if int(d.get("IDStato") or 0) == 1 else None,
|
||||
cell_bg={"IDStato": PAIR_SLOT_COLORS[int(d["IDStato"])]} if int(d.get("IDStato") or 0) in PAIR_SLOT_COLORS else None,
|
||||
cell_fg={"IDStato": RESERVED_IDSTATO_FG} if int(d.get("IDStato") or 0) in PAIR_SLOT_COLORS else None,
|
||||
)
|
||||
|
||||
# 🎯 Colora dopo che la UI è resa → no balzi visivi
|
||||
@@ -961,11 +974,25 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
# ----- helpers -----
|
||||
def _get_selected_model(self) -> Optional[PLRow]:
|
||||
"""Return the currently checked picking list row, if any."""
|
||||
for m in self.rows_models:
|
||||
if m.is_checked():
|
||||
return m
|
||||
selected = self._selected_models()
|
||||
if selected:
|
||||
return next((m for m in selected if _s(m.pl.get("Documento")) == _s(self.detail_doc)), selected[0])
|
||||
return None
|
||||
|
||||
def _selected_models(self) -> list[PLRow]:
|
||||
by_doc = {_s(m.pl.get("Documento")): m for m in self.rows_models if m.is_checked()}
|
||||
return [by_doc[d] for d in self._selection_order if d in by_doc]
|
||||
|
||||
def _update_sequence_button(self):
|
||||
selected = self._selected_models()
|
||||
blocked = len(selected) != 1 or any(_s(m.pl.get("Documento")) in self._reserved_pair_documents for m in selected)
|
||||
self.sequence_button.configure(state="disabled" if blocked else "normal")
|
||||
docs = [_s(m.pl.get("Documento")) for m in selected]
|
||||
hint = loc_text("picking.pair.selection_hint", catalog=self._locale_catalog,
|
||||
default="Ordine di selezione: {documents}. Conferma con Prenota.")
|
||||
self.selection_hint.configure(text=hint.format(documents=" / ".join(
|
||||
f"{i}: PL {doc}" for i, doc in enumerate(docs, 1))) if docs else "")
|
||||
|
||||
def _recolor_row_by_documento(self, documento: str, idstato: int):
|
||||
"""Aggiorna colore riga e cella IDStato per il Documento indicato."""
|
||||
for idx, m in enumerate(self.rows_models):
|
||||
@@ -1031,21 +1058,24 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
@_log_call()
|
||||
def on_row_checked(self, model: PLRow, is_checked: bool):
|
||||
"""Handle row selection changes and refresh the detail section."""
|
||||
# Only one backoffice picking list can be selected at a time. Barcode
|
||||
# F1/F2 priority behavior is derived downstream; this UI controls only
|
||||
# the explicit Python reservation slot.
|
||||
document = _s(model.pl.get("Documento"))
|
||||
if is_checked:
|
||||
for m in self.rows_models:
|
||||
if m is not model and m.is_checked():
|
||||
m.set_checked(False)
|
||||
if document not in self._selection_order:
|
||||
if len(self._selection_order) >= 2:
|
||||
model.set_checked(False)
|
||||
messagebox.showinfo("Picking List", loc_text("picking.pair.max_two", catalog=self._locale_catalog,
|
||||
default="Seleziona al massimo due liste."), parent=self)
|
||||
return
|
||||
self._selection_order.append(document)
|
||||
self._update_sequence_button()
|
||||
|
||||
self.detail_doc = model.pl.get("Documento")
|
||||
self.detail_doc = document
|
||||
_MODULE_LOGGER.info(f"Documento selezionato per il dettaglio: {self.detail_doc}")
|
||||
self.spinner.start(" Carico dettagli…") # spinner ON
|
||||
days = self._selected_days()
|
||||
|
||||
async def _job():
|
||||
params = {"Documento": self.detail_doc, "days": days}
|
||||
params = {"Documento": document, "days": days}
|
||||
_log_sql("SQL_PL_DETAILS", SQL_PL_DETAILS, params)
|
||||
return await self.db_client.query_json(SQL_PL_DETAILS, params)
|
||||
|
||||
@@ -1053,7 +1083,9 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
# NON fermare lo spinner subito: lo farà _refresh_details_incremental
|
||||
rows = _expand_detail_rows_for_test(_rows_to_dicts(res))
|
||||
_log_dataset("SQL_PL_DETAILS", rows)
|
||||
self._detail_cache[self.detail_doc] = rows
|
||||
self._detail_cache[document] = rows
|
||||
if _s(self.detail_doc) != document:
|
||||
return
|
||||
# Avvia il rendering incrementale che mantiene l'overlay attivo
|
||||
self._refresh_details_incremental()
|
||||
|
||||
@@ -1074,6 +1106,12 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
self.busy.show(f"Carico UDC per Documento {self.detail_doc}…")
|
||||
|
||||
else:
|
||||
self._selection_order = [d for d in self._selection_order if d != document]
|
||||
self._update_sequence_button()
|
||||
remaining = self._selected_models()
|
||||
if remaining:
|
||||
self.on_row_checked(remaining[-1], True)
|
||||
return
|
||||
if not any(m.is_checked() for m in self.rows_models):
|
||||
self.detail_doc = None
|
||||
_MODULE_LOGGER.info("Nessun documento selezionato: ripristino placeholder del dettaglio.")
|
||||
@@ -1093,14 +1131,36 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
async def _job():
|
||||
params = {"days": days}
|
||||
_log_sql("SQL_PL", SQL_PL, params)
|
||||
return await self.db_client.query_json(SQL_PL, params)
|
||||
result = await self.db_client.query_json(SQL_PL, params)
|
||||
pair_result = await self.db_client.query_json(
|
||||
"SELECT Documento1,Documento2,Active1,Active2 FROM dbo.py_PickingPair WHERE IsOpen=1;")
|
||||
pair_rows = _rows_to_dicts(pair_result)
|
||||
pair_docs, slots = [], {}
|
||||
if pair_rows:
|
||||
pair = pair_rows[0]
|
||||
pair_docs = [_s(pair["Documento1"]), _s(pair["Documento2"])]
|
||||
slots = {pair_docs[i - 1]: i if pair[f"Active{i}"] else 0 for i in (1, 2)}
|
||||
rows = _rows_to_dicts(result)
|
||||
for row in rows:
|
||||
doc = _s(row["Documento"])
|
||||
if doc in slots:
|
||||
row["IDStato"] = slots[doc]
|
||||
return {"rows": rows, "pair_documents": pair_docs}
|
||||
def _on_success(res):
|
||||
rows = _rows_to_dicts(res)
|
||||
_log_dataset("SQL_PL", rows)
|
||||
self._refresh_mid_rows(rows)
|
||||
self._reserved_pair_documents = set(res.get("pair_documents", []))
|
||||
visible = {_s(m.pl.get("Documento")) for m in self.rows_models}
|
||||
self._selection_order = [d for d in self._selection_order if d in visible]
|
||||
for m in self.rows_models:
|
||||
m.set_checked(_s(m.pl.get("Documento")) in self._selection_order)
|
||||
self._update_sequence_button()
|
||||
if reselect_documento:
|
||||
def _reselect_or_clear(doc=reselect_documento):
|
||||
found = self._reselect_documento_after_reload(doc)
|
||||
if not found and self._selection_order:
|
||||
found = self._reselect_documento_after_reload(self._selection_order[-1])
|
||||
if not found:
|
||||
self.detail_doc = None
|
||||
self._draw_details_hint()
|
||||
@@ -1193,6 +1253,13 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
def on_sequence(self):
|
||||
"""Open the pickup-sequence editor for the selected picking list."""
|
||||
|
||||
if len(self._selected_models()) != 1 or any(
|
||||
_s(m.pl.get("Documento")) in self._reserved_pair_documents for m in self._selected_models()
|
||||
):
|
||||
messagebox.showinfo("Sequenza di prelievo", loc_text("picking.pair.sequence_blocked", catalog=self._locale_catalog,
|
||||
default="Personalizzazione disponibile solo per una lista non abbinata."), parent=self)
|
||||
return
|
||||
|
||||
if not self._can("pickinglist.sequence"):
|
||||
messagebox.showwarning(
|
||||
loc_text("picking.sequence.title", catalog=self._locale_catalog, default="Sequenza di prelievo"),
|
||||
@@ -1234,17 +1301,18 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
if not self._can("pickinglist.prenota"):
|
||||
messagebox.showwarning("Permesso negato", "L'operatore corrente non puo' prenotare picking list.", parent=self)
|
||||
return
|
||||
model = self._get_selected_model()
|
||||
if not model:
|
||||
selected = self._selected_models()
|
||||
if not selected:
|
||||
messagebox.showinfo("Prenota", "Seleziona una Picking List (checkbox) prima di prenotare.")
|
||||
return
|
||||
|
||||
documento = _s(model.pl.get("Documento"))
|
||||
current = int(model.pl.get("IDStato") or 0)
|
||||
documents = [_s(m.pl.get("Documento")) for m in selected]
|
||||
documento = documents[0]
|
||||
current = int(selected[0].pl.get("IDStato") or 0)
|
||||
desired = 1
|
||||
# Idempotent semantics: Prenota never toggles a list off. If the list
|
||||
# is already reserved, S-prenota remains the only explicit release path.
|
||||
if current == desired:
|
||||
if len(documents) == 1 and current in (1, 2):
|
||||
messagebox.showinfo("Prenota", f"La Picking List {documento} è già prenotata.")
|
||||
return
|
||||
|
||||
@@ -1256,9 +1324,12 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
self.spinner.start(" Prenoto…")
|
||||
|
||||
async def _job():
|
||||
# Use the Python-specific py_* stored procedure so C# legacy
|
||||
# procedures can coexist unchanged on the same database.
|
||||
return await sp_xExePackingListPallet_async(self.db_client, id_operatore, documento, "P")
|
||||
pairs = PickingPairRepository(self.db_client)
|
||||
if len(documents) == 2:
|
||||
await pairs.command("R", id_operatore, documents=tuple(documents))
|
||||
else:
|
||||
await pairs.reserve_single(documento, id_operatore)
|
||||
return SPResult(rc=0, message="", id_result=None)
|
||||
|
||||
def _ok(res: SPResult):
|
||||
_MODULE_LOGGER.log(_MODULE_LOG_LEVEL, f"Esito prenotazione documento={documento} rc={getattr(res, 'rc', None)} message={getattr(res, 'message', None)}")
|
||||
@@ -1270,6 +1341,7 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
action="pickinglist.prenota",
|
||||
outcome="ok",
|
||||
target=documento,
|
||||
details={"documents": documents},
|
||||
)
|
||||
self._detail_cache.pop(documento, None)
|
||||
self.reload_from_db(reselect_documento=documento)
|
||||
@@ -1303,7 +1375,7 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
on_success=_ok,
|
||||
on_error=_err,
|
||||
busy=self.busy,
|
||||
message=f"Prenoto la Picking List {documento}…"
|
||||
message=f"Prenoto PL {', '.join(documents)}..."
|
||||
)
|
||||
|
||||
@_log_call()
|
||||
@@ -1312,13 +1384,14 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
if not self._can("pickinglist.sprenota"):
|
||||
messagebox.showwarning("Permesso negato", "L'operatore corrente non puo' s-prenotare picking list.", parent=self)
|
||||
return
|
||||
model = self._get_selected_model()
|
||||
if not model:
|
||||
selected = self._selected_models()
|
||||
if not selected:
|
||||
messagebox.showinfo("S-prenota", "Seleziona una Picking List (checkbox) prima di s-prenotare.")
|
||||
return
|
||||
|
||||
documento = _s(model.pl.get("Documento"))
|
||||
current = int(model.pl.get("IDStato") or 0)
|
||||
documents = [_s(m.pl.get("Documento")) for m in selected]
|
||||
documento = documents[0]
|
||||
current = max(int(m.pl.get("IDStato") or 0) for m in selected)
|
||||
desired = 0
|
||||
# Idempotent semantics: S-prenota never reserves a list; if the list is
|
||||
# already free, the operation is a no-op rather than an implicit toggle.
|
||||
@@ -1334,9 +1407,9 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
self.spinner.start(" S-prenoto…")
|
||||
|
||||
async def _job():
|
||||
# Same Python-only procedure as Prenota, with action S to release
|
||||
# the singleton reservation and clear affected cell states.
|
||||
return await sp_xExePackingListPallet_async(self.db_client, id_operatore, documento, "S")
|
||||
# Release selected slots atomically, without changing the other role.
|
||||
await PickingPairRepository(self.db_client).release_selected(documents, id_operatore)
|
||||
return SPResult(rc=0, message="", id_result=None)
|
||||
|
||||
def _ok(res: SPResult):
|
||||
_MODULE_LOGGER.log(_MODULE_LOG_LEVEL, f"Esito s-prenotazione documento={documento} rc={getattr(res, 'rc', None)} message={getattr(res, 'message', None)}")
|
||||
@@ -1348,6 +1421,7 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
action="pickinglist.sprenota",
|
||||
outcome="ok",
|
||||
target=documento,
|
||||
details={"documents": documents},
|
||||
)
|
||||
self._detail_cache.pop(documento, None)
|
||||
self.reload_from_db(reselect_documento=documento)
|
||||
@@ -1381,7 +1455,7 @@ class GestionePickingListFrame(ctk.CTkFrame):
|
||||
on_success=_ok,
|
||||
on_error=_err,
|
||||
busy=self.busy,
|
||||
message=f"S-prenoto la Picking List {documento}…"
|
||||
message=f"S-prenoto PL {', '.join(documents)}..."
|
||||
)
|
||||
|
||||
def on_export(self):
|
||||
|
||||
Reference in New Issue
Block a user