Compare commits

..

10 Commits

26 changed files with 3254 additions and 56 deletions

View File

@@ -0,0 +1,247 @@
# Picking list con nazione di destinazione mancante
## Scopo
Questo documento descrive il difetto che rendeva invisibile una picking list
quando il documento SAM utilizzava un indirizzo di destinazione alternativo
senza nazione valorizzata. Descrive inoltre la correzione applicata al solo
ramo dati Python del WMS, le conseguenze funzionali e le verifiche da eseguire.
La correzione non modifica tabelle o viste legacy utilizzate dal programma C#
e non aggiorna dati anagrafici o documentali in SAM.
## Caso che ha evidenziato il difetto
Il problema e' stato rilevato sulla picking list `244/2026`:
| Dato | Valore |
|---|---|
| `BAMTES.ID` | `11896` |
| `BAMTES.NUMDOC` | `244` |
| `BAMTES.ANNDOC` | `2026` |
| Data documento | `24/09/2026` |
| `BAMTES.STATO` | `P` |
| `BAMTES.IDINDDEST` | `274` |
| Destinazione | `c/o UNITRANS Magazzino 4 Mirandola` |
| `BAMTES.IDNAZDEST` | `NULL` |
L'indirizzo alternativo `SAMA1.dbo.INDANA.ID = 274` esiste, ma anche il suo
campo `IDNAZIONI` e' `NULL`. L'indirizzo abituale della stessa controparte ha
invece `IDNAZIONI = 21`, corrispondente all'Italia.
Il documento non aveva altri problemi:
- anno corrente valido;
- stato `P` valido;
- data compresa nell'intervallo operativo;
- 4 righe documento;
- 494 righe lotto valide;
- 26 UDC distinte;
- trasportatore esistente.
## Causa tecnica
La preparazione delle picking list collegava la testata del documento alla
tabella delle nazioni con un collegamento obbligatorio:
```sql
FROM SAMA1.dbo.NAZIONI
INNER JOIN SAMA1.dbo.BAMTES
ON SAMA1.dbo.NAZIONI.ID = SAMA1.dbo.BAMTES.IDNAZDEST
```
Un `INNER JOIN` conserva soltanto le testate per le quali esiste una nazione
compatibile. Quando `BAMTES.IDNAZDEST` e' `NULL`, il confronto non puo' trovare
alcuna riga e l'intero documento scompare prima che vengano elaborate righe,
lotti e UDC.
Nel caso 244 la diagnosi ha prodotto:
```text
Righe valide senza join obbligatorio a NAZIONI: 494
Righe valide con join obbligatorio a NAZIONI: 0
```
Il filtro della GUI a 10 giorni, la prenotazione e la sequenza di prelievo non
erano quindi responsabili del problema.
## Flusso dati interessato
La pagina Gestione Picking List usa questa catena:
```text
SAMA1.dbo.BAMTES / FATRIG / LOTTIBF / LOTSER
|
v
dbo.py_XMag_ViewPackingList
|
v
dbo.py_ViewPackingListRestante
|
v
dbo.py_ViewPackingListPrelievo
|
v
Gestione Picking List e barcode Python
```
Lo storico Python utilizza inoltre:
```text
SAMA1
-> dbo.py_vPreparaPackingListSAMA1
-> dbo.py_vPreparaPackingList
-> dbo.py_XMag_ViewPackingListStorico
```
Per evitare comportamenti diversi fra operativita' e storico, la correzione e'
stata applicata sia alla preparazione operativa sia alla preparazione storica.
## Correzione applicata
Il collegamento e' stato reso opzionale:
```sql
FROM SAMA1.dbo.BAMTES AS bt
LEFT OUTER JOIN SAMA1.dbo.NAZIONI AS n
ON n.ID = bt.IDNAZDEST
```
Con il `LEFT OUTER JOIN` la testata BAMTES viene conservata anche quando la
nazione e' assente. Gli altri collegamenti essenziali verso righe, lotti e
seriali restano obbligatori.
Sono state modificate soltanto queste viste Python:
- `dbo.py_vPreparaPackingListSAMA1`;
- `dbo.py_XMag_ViewPackingList`.
Le viste dipendenti ricevono automaticamente i dati corretti:
- `dbo.py_vPreparaPackingList`;
- `dbo.py_XMag_ViewPackingListStorico`;
- `dbo.py_ViewPackingListRestante`;
- `dbo.py_ViewPackingListPrelievo`.
## Gestione dei valori mancanti
La patch non assegna automaticamente una nazione. Questo evita di trasformare
un'anagrafica incompleta in un dato apparentemente certo.
Quando la nazione manca:
- `CodNazione` resta `NULL`;
- `NAZIONE` resta `NULL`;
- il documento e le sue UDC restano visibili;
- il `CASE` applicativo gia' esistente usa il ramo generico `Stato = 4`;
- prenotazione, dettaglio UDC e sequenza di prelievo continuano a funzionare.
La correzione dell'anagrafica SAM rimane comunque consigliata. La robustezza
della vista impedisce pero' che un campo descrittivo incompleto blocchi un
processo operativo di magazzino.
## Script di installazione
La patch autonoma e' contenuta in:
```text
apply_pickinglist_optional_destination_country_patch.sql
```
Lo stesso cambiamento e' stato integrato anche negli script cumulativi:
- `apply_pickinglist_days_filter_patch.sql`;
- `apply_online_python_wms_full_patch.sql`.
Per aggiornare un database che possiede gia' le viste Python e' sufficiente
eseguire una sola volta in SSMS la patch autonoma. Gli script usano
`CREATE OR ALTER VIEW`, quindi possono essere rieseguiti senza duplicare dati.
## Verifica post-patch
Controllo della vista operativa:
```sql
SELECT
Documento,
DataDocumento,
CodNazione,
NAZIONE,
Stato,
IDStato,
COUNT(DISTINCT Pallet) AS NumeroUDC
FROM dbo.py_ViewPackingListPrelievo
WHERE Documento = 244
GROUP BY
Documento,
DataDocumento,
CodNazione,
NAZIONE,
Stato,
IDStato;
```
Risultato ottenuto dopo la correzione:
```text
Documento: 244
CodNazione: NULL
NAZIONE: NULL
Stato: 4
IDStato: 0
Numero UDC: 26
```
Controllo dei quattro livelli principali:
```sql
SELECT COUNT(DISTINCT UDC) AS UDC
FROM dbo.py_vPreparaPackingListSAMA1
WHERE NUMDOC = 244 AND StatoDocumento = 'P';
SELECT COUNT(DISTINCT Pallet) AS UDC
FROM dbo.py_XMag_ViewPackingList
WHERE Documento = 244;
SELECT COUNT(DISTINCT Pallet) AS UDC
FROM dbo.py_ViewPackingListRestante
WHERE Documento = 244;
SELECT COUNT(DISTINCT Pallet) AS UDC
FROM dbo.py_ViewPackingListPrelievo
WHERE Documento = 244;
```
Tutte e quattro le query devono restituire `26` per il caso analizzato.
## Impatto e compatibilita'
- Nessuna riga SAM viene inserita, aggiornata o cancellata.
- Nessuna prenotazione viene modificata.
- Nessuna stored procedure di movimentazione viene modificata.
- Le picking list con nazione valida mantengono lo stesso risultato.
- Le picking list prima invisibili per nazione nulla diventano visibili.
- Gli oggetti legacy senza prefisso `py_` non vengono modificati.
## Ripristino concettuale
Per ripristinare il comportamento precedente occorrerebbe riportare il
collegamento `BAMTES -> NAZIONI` da `LEFT OUTER JOIN` a `INNER JOIN` nelle due
viste Python modificate. Tale rollback farebbe nuovamente scomparire tutti i
documenti con `IDNAZDEST` nullo e non e' quindi raccomandato come soluzione
operativa.
## Miglioramento futuro consigliato
Una diagnostica separata potrebbe mostrare le picking list con dati anagrafici
incompleti, senza impedirne l'uso:
```sql
SELECT ID, ANNDOC, NUMDOC, IDINDDEST, DESCRDEST
FROM SAMA1.dbo.BAMTES
WHERE STATO = 'P'
AND IDNAZDEST IS NULL;
```
In questo modo il WMS rimane operativo e l'amministratore puo' correggere
l'anagrafica SAM in un secondo momento.

View File

@@ -72,7 +72,7 @@ LEFT OUTER JOIN SAMA1.sam.EXTUC
LEFT OUTER JOIN SAMA1.dbo.MTRASP
ON SAMA1.dbo.BAMTES.IDMTRASP = SAMA1.dbo.MTRASP.ID
WHERE
SAMA1.dbo.BAMTES.ANNDOC >= YEAR(GETDATE()) - 1
SAMA1.dbo.BAMTES.ANNDOC >= YEAR(GETDATE())
AND SAMA1.dbo.BAMTES.STATO IN ('P', 'D')
AND SAMA1.dbo.LOTSER.NUMLOT <> '00000000000'
GROUP BY

View File

@@ -16,10 +16,18 @@ Include:
- dbo.py_vPreparaPackingList
- dbo.py_XMag_ViewPackingListStorico
- dbo.py_BarcodePickingListSkip
- dbo.py_PickingListPrelievoSequenza
- dbo.py_ViewPackingListPrelievo
Include inoltre:
- intervallo operativo massimo di 30 giorni, filtrato dalla UI a 10/15/20/30;
- destinazione senza nazione ammessa nel ramo Python mediante LEFT JOIN;
- ordine predefinito con UDC Non scaff. prima degli scaffali;
- ordine personalizzabile per singolo Documento.
Nota storico:
dbo.py_vPreparaPackingListSAMA1 usa ANNDOC >= YEAR(GETDATE()) - 1
per mantenere visibili anche le picking list dell'anno precedente.
dbo.py_vPreparaPackingListSAMA1 usa ANNDOC >= YEAR(GETDATE())
per mantenere visibili solo le picking list dall'inizio dell'anno corrente.
*/
SET ANSI_NULLS ON;
@@ -362,8 +370,8 @@ SELECT
SAMA1.dbo.NAZIONI.CODICE AS Expr1,
SAMA1.dbo.MTRASP.CODICE + ' ' + SAMA1.dbo.NAZIONI.DESCR AS NAZIONE,
SAMA1.dbo.BAMTES.IDMTRASP
FROM SAMA1.dbo.NAZIONI
INNER JOIN SAMA1.dbo.BAMTES
FROM SAMA1.dbo.BAMTES
LEFT OUTER JOIN SAMA1.dbo.NAZIONI
ON SAMA1.dbo.NAZIONI.ID = SAMA1.dbo.BAMTES.IDNAZDEST
RIGHT OUTER JOIN SAMA1.dbo.LOTTIBF
LEFT OUTER JOIN SAMA1.dbo.LOTSER
@@ -378,7 +386,7 @@ LEFT OUTER JOIN SAMA1.sam.EXTUC
LEFT OUTER JOIN SAMA1.dbo.MTRASP
ON SAMA1.dbo.BAMTES.IDMTRASP = SAMA1.dbo.MTRASP.ID
WHERE
SAMA1.dbo.BAMTES.ANNDOC >= YEAR(GETDATE()) - 1
SAMA1.dbo.BAMTES.ANNDOC >= YEAR(GETDATE())
AND SAMA1.dbo.BAMTES.STATO IN ('P', 'D')
AND SAMA1.dbo.LOTSER.NUMLOT <> '00000000000'
GROUP BY
@@ -527,5 +535,191 @@ BEGIN
END;
GO
/* ============================================================
6. Intervallo operativo 30 giorni e sequenza di prelievo
La vista di preparazione Python creata al punto 4 contiene sia P sia D.
Qui il ramo operativo viene ristretto alle sole liste P degli ultimi 30
giorni; la UI applica successivamente il valore scelto (10/15/20/30).
============================================================ */
CREATE OR ALTER VIEW dbo.py_XMag_ViewPackingList
AS
SELECT TOP (10000)
prep.UDC AS Pallet,
prep.NUMLOT AS Lotto,
prep.CODICE AS Articolo,
prep.DESCR AS Descrizione,
prep.Qta,
prep.NUMDOC AS Documento,
prep.DATDOC AS DataDocumento,
prep.Expr1 AS CodNazione,
prep.NAZIONE,
CASE
WHEN prep.Expr1 = 'DE' THEN 10
WHEN prep.Expr1 = 'TH' THEN
CASE WHEN SUBSTRING(prep.DESCRDEST, 1, 2) = 'NA' THEN 11 ELSE 13 END
WHEN prep.Expr1 = 'MEX' THEN 12
ELSE 4
END AS Stato,
ISNULL(g.NumeroPallet, 0) AS PalletCella,
ISNULL(g.IDMagazzino, 1) AS Magazzino,
ISNULL(g.IDArea, 5) AS Area,
ISNULL(g.IDCella, 1000) AS Cella,
ISNULL(c.Ordinamento, 99999) AS Ordinamento,
ISNULL(c.Corsia + ' - ' + c.Colonna + ' - ' + c.Fila, 'Non scaff.') AS Ubicazione,
SUBSTRING(prep.DESCRDEST, 1, 2) AS DEST,
CASE
WHEN MAX(CASE
WHEN pr.Documento IS NOT NULL
AND pr.Documento = CAST(prep.NUMDOC AS varchar(8))
THEN 1 ELSE 0 END) = 1
THEN 1
ELSE 0
END AS IDStato
FROM dbo.Celle AS c
INNER JOIN dbo.XMag_GiacenzaPallet AS g
ON c.ID = g.IDCella
RIGHT OUTER JOIN dbo.py_vPreparaPackingList AS prep
ON g.BarcodePallet COLLATE SQL_Latin1_General_CP1_CI_AS = prep.UDC
LEFT JOIN dbo.PyPickingListReservation AS pr
ON pr.ID = 1
AND NULLIF(LTRIM(RTRIM(pr.Documento)), '') IS NOT NULL
WHERE prep.StatoDocumento = 'P'
AND prep.DATDOC > CAST((GETDATE() - 30) AS int)
GROUP BY
prep.Expr1,
prep.NAZIONE,
prep.UDC,
prep.NUMDOC,
prep.DATDOC,
prep.NUMLOT,
prep.CODICE,
prep.DESCR,
prep.Qta,
g.NumeroPallet,
g.IDMagazzino,
g.IDArea,
g.IDCella,
c.Ordinamento,
c.Corsia,
c.Colonna,
c.Fila,
prep.DESCRDEST;
GO
CREATE OR ALTER VIEW dbo.py_ViewPackingListRestante
AS
SELECT
Pallet, Lotto, Articolo, Descrizione, Qta, Documento, DataDocumento,
CodNazione, NAZIONE, Stato, PalletCella, Magazzino, Area, Cella,
Ordinamento, Ubicazione, DEST, IDStato
FROM dbo.py_XMag_ViewPackingList
WHERE Cella <> 9999;
GO
IF OBJECT_ID(N'dbo.py_PickingListPrelievoSequenza', N'U') IS NULL
BEGIN
CREATE TABLE dbo.py_PickingListPrelievoSequenza (
ID bigint IDENTITY(1,1) NOT NULL
CONSTRAINT PK_py_PickingListPrelievoSequenza PRIMARY KEY,
Documento varchar(50) NOT NULL,
GruppoChiave varchar(160) NOT NULL,
GruppoEtichetta varchar(100) NOT NULL,
Ordine int NOT NULL,
InsUtente varchar(50) NULL,
InsDataOra datetime2(0) NOT NULL
CONSTRAINT DF_py_PickingListPrelievoSequenza_InsDataOra DEFAULT SYSDATETIME(),
ModUtente varchar(50) NULL,
ModDataOra datetime2(0) NULL,
DelUtente varchar(50) NULL,
DelDataOra datetime2(0) NULL,
CONSTRAINT CK_py_PickingListPrelievoSequenza_Ordine CHECK (Ordine >= 0)
);
END;
GO
IF NOT EXISTS (
SELECT 1 FROM sys.indexes
WHERE object_id = OBJECT_ID(N'dbo.py_PickingListPrelievoSequenza', N'U')
AND name = N'UX_py_PickingListPrelievoSequenza_DocGruppo'
)
BEGIN
CREATE UNIQUE INDEX UX_py_PickingListPrelievoSequenza_DocGruppo
ON dbo.py_PickingListPrelievoSequenza (Documento, GruppoChiave)
WHERE DelDataOra IS NULL;
END;
GO
IF NOT EXISTS (
SELECT 1 FROM sys.indexes
WHERE object_id = OBJECT_ID(N'dbo.py_PickingListPrelievoSequenza', N'U')
AND name = N'UX_py_PickingListPrelievoSequenza_DocOrdine'
)
BEGIN
CREATE UNIQUE INDEX UX_py_PickingListPrelievoSequenza_DocOrdine
ON dbo.py_PickingListPrelievoSequenza (Documento, Ordine)
WHERE DelDataOra IS NULL;
END;
GO
CREATE OR ALTER VIEW dbo.py_ViewPackingListPrelievo
AS
WITH Gruppi AS (
SELECT
pl.*,
CASE
WHEN pl.Cella = 1000 THEN 'NON_SCAFF'
ELSE CONCAT(
'M:', pl.Magazzino, '|A:', pl.Area, '|C:',
LTRIM(RTRIM(CASE
WHEN CHARINDEX(' - ', pl.Ubicazione) > 0
THEN LEFT(pl.Ubicazione, CHARINDEX(' - ', pl.Ubicazione) - 1)
ELSE pl.Ubicazione
END))
)
END AS GruppoPrelievoChiave,
CASE
WHEN pl.Cella = 1000 THEN 'Non scaff.'
ELSE LTRIM(RTRIM(CASE
WHEN CHARINDEX(' - ', pl.Ubicazione) > 0
THEN LEFT(pl.Ubicazione, CHARINDEX(' - ', pl.Ubicazione) - 1)
ELSE pl.Ubicazione
END))
END AS GruppoPrelievo,
CASE
WHEN pl.Cella = 1000 THEN 0
ELSE 1000 + ISNULL(pl.Ordinamento, 99999)
END AS OrdineGruppoDefault
FROM dbo.py_ViewPackingListRestante AS pl
), Sequenza AS (
SELECT
g.*,
s.Ordine AS OrdineGruppoConfigurato,
CASE WHEN EXISTS (
SELECT 1
FROM dbo.py_PickingListPrelievoSequenza AS any_s
WHERE any_s.Documento COLLATE Latin1_General_CI_AS =
CAST(g.Documento AS varchar(50)) COLLATE Latin1_General_CI_AS
AND any_s.DelDataOra IS NULL
) THEN 1 ELSE 0 END AS HaSequenzaPersonalizzata
FROM Gruppi AS g
LEFT JOIN dbo.py_PickingListPrelievoSequenza AS s
ON s.Documento COLLATE Latin1_General_CI_AS =
CAST(g.Documento AS varchar(50)) COLLATE Latin1_General_CI_AS
AND s.GruppoChiave COLLATE Latin1_General_CI_AS =
g.GruppoPrelievoChiave COLLATE Latin1_General_CI_AS
AND s.DelDataOra IS NULL
)
SELECT
Sequenza.*,
CASE
WHEN OrdineGruppoConfigurato IS NOT NULL THEN OrdineGruppoConfigurato
WHEN HaSequenzaPersonalizzata = 1 THEN 100000 + OrdineGruppoDefault
ELSE OrdineGruppoDefault
END AS OrdinePrelievoGruppo
FROM Sequenza;
GO
PRINT 'Patch cumulativa Python WMS completata.';
GO

View File

@@ -0,0 +1,190 @@
/*
Patch: intervallo giorni selezionabile in Gestione Picking List Python
Scopo:
- permettere alla UI Python di caricare picking list operative degli ultimi
10, 15, 20 o 30 giorni;
- mantenere 10 giorni come default applicativo;
- non escludere documenti validi con nazione di destinazione non valorizzata;
- non modificare gli oggetti legacy usati dal programma C#.
Oggetti modificati:
- dbo.py_XMag_ViewPackingList
- dbo.py_ViewPackingListRestante
Nota:
La vista legacy dbo.XMag_ViewPackingList dipende da dbo.vPreparaPackingListSAMA1,
che contiene il filtro fisso GETDATE()-10. Per questo la vista Python viene
ricalcolata direttamente dalle tabelle SAM con un orizzonte massimo di 30 giorni.
La UI applica poi il filtro effettivo scelto dall'operatore.
Il collegamento a NAZIONI e' intenzionalmente LEFT OUTER JOIN: un indirizzo
alternativo incompleto non deve rendere invisibile l'intera picking list.
*/
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
CREATE OR ALTER VIEW [dbo].[py_XMag_ViewPackingList]
AS
WITH PreparaPackingList AS (
SELECT
SAMA1.dbo.LOTSER.NUMLOT,
SAMA1.dbo.ARTICO.CODICE,
SAMA1.dbo.FATRIG.DESCR,
LEFT(SAMA1.dbo.LOTSER.NUMSER, 6) AS UDC,
SUM(SAMA1.dbo.LOTSER.QTTGIAI) AS Qta,
SAMA1.dbo.BAMTES.NUMDOC,
SAMA1.dbo.BAMTES.DATDOC,
SAMA1.dbo.BAMTES.ID,
SAMA1.dbo.BAMTES.DESCRDEST,
SAMA1.dbo.NAZIONI.CODICE AS Expr1,
SAMA1.dbo.MTRASP.CODICE + ' ' + SAMA1.dbo.NAZIONI.DESCR AS NAZIONE,
SAMA1.dbo.BAMTES.IDMTRASP
FROM SAMA1.dbo.BAMTES
LEFT OUTER JOIN SAMA1.dbo.NAZIONI
ON SAMA1.dbo.NAZIONI.ID = SAMA1.dbo.BAMTES.IDNAZDEST
RIGHT OUTER JOIN SAMA1.dbo.LOTTIBF
LEFT OUTER JOIN SAMA1.dbo.LOTSER
ON SAMA1.dbo.LOTTIBF.IDLOTSER = SAMA1.dbo.LOTSER.ID
LEFT OUTER JOIN SAMA1.dbo.ARTICO
ON SAMA1.dbo.LOTSER.IDARTICO = SAMA1.dbo.ARTICO.ID
LEFT OUTER JOIN SAMA1.dbo.FATRIG
ON SAMA1.dbo.LOTTIBF.IDFATRIG = SAMA1.dbo.FATRIG.ID
ON SAMA1.dbo.BAMTES.ID = SAMA1.dbo.FATRIG.IDBAM
LEFT OUTER JOIN SAMA1.sam.EXTUC
ON LEFT(SAMA1.dbo.LOTSER.NUMSER, 6) = SAMA1.sam.EXTUC.CODICE
LEFT OUTER JOIN SAMA1.dbo.MTRASP
ON SAMA1.dbo.BAMTES.IDMTRASP = SAMA1.dbo.MTRASP.ID
WHERE SAMA1.dbo.BAMTES.ANNDOC >= YEAR(GETDATE())
AND SAMA1.dbo.BAMTES.STATO = 'P'
AND SAMA1.dbo.BAMTES.DATDOC > CAST((GETDATE() - 30) AS int)
AND SAMA1.dbo.LOTSER.NUMLOT <> '00000000000'
GROUP BY
SAMA1.dbo.LOTSER.NUMLOT,
SAMA1.dbo.ARTICO.CODICE,
SAMA1.dbo.FATRIG.DESCR,
LEFT(SAMA1.dbo.LOTSER.NUMSER, 6),
SAMA1.dbo.BAMTES.NUMDOC,
SAMA1.dbo.BAMTES.DATDOC,
SAMA1.dbo.BAMTES.ID,
SAMA1.dbo.BAMTES.DESCRDEST,
SAMA1.dbo.NAZIONI.CODICE,
SAMA1.dbo.NAZIONI.DESCR,
SAMA1.dbo.BAMTES.IDMTRASP,
SAMA1.dbo.MTRASP.CODICE
),
Base AS (
SELECT
prep.UDC AS Pallet,
prep.NUMLOT AS Lotto,
prep.CODICE AS Articolo,
prep.DESCR AS Descrizione,
prep.Qta,
prep.NUMDOC AS Documento,
prep.DATDOC AS DataDocumento,
prep.Expr1 AS CodNazione,
prep.NAZIONE,
CASE
WHEN prep.Expr1 = 'DE' THEN 10
WHEN prep.Expr1 = 'TH' THEN CASE WHEN SUBSTRING(prep.DESCRDEST, 1, 2) = 'NA' THEN 11 ELSE 13 END
WHEN prep.Expr1 = 'MEX' THEN 12
ELSE 4
END AS Stato,
ISNULL(g.NumeroPallet, 0) AS PalletCella,
ISNULL(g.IDMagazzino, 1) AS Magazzino,
ISNULL(g.IDArea, 5) AS Area,
ISNULL(g.IDCella, 1000) AS Cella,
ISNULL(c.Ordinamento, 99999) AS Ordinamento,
ISNULL(c.Corsia + ' - ' + c.Colonna + ' - ' + c.Fila, 'Non scaff.') AS Ubicazione,
SUBSTRING(prep.DESCRDEST, 1, 2) AS DEST
FROM dbo.Celle AS c
INNER JOIN dbo.XMag_GiacenzaPallet AS g
ON c.ID = g.IDCella
RIGHT OUTER JOIN PreparaPackingList AS prep
ON g.BarcodePallet COLLATE SQL_Latin1_General_CP1_CI_AS = prep.UDC
WHERE prep.NUMLOT <> ''
GROUP BY
prep.Expr1,
prep.NAZIONE,
prep.UDC,
prep.NUMDOC,
prep.DATDOC,
prep.NUMLOT,
prep.CODICE,
prep.DESCR,
prep.Qta,
g.NumeroPallet,
g.IDMagazzino,
g.IDArea,
g.IDCella,
c.Ordinamento,
c.Corsia,
c.Colonna,
c.Fila,
prep.DESCRDEST
)
SELECT TOP 10000
Base.Pallet,
Base.Lotto,
Base.Articolo,
Base.Descrizione,
Base.Qta,
Base.Documento,
Base.DataDocumento,
Base.CodNazione,
Base.NAZIONE,
Base.Stato,
Base.PalletCella,
Base.Magazzino,
Base.Area,
Base.Cella,
Base.Ordinamento,
Base.Ubicazione,
Base.DEST,
CASE
WHEN pr.Documento IS NOT NULL
AND pr.Documento = CAST(Base.Documento AS varchar(8))
THEN 1
ELSE 0
END AS IDStato
FROM Base
LEFT JOIN dbo.PyPickingListReservation AS pr
ON pr.ID = 1
AND NULLIF(LTRIM(RTRIM(pr.Documento)), '') IS NOT NULL
ORDER BY
CASE
WHEN pr.Documento IS NOT NULL
AND pr.Documento = CAST(Base.Documento AS varchar(8))
THEN 1
ELSE 0
END DESC,
Base.Documento,
Base.Ordinamento;
GO
CREATE OR ALTER VIEW [dbo].[py_ViewPackingListRestante]
AS
SELECT
Pallet,
Lotto,
Articolo,
Descrizione,
Qta,
Documento,
DataDocumento,
CodNazione,
NAZIONE,
Stato,
PalletCella,
Magazzino,
Area,
Cella,
Ordinamento,
Ubicazione,
DEST,
IDStato
FROM dbo.py_XMag_ViewPackingList
WHERE Cella <> 9999;
GO

View File

@@ -0,0 +1,217 @@
/*
Patch Python WMS: picking list con nazione di destinazione non valorizzata
Problema risolto:
- una testata BAMTES valida puo' avere IDNAZDEST NULL, in particolare quando
viene scelto un indirizzo di destinazione alternativo privo di nazione;
- l'INNER JOIN a SAMA1.dbo.NAZIONI eliminava l'intero documento;
- la picking list non compariva quindi in Gestione Picking List ne' nello
storico Python, anche se stato, data, righe, lotti e UDC erano validi.
Strategia:
- il collegamento BAMTES -> NAZIONI diventa LEFT OUTER JOIN;
- non viene inventato alcun codice nazione: Expr1 e NAZIONE restano NULL;
- il CASE gia' esistente assegna in questo caso lo Stato generico 4;
- nessun oggetto legacy usato dal programma C# viene modificato.
Oggetti Python modificati:
- dbo.py_vPreparaPackingListSAMA1 (storico e base condivisa);
- dbo.py_XMag_ViewPackingList (gestione operativa).
*/
SET XACT_ABORT ON;
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
CREATE OR ALTER VIEW dbo.py_vPreparaPackingListSAMA1
AS
SELECT
ls.NUMLOT,
a.CODICE,
fr.DESCR,
LEFT(ls.NUMSER, 6) AS UDC,
SUM(ls.QTTGIAI) AS Qta,
bt.NUMDOC,
bt.DATDOC,
bt.ID,
bt.DESCRDEST,
bt.STATO AS StatoDocumento,
n.CODICE AS Expr1,
mt.CODICE + ' ' + n.DESCR AS NAZIONE,
bt.IDMTRASP
FROM SAMA1.dbo.BAMTES AS bt
INNER JOIN SAMA1.dbo.FATRIG AS fr
ON fr.IDBAM = bt.ID
INNER JOIN SAMA1.dbo.LOTTIBF AS lb
ON lb.IDFATRIG = fr.ID
INNER JOIN SAMA1.dbo.LOTSER AS ls
ON ls.ID = lb.IDLOTSER
LEFT OUTER JOIN SAMA1.dbo.ARTICO AS a
ON a.ID = ls.IDARTICO
LEFT OUTER JOIN SAMA1.sam.EXTUC AS uc
ON uc.CODICE = LEFT(ls.NUMSER, 6)
LEFT OUTER JOIN SAMA1.dbo.MTRASP AS mt
ON mt.ID = bt.IDMTRASP
LEFT OUTER JOIN SAMA1.dbo.NAZIONI AS n
ON n.ID = bt.IDNAZDEST
WHERE bt.ANNDOC >= YEAR(GETDATE())
AND bt.STATO IN ('P', 'D')
AND ls.NUMLOT <> '00000000000'
GROUP BY
ls.NUMLOT,
a.CODICE,
fr.DESCR,
LEFT(ls.NUMSER, 6),
bt.NUMDOC,
bt.DATDOC,
bt.ID,
bt.DESCRDEST,
bt.STATO,
n.CODICE,
n.DESCR,
bt.IDMTRASP,
mt.CODICE;
GO
CREATE OR ALTER VIEW dbo.py_XMag_ViewPackingList
AS
WITH PreparaPackingList AS (
SELECT
ls.NUMLOT,
a.CODICE,
fr.DESCR,
LEFT(ls.NUMSER, 6) AS UDC,
SUM(ls.QTTGIAI) AS Qta,
bt.NUMDOC,
bt.DATDOC,
bt.ID,
bt.DESCRDEST,
n.CODICE AS Expr1,
mt.CODICE + ' ' + n.DESCR AS NAZIONE,
bt.IDMTRASP
FROM SAMA1.dbo.BAMTES AS bt
INNER JOIN SAMA1.dbo.FATRIG AS fr
ON fr.IDBAM = bt.ID
INNER JOIN SAMA1.dbo.LOTTIBF AS lb
ON lb.IDFATRIG = fr.ID
INNER JOIN SAMA1.dbo.LOTSER AS ls
ON ls.ID = lb.IDLOTSER
LEFT OUTER JOIN SAMA1.dbo.ARTICO AS a
ON a.ID = ls.IDARTICO
LEFT OUTER JOIN SAMA1.sam.EXTUC AS uc
ON uc.CODICE = LEFT(ls.NUMSER, 6)
LEFT OUTER JOIN SAMA1.dbo.MTRASP AS mt
ON mt.ID = bt.IDMTRASP
LEFT OUTER JOIN SAMA1.dbo.NAZIONI AS n
ON n.ID = bt.IDNAZDEST
WHERE bt.ANNDOC >= YEAR(GETDATE())
AND bt.STATO = 'P'
AND bt.DATDOC > CAST((GETDATE() - 30) AS int)
AND ls.NUMLOT <> '00000000000'
GROUP BY
ls.NUMLOT,
a.CODICE,
fr.DESCR,
LEFT(ls.NUMSER, 6),
bt.NUMDOC,
bt.DATDOC,
bt.ID,
bt.DESCRDEST,
n.CODICE,
n.DESCR,
bt.IDMTRASP,
mt.CODICE
),
Base AS (
SELECT
prep.UDC AS Pallet,
prep.NUMLOT AS Lotto,
prep.CODICE AS Articolo,
prep.DESCR AS Descrizione,
prep.Qta,
prep.NUMDOC AS Documento,
prep.DATDOC AS DataDocumento,
prep.Expr1 AS CodNazione,
prep.NAZIONE,
CASE
WHEN prep.Expr1 = 'DE' THEN 10
WHEN prep.Expr1 = 'TH' THEN
CASE WHEN SUBSTRING(prep.DESCRDEST, 1, 2) = 'NA' THEN 11 ELSE 13 END
WHEN prep.Expr1 = 'MEX' THEN 12
ELSE 4
END AS Stato,
ISNULL(g.NumeroPallet, 0) AS PalletCella,
ISNULL(g.IDMagazzino, 1) AS Magazzino,
ISNULL(g.IDArea, 5) AS Area,
ISNULL(g.IDCella, 1000) AS Cella,
ISNULL(c.Ordinamento, 99999) AS Ordinamento,
ISNULL(c.Corsia + ' - ' + c.Colonna + ' - ' + c.Fila, 'Non scaff.') AS Ubicazione,
SUBSTRING(prep.DESCRDEST, 1, 2) AS DEST
FROM dbo.Celle AS c
INNER JOIN dbo.XMag_GiacenzaPallet AS g
ON c.ID = g.IDCella
RIGHT OUTER JOIN PreparaPackingList AS prep
ON g.BarcodePallet COLLATE SQL_Latin1_General_CP1_CI_AS = prep.UDC
WHERE prep.NUMLOT <> ''
GROUP BY
prep.Expr1,
prep.NAZIONE,
prep.UDC,
prep.NUMDOC,
prep.DATDOC,
prep.NUMLOT,
prep.CODICE,
prep.DESCR,
prep.Qta,
g.NumeroPallet,
g.IDMagazzino,
g.IDArea,
g.IDCella,
c.Ordinamento,
c.Corsia,
c.Colonna,
c.Fila,
prep.DESCRDEST
)
SELECT TOP (10000)
Base.Pallet,
Base.Lotto,
Base.Articolo,
Base.Descrizione,
Base.Qta,
Base.Documento,
Base.DataDocumento,
Base.CodNazione,
Base.NAZIONE,
Base.Stato,
Base.PalletCella,
Base.Magazzino,
Base.Area,
Base.Cella,
Base.Ordinamento,
Base.Ubicazione,
Base.DEST,
CASE
WHEN pr.Documento IS NOT NULL
AND pr.Documento = CAST(Base.Documento AS varchar(8))
THEN 1
ELSE 0
END AS IDStato
FROM Base
LEFT JOIN dbo.PyPickingListReservation AS pr
ON pr.ID = 1
AND NULLIF(LTRIM(RTRIM(pr.Documento)), '') IS NOT NULL
ORDER BY
CASE
WHEN pr.Documento IS NOT NULL
AND pr.Documento = CAST(Base.Documento AS varchar(8))
THEN 1
ELSE 0
END DESC,
Base.Documento,
Base.Ordinamento;
GO
PRINT 'Patch nazione destinazione opzionale applicata alle viste Python.';
GO

