Files
Samuele E. Locatelli bdd7b413e8 Riaggiunto GMW_data
2016-11-22 17:58:00 +01:00

340 lines
11 KiB
Transact-SQL

set xact_abort on;
go
begin transaction;
go
set ANSI_NULLS on;
go
/*****************************************
* STORED stp_DtMtrx_import
*
* Recupera dati Gitterbox/DataMatrix partendo leggendo dati successivi all'ultimo import e importanto in tab GMW
*
* Steamware, S.E.L.
* mod: 2011.04.29
*
****************************************/
ALTER PROCEDURE dbo.stp_DtMtrx_import
(
@CodCS VARCHAR(2),
@IdxPosizione INT,
@CodSoggetto VARCHAR(17)
)
AS
------------------------------------------------------------------------------------------------------
-- leggo data ultimo import (o creo record...)
------------------------------------------------------------------------------------------------------
DECLARE @nomeFlusso AS NVARCHAR(50)
DECLARE @lastImport AS DATETIME
DECLARE @trovati AS INT
-- imposto valori
SET @nomeFlusso = 'DataMatrix'
SET @lastImport = DATEADD(yy,-10,GETDATE()) -- inizializzo a -10 anni ...
-- sistemo tab registrazione import...
BEGIN TRAN
-- cerco nella tab log ultima data caricamento
SET @trovati = (
SELECT COUNT(*)
FROM logImportFlussi
WHERE NomeFlusso = @nomeFlusso
)
-- controllo se record c'è...
IF(@trovati > 0)
BEGIN
SET @lastImport = ( SELECT LastImport FROM LogImportFlussi WHERE NomeFlusso = @nomeFlusso )
END
ELSE
BEGIN
-- lo creo!
INSERT INTO LogImportFlussi(NomeFlusso, LastImport)
VALUES (@nomeFlusso, @lastImport)
END
COMMIT TRAN
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
-- ora gestisco il vero caricamento...
------------------------------------------------------------------------------------------------------
-- filtro e salvo in tabella... gitterbox validi SOLO se iniziano per "U"
DECLARE @newData TABLE
(
CodDataMatrix numeric(23, 0) ,
CodGitterbox varchar(10) ,
NumConchiglia int ,
NumDisegno numeric(7, 0) ,
EsponenteDisegno tinyint ,
CodDifettoScarto varchar(5) ,
InizioPreparazione smalldatetime ,
FinePreparazione smalldatetime ,
InizioIniezione smalldatetime ,
FineIniezione smalldatetime ,
MagDestinazione varchar(2) ,
DataImport smalldatetime
)
INSERT INTO @newData
SELECT CodDataMatrix, LTRIM(RTRIM(CodGitterbox)), NumConchiglia, NumDisegno, EsponenteDisegno, CodDifettoScarto, InizioPreparazione, FinePreparazione, InizioIniezione, FineIniezione, MagDestinazione, DataImport
FROM v_trasfDataMatrix
WHERE DataImport > @lastImport AND LEFT(CodGitterbox, 1) = 'U'
AND CodDifettoScarto = '00' -- non importerò gli scarti!!!
-- creo UDC da gitterbox nuovi
INSERT INTO ElencoCartellini(UDC, Particolare, DataFus, Qta, CodSoggetto)
SELECT DISTINCT nd.CodGitterbox, null, MIN(InizioIniezione), dbo.getNumDatamatrix(nd.CodGitterbox), @CodSoggetto
FROM @newData nd LEFT OUTER JOIN ElencoCartellini ec ON nd.CodGitterbox = ec.UDC
WHERE ec.UDC IS NULL
GROUP BY nd.CodGitterbox
DECLARE @udc NVARCHAR(50)
DECLARE @Particolare NVARCHAR(50)
DECLARE @NumDisegno NVARCHAR(50)
DECLARE @EsponenteDisegno NVARCHAR(50)
DECLARE @IdxCellaTo INT
DECLARE @adesso DATETIME
DECLARE @posTrovate INT
SET @adesso = GETDATE()
SET @posTrovate = 0
DECLARE cursoreImport CURSOR FOR
SELECT nd.CodGitterbox, tp.Particolare, tp.NumDisegno, tp.EsponenteDisegno
FROM @newData nd
INNER JOIN v_transcParticolari tp ON nd.NumDisegno = tp.NumDisegno AND nd.EsponenteDisegno = tp.EsponenteDisegno
OPEN cursoreImport
FETCH NEXT FROM cursoreImport INTO @udc, @Particolare, @NumDisegno, @EsponenteDisegno
WHILE @@FETCH_STATUS = 0
BEGIN
-- aggiorno gli UDC gitterbox esistenti per quantità tra quelli dei dati da importare...
UPDATE ElencoCartellini
SET CodCS = @CodCS,
IdxPosizione = @IdxPosizione,
CreateDate = @adesso,
Particolare = @Particolare,
DisegnoGrezzo = @NumDisegno,
Esponente = @EsponenteDisegno,
Figura = '',
CodImballo = '',
Tara = 0,
PesoTot = 0,
PesoCad = 0,
CodStato = 'Fin',
NumCont = 1,
TurnoFus = 0,
ModDate = @adesso,
CodSoggetto = @CodSoggetto
WHERE UDC = @udc
-- indico cella UDC corrente... calcolo la prima cella della nuova posizione...
SET @IdxCellaTo = ( SELECT dbo.f_getCellaByPos(CAST(@IdxPosizione AS NVARCHAR(50))) )
/*(
SELECT TOP 1 IdxCella FROM Celle
WHERE IdxBlocco = (
SELECT IdxBlocco FROM Blocchi
WHERE CodMag=CAST(@IdxPosizione AS NVARCHAR(50)))
)*/
-- controllo se posizione non ancora creata...
SET @posTrovate = ( SELECT COUNT(*) FROM PosizioneUdcCorrente WHERE UDC = @udc )
IF( @posTrovate =0)
BEGIN
INSERT INTO PosizioneUdcCorrente
VALUES (@udc, @IdxCellaTo, @CodCS, @adesso)
END
FETCH NEXT FROM cursoreImport INTO @udc, @Particolare, @NumDisegno, @EsponenteDisegno
END
CLOSE cursoreImport
DEALLOCATE cursoreImport
-- inserisco datamatrix da ultimo import
INSERT INTO ElencoDataMatrix
SELECT * FROM @newData
-- aggiorno dati ultimo caricamento
SET @lastImport = ( SELECT ISNULL(MAX(DataImport),@lastImport) FROM @newData )
UPDATE LogImportFlussi
SET LastImport = @lastImport
WHERE NomeFlusso = @nomeFlusso
RETURN
GO
/*****************************************
* STORED stp_EDM_spostaDataMtx
*
* Sposta un datamatrix in un nuovo gitterbox
*
* Steamware, S.E.L.
* mod: 2011.05.05
*
****************************************/
ALTER PROCEDURE dbo.stp_EDM_spostaDataMtx
(
@CodDataMatrix NUMERIC(23),
@CodGitterbox NVARCHAR(10),
@CodSoggetto VARCHAR(17)
)
AS
-- salvo codice gitterbox di partenza
DECLARE @CodGitterboxFrom AS NVARCHAR(10)
SET @CodGitterboxFrom = ( SELECT CodGitterbox FROM ElencoDataMatrix WHERE CodDataMatrix = @CodDataMatrix )
UPDATE ElencoDataMatrix
SET CodGitterbox = @CodGitterbox
WHERE CodDataMatrix = @CodDataMatrix
-- aggiorno valori QTA gitterbox di partenza...
UPDATE ElencoCartellini
SET Qta = ( SELECT COUNT(*) FROM ElencoDataMatrix WHERE CodGitterbox = @CodGitterboxFrom )
WHERE UDC = @CodGitterboxFrom
-- aggiorno valoti QTA gitterbox destinazione e modificatore...
UPDATE ElencoCartellini
SET Qta = ( SELECT COUNT(*) FROM ElencoDataMatrix WHERE CodGitterbox = @CodGitterbox ), CodSoggetto = @CodSoggetto, ModDate = GETDATE()
WHERE UDC = @CodGitterbox
-- aggiorno righe liste di prelievo...
UPDATE RigheListePrelievo
SET Qta = ( SELECT COUNT(*) FROM ElencoDataMatrix WHERE CodGitterbox = @CodGitterboxFrom )
WHERE UDC = @CodGitterboxFrom
-- aggiorno valoti QTA gitterbox destinazione...
UPDATE RigheListePrelievo
SET Qta = ( SELECT COUNT(*) FROM ElencoDataMatrix WHERE CodGitterbox = @CodGitterbox )
WHERE UDC = @CodGitterbox
-- select finale!
SELECT *
FROM ElencoDataMatrix
WHERE CodDataMatrix = @CodDataMatrix
RETURN
GO
/*****************************************
* STORED stp_EDM_svuotaGitterbox
*
* Elimina il codice gitterbox dai datamatrix associati (svuotandolo...)
*
* Steamware, S.E.L.
* mod: 2011.05.06
*
****************************************/
ALTER PROCEDURE dbo.stp_EDM_svuotaGitterbox
(
@CodGitterbox NVARCHAR(10),
@CodSoggetto VARCHAR(17)
)
AS
UPDATE ElencoDataMatrix
SET CodGitterbox = 'EmptyGBox'
WHERE CodGitterbox = @CodGitterbox
-- aggiorno valoti QTA gitterbox originale...
UPDATE ElencoCartellini
SET Qta = ( SELECT COUNT(*) FROM ElencoDataMatrix WHERE CodGitterbox = @CodGitterbox )
WHERE UDC = @CodGitterbox
-- aggiorno valoti QTA gitterbox 'EmptyGBox
UPDATE ElencoCartellini
SET Qta = ( SELECT COUNT(*) FROM ElencoDataMatrix WHERE CodGitterbox = 'EmptyGBox' ), CodSoggetto = @CodSoggetto, ModDate = GETDATE()
WHERE UDC = 'EmptyGBox'
-- aggiorno righe liste di prelievo...
UPDATE RigheListePrelievo
SET Qta = ( SELECT COUNT(*) FROM ElencoDataMatrix WHERE CodGitterbox = @CodGitterbox )
WHERE UDC = @CodGitterbox
RETURN
GO
/*****************************************
* STORED stp_ODETTE_getByUDC
*
* Recupera etichette odette da stampare (dato un codice UDC)
*
* Steamware, S.E.L.
* mod: 2011.04.27
*
****************************************/
ALTER PROCEDURE dbo.stp_ODETTE_getByUDC
(
@UDC NVARCHAR(10)
)
AS
/* calcolo il codice di raggrupp bolla da passare x generazione cartellino al report partendo da CodCS */
SELECT Odette.CampoUDC, Odette.Campo1_1, Odette.Campo1_2, Odette.Campo1_3, Odette.Campo1_4, Odette.Campo1_5, Odette.Campo2_1, Odette.Campo2_2,
Odette.Campo2_3, Odette.Campo2_4, Odette.Campo2_5, Odette.Campo3_1, dbo.f_padRight(RilPro.TabDecodBolla.NumRaggrMag, 3, '0')
+ dbo.f_padLeft(Odette.Campo3_2, 6, '0') AS Campo3_2, Odette.Campo3_3, Odette.Campo3_4, Odette.Campo3_5, Odette.Campo4_1, Odette.Campo4_2,
Odette.Campo4_3, Odette.Campo4_4, Odette.Campo4_5, Odette.Campo5_1, Odette.Campo5_2, Odette.Campo5_3, Odette.Campo5_4, Odette.Campo5_5,
Odette.Campo6_1, Odette.Campo6_2, Odette.Campo6_3, Odette.Campo6_4, Odette.Campo6_5, Odette.Campo7_1, Odette.Campo7_2, Odette.Campo7_3,
Odette.Campo7_4, Odette.Campo7_5, Odette.Campo8_1, Odette.Campo8_2, Odette.Campo8_3, Odette.Campo8_4, Odette.Campo8_5, Odette.Campo9_1,
Odette.Campo9_2, Odette.Campo9_3, Odette.Campo9_4, Odette.Campo9_5, Odette.Campo10_1, Odette.Campo10_2, Odette.Campo10_3, Odette.Campo10_4,
Odette.Campo10_5, Odette.Campo11_1_1, Odette.Campo11_1_2, Odette.Campo11_1_3, Odette.Campo11_1_4, Odette.Campo11_1_5, Odette.Campo11_2_1,
Odette.Campo11_2_2, Odette.Campo11_2_3, Odette.Campo11_2_4, Odette.Campo11_2_5, Odette.Campo12_1, Odette.Campo12_2, Odette.Campo12_3,
Odette.Campo12_4, Odette.Campo12_5, Odette.Campo13_1, Odette.Campo13_2, Odette.Campo13_3, Odette.Campo13_4, Odette.Campo13_5, Odette.Campo14_1,
Odette.Campo14_2, Odette.Campo14_3, Odette.Campo14_4, Odette.Campo14_5, Odette.Campo15_1, Odette.Campo15_2, Odette.Campo15_3, Odette.Campo15_4,
Odette.Campo15_5, Odette.Campo16_1, Odette.Campo16_2, Odette.Campo16_3, Odette.Campo16_4, Odette.Campo16_5, Odette.StatoOk, Odette.CodCS,
Odette.CodMag, Odette.GrpBolla, Odette.DataBolla, Odette.NumBolla, Odette.Particolare, Odette.CodLista
FROM Odette INNER JOIN RilPro.TabDecodBolla ON Odette.CodMag = RilPro.TabDecodBolla.CodRaggrMag
WHERE (Odette.CampoUDC = @UDC)
RETURN
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/***************************************
* FUNCTION f_padLeft
*
* fornisce una stringa della lunghezza desiderata aggiungendo a sx il carattere richiesto alla @string originale
*
* Steamware, S.E.L.
* mod: 2010.03.19
*
****************************************/
create FUNCTION [dbo].[f_padRight] (@string VARCHAR(255), @desired_length INTEGER, @pad_character CHAR(1))
RETURNS VARCHAR(255) AS
BEGIN
-- Prefix the required number of spaces to bulk up the string and then replace the spaces with the desired character
RETURN CASE
WHEN LEN(@string) < @desired_length
THEN @string + REPLACE(SPACE(@desired_length - LEN(@string)), ' ', @pad_character)
ELSE @string
END
END
GO
commit;
go
-- registro versione...
INSERT INTO [dbo].[LogUpdateDb] ([Versione],[Data]) VALUES(402, GETDATE())
GO