122 lines
4.4 KiB
Python
122 lines
4.4 KiB
Python
"""Classify and move aisle-reset UDCs without changing barcode workflows.
|
|
|
|
Each call is one transaction: recheck the source, read the ERP document state,
|
|
record P/V movements, refresh reservations and verify the resulting location.
|
|
The caller must use query_json(commit=True), never execute this batch piecemeal.
|
|
"""
|
|
|
|
from version_info import module_version
|
|
|
|
__version__ = module_version(__name__)
|
|
|
|
SQL_RESET_PALLET = """
|
|
SET NOCOUNT ON;
|
|
SET XACT_ABORT ON;
|
|
DECLARE @Pallet varchar(32) = :barcode_pallet;
|
|
DECLARE @ExpectedCell int = :source_idcella;
|
|
DECLARE @User varchar(128) = :utente;
|
|
DECLARE @Now datetime = GETDATE();
|
|
DECLARE @LockedRows int;
|
|
DECLARE @TargetCell int = 1000;
|
|
DECLARE @SourceID int;
|
|
|
|
-- Keep other ledger writers away until this UDC transaction completes.
|
|
SELECT @LockedRows = COUNT(*)
|
|
FROM dbo.MagazziniPallet WITH (UPDLOCK, HOLDLOCK)
|
|
WHERE Attributo = @Pallet;
|
|
|
|
IF @ExpectedCell IN (1000, 9999)
|
|
THROW 51001, 'Reset: la sorgente deve essere una cella fisica.', 1;
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM dbo.XMag_GiacenzaPallet
|
|
WHERE BarcodePallet = @Pallet AND IDCella = @ExpectedCell
|
|
) OR EXISTS (
|
|
SELECT 1 FROM dbo.XMag_GiacenzaPallet
|
|
WHERE BarcodePallet = @Pallet AND (IDCella <> @ExpectedCell OR IDCella IS NULL)
|
|
)
|
|
THROW 51002, 'Reset: UDC spostata o presente in piu celle. Ricaricare e verificare.', 1;
|
|
|
|
-- Read the ERP association directly: history UI date filters must not decide
|
|
-- whether an old, already shipped UDC is put back into available stock.
|
|
IF EXISTS (
|
|
SELECT 1
|
|
FROM SAMA1.dbo.LOTSER AS ls
|
|
JOIN SAMA1.dbo.LOTTIBF AS lb ON lb.IDLOTSER = ls.ID
|
|
JOIN SAMA1.dbo.FATRIG AS fr ON fr.ID = lb.IDFATRIG
|
|
JOIN SAMA1.dbo.BAMTES AS bt ON bt.ID = fr.IDBAM
|
|
WHERE bt.STATO = 'D'
|
|
AND LEFT(ls.NUMSER, 6) COLLATE Latin1_General_CI_AS =
|
|
@Pallet COLLATE Latin1_General_CI_AS
|
|
)
|
|
SET @TargetCell = 9999;
|
|
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM dbo.Celle c JOIN dbo.Aree a ON a.ID = c.IDArea
|
|
WHERE c.ID = @TargetCell AND c.DelDataOra IS NULL
|
|
)
|
|
THROW 51003, 'Reset: cella virtuale di destinazione non disponibile.', 1;
|
|
|
|
SELECT TOP (1) @SourceID = ID
|
|
FROM dbo.MagazziniPallet
|
|
WHERE Attributo = @Pallet AND IDCella = @ExpectedCell
|
|
AND Tipo = 'V' AND PesoUnitario > 0
|
|
ORDER BY ID DESC;
|
|
IF @SourceID IS NULL
|
|
THROW 51004, 'Reset: movimento sorgente non trovato.', 1;
|
|
|
|
UPDATE dbo.MagazziniPallet
|
|
SET ModUtente = @User, ModDataOra = @Now
|
|
WHERE ID = @SourceID;
|
|
|
|
INSERT INTO dbo.MagazziniPallet (
|
|
Tipo, IDRiferimento, NumeroPallet, Attributo, IDMagazzino, IDArea,
|
|
IDCella, DataMagazzino, PesoUnitario, Tara, InsUtente, InsDataOra
|
|
)
|
|
SELECT 'P', src.ID, ISNULL(src.NumeroPallet, 0), src.Attributo,
|
|
src.IDMagazzino, src.IDArea, src.IDCella, @Now,
|
|
src.PesoUnitario, ISNULL(src.Tara, 0), @User, @Now
|
|
FROM dbo.MagazziniPallet src WHERE src.ID = @SourceID;
|
|
|
|
UPDATE dbo.Celle SET IDStato = 0, ModUtente = @User, ModDataOra = @Now
|
|
WHERE ID = @ExpectedCell;
|
|
|
|
INSERT INTO dbo.MagazziniPallet (
|
|
Tipo, IDRiferimento, NumeroPallet, Attributo, IDMagazzino, IDArea,
|
|
IDCella, DataMagazzino, PesoUnitario, Tara, InsUtente, InsDataOra
|
|
)
|
|
SELECT 'V', src.ID, ISNULL(src.NumeroPallet, 0), src.Attributo,
|
|
a.IDMagazzino, c.IDArea, c.ID, @Now,
|
|
src.PesoUnitario, ISNULL(src.Tara, 0), @User, @Now
|
|
FROM dbo.MagazziniPallet src
|
|
JOIN dbo.Celle c ON c.ID = @TargetCell
|
|
JOIN dbo.Aree a ON a.ID = c.IDArea
|
|
WHERE src.ID = @SourceID;
|
|
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM dbo.XMag_GiacenzaPallet
|
|
WHERE BarcodePallet = @Pallet AND IDCella = @TargetCell
|
|
) OR EXISTS (
|
|
SELECT 1 FROM dbo.XMag_GiacenzaPallet
|
|
WHERE BarcodePallet = @Pallet AND (IDCella <> @TargetCell OR IDCella IS NULL)
|
|
)
|
|
THROW 51005, 'Reset: ubicazione finale incoerente. Movimento annullato.', 1;
|
|
|
|
EXEC dbo.py_sp_ControllaPrenotazionePackingListPalletNew;
|
|
SELECT @TargetCell AS TargetIDCella;
|
|
"""
|
|
|
|
|
|
async def reset_pallet_async(db_client, *, barcode_pallet: str,
|
|
source_idcella: int, utente: str) -> int:
|
|
"""Move one UDC, returning 9999 (definitive document) or 1000 (otherwise).
|
|
|
|
source_idcella is the location shown in the reset selection, not a new
|
|
destination. Any SQL failure rolls back this UDC through the DB client.
|
|
"""
|
|
result = await db_client.query_json(SQL_RESET_PALLET, {
|
|
"barcode_pallet": str(barcode_pallet or "").strip(),
|
|
"source_idcella": int(source_idcella),
|
|
"utente": str(utente or "warehouse_ui").strip(),
|
|
}, commit=True)
|
|
return int(result["rows"][0][0])
|