View File

@@ -0,0 +1,136 @@
/*
Patch Python WMS: ordine di prelievo per singola picking list
Prerequisito:
- eseguire prima apply_pickinglist_days_filter_patch.sql, che espone
DataDocumento nelle viste operative Python.
Effetti:
- crea una tabella Python-only con la sequenza personalizzata per Documento;
- crea una vista Python-only con l'ordine effettivo condiviso da backoffice e barcode;
- senza configurazione applica il default: Non scaff. prima degli scaffali;
- con configurazione applica i gruppi scelti e accoda in modo sicuro quelli mancanti;
- non modifica tabelle, viste o stored procedure legacy usate dal programma C#.
*/
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
IF OBJECT_ID(N'dbo.py_PickingListPrelievoSequenza', N'U') IS NULL
BEGIN
CREATE TABLE dbo.py_PickingListPrelievoSequenza (
ID bigint IDENTITY(1,1) NOT NULL
CONSTRAINT PK_py_PickingListPrelievoSequenza PRIMARY KEY,
Documento varchar(50) NOT NULL,
GruppoChiave varchar(160) NOT NULL,
GruppoEtichetta varchar(100) NOT NULL,
Ordine int NOT NULL,
InsUtente varchar(50) NULL,
InsDataOra datetime2(0) NOT NULL
CONSTRAINT DF_py_PickingListPrelievoSequenza_InsDataOra DEFAULT SYSDATETIME(),
ModUtente varchar(50) NULL,
ModDataOra datetime2(0) NULL,
DelUtente varchar(50) NULL,
DelDataOra datetime2(0) NULL,
CONSTRAINT CK_py_PickingListPrelievoSequenza_Ordine CHECK (Ordine >= 0)
);
END
GO
IF NOT EXISTS (
SELECT 1
FROM sys.indexes
WHERE object_id = OBJECT_ID(N'dbo.py_PickingListPrelievoSequenza', N'U')
AND name = N'UX_py_PickingListPrelievoSequenza_DocGruppo'
)
BEGIN
CREATE UNIQUE INDEX UX_py_PickingListPrelievoSequenza_DocGruppo
ON dbo.py_PickingListPrelievoSequenza (Documento, GruppoChiave)
WHERE DelDataOra IS NULL;
END
GO
IF NOT EXISTS (
SELECT 1
FROM sys.indexes
WHERE object_id = OBJECT_ID(N'dbo.py_PickingListPrelievoSequenza', N'U')
AND name = N'UX_py_PickingListPrelievoSequenza_DocOrdine'
)
BEGIN
CREATE UNIQUE INDEX UX_py_PickingListPrelievoSequenza_DocOrdine
ON dbo.py_PickingListPrelievoSequenza (Documento, Ordine)
WHERE DelDataOra IS NULL;
END
GO
CREATE OR ALTER VIEW dbo.py_ViewPackingListPrelievo
AS
WITH Gruppi AS (
SELECT
pl.*,
CASE
WHEN pl.Cella = 1000 THEN 'NON_SCAFF'
ELSE CONCAT(
'M:', pl.Magazzino,
'|A:', pl.Area,
'|C:',
LTRIM(RTRIM(
CASE
WHEN CHARINDEX(' - ', pl.Ubicazione) > 0
THEN LEFT(pl.Ubicazione, CHARINDEX(' - ', pl.Ubicazione) - 1)
ELSE pl.Ubicazione
END
))
)
END AS GruppoPrelievoChiave,
CASE
WHEN pl.Cella = 1000 THEN 'Non scaff.'
ELSE LTRIM(RTRIM(
CASE
WHEN CHARINDEX(' - ', pl.Ubicazione) > 0
THEN LEFT(pl.Ubicazione, CHARINDEX(' - ', pl.Ubicazione) - 1)
ELSE pl.Ubicazione
END
))
END AS GruppoPrelievo,
CASE
WHEN pl.Cella = 1000 THEN 0
ELSE 1000 + ISNULL(pl.Ordinamento, 99999)
END AS OrdineGruppoDefault
FROM dbo.py_ViewPackingListRestante AS pl
), Sequenza AS (
SELECT
g.*,
s.Ordine AS OrdineGruppoConfigurato,
CASE
WHEN EXISTS (
SELECT 1
FROM dbo.py_PickingListPrelievoSequenza AS any_s
WHERE any_s.Documento COLLATE Latin1_General_CI_AS =
CAST(g.Documento AS varchar(50)) COLLATE Latin1_General_CI_AS
AND any_s.DelDataOra IS NULL
) THEN 1
ELSE 0
END AS HaSequenzaPersonalizzata
FROM Gruppi AS g
LEFT JOIN dbo.py_PickingListPrelievoSequenza AS s
ON s.Documento COLLATE Latin1_General_CI_AS =
CAST(g.Documento AS varchar(50)) COLLATE Latin1_General_CI_AS
AND s.GruppoChiave COLLATE Latin1_General_CI_AS =
g.GruppoPrelievoChiave COLLATE Latin1_General_CI_AS
AND s.DelDataOra IS NULL
)
SELECT
Sequenza.*,
CASE
WHEN OrdineGruppoConfigurato IS NOT NULL THEN OrdineGruppoConfigurato
WHEN HaSequenzaPersonalizzata = 1 THEN 100000 + OrdineGruppoDefault
ELSE OrdineGruppoDefault
END AS OrdinePrelievoGruppo
FROM Sequenza;
GO
PRINT 'Patch sequenza prelievo Python applicata.';
GO

View File

@@ -48,7 +48,7 @@ LEFT OUTER JOIN SAMA1.sam.EXTUC
LEFT OUTER JOIN SAMA1.dbo.MTRASP
ON SAMA1.dbo.BAMTES.IDMTRASP = SAMA1.dbo.MTRASP.ID
WHERE
SAMA1.dbo.BAMTES.ANNDOC >= YEAR(GETDATE()) - 1
SAMA1.dbo.BAMTES.ANNDOC >= YEAR(GETDATE())
AND SAMA1.dbo.BAMTES.STATO IN ('P', 'D')
AND SAMA1.dbo.LOTSER.NUMLOT <> '00000000000'
GROUP BY

View File

@@ -10,9 +10,13 @@ cross-loop errors.
from __future__ import annotations
import asyncio
import inspect
import json
import logging
import os
import time
import urllib.parse
from pathlib import Path
from typing import Any, Dict, Optional
from sqlalchemy import text
@@ -22,6 +26,59 @@ from version_info import module_version
__version__ = module_version(__name__)
QUERY_PROFILER_LOG = Path(__file__).with_name("warehouse_query_profiler.log")
QUERY_PROFILER_CONFIG_PATH = Path(__file__).with_name("db_connection.json")
def _load_query_profiler_config() -> dict[str, Any]:
"""Read profiler settings from db_connection.json, with env overrides."""
config: dict[str, Any] = {
"enabled": False,
"slow_ms": 0,
"sql_limit": 4000,
}
try:
data = json.loads(QUERY_PROFILER_CONFIG_PATH.read_text(encoding="utf-8"))
section = data.get("query_profiler") if isinstance(data, dict) else None
if isinstance(section, dict):
config.update(section)
except Exception:
pass
if "WAREHOUSE_QUERY_PROFILER" in os.environ:
config["enabled"] = os.environ.get("WAREHOUSE_QUERY_PROFILER", "1").strip().lower() not in {
"0",
"false",
"no",
"off",
}
if "WAREHOUSE_QUERY_PROFILER_SLOW_MS" in os.environ:
config["slow_ms"] = os.environ.get("WAREHOUSE_QUERY_PROFILER_SLOW_MS", "0")
if "WAREHOUSE_QUERY_PROFILER_SQL_LIMIT" in os.environ:
config["sql_limit"] = os.environ.get("WAREHOUSE_QUERY_PROFILER_SQL_LIMIT", "4000")
return config
def _query_profiler_enabled() -> bool:
value = _load_query_profiler_config().get("enabled", False)
if isinstance(value, str):
return value.strip().lower() not in {"0", "false", "no", "off"}
return bool(value)
def _query_profiler_slow_ms() -> float:
try:
return float(_load_query_profiler_config().get("slow_ms", 0) or 0)
except Exception:
return 0.0
def _query_profiler_sql_limit() -> int:
try:
return int(_load_query_profiler_config().get("sql_limit", 4000) or 4000)
except Exception:
return 4000
try:
import pyodbc
@@ -76,6 +133,83 @@ def make_mssql_dsn(
return f"mssql+aioodbc:///?odbc_connect={urllib.parse.quote_plus(odbc)}"
def _compact_sql(sql: str, *, limit: int | None = None) -> str:
"""Collapse SQL whitespace so profiler entries stay readable."""
if limit is None:
limit = _query_profiler_sql_limit()
text_value = " ".join(str(sql or "").split())
if limit > 0 and len(text_value) > limit:
return text_value[:limit] + "...<truncated>"
return text_value
def _profile_params(params: Optional[Dict[str, Any]]) -> str:
"""Serialize SQL parameters for diagnostics."""
if not params:
return "{}"
try:
return _dumps(params)
except Exception:
return repr(params)
def _query_caller() -> str:
"""Return the first external Python frame that triggered the DB call."""
current_file = Path(__file__).resolve()
for frame in inspect.stack(context=0)[2:]:
try:
frame_file = Path(frame.filename).resolve()
except Exception:
continue
if frame_file == current_file:
continue
return f"{frame_file.name}:{frame.lineno}:{frame.function}"
return "unknown"
def _append_query_profile(
*,
method: str,
elapsed_ms: float,
rows: int | None,
rowcount: int | None,
commit: bool,
ok: bool,
sql: str,
params: Optional[Dict[str, Any]],
caller: str,
error: str = "",
) -> None:
"""Append one query timing line to the local profiler log."""
if not _query_profiler_enabled():
return
slow_ms = _query_profiler_slow_ms()
if ok and slow_ms > 0 and elapsed_ms < slow_ms:
return
try:
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
row_info = f"rows={rows}" if rows is not None else f"rowcount={rowcount}"
lines = [
(
f"{timestamp} | {elapsed_ms:.3f} ms | {method} | "
f"{row_info} | commit={int(commit)} | ok={int(ok)} | caller={caller}"
),
f"PARAMS: {_profile_params(params)}",
f"SQL: {_compact_sql(sql)}",
]
if error:
lines.append(f"ERROR: {error}")
with QUERY_PROFILER_LOG.open("a", encoding="utf-8") as handle:
handle.write("\n".join(lines) + "\n---\n")
except Exception:
# Profiling must never interfere with warehouse operations.
pass
class AsyncMSSQLClient:
"""Thin async query client for SQL Server.
@@ -152,23 +286,83 @@ class AsyncMSSQLClient:
"""
await self._ensure_engine()
t0 = time.perf_counter()
async with (self._engine.begin() if commit else self._engine.connect()) as conn:
res = await conn.execute(text(sql), params or {})
rows = res.fetchall()
cols = list(res.keys())
caller = _query_caller()
try:
async with (self._engine.begin() if commit else self._engine.connect()) as conn:
res = await conn.execute(text(sql), params or {})
rows = res.fetchall()
cols = list(res.keys())
except Exception as exc:
elapsed_ms = round((time.perf_counter() - t0) * 1000, 3)
_append_query_profile(
method="query_json",
elapsed_ms=elapsed_ms,
rows=None,
rowcount=None,
commit=commit,
ok=False,
sql=sql,
params=params,
caller=caller,
error=repr(exc),
)
raise
if as_dict_rows:
rows_out = [dict(zip(cols, row)) for row in rows]
else:
rows_out = [list(row) for row in rows]
elapsed_ms = round((time.perf_counter() - t0) * 1000, 3)
_append_query_profile(
method="query_json",
elapsed_ms=elapsed_ms,
rows=len(rows_out),
rowcount=None,
commit=commit,
ok=True,
sql=sql,
params=params,
caller=caller,
)
return {
"columns": cols,
"rows": rows_out,
"elapsed_ms": round((time.perf_counter() - t0) * 1000, 3),
"elapsed_ms": elapsed_ms,
}
async def exec(self, sql: str, params: Optional[Dict[str, Any]] = None, *, commit: bool = False) -> int:
"""Execute a DML statement and return its row count."""
await self._ensure_engine()
async with (self._engine.begin() if commit else self._engine.connect()) as conn:
res = await conn.execute(text(sql), params or {})
return res.rowcount or 0
t0 = time.perf_counter()
caller = _query_caller()
try:
async with (self._engine.begin() if commit else self._engine.connect()) as conn:
res = await conn.execute(text(sql), params or {})
rowcount = res.rowcount or 0
except Exception as exc:
elapsed_ms = round((time.perf_counter() - t0) * 1000, 3)
_append_query_profile(
method="exec",
elapsed_ms=elapsed_ms,
rows=None,
rowcount=None,
commit=commit,
ok=False,
sql=sql,
params=params,
caller=caller,
error=repr(exc),
)
raise
elapsed_ms = round((time.perf_counter() - t0) * 1000, 3)
_append_query_profile(
method="exec",
elapsed_ms=elapsed_ms,
rows=None,
rowcount=rowcount,
commit=commit,
ok=True,
sql=sql,
params=params,
caller=caller,
)
return rowcount

View File

@@ -68,6 +68,7 @@ class BarcodeClientApp:
self._auto_advance_id: str | None = None
self._pallet_auto_focus_id: str | None = None
self._paused_queue_id: int | None = None
self._paused_priority_state: BarcodeViewState | None = None
self._pallet_user_input_serial = 0
self._priority_loaded_input_serial = 0
self._priority_expected_pallet = ""
@@ -518,11 +519,12 @@ class BarcodeClientApp:
def _on_destination_enter(self, _event=None) -> str:
pallet = str(self.scanned_var.get() or "").strip()
destination = str(self.destination_var.get() or "").strip()
if pallet and destination:
self._submit()
else:
if not pallet:
self._focus_primary_input()
else:
self.info1_var.set("F3 per associare, F4 per disassociare")
self.status_band.configure(bg=self._status_colors["yellow"])
self._focus_destination_input()
return "break"
def _on_unload_key(self, _event=None) -> str:
@@ -531,6 +533,12 @@ class BarcodeClientApp:
def _on_escape_key(self, _event=None) -> str:
if self._is_priority_pause() and self._paused_queue_id is not None:
if self._paused_priority_state is not None:
state = self.service.resume_priority_state(self._paused_priority_state)
self._paused_priority_state = None
self._paused_queue_id = None
self._apply_state(state)
return "break"
self._start_queue(self._paused_queue_id, allow_during_pause=True)
return "break"
@@ -542,15 +550,18 @@ class BarcodeClientApp:
mode = getattr(self.service.state, "mode", "")
if mode == "priority_high":
self._paused_queue_id = 1
self._paused_priority_state = self.service.state
self._apply_state(self.service.begin_priority_pause(1))
return
if mode == "priority_low":
self._paused_queue_id = 0
self._paused_priority_state = self.service.state
self._apply_state(self.service.begin_priority_pause(0))
return
resumable_queue = self._resumable_queue_from_state(self.service.state)
if resumable_queue is not None:
self._paused_queue_id = resumable_queue
self._paused_priority_state = None
self._apply_state(self.service.begin_priority_pause(resumable_queue))
return
self._submit()

View File

@@ -26,7 +26,7 @@ SELECT TOP (1)
Ubicazione,
Ordinamento,
IDStato
FROM dbo.py_XMag_ViewPackingList AS pl
FROM dbo.py_ViewPackingListPrelievo AS pl
WHERE Ordinamento > 0
AND IDStato = :id_stato
AND NOT EXISTS (
@@ -39,7 +39,7 @@ WHERE Ordinamento > 0
AND s.IDStato = pl.IDStato
AND s.Risolto = 0
)
ORDER BY Ordinamento;
ORDER BY OrdinePrelievoGruppo, Ordinamento, Pallet;
"""
SQL_PICKING_BY_PALLET = """
@@ -53,9 +53,9 @@ SELECT TOP (1)
Ubicazione,
Ordinamento,
IDStato
FROM dbo.py_XMag_ViewPackingList
FROM dbo.py_ViewPackingListPrelievo
WHERE Pallet COLLATE Latin1_General_CI_AS = :pallet COLLATE Latin1_General_CI_AS
ORDER BY Ordinamento;
ORDER BY OrdinePrelievoGruppo, Ordinamento, Pallet;
"""
SQL_TRACE_BY_PALLET = """
@@ -107,15 +107,30 @@ ORDER BY g.BarcodePallet;
SQL_CLOSED_PICKING_BY_PALLET = """
SELECT TOP (1)
BarcodePallet,
NumeroPallet,
IDMagazzino,
IDArea,
IDCella,
IDStato
FROM dbo.XMag_GiacenzaPalletPlistChiuse
WHERE BarcodePallet COLLATE Latin1_General_CI_AS = :pallet COLLATE Latin1_General_CI_AS
ORDER BY IDCella DESC;
g.BarcodePallet,
g.NumeroPallet,
g.IDMagazzino,
g.IDArea,
g.IDCella,
g.IDStato
FROM dbo.XMag_GiacenzaPallet AS g
WHERE g.IDCella <> 9999
AND g.BarcodePallet COLLATE Latin1_General_CI_AS = :pallet COLLATE Latin1_General_CI_AS
AND EXISTS (
SELECT 1
FROM SAMA1.dbo.LOTSER AS ls
INNER JOIN SAMA1.dbo.LOTTIBF AS lb
ON lb.IDLOTSER = ls.ID
INNER JOIN SAMA1.dbo.FATRIG AS fr
ON lb.IDFATRIG = fr.ID
INNER JOIN SAMA1.dbo.BAMTES AS bt
ON bt.ID = fr.IDBAM
WHERE bt.ANNDOC >= 2011
AND bt.STATO = 'D'
AND LEFT(ls.NUMSER, 6) COLLATE Latin1_General_CI_AS =
:pallet COLLATE Latin1_General_CI_AS
)
ORDER BY g.IDCella DESC;
"""
SQL_RESOLVE_PHYSICAL_CELL = """
@@ -142,6 +157,7 @@ SQL_LEGACY_MOVE = """
SET NOCOUNT ON;
SET XACT_ABORT ON;
DECLARE @RC int = 0;
DECLARE @RefreshPickingReservation bit = :refresh_picking_reservation;
EXEC dbo.sp_xMagGestioneMagazziniPallet
@IDOperatore = :id_operatore,
@@ -150,7 +166,10 @@ EXEC dbo.sp_xMagGestioneMagazziniPallet
@NumeroCella = :numero_cella,
@RC = @RC OUTPUT;
EXEC dbo.py_sp_ControllaPrenotazionePackingListPalletNew;
IF @RefreshPickingReservation = 1
BEGIN
EXEC dbo.py_sp_ControllaPrenotazionePackingListPalletNew;
END;
SELECT
@RC AS RC,
@@ -408,6 +427,7 @@ class BarcodeRepository:
barcode_cella: str,
barcode_pallet: str,
numero_cella: int,
refresh_picking_reservation: bool = True,
) -> LegacyMoveResult:
"""Execute the same stored procedure used by the C# barcode form."""
@@ -416,6 +436,7 @@ class BarcodeRepository:
"barcode_cella": str(barcode_cella or "").strip(),
"barcode_pallet": str(barcode_pallet or "").strip(),
"numero_cella": int(numero_cella),
"refresh_picking_reservation": 1 if refresh_picking_reservation else 0,
}
log_runtime_event(
"Barcode WMS",
@@ -423,7 +444,8 @@ class BarcodeRepository:
"MOVE START "
f"operator={params['id_operatore']} "
f"cell={params['barcode_cella']} "
f"pallet={params['barcode_pallet']}"
f"pallet={params['barcode_pallet']} "
f"refresh_pl={params['refresh_picking_reservation']}"
),
)
try:

