Implementa picking bilista 2.1.0 con test, patch SQL e rollback
This commit is contained in:
139
pickinglist_pair.py
Normal file
139
pickinglist_pair.py
Normal file
@@ -0,0 +1,139 @@
|
||||
"""Pure routing rules for paired picking.
|
||||
|
||||
The caller supplies a fresh residual snapshot and per-document rack sequences.
|
||||
No database writes, Tk calls, movement execution or reservation changes occur
|
||||
here. Keeping this calculation independent allows deterministic regression
|
||||
tests without moving stock. SQL access and UI integration live in separate modules.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Iterable, Mapping
|
||||
|
||||
from version_info import module_version
|
||||
|
||||
__version__ = module_version(__name__)
|
||||
|
||||
NON_SHELVED_CELL = 1000
|
||||
SHIPPED_CELL = 9999
|
||||
PAIR_SLOT_COLORS = {1: "#ffe6f2", 2: "#fff2cc"}
|
||||
|
||||
|
||||
class PairRoutingError(ValueError):
|
||||
"""Reject an inconsistent snapshot rather than silently omitting stock."""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PickingCandidate:
|
||||
"""One physical UDC, possibly repeated across lot rows of the same document.
|
||||
|
||||
``area`` is the operational area 5/6, NOT the legacy Aree primary key.
|
||||
It is ignored for virtual locations. ``cell_order`` is Celle.Ordinamento;
|
||||
it preserves both the Area 5 serpentine and the Area 6 upward route.
|
||||
``group_key`` identifies the rack for a shared custom sequence.
|
||||
"""
|
||||
|
||||
document: str
|
||||
pallet: str
|
||||
cell_id: int
|
||||
area: int | None
|
||||
cell_order: int
|
||||
group_key: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PairRoute:
|
||||
"""Read-only result; skipped candidates remain residual, never shipped."""
|
||||
|
||||
documents: tuple[str, str]
|
||||
area: int | None
|
||||
pending: tuple[PickingCandidate, ...]
|
||||
skipped: tuple[PickingCandidate, ...]
|
||||
|
||||
def slot_for(self, document: str) -> int:
|
||||
"""Return the stable desktop IDStato 1/2, independent of route position."""
|
||||
if document not in self.documents:
|
||||
raise PairRoutingError("Documento estraneo alla coppia.")
|
||||
return self.documents.index(document) + 1
|
||||
|
||||
|
||||
def build_pair_route(
|
||||
candidates: Iterable[PickingCandidate],
|
||||
*,
|
||||
documents: tuple[str, str],
|
||||
document_sequences: Mapping[str, Iterable[str]] | None = None,
|
||||
skipped: Iterable[tuple[str, str]] = (),
|
||||
) -> PairRoute:
|
||||
"""Build a route from a residual snapshot without changing any input.
|
||||
|
||||
``documents`` defines the reservation roles, not which document starts.
|
||||
``document_sequences`` contains saved per-document sequences. Only the
|
||||
first selected document's sequence governs the pair, even if the second
|
||||
document starts the route. The second sequence is never merged or changed.
|
||||
An empty/missing first sequence uses the legacy global order for both.
|
||||
Non-shelved UDCs always precede racks, even if NON_SCAFF appears last in
|
||||
an older sequence. Unconfigured racks follow configured racks in legacy
|
||||
cell order. Non-shelved UDCs are ordered by slot 1 then slot 2, and by
|
||||
pallet code within each slot, before any physical cell is proposed.
|
||||
|
||||
``skipped`` contains (document, pallet) identities from the active cycle.
|
||||
Area consistency is checked before separating skips from workable UDCs.
|
||||
Duplicate lot rows collapse only when their route metadata agrees.
|
||||
"""
|
||||
if len(documents) != 2 or any(not d or d != d.strip() for d in documents):
|
||||
raise PairRoutingError("Servono due documenti non vuoti.")
|
||||
if documents[0] == documents[1]:
|
||||
raise PairRoutingError("La coppia deve contenere due documenti diversi.")
|
||||
first_sequence = (document_sequences or {}).get(documents[0], ())
|
||||
groups = tuple(key for key in first_sequence if key != "NON_SCAFF")
|
||||
if any(not key for key in groups) or len(groups) != len(set(groups)):
|
||||
raise PairRoutingError("Sequenza scaffali non valida.")
|
||||
rank = {key: index for index, key in enumerate(groups)}
|
||||
slots = {document: index + 1 for index, document in enumerate(documents)}
|
||||
unique: dict[tuple[str, str], PickingCandidate] = {}
|
||||
owners: dict[str, str] = {}
|
||||
areas: set[int] = set()
|
||||
|
||||
for row in candidates:
|
||||
if row.document not in slots:
|
||||
raise PairRoutingError("Documento estraneo alla coppia.")
|
||||
if row.cell_id == SHIPPED_CELL:
|
||||
continue
|
||||
if not row.pallet or row.pallet != row.pallet.strip():
|
||||
raise PairRoutingError("Codice UDC mancante o non normalizzato.")
|
||||
if row.cell_id != NON_SHELVED_CELL:
|
||||
if row.cell_id <= 0 or row.area not in (5, 6):
|
||||
raise PairRoutingError("Area della cella fisica non riconosciuta.")
|
||||
if row.cell_order <= 0 or not row.group_key:
|
||||
raise PairRoutingError("Ordinamento della cella fisica non valido.")
|
||||
areas.add(row.area)
|
||||
owner = owners.setdefault(row.pallet, row.document)
|
||||
if owner != row.document:
|
||||
raise PairRoutingError(f"UDC {row.pallet} presente in entrambe le liste.")
|
||||
identity = (row.document, row.pallet)
|
||||
previous = unique.get(identity)
|
||||
if previous is not None and previous != row:
|
||||
raise PairRoutingError(f"Posizione o percorso discordante per UDC {row.pallet}.")
|
||||
unique[identity] = row
|
||||
|
||||
if len(areas) > 1:
|
||||
raise PairRoutingError("Le UDC scaffalate devono appartenere alla stessa area.")
|
||||
|
||||
def route_key(row: PickingCandidate) -> tuple:
|
||||
slot = slots[row.document]
|
||||
if row.cell_id == NON_SHELVED_CELL:
|
||||
return (0, slot, row.pallet)
|
||||
if row.group_key in rank:
|
||||
return (1, 0, rank[row.group_key], row.cell_order, row.pallet, slot)
|
||||
# Do not group racks in the default: that would destroy Area 5's serpentine.
|
||||
return (1, 1, 0, row.cell_order, row.pallet, slot)
|
||||
|
||||
ordered = sorted(unique.values(), key=route_key)
|
||||
skip_ids = set(skipped)
|
||||
return PairRoute(
|
||||
documents=documents,
|
||||
area=next(iter(areas)) if areas else None,
|
||||
pending=tuple(row for row in ordered if (row.document, row.pallet) not in skip_ids),
|
||||
skipped=tuple(row for row in ordered if (row.document, row.pallet) in skip_ids),
|
||||
)
|
||||
Reference in New Issue
Block a user