Implementa picking bilista 2.1.0 con test, patch SQL e rollback

This commit is contained in:
2026-10-13 17:11:23 +02:00
parent a60610c03e
commit bd73fe5edb
19 changed files with 2168 additions and 130 deletions

View File

@@ -0,0 +1,240 @@
/* Warehouse bilista. Eseguire su Mediseawall, dopo la patch cumulativa.
Solo nuovi oggetti py_*; nessuna modifica a oggetti C# o a dati SAM.
Idempotente: non azzera giri, skip o sequenze esistenti.
Fermare tutti i client Python prima di installare o ripristinare.
*/
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
IF OBJECT_ID('dbo.py_ViewPackingListPrelievo','V') IS NULL
OR OBJECT_ID('dbo.py_XMag_ViewPackingListStorico','V') IS NULL
THROW 51100, 'Applicare prima le patch Python esistenti.', 1;
GO
IF OBJECT_ID('dbo.py_PickingPair','U') IS NULL
BEGIN
CREATE TABLE dbo.py_PickingPair (
RunId uniqueidentifier NOT NULL PRIMARY KEY,
Documento1 varchar(8) NOT NULL,
Documento2 varchar(8) NOT NULL,
Active1 bit NOT NULL,
Active2 bit NOT NULL,
IsOpen bit NOT NULL,
InsUtente int NOT NULL,
InsDataOra datetime2 NOT NULL DEFAULT SYSDATETIME(),
ModUtente int NOT NULL,
ModDataOra datetime2 NOT NULL DEFAULT SYSDATETIME(),
ClosedAt1 datetime2 NULL,
ClosedAt2 datetime2 NULL,
CONSTRAINT CK_py_PickingPair_Doc CHECK (Documento1 <> Documento2),
CONSTRAINT CK_py_PickingPair_Open CHECK
((IsOpen=1 AND (Active1=1 OR Active2=1)) OR (IsOpen=0 AND Active1=0 AND Active2=0))
);
CREATE UNIQUE INDEX UX_py_PickingPair_Open ON dbo.py_PickingPair(IsOpen) WHERE IsOpen=1;
END;
IF OBJECT_ID('dbo.py_PickingPairSequence','U') IS NULL
BEGIN
CREATE TABLE dbo.py_PickingPairSequence (
RunId uniqueidentifier NOT NULL REFERENCES dbo.py_PickingPair(RunId),
GruppoChiave varchar(160) NOT NULL,
Ordine int NOT NULL,
PRIMARY KEY(RunId,GruppoChiave),
UNIQUE(RunId,Ordine)
);
END;
IF OBJECT_ID('dbo.py_PickingPairSkip','U') IS NULL
BEGIN
CREATE TABLE dbo.py_PickingPairSkip (
RunId uniqueidentifier NOT NULL REFERENCES dbo.py_PickingPair(RunId),
Documento varchar(8) NOT NULL,
Pallet varchar(50) NOT NULL,
InsUtente int NOT NULL,
InsDataOra datetime2 NOT NULL DEFAULT SYSDATETIME(),
PRIMARY KEY(RunId,Documento,Pallet)
);
END;
GO
CREATE OR ALTER VIEW dbo.py_ViewPickingPairRows
AS
WITH Residue AS (
SELECT h.*,
CASE WHEN h.Cella=1000 THEN NULL
WHEN RTRIM(m.Descrizione)='MDE5' THEN 5
WHEN RTRIM(m.Descrizione)='MDE6' THEN 6 END AS OperationalArea,
CASE WHEN h.Cella=1000 THEN 'NON_SCAFF'
ELSE CONCAT('M:',h.Magazzino,'|A:',h.Area,'|C:',LTRIM(RTRIM(
CASE WHEN CHARINDEX(' - ',h.Ubicazione)>0
THEN LEFT(h.Ubicazione,CHARINDEX(' - ',h.Ubicazione)-1)
ELSE h.Ubicazione END))) END AS GroupKey
FROM dbo.py_XMag_ViewPackingListStorico h
LEFT JOIN dbo.Celle c ON c.ID=h.Cella
LEFT JOIN dbo.Aree a ON a.ID=c.IDArea
LEFT JOIN dbo.Magazzini m ON m.ID=a.IDMagazzino
WHERE h.Cella<>9999
)
SELECT r.RunId, r.Documento1, r.Documento2, r.Active1, r.Active2,
d.Slot, d.Documento AS SlotDocument, d.IsActive,
h.Pallet, h.Cella, h.Ubicazione, h.Ordinamento, h.OperationalArea,
h.GroupKey, h.StatoDocumento, h.CodNazione, h.NAZIONE,
s.Ordine AS SequenceOrder,
CONVERT(bit,CASE WHEN k.Pallet IS NULL THEN 0 ELSE 1 END) AS IsSkipped
FROM dbo.py_PickingPair r
CROSS APPLY (VALUES (1,r.Documento1,r.Active1),(2,r.Documento2,r.Active2)) d(Slot,Documento,IsActive)
LEFT JOIN Residue h ON h.Documento=TRY_CONVERT(int,d.Documento)
LEFT JOIN dbo.py_PickingPairSequence s ON s.RunId=r.RunId
AND s.GruppoChiave COLLATE Latin1_General_CI_AS=h.GroupKey COLLATE Latin1_General_CI_AS
LEFT JOIN dbo.py_PickingPairSkip k ON k.RunId=r.RunId AND k.Documento=d.Documento
AND k.Pallet COLLATE Latin1_General_CI_AS=CAST(h.Pallet AS varchar(50)) COLLATE Latin1_General_CI_AS
WHERE r.IsOpen=1;
GO
CREATE OR ALTER PROCEDURE dbo.py_sp_PickingPair
@Action char(1),
@IDOperatore int,
@Documento1 varchar(8)=NULL,
@Documento2 varchar(8)=NULL,
@RunId uniqueidentifier=NULL,
@Documento varchar(8)=NULL,
@Pallet varchar(50)=NULL,
@Silent bit=0
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;
BEGIN TRY
BEGIN TRANSACTION;
DECLARE @LockResult int;
EXEC @LockResult=sys.sp_getapplock @Resource='py_PickingPair',
@LockMode='Exclusive',@LockOwner='Transaction',@LockTimeout=10000;
IF @LockResult<0 THROW 51101, 'Prenotazione occupata. Riprovare.', 1;
IF @IDOperatore<=0 THROW 51102, 'Operatore non valido.', 1;
IF @Action='R'
BEGIN
IF @Documento1 IS NULL OR @Documento2 IS NULL OR @Documento1=@Documento2
THROW 51103, 'Selezionare due liste diverse.', 1;
DECLARE @Existing uniqueidentifier;
SELECT @Existing=RunId FROM dbo.py_PickingPair WITH (UPDLOCK,HOLDLOCK) WHERE IsOpen=1;
IF @Existing IS NOT NULL
BEGIN
IF NOT EXISTS(SELECT 1 FROM dbo.py_PickingPair WHERE RunId=@Existing
AND Documento1=@Documento1 AND Documento2=@Documento2 AND Active1=1 AND Active2=1)
THROW 51104, 'Un giro bilista e gia prenotato. Sprenotarlo prima di cambiarlo.', 1;
SET @RunId=@Existing;
END
ELSE
BEGIN
IF (SELECT COUNT(DISTINCT Documento) FROM dbo.py_ViewPackingListRestante
WHERE Documento IN (TRY_CONVERT(int,@Documento1),TRY_CONVERT(int,@Documento2)))<>2
THROW 51105, 'Una lista non e piu prenotabile o non ha residue.', 1;
IF EXISTS(SELECT Pallet FROM dbo.py_ViewPackingListRestante
WHERE Documento IN (TRY_CONVERT(int,@Documento1),TRY_CONVERT(int,@Documento2))
GROUP BY Pallet HAVING COUNT(DISTINCT Documento)>1 OR COUNT(DISTINCT Cella)>1)
THROW 51106, 'UDC condivisa tra liste o presente in piu celle.', 1;
IF EXISTS(SELECT 1 FROM dbo.py_ViewPackingListRestante h
LEFT JOIN dbo.Celle c ON c.ID=h.Cella
LEFT JOIN dbo.Aree a ON a.ID=c.IDArea
LEFT JOIN dbo.Magazzini m ON m.ID=a.IDMagazzino
WHERE h.Documento IN (TRY_CONVERT(int,@Documento1),TRY_CONVERT(int,@Documento2))
AND h.Cella<>1000 AND (m.ID IS NULL OR RTRIM(m.Descrizione) NOT IN ('MDE5','MDE6')))
THROW 51107, 'Area fisica non riconosciuta.', 1;
IF (SELECT COUNT(DISTINCT m.ID) FROM dbo.py_ViewPackingListRestante h
JOIN dbo.Celle c ON c.ID=h.Cella JOIN dbo.Aree a ON a.ID=c.IDArea
JOIN dbo.Magazzini m ON m.ID=a.IDMagazzino
WHERE h.Documento IN (TRY_CONVERT(int,@Documento1),TRY_CONVERT(int,@Documento2))
AND h.Cella<>1000)>1
THROW 51108, 'Le liste contengono UDC in aree diverse.', 1;
DECLARE @Single varchar(8), @RC int=0;
SELECT @Single=Documento FROM dbo.PyPickingListReservation WITH (UPDLOCK,HOLDLOCK) WHERE ID=1;
IF NULLIF(@Single,'') IS NOT NULL
BEGIN
EXEC dbo.py_sp_xExePackingListPallet @IDOperatore=@IDOperatore,
@Documento=@Single,@Azione='S',@RC=@RC OUTPUT;
IF @RC<>0 THROW 51109, 'Impossibile rilasciare la prenotazione monolista.', 1;
END;
SET @RunId=NEWID();
INSERT dbo.py_PickingPair(RunId,Documento1,Documento2,Active1,Active2,IsOpen,InsUtente,ModUtente)
VALUES(@RunId,@Documento1,@Documento2,1,1,1,@IDOperatore,@IDOperatore);
INSERT dbo.py_PickingPairSequence(RunId,GruppoChiave,Ordine)
SELECT @RunId,GruppoChiave,Ordine FROM dbo.py_PickingListPrelievoSequenza
WHERE Documento=@Documento1 AND DelDataOra IS NULL AND GruppoChiave<>'NON_SCAFF';
END;
END
ELSE IF @Action IN ('K','G','F','U')
BEGIN
IF NOT EXISTS(SELECT 1 FROM dbo.py_PickingPair WITH (UPDLOCK,HOLDLOCK)
WHERE RunId=@RunId AND IsOpen=1)
THROW 51110, 'Il giro bilista e terminato o e cambiato. Ricaricare.', 1;
-- The inherited historical view is scoped to the current year.
-- Never mistake the annual cutoff for a fully shipped pair.
IF @Action<>'U' AND EXISTS(SELECT 1 FROM dbo.py_PickingPair
WHERE RunId=@RunId AND YEAR(InsDataOra)<>YEAR(GETDATE()))
THROW 51116, 'Giro di un anno precedente: verificare e sprenotare dal desktop.', 1;
IF @Action IN ('K','G')
BEGIN
IF NOT EXISTS(SELECT 1 FROM dbo.py_ViewPickingPairRows
WHERE RunId=@RunId AND SlotDocument=@Documento AND IsActive=1
AND CAST(Pallet AS varchar(50)) COLLATE Latin1_General_CI_AS=@Pallet COLLATE Latin1_General_CI_AS
AND StatoDocumento='P' AND IsSkipped=0)
THROW 51111, 'UDC non piu disponibile nella lista corrente.', 1;
IF EXISTS(SELECT 1 FROM dbo.py_ViewPickingPairRows WHERE RunId=@RunId
AND IsActive=1 AND Cella<>1000 AND Pallet IS NOT NULL AND OperationalArea IS NULL)
OR (SELECT COUNT(DISTINCT OperationalArea) FROM dbo.py_ViewPickingPairRows
WHERE RunId=@RunId AND IsActive=1 AND Cella<>1000)>1
THROW 51108, 'Le liste contengono UDC in aree diverse o non riconosciute.', 1;
IF @Action='K'
BEGIN
INSERT dbo.py_PickingPairSkip(RunId,Documento,Pallet,InsUtente)
VALUES(@RunId,@Documento,@Pallet,@IDOperatore);
-- Preserve the monolist F2 rule after this pair is released:
-- skipped UDCs require an explicit new reservation.
IF NOT EXISTS(SELECT 1 FROM dbo.py_BarcodePickingListSkip
WHERE Documento=@Documento AND Pallet=@Pallet AND IDStato=0 AND Risolto=0)
INSERT dbo.py_BarcodePickingListSkip
(Documento,Pallet,IDStato,IDOperatore,DataOra,Motivo,Risolto)
VALUES(@Documento,@Pallet,0,@IDOperatore,SYSDATETIME(),'Salto bilista',0);
END;
END;
IF @Action='U'
BEGIN
IF @Documento IS NULL OR @Documento NOT IN (SELECT Documento1 FROM dbo.py_PickingPair WHERE RunId=@RunId
UNION ALL SELECT Documento2 FROM dbo.py_PickingPair WHERE RunId=@RunId)
THROW 51112, 'Documento estraneo al giro.', 1;
UPDATE dbo.py_PickingPair SET
Active1=CASE WHEN Documento1=@Documento THEN 0 ELSE Active1 END,
Active2=CASE WHEN Documento2=@Documento THEN 0 ELSE Active2 END,
ClosedAt1=CASE WHEN Documento1=@Documento THEN SYSDATETIME() ELSE ClosedAt1 END,
ClosedAt2=CASE WHEN Documento2=@Documento THEN SYSDATETIME() ELSE ClosedAt2 END,
IsOpen=CASE WHEN (Documento1=@Documento OR Active1=0)
AND (Documento2=@Documento OR Active2=0) THEN 0 ELSE 1 END,
ModUtente=@IDOperatore,ModDataOra=SYSDATETIME()
WHERE RunId=@RunId;
END;
IF @Action='F'
BEGIN
DECLARE @Work1 bit=0,@Work2 bit=0;
SELECT @Work1=MAX(CASE WHEN Slot=1 THEN 1 ELSE 0 END),
@Work2=MAX(CASE WHEN Slot=2 THEN 1 ELSE 0 END)
FROM dbo.py_ViewPickingPairRows WHERE RunId=@RunId AND IsActive=1
AND Pallet IS NOT NULL AND IsSkipped=0;
UPDATE dbo.py_PickingPair SET
Active1=ISNULL(@Work1,0),Active2=ISNULL(@Work2,0),
IsOpen=CASE WHEN ISNULL(@Work1,0)=1 OR ISNULL(@Work2,0)=1 THEN 1 ELSE 0 END,
ClosedAt1=CASE WHEN ISNULL(@Work1,0)=0 THEN COALESCE(ClosedAt1,SYSDATETIME()) ELSE ClosedAt1 END,
ClosedAt2=CASE WHEN ISNULL(@Work2,0)=0 THEN COALESCE(ClosedAt2,SYSDATETIME()) ELSE ClosedAt2 END,
ModUtente=@IDOperatore,ModDataOra=SYSDATETIME()
WHERE RunId=@RunId;
END;
END
ELSE THROW 51113, 'Azione bilista non valida.', 1;
COMMIT TRANSACTION;
IF @Silent=0 SELECT CAST(@RunId AS varchar(36)) AS RunId,0 AS RC;
END TRY
BEGIN CATCH
IF XACT_STATE()<>0 ROLLBACK TRANSACTION;
THROW;
END CATCH;
END;
GO
PRINT 'Patch bilista installata. Nessun giro creato e nessuna UDC movimentata.';
GO