Monolista: correggi date SAM e soglie delle picking list

This commit is contained in:
2026-11-06 10:05:00 +01:00
parent 9d87b07648
commit 1553427c21
9 changed files with 98 additions and 13 deletions

View File

@@ -0,0 +1,21 @@
-- SSMS: select the monolista WMS database (not SAMA1).
-- Change only the known 30-day predicate of the installed Python view.
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
DECLARE @ddl nvarchar(max) = OBJECT_DEFINITION(OBJECT_ID(N'dbo.py_XMag_ViewPackingList', N'V'));
DECLARE @old nvarchar(150) = N'DATDOC > CAST((GETDATE() - 30) AS int)';
DECLARE @new nvarchar(150) = N'DATDOC >= DATEDIFF(day, CONVERT(date, ''18991230'', 112), GETDATE()) - 29';
IF @ddl IS NULL
THROW 51000, 'Vista Python monolista assente o definizione non leggibile.', 1;
IF CHARINDEX(@new, @ddl) > 0 AND CHARINDEX(@old, @ddl) = 0
BEGIN
PRINT 'Soglia SAM gia aggiornata.';
RETURN;
END;
DECLARE @pos int = CHARINDEX(@old, @ddl);
DECLARE @view int = CHARINDEX(N'VIEW dbo.py_XMag_ViewPackingList', @ddl);
IF @pos = 0 OR @view = 0 OR CHARINDEX(@old, @ddl, @pos + 1) > 0
THROW 51001, 'Definizione diversa dalle versioni previste: nessuna modifica.', 1;
SET @ddl = STUFF(REPLACE(@ddl, @old, @new), 1, @view - 1, N'ALTER ');
EXEC sys.sp_executesql @ddl;
PRINT 'Soglia SAM aggiornata nella sola dbo.py_XMag_ViewPackingList.';

View File

@@ -586,7 +586,7 @@ 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)
AND prep.DATDOC >= DATEDIFF(day, CONVERT(date, '18991230', 112), GETDATE()) - 29
GROUP BY
prep.Expr1,
prep.NAZIONE,

View File

