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

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

View File

@@ -0,0 +1,100 @@
"""Read-only UDC lookup across the current and previous ERP document years.
Unlike the default history view this query retains BAMTES.ID, so documents with
the same display number in different years cannot share details or movements.
The UDC selects documents, not detail rows: counts always describe whole lists.
"""
from version_info import module_version
__version__ = module_version(__name__)
_BASE = """
DECLARE @UDC varchar(32) = :pallet;
DECLARE @Document varchar(32) = :documento;
DECLARE @DocumentID int = :id_documento;
WITH documents AS (
SELECT bt.*
FROM SAMA1.dbo.BAMTES bt
WHERE bt.ANNDOC BETWEEN YEAR(GETDATE()) - 1 AND YEAR(GETDATE())
AND bt.STATO IN ('P', 'D')
AND (@DocumentID IS NULL OR bt.ID = @DocumentID)
AND (@Document IS NULL OR CAST(bt.NUMDOC AS varchar(32)) LIKE '%' + @Document + '%')
AND (@UDC IS NULL OR EXISTS (
SELECT 1 FROM SAMA1.dbo.FATRIG fr
JOIN SAMA1.dbo.LOTTIBF lb ON lb.IDFATRIG = fr.ID
JOIN SAMA1.dbo.LOTSER ls ON ls.ID = lb.IDLOTSER
WHERE fr.IDBAM = bt.ID
AND ls.NUMLOT NOT IN ('', '00000000000')
AND LEFT(ls.NUMSER, 6) COLLATE Latin1_General_CI_AS = @UDC COLLATE Latin1_General_CI_AS
))
), prep AS (
SELECT bt.ID AS IDDocumento, bt.ANNDOC AS AnnoDocumento,
bt.NUMDOC AS Documento, bt.DATDOC AS DataDocumento, bt.STATO AS StatoDocumento,
LEFT(ls.NUMSER, 6) AS Pallet, ls.NUMLOT AS Lotto, a.CODICE AS Articolo,
fr.DESCR AS Descrizione, SUM(ls.QTTGIAI) AS Qta,
n.CODICE AS CodNazione, mt.CODICE + ' ' + n.DESCR AS NAZIONE
FROM documents bt
JOIN SAMA1.dbo.FATRIG fr ON fr.IDBAM = bt.ID
JOIN SAMA1.dbo.LOTTIBF lb ON lb.IDFATRIG = fr.ID
JOIN SAMA1.dbo.LOTSER ls ON ls.ID = lb.IDLOTSER
LEFT JOIN SAMA1.dbo.ARTICO a ON a.ID = ls.IDARTICO
LEFT JOIN SAMA1.dbo.NAZIONI n ON n.ID = bt.IDNAZDEST
LEFT JOIN SAMA1.dbo.MTRASP mt ON mt.ID = bt.IDMTRASP
WHERE ls.NUMLOT NOT IN ('', '00000000000')
GROUP BY bt.ID, bt.ANNDOC, bt.NUMDOC, bt.DATDOC, bt.STATO,
LEFT(ls.NUMSER, 6), ls.NUMLOT, a.CODICE, fr.DESCR, n.CODICE, mt.CODICE, n.DESCR
), locations AS (
SELECT g.BarcodePallet, g.IDCella, c.Ordinamento,
c.Corsia + ' - ' + c.Colonna + ' - ' + c.Fila AS Ubicazione
FROM dbo.XMag_GiacenzaPallet g JOIN dbo.Celle c ON c.ID = g.IDCella
), base AS (
SELECT prep.*, ISNULL(g.IDCella, 1000) AS Cella,
ISNULL(g.Ordinamento, 99999) AS Ordinamento,
ISNULL(g.Ubicazione, 'Non scaff.') AS Ubicazione,
CASE WHEN prep.AnnoDocumento = YEAR(GETDATE()) AND EXISTS (
SELECT 1 FROM dbo.PyPickingListReservation pr
WHERE pr.ID = 1 AND pr.Documento = CAST(prep.Documento AS varchar(8))
) THEN 1 ELSE 0 END AS IDStato
FROM prep LEFT JOIN locations g
ON g.BarcodePallet COLLATE Latin1_General_CI_AS = prep.Pallet COLLATE Latin1_General_CI_AS
)
"""
SQL_UDC_HISTORY_DETAILS = _BASE + """
SELECT * FROM base ORDER BY Ordinamento, Pallet, Lotto;
"""
SQL_UDC_HISTORY = _BASE + """
, pallets AS (
SELECT IDDocumento, Pallet,
MAX(CASE WHEN Cella <> 9999 THEN 1 ELSE 0 END) AS HasResiduo
FROM base WHERE NULLIF(LTRIM(RTRIM(Pallet)), '') IS NOT NULL
GROUP BY IDDocumento, Pallet
), totals AS (
SELECT IDDocumento, COUNT(*) AS TotUDC, SUM(HasResiduo) AS RigheResidue,
SUM(1 - HasResiduo) AS RigheSpedite
FROM pallets GROUP BY IDDocumento
), meta AS (
SELECT IDDocumento, MAX(AnnoDocumento) AS AnnoDocumento,
MAX(Documento) AS Documento, MAX(DataDocumento) AS DataDocumento,
MAX(StatoDocumento) AS StatoDocumento, MAX(NAZIONE) AS NAZIONE,
MAX(CodNazione) AS CodNazione, MAX(IDStato) AS IDStato
FROM base GROUP BY IDDocumento
)
SELECT m.*, ISNULL(t.TotUDC, 0) AS TotUDC, ISNULL(t.RigheResidue, 0) AS RigheResidue,
ISNULL(t.RigheSpedite, 0) AS RigheSpedite,
CASE WHEN m.StatoDocumento = 'D' AND ISNULL(t.RigheResidue, 0) > 0 THEN 'Chiusa ERP con residui'
WHEN m.StatoDocumento = 'D' THEN 'Chiusa'
WHEN ISNULL(t.RigheResidue, 0) = 0 THEN 'Esaurita'
WHEN ISNULL(t.RigheSpedite, 0) > 0 THEN 'In corso'
ELSE 'Da lavorare' END AS StatoOperativo
FROM meta m LEFT JOIN totals t ON t.IDDocumento = m.IDDocumento
ORDER BY m.DataDocumento DESC, m.Documento DESC, m.IDDocumento DESC;
"""
def detail_request(documento: str, document_id: int | None, default_sql: str):
"""Return a bound detail query; never use a cross-year display number alone."""
if document_id is not None:
return SQL_UDC_HISTORY_DETAILS, {"pallet": None, "documento": None, "id_documento": document_id}
return default_sql, {"documento": documento}