View File

@@ -2,7 +2,7 @@
from __future__ import annotations
from dataclasses import dataclass
from dataclasses import dataclass, replace
from typing import Literal
from barcode_repository import BarcodeRepository, LegacyMoveResult
@@ -115,6 +115,15 @@ class BarcodeService:
)
return self._state
def resume_priority_state(self, state: BarcodeViewState) -> BarcodeViewState:
"""Restore the exact picking pallet that was visible before a local pause."""
if state.mode not in ("priority_high", "priority_low"):
return self._state
self._current_priority_state = 1 if state.mode == "priority_high" else 0
self._state = replace(state, scanned_pallet="", auto_advance_delay_ms=0)
return self._state
async def skip_current_picking_pallet(self) -> BarcodeActionResult:
"""Mark the currently proposed picking pallet as skipped and advance."""
@@ -277,7 +286,11 @@ class BarcodeService:
target_display = resolved_cell.ubicazione or destination
current_location = await self.repository.fetch_current_location_by_pallet(pallet)
picking_before_move = await self.repository.fetch_picking_by_pallet(pallet)
picking_before_move = (
None
if current_location
else await self.repository.fetch_picking_by_pallet(pallet)
)
if not current_location and not picking_before_move:
if is_direct_load:
trace_row = await self.repository.fetch_trace_by_pallet(pallet)
@@ -297,6 +310,7 @@ class BarcodeService:
barcode_cella=self.NON_SCAFFALATA_BARCODE,
barcode_pallet=pallet,
numero_cella=int(self.NON_SCAFFALATA_BARCODE),
refresh_picking_reservation=False,
)
current_location = await self.repository.fetch_current_location_by_pallet(pallet)
try:
@@ -354,6 +368,7 @@ class BarcodeService:
barcode_cella=target_barcode,
barcode_pallet=pallet,
numero_cella=target_numero_cella,
refresh_picking_reservation=is_picking_unload,
)
final_location = await self.repository.fetch_current_location_by_pallet(pallet)
@@ -476,7 +491,45 @@ class BarcodeService:
self._state.status_color = self.RED
return BarcodeActionResult(False, self._state, self._state.status_text)
return await self.submit(scanned_pallet=pallet, destination_barcode=self.NON_SCAFFALATA_BARCODE)
await self.repository.execute_legacy_move(
operator_id=self.operator_id,
barcode_cella=self.NON_SCAFFALATA_BARCODE,
barcode_pallet=pallet,
numero_cella=int(self.NON_SCAFFALATA_BARCODE),
refresh_picking_reservation=False,
)
final_location = await self.repository.fetch_current_location_by_pallet(pallet)
try:
final_cell = int((final_location or {}).get("IDCella") or 0)
except Exception:
final_cell = 0
if final_cell != 1000:
self._state.scanned_pallet = pallet
self._state.destination_barcode = source
self._state.status_text = "Movimento non confermato: la UDC non risulta non scaffalata."
self._state.status_color = self.RED
return BarcodeActionResult(False, self._state, self._state.status_text)
try:
self._state = await self._build_post_move_state(
barcode_pallet=pallet,
destination_barcode=self.NON_SCAFFALATA_BARCODE,
destination_display=self.CONVENTIONAL_LOCATION_BY_CELL[1000],
last_priority_state=self._current_priority_state,
auto_advance_delay_ms=0,
)
except Exception as exc:
log_exception("Barcode WMS", exc, context=f"post source-check unload pallet={pallet} source={source}")
self._state = BarcodeViewState(
mode="confirm",
queue_label="Conferma movimento",
status_text="Movimento eseguito. Dettagli non aggiornati.",
status_color=self.GREEN_YELLOW,
destination_barcode=self.NON_SCAFFALATA_BARCODE,
scanned_pallet=pallet,
)
return BarcodeActionResult(True, self._state, self._state.status_text)
async def _block_closed_picking_free_move(
self,

View File

@@ -26,6 +26,11 @@ DEFAULT_DB_CONFIG: dict[str, Any] = {
"driver": "ODBC Driver 17 for SQL Server",
"trust_server_certificate": True,
"encrypt": "",
"query_profiler": {
"enabled": False,
"slow_ms": 0,
"sql_limit": 4000,
},
}

543
diagnostica.py Normal file
View File

@@ -0,0 +1,543 @@
"""Operational diagnostic checks for warehouse data quadrature."""
from __future__ import annotations
import tkinter as tk
from datetime import datetime
from tkinter import filedialog, messagebox, ttk
from typing import Any
import customtkinter as ctk
from busy_overlay import InlineBusyOverlay
from gestione_aree import AsyncRunner
from locale_text import load_locale_catalog, text as loc_text
from runtime_support import log_exception
from ui_tables import style_treeview, zebra_tag
from ui_theme import theme_color, theme_font, theme_section, theme_value
from version_info import module_version, versioned_title
from window_placement import place_window_fullsize_below_parent_later
__version__ = module_version(__name__)
SQL_SHIPPED_NOT_DISASSOCIATED = """
SELECT DISTINCT
LTRIM(RTRIM(g.BarcodePallet)) AS UDC,
CAST('Spedita ma non disassociata' AS varchar(64)) AS Diagnostica,
CONCAT(
RTRIM(c.Corsia),
' - ',
RTRIM(CAST(c.Colonna AS varchar(32))),
' - ',
RTRIM(CAST(c.Fila AS varchar(32)))
) AS NomeSimbolicoCella
FROM dbo.XMag_GiacenzaPallet AS g
INNER JOIN dbo.Celle AS c
ON c.ID = g.IDCella
INNER JOIN dbo.py_XMag_ViewPackingListStorico AS chiuse
ON LTRIM(RTRIM(chiuse.Pallet)) COLLATE Latin1_General_CI_AS =
LTRIM(RTRIM(g.BarcodePallet)) COLLATE Latin1_General_CI_AS
WHERE chiuse.StatoDocumento = 'D'
AND g.IDCella <> 9999
ORDER BY NomeSimbolicoCella, UDC;
"""
SQL_MULTIPLE_UDC_BY_CELL = """
WITH celle_multiple AS (
SELECT
g.IDCella
FROM dbo.XMag_GiacenzaPallet AS g
WHERE g.IDCella NOT IN (1000, 9999)
GROUP BY g.IDCella
HAVING COUNT(DISTINCT LTRIM(RTRIM(g.BarcodePallet))) > 1
)
SELECT
CONCAT(
RTRIM(c.Corsia),
' - ',
RTRIM(CAST(c.Colonna AS varchar(32))),
' - ',
RTRIM(CAST(c.Fila AS varchar(32)))
) AS CELLA,
LTRIM(RTRIM(g.BarcodePallet)) AS UDC
FROM dbo.XMag_GiacenzaPallet AS g
INNER JOIN celle_multiple AS cm
ON cm.IDCella = g.IDCella
INNER JOIN dbo.Celle AS c
ON c.ID = g.IDCella
ORDER BY CELLA, UDC;
"""
SQL_SAM_UDC_NOT_IN_WMS = """
WITH recent_lotser AS (
SELECT TOP (5000)
ls.ID,
LEFT(ls.NUMSER, 6) AS UDC,
ls.NUMLOT AS Lotto,
ls.IDARTICO
FROM SAMA1.dbo.LOTSER AS ls
WHERE NULLIF(LTRIM(RTRIM(LEFT(ls.NUMSER, 6))), '') IS NOT NULL
AND TRY_CONVERT(int, LTRIM(RTRIM(LEFT(ls.NUMSER, 6)))) > 0
AND LEN(LTRIM(RTRIM(LEFT(ls.NUMSER, 6)))) = 6
AND ls.NUMLOT LIKE CONCAT('P', RIGHT(CONVERT(varchar(4), YEAR(GETDATE())), 2), '%')
ORDER BY ls.ID DESC
), sam_udc AS (
SELECT
LTRIM(RTRIM(r.UDC)) AS UDC,
MIN(r.Lotto) AS Lotto,
MIN(a.CODICE) AS Prodotto,
MIN(a.DESCR) AS Descrizione,
MAX(r.ID) AS UltimoIDLotSer
FROM recent_lotser AS r
INNER JOIN SAMA1.dbo.ARTICO AS a
ON a.ID = r.IDARTICO
GROUP BY LTRIM(RTRIM(r.UDC))
)
SELECT TOP (100)
s.UDC,
s.Lotto,
s.Prodotto,
s.Descrizione
FROM sam_udc AS s
WHERE NOT EXISTS (
SELECT 1
FROM dbo.MagazziniPallet AS mp
WHERE LTRIM(RTRIM(mp.Attributo)) COLLATE Latin1_General_CI_AS =
s.UDC COLLATE Latin1_General_CI_AS
)
ORDER BY s.UltimoIDLotSer DESC;
"""
SHIPPED_NOT_DISASSOCIATED_COLUMNS = (
("UDC", "diagnostics.col.udc", "UDC", 130, "w"),
(
"Diagnostica",
"diagnostics.col.shipped_not_disassociated",
"Spedita ma non disassociata",
280,
"w",
),
("NomeSimbolicoCella", "diagnostics.col.symbolic_cell", "Nome simbolico cella", 240, "w"),
)
MULTIPLE_UDC_COLUMNS = (
("CELLA", "diagnostics.col.cell", "CELLA", 220, "w"),
("UDC", "diagnostics.col.udc", "UDC", 160, "w"),
)
SAM_UDC_NOT_IN_WMS_COLUMNS = (
("UDC", "diagnostics.col.udc", "UDC", 130, "w"),
("Lotto", "diagnostics.col.lot", "Lotto", 160, "w"),
("Prodotto", "diagnostics.col.product", "Prodotto", 150, "w"),
("Descrizione", "diagnostics.col.description", "Descrizione", 360, "w"),
)
def _rows_to_dicts(res: dict[str, Any] | None) -> list[dict[str, Any]]:
if not isinstance(res, dict):
return []
rows = res.get("rows") or []
cols = res.get("columns") or []
if rows and isinstance(rows[0], dict):
return [row for row in rows if isinstance(row, dict)]
out: list[dict[str, Any]] = []
for row in rows:
if isinstance(row, (list, tuple)) and cols:
out.append({str(cols[i]): row[i] for i in range(min(len(cols), len(row)))})
return out
class DiagnosticaWindow(ctk.CTkToplevel):
"""Window collecting read-only diagnostic queries."""
def __init__(self, parent: tk.Widget, db_client, session=None):
super().__init__(parent)
self.db_client = db_client
self.session = session
self._theme = theme_section("diagnostics_window", theme_section("search_window", {}))
self._locale_catalog = load_locale_catalog()
self._async = AsyncRunner(self)
self._busy = InlineBusyOverlay(self, self._theme)
self._load_in_progress = False
self._current_check = ""
self.var_status = tk.StringVar(value="")
self._last_export_slug = "diagnostica"
self._current_columns = SHIPPED_NOT_DISASSOCIATED_COLUMNS
self._column_titles: dict[str, str] = {}
self._sort_column = ""
self._sort_descending = False
self.title(
versioned_title(
loc_text("diagnostics.title", catalog=self._locale_catalog, default="Diagnostica"),
__name__,
)
)
self.geometry(str(theme_value(self._theme, "window_geometry", "1100x680")))
minsize = theme_value(self._theme, "window_minsize", [900, 560])
self.minsize(int(minsize[0]), int(minsize[1]))
try:
self.configure(fg_color=theme_color(self._theme, "window_fg_color", ("#efefef", "#2f2f2f")))
except Exception:
pass
self._build_ui()
def _is_alive(self) -> bool:
try:
return bool(self.winfo_exists())
except tk.TclError:
return False
def _hide_busy_safe(self) -> None:
try:
self._busy.hide()
except Exception as exc:
log_exception(__name__, exc, context="hide busy diagnostica")
def _build_ui(self) -> None:
self.grid_rowconfigure(2, weight=1)
self.grid_columnconfigure(0, weight=1)
top = ctk.CTkFrame(
self,
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(3, weight=1)
button_font = theme_font(self._theme, "toolbar_button_font", ("Segoe UI", 10, "bold"))
ctk.CTkButton(
top,
text=loc_text(
"diagnostics.button.shipped_not_disassociated",
catalog=self._locale_catalog,
default="UDC spedite non disassociate",
),
command=self._load_shipped_not_disassociated,
font=button_font,
).grid(row=0, column=0, sticky="w", padx=(0, 8))
ctk.CTkButton(
top,
text=loc_text(
"diagnostics.button.multiple_udc_by_cell",
catalog=self._locale_catalog,
default="Celle con multiple UDC",
),
command=self._load_multiple_udc_by_cell,
font=button_font,
).grid(row=0, column=1, sticky="w", padx=(0, 8))
ctk.CTkButton(
top,
text=loc_text(
"diagnostics.button.sam_udc_not_in_wms",
catalog=self._locale_catalog,
default="Ultime UDC SAM non in WMS",
),
command=self._load_sam_udc_not_in_wms,
font=button_font,
).grid(row=0, column=2, sticky="w", padx=(0, 8))
self.btn_export = ctk.CTkButton(
top,
text=loc_text("diagnostics.button.export", catalog=self._locale_catalog, default="Esporta XLSX"),
command=self._export_xlsx,
font=button_font,
state="disabled",
)
self.btn_export.grid(row=0, column=4, sticky="e")
ctk.CTkLabel(
self,
textvariable=self.var_status,
anchor="w",
font=theme_font(self._theme, "status_font", ("Segoe UI", 10, "bold")),
).grid(row=1, column=0, sticky="ew", padx=10, pady=(0, 6))
wrap = ctk.CTkFrame(self)
wrap.grid(row=2, column=0, sticky="nsew", padx=8, pady=(0, 8))
wrap.grid_rowconfigure(0, weight=1)
wrap.grid_columnconfigure(0, weight=1)
self.tree = ttk.Treeview(wrap, show="headings")
self._configure_tree_columns(self._current_columns)
style_treeview(
self.tree,
style_name="Diagnostics.Treeview",
rowheight=22,
font=("", 9),
heading_font=("", 9, "bold"),
)
sy = ttk.Scrollbar(wrap, orient="vertical", command=self.tree.yview)
sx = ttk.Scrollbar(wrap, orient="horizontal", command=self.tree.xview)
self.tree.configure(yscrollcommand=sy.set, xscrollcommand=sx.set)
self.tree.grid(row=0, column=0, sticky="nsew")
sy.grid(row=0, column=1, sticky="ns")
sx.grid(row=1, column=0, sticky="ew")
def _set_status(self, text: str) -> None:
self.var_status.set(text)
def _configure_tree_columns(self, columns) -> None:
self.tree.delete(*self.tree.get_children(""))
keys = tuple(str(col[0]) for col in columns)
self.tree.configure(columns=keys, show="headings")
self._sort_column = ""
self._sort_descending = False
self._column_titles = {}
for key, locale_key, default, width, anchor in columns:
title = loc_text(str(locale_key), catalog=self._locale_catalog, default=str(default))
self._column_titles[str(key)] = title
self.tree.heading(
key,
text=title,
command=lambda col=str(key): self._sort_by_column(col),
)
self.tree.column(key, width=int(width), anchor=str(anchor), stretch=True)
def _refresh_sort_headings(self) -> None:
for key, title in self._column_titles.items():
suffix = ""
if key == self._sort_column:
suffix = " v" if self._sort_descending else " ^"
self.tree.heading(key, text=f"{title}{suffix}", command=lambda col=key: self._sort_by_column(col))
def _sort_key(self, value: Any) -> tuple[int, Any]:
text = str(value or "").strip()
if not text:
return (2, "")
normalized = text.replace(".", "").replace(",", ".")
try:
return (0, float(normalized))
except ValueError:
return (1, text.casefold())
def _sort_by_column(self, column: str) -> None:
columns = list(self.tree["columns"])
if column not in columns:
return
descending = not self._sort_descending if self._sort_column == column else False
col_index = columns.index(column)
rows = []
for iid in self.tree.get_children(""):
values = self.tree.item(iid, "values")
value = values[col_index] if col_index < len(values) else ""
rows.append((self._sort_key(value), iid))
rows.sort(key=lambda item: item[0], reverse=descending)
for index, (_key, iid) in enumerate(rows):
self.tree.move(iid, "", index)
self.tree.item(iid, tags=(zebra_tag(index),))
self._sort_column = column
self._sort_descending = descending
self._refresh_sort_headings()
def _load_shipped_not_disassociated(self) -> None:
self._run_check(
key="shipped_not_disassociated",
sql=SQL_SHIPPED_NOT_DISASSOCIATED,
columns=SHIPPED_NOT_DISASSOCIATED_COLUMNS,
busy_text=loc_text(
"diagnostics.busy.shipped_not_disassociated",
catalog=self._locale_catalog,
default="Cerco UDC spedite ma ancora in cella...",
),
)
def _load_multiple_udc_by_cell(self) -> None:
self._run_check(
key="multiple_udc_by_cell",
sql=SQL_MULTIPLE_UDC_BY_CELL,
columns=MULTIPLE_UDC_COLUMNS,
busy_text=loc_text(
"diagnostics.busy.multiple_udc_by_cell",
catalog=self._locale_catalog,
default="Cerco celle con multiple UDC...",
),
)
def _load_sam_udc_not_in_wms(self) -> None:
self._run_check(
key="sam_udc_not_in_wms",
sql=SQL_SAM_UDC_NOT_IN_WMS,
columns=SAM_UDC_NOT_IN_WMS_COLUMNS,
busy_text=loc_text(
"diagnostics.busy.sam_udc_not_in_wms",
catalog=self._locale_catalog,
default="Cerco le ultime UDC presenti in SAM ma non ancora in WMS...",
),
)
def _run_check(self, *, key: str, sql: str, columns, busy_text: str) -> None:
if self._load_in_progress or not self._is_alive():
return
async def job():
return await self.db_client.query_json(sql, as_dict_rows=True)
self._load_in_progress = True
self._current_check = key
self._last_export_slug = key
self._current_columns = columns
self._configure_tree_columns(columns)
try:
self.btn_export.configure(state="disabled")
except Exception:
pass
try:
self._busy.show(busy_text)
self._async.run(job(), self._on_loaded, self._on_error)
except Exception as exc:
self._load_in_progress = False
self._hide_busy_safe()
log_exception(__name__, exc, context=f"avvio diagnostica {key}")
messagebox.showerror(
loc_text("diagnostics.title", catalog=self._locale_catalog, default="Diagnostica"),
str(exc),
parent=self if self._is_alive() else None,
)
def _on_loaded(self, res: dict[str, Any] | None) -> None:
self._load_in_progress = False
self._hide_busy_safe()
if not self._is_alive():
return
try:
rows = _rows_to_dicts(res)
self.tree.delete(*self.tree.get_children(""))
keys = [str(col[0]) for col in self._current_columns]
for index, row in enumerate(rows):
self.tree.insert(
"",
"end",
values=tuple(row.get(key) or "" for key in keys),
tags=(zebra_tag(index),),
)
try:
self.btn_export.configure(state="normal" if rows else "disabled")
except Exception:
pass
self._set_status(
loc_text(
"diagnostics.status.rows",
catalog=self._locale_catalog,
default="Righe trovate: {count}",
).format(count=len(rows))
)
except Exception as exc:
log_exception(__name__, exc, context="render diagnostica")
messagebox.showerror(
loc_text("diagnostics.title", catalog=self._locale_catalog, default="Diagnostica"),
str(exc),
parent=self,
)
def _on_error(self, exc: BaseException) -> None:
self._load_in_progress = False
self._hide_busy_safe()
log_exception(__name__, exc, context=f"query diagnostica {self._current_check}")
if not self._is_alive():
return
messagebox.showerror(
loc_text("diagnostics.title", catalog=self._locale_catalog, default="Diagnostica"),
str(exc),
parent=self,
)
def _export_xlsx(self) -> None:
"""Export the currently visible diagnostic grid to an Excel file."""
rows = [self.tree.item(iid, "values") for iid in self.tree.get_children("")]
if not rows:
messagebox.showinfo(
loc_text("diagnostics.export.title", catalog=self._locale_catalog, default="Esporta"),
loc_text(
"diagnostics.export.empty",
catalog=self._locale_catalog,
default="Non ci sono righe da esportare.",
),
parent=self,
)
return
try:
from openpyxl import Workbook
from openpyxl.styles import Alignment, Font
from openpyxl.utils import get_column_letter
except Exception as exc:
messagebox.showerror(
loc_text("diagnostics.export.title", catalog=self._locale_catalog, default="Esporta"),
loc_text(
"diagnostics.export.dep",
catalog=self._locale_catalog,
default="Per l'esportazione serve 'openpyxl' (pip install openpyxl).",
).format(error=exc),
parent=self,
)
return
ts = datetime.now().strftime("%Y%m%d_%H%M")
default_name = f"{self._last_export_slug}_{ts}.xlsx"
path = filedialog.asksaveasfilename(
parent=self,
title=loc_text("diagnostics.export.title", catalog=self._locale_catalog, default="Esporta"),
defaultextension=".xlsx",
filetypes=[("Excel Workbook", "*.xlsx")],
initialfile=default_name,
)
if not path:
return
try:
wb = Workbook()
ws = wb.active
ws.title = "Diagnostica"
columns = list(self.tree["columns"])
headers = [str(self.tree.heading(col, "text") or col) for col in columns]
for col_index, header in enumerate(headers, start=1):
cell = ws.cell(row=1, column=col_index, value=header)
cell.font = Font(bold=True)
cell.alignment = Alignment(horizontal="center", vertical="center")
for row_index, row in enumerate(rows, start=2):
for col_index, value in enumerate(row, start=1):
ws.cell(row=row_index, column=col_index, value=value)
widths: dict[int, int] = {}
for row in ws.iter_rows(values_only=True):
for col_index, value in enumerate(row, start=1):
widths[col_index] = max(widths.get(col_index, 0), len("" if value is None else str(value)))
for col_index, width in widths.items():
ws.column_dimensions[get_column_letter(col_index)].width = min(max(width + 2, 12), 70)
wb.save(path)
messagebox.showinfo(
loc_text("diagnostics.export.title", catalog=self._locale_catalog, default="Esporta"),
loc_text("diagnostics.export.ok", catalog=self._locale_catalog, default="File creato:\n{path}").format(path=path),
parent=self,
)
except Exception as exc:
log_exception(__name__, exc, context="export diagnostica xlsx")
messagebox.showerror(
loc_text("diagnostics.export.title", catalog=self._locale_catalog, default="Esporta"),
loc_text(
"diagnostics.export.error",
catalog=self._locale_catalog,
default="Errore durante l'esportazione:\n{error}",
).format(error=exc),
parent=self,
)
def open_diagnostica_window(parent: tk.Misc, db_client, session=None) -> tk.Misc:
"""Open the diagnostics window."""
win = DiagnosticaWindow(parent, db_client, session=session)
place_window_fullsize_below_parent_later(parent, win)
win.bind("<Escape>", lambda _e: win.destroy())
return win

View File

@@ -81,6 +81,7 @@ from ui_tables import (
from user_session import UserSession
from version_info import module_version, versioned_title
from window_placement import place_window_fullsize_below_parent_later
from pickinglist_sequence import open_pickinglist_sequence_window
__version__ = module_version(__name__)
@@ -227,18 +228,21 @@ SELECT
MAX(Magazzino) AS Magazzino,
MAX(Area) AS Area,
MAX(Cella) AS Cella,
MIN(OrdinePrelievoGruppo) AS OrdinePrelievoGruppo,
MIN(Ordinamento) AS Ordinamento,
MAX(IDStato) AS IDStato
FROM dbo.py_ViewPackingListRestante
FROM dbo.py_ViewPackingListPrelievo
WHERE DataDocumento > CAST((GETDATE() - :days) AS int)
GROUP BY Documento, CodNazione, NAZIONE, Stato
ORDER BY MIN(Ordinamento), Documento, NAZIONE, Stato;
ORDER BY MIN(OrdinePrelievoGruppo), MIN(Ordinamento), Documento, NAZIONE, Stato;
"""
SQL_PL_DETAILS = """
SELECT *
FROM dbo.py_ViewPackingListRestante
FROM dbo.py_ViewPackingListPrelievo
WHERE Documento = :Documento
ORDER BY Ordinamento;
AND DataDocumento > CAST((GETDATE() - :days) AS int)
ORDER BY OrdinePrelievoGruppo, Ordinamento, Pallet;
"""
# -------------------- helpers --------------------
@@ -649,6 +653,7 @@ class GestionePickingListFrame(ctk.CTkFrame):
self.rows_models: list[PLRow] = []
self._detail_cache: Dict[Any, list] = {}
self.detail_doc = None
self.days_var = tk.StringVar(value="10")
self._detail_sort_key: Optional[str] = None
self._detail_sort_reverse = False
self._detail_sorting = False
@@ -699,6 +704,7 @@ class GestionePickingListFrame(ctk.CTkFrame):
(loc_text("picking.button.reload", catalog=self._locale_catalog, default="Ricarica"), self.reload_from_db),
(loc_text("picking.button.prenota", catalog=self._locale_catalog, default="Prenota"), self.on_prenota),
(loc_text("picking.button.sprenota", catalog=self._locale_catalog, default="S-prenota"), self.on_sprenota),
(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(
@@ -708,6 +714,20 @@ class GestionePickingListFrame(ctk.CTkFrame):
font=theme_font(self._theme, "toolbar_button_font", ("Segoe UI", 10, "bold")),
).grid(row=0, column=i, padx=6)
ctk.CTkLabel(
top,
text=loc_text("picking.label.days", catalog=self._locale_catalog, default="Giorni:"),
font=theme_font(self._theme, "toolbar_label_font", ("Segoe UI", 10)),
).grid(row=0, column=5, padx=(18, 4), sticky="e")
ctk.CTkOptionMenu(
top,
values=["10", "15", "20", "30"],
variable=self.days_var,
width=82,
command=lambda _value: self.reload_from_db(),
font=theme_font(self._theme, "toolbar_button_font", ("Segoe UI", 10, "bold")),
).grid(row=0, column=6, padx=(0, 6), sticky="w")
# --- 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
@@ -998,6 +1018,15 @@ class GestionePickingListFrame(ctk.CTkFrame):
documento = _s(self.detail_doc)
return documento or None
def _selected_days(self) -> int:
"""Return the selected look-back interval for operational picking lists."""
try:
days = int(str(self.days_var.get()).strip())
except Exception:
days = 10
return days if days in (10, 15, 20, 30) else 10
# ----- eventi -----
@_log_call()
def on_row_checked(self, model: PLRow, is_checked: bool):
@@ -1013,10 +1042,12 @@ class GestionePickingListFrame(ctk.CTkFrame):
self.detail_doc = model.pl.get("Documento")
_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():
_log_sql("SQL_PL_DETAILS", SQL_PL_DETAILS, {"Documento": self.detail_doc})
return await self.db_client.query_json(SQL_PL_DETAILS, {"Documento": self.detail_doc})
params = {"Documento": self.detail_doc, "days": days}
_log_sql("SQL_PL_DETAILS", SQL_PL_DETAILS, params)
return await self.db_client.query_json(SQL_PL_DETAILS, params)
def _ok(res):
# NON fermare lo spinner subito: lo farà _refresh_details_incremental
@@ -1058,9 +1089,11 @@ class GestionePickingListFrame(ctk.CTkFrame):
# document selected when it is still visible.
reselect_documento = self._selected_documento_for_reload()
self.spinner.start(" Carico…") # spinner ON
days = self._selected_days()
async def _job():
_log_sql("SQL_PL", SQL_PL, {})
return await self.db_client.query_json(SQL_PL, {})
params = {"days": days}
_log_sql("SQL_PL", SQL_PL, params)
return await self.db_client.query_json(SQL_PL, params)
def _on_success(res):
rows = _rows_to_dicts(res)
_log_dataset("SQL_PL", rows)
@@ -1156,6 +1189,45 @@ class GestionePickingListFrame(ctk.CTkFrame):
self._load_detail_sheet_data(self._detail_rows_to_sheet_data(rows))
# ----- azioni -----
@_log_call()
def on_sequence(self):
"""Open the pickup-sequence editor for the selected picking list."""
if not self._can("pickinglist.sequence"):
messagebox.showwarning(
loc_text("picking.sequence.title", catalog=self._locale_catalog, default="Sequenza di prelievo"),
loc_text("picking.sequence.msg.denied", catalog=self._locale_catalog, default="Permesso negato."),
parent=self,
)
return
model = self._get_selected_model()
if not model:
messagebox.showinfo(
loc_text("picking.sequence.title", catalog=self._locale_catalog, default="Sequenza di prelievo"),
loc_text(
"picking.sequence.msg.select",
catalog=self._locale_catalog,
default="Seleziona una Picking List prima di definire la sequenza.",
),
parent=self,
)
return
documento = _s(model.pl.get("Documento"))
def _on_saved():
self._detail_cache.pop(documento, None)
self.reload_from_db(reselect_documento=documento)
open_pickinglist_sequence_window(
self.winfo_toplevel(),
self.db_client,
documento=documento,
session=self.session,
on_saved=_on_saved,
)
@_log_call()
def on_prenota(self):
"""Reserve the selected picking list."""

View File

@@ -10,6 +10,7 @@
"launcher.history_udc": "Storico movimenti UDC",
"launcher.pickinglist": "Gestione Picking List",
"launcher.history_pickinglist": "Storico Picking List",
"launcher.diagnostics": "Diagnostica",
"launcher.arrange": "Ridisponi finestre",
"launcher.exit": "Esci",
"launcher.already_running_title": "Warehouse",
@@ -75,6 +76,27 @@
"history.picking.msg.title": "Storico Picking List",
"history.picking.msg.load_error": "Errore caricamento:\n{error}",
"history.picking.msg.detail_error": "Errore dettaglio:\n{error}",
"diagnostics.title": "Diagnostica",
"diagnostics.button.shipped_not_disassociated": "UDC spedite non disassociate",
"diagnostics.button.multiple_udc_by_cell": "Celle con multiple UDC",
"diagnostics.button.sam_udc_not_in_wms": "Ultime UDC SAM non in WMS",
"diagnostics.button.export": "Esporta XLSX",
"diagnostics.busy.shipped_not_disassociated": "Cerco UDC spedite ma ancora in cella...",
"diagnostics.busy.multiple_udc_by_cell": "Cerco celle con multiple UDC...",
"diagnostics.busy.sam_udc_not_in_wms": "Cerco le ultime UDC presenti in SAM ma non ancora in WMS...",
"diagnostics.status.rows": "Righe trovate: {count}",
"diagnostics.col.udc": "UDC",
"diagnostics.col.cell": "CELLA",
"diagnostics.col.lot": "Lotto",
"diagnostics.col.product": "Prodotto",
"diagnostics.col.description": "Descrizione",
"diagnostics.col.shipped_not_disassociated": "Spedita ma non disassociata",
"diagnostics.col.symbolic_cell": "Nome simbolico cella",
"diagnostics.export.title": "Esporta",
"diagnostics.export.empty": "Non ci sono righe da esportare.",
"diagnostics.export.dep": "Per l'esportazione serve 'openpyxl' (pip install openpyxl).",
"diagnostics.export.ok": "File creato:\n{path}",
"diagnostics.export.error": "Errore durante l'esportazione:\n{error}",
"reset.title": "Gestione Corsie - svuotamento celle per corsia",
"reset.label.aisle": "Corsia:",
"reset.button.refresh": "Carica",
@@ -99,7 +121,30 @@
"picking.button.reload": "Ricarica",
"picking.button.prenota": "Prenota",
"picking.button.sprenota": "S-prenota",
"picking.button.sequence": "Sequenza di prelievo",
"picking.button.export": "Esporta XLSX",
"picking.label.days": "Giorni:",
"picking.sequence.title": "Sequenza di prelievo",
"picking.sequence.document": "Picking List {documento}",
"picking.sequence.available": "Scaffali disponibili",
"picking.sequence.selected": "Ordine personalizzato",
"picking.sequence.hint": "Trascina gli scaffali a destra. Quelli non inseriti saranno accodati secondo l'ordine predefinito.",
"picking.sequence.default_hint": "Default: prima Non scaff., poi gli scaffali secondo l'ordine WMS.",
"picking.sequence.button.add": "Aggiungi >",
"picking.sequence.button.remove": "< Rimuovi",
"picking.sequence.button.up": "Su",
"picking.sequence.button.down": "Giu'",
"picking.sequence.button.save": "Salva",
"picking.sequence.button.reset": "Ripristina default",
"picking.sequence.button.close": "Chiudi",
"picking.sequence.busy.load": "Carico gli scaffali...",
"picking.sequence.busy.save": "Salvo la sequenza...",
"picking.sequence.msg.select": "Seleziona una Picking List prima di definire la sequenza.",
"picking.sequence.msg.denied": "Permesso negato.",
"picking.sequence.msg.saved": "Sequenza salvata.",
"picking.sequence.msg.reset": "Sequenza personalizzata rimossa. E' attivo l'ordine predefinito.",
"picking.sequence.msg.load_error": "Errore durante il caricamento:\n{error}",
"picking.sequence.msg.save_error": "Errore durante il salvataggio:\n{error}",
"scarico.title": "Scarica {ubicazione}",
"scarico.label.location": "Ubicazione: {ubicazione}",
"scarico.label.select": "Seleziona le UDC da scaricare",
@@ -125,6 +170,7 @@
"launcher.history_udc": "UDC Movement History",
"launcher.pickinglist": "Picking List Management",
"launcher.history_pickinglist": "Picking List History",
"launcher.diagnostics": "Diagnostics",
"launcher.arrange": "Arrange windows",
"launcher.exit": "Exit",
"launcher.already_running_title": "Warehouse",
@@ -190,6 +236,27 @@
"history.picking.msg.title": "Picking List History",
"history.picking.msg.load_error": "Load failed:\n{error}",
"history.picking.msg.detail_error": "Detail load failed:\n{error}",
"diagnostics.title": "Diagnostics",
"diagnostics.button.shipped_not_disassociated": "Shipped UDC not disassociated",
"diagnostics.button.multiple_udc_by_cell": "Cells with multiple UDC",
"diagnostics.button.sam_udc_not_in_wms": "Latest UDC in SAM not in WMS",
"diagnostics.button.export": "Export XLSX",
"diagnostics.busy.shipped_not_disassociated": "Searching shipped UDC still assigned to cells...",
"diagnostics.busy.multiple_udc_by_cell": "Searching cells with multiple UDC...",
"diagnostics.busy.sam_udc_not_in_wms": "Searching latest UDC present in SAM but not yet in WMS...",
"diagnostics.status.rows": "Rows found: {count}",
"diagnostics.col.udc": "UDC",
"diagnostics.col.cell": "CELL",
"diagnostics.col.lot": "Lot",
"diagnostics.col.product": "Product",
"diagnostics.col.description": "Description",
"diagnostics.col.shipped_not_disassociated": "Shipped but not disassociated",
"diagnostics.col.symbolic_cell": "Symbolic cell name",
"diagnostics.export.title": "Export",
"diagnostics.export.empty": "There are no rows to export.",
"diagnostics.export.dep": "Export requires 'openpyxl' (pip install openpyxl).",
"diagnostics.export.ok": "File created:\n{path}",
"diagnostics.export.error": "Export failed:\n{error}",
"reset.title": "Aisle Management - empty cells by aisle",
"reset.label.aisle": "Aisle:",
"reset.button.refresh": "Load",
@@ -214,7 +281,30 @@
"picking.button.reload": "Reload",
"picking.button.prenota": "Reserve",
"picking.button.sprenota": "Unreserve",
"picking.button.sequence": "Pickup sequence",
"picking.button.export": "Export XLSX",
"picking.label.days": "Days:",
"picking.sequence.title": "Pickup sequence",
"picking.sequence.document": "Picking List {documento}",
"picking.sequence.available": "Available racks",
"picking.sequence.selected": "Custom order",
"picking.sequence.hint": "Drag racks to the right. Missing racks are appended using the default order.",
"picking.sequence.default_hint": "Default: non-shelved first, then racks in WMS order.",
"picking.sequence.button.add": "Add >",
"picking.sequence.button.remove": "< Remove",
"picking.sequence.button.up": "Up",
"picking.sequence.button.down": "Down",
"picking.sequence.button.save": "Save",
"picking.sequence.button.reset": "Restore default",
"picking.sequence.button.close": "Close",
"picking.sequence.busy.load": "Loading racks...",
"picking.sequence.busy.save": "Saving sequence...",
"picking.sequence.msg.select": "Select a Picking List before defining its sequence.",
"picking.sequence.msg.denied": "Permission denied.",
"picking.sequence.msg.saved": "Sequence saved.",
"picking.sequence.msg.reset": "Custom sequence removed. The default order is active.",
"picking.sequence.msg.load_error": "Load failed:\n{error}",
"picking.sequence.msg.save_error": "Save failed:\n{error}",
"scarico.title": "Unload {ubicazione}",
"scarico.label.location": "Location: {ubicazione}",
"scarico.label.select": "Select the UDCs to unload",

11
main.py
View File

@@ -22,6 +22,7 @@ from async_loop_singleton import get_global_loop, stop_global_loop
from async_msssql_query import AsyncMSSQLClient
from audit_log import log_session_event
from db_config import build_dsn_from_config, ensure_db_config
from diagnostica import open_diagnostica_window
from gestione_layout import open_layout_window
from gestione_pickinglist import open_pickinglist_window
from login_window import prompt_login
@@ -161,6 +162,7 @@ class Launcher(ctk.CTk):
"storico_udc",
"pickinglist",
"storico_pickinglist",
"diagnostica",
]
def __init__(self, session: UserSession, db_client: AsyncMSSQLClient):
@@ -268,6 +270,15 @@ class Launcher(ctk.CTk):
lambda: open_storico_pickinglist_window(self, self.db_client, session=self.session),
),
),
(
"diagnostica",
loc_text("launcher.diagnostics", catalog=self._locale_catalog, default="Diagnostica"),
"launcher.open_diagnostics",
lambda: self._open_or_focus_child_window(
"diagnostica",
lambda: open_diagnostica_window(self, self.db_client, session=self.session),
),
),
(
"arrange",
loc_text("launcher.arrange", catalog=self._locale_catalog, default="Ridisponi finestre"),

414
ordinamento.md Normal file
View File

@@ -0,0 +1,414 @@
# Ordinamento di prelievo delle celle
## 1. Scopo del documento
Questo documento descrive il significato e l'origine del campo
`dbo.Celle.Ordinamento`, il modo in cui viene utilizzato per proporre le UDC al
barcode e le differenze riscontrate tra i due magazzini storici:
- Area 5, identificata nel database legacy come magazzino `MDE5`;
- Area 6, identificata nel database legacy come magazzino `MDE6`.
Nel linguaggio operativo si parla di Area 5 e Area 6. Nel modello legacy,
tuttavia, la tabella `Magazzini` contiene `MDE5` e `MDE6`, mentre la tabella
`Aree` contiene le singole corsie o scaffali. Gli ID della tabella `Aree` non
devono quindi essere confusi con i nomi operativi Area 5 e Area 6.
Le conclusioni riportate derivano da:
- struttura e dati correnti della tabella `dbo.Celle`;
- stored procedure legacy contenute in `script.sql`;
- viste usate dalle picking list;
- codice Python del barcode e della gestione picking list;
- confronto tra `ID`, ubicazione e `Ordinamento` delle celle reali.
## 2. Significato di `Celle.Ordinamento`
`Celle.Ordinamento` e' una priorita' numerica associata all'anagrafica della
cella. Rappresenta la posizione della cella nel percorso predefinito di
prelievo.
Il campo non indica:
- la quantita' di UDC presenti;
- la priorita' della picking list;
- lo stato libero o occupato della cella;
- l'ordine cronologico di inserimento delle UDC;
- l'ID della UDC;
- la distanza calcolata dinamicamente dal barcode.
Il valore non viene ricalcolato durante un versamento o un prelievo. E' un dato
statico dell'anagrafica della cella, salvo interventi amministrativi o
procedure specifiche di rinumerazione.
Il tipo SQL storico e' `float`, ma nei dati analizzati viene usato come numero
intero progressivo. Nella futura riprogettazione sarebbe preferibile un tipo
intero o una struttura esplicita di percorso.
## 3. Dove viene assegnato
La stored procedure legacy `dbo.spt_SaveCelle` riceve il parametro
`@Ordinamento` e lo salva direttamente:
```sql
UPDATE Celle
SET Ordinamento = @Ordinamento
WHERE ID = @ID;
```
Se la cella non esiste, la stessa stored procedure esegue una `INSERT` e
inserisce il valore ricevuto. La procedura non contiene una formula per
calcolarlo: chi crea o modifica la cella deve decidere il valore e passarlo.
Nel database non risultano trigger sulla tabella `Celle` che aggiornino
automaticamente `Ordinamento`.
Sono state individuate anche procedure legacy di supporto:
- `CreaNuoveCelle`;
- `CreaNuoveCelleMDE6`;
- `CreaNuoveCelleMDE6_BIS`;
- `sp_OrdinaCelle`;
- `spt_SaveCelle`.
Le procedure di creazione MDE6 impostano normalmente:
```sql
SET @Ordinamento = @ID;
```
La procedura `sp_OrdinaCelle` dimostra invece che il campo puo' essere
rinumerato in un secondo momento per rappresentare un percorso differente
dall'ID della cella.
## 4. Uso durante una picking list
Le viste operative associano ogni UDC alla cella corrente e recuperano
`Celle.Ordinamento`. Il barcode richiede la prima riga residua secondo l'ordine
effettivo.
Prima dell'introduzione della sequenza personalizzata, il criterio principale
era sostanzialmente:
```sql
ORDER BY Ordinamento;
```
Questo significa che il barcode non percorre tutte le celle del magazzino. La
vista contiene soltanto le UDC ancora presenti nella picking list: le celle che
non contengono una UDC richiesta vengono saltate automaticamente.
Esempio: se il percorso contiene le celle con ordine `1001`, `1002`, `1003` e
`1004`, ma la picking list contiene UDC soltanto nelle celle `1001` e `1004`, il
barcode propone direttamente la UDC di `1001` e poi quella di `1004`.
## 5. Ordinamento dell'Area 5 / MDE5
### 5.1 Caratteristica generale
Nell'Area 5 `Ordinamento` non coincide normalmente con l'ID della cella. Dai
dati emerge un percorso fisico costruito intenzionalmente, verosimilmente
hardcoded o rinumerato durante la configurazione iniziale del magazzino.
Le corsie principali coinvolte sono:
- `1A`;
- `2B`;
- `3C`;
- `4D`.
Il percorso iniziale delle corsie `1A` e `2B` e' a serpentina. Le due corsie
vengono trattate come lati o scaffali adiacenti di uno stesso tragitto fisico.
### 5.2 Esempio reale dell'inizio del percorso
```text
Ordine Ubicazione ID cella
1001 1A.1.e 1005
1002 1A.1.d 1004
1003 1A.1.c 1003
1004 1A.1.b 1002
1005 1A.1.a 1001
1007 2B.1.e 2005
1008 2B.1.d 2004
1009 2B.1.c 2003
1010 2B.1.b 2002
1011 2B.1.a 2001
1013 2B.2.e 2011
1014 2B.2.d 2010
1015 2B.2.c 2009
1016 2B.2.b 2008
1017 2B.2.a 2007
1019 1A.2.e 1011
1020 1A.2.d 1010
1021 1A.2.c 1009
1022 1A.2.b 1008
1023 1A.2.a 1007
```
La sequenza prosegue con lo stesso principio:
```text
1A campata 1
2B campata 1
2B campata 2
1A campata 2
1A campata 3
2B campata 3
2B campata 4
1A campata 4
...
```
### 5.3 Movimento verticale nella campata
All'interno di ciascuna campata l'ordine verificato e':
```text
e -> d -> c -> b -> a
```
Il percorso procede quindi dal livello identificato con `e` verso il livello
`a`. Non viene prelevata una sola UDC per campata per poi ricominciare il giro.
Vengono considerate, in sequenza, tutte le celle richieste della campata
corrente prima di passare alla campata successiva.
Se una campata contiene piu' UDC richieste in livelli diversi, il barcode le
propone secondo `e, d, c, b, a`. Se alcuni livelli non contengono UDC della
picking list, vengono semplicemente saltati.
### 5.4 Alternanza tra 1A e 2B
Il sistema non esaurisce tutto `1A` prima di iniziare `2B`. Alterna blocchi di
campate tra le due corsie per seguire un tragitto a serpentina ed evitare
ritorni inutili.
Il comportamento predefinito storico e' quindi:
- completare le UDC richieste nella campata corrente;
- passare alla campata fisicamente successiva del percorso;
- alternare `1A` e `2B` secondo la serpentina configurata;
- non tornare all'inizio dopo ogni singola UDC.
### 5.5 Passaggio a 3C e 4D
Le corsie `3C` e `4D` hanno valori di ordinamento molto piu' alti. Nei dati
analizzati:
```text
1A: minimo 1001, massimo 1275
2B: minimo 1007, massimo 1251
3C: minimo 10019, massimo 10227
4D: valori nell'intervallo 10055 - 10233 circa
```
Di conseguenza il percorso storico esaurisce normalmente il blocco `1A/2B`
prima di passare al blocco `3C/4D`.
La presenza di famiglie numeriche differenti indica che l'ordinamento
dell'Area 5 e' stato costruito o modificato in piu' fasi storiche.
### 5.6 Significato dei numeri mancanti
La progressione contiene intervalli non utilizzati, per esempio `1005` seguito
da `1007`. Il numero mancante non indica necessariamente una cella eliminata o
un errore. Puo' essere stato lasciato come separatore tra campate o derivare
dalla procedura di rinumerazione.
Il software deve usare il confronto numerico e non deve presumere che tutti i
valori siano consecutivi.
## 6. Ordinamento dell'Area 6 / MDE6
### 6.1 Caratteristica generale
Nell'Area 6 il modello e' piu' semplice. La maggior parte delle celle e' stata
generata con `Ordinamento` uguale all'ID della cella.
Le famiglie principali sono:
```text
1M -> celle e ordinamenti 5001, 5002, 5003, ...
2N -> celle e ordinamenti 6001, 6002, 6003, ...
3O -> celle e ordinamenti 7001, 7002, 7003, ...
4P -> celle e ordinamenti 8001, 8002, 8003, ...
```
Le procedure `CreaNuoveCelleMDE6` e `CreaNuoveCelleMDE6_BIS` creano le celle
con cicli annidati e assegnano esplicitamente:
```sql
SET @Ordinamento = @ID;
```
### 6.2 Ordine degli scaffali
Poiche' le famiglie numeriche sono separate, l'ordine globale predefinito e':
```text
1M -> 2N -> 3O -> 4P
```
Senza una sequenza personalizzata vengono quindi considerate prima tutte le
UDC richieste in `1M`, poi quelle in `2N`, successivamente `3O` e infine `4P`.
### 6.3 Ordine interno dello scaffale
Durante la generazione delle celle, i cicli legacy incrementano prima la
campata e, all'interno della campata, il piano. L'ordine risultante e':
```text
1M.1.a -> 1M.1.b -> 1M.1.c -> 1M.1.d -> 1M.1.e
1M.2.a -> 1M.2.b -> 1M.2.c -> 1M.2.d -> 1M.2.e
...
2N.1.a -> 2N.1.b -> ...
```
Quindi l'Area 6 differisce dall'Area 5 in due aspetti principali:
- non presenta l'alternanza a serpentina tra due corsie adiacenti;
- dentro la campata procede da `a` verso `e`, mentre nell'Area 5 verificata
procede da `e` verso `a`.
### 6.4 Limiti della ricostruzione
Il comportamento descritto deriva dalla procedura di generazione e dai valori
correnti. Eventuali celle aggiunte manualmente in tempi successivi possono
avere eccezioni. L'applicazione deve sempre leggere il valore effettivo dal
database e non ricostruirlo aritmeticamente dall'ubicazione.
## 7. Celle virtuali
Nel sistema legacy esistono due ubicazioni convenzionali:
```text
ID 1000 -> 5E.1.1 -> Non scaff. -> Ordinamento 99999
ID 9999 -> 7G.1.1 -> Spedita -> Ordinamento 0
```
Questi valori non rappresentano un percorso fisico:
- `5E.1.1` identifica UDC presenti nel magazzino ma non allocate in uno
scaffale fisico;
- `7G.1.1` identifica UDC spedite.
Storicamente `Non scaff.` aveva ordinamento `99999`, quindi finiva in fondo
alla picking list. Il nuovo requisito operativo stabilisce invece che le UDC
non scaffalate devono essere proposte per prime.
La nuova vista Python non modifica `Celle.Ordinamento`: assegna a `Non scaff.`
una precedenza logica di gruppo pari a zero. La cella `Spedita` non entra nella
vista residuale e quindi non viene proposta dal barcode.
## 8. Nuovo ordinamento predefinito Python
In assenza di una configurazione specifica per il documento, il nuovo ordine
effettivo e':
1. tutte le UDC `Non scaff.` ancora appartenenti alla picking list;
2. tutte le UDC nelle celle fisiche secondo `Celle.Ordinamento`;
3. codice pallet come criterio stabile in caso di parita'.
In forma semplificata:
```sql
ORDER BY
OrdinePrelievoGruppo,
Ordinamento,
Pallet;
```
Per le celle fisiche, se non esiste una personalizzazione,
`OrdinePrelievoGruppo` conserva la progressione storica. Il percorso dell'Area
5 rimane quindi a serpentina e quello dell'Area 6 rimane progressivo.
## 9. Effetto della sequenza personalizzata
La sequenza personalizzata opera a livello di gruppo o scaffale, non a livello
di singola cella.
Esempio di configurazione:
```text
1. Non scaff.
2. 3C
3. 1A
4. 2B
5. 4D
```
In questo caso il sistema esaurisce prima il gruppo `Non scaff.`, poi tutte le
UDC di `3C`, quindi tutte quelle di `1A` e cosi' via.
All'interno di ciascun gruppo continua a usare `Celle.Ordinamento`. Per
esempio, se `1A` viene posizionata prima di `2B`, non viene piu' conservata
l'alternanza storica tra `1A` e `2B`: vengono esaurite prima tutte le UDC di
`1A`, nel loro ordine interno, e successivamente quelle di `2B`.
Questa differenza e' intenzionale:
- ordine predefinito: conserva il percorso storico globale;
- ordine personalizzato: impone la precedenza degli scaffali scelta
dall'operatore e conserva l'ordine storico soltanto dentro ogni scaffale.
Se la sequenza personalizzata non include tutti i gruppi, quelli mancanti
vengono accodati secondo il percorso predefinito. Nessuna UDC viene esclusa.
## 10. UDC multiple, lotti e parita' di ordine
Piu' righe della vista possono riferirsi alla stessa UDC quando contiene piu'
lotti o articoli. Questo non significa che il magazziniere debba movimentare
fisicamente la stessa UDC piu' volte: l'unita' di movimentazione rimane il
pallet/UDC.
Piu' UDC possono inoltre risultare associate alla stessa cella nel modello
legacy. In quel caso condividono lo stesso `Ordinamento`. Il nuovo codice usa
anche `Pallet` come criterio finale per rendere stabile la sequenza, ma il
percorso fisico resta quello della cella.
## 11. Considerazioni per la futura riprogettazione
Nel futuro FlyWMS Core e' consigliabile separare esplicitamente:
- ordine fisico della cella dentro lo scaffale;
- ordine predefinito degli scaffali;
- percorso o strategia di visita del magazzino;
- ordine personalizzato per documento;
- priorita' operativa della picking list;
- eventuale ottimizzazione dinamica del percorso.
Il campo legacy `Celle.Ordinamento` incorpora piu' significati e dipende da
decisioni storiche non completamente documentate. Non dovrebbe essere copiato
alla lettera nel nuovo modello dati senza scomporne la semantica.
Una possibile struttura futura potrebbe prevedere:
- `Location.SequenceWithinRack` per l'ordine interno;
- `Rack.DefaultSequence` per l'ordine tra scaffali;
- `Route` e `RouteStep` per percorsi configurabili;
- `PickingDocumentRoute` per la personalizzazione della singola lista.
## 12. Riferimenti tecnici nel progetto
- `script.sql`: definizioni di `spt_SaveCelle`, `CreaNuoveCelleMDE6`,
`CreaNuoveCelleMDE6_BIS` e `sp_OrdinaCelle`;
- `barcode_repository.py`: interrogazione della prossima UDC;
- `gestione_pickinglist.py`: visualizzazione del dettaglio ordinato;
- `pickinglist_sequence.py`: gestione della sequenza per documento;
- `apply_pickinglist_sequence_patch.sql`: tabella e vista Python dedicate;
- `specifica_sequenza_prelievo.md`: specifica funzionale della nuova
configurazione.
## 13. Conclusione
L'Area 5 utilizza un percorso storico a serpentina, costruito per alternare
corsie e campate secondo la disposizione fisica. L'Area 6 utilizza invece una
progressione piu' semplice, sostanzialmente coincidente con gli ID creati in
sequenza.
Il nuovo software conserva entrambi i comportamenti come default, anticipa le
UDC non scaffalate e permette di sostituire l'ordine globale degli scaffali per
una singola picking list senza modificare l'anagrafica delle celle.

584
pickinglist_sequence.py Normal file
View File

@@ -0,0 +1,584 @@
"""Editor for the per-document picking-list pickup sequence.
The barcode client remains unaware of the editor: it reads the effective order
from ``dbo.py_ViewPackingListPrelievo``. This module only manages the Python-
specific configuration rows used by that view.
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Callable
import tkinter as tk
from tkinter import messagebox
import customtkinter as ctk
from busy_overlay import InlineBusyOverlay
from gestione_aree import AsyncRunner
from locale_text import load_locale_catalog, text as loc_text
from ui_theme import theme_color, theme_font, theme_section
from user_session import UserSession
from version_info import module_version, versioned_title
from tooltips import WidgetToolTip, load_tooltip_catalog, tooltip_text
__version__ = module_version(__name__)
SQL_LOAD_GROUPS = """
SELECT
GruppoPrelievoChiave,
MAX(GruppoPrelievo) AS GruppoPrelievo,
MIN(OrdineGruppoDefault) AS OrdineGruppoDefault,
MIN(OrdineGruppoConfigurato) AS OrdineGruppoConfigurato
FROM dbo.py_ViewPackingListPrelievo
WHERE CAST(Documento AS varchar(50)) = :documento
AND Cella <> 9999
GROUP BY GruppoPrelievoChiave
ORDER BY
CASE WHEN MIN(OrdineGruppoConfigurato) IS NULL THEN 1 ELSE 0 END,
MIN(OrdineGruppoConfigurato),
MIN(OrdineGruppoDefault),
MAX(GruppoPrelievo);
"""
SQL_RESET_SEQUENCE = """
SET NOCOUNT ON;
SET XACT_ABORT ON;
UPDATE dbo.py_PickingListPrelievoSequenza
SET DelUtente = :utente,
DelDataOra = SYSDATETIME(),
ModUtente = :utente,
ModDataOra = SYSDATETIME()
WHERE Documento = :documento
AND DelDataOra IS NULL;
SELECT CAST(0 AS int) AS ElementiSalvati;
"""
def _rows_to_dicts(result: dict[str, Any] | None) -> list[dict[str, Any]]:
"""Convert the shared database payload to dictionaries."""
if not isinstance(result, dict):
return []
rows = result.get("rows") or []
columns = result.get("columns") or []
if rows and isinstance(rows[0], dict):
return [dict(row) for row in rows]
return [
{str(columns[index]): row[index] for index in range(min(len(columns), len(row)))}
for row in rows
if isinstance(row, (list, tuple))
]
@dataclass(frozen=True)
class PickupGroup:
"""One rack group, or the conventional non-shelved group."""
key: str
label: str
default_order: int
class PickingListSequenceWindow(ctk.CTkToplevel):
"""Drag-and-drop editor for one picking-list pickup sequence."""
def __init__(
self,
parent: tk.Misc,
db_client,
*,
documento: str,
session: UserSession | None,
on_saved: Callable[[], None] | None = None,
):
super().__init__(parent)
self.db_client = db_client
self.documento = str(documento or "").strip()
self.session = session
self.on_saved = on_saved
self._catalog = load_locale_catalog()
self._tooltip_catalog = load_tooltip_catalog()
self._theme = theme_section("picking_sequence_window", {})
self.runner = AsyncRunner(self)
self.busy = InlineBusyOverlay(self, self._theme)
self.available: list[PickupGroup] = []
self.selected: list[PickupGroup] = []
self._selected_key: str | None = None
self._selected_side: str | None = None
self._drag_group: PickupGroup | None = None
self._drag_side: str | None = None
self.title(versioned_title(self._t("picking.sequence.title", "Sequenza di prelievo"), __name__))
self.geometry("780x560")
self.minsize(680, 480)
self.transient(parent)
self.configure(fg_color=theme_color(self._theme, "window_fg_color", ("#efefef", "#2f2f2f")))
self.protocol("WM_DELETE_WINDOW", self.destroy)
self.bind("<Escape>", lambda _event: self.destroy())
self._build_layout()
self.withdraw()
self.after_idle(self._show_and_load)
def _t(self, key: str, default: str, **values: Any) -> str:
"""Return one localized and formatted label."""
value = loc_text(key, catalog=self._catalog, default=default)
try:
return value.format(**values)
except Exception:
return value
def _build_layout(self) -> None:
"""Build the two lists and the explicit keyboard-friendly controls."""
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(2, weight=1)
self.grid_rowconfigure(2, weight=1)
ctk.CTkLabel(
self,
text=self._t("picking.sequence.document", "Picking List {documento}", documento=self.documento),
font=theme_font(self._theme, "title_font", ("Segoe UI", 16, "bold")),
).grid(row=0, column=0, columnspan=3, padx=16, pady=(14, 4), sticky="w")
ctk.CTkLabel(
self,
text=self._t(
"picking.sequence.hint",
"Trascina gli scaffali a destra. Quelli non inseriti saranno accodati secondo l'ordine predefinito.",
),
anchor="w",
justify="left",
wraplength=720,
).grid(row=1, column=0, columnspan=3, padx=16, pady=(0, 10), sticky="ew")
self.available_panel, self.available_list = self._build_list_panel(
0, self._t("picking.sequence.available", "Scaffali disponibili")
)
controls = ctk.CTkFrame(self, fg_color="transparent")
controls.grid(row=2, column=1, padx=10, pady=4)
for label, command in (
(self._t("picking.sequence.button.add", "Aggiungi >"), self._add_selected),
(self._t("picking.sequence.button.remove", "< Rimuovi"), self._remove_selected),
(self._t("picking.sequence.button.up", "Su"), lambda: self._move_selected(-1)),
(self._t("picking.sequence.button.down", "Giu'"), lambda: self._move_selected(1)),
):
button = ctk.CTkButton(controls, text=label, width=120, command=command)
button.pack(pady=5)
tooltip_key = {
self._t("picking.sequence.button.add", "Aggiungi >"): "picking.sequence.add",
self._t("picking.sequence.button.remove", "< Rimuovi"): "picking.sequence.remove",
self._t("picking.sequence.button.up", "Su"): "picking.sequence.up",
self._t("picking.sequence.button.down", "Giu'"): "picking.sequence.down",
}.get(label, "")
self._attach_tooltip(button, tooltip_key)
self.selected_panel, self.selected_list = self._build_list_panel(
2, self._t("picking.sequence.selected", "Ordine personalizzato")
)
ctk.CTkLabel(
self,
text=self._t(
"picking.sequence.default_hint",
"Default: prima Non scaff., poi gli scaffali secondo l'ordine WMS.",
),
anchor="w",
text_color=theme_color(self._theme, "secondary_text_color", "#4b5563"),
).grid(row=3, column=0, columnspan=3, padx=16, pady=(8, 4), sticky="ew")
bottom = ctk.CTkFrame(self)
bottom.grid(row=4, column=0, columnspan=3, padx=16, pady=(6, 14), sticky="ew")
bottom.grid_columnconfigure(0, weight=1)
reset_button = ctk.CTkButton(
bottom,
text=self._t("picking.sequence.button.reset", "Ripristina default"),
command=self._reset_default,
fg_color=theme_color(self._theme, "reset_button_fg_color", "#6b7280"),
)
reset_button.grid(row=0, column=1, padx=5, pady=8)
self._attach_tooltip(reset_button, "picking.sequence.reset")
save_button = ctk.CTkButton(
bottom,
text=self._t("picking.sequence.button.save", "Salva"),
command=self._save,
)
save_button.grid(row=0, column=2, padx=5, pady=8)
self._attach_tooltip(save_button, "picking.sequence.save")
close_button = ctk.CTkButton(
bottom,
text=self._t("picking.sequence.button.close", "Chiudi"),
command=self.destroy,
)
close_button.grid(row=0, column=3, padx=5, pady=8)
self._attach_tooltip(close_button, "picking.sequence.close")
def _attach_tooltip(self, widget: tk.Misc, key: str) -> None:
"""Attach a localized tooltip when the catalog contains one."""
value = tooltip_text(key, catalog=self._tooltip_catalog) if key else ""
if value:
WidgetToolTip(widget, value)
def _build_list_panel(self, column: int, title: str):
"""Create one titled, scrollable group list."""
panel = ctk.CTkFrame(self)
panel.grid(row=2, column=column, padx=(16 if column == 0 else 0, 16 if column == 2 else 0), pady=4, sticky="nsew")
panel.grid_columnconfigure(0, weight=1)
panel.grid_rowconfigure(1, weight=1)
ctk.CTkLabel(panel, text=title, font=("Segoe UI", 12, "bold")).grid(
row=0, column=0, padx=10, pady=(10, 6), sticky="w"
)
content = ctk.CTkScrollableFrame(panel, fg_color=theme_color(self._theme, "list_fg_color", "#f8fafc"))
content.grid(row=1, column=0, padx=8, pady=(0, 8), sticky="nsew")
content.grid_columnconfigure(0, weight=1)
return panel, content
def _show_and_load(self) -> None:
"""Center the ready window and start its asynchronous load."""
try:
self.update_idletasks()
parent = self.master
x = max(0, parent.winfo_rootx() + (parent.winfo_width() - self.winfo_width()) // 2)
y = max(0, parent.winfo_rooty() + (parent.winfo_height() - self.winfo_height()) // 2)
self.geometry(f"+{x}+{y}")
except Exception:
pass
self.deiconify()
self.lift()
self.focus_force()
self._load()
def _load(self) -> None:
"""Load available groups and the optional saved sequence."""
async def _job():
return await self.db_client.query_json(SQL_LOAD_GROUPS, {"documento": self.documento})
def _ok(result):
rows = _rows_to_dicts(result)
configured: list[tuple[int, PickupGroup]] = []
available: list[PickupGroup] = []
for row in rows:
group = PickupGroup(
key=str(row.get("GruppoPrelievoChiave") or "").strip(),
label=str(row.get("GruppoPrelievo") or "").strip(),
default_order=int(row.get("OrdineGruppoDefault") or 0),
)
configured_order = row.get("OrdineGruppoConfigurato")
if configured_order is None:
available.append(group)
else:
configured.append((int(configured_order), group))
self.available = sorted(available, key=lambda item: (item.default_order, item.label))
self.selected = [item for _order, item in sorted(configured, key=lambda pair: pair[0])]
self._render_lists()
def _error(exc):
messagebox.showerror(
self._t("picking.sequence.title", "Sequenza di prelievo"),
self._t("picking.sequence.msg.load_error", "Errore durante il caricamento:\n{error}", error=exc),
parent=self,
)
self.runner.run(
_job(),
on_success=_ok,
on_error=_error,
busy=self.busy,
message=self._t("picking.sequence.busy.load", "Carico gli scaffali..."),
)
def _render_lists(self) -> None:
"""Redraw list buttons after a move or a database load."""
for host in (self.available_list, self.selected_list):
for child in host.winfo_children():
child.destroy()
for row, group in enumerate(self.available):
self._render_group_button(self.available_list, group, "available", row)
for row, group in enumerate(self.selected):
self._render_group_button(self.selected_list, group, "selected", row)
def _render_group_button(self, host, group: PickupGroup, side: str, row: int) -> None:
"""Render one selectable and draggable group button."""
is_selected = self._selected_key == group.key and self._selected_side == side
prefix = f"{row + 1}. " if side == "selected" else ""
button = ctk.CTkButton(
host,
text=f"{prefix}{group.label}",
anchor="w",
height=34,
fg_color="#1976d2" if is_selected else theme_color(self._theme, "item_fg_color", "#2ebf74"),
command=lambda g=group, s=side: self._select(g, s),
)
button.grid(row=row, column=0, padx=4, pady=4, sticky="ew")
button.bind("<Double-Button-1>", lambda _event, g=group, s=side: self._toggle_group(g, s), add="+")
drag_widgets = [button]
for attr in ("_canvas", "_text_label", "_image_label"):
widget = getattr(button, attr, None)
if widget is not None:
drag_widgets.append(widget)
for widget in drag_widgets:
widget.bind("<ButtonPress-1>", lambda _event, g=group, s=side: self._start_drag(g, s), add="+")
widget.bind("<ButtonRelease-1>", self._finish_drag, add="+")
self._attach_tooltip(button, "picking.sequence.item")
def _select(self, group: PickupGroup, side: str) -> None:
"""Select one group for the explicit move controls."""
self._selected_key = group.key
self._selected_side = side
self._render_lists()
def _toggle_group(self, group: PickupGroup, side: str) -> None:
"""Move a double-clicked group to the opposite side."""
self._selected_key = group.key
self._selected_side = side
self._add_selected() if side == "available" else self._remove_selected()
def _selected_group(self) -> PickupGroup | None:
"""Return the group selected in the active side."""
source = self.available if self._selected_side == "available" else self.selected
return next((item for item in source if item.key == self._selected_key), None)
def _add_selected(self) -> None:
"""Append the selected available group to the custom sequence."""
if self._selected_side != "available":
return
group = self._selected_group()
if group is None:
return
self.available = [item for item in self.available if item.key != group.key]
self.selected.append(group)
self._selected_side = "selected"
self._render_lists()
def _remove_selected(self) -> None:
"""Return the selected configured group to the available list."""
if self._selected_side != "selected":
return
group = self._selected_group()
if group is None:
return
self.selected = [item for item in self.selected if item.key != group.key]
self.available.append(group)
self.available.sort(key=lambda item: (item.default_order, item.label))
self._selected_side = "available"
self._render_lists()
def _move_selected(self, offset: int) -> None:
"""Move one configured group up or down."""
if self._selected_side != "selected":
return
index = next((i for i, item in enumerate(self.selected) if item.key == self._selected_key), -1)
target = index + int(offset)
if index < 0 or target < 0 or target >= len(self.selected):
return
self.selected[index], self.selected[target] = self.selected[target], self.selected[index]
self._render_lists()
def _start_drag(self, group: PickupGroup, side: str) -> None:
"""Remember the item being dragged."""
self._drag_group = group
self._drag_side = side
@staticmethod
def _is_descendant(widget: tk.Misc | None, ancestor: tk.Misc) -> bool:
"""Return whether a widget belongs to one list panel."""
current = widget
while current is not None:
if current == ancestor:
return True
current = getattr(current, "master", None)
return False
def _finish_drag(self, event) -> None:
"""Drop the dragged group into a list and place it near the pointer."""
group = self._drag_group
if group is None:
return
self._drag_group = None
try:
target_widget = self.winfo_containing(event.x_root, event.y_root)
except Exception:
return
if self._is_descendant(target_widget, self.selected_panel):
self.available = [item for item in self.available if item.key != group.key]
self.selected = [item for item in self.selected if item.key != group.key]
target_y = event.y_root - self.selected_list.winfo_rooty()
row_height = 42
target_index = max(0, min(len(self.selected), target_y // row_height))
self.selected.insert(target_index, group)
self._selected_side = "selected"
elif self._is_descendant(target_widget, self.available_panel):
self.selected = [item for item in self.selected if item.key != group.key]
if not any(item.key == group.key for item in self.available):
self.available.append(group)
self.available.sort(key=lambda item: (item.default_order, item.label))
self._selected_side = "available"
else:
return
self._selected_key = group.key
self._render_lists()
def _save_sql(self) -> tuple[str, dict[str, Any]]:
"""Build one atomic soft-delete/insert batch using bound parameters."""
params: dict[str, Any] = {
"documento": self.documento,
"utente": self.session.login if self.session else "SYSTEM",
}
values: list[str] = []
for index, group in enumerate(self.selected):
params[f"key_{index}"] = group.key
params[f"label_{index}"] = group.label
params[f"order_{index}"] = index
values.append(
f"(:documento, :key_{index}, :label_{index}, :order_{index}, :utente, SYSDATETIME(), :utente, SYSDATETIME())"
)
insert = ""
if values:
insert = """
INSERT INTO dbo.py_PickingListPrelievoSequenza (
Documento, GruppoChiave, GruppoEtichetta, Ordine,
InsUtente, InsDataOra, ModUtente, ModDataOra
)
VALUES
""" + ",\n".join(values) + ";"
sql = f"""
SET NOCOUNT ON;
SET XACT_ABORT ON;
UPDATE dbo.py_PickingListPrelievoSequenza
SET DelUtente = :utente,
DelDataOra = SYSDATETIME(),
ModUtente = :utente,
ModDataOra = SYSDATETIME()
WHERE Documento = :documento
AND DelDataOra IS NULL;
{insert}
SELECT CAST({len(values)} AS int) AS ElementiSalvati;
"""
return sql, params
def _save(self) -> None:
"""Persist the configured prefix atomically."""
sql, params = self._save_sql()
async def _job():
return await self.db_client.query_json(sql, params, commit=True)
self.runner.run(
_job(),
on_success=lambda _result: self._save_completed("saved"),
on_error=self._save_error,
busy=self.busy,
message=self._t("picking.sequence.busy.save", "Salvo la sequenza..."),
)
def _reset_default(self) -> None:
"""Delete the custom sequence so the shared default becomes active."""
async def _job():
return await self.db_client.query_json(
SQL_RESET_SEQUENCE,
{
"documento": self.documento,
"utente": self.session.login if self.session else "SYSTEM",
},
commit=True,
)
self.runner.run(
_job(),
on_success=lambda _result: self._save_completed("reset"),
on_error=self._save_error,
busy=self.busy,
message=self._t("picking.sequence.busy.save", "Salvo la sequenza..."),
)
def _save_completed(self, mode: str) -> None:
"""Refresh the editor and notify the parent after a successful save."""
message_key = "picking.sequence.msg.reset" if mode == "reset" else "picking.sequence.msg.saved"
default = (
"Sequenza personalizzata rimossa. E' attivo l'ordine predefinito."
if mode == "reset"
else "Sequenza salvata."
)
messagebox.showinfo(self._t("picking.sequence.title", "Sequenza di prelievo"), self._t(message_key, default), parent=self)
if self.on_saved:
self.on_saved()
self._selected_key = None
self._selected_side = None
self._load()
def _save_error(self, exc: BaseException) -> None:
"""Show a localized save error."""
messagebox.showerror(
self._t("picking.sequence.title", "Sequenza di prelievo"),
self._t("picking.sequence.msg.save_error", "Errore durante il salvataggio:\n{error}", error=exc),
parent=self,
)
def open_pickinglist_sequence_window(
parent: tk.Misc,
db_client,
*,
documento: str,
session: UserSession | None,
on_saved: Callable[[], None] | None = None,
) -> PickingListSequenceWindow:
"""Open at most one sequence editor for the picking-list window."""
key = "_pickinglist_sequence_window_singleton"
existing = getattr(parent, key, None)
if existing is not None:
try:
if existing.winfo_exists() and existing.documento == str(documento).strip():
existing.on_saved = on_saved
existing.deiconify()
existing.lift()
existing.focus_force()
return existing
if existing.winfo_exists():
existing.destroy()
except Exception:
pass
window = PickingListSequenceWindow(
parent,
db_client,
documento=documento,
session=session,
on_saved=on_saved,
)
setattr(parent, key, window)
def _clear_reference() -> None:
try:
if getattr(parent, key, None) is window:
setattr(parent, key, None)
finally:
window.destroy()
window.protocol("WM_DELETE_WINDOW", _clear_reference)
return window

View File

@@ -0,0 +1,76 @@
/*
Rollback: intervallo giorni selezionabile in Gestione Picking List Python
Effetto:
- ripristina le viste Python precedenti, appoggiate a dbo.XMag_ViewPackingList;
- il filtro effettivo torna quindi a quello legacy fisso a 10 giorni.
*/
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
CREATE OR ALTER VIEW [dbo].[py_XMag_ViewPackingList]
AS
SELECT TOP 10000
legacy.Pallet,
legacy.Lotto,
legacy.Articolo,
legacy.Descrizione,
legacy.Qta,
legacy.Documento,
legacy.CodNazione,
legacy.NAZIONE,
legacy.Stato,
legacy.PalletCella,
legacy.Magazzino,
legacy.Area,
legacy.Cella,
legacy.Ordinamento,
legacy.Ubicazione,
legacy.DEST,
CASE
WHEN pr.Documento IS NOT NULL
AND pr.Documento = CAST(legacy.Documento AS varchar(8))
THEN 1
ELSE 0
END AS IDStato
FROM dbo.XMag_ViewPackingList AS legacy
LEFT JOIN dbo.PyPickingListReservation AS pr
ON pr.ID = 1
AND NULLIF(LTRIM(RTRIM(pr.Documento)), '') IS NOT NULL
ORDER BY
CASE
WHEN pr.Documento IS NOT NULL
AND pr.Documento = CAST(legacy.Documento AS varchar(8))
THEN 1
ELSE 0
END DESC,
legacy.Documento,
legacy.Ordinamento;
GO
CREATE OR ALTER VIEW [dbo].[py_ViewPackingListRestante]
AS
SELECT
Pallet,
Lotto,
Articolo,
Descrizione,
Qta,
Documento,
CodNazione,
NAZIONE,
Stato,
PalletCella,
Magazzino,
Area,
Cella,
Ordinamento,
Ubicazione,
DEST,
IDStato
FROM dbo.py_XMag_ViewPackingList
WHERE Cella <> 9999;
GO

View File

@@ -0,0 +1,19 @@
/*
Rollback della sequenza personalizzata per picking list.
Attenzione:
- elimina le configurazioni di sequenza salvate nel ramo Python;
- non modifica gli oggetti legacy C#;
- dopo il rollback occorre usare una versione Python precedente alla feature.
*/
IF OBJECT_ID(N'dbo.py_ViewPackingListPrelievo', N'V') IS NOT NULL
DROP VIEW dbo.py_ViewPackingListPrelievo;
GO
IF OBJECT_ID(N'dbo.py_PickingListPrelievoSequenza', N'U') IS NOT NULL
DROP TABLE dbo.py_PickingListPrelievoSequenza;
GO
PRINT 'Rollback sequenza prelievo Python completato.';
GO

View File

@@ -0,0 +1,74 @@
# Sequenza di prelievo per picking list
## Obiettivo
Consentire al responsabile di definire, per una singola picking list, l'ordine
dei gruppi di ubicazioni che il barcode deve seguire. Il barcode continua a
presentare una sola UDC alla volta e non espone la configurazione all'operatore.
## Ordine predefinito
In assenza di una configurazione personalizzata il sistema propone:
1. UDC `Non scaff.` nella cella convenzionale `1000` / `5E.1.1`.
2. UDC negli scaffali secondo il valore legacy `Celle.Ordinamento`.
L'ordinamento globale delle celle non viene modificato.
## Configurazione per documento
La finestra `Sequenza di prelievo` si apre dalla picking list selezionata in
`Gestione Picking List`. I gruppi disponibili sono `Non scaff.` e le corsie
effettivamente presenti nel contenuto residuo del documento.
L'utente puo' trascinare i gruppi nell'area destra, usare il doppio clic oppure
i pulsanti `Aggiungi`, `Rimuovi`, `Su` e `Giu'`. L'ordine dall'alto verso il
basso definisce la precedenza dei gruppi configurati.
Una sequenza puo' essere parziale: gli eventuali gruppi non inseriti vengono
accodati secondo l'ordine predefinito. In questo modo una configurazione non
puo' rendere invisibili UDC o scaffali aggiunti successivamente alla picking
list.
`Ripristina default` elimina la configurazione del documento e riattiva
immediatamente l'ordine standard.
## Persistenza e concorrenza
La tabella Python-only `dbo.py_PickingListPrelievoSequenza` memorizza una riga
per documento e gruppo, con ordine e campi di audit. Il salvataggio marca come
cancellata la configurazione precedente e inserisce quella nuova in una singola
transazione con `XACT_ABORT ON`, conservando quindi anche lo storico delle
variazioni.
Due picking list possono quindi avere sequenze diverse senza modificare le
celle, le altre liste o gli oggetti usati dal programma C#.
## Consumo dal barcode
La vista Python-only `dbo.py_ViewPackingListPrelievo` deriva dalla vista
residuale, esclude quindi le UDC gia' in `7G.1.1` e calcola
`OrdinePrelievoGruppo`. Sia la griglia di dettaglio sia il repository barcode
ordinano per:
1. `OrdinePrelievoGruppo`;
2. `Ordinamento` legacy della cella;
3. codice pallet come criterio stabile finale.
Pausa, ripresa, salto UDC, F1/F2 e prenotazione non vengono modificati. Quando
una UDC viene movimentata, la richiesta successiva rilegge la vista e usa la
sequenza corrente sul contenuto residuo.
## Installazione e rollback
Per un aggiornamento completo del database online usare
`apply_online_python_wms_full_patch.sql`.
Per applicare solo questa evoluzione a un database gia' aggiornato:
1. eseguire `apply_pickinglist_days_filter_patch.sql`;
2. eseguire `apply_pickinglist_sequence_patch.sql`.
Il rollback specifico e' `rollback_pickinglist_sequence_patch.sql`; dopo il
rollback deve essere ripristinata anche una versione applicativa precedente,
poiche' il nuovo codice interroga la vista di prelievo.

View File

@@ -93,7 +93,7 @@ SELECT
FROM meta m
LEFT JOIN agg a ON a.Documento = m.Documento
WHERE (:documento IS NULL OR CAST(m.Documento AS varchar(32)) LIKE CONCAT('%', :documento, '%'))
ORDER BY m.Documento DESC;
ORDER BY m.DataDocumento DESC, m.Documento DESC;
"""
# Detail query:

View File

@@ -8,6 +8,7 @@
"launcher.open_history_udc": "Apre lo storico movimenti UDC per ricostruire carichi, scarichi, celle e utenti coinvolti.",
"launcher.open_pickinglist": "Apre la gestione delle picking list per prenotare, controllare e aggiornare le liste di prelievo.",
"launcher.open_history_pickinglist": "Apre lo storico picking list per consultare liste, stato operativo e dettaglio UDC.",
"launcher.open_diagnostics": "Apre la diagnostica operativa con quadrature e controlli di coerenza sui dati di magazzino.",
"launcher.arrange_windows": "Dispone in cascata le finestre aperte seguendo l'ordine dei pulsanti del launcher.",
"launcher.exit": "Chiude l'applicazione in modo pulito terminando la sessione utente e rilasciando la connessione condivisa al database.",
"dbconfig.heading": "Spiega che qui si inseriscono i dati minimi per permettere al programma di collegarsi al database del magazzino al primo avvio.",
@@ -32,7 +33,15 @@
"multi_udc.collapse_all": "Comprime tutti i nodi dell'albero.",
"multi_udc.preselect": "Espande la corsia selezionata e preseleziona automaticamente le UDC con causale fantasma.",
"multi_udc.remove_ghosts": "Scarica logicamente le UDC selezionate della corsia attiva verso l'ubicazione di uscita.",
"multi_udc.export_xlsx": "Esporta in Excel il contenuto corrente della vista UDC fantasma."
"multi_udc.export_xlsx": "Esporta in Excel il contenuto corrente della vista UDC fantasma.",
"picking.sequence.item": "Seleziona il gruppo. Puoi trascinarlo nell'altra area oppure usare doppio clic e pulsanti centrali.",
"picking.sequence.add": "Aggiunge il gruppo selezionato alla fine della sequenza personalizzata.",
"picking.sequence.remove": "Rimuove il gruppo dalla sequenza personalizzata e lo riporta tra quelli disponibili.",
"picking.sequence.up": "Sposta il gruppo selezionato una posizione verso l'alto.",
"picking.sequence.down": "Sposta il gruppo selezionato una posizione verso il basso.",
"picking.sequence.save": "Salva in modo atomico la sequenza per la picking list corrente.",
"picking.sequence.reset": "Elimina la personalizzazione e ripristina Non scaff. prima degli scaffali.",
"picking.sequence.close": "Chiude la finestra senza effettuare altri salvataggi."
},
"ENG": {
"launcher.open_reset_corsie": "Open the aisle management window to inspect an aisle and empty it in a controlled way.",
@@ -42,6 +51,7 @@
"launcher.open_history_udc": "Open UDC movement history to review loads, unloads, cells and users involved.",
"launcher.open_pickinglist": "Open picking list management to reserve, inspect and update picking lists.",
"launcher.open_history_pickinglist": "Open picking-list history to inspect lists, operational status and UDC detail.",
"launcher.open_diagnostics": "Open operational diagnostics with data consistency checks and warehouse quadratures.",
"launcher.arrange_windows": "Arrange open windows in cascade order following the launcher buttons.",
"launcher.exit": "Close the application cleanly by ending the user session and releasing the shared database connection.",
"dbconfig.heading": "Explain that this form collects the minimum data needed to connect the program to the warehouse database on first startup.",
@@ -66,6 +76,14 @@
"multi_udc.collapse_all": "Collapse all tree nodes.",
"multi_udc.preselect": "Expand the selected aisle and automatically preselect UDCs classified as ghost candidates.",
"multi_udc.remove_ghosts": "Logically unload the selected UDCs of the active aisle to the outbound location.",
"multi_udc.export_xlsx": "Export the current ghost UDC view to Excel."
"multi_udc.export_xlsx": "Export the current ghost UDC view to Excel.",
"picking.sequence.item": "Select the group. You can drag it to the other area or use double-click and the center buttons.",
"picking.sequence.add": "Append the selected group to the custom sequence.",
"picking.sequence.remove": "Remove the group from the custom sequence and return it to the available list.",
"picking.sequence.up": "Move the selected group one position up.",
"picking.sequence.down": "Move the selected group one position down.",
"picking.sequence.save": "Atomically save the sequence for the current picking list.",
"picking.sequence.reset": "Remove the customization and restore non-shelved before racks.",
"picking.sequence.close": "Close the window without further saves."
}
}

View File

@@ -301,6 +301,29 @@
"overlay_label_padding": [18, 14, 18, 8],
"overlay_progress_padding": [18, 0, 18, 14]
},
"picking_sequence_window": {
"window_fg_color": ["#eef2f3", "#2f2f2f"],
"list_fg_color": ["#f8fafc", "#363636"],
"item_fg_color": "#2ebf74",
"secondary_text_color": "#4b5563",
"reset_button_fg_color": "#6b7280",
"title_font": {
"family": "Segoe UI",
"size": 16,
"weight": "bold"
},
"overlay_cover_fg_color": ["#d9d9d9", "#4a4a4a"],
"overlay_panel_fg_color": ["#f2f2f2", "#353535"],
"overlay_panel_corner_radius": 10,
"overlay_label_font": {
"family": "Segoe UI",
"size": 11,
"weight": "bold"
},
"overlay_progress_width": 220,
"overlay_label_padding": [18, 14, 18, 8],
"overlay_progress_padding": [18, 0, 18, 14]
},
"history_picking_window": {
"window_geometry": "1200x720",
"window_minsize": [980, 560],

View File

@@ -20,6 +20,7 @@ ALL_ACTIONS: FrozenSet[str] = frozenset(
"launcher.open_history_udc",
"launcher.open_pickinglist",
"launcher.open_history_pickinglist",
"launcher.open_diagnostics",
"launcher.arrange_windows",
"launcher.exit",
"reset_corsie.view",
@@ -27,6 +28,7 @@ ALL_ACTIONS: FrozenSet[str] = frozenset(
"non_shelved.view",
"history_udc.view",
"history_pickinglist.view",
"diagnostics.view",
"multi_udc.view",
"layout.view",
"layout.carico",
@@ -38,6 +40,7 @@ ALL_ACTIONS: FrozenSet[str] = frozenset(
"pickinglist.view",
"pickinglist.prenota",
"pickinglist.sprenota",
"pickinglist.sequence",
}
)

View File

@@ -10,30 +10,32 @@ APP_VERSION = "1.0.0"
MODULE_VERSIONS: dict[str, str] = {
"app": APP_VERSION,
"async_loop_singleton": "1.0.0",
"async_msssql_query": "1.0.0",
"async_msssql_query": "1.0.1",
"audit_log": "1.0.0",
"main": "1.0.1",
"barcode_client": "1.0.24",
"barcode_repository": "1.0.10",
"barcode_service": "1.0.22",
"main": "1.0.2",
"barcode_client": "1.0.26",
"barcode_repository": "1.0.13",
"barcode_service": "1.0.24",
"busy_overlay": "1.0.0",
"db_config": "1.0.0",
"diagnostica": "1.0.4",
"gestione_aree": "1.0.1",
"gestione_layout": "1.0.2",
"gestione_pickinglist": "1.0.4",
"gestione_pickinglist": "1.0.6",
"gestione_scarico": "1.0.8",
"locale_text": "1.0.0",
"login_window": "1.0.0",
"prenota_sprenota_sql": "1.0.1",
"pickinglist_sequence": "1.0.0",
"reset_corsie": "1.0.0",
"runtime_support": "1.0.1",
"search_pallets": "1.0.0",
"storico_pickinglist": "1.0.3",
"storico_pickinglist": "1.0.4",
"storico_udc": "1.0.0",
"tooltips": "1.0.0",
"udc_non_scaffalate": "1.0.1",
"ui_theme": "1.0.0",
"user_session": "1.0.0",
"user_session": "1.0.2",
"view_celle_multi_udc": "1.0.0",
"window_placement": "1.0.0",
}