@@ -59,7 +59,7 @@ WITH PreparaPackingList AS (
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.BAMTES.DATDOC >= DATEDIFF(day, CONVERT(date, '18991230', 112), GETDATE()) - 29
AND SAMA1.dbo.LOTSER.NUMLOT <> '00000000000'
GROUP BY
SAMA1.dbo.LOTSER.NUMLOT,

View File

@@ -107,7 +107,7 @@ WITH PreparaPackingList AS (
ON n.ID = bt.IDNAZDEST
WHERE bt.ANNDOC >= YEAR(GETDATE())
AND bt.STATO = 'P'
AND bt.DATDOC > CAST((GETDATE() - 30) AS int)
AND bt.DATDOC >= DATEDIFF(day, CONVERT(date, '18991230', 112), GETDATE()) - 29
AND ls.NUMLOT <> '00000000000'
GROUP BY
ls.NUMLOT,

View File

@@ -213,7 +213,9 @@ if _MODULE_LOG_ENABLED:
# Operational picking-list management uses the Python-only residual view.
# Unlike the historical view, this dataset intentionally contains only UDCs
# that still have to be exhausted by the warehouse workflow.
SQL_PL = """
# Include today and the preceding days - 1 civil dates, regardless of time.
_SAM_FIRST_DAY = "DATEDIFF(day, CONVERT(date, '18991230', 112), GETDATE()) + 1 - :days"
SQL_PL = f"""
SELECT
COUNT(DISTINCT NULLIF(LTRIM(RTRIM(CAST(Pallet AS varchar(32)))), '')) AS Pallet,
COUNT(DISTINCT Lotto) AS Lotto,
@@ -232,16 +234,16 @@ SELECT
MIN(Ordinamento) AS Ordinamento,
MAX(IDStato) AS IDStato
FROM dbo.py_ViewPackingListPrelievo
WHERE DataDocumento > CAST((GETDATE() - :days) AS int)
WHERE DataDocumento >= {_SAM_FIRST_DAY}
GROUP BY Documento, CodNazione, NAZIONE, Stato
ORDER BY MIN(OrdinePrelievoGruppo), MIN(Ordinamento), Documento, NAZIONE, Stato;
"""
SQL_PL_DETAILS = """
SQL_PL_DETAILS = f"""
SELECT *
FROM dbo.py_ViewPackingListPrelievo
WHERE Documento = :Documento
AND DataDocumento > CAST((GETDATE() - :days) AS int)
AND DataDocumento >= {_SAM_FIRST_DAY}
ORDER BY OrdinePrelievoGruppo, Ordinamento, Pallet;
"""

View File

@@ -21,7 +21,7 @@ BEGIN TRY
IF (SELECT COUNT(*) FROM SAMA1.dbo.BAMTES b WITH (UPDLOCK,HOLDLOCK)
JOIN @Targets t ON b.ID=t.ID AND b.NUMDOC=t.Documento
WHERE b.ANNDOC=2026 AND b.STATO='D'
AND b.DATDOC>CAST(GETDATE()-30 AS int)) <> 4
AND b.DATDOC >= DATEDIFF(day, CONVERT(date, '18991230', 112), GETDATE()) - 29) <> 4
THROW 51002, 'Le quattro testate non corrispondono allo scenario atteso.', 1;
IF EXISTS (
@@ -66,7 +66,7 @@ BEGIN TRY
IF (SELECT COUNT(DISTINCT Documento) FROM dbo.py_ViewPackingListPrelievo
WHERE Documento IN (233,234,240,241,244)
AND DataDocumento>CAST(GETDATE()-30 AS int))<>5
AND DataDocumento >= DATEDIFF(day, CONVERT(date, '18991230', 112), GETDATE()) - 29)<>5
THROW 51007, 'Le cinque liste non risultano tutte visibili. Rollback.', 1;
COMMIT TRANSACTION;

View File

@@ -147,8 +147,8 @@ def _format_date(value: Any) -> str:
return value.strftime("%d/%m/%Y")
if isinstance(value, (int, float)):
try:
# SQL Server numeric datetime: CAST(datetime AS int), day 0 = 1900-01-01.
return (datetime(1900, 1, 1) + timedelta(days=int(value))).strftime("%d/%m/%Y")
# DATDOC is a SAM day serial; its fractional part is not a new day.
return (date(1899, 12, 30) + timedelta(days=int(value))).strftime("%d/%m/%Y")
except Exception:
return str(value)
text = str(value).strip()

View File

@@ -0,0 +1,62 @@
"""Diagnostic anchor from the SAM print: serial 43570 means 15 April 2019."""
import os
import unittest
from datetime import date, datetime
from inventory_service import inventory_cutoff
from storico_pickinglist import _format_date
class SamDateOriginTest(unittest.TestCase):
def test_inventory_exclusive_midnight_matches_sam_anchor(self):
_, cutoff, serial = inventory_cutoff("15/04/2019", today=date(2026, 11, 6))
self.assertEqual(cutoff, datetime(2019, 4, 16))
self.assertEqual(serial, 43571)
def test_native_datetime_preserved(self):
self.assertEqual(_format_date(datetime(2019, 4, 15, 18)), "15/04/2019")
def test_history_formats_sam_numeric_anchor(self):
self.assertEqual(_format_date(43570.0), "15/04/2019")
def test_document_244_changes_day_at_midnight_not_noon(self):
for fraction in (0, 0.49999, 0.5, 0.99999):
with self.subTest(fraction=fraction):
self.assertEqual(_format_date(46289 + fraction), "24/09/2026")
self.assertEqual(_format_date(46290), "25/09/2026")
def test_non_serial_inputs_are_not_shifted(self):
for value, expected in ((None, ""), (date(2024, 2, 29), "29/02/2024"),
("20260924", "24/09/2026"),
("2026-09-24T23:59:59", "24/09/2026")):
with self.subTest(value=value):
self.assertEqual(_format_date(value), expected)
@unittest.skipUnless(os.getenv("SAM_DATE_TEST_LOCAL") == "1", "opt-in local SQL Server test")
def test_actual_master_and_detail_filters_on_local_sql_server(self):
import pyodbc
from gestione_pickinglist import SQL_PL, SQL_PL_DETAILS
cn = pyodbc.connect("DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost;"
"DATABASE=tempdb;Trusted_Connection=yes;", timeout=5)
self.addCleanup(cn.close)
# No warehouse objects: replace only the source and clock of each real query.
source = """(SELECT n AS Documento, n AS DataDocumento, 'x' AS Pallet,
1 AS Lotto, 1 AS Articolo, 1 AS Descrizione, 1 AS Qta, 1 AS CodNazione,
1 AS NAZIONE, 1 AS Stato, 1 AS PalletCella, 1 AS Magazzino, 1 AS Area,
1 AS Cella, 1 AS OrdinePrelievoGruppo, 1 AS Ordinamento, 0 AS IDStato
FROM (VALUES (CAST(? AS float))) v(n)) sample"""
for days in (10, 15, 20, 30):
first = 46289 - days + 1 # 24 September is the final included day.
for clock in ("00:00:00", "11:59:59", "12:00:00", "23:59:59"):
for serial, inside in ((first - 0.001, False), (first, True), (46289.99, True)):
for query in (SQL_PL, SQL_PL_DETAILS):
sql = query.replace("dbo.py_ViewPackingListPrelievo", source)
sql = sql.replace("GETDATE()", "CONVERT(datetime2, ?, 126)")
sql = sql.replace(":days", "?").replace(":Documento", "?")
params = [serial]
if query == SQL_PL_DETAILS:
params.append(serial)
params.extend(["2026-09-24T" + clock, days])
with self.subTest(days=days, clock=clock, serial=serial, detail=query==SQL_PL_DETAILS):
self.assertEqual(bool(cn.execute(sql, *params).fetchall()), inside)

View File

@@ -24,7 +24,7 @@ MODULE_VERSIONS: dict[str, str] = {
"diagnostica": "1.0.5",
"gestione_aree": "1.0.1",
"gestione_layout": "1.0.3",
"gestione_pickinglist": "1.0.7",
"gestione_pickinglist": "1.0.8",
"gestione_scarico": "1.0.9",
"locale_text": "1.0.0",
"login_window": "1.0.1",
@@ -34,7 +34,7 @@ MODULE_VERSIONS: dict[str, str] = {
"reset_corsie_service": "1.0.0",
"runtime_support": "1.0.2",
"search_pallets": "1.0.1",
"storico_pickinglist": "1.0.6",
"storico_pickinglist": "1.0.7",
"pickinglist_history_search": "1.0.0",
"storico_udc": "1.0.1",
"tooltips": "1.0.0",