USE [master]
GO
/****** Object:  Database [Mediseawall]    Script Date: 17/09/2025 15:32:03 ******/
CREATE DATABASE [Mediseawall]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'MediSeawall', FILENAME = N'D:\db\Mediseawall_Mediseawall.MDF' , SIZE = 126912KB , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
 LOG ON 
( NAME = N'MediSeawall_log', FILENAME = N'D:\db\Mediseawall_Mediseawall.LDF' , SIZE = 4672KB , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
GO
ALTER DATABASE [Mediseawall] SET COMPATIBILITY_LEVEL = 140
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [Mediseawall].[dbo].[sp_fulltext_database] @action = 'disable'
end
GO
ALTER DATABASE [Mediseawall] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [Mediseawall] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [Mediseawall] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [Mediseawall] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [Mediseawall] SET ARITHABORT OFF 
GO
ALTER DATABASE [Mediseawall] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [Mediseawall] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [Mediseawall] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [Mediseawall] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [Mediseawall] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [Mediseawall] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [Mediseawall] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [Mediseawall] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [Mediseawall] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [Mediseawall] SET  DISABLE_BROKER 
GO
ALTER DATABASE [Mediseawall] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [Mediseawall] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [Mediseawall] SET TRUSTWORTHY OFF 
GO
ALTER DATABASE [Mediseawall] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO
ALTER DATABASE [Mediseawall] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [Mediseawall] SET READ_COMMITTED_SNAPSHOT OFF 
GO
ALTER DATABASE [Mediseawall] SET HONOR_BROKER_PRIORITY OFF 
GO
ALTER DATABASE [Mediseawall] SET RECOVERY SIMPLE 
GO
ALTER DATABASE [Mediseawall] SET  MULTI_USER 
GO
ALTER DATABASE [Mediseawall] SET PAGE_VERIFY TORN_PAGE_DETECTION  
GO
ALTER DATABASE [Mediseawall] SET DB_CHAINING OFF 
GO
ALTER DATABASE [Mediseawall] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) 
GO
ALTER DATABASE [Mediseawall] SET TARGET_RECOVERY_TIME = 0 SECONDS 
GO
ALTER DATABASE [Mediseawall] SET DELAYED_DURABILITY = DISABLED 
GO
EXEC sys.sp_db_vardecimal_storage_format N'Mediseawall', N'ON'
GO
ALTER DATABASE [Mediseawall] SET QUERY_STORE = OFF
GO
USE [Mediseawall]
GO
/****** Object:  User [sa]    Script Date: 17/09/2025 15:32:03 ******/
CREATE USER [sa] FOR LOGIN [SAM] WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object:  User [reparto]    Script Date: 17/09/2025 15:32:03 ******/
CREATE USER [reparto] WITHOUT LOGIN WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object:  User [raf]    Script Date: 17/09/2025 15:32:03 ******/
CREATE USER [raf] WITHOUT LOGIN WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object:  User [NXSTAGE.DOM\nxadmin1sys]    Script Date: 17/09/2025 15:32:03 ******/
CREATE USER [NXSTAGE.DOM\nxadmin1sys] WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object:  User [NXSTAGE.DOM\mdeadmin]    Script Date: 17/09/2025 15:32:03 ******/
CREATE USER [NXSTAGE.DOM\mdeadmin] WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object:  User [NXSTAGE.DOM\abonvicini]    Script Date: 17/09/2025 15:32:03 ******/
CREATE USER [NXSTAGE.DOM\abonvicini] WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object:  User [MEDI]    Script Date: 17/09/2025 15:32:03 ******/
CREATE USER [MEDI] WITHOUT LOGIN WITH DEFAULT_SCHEMA=[MEDI]
GO
/****** Object:  User [mbadolati]    Script Date: 17/09/2025 15:32:03 ******/
CREATE USER [mbadolati] WITHOUT LOGIN WITH DEFAULT_SCHEMA=[mbadolati]
GO
/****** Object:  User [magazzino]    Script Date: 17/09/2025 15:32:03 ******/
CREATE USER [magazzino] WITHOUT LOGIN WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object:  User [MAG2]    Script Date: 17/09/2025 15:32:03 ******/
CREATE USER [MAG2] WITHOUT LOGIN WITH DEFAULT_SCHEMA=[MAG2]
GO
/****** Object:  User [MAG1]    Script Date: 17/09/2025 15:32:03 ******/
CREATE USER [MAG1] WITHOUT LOGIN WITH DEFAULT_SCHEMA=[MAG1]
GO
/****** Object:  User [amministrazione]    Script Date: 17/09/2025 15:32:03 ******/
CREATE USER [amministrazione] WITHOUT LOGIN WITH DEFAULT_SCHEMA=[dbo]
GO
ALTER ROLE [db_owner] ADD MEMBER [reparto]
GO
ALTER ROLE [db_accessadmin] ADD MEMBER [reparto]
GO
ALTER ROLE [db_owner] ADD MEMBER [raf]
GO
ALTER ROLE [db_accessadmin] ADD MEMBER [raf]
GO
ALTER ROLE [db_accessadmin] ADD MEMBER [MEDI]
GO
ALTER ROLE [db_accessadmin] ADD MEMBER [mbadolati]
GO
ALTER ROLE [db_owner] ADD MEMBER [magazzino]
GO
ALTER ROLE [db_accessadmin] ADD MEMBER [magazzino]
GO
ALTER ROLE [db_owner] ADD MEMBER [MAG2]
GO
ALTER ROLE [db_owner] ADD MEMBER [MAG1]
GO
ALTER ROLE [db_owner] ADD MEMBER [amministrazione]
GO
ALTER ROLE [db_accessadmin] ADD MEMBER [amministrazione]
GO
/****** Object:  Schema [MAG1]    Script Date: 17/09/2025 15:32:03 ******/
CREATE SCHEMA [MAG1]
GO
/****** Object:  Schema [MAG2]    Script Date: 17/09/2025 15:32:03 ******/
CREATE SCHEMA [MAG2]
GO
/****** Object:  Schema [mbadolati]    Script Date: 17/09/2025 15:32:03 ******/
CREATE SCHEMA [mbadolati]
GO
/****** Object:  Schema [MEDI]    Script Date: 17/09/2025 15:32:03 ******/
CREATE SCHEMA [MEDI]
GO
/****** Object:  Schema [sam]    Script Date: 17/09/2025 15:32:03 ******/
CREATE SCHEMA [sam]
GO
/****** Object:  View [dbo].[vPreparaPackingListSAMA1]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE view [dbo].[vPreparaPackingListSAMA1] as
SELECT     SAMA1.dbo.LOTSER.NUMLOT, SAMA1.dbo.ARTICO.CODICE, SAMA1.dbo.FATRIG.DESCR, 
                      LEFT(SAMA1.dbo.LOTSER.NUMSER, 6) AS UDC, 
                      SUM(SAMA1.dbo.LOTSER.QTTGIAI) AS Qta, SAMA1.dbo.BAMTES.NUMDOC, SAMA1.dbo.BAMTES.DATDOC, SAMA1.dbo.BAMTES.ID, 
                      SAMA1.dbo.BAMTES.DESCRDEST, SAMA1.dbo.NAZIONI.CODICE AS Expr1, 
                      SAMA1.dbo.MTRASP.CODICE + '  ' + SAMA1.dbo.NAZIONI.DESCR AS NAZIONE, SAMA1.dbo.BAMTES.IDMTRASP
FROM         SAMA1.dbo.NAZIONI INNER JOIN
                      SAMA1.dbo.BAMTES ON SAMA1.dbo.NAZIONI.ID = SAMA1.dbo.BAMTES.IDNAZDEST RIGHT OUTER JOIN
                      SAMA1.dbo.LOTTIBF LEFT OUTER JOIN
                      SAMA1.dbo.LOTSER ON SAMA1.dbo.LOTTIBF.IDLOTSER = SAMA1.dbo.LOTSER.ID LEFT OUTER JOIN
                      SAMA1.dbo.ARTICO ON SAMA1.dbo.LOTSER.IDARTICO = SAMA1.dbo.ARTICO.ID LEFT OUTER JOIN
                      SAMA1.dbo.FATRIG ON SAMA1.dbo.LOTTIBF.IDFATRIG = SAMA1.dbo.FATRIG.ID ON 
                      SAMA1.dbo.BAMTES.ID = SAMA1.dbo.FATRIG.IDBAM LEFT OUTER JOIN
                      SAMA1.sam.EXTUC ON LEFT(SAMA1.dbo.LOTSER.NUMSER, 6) = SAMA1.sam.EXTUC.CODICE LEFT OUTER JOIN
                      SAMA1.dbo.MTRASP 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()-10) as int )
 and SAMA1.dbo.LOTSER.NUMLOT <>'00000000000'
 
GROUP BY SAMA1.dbo.LOTSER.NUMLOT, SAMA1.dbo.ARTICO.CODICE, SAMA1.dbo.FATRIG.DESCR, LEFT(SAMA1.dbo.LOTSER.NUMSER, 6), 
                      SAMA1.dbo.BAMTES.NUMDOC, SAMA1.dbo.BAMTES.DATDOC, SAMA1.dbo.BAMTES.ID, SAMA1.dbo.BAMTES.DESCRDEST, 
                      SAMA1.dbo.NAZIONI.CODICE, SAMA1.dbo.NAZIONI.DESCR, SAMA1.dbo.BAMTES.IDMTRASP, SAMA1.dbo.MTRASP.CODICE
GO
/****** Object:  View [dbo].[vPreparaPackingList]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[vPreparaPackingList]
AS
SELECT     NUMLOT, CODICE, DESCR, UDC, Qta, NUMDOC, DATDOC, ID, DESCRDEST, Expr1, NAZIONE
FROM         dbo.vPreparaPackingListSAMA1
WHERE     (NUMLOT <> '')
GO
/****** Object:  Table [dbo].[MagazziniPallet]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[MagazziniPallet](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Tipo] [char](1) NOT NULL,
	[IDRiferimento] [int] NULL,
	[Attributo] [varchar](16) NULL,
	[NumeroPallet] [int] NULL,
	[IDMagazzino] [int] NULL,
	[IDArea] [int] NULL,
	[IDCella] [int] NULL,
	[DataMagazzino] [datetime] NULL,
	[PesoUnitario] [float] NULL,
	[Tara] [float] NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_MagazziniPallet] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Celle]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Celle](
	[ID] [int] NOT NULL,
	[Descrizione] [varchar](32) NULL,
	[IDArea] [int] NOT NULL,
	[IDDimensione] [int] NULL,
	[IDStato] [int] NULL,
	[Ordinamento] [float] NULL,
	[X] [int] NULL,
	[Y] [int] NULL,
	[Z] [int] NULL,
	[Corsia] [varchar](8) NULL,
	[Colonna] [varchar](8) NULL,
	[Fila] [varchar](8) NULL,
	[PortataMassimaCella] [float] NULL,
	[PortataMassimaColonna] [float] NULL,
	[UnitaVolumeOccupata] [float] NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[DelUtente] [varchar](50) NULL,
	[DelDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_Celle_1] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  View [dbo].[XMag_DettaglioPallet]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE VIEW [dbo].[XMag_DettaglioPallet]
AS
SELECT     dbo.MagazziniPallet.ID, dbo.MagazziniPallet.Tipo, dbo.MagazziniPallet.IDRiferimento, dbo.MagazziniPallet.NumeroPallet, 
                      dbo.MagazziniPallet.IDMagazzino, dbo.MagazziniPallet.IDArea, dbo.MagazziniPallet.IDCella, dbo.MagazziniPallet.DataMagazzino, 
                      dbo.MagazziniPallet.PesoUnitario, dbo.MagazziniPallet.Tara, dbo.MagazziniPallet.Attributo, dbo.Celle.IDStato
FROM         dbo.MagazziniPallet INNER JOIN
                      dbo.Celle ON dbo.MagazziniPallet.IDCella = dbo.Celle.ID
WHERE     (dbo.MagazziniPallet.Tipo = 'V')
UNION ALL
SELECT     MagazziniPallet_1.ID, MagazziniPallet_1.Tipo, MagazziniPallet_1.IDRiferimento, MagazziniPallet_1.NumeroPallet, MagazziniPallet_1.IDMagazzino, 
                      MagazziniPallet_1.IDArea, MagazziniPallet_1.IDCella, MagazziniPallet_1.DataMagazzino, - (1 * MagazziniPallet_1.PesoUnitario) AS PesoUnitario, 
                      MagazziniPallet_1.Tara, MagazziniPallet_1.Attributo, Celle_1.IDStato
FROM         dbo.MagazziniPallet AS MagazziniPallet_1 INNER JOIN
                      dbo.Celle AS Celle_1 ON MagazziniPallet_1.IDCella = Celle_1.ID
WHERE     (MagazziniPallet_1.Tipo = 'P')

GO
/****** Object:  View [dbo].[XMag_GiacenzaPallet]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

CREATE VIEW [dbo].[XMag_GiacenzaPallet]
AS
SELECT     Attributo AS BarcodePallet, NumeroPallet, IDMagazzino, IDArea, IDCella, SUM(PesoUnitario) AS Peso, Attributo AS CodiceProdotto, IDStato
FROM         dbo.XMag_DettaglioPallet
GROUP BY NumeroPallet, IDMagazzino, IDArea, IDCella, Attributo, IDStato
HAVING      (SUM(PesoUnitario) > 0)

GO
/****** Object:  View [dbo].[XMag_ViewPackingList]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE VIEW [dbo].[XMag_ViewPackingList]
AS
SELECT  TOP 10000   dbo.vPreparaPackingList.UDC AS Pallet, dbo.vPreparaPackingList.NUMLOT AS Lotto, dbo.vPreparaPackingList.CODICE AS Articolo, 
                      dbo.vPreparaPackingList.DESCR AS Descrizione, dbo.vPreparaPackingList.Qta, dbo.vPreparaPackingList.NUMDOC AS Documento, 
                      dbo.vPreparaPackingList.Expr1 AS CodNazione, dbo.vPreparaPackingList.NAZIONE, 
                      CASE WHEN Expr1 = 'DE' THEN 10 ELSE CASE WHEN Expr1 = 'TH' THEN (CASE WHEN SUBSTRING(dbo.vPreparaPackingList.DESCRDEST, 1, 2) 
                      = 'NA' THEN 11 ELSE 13 END) ELSE CASE WHEN Expr1 = 'MEX' THEN 12 ELSE 4 END END END AS Stato, 
                      ISNULL(dbo.XMag_GiacenzaPallet.NumeroPallet, 0) AS PalletCella, ISNULL(dbo.XMag_GiacenzaPallet.IDMagazzino, 1) AS Magazzino, 
                      ISNULL(dbo.XMag_GiacenzaPallet.IDArea, 5) AS Area, ISNULL(dbo.XMag_GiacenzaPallet.IDCella, 1000) AS Cella, ISNULL(dbo.Celle.Ordinamento, 
                      99999) AS Ordinamento, ISNULL(dbo.Celle.Corsia + ' - ' + dbo.Celle.Colonna + ' - ' + dbo.Celle.Fila, 'Non scaff.') AS Ubicazione, 
                      SUBSTRING(dbo.vPreparaPackingList.DESCRDEST, 1, 2) AS DEST, ISNULL(dbo.Celle.IDStato, 0) AS IDStato
FROM         dbo.Celle INNER JOIN
                      dbo.XMag_GiacenzaPallet ON dbo.Celle.ID = dbo.XMag_GiacenzaPallet.IDCella RIGHT OUTER JOIN
                      dbo.vPreparaPackingList ON 
                      dbo.XMag_GiacenzaPallet.BarcodePallet COLLATE SQL_Latin1_General_CP1_CI_AS = dbo.vPreparaPackingList.UDC
GROUP BY dbo.vPreparaPackingList.Expr1, dbo.vPreparaPackingList.NAZIONE, dbo.vPreparaPackingList.UDC, dbo.vPreparaPackingList.NUMDOC, 
                      dbo.vPreparaPackingList.NUMLOT, dbo.vPreparaPackingList.CODICE, dbo.vPreparaPackingList.DESCR, dbo.vPreparaPackingList.Qta, 
                      dbo.XMag_GiacenzaPallet.NumeroPallet, dbo.XMag_GiacenzaPallet.IDMagazzino, dbo.XMag_GiacenzaPallet.IDArea, 
                      dbo.XMag_GiacenzaPallet.IDCella, dbo.Celle.Ordinamento, dbo.Celle.Corsia, dbo.Celle.Colonna, dbo.Celle.Fila, dbo.vPreparaPackingList.DESCRDEST, 
                      dbo.Celle.IDStato
                      ORDER By ISNULL(dbo.Celle.IDStato, 0) DESC, dbo.vPreparaPackingList.NUMDOC, Ordinamento

GO
/****** Object:  View [dbo].[ViewPackingListRestante]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[ViewPackingListRestante]
AS
SELECT Pallet, Lotto, Articolo, Descrizione, Qta, Documento, CodNazione, NAZIONE, Stato, PalletCella, Magazzino, Area, Cella, Ordinamento, Ubicazione, DEST, IDStato
FROM  XMag_ViewPackingList
WHERE (Cella <> 9999)

GO
/****** Object:  Table [dbo].[MagLayout]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[MagLayout](
	[IDArea] [int] NOT NULL,
	[Corsia] [varchar](8) NOT NULL,
	[Riga] [int] NOT NULL,
	[ColonnaI] [int] NOT NULL,
	[ColonnaF] [int] NOT NULL,
	[Colore] [char](16) NULL,
	[Nota] [char](32) NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_MagLayout] PRIMARY KEY CLUSTERED 
(
	[IDArea] ASC,
	[Corsia] ASC,
	[Riga] ASC,
	[ColonnaI] ASC,
	[ColonnaF] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Aree]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Aree](
	[ID] [int] NOT NULL,
	[IDMagazzino] [int] NOT NULL,
	[Descrizione] [varchar](64) NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[DelUtente] [varchar](50) NULL,
	[DelDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_Celle] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  View [dbo].[XMag_ViewLayout]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[XMag_ViewLayout]
AS
SELECT     dbo.MagLayout.IDArea, dbo.MagLayout.Corsia, dbo.MagLayout.Riga, dbo.MagLayout.ColonnaI, dbo.MagLayout.ColonnaF, dbo.MagLayout.Colore, 
                      dbo.MagLayout.Nota, dbo.Aree.IDMagazzino
FROM         dbo.MagLayout INNER JOIN
                      dbo.Aree ON dbo.MagLayout.IDArea = dbo.Aree.ID

GO
/****** Object:  View [dbo].[vViewMappaturaDescrizioneCorsia]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

 CREATE VIEW [dbo].[vViewMappaturaDescrizioneCorsia]
AS


/****** Script per comando SelectTopNRows da SSMS  ******/
SELECT [IDArea] as [ID]
      ,[Corsia] as CorsiaDescrizione
	  ,[IDArea] as Area
      , IDMagazzino
   FROM [Mediseawall].[dbo].[XMag_ViewLayout]

GO
/****** Object:  View [dbo].[vViewMappaturaPosizCorsia]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

 CREATE VIEW [dbo].[vViewMappaturaPosizCorsia]
AS
SELECT TOP 1000 [IDArea] as mc_X
, 1 as minFila
, [Riga] as maxFila
, [ColonnaI] as minColonna
, [ColonnaF] as maxColonna
, IDMagazzino as mc_Numero_Magazzino
, [IDArea]  as mc_Numero_Area

  FROM [Mediseawall].[dbo].[XMag_ViewLayout]
GO
/****** Object:  View [dbo].[XMag_GiacenzaPalletxUbicazioneCellaMag]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE VIEW [dbo].[XMag_GiacenzaPalletxUbicazioneCellaMag]
AS
SELECT    dbo.Celle.ID as IDCella
,  dbo.XMag_GiacenzaPallet.BarcodePallet as Pallet
, UPPER(REPLICATE('0', 3 - DATALENGTH(dbo.Celle.Corsia)) + dbo.Celle.Corsia + '.' + + REPLICATE('0', 2 - DATALENGTH(dbo.Celle.Colonna)) 
                      + dbo.Celle.Colonna + '.' + REPLICATE('0', 2 - DATALENGTH(dbo.Celle.Fila)) + dbo.Celle.Fila) AS Ubicazione
FROM         dbo.XMag_GiacenzaPallet INNER JOIN
                      dbo.Celle ON dbo.XMag_GiacenzaPallet.IDCella = dbo.Celle.ID
WHERE dbo.Celle.ID <> 9999

GO
/****** Object:  Table [dbo].[LogOperation]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[LogOperation](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Code] [varchar](64) NULL,
	[Description] [varchar](255) NULL,
	[IDInsUser] [int] NULL,
	[InsDateTime] [datetime] NULL,
	[IDModUser] [int] NULL,
	[ModDateTime] [datetime] NULL,
	[IDDelUser] [int] NULL,
	[DelDateTime] [datetime] NULL,
 CONSTRAINT [PK_LogOperation] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  View [dbo].[vVStatisticaPalletGiorno]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
  CREATE VIEW [dbo].[vVStatisticaPalletGiorno]
AS
  SELECT top 1000 DATEPART(dayofyear, cast([InsDateTime] as datetime)) as Giorno
, cOUNT (id) as ID,  DATEPART(day, cast([InsDateTime] as datetime)) as Day1
  FROM [Mediseawall].[dbo].[LogOperation]
  --WHERE DATEDIFF(MILLISECOND,[InsDateTime], [ModDateTime]) > 2000
 grouP BY DATEPART(dayofyear, cast([InsDateTime] as datetime)), year([InsDateTime]),  DATEPART(day, cast([InsDateTime] as datetime))
 HAVING year([InsDateTime]) = 2019
 ORDER BY DATEPART(dayofyear, cast([InsDateTime] as datetime)) DESC
GO
/****** Object:  View [dbo].[vVStatisticaPalletGiornoDetail]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
  CREATE VIEW [dbo].[vVStatisticaPalletGiornoDetail]
AS
SELECT TOP (1000) [ID]
      ,[Code]
      ,[Description]
      , DATEDIFF(MILLISECOND,[InsDateTime], [ModDateTime]) as difer
      , DATEPART(dayofyear, cast([InsDateTime] as datetime)) as dai
      ,[IDInsUser]
      ,[InsDateTime]
      ,[IDModUser]
      ,[ModDateTime]
      ,[IDDelUser]
      ,[DelDateTime]
  FROM [Mediseawall].[dbo].[LogOperation]
  --WHERE DATEDIFF(MILLISECOND,[InsDateTime], [ModDateTime]) > 2000
  order by 1 desc
GO
/****** Object:  Table [dbo].[Magazzini]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Magazzini](
	[ID] [int] NOT NULL,
	[Codice] [varchar](16) NULL,
	[CodiceDivisione] [varchar](8) NULL,
	[NomeBreve] [varchar](32) NULL,
	[Descrizione] [varchar](64) NULL,
	[FreqRadio] [varchar](1) NULL,
	[Informatizzato] [varchar](1) NULL,
	[ProgressivoPallet] [int] NULL,
	[CodiceStabilimento] [varchar](8) NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[DelUtente] [varchar](50) NULL,
	[DelDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_Magazzini] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  View [dbo].[vViewAzzeraUDCAreaMag6]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
















CREATE VIEW [dbo].[vViewAzzeraUDCAreaMag6]
AS
SELECT        
XMag_GiacenzaPallet.IDArea as ID
, Aree.Descrizione as Area
,XMag_GiacenzaPallet.IDMagazzino 
, Magazzini.Descrizione as Magazzino

, COUNT( XMag_GiacenzaPallet.BarcodePallet) As NPallet
,  XMag_GiacenzaPallet.IDArea  AS BackColor
, 17 AS ForeColor
FROM            XMag_GiacenzaPallet INNER JOIN Magazzini ON XMag_GiacenzaPallet.IDMagazzino = Magazzini.ID
INNER JOIN Aree on XMag_GiacenzaPallet.IDArea = Aree.ID

GROUP BY 

XMag_GiacenzaPallet.IDArea 
, Aree.Descrizione ,
XMag_GiacenzaPallet.IDMagazzino, Magazzini.Descrizione
HAVING XMag_GiacenzaPallet.IDMagazzino = 2 AND NOT IDArea in (7)





GO
/****** Object:  View [dbo].[vViewAzzeraUDCAreaMag5]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
















CREATE VIEW [dbo].[vViewAzzeraUDCAreaMag5]
AS
SELECT        
XMag_GiacenzaPallet.IDArea as ID
, Aree.Descrizione as Area
,XMag_GiacenzaPallet.IDMagazzino 
, Magazzini.Descrizione as Magazzino

, COUNT( XMag_GiacenzaPallet.BarcodePallet) As NPallet
, XMag_GiacenzaPallet.IDArea  AS BackColor
, 17 AS ForeColor
FROM            XMag_GiacenzaPallet INNER JOIN Magazzini ON XMag_GiacenzaPallet.IDMagazzino = Magazzini.ID
INNER JOIN Aree on XMag_GiacenzaPallet.IDArea = Aree.ID

GROUP BY 

XMag_GiacenzaPallet.IDArea 
, Aree.Descrizione ,
XMag_GiacenzaPallet.IDMagazzino, Magazzini.Descrizione
HAVING XMag_GiacenzaPallet.IDMagazzino = 1 AND NOT IDArea in (7)





GO
/****** Object:  View [dbo].[ReportCelle]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[ReportCelle]
AS
SELECT     dbo.Magazzini.ID AS Magazzino, dbo.Aree.ID AS Area, dbo.Celle.ID AS Cella, 0 AS IDCarico, dbo.Celle.Corsia, dbo.Celle.Colonna, dbo.Celle.Fila, 
                      dbo.Celle.IDDimensione, dbo.Celle.Descrizione, dbo.Celle.Ordinamento, dbo.Celle.IDStato, 0 AS pallet, dbo.Celle.X, dbo.Celle.Y, dbo.Celle.Z, 1 AS lato, 
                      0 AS Luogo, 0 AS Colm, 0 AS Piano
FROM         dbo.Celle INNER JOIN
                      dbo.Aree ON dbo.Celle.IDArea = dbo.Aree.ID INNER JOIN
                      dbo.Magazzini ON dbo.Aree.IDMagazzino = dbo.Magazzini.ID

GO
/****** Object:  View [dbo].[vViewAzzeraUDCMagazzino]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO














CREATE VIEW [dbo].[vViewAzzeraUDCMagazzino]
AS
SELECT        XMag_GiacenzaPallet.IDMagazzino as ID
, Magazzini.Descrizione
, COUNT( XMag_GiacenzaPallet.BarcodePallet) As NPallet
,  XMag_GiacenzaPallet.IDMagazzino AS BackColor
, 17 AS ForeColor
FROM            XMag_GiacenzaPallet INNER JOIN
                         Magazzini ON XMag_GiacenzaPallet.IDMagazzino = Magazzini.ID
GROUP BY XMag_GiacenzaPallet.IDMagazzino, Magazzini.Descrizione






GO
/****** Object:  View [dbo].[vXTracciaProdotti]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE VIEW [dbo].[vXTracciaProdotti]
AS
SELECT     LEFT(SAMA1.dbo.LOTSER.NUMSER, 6) AS Pallet, SAMA1.dbo.LOTSER.NUMLOT AS Lotto, SAMA1.dbo.ARTICO.CODICE AS Prodotto, 
                      SAMA1.dbo.ARTICO.DESCR AS Descrizione
FROM         SAMA1.dbo.LOTSER INNER JOIN
                      SAMA1.dbo.ARTICO ON SAMA1.dbo.LOTSER.IDARTICO = SAMA1.dbo.ARTICO.ID
GROUP BY LEFT(SAMA1.dbo.LOTSER.NUMSER, 6), SAMA1.dbo.ARTICO.CODICE, SAMA1.dbo.LOTSER.NUMLOT, SAMA1.dbo.ARTICO.DESCR
having substring(SAMA1.dbo.LOTSER.NUMLOT,1,1)='P'
GO
/****** Object:  View [dbo].[XMag_GiacenzaPalletxUbicazione]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[XMag_GiacenzaPalletxUbicazione]
AS
SELECT     dbo.vXTracciaProdotti.Pallet, dbo.vXTracciaProdotti.Lotto, dbo.vXTracciaProdotti.Prodotto, dbo.vXTracciaProdotti.Descrizione, UPPER(REPLICATE('0', 
                      3 - DATALENGTH(dbo.Celle.Corsia)) + dbo.Celle.Corsia + '.' + + REPLICATE('0', 2 - DATALENGTH(dbo.Celle.Colonna)) 
                      + dbo.Celle.Colonna + '.' + REPLICATE('0', 2 - DATALENGTH(dbo.Celle.Fila)) + dbo.Celle.Fila) AS Ubicazione
FROM         dbo.XMag_GiacenzaPallet INNER JOIN
                      dbo.vXTracciaProdotti ON dbo.XMag_GiacenzaPallet.BarcodePallet = dbo.vXTracciaProdotti.Pallet COLLATE Latin1_General_CI_AS LEFT OUTER JOIN
                      dbo.Celle ON dbo.XMag_GiacenzaPallet.IDCella = dbo.Celle.ID

GO
/****** Object:  Table [dbo].[CelleDimensione]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[CelleDimensione](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Descrizione] [varchar](32) NULL,
	[Dimensione] [float] NULL,
	[UnitaVolume] [float] NULL,
	[A] [float] NULL,
	[B] [float] NULL,
	[C] [float] NULL,
	[IDSolido] [int] NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[DelUtente] [varchar](50) NULL,
	[DelDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_CelleDimensione] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[CelleProdotti]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[CelleProdotti](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[IDCella] [int] NOT NULL,
	[CodiceProdotto] [varchar](64) NOT NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_CelleProdotti] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  View [dbo].[XMag_Layout]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[XMag_Layout]
AS
SELECT     TOP 100 PERCENT dbo.Aree.IDMagazzino, dbo.Celle.IDArea, dbo.Celle.ID AS Cella, dbo.CelleProdotti.CodiceProdotto AS ProdottBase, 
                      dbo.CelleDimensione.ID AS IDDimensione, dbo.CelleDimensione.Descrizione AS DesDim, 0 AS Row, 0 AS Col, ' ' AS Image, ' ' AS ColorCella, 
                      ' ' AS ColorArea, dbo.CelleDimensione.A, dbo.CelleDimensione.B, dbo.CelleDimensione.C, dbo.CelleDimensione.Dimensione, 
                      dbo.CelleDimensione.UnitaVolume, dbo.Celle.IDStato, dbo.Celle.Corsia, dbo.Celle.Colonna, dbo.Celle.Fila, 
                      dbo.XMag_GiacenzaPallet.CodiceProdotto AS ProdottoGiacenza
FROM         dbo.Aree INNER JOIN
                      dbo.Celle ON dbo.Aree.ID = dbo.Celle.IDArea LEFT OUTER JOIN
                      dbo.XMag_GiacenzaPallet ON dbo.Aree.IDMagazzino = dbo.XMag_GiacenzaPallet.IDMagazzino AND 
                      dbo.Celle.ID = dbo.XMag_GiacenzaPallet.IDCella AND dbo.Celle.IDArea = dbo.XMag_GiacenzaPallet.IDArea LEFT OUTER JOIN
                      dbo.CelleDimensione ON dbo.Celle.IDDimensione = dbo.CelleDimensione.ID LEFT OUTER JOIN
                      dbo.CelleProdotti ON dbo.Celle.ID = dbo.CelleProdotti.IDCella
GROUP BY dbo.Aree.IDMagazzino, dbo.Celle.IDArea, dbo.CelleProdotti.CodiceProdotto, dbo.CelleDimensione.ID, dbo.CelleDimensione.Descrizione, 
                      dbo.CelleDimensione.B, dbo.CelleDimensione.A, dbo.CelleDimensione.C, dbo.CelleDimensione.Dimensione, dbo.Celle.IDStato, dbo.Celle.Corsia, 
                      dbo.Celle.Colonna, dbo.Celle.Fila, dbo.XMag_GiacenzaPallet.CodiceProdotto, dbo.Celle.ID, dbo.CelleDimensione.UnitaVolume
ORDER BY dbo.Celle.Fila, dbo.Celle.Colonna

GO
/****** Object:  View [dbo].[vVerificaHighPriority]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[vVerificaHighPriority]
AS
SELECT     TOP 100 PERCENT *, Ordinamento AS Expr1, IDStato AS Expr2
FROM         dbo.XMag_ViewPackingList
WHERE     (Ordinamento > 0) AND (IDStato = 1)
ORDER BY Ordinamento
GO
/****** Object:  View [dbo].[XMag_GiacenzaPalletxUbicazioneCella]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE VIEW [dbo].[XMag_GiacenzaPalletxUbicazioneCella]
AS
SELECT    dbo.Celle.ID as IDCella,  dbo.vXTracciaProdotti.Pallet, dbo.vXTracciaProdotti.Lotto, dbo.vXTracciaProdotti.Prodotto, dbo.vXTracciaProdotti.Descrizione, UPPER(REPLICATE('0', 
                      3 - DATALENGTH(dbo.Celle.Corsia)) + dbo.Celle.Corsia + '.' + + REPLICATE('0', 2 - DATALENGTH(dbo.Celle.Colonna)) 
                      + dbo.Celle.Colonna + '.' + REPLICATE('0', 2 - DATALENGTH(dbo.Celle.Fila)) + dbo.Celle.Fila) AS Ubicazione
FROM         dbo.XMag_GiacenzaPallet INNER JOIN
                      dbo.vXTracciaProdotti ON dbo.XMag_GiacenzaPallet.BarcodePallet = dbo.vXTracciaProdotti.Pallet COLLATE Latin1_General_CI_AS LEFT OUTER JOIN
                      dbo.Celle ON dbo.XMag_GiacenzaPallet.IDCella = dbo.Celle.ID


GO
/****** Object:  View [dbo].[ab_magazzino]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [dbo].[ab_magazzino] as 
select 
c.ID id_cella
,c.Descrizione descr_cella
,x.pallet pallet
,x.lotto  lotto
,x.prodotto codice_prodotto
,x.descrizione descr_prodotto 
,x.ubicazione  locazione
from XMag_GiacenzaPalletxUbicazioneCella x  
join celle c on c.ID=x.idcella 

where x.ubicazione not like '%7G.01.01%'
GO
/****** Object:  View [dbo].[XMag_GiacenzaPalletStessaUbicazione]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE VIEW [dbo].[XMag_GiacenzaPalletStessaUbicazione]
AS
SELECT IDCella,   dbo.vXTracciaProdotti.Pallet, dbo.vXTracciaProdotti.Lotto, dbo.vXTracciaProdotti.Prodotto, dbo.vXTracciaProdotti.Descrizione, UPPER(REPLICATE('0', 
                      3 - DATALENGTH(dbo.Celle.Corsia)) + dbo.Celle.Corsia + '.' + + REPLICATE('0', 2 - DATALENGTH(dbo.Celle.Colonna)) 
                      + dbo.Celle.Colonna + '.' + REPLICATE('0', 2 - DATALENGTH(dbo.Celle.Fila)) + dbo.Celle.Fila) AS Ubicazione
FROM         dbo.XMag_GiacenzaPallet INNER JOIN
                      dbo.vXTracciaProdotti ON dbo.XMag_GiacenzaPallet.BarcodePallet = dbo.vXTracciaProdotti.Pallet COLLATE Latin1_General_CI_AS LEFT OUTER JOIN
                      dbo.Celle ON dbo.XMag_GiacenzaPallet.IDCella = dbo.Celle.ID

WHERE IDCella <> 9999 AND IDCella IN (
SELECT     XMag_GiacenzaPallet.IDCella
--, COUNT(XMag_GiacenzaPallet.BarcodePallet) AS Expr1, UPPER(REPLICATE('0', 3 - DATALENGTH(Celle.Corsia)) 
--                      + Celle.Corsia + '.' + + REPLICATE('0', 2 - DATALENGTH(Celle.Colonna)) + Celle.Colonna + '.' + REPLICATE('0', 2 - DATALENGTH(Celle.Fila)) + Celle.Fila) 
--                      AS Ubicazione
FROM         XMag_GiacenzaPallet LEFT OUTER JOIN
                      Celle ON XMag_GiacenzaPallet.IDCella = Celle.ID
GROUP BY XMag_GiacenzaPallet.IDCella, UPPER(REPLICATE('0', 3 - DATALENGTH(Celle.Corsia)) + Celle.Corsia + '.' + + REPLICATE('0', 2 - DATALENGTH(Celle.Colonna)) 
                      + Celle.Colonna + '.' + REPLICATE('0', 2 - DATALENGTH(Celle.Fila)) + Celle.Fila)
HAVING      (XMag_GiacenzaPallet.IDCella <> 9999) AND  COUNT(XMag_GiacenzaPallet.BarcodePallet)  > 1
--ORDER BY XMag_GiacenzaPallet.IDCella
)

GO
/****** Object:  View [dbo].[XMag_GiacenzaPalletPlistChiuse]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[XMag_GiacenzaPalletPlistChiuse]
AS
SELECT [BarcodePallet]
      ,[NumeroPallet]
      ,[IDMagazzino]
      ,[IDArea]
      ,[IDCella]
      ,[Peso]
      ,[CodiceProdotto]
      ,[IDStato]
  FROM [Mediseawall].[dbo].[XMag_GiacenzaPallet]
  WHERE idcella <> 9999 AND CAST([BarcodePallet] AS CHAR(6)) COLLATE SQL_Latin1_General_CP1_CI_AS IN
  (
  SELECT    CAST(LEFT(SAMA1.dbo.LOTSER.NUMSER, 6)  AS CHAR(6))
FROM         SAMA1.dbo.NAZIONI INNER JOIN
                      SAMA1.dbo.BAMTES ON SAMA1.dbo.NAZIONI.ID = SAMA1.dbo.BAMTES.IDNAZDEST RIGHT OUTER JOIN
                      SAMA1.dbo.LOTTIBF LEFT OUTER JOIN
                      SAMA1.dbo.LOTSER ON SAMA1.dbo.LOTTIBF.IDLOTSER = SAMA1.dbo.LOTSER.ID LEFT OUTER JOIN
                      SAMA1.dbo.ARTICO ON SAMA1.dbo.LOTSER.IDARTICO = SAMA1.dbo.ARTICO.ID LEFT OUTER JOIN
                      SAMA1.dbo.FATRIG ON SAMA1.dbo.LOTTIBF.IDFATRIG = SAMA1.dbo.FATRIG.ID ON 
                      SAMA1.dbo.BAMTES.ID = SAMA1.dbo.FATRIG.IDBAM LEFT OUTER JOIN
                      SAMA1.sam.EXTUC ON LEFT(SAMA1.dbo.LOTSER.NUMSER, 6) = SAMA1.sam.EXTUC.CODICE LEFT OUTER JOIN
                      SAMA1.dbo.MTRASP ON SAMA1.dbo.BAMTES.IDMTRASP = SAMA1.dbo.MTRASP.ID
WHERE     (SAMA1.dbo.BAMTES.ANNDOC >= 2011) 
AND (SAMA1.dbo.BAMTES.STATO = 'D')AND NOT(LEFT(SAMA1.dbo.LOTSER.NUMSER, 6) IS NULL) 
--AND (SAMA1.dbo.BAMTES.NUMDOC = 97)
GROUP BY SAMA1.dbo.BAMTES.STATO, SAMA1.dbo.LOTSER.NUMLOT, SAMA1.dbo.ARTICO.CODICE, SAMA1.dbo.FATRIG.DESCR, LEFT(SAMA1.dbo.LOTSER.NUMSER, 6), 
                      SAMA1.dbo.BAMTES.NUMDOC, SAMA1.dbo.BAMTES.DATDOC, SAMA1.dbo.BAMTES.ID, SAMA1.dbo.BAMTES.DESCRDEST, 
                      SAMA1.dbo.NAZIONI.CODICE, SAMA1.dbo.NAZIONI.DESCR, SAMA1.dbo.BAMTES.IDMTRASP, SAMA1.dbo.MTRASP.CODICE

  )
GO
/****** Object:  View [dbo].[XMag_GiacenzaPalletDaLiberare]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[XMag_GiacenzaPalletDaLiberare]
AS
SELECT [IDCella]
      ,[Pallet]
      ,[Lotto]
      ,[Prodotto]
      ,[Descrizione]
      ,[Ubicazione]
  FROM [Mediseawall].[dbo].[XMag_GiacenzaPalletStessaUbicazione]
  WHERE [Pallet]  COLLATE SQL_Latin1_General_CP1_CI_AS IN (SELECT [BarcodePallet]

  FROM [Mediseawall].[dbo].[XMag_GiacenzaPalletPlistChiuse])
  --ORDER BY Pallet
GO
/****** Object:  Table [dbo].[Accettazione]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Accettazione](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Tipo] [char](1) NOT NULL,
	[IDRiferimento] [int] NULL,
	[Attributo] [varchar](16) NULL,
	[NumeroPallet] [int] NULL,
	[IDMagazzino] [int] NULL,
	[IDArea] [int] NULL,
	[IDCella] [int] NULL,
	[DataMagazzino] [datetime] NULL,
	[PesoUnitario] [float] NULL,
	[Tara] [float] NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_Accettazione] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  View [dbo].[XMag_DettaglioAccettazione]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE VIEW [dbo].[XMag_DettaglioAccettazione]
AS
SELECT     dbo.Accettazione.ID, dbo.Accettazione.Tipo, dbo.Accettazione.IDRiferimento, dbo.Accettazione.NumeroPallet, 
                      dbo.Accettazione.IDMagazzino, dbo.Accettazione.IDArea, dbo.Accettazione.IDCella, dbo.Accettazione.DataMagazzino, 
                      dbo.Accettazione.PesoUnitario, dbo.Accettazione.Tara, dbo.Accettazione.Attributo, dbo.Celle.IDStato
FROM         dbo.Accettazione INNER JOIN
                      dbo.Celle ON dbo.Accettazione.IDCella = dbo.Celle.ID
WHERE     (dbo.Accettazione.Tipo = 'V')
UNION ALL
SELECT     Accettazione_1.ID, Accettazione_1.Tipo, Accettazione_1.IDRiferimento, Accettazione_1.NumeroPallet, Accettazione_1.IDMagazzino, 
                      Accettazione_1.IDArea, Accettazione_1.IDCella, Accettazione_1.DataMagazzino, - (1 * Accettazione_1.PesoUnitario) AS Expr1, 
                      Accettazione_1.Tara, Accettazione_1.Attributo, Celle_1.IDStato
FROM         dbo.Accettazione AS Accettazione_1 INNER JOIN
                      dbo.Celle AS Celle_1 ON Accettazione_1.IDCella = Celle_1.ID
WHERE     (Accettazione_1.Tipo = 'P')


GO
/****** Object:  View [dbo].[XMag_GiacenzaAccettazione]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO


CREATE VIEW [dbo].[XMag_GiacenzaAccettazione]
AS
SELECT     Attributo AS BarcodePallet, NumeroPallet, IDMagazzino, IDArea, IDCella, SUM(PesoUnitario) AS Peso, Attributo AS CodiceProdotto, IDStato
FROM         dbo.XMag_DettaglioAccettazione
GROUP BY NumeroPallet, IDMagazzino, IDArea, IDCella, Attributo, IDStato
HAVING      (SUM(PesoUnitario) > 0)


GO
/****** Object:  View [dbo].[VIEW1]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[VIEW1]
AS
SELECT     NUMDOC, DATDOC, ID, DESCRDEST, IDMTRASP
FROM         SAMA1.dbo.BAMTES
WHERE     (ANNDOC >= 2011) AND (STATO = 'P')
GROUP BY NUMDOC, DATDOC, ID, DESCRDEST, IDMTRASP
GO
/****** Object:  View [dbo].[vPreparaPackingListOld]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[vPreparaPackingListOld]
AS
SELECT     [Medisystems].[dbo].LOTSER.NUMLOT, [Medisystems].[dbo].ARTICO.CODICE, [Medisystems].[dbo].FATRIG.DESCR, LEFT([Medisystems].[dbo].LOTSER.NUMSER, 6) AS UDC, SUM([Medisystems].[dbo].LOTSER.QTTGIAI) AS Qta, 
                      [Medisystems].[dbo].BAMTES.NUMDOC, [Medisystems].[dbo].BAMTES.DATDOC, [Medisystems].[dbo].BAMTES.ID, [Medisystems].[dbo].BAMTES.DESCRDEST, [Medisystems].[dbo].NAZIONI.CODICE AS Expr1, 
                      [Medisystems].[dbo].NAZIONI.DESCR AS NAZIONE
FROM         [Medisystems].[dbo].NAZIONI INNER JOIN
                      [Medisystems].[dbo].BAMTES ON [Medisystems].[dbo].NAZIONI.ID = [Medisystems].[dbo].BAMTES.IDNAZDEST RIGHT OUTER JOIN
                      [Medisystems].[dbo].LOTTIBF LEFT OUTER JOIN
                      [Medisystems].[dbo].LOTSER ON [Medisystems].[dbo].LOTTIBF.IDLOTSER = [Medisystems].[dbo].LOTSER.ID LEFT OUTER JOIN
                      [Medisystems].[dbo].ARTICO ON [Medisystems].[dbo].LOTSER.IDARTICO = [Medisystems].[dbo].ARTICO.ID LEFT OUTER JOIN
                      [Medisystems].[dbo].FATRIG ON [Medisystems].[dbo].LOTTIBF.IDFATRIG = [Medisystems].[dbo].FATRIG.ID ON [Medisystems].[dbo].BAMTES.ID = [Medisystems].[dbo].FATRIG.IDBAM LEFT OUTER JOIN
                      [Medisystems].[dbo].EXTUC ON LEFT([Medisystems].[dbo].LOTSER.NUMSER, 6) = [Medisystems].[dbo].EXTUC.CODICE
WHERE     ([Medisystems].[dbo].BAMTES.ANNDOC = 2010) AND ([Medisystems].[dbo].BAMTES.STATO = 'P')
GROUP BY [Medisystems].[dbo].LOTSER.NUMLOT, [Medisystems].[dbo].ARTICO.CODICE, [Medisystems].[dbo].FATRIG.DESCR, LEFT([Medisystems].[dbo].LOTSER.NUMSER, 6), [Medisystems].[dbo].BAMTES.NUMDOC, [Medisystems].[dbo].BAMTES.DATDOC, 
                      [Medisystems].[dbo].BAMTES.ID, [Medisystems].[dbo].BAMTES.DESCRDEST, [Medisystems].[dbo].NAZIONI.CODICE, [Medisystems].[dbo].NAZIONI.DESCR
HAVING      (NOT (LEFT([Medisystems].[dbo].LOTSER.NUMSER, 6) IS NULL)) AND (LEFT([Medisystems].[dbo].LOTSER.NUMSER, 6) <> '000000')
GO
/****** Object:  View [dbo].[vPreparaPackingListSAMA1Chiuse]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[vPreparaPackingListSAMA1Chiuse]
AS
SELECT     SAMA1.dbo.LOTSER.NUMLOT, SAMA1.dbo.ARTICO.CODICE, SAMA1.dbo.FATRIG.DESCR, LEFT(SAMA1.dbo.LOTSER.NUMSER, 6) AS UDC, 
                      SUM(SAMA1.dbo.LOTSER.QTTGIAI) AS Qta, SAMA1.dbo.BAMTES.NUMDOC, SAMA1.dbo.BAMTES.DATDOC, SAMA1.dbo.BAMTES.ID, 
                      SAMA1.dbo.BAMTES.DESCRDEST, SAMA1.dbo.NAZIONI.CODICE AS Expr1, 
                      SAMA1.dbo.MTRASP.CODICE + '  ' + SAMA1.dbo.NAZIONI.DESCR AS NAZIONE, SAMA1.dbo.BAMTES.IDMTRASP
FROM         SAMA1.dbo.NAZIONI INNER JOIN
                      SAMA1.dbo.BAMTES ON SAMA1.dbo.NAZIONI.ID = SAMA1.dbo.BAMTES.IDNAZDEST RIGHT OUTER JOIN
                      SAMA1.dbo.LOTTIBF LEFT OUTER JOIN
                      SAMA1.dbo.LOTSER ON SAMA1.dbo.LOTTIBF.IDLOTSER = SAMA1.dbo.LOTSER.ID LEFT OUTER JOIN
                      SAMA1.dbo.ARTICO ON SAMA1.dbo.LOTSER.IDARTICO = SAMA1.dbo.ARTICO.ID LEFT OUTER JOIN
                      SAMA1.dbo.FATRIG ON SAMA1.dbo.LOTTIBF.IDFATRIG = SAMA1.dbo.FATRIG.ID ON 
                      SAMA1.dbo.BAMTES.ID = SAMA1.dbo.FATRIG.IDBAM LEFT OUTER JOIN
                      SAMA1.sam.EXTUC ON LEFT(SAMA1.dbo.LOTSER.NUMSER, 6) = SAMA1.sam.EXTUC.CODICE LEFT OUTER JOIN
                      SAMA1.dbo.MTRASP ON SAMA1.dbo.BAMTES.IDMTRASP = SAMA1.dbo.MTRASP.ID
WHERE     (SAMA1.dbo.BAMTES.ANNDOC >= 2011) AND (SAMA1.dbo.BAMTES.STATO = 'D')
GROUP BY SAMA1.dbo.LOTSER.NUMLOT, SAMA1.dbo.ARTICO.CODICE, SAMA1.dbo.FATRIG.DESCR, LEFT(SAMA1.dbo.LOTSER.NUMSER, 6), 
                      SAMA1.dbo.BAMTES.NUMDOC, SAMA1.dbo.BAMTES.DATDOC, SAMA1.dbo.BAMTES.ID, SAMA1.dbo.BAMTES.DESCRDEST, 
                      SAMA1.dbo.NAZIONI.CODICE, SAMA1.dbo.NAZIONI.DESCR, SAMA1.dbo.BAMTES.IDMTRASP, SAMA1.dbo.MTRASP.CODICE

GO
/****** Object:  View [dbo].[vXTracciaOld]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[vXTracciaOld]
AS
SELECT     LEFT(Medisystems.dbo.LOTSER.NUMSER, 6) AS Pallet, Medisystems.dbo.LOTSER.NUMLOT AS Lotto, Medisystems.dbo.ARTICO.CODICE AS Prodotto, 
                      Medisystems.dbo.ARTICO.DESCR AS Descrizione
FROM         Medisystems.dbo.LOTSER INNER JOIN
                      Medisystems.dbo.ARTICO ON Medisystems.dbo.LOTSER.IDARTICO = Medisystems.dbo.ARTICO.ID
GROUP BY LEFT(Medisystems.dbo.LOTSER.NUMSER, 6), Medisystems.dbo.ARTICO.CODICE, Medisystems.dbo.LOTSER.NUMLOT, 
                      Medisystems.dbo.ARTICO.DESCR
HAVING      (Medisystems.dbo.LOTSER.NUMLOT LIKE 'P10%')

GO
/****** Object:  Table [dbo].[Accessi]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Accessi](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[NomeBreve] [varchar](32) NULL,
	[Pagina] [nvarchar](255) NULL,
	[Utente] [nvarchar](50) NULL,
	[DataInserimento] [datetime] NULL,
	[DataOraFine] [datetime] NULL,
	[TempoTransito] [float] NULL,
	[TempoIndeterminato] [float] NULL,
	[Device] [varchar](255) NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
 CONSTRAINT [PK_Accessi] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[CelleStati]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[CelleStati](
	[ID] [int] NOT NULL,
	[Descrizione] [varchar](32) NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[DelUtente] [varchar](50) NULL,
	[DelDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_CelleStati] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Divisioni]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Divisioni](
	[Codice] [varchar](8) NOT NULL,
	[CodiceAnagrafica] [varchar](16) NULL,
	[Descrizione] [varchar](64) NULL,
	[CodicePaese] [varchar](8) NULL,
	[CodiceProduttore] [varchar](8) NULL,
	[Prfx] [varchar](1) NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[DelUtente] [varchar](50) NULL,
	[DelDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_Divisioni] PRIMARY KEY CLUSTERED 
(
	[Codice] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[errorLog]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[errorLog](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Data] [datetime] NULL,
	[Descrizione] [varchar](1024) NULL,
	[Metodo] [varchar](1024) NULL,
	[Libreria] [varchar](1024) NULL,
	[Oggetto] [varchar](1024) NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_errorLog] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[FieldsAttributes]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[FieldsAttributes](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Nome] [varchar](32) NULL,
	[CodiceLingua] [varchar](8) NULL,
	[Testo] [varchar](32) NULL,
	[Contesto] [varchar](16) NULL,
	[Visible] [int] NULL,
	[Enabled] [int] NULL,
	[ReadOnly] [int] NULL,
	[Attributo1] [varchar](32) NULL,
	[Attributo2] [varchar](32) NULL,
	[Attributo3] [varchar](32) NULL,
	[Attributo4] [varchar](32) NULL,
	[Attributo5] [varchar](32) NULL,
	[MachineName] [varchar](64) NULL,
	[Namespace] [varchar](64) NULL,
	[Class] [varchar](64) NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_FieldsAttributes] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[FormatiCodici]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[FormatiCodici](
	[ID] [int] NOT NULL,
	[Formato] [varchar](40) NOT NULL,
	[Nota] [varchar](64) NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[DelUtente] [varchar](50) NULL,
	[DelDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_FormatiCodici] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[LogPackingList]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[LogPackingList](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Code] [varchar](64) NULL,
	[Description] [varchar](255) NULL,
	[IDInsUser] [int] NULL,
	[InsDateTime] [datetime] NULL,
	[IDModUser] [int] NULL,
	[ModDateTime] [datetime] NULL,
	[IDDelUser] [int] NULL,
	[DelDateTime] [datetime] NULL,
 CONSTRAINT [PK_LogPackingList] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Menu]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Menu](
	[IdMenu] [int] NOT NULL,
	[Descrizione] [varchar](64) NULL,
	[MinAccessLevel] [int] NULL,
	[CodiceUnita] [varchar](8) NULL,
 CONSTRAINT [PK_Menu] PRIMARY KEY CLUSTERED 
(
	[IdMenu] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Operatori]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Operatori](
	[ID] [int] NOT NULL,
	[Nominativo] [varchar](64) NULL,
	[CodiceDivisione] [varchar](8) NULL,
	[CodiceReparto] [varchar](8) NULL,
	[CodiceUnita] [varchar](8) NULL,
	[CodiceBadge] [varchar](16) NULL,
	[Login] [varchar](32) NULL,
	[Password] [varchar](16) NULL,
	[Privilegio] [int] NULL,
	[CodiceQualifica] [varchar](8) NULL,
	[Lingua] [varchar](8) NULL,
	[Ruolo] [varchar](64) NULL,
	[Email] [varchar](64) NULL,
	[Telefono] [varchar](64) NULL,
	[Fax] [varchar](64) NULL,
	[Interno] [varchar](64) NULL,
	[VistaCommesse] [varchar](10) NULL,
	[VistaDocumenti] [varchar](50) NULL,
	[IDDatiSistema] [int] NULL,
	[CodiceAnagraficaC] [varchar](16) NULL,
	[CodiceAnagraficaF] [varchar](16) NULL,
	[CodiceProdotto] [varchar](64) NULL,
	[IDAlzaQualifica] [int] NULL,
	[TitoloNominativo] [varchar](64) NULL,
	[IDOPCentrodiCosto] [int] NULL,
	[IDVincolo] [int] NULL,
	[IDInsertToFieldsAttributes] [int] NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[DelUtente] [varchar](50) NULL,
	[DelDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_Operatori] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY],
 CONSTRAINT [IX_Operatori] UNIQUE NONCLUSTERED 
(
	[Login] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Reparti]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Reparti](
	[Codice] [varchar](8) NOT NULL,
	[CodiceStabilimento] [varchar](8) NULL,
	[CodiceDivisione] [varchar](8) NOT NULL,
	[Descrizione] [varchar](64) NULL,
	[PrfxLotto] [int] NULL,
	[PrfxAlias] [varchar](4) NULL,
	[PrfxCodiceLotto] [varchar](4) NULL,
	[IDFormato] [int] NULL,
	[Nota] [varchar](128) NULL,
	[IndiceSSCC] [int] NULL,
	[Prfx] [varchar](1) NULL,
	[VistaCommesse] [varchar](10) NULL,
	[CodiceAnagrafica] [varchar](16) NULL,
	[InsUtente] [varchar](32) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](32) NULL,
	[ModDataOra] [datetime] NULL,
	[DelUtente] [varchar](50) NULL,
	[DelDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_Reparti] PRIMARY KEY CLUSTERED 
(
	[Codice] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Stabilimenti]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Stabilimenti](
	[Codice] [varchar](8) NOT NULL,
	[Descrizione] [varchar](64) NULL,
	[InsUtente] [varchar](50) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](50) NULL,
	[ModDataOra] [datetime] NULL,
	[DelUtente] [varchar](50) NULL,
	[DelDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_Stabilimenti] PRIMARY KEY CLUSTERED 
(
	[Codice] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[SysGestioneViste]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SysGestioneViste](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Valore] [varchar](64) NULL,
	[Descrizione] [varchar](128) NULL,
	[MinAccessLevel] [int] NULL,
	[CodiceUnita] [varchar](8) NULL,
 CONSTRAINT [PK_SysGestioneViste] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[UnitaProduzione]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UnitaProduzione](
	[Codice] [varchar](8) NOT NULL,
	[CodiceReparto] [varchar](8) NOT NULL,
	[Descrizione] [varchar](64) NULL,
	[IDMagPrelievo] [int] NULL,
	[IDMagVersamento] [int] NULL,
	[Nota] [varchar](128) NULL,
	[VistaCommesse] [varchar](10) NULL,
	[VistaDocumenti] [varchar](50) NULL,
	[VistaDettagli] [varchar](50) NULL,
	[InsUtente] [varchar](32) NULL,
	[InsDataOra] [datetime] NULL,
	[ModUtente] [varchar](32) NULL,
	[ModDataOra] [datetime] NULL,
	[DelUtente] [varchar](50) NULL,
	[DelDataOra] [datetime] NULL,
	[VersioneDati] [timestamp] NULL,
 CONSTRAINT [PK_UnitaProduzione] PRIMARY KEY CLUSTERED 
(
	[Codice] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Voci]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Voci](
	[IdVoce] [int] IDENTITY(1,1) NOT NULL,
	[IdMenu] [int] NOT NULL,
	[NomeBreve] [varchar](32) NOT NULL,
	[Descrizione] [varchar](64) NOT NULL,
	[ObjectAssemblyName] [varchar](64) NOT NULL,
	[ObjectClassName] [varchar](64) NOT NULL,
	[ObjectBaseAssemblyName] [varchar](64) NOT NULL,
	[ObjectBaseClassName] [varchar](64) NOT NULL,
	[MinAccessLevel] [int] NULL,
	[LargeImage] [char](10) NULL,
	[LargeImageIndex] [int] NULL,
 CONSTRAINT [PK_Voci] PRIMARY KEY CLUSTERED 
(
	[IdVoce] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Index [IX_MagazziniPallet]    Script Date: 17/09/2025 15:32:03 ******/
CREATE NONCLUSTERED INDEX [IX_MagazziniPallet] ON [dbo].[MagazziniPallet]
(
	[ID] ASC,
	[IDMagazzino] ASC,
	[IDArea] ASC,
	[IDCella] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [IX_MagazziniPallet_1]    Script Date: 17/09/2025 15:32:03 ******/
CREATE NONCLUSTERED INDEX [IX_MagazziniPallet_1] ON [dbo].[MagazziniPallet]
(
	[ID] ASC,
	[IDCella] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [IX_MagazziniPallet_2]    Script Date: 17/09/2025 15:32:03 ******/
CREATE NONCLUSTERED INDEX [IX_MagazziniPallet_2] ON [dbo].[MagazziniPallet]
(
	[ID] ASC,
	[Attributo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Operatori] ADD  CONSTRAINT [DF_Operatori_CodiceQualifica]  DEFAULT ((0)) FOR [CodiceQualifica]
GO
ALTER TABLE [dbo].[Operatori] ADD  CONSTRAINT [DF_Operatori_VistaCommesse]  DEFAULT ('1000000000') FOR [VistaCommesse]
GO
ALTER TABLE [dbo].[Operatori] ADD  CONSTRAINT [DF_Operatori_VistaDocumenti]  DEFAULT ('1000000000') FOR [VistaDocumenti]
GO
ALTER TABLE [dbo].[Operatori] ADD  CONSTRAINT [DF_Operatori_IDDatiSistema_1]  DEFAULT ((1)) FOR [IDDatiSistema]
GO
ALTER TABLE [dbo].[Operatori] ADD  CONSTRAINT [DF_Operatori_CodiceAnagraficaC]  DEFAULT ('REP1') FOR [CodiceAnagraficaC]
GO
ALTER TABLE [dbo].[Operatori] ADD  CONSTRAINT [DF_Operatori_CodiceAnagraficaF]  DEFAULT ('REP1') FOR [CodiceAnagraficaF]
GO
ALTER TABLE [dbo].[Operatori] ADD  CONSTRAINT [DF_Operatori_CodiceProdotto_1]  DEFAULT ('A1') FOR [CodiceProdotto]
GO
ALTER TABLE [dbo].[Operatori] ADD  CONSTRAINT [DF_Operatori_IDAlzaQualifica_1]  DEFAULT ((0)) FOR [IDAlzaQualifica]
GO
ALTER TABLE [dbo].[Operatori] ADD  CONSTRAINT [DF_Operatori_IDInsertToFieldsAttributes]  DEFAULT ((0)) FOR [IDInsertToFieldsAttributes]
GO
ALTER TABLE [dbo].[Reparti] ADD  CONSTRAINT [DF_Reparti_VistaCommesse]  DEFAULT ('1110000000') FOR [VistaCommesse]
GO
ALTER TABLE [dbo].[UnitaProduzione] ADD  CONSTRAINT [DF_UnitaProduzione_VistaCommesse]  DEFAULT ('1000000000') FOR [VistaCommesse]
GO
ALTER TABLE [dbo].[UnitaProduzione] ADD  CONSTRAINT [DF_UnitaProduzione_VistaDocumenti]  DEFAULT ('1000000000') FOR [VistaDocumenti]
GO
ALTER TABLE [dbo].[UnitaProduzione] ADD  CONSTRAINT [DF_UnitaProduzione_VistaDettagli]  DEFAULT ('0001000000') FOR [VistaDettagli]
GO
ALTER TABLE [dbo].[Voci] ADD  CONSTRAINT [DF_Voci_MinAccessLevel]  DEFAULT (0) FOR [MinAccessLevel]
GO
ALTER TABLE [dbo].[Voci] ADD  CONSTRAINT [DF_Voci_LargeImage]  DEFAULT ('ilIcons') FOR [LargeImage]
GO
ALTER TABLE [dbo].[Voci] ADD  CONSTRAINT [DF_Voci_LargeImageIndex]  DEFAULT (1) FOR [LargeImageIndex]
GO
ALTER TABLE [dbo].[Accettazione]  WITH NOCHECK ADD  CONSTRAINT [FK_Accettazione_Aree] FOREIGN KEY([IDArea])
REFERENCES [dbo].[Aree] ([ID])
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[Accettazione] CHECK CONSTRAINT [FK_Accettazione_Aree]
GO
ALTER TABLE [dbo].[Accettazione]  WITH NOCHECK ADD  CONSTRAINT [FK_Accettazione_Celle] FOREIGN KEY([IDCella])
REFERENCES [dbo].[Celle] ([ID])
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[Accettazione] CHECK CONSTRAINT [FK_Accettazione_Celle]
GO
ALTER TABLE [dbo].[Accettazione]  WITH NOCHECK ADD  CONSTRAINT [FK_Accettazione_Magazzini] FOREIGN KEY([IDMagazzino])
REFERENCES [dbo].[Magazzini] ([ID])
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[Accettazione] CHECK CONSTRAINT [FK_Accettazione_Magazzini]
GO
ALTER TABLE [dbo].[Aree]  WITH NOCHECK ADD  CONSTRAINT [FK_Celle_Magazzini] FOREIGN KEY([IDMagazzino])
REFERENCES [dbo].[Magazzini] ([ID])
ON UPDATE CASCADE
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[Aree] CHECK CONSTRAINT [FK_Celle_Magazzini]
GO
ALTER TABLE [dbo].[Celle]  WITH NOCHECK ADD  CONSTRAINT [FK_Celle_Aree] FOREIGN KEY([IDArea])
REFERENCES [dbo].[Aree] ([ID])
ON UPDATE CASCADE
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[Celle] CHECK CONSTRAINT [FK_Celle_Aree]
GO
ALTER TABLE [dbo].[Celle]  WITH NOCHECK ADD  CONSTRAINT [FK_Celle_CelleDimensione] FOREIGN KEY([IDDimensione])
REFERENCES [dbo].[CelleDimensione] ([ID])
ON UPDATE CASCADE
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[Celle] CHECK CONSTRAINT [FK_Celle_CelleDimensione]
GO
ALTER TABLE [dbo].[Celle]  WITH NOCHECK ADD  CONSTRAINT [FK_Celle_CelleStati] FOREIGN KEY([IDStato])
REFERENCES [dbo].[CelleStati] ([ID])
ON UPDATE CASCADE
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[Celle] CHECK CONSTRAINT [FK_Celle_CelleStati]
GO
ALTER TABLE [dbo].[CelleProdotti]  WITH NOCHECK ADD  CONSTRAINT [FK_CelleProdotti_Celle] FOREIGN KEY([IDCella])
REFERENCES [dbo].[Celle] ([ID])
ON UPDATE CASCADE
ON DELETE CASCADE
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[CelleProdotti] CHECK CONSTRAINT [FK_CelleProdotti_Celle]
GO
ALTER TABLE [dbo].[Magazzini]  WITH NOCHECK ADD  CONSTRAINT [FK_Magazzini_Stabilimenti] FOREIGN KEY([CodiceDivisione])
REFERENCES [dbo].[Stabilimenti] ([Codice])
ON UPDATE CASCADE
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[Magazzini] CHECK CONSTRAINT [FK_Magazzini_Stabilimenti]
GO
ALTER TABLE [dbo].[MagazziniPallet]  WITH NOCHECK ADD  CONSTRAINT [FK_MagazziniPallet_Aree] FOREIGN KEY([IDArea])
REFERENCES [dbo].[Aree] ([ID])
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[MagazziniPallet] CHECK CONSTRAINT [FK_MagazziniPallet_Aree]
GO
ALTER TABLE [dbo].[MagazziniPallet]  WITH NOCHECK ADD  CONSTRAINT [FK_MagazziniPallet_Celle] FOREIGN KEY([IDCella])
REFERENCES [dbo].[Celle] ([ID])
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[MagazziniPallet] CHECK CONSTRAINT [FK_MagazziniPallet_Celle]
GO
ALTER TABLE [dbo].[MagazziniPallet]  WITH NOCHECK ADD  CONSTRAINT [FK_MagazziniPallet_Magazzini] FOREIGN KEY([IDMagazzino])
REFERENCES [dbo].[Magazzini] ([ID])
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[MagazziniPallet] CHECK CONSTRAINT [FK_MagazziniPallet_Magazzini]
GO
ALTER TABLE [dbo].[Operatori]  WITH NOCHECK ADD  CONSTRAINT [FK_Operatori_Divisioni] FOREIGN KEY([CodiceDivisione])
REFERENCES [dbo].[Divisioni] ([Codice])
ON UPDATE CASCADE
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[Operatori] CHECK CONSTRAINT [FK_Operatori_Divisioni]
GO
ALTER TABLE [dbo].[Operatori]  WITH NOCHECK ADD  CONSTRAINT [FK_Operatori_Reparti] FOREIGN KEY([CodiceReparto])
REFERENCES [dbo].[Reparti] ([Codice])
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[Operatori] CHECK CONSTRAINT [FK_Operatori_Reparti]
GO
ALTER TABLE [dbo].[Reparti]  WITH NOCHECK ADD  CONSTRAINT [FK_Reparti_Divisioni] FOREIGN KEY([CodiceDivisione])
REFERENCES [dbo].[Divisioni] ([Codice])
ON UPDATE CASCADE
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[Reparti] CHECK CONSTRAINT [FK_Reparti_Divisioni]
GO
ALTER TABLE [dbo].[Reparti]  WITH NOCHECK ADD  CONSTRAINT [FK_Reparti_FormatiCodici] FOREIGN KEY([IDFormato])
REFERENCES [dbo].[FormatiCodici] ([ID])
ON UPDATE CASCADE
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[Reparti] CHECK CONSTRAINT [FK_Reparti_FormatiCodici]
GO
ALTER TABLE [dbo].[Reparti]  WITH NOCHECK ADD  CONSTRAINT [FK_Reparti_Stabilimenti] FOREIGN KEY([CodiceDivisione])
REFERENCES [dbo].[Stabilimenti] ([Codice])
ON UPDATE CASCADE
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[Reparti] CHECK CONSTRAINT [FK_Reparti_Stabilimenti]
GO
ALTER TABLE [dbo].[UnitaProduzione]  WITH NOCHECK ADD  CONSTRAINT [FK_UnitaProduzione_Reparti] FOREIGN KEY([CodiceReparto])
REFERENCES [dbo].[Reparti] ([Codice])
ON UPDATE CASCADE
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[UnitaProduzione] CHECK CONSTRAINT [FK_UnitaProduzione_Reparti]
GO
ALTER TABLE [dbo].[Voci]  WITH NOCHECK ADD  CONSTRAINT [FK_Voci_Menu] FOREIGN KEY([IdMenu])
REFERENCES [dbo].[Menu] ([IdMenu])
ON UPDATE CASCADE
ON DELETE CASCADE
NOT FOR REPLICATION 
GO
ALTER TABLE [dbo].[Voci] CHECK CONSTRAINT [FK_Voci_Menu]
GO
/****** Object:  StoredProcedure [dbo].[BackupDB]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[BackupDB] AS

DECLARE @xxx VARCHAR(120)
SELECT @xxx = 'D:\gest\SEAWALL\Backup\Mediseawall' + CAST(GETDATE()AS CHAR(14))+'.bck'
SELECT @xxx
--BACKUP LOG [MediSeawall] With TRUNCATE_ONLY
BACKUP DATABASE [MediSeawall] TO  DISK = @xxx WITH  NOINIT ,  NOUNLOAD ,  NAME = N'Backup di Seawall',  NOSKIP ,  STATS = 10,  NOFORMAT 


GO
/****** Object:  StoredProcedure [dbo].[CreaNuoveCelle]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[CreaNuoveCelle]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;





DECLARE @RC int
DECLARE @ID int
DECLARE @Descrizione varchar(32)
DECLARE @IDArea int
DECLARE @IDDimensione int
DECLARE @IDStato int
DECLARE @Ordinamento float
DECLARE @X int
DECLARE @Y int
DECLARE @Z int
DECLARE @Corsia varchar(8)
DECLARE @Colonna varchar(8)
DECLARE @Fila varchar(8)
DECLARE @PortataMassimaCella float
DECLARE @PortataMassimaColonna float
DECLARE @UnitaVolumeOccupata float
DECLARE @InsUtente varchar(50)
DECLARE @InsDataOra datetime
DECLARE @ModUtente varchar(50)
DECLARE @ModDataOra datetime

SET @IDArea = 4
SET @Corsia = ' 4D' 
SET @Colonna = '18' 


SET @ID = 1001 
SET @Descrizione = '1001 :  :  1A - 1  - a' 
SET @IDDimensione = 1 
SET @IDStato = 1 
SET @Ordinamento = 1.001000000000000e+003 
SET @X = 0 
SET @Y = 0 
SET @Z = 0 

SET @PortataMassimaCella = 0.000000000000000e+000 
SET @PortataMassimaColonna = 0.000000000000000e+000 
SET @UnitaVolumeOccupata = 0.000000000000000e+000 
SET @InsUtente = 'raf' 
SET @InsDataOra = 'set 11 2010  5:25PM' 
SET @ModUtente = 'raf' 
SET @ModDataOra = 'set 11 2010  5:25PM'

declare @IDDaCopiare int
declare @IDNuovo int

-- Imposta valori dei parametri

-- Declare an inner cursor based   
   -- on au_id from the outer cursor.

   DECLARE IDDaCopiare_cursor CURSOR FOR 


SELECT [ID]
  FROM [MediSeawall].[dbo].[Aree]

   OPEN IDDaCopiare_cursor
   FETCH NEXT FROM IDDaCopiare_cursor INTO @IDDaCopiare

   WHILE @@FETCH_STATUS = 0
   BEGIN

if @IDDaCopiare  = 1 BEGIN SET @Fila = 'a' END
if @IDDaCopiare  = 2 BEGIN SET @Fila = 'b' END
if @IDDaCopiare  = 3 BEGIN SET @Fila = 'c' END
if @IDDaCopiare  = 4 BEGIN SET @Fila = 'd' END
if @IDDaCopiare  = 5 BEGIN SET @Fila = 'e' END
if @IDDaCopiare  = 6 BEGIN SET @Fila = 'f' END

SET @ID = (SELECT MAX([ID]) FROM [MediSeawall].[dbo].[Celle]) + 1

SET @Descrizione = CAST(@ID as char(4)) + ' cors: ' + @Corsia+ ' col: ' + @Colonna + ' fil: ' + @Fila

		EXECUTE @RC = [MediSeawall].[dbo].[spt_SaveCelle] 
		   @ID
		  ,@Descrizione
		  ,@IDArea
		  ,@IDDimensione
		  ,@IDStato
		  ,@Ordinamento
		  ,@X
		  ,@Y
		  ,@Z
		  ,@Corsia
		  ,@Colonna
		  ,@Fila
		  ,@PortataMassimaCella
		  ,@PortataMassimaColonna
		  ,@UnitaVolumeOccupata
		  ,@InsUtente
		  ,@InsDataOra
		  ,@ModUtente
		  ,@ModDataOra
      
      FETCH NEXT FROM IDDaCopiare_cursor INTO @IDDaCopiare
   
   END

   CLOSE IDDaCopiare_cursor
   DEALLOCATE IDDaCopiare_cursor





END

GO
/****** Object:  StoredProcedure [dbo].[CreaNuoveCelleMDE6]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[CreaNuoveCelleMDE6]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;




DECLARE @IDMagazzino int = 2
DECLARE @RC int
DECLARE @ID int
DECLARE @Descrizione varchar(32)
DECLARE @IDArea int
DECLARE @IDDimensione int
DECLARE @IDStato int
DECLARE @Ordinamento float
DECLARE @X int
DECLARE @Y int
DECLARE @Z int
DECLARE @Corsia varchar(8)
DECLARE @Colonna varchar(8)
DECLARE @Fila varchar(8)
DECLARE @PortataMassimaCella float
DECLARE @PortataMassimaColonna float
DECLARE @UnitaVolumeOccupata float
DECLARE @InsUtente varchar(50)
DECLARE @InsDataOra datetime
DECLARE @ModUtente varchar(50)
DECLARE @ModDataOra datetime

SET @IDArea = 4
SET @Corsia = ' 4D' 
SET @Colonna = '18' 


SET @ID = 1001 
SET @Descrizione = '1001 :  :  1A - 1  - a' 
SET @IDDimensione = 1 
SET @IDStato = 0 
SET @Ordinamento = 1.001000000000000e+003 
SET @X = 0 
SET @Y = 0 
SET @Z = 0 

SET @PortataMassimaCella = 0.000000000000000e+000 
SET @PortataMassimaColonna = 0.000000000000000e+000 
SET @UnitaVolumeOccupata = 0.000000000000000e+000 
SET @InsUtente = 'raf' 
SET @InsDataOra = 'set 11 2010  5:25PM' 
SET @ModUtente = 'raf' 
SET @ModDataOra = 'set 11 2010  5:25PM'

declare @IDDaCopiare int
declare @IDNuovo int

-- Imposta valori dei parametri

-- Declare an inner cursor based   
   -- on au_id from the outer cursor.

   DECLARE IDDaCopiare_cursor CURSOR FOR 


    SELECT [ID] FROM [MediSeawall].[dbo].[Aree]
    WHERE IDMagazzino = @IDMagazzino

   OPEN IDDaCopiare_cursor
   FETCH NEXT FROM IDDaCopiare_cursor INTO @IDDaCopiare

   WHILE @@FETCH_STATUS = 0
   BEGIN
        
        SET @IDArea = @IDDaCopiare
        SET @Corsia = ' 4D' 

        SELECT TOP (1) @Corsia = ' '+ SUBSTRING(Descrizione,1,2)
        FROM   Aree
        WHERE ID = @IDArea

   		DECLARE @site_value INT = 1, @Locazioni int = 30

        if @IDDaCopiare  = 11 BEGIN SET @Locazioni = 24  END
        if @IDDaCopiare  = 12 BEGIN SET @Locazioni = 15  END
        if @IDDaCopiare  = 13 BEGIN SET @Locazioni = 15  END
        if @IDDaCopiare  = 14 BEGIN SET @Locazioni = 24  END

    	WHILE @site_value <= @Locazioni
		BEGIN

            SET @Colonna = @site_value

            DECLARE @Piani_value INT = 1, @Piani int = 5

    		WHILE @Piani_value <= @Piani
			BEGIN

                    if @Piani_value  = 1 BEGIN SET @Fila = 'a' END
                    if @Piani_value  = 2 BEGIN SET @Fila = 'b' END
                    if @Piani_value  = 3 BEGIN SET @Fila = 'c' END
                    if @Piani_value  = 4 BEGIN SET @Fila = 'd' END
                    if @Piani_value  = 5 BEGIN SET @Fila = 'e' END
                    if @Piani_value  = 6 BEGIN SET @Fila = 'f' END

                    SELECT TOP 1 @ID = ISNULL(MAX([ID]),0) FROM [MediSeawall].[dbo].[Celle]
                    WHERE IDArea = @IDDaCopiare 

                    if @ID = 0 
                    BEGIN
                        
                            if @IDDaCopiare  = 11 BEGIN SET @ID = 5000 + 1 END
                            if @IDDaCopiare  = 12 BEGIN SET @ID = 6000 + 1  END
                            if @IDDaCopiare  = 13 BEGIN SET @ID = 7000 + 1  END
                            if @IDDaCopiare  = 14 BEGIN SET @ID = 8000 + 1  END
                    END
                    ELSE
                    BEGIN
                         SET @ID = @ID + 1
                    END

                    SET @Ordinamento = @ID

                    SET @Descrizione = CAST(@ID as char(4)) + ' cors: ' + @Corsia+ ' col: ' + @Colonna + ' fil: ' + @Fila

		            EXECUTE @RC = [MediSeawall].[dbo].[spt_SaveCelle] 
		               @ID
		              ,@Descrizione
		              ,@IDArea
		              ,@IDDimensione
		              ,@IDStato
		              ,@Ordinamento
		              ,@X
		              ,@Y
		              ,@Z
		              ,@Corsia
		              ,@Colonna
		              ,@Fila
		              ,@PortataMassimaCella
		              ,@PortataMassimaColonna
		              ,@UnitaVolumeOccupata
		              ,@InsUtente
		              ,@InsDataOra
		              ,@ModUtente
		              ,@ModDataOra



			   SET @Piani_value = @Piani_value + 1
			END;

 			SET @site_value = @site_value + 1
		END;


      
      FETCH NEXT FROM IDDaCopiare_cursor INTO @IDDaCopiare
   
   END

   CLOSE IDDaCopiare_cursor
   DEALLOCATE IDDaCopiare_cursor





END

GO
/****** Object:  StoredProcedure [dbo].[CreaNuoveCelleMDE6_BIS]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[CreaNuoveCelleMDE6_BIS]
AS

DECLARE @IDMagazzino int = 2
DECLARE @RC int
DECLARE @ID int
DECLARE @Descrizione varchar(32)
DECLARE @IDArea int
DECLARE @IDDimensione int
DECLARE @IDStato int
DECLARE @Ordinamento float
DECLARE @X int
DECLARE @Y int
DECLARE @Z int
DECLARE @Corsia varchar(8)
DECLARE @Colonna varchar(8)
DECLARE @Fila varchar(8)
DECLARE @PortataMassimaCella float
DECLARE @PortataMassimaColonna float
DECLARE @UnitaVolumeOccupata float
DECLARE @InsUtente varchar(50)
DECLARE @InsDataOra datetime
DECLARE @ModUtente varchar(50)
DECLARE @ModDataOra datetime

SET @IDArea = 4
SET @Corsia = ' 4D' 
SET @Colonna = '18' 


SET @ID = 1001 
SET @Descrizione = '1001 :  :  1A - 1  - a' 
SET @IDDimensione = 1 
SET @IDStato = 0 
SET @Ordinamento = 1.001000000000000e+003 
SET @X = 0 
SET @Y = 0 
SET @Z = 0 

SET @PortataMassimaCella = 0.000000000000000e+000 
SET @PortataMassimaColonna = 0.000000000000000e+000 
SET @UnitaVolumeOccupata = 0.000000000000000e+000 
SET @InsUtente = 'raf' 
SET @InsDataOra = 'set 11 2010  5:25PM' 
SET @ModUtente = 'raf' 
SET @ModDataOra = 'set 11 2010  5:25PM'

declare @IDDaCopiare int
declare @IDNuovo int

-- Imposta valori dei parametri

-- Declare an inner cursor based   
   -- on au_id from the outer cursor.

   DECLARE IDDaCopiare_cursor CURSOR FOR 


    SELECT [ID] FROM [MediSeawall].[dbo].[Aree]
    WHERE IDMagazzino = @IDMagazzino

   OPEN IDDaCopiare_cursor
   FETCH NEXT FROM IDDaCopiare_cursor INTO @IDDaCopiare

   WHILE @@FETCH_STATUS = 0
   BEGIN
        
        SET @IDArea = @IDDaCopiare
        SET @Corsia = ' 4D' 

        SELECT TOP (1) @Corsia = ' '+ SUBSTRING(Descrizione,1,2)
        FROM   Aree
        WHERE ID = @IDArea

   		DECLARE @site_value INT = 1, @Locazioni int = 30

        if @IDDaCopiare  = 11 BEGIN SET @Locazioni = 28  END
        if @IDDaCopiare  = 12 BEGIN SET @Locazioni = 25  END
        if @IDDaCopiare  = 13 BEGIN SET @Locazioni = 25  END
        if @IDDaCopiare  = 14 BEGIN SET @Locazioni = 36  END

    	WHILE @site_value <= @Locazioni
		BEGIN

            SET @Colonna = @site_value

            DECLARE @Piani_value INT = 3, @Piani int =5

    		WHILE @Piani_value <= @Piani
			BEGIN

                    if @Piani_value  = 1 BEGIN SET @Fila = 'a' END
                    if @Piani_value  = 2 BEGIN SET @Fila = 'b' END
                    if @Piani_value  = 3 BEGIN SET @Fila = 'c' END
                    if @Piani_value  = 4 BEGIN SET @Fila = 'd' END
                    if @Piani_value  = 5 BEGIN SET @Fila = 'e' END
                    if @Piani_value  = 6 BEGIN SET @Fila = 'f' END

                    SELECT TOP 1 @ID = ISNULL(MAX([ID]),0) FROM [MediSeawall].[dbo].[Celle]
                    WHERE IDArea = @IDDaCopiare 

                    if @ID = 0 
                    BEGIN
                        
                            if @IDDaCopiare  = 11 BEGIN SET @ID = 5000 + 1 END
                            if @IDDaCopiare  = 12 BEGIN SET @ID = 6000 + 1  END
                            if @IDDaCopiare  = 13 BEGIN SET @ID = 7000 + 1  END
                            if @IDDaCopiare  = 14 BEGIN SET @ID = 8000 + 1  END
                    END
                    ELSE
                    BEGIN
                         SET @ID = @ID + 1
                    END

                    SET @Ordinamento = @ID

                    SET @Descrizione = CAST(@ID as char(4)) + ' cors: ' + @Corsia+ ' col: ' + @Colonna + ' fil: ' + @Fila

		            EXECUTE @RC = [MediSeawall].[dbo].[spt_SaveCelle] 
		               @ID
		              ,@Descrizione
		              ,@IDArea
		              ,@IDDimensione
		              ,@IDStato
		              ,@Ordinamento
		              ,@X
		              ,@Y
		              ,@Z
		              ,@Corsia
		              ,@Colonna
		              ,@Fila
		              ,@PortataMassimaCella
		              ,@PortataMassimaColonna
		              ,@UnitaVolumeOccupata
		              ,@InsUtente
		              ,@InsDataOra
		              ,@ModUtente
		              ,@ModDataOra



			   SET @Piani_value = @Piani_value + 1
			END;

 			SET @site_value = @site_value + 1
		END;


      
      FETCH NEXT FROM IDDaCopiare_cursor INTO @IDDaCopiare
   
   END

   CLOSE IDDaCopiare_cursor
   DEALLOCATE IDDaCopiare_cursor

GO
/****** Object:  StoredProcedure [dbo].[CreateLoopArea]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CreateLoopArea](@IDArea int)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	

		DECLARE @ElaboraLoopBarcodePallet varchar(16)

        DECLARE @RC int
        DECLARE @IDOperatore int
        DECLARE @BarcodeCella varchar(8) =  '9000000'
        DECLARE @BarcodePallet varchar(16) = ''
        DECLARE @NumeroCella int =  9000000

		DECLARE ElaboraLoopBarcodePallet_cursor CURSOR FOR 

        SELECT [BarcodePallet]
          FROM [dbo].[XMag_GiacenzaPallet]
          WHERE [IDArea] = @IDArea


		OPEN ElaboraLoopBarcodePallet_cursor
		FETCH NEXT FROM ElaboraLoopBarcodePallet_cursor INTO @ElaboraLoopBarcodePallet

		WHILE @@FETCH_STATUS = 0
		BEGIN
      				
             SET @BarcodePallet = @ElaboraLoopBarcodePallet

            -- TODO: impostare qui i valori dei parametri.

            EXECUTE @RC = [sp_xMagGestioneMagazziniPallet] 
               @IDOperatore
              ,@BarcodeCella
              ,@BarcodePallet
              ,@NumeroCella
              ,@RC OUTPUT
   
			FETCH NEXT FROM ElaboraLoopBarcodePallet_cursor INTO @ElaboraLoopBarcodePallet
   
		END

		CLOSE ElaboraLoopBarcodePallet_cursor
		DEALLOCATE ElaboraLoopBarcodePallet_cursor


END
GO
/****** Object:  StoredProcedure [dbo].[sp_ControllaPrenotazionePackingListPallet]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO





CREATE PROCEDURE [dbo].[sp_ControllaPrenotazionePackingListPallet]
 AS
BEGIN


	DECLARE @RC int = 0
	DECLARE @IDOperatore int = 0
	DECLARE @IDCellaPrenotata int = 0
	DECLARE @Documento varchar(8) = ''
	DECLARE @Nominativo varchar(50) = ''

	SELECT TOP 1 @IDCellaPrenotata = [ID]
	FROM [Mediseawall].[dbo].[Celle]
	where IDStato = 1

	SELECT TOP 1 @Documento = Documento, @IDCellaPrenotata = Cella
	FROM  XMag_ViewPackingList
	GROUP BY Documento, IDStato,Cella
	HAVING (IDStato = 1) and Cella <> 9999
	ORDer BY Documento

	IF @IDCellaPrenotata > 0 
	BEGIN

		SELECT TOP 1  @Nominativo = [ModUtente]
		FROM [Mediseawall].[dbo].[Celle]
		where IDStato = 1

		SELECT  @IDOperatore = ID FROM Operatori WHERE LOGIN = @Nominativo

		--SELECT  TOP 1  @Documento =  Documento
		--FROM    dbo.XMag_ViewPackingList
		--GROUP BY Documento, CodNazione, NAZIONE, Stato, Magazzino, Area, Cella
		--HAVING      (Cella = @IDCellaPrenotata)   

		SELECT TOP 1  @Documento = Documento
		FROM  XMag_ViewPackingList
		GROUP BY Documento, IDStato
		HAVING (IDStato = 1)
		ORDER BY Documento

		EXECUTE [dbo].sp_xExePackingListPalletPrenota 
		   @IDOperatore
		  ,@Documento
		  ,@RC OUTPUT
	END


END

GO
/****** Object:  StoredProcedure [dbo].[sp_ControllaPrenotazionePackingListPalletNew]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO





CREATE PROCEDURE [dbo].[sp_ControllaPrenotazionePackingListPalletNew]
 AS
BEGIN


	DECLARE @RC int = 0
	DECLARE @IDOperatore int = 0
	DECLARE @IDCellaPrenotata int = 0
	DECLARE @Documento varchar(8) = ''
	DECLARE @Nominativo varchar(50) = ''

	SELECT TOP 1 @IDCellaPrenotata = [ID]
	FROM [Mediseawall].[dbo].[Celle]
	where IDStato = 1

	SELECT TOP 1 @Documento = Documento, @IDCellaPrenotata = Cella
	FROM  XMag_ViewPackingList
	GROUP BY Documento, IDStato,Cella
	HAVING (IDStato = 1) 
    --and Cella <> 9999
    AND Documento IN (SELECT TOP (1) CAST([Code] as int) FROM [LogPackingList] ORDER By ID DESC)
	ORDer BY Documento

	IF @IDCellaPrenotata > 0 
	BEGIN

		SELECT TOP 1  @Nominativo = [ModUtente]
		FROM [Mediseawall].[dbo].[Celle]
		where IDStato = 1

		SELECT  @IDOperatore = ID FROM Operatori WHERE LOGIN = @Nominativo

		--SELECT  TOP 1  @Documento =  Documento
		--FROM    dbo.XMag_ViewPackingList
		--GROUP BY Documento, CodNazione, NAZIONE, Stato, Magazzino, Area, Cella
		--HAVING      (Cella = @IDCellaPrenotata)   

		SELECT TOP 1  @Documento = Documento
		FROM  XMag_ViewPackingList
		GROUP BY Documento, IDStato
		HAVING (IDStato = 1)
         AND Documento IN (SELECT TOP (1) CAST([Code] as int) FROM [LogPackingList] ORDER By ID DESC)
		ORDER BY Documento

		EXECUTE [dbo].sp_xExePackingListPalletPrenota 
		   @IDOperatore
		  ,@Documento
		  ,@RC OUTPUT
	END


END

GO
/****** Object:  StoredProcedure [dbo].[sp_LogOperation]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [dbo].[sp_LogOperation]
(  @ID int
, @Code varchar(64 )
, @Description varchar(255)
, @Message varchar(255) Output 
, @IDResult int Output 
)  
AS  
	SET  
	NOCOUNT ON  
	set @Message = ''

DECLARE @IDInsUser int
DECLARE @InsDateTime datetime
DECLARE @IDModUser int
DECLARE @ModDateTime datetime
DECLARE @IDDelUser int
DECLARE @DelDateTime datetime
DECLARE @IDEvents int

SET @IDInsUser = 1
SET @InsDateTime = GETDATE()

SET @IDModUser = 1
SET @ModDateTime = GETDATE()

SET @IDDelUser = 0
SET @DelDateTime = GETDATE()

-- TODO: impostare qui i valori dei parametri.

EXECUTE spt_SaveLogOperation
   @ID  ,@Code  ,@Description  
   ,@IDInsUser  ,@InsDateTime  ,@IDModUser  ,@ModDateTime  ,@IDDelUser  ,@DelDateTime  ,@IDEvents  ,@Message OUTPUT  ,@IDResult OUTPUT
GO
/****** Object:  StoredProcedure [dbo].[sp_LogPackingList]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE Procedure [dbo].[sp_LogPackingList]
(  @ID int
, @Code varchar(64 )
, @Description varchar(255)
, @Message varchar(255) Output 
, @IDResult int Output 
)  
AS  
	SET  
	NOCOUNT ON  
	set @Message = ''

DECLARE @IDInsUser int
DECLARE @InsDateTime datetime
DECLARE @IDModUser int
DECLARE @ModDateTime datetime
DECLARE @IDDelUser int
DECLARE @DelDateTime datetime
DECLARE @IDEvents int

SET @IDInsUser = 1
SET @InsDateTime = GETDATE()

SET @IDModUser = 1
SET @ModDateTime = GETDATE()

SET @IDDelUser = 0
SET @DelDateTime = GETDATE()

-- TODO: impostare qui i valori dei parametri.

EXECUTE spt_SaveLogPackingList
   @ID  ,@Code  ,@Description  
   ,@IDInsUser  ,@InsDateTime  ,@IDModUser  ,@ModDateTime  ,@IDDelUser  ,@DelDateTime  ,@IDEvents  ,@Message OUTPUT  ,@IDResult OUTPUT
GO
/****** Object:  StoredProcedure [dbo].[sp_OrdinaCelle]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO






CREATE            PROCEDURE [dbo].[sp_OrdinaCelle]
AS


	declare @IDDaCopiare int
	declare @IDNuovo int

SET @IDNuovo = 10000
	-- Imposta valori dei parametri

-- Declare an inner cursor based   
   -- on au_id from the outer cursor.

   DECLARE IDDaCopiare_cursor CURSOR FOR 

SELECT     ID
FROM         Celle
WHERE     (IDArea = 1) OR
                      (IDArea = 2)
ORDER BY CAST(Colonna as INT) DESC

   OPEN IDDaCopiare_cursor
   FETCH NEXT FROM IDDaCopiare_cursor INTO @IDDaCopiare

   WHILE @@FETCH_STATUS = 0
   BEGIN
      
	SET @IDNuovo = @IDNuovo + 1


UPDATE [MediSeawall].[dbo].[Celle]
   SET [Ordinamento] = @IDNuovo      
 WHERE [ID] = @IDDaCopiare

      FETCH NEXT FROM IDDaCopiare_cursor INTO @IDDaCopiare
   
   END

   CLOSE IDDaCopiare_cursor
   DEALLOCATE IDDaCopiare_cursor









GO
/****** Object:  StoredProcedure [dbo].[sp_xExePackingListPallet]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[sp_xExePackingListPallet]
@IDOperatore int, @Documento varchar(8), @RC int OUTPUT
 AS

SET @RC = 0
-- Recupera operatore
DECLARE @Nominativo varchar(50)
SELECT  @Nominativo = LOGIN FROM Operatori WHERE id = @IDOperatore
DECLARE @Stato int
PRINT @Nominativo


	declare @IDDaCopiare int
	declare @IDNuovo int

	-- Imposta valori dei parametri

-- Declare an inner cursor based   
   -- on au_id from the outer cursor.

   DECLARE IDDaCopiare_cursor CURSOR FOR 

    SELECT      Cella
    FROM         dbo.XMag_ViewPackingList
    GROUP BY Documento, CodNazione, NAZIONE, Stato, Magazzino, Area, Cella
    HAVING      (Documento = @Documento)   

   OPEN IDDaCopiare_cursor
   FETCH NEXT FROM IDDaCopiare_cursor INTO @IDDaCopiare

   WHILE @@FETCH_STATUS = 0
   BEGIN
      
	SELECT @Stato = [IDStato] FROM [Celle] WHERE [ID] = @IDDaCopiare

	if @Stato = 0 
	BEGIN
		UPDATE [Celle]    SET       [IDStato] = 1
		  ,[ModUtente] = @Nominativo
		  ,[ModDataOra] = GETDATE()
 		WHERE [ID] = @IDDaCopiare
	END
	ELSE
	BEGIN
	UPDATE [Celle]     SET       [IDStato] = 0
		  ,[ModUtente] = @Nominativo
		  ,[ModDataOra] = GETDATE()
 		WHERE [ID] = @IDDaCopiare
	END

      FETCH NEXT FROM IDDaCopiare_cursor INTO @IDDaCopiare
   
   END

   CLOSE IDDaCopiare_cursor
   DEALLOCATE IDDaCopiare_cursor



    DECLARE @ID int = 0
    DECLARE @Code varchar(64) = @Documento
    DECLARE @Description varchar(255)
    DECLARE @Message varchar(255)
    DECLARE @IDResult int

    SELECT  TOP 1 @Description =  NAZIONE
    FROM         dbo.XMag_ViewPackingList
    GROUP by Documento, NAZIONE
    HAVING Documento = @Documento
    -- TODO: impostare qui i valori dei parametri.

    EXECUTE @RC = [dbo].[sp_LogPackingList] 
       @ID
      ,@Code
      ,@Description
      ,@Message OUTPUT
      ,@IDResult OUTPUT
GO
/****** Object:  StoredProcedure [dbo].[sp_xExePackingListPalletPrenota]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO





CREATE PROCEDURE [dbo].[sp_xExePackingListPalletPrenota]
@IDOperatore int, @Documento varchar(8), @RC int OUTPUT
 AS

SET @RC = 0
-- Recupera operatore
DECLARE @Nominativo varchar(50)
SELECT  @Nominativo = LOGIN FROM Operatori WHERE id = @IDOperatore
DECLARE @Stato int
PRINT @Nominativo


	declare @IDDaCopiare int
	declare @IDNuovo int

	-- Imposta valori dei parametri

-- Declare an inner cursor based   
   -- on au_id from the outer cursor.

   DECLARE IDDaCopiare_cursor CURSOR FOR 

SELECT      Cella
FROM         dbo.XMag_ViewPackingList
GROUP BY Documento, CodNazione, NAZIONE, Stato, Magazzino, Area, Cella
HAVING      (Documento = @Documento)   

   OPEN IDDaCopiare_cursor
   FETCH NEXT FROM IDDaCopiare_cursor INTO @IDDaCopiare

   WHILE @@FETCH_STATUS = 0
   BEGIN
      

		UPDATE [Celle]    SET       [IDStato] = 1
		  ,[ModUtente] = @Nominativo
		  ,[ModDataOra] = GETDATE()
 		WHERE [ID] = @IDDaCopiare



      FETCH NEXT FROM IDDaCopiare_cursor INTO @IDDaCopiare
   
   END

   CLOSE IDDaCopiare_cursor
   DEALLOCATE IDDaCopiare_cursor





GO
/****** Object:  StoredProcedure [dbo].[sp_xMagGestioneAccettazione]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO










CREATE PROCEDURE [dbo].[sp_xMagGestioneAccettazione]
@IDOperatore int, @BarcodeCella varchar(8), @BarcodePallet varchar(16), @NumeroCella int, @RC int OUTPUT
, @Out1 varchar(16) OUTPUT
, @Out2 varchar(16) OUTPUT
, @Out3 varchar(16) OUTPUT
, @Out4 varchar(16) OUTPUT
, @Out5 varchar(16) OUTPUT
, @Out6 varchar(16) OUTPUT
, @Out7 varchar(16) OUTPUT
 AS

--DECLARE @RC int
--DECLARE @IDOperatore int
--DECLARE @BarcodeCella varchar(8)
--DECLARE @BarcodePallet varchar(16)
--DECLARE @NumeroCella int
--SET @IDOperatore = 1
SET @BarcodeCella = '9009000'
--SET @BarcodePallet  = '100782'
SET @NumeroCella = 9000

SET @RC = 0
-- Recupera operatore
DECLARE @Output1 varchar(16)
DECLARE @Output2 varchar(16)
DECLARE @Output3 varchar(16)
DECLARE @Output4 varchar(16)

DECLARE @Nominativo varchar(50)
SELECT  @Nominativo = LOGIN FROM Operatori WHERE id = @IDOperatore

PRINT @Nominativo
PRINT SUBSTRING(@BarcodeCella,1,3)
PRINT SUBSTRING(@BarcodeCella,4,2)
PRINT SUBSTRING(@BarcodeCella,6,2)
-- Cerca la cella 
DECLARE @IDMagazzino int ,@IDArea int, @IDCella int, @NumeroPallet int, @NewIDCella int
SET @NewIDCella = -1*(9000000 - @NumeroCella)
SET @IDMagazzino = 0 
SET @IDArea = 0
SET @IDCella  = 0
SET @NumeroPallet = 0

SELECT @IDCella = [ID], @IDArea = [IDArea]
  FROM [Celle]
WHERE ID = @NewIDCella



if @IDArea > 0
	BEGIN
		-- Magazzino da area
		SELECT @IDMagazzino = [IDMagazzino]
		  FROM [Aree]
		WHERE [ID] = @IDArea
	END
ELSE
	BEGIN 
		-- se non trova la cella recupera il magazzino di default 
		SELECT TOP 1 @IDMagazzino = UnitaProduzione.IDMagVersamento
		FROM   Operatori INNER JOIN UnitaProduzione ON Operatori.CodiceUnita = UnitaProduzione.Codice
		WHERE  (Operatori.ID = @IDOperatore)

		SELECT @IDCella = [ID], @IDArea = [IDArea]
		FROM [Celle]
		WHERE ID = 9000

	END

-- Recupera il peso unitario e la tara
DECLARE  @PesoUnitario float, @Tara float, @IDRiferimento int
SET @PesoUnitario = 1
SET @Tara = 0
SET @IDRiferimento = 0

	SELECT TOP 1 @IDRiferimento = ID
	FROM [Accettazione]
	WHERE (IDMagazzino  = @IDMagazzino)	AND (Attributo = @BarcodePallet) -- AND (NumeroPallet = @NumeroPallet)
	AND  PesoUnitario > 0 AND Tipo = 'V' AND [IDRiferimento] = 0
	ORDER BY ID DESC


UPDATE [Accettazione]
   SET [ModUtente] = @Nominativo, [ModDataOra] = GETDATE(), [IDRiferimento] = @IDRiferimento
WHERE ID = @IDRiferimento AND [IDRiferimento] = 0

--WHERE (IDMagazzino  = @IDMagazzino)  AND (Attributo = @BarcodePallet) -- AND (NumeroPallet = @NumeroPallet)
--AND  PesoUnitario > 0 AND Tipo = 'V'
--ORDER BY ID DESC

 IF @@ROWCOUNT > 0 -- SE TROVO QUALCOSA LA PRELIEVO - Poi Trasferisco in area transito
 BEGIN 
 
	INSERT INTO [Accettazione] ([Tipo],[IDRiferimento] ,[NumeroPallet], Attributo
			   ,[IDMagazzino] ,[IDArea] ,[IDCella] ,[DataMagazzino]
			   ,[PesoUnitario] ,[Tara],[InsUtente] ,[InsDataOra])
	

 
 	SELECT 'P' ,@IDRiferimento,[NumeroPallet], Attributo
		  ,[IDMagazzino] ,[IDArea] ,[IDCella] ,GETDATE()
		  ,[PesoUnitario],[Tara], @Nominativo	,GETDATE()
	FROM [Accettazione]
    WHERE ID = @IDRiferimento

	SET @RC = @@IDENTITY 

	SELECT @Output2 =  Attributo,
		  @Output3 =  [InsUtente]	, @Output4 = CAST([InsDataOra] as CHAR(16))
	FROM [Accettazione]
    WHERE ID = @IDRiferimento
    
    
  SET @Out1 = 'DIS-ACCETTAZIONE'
SET @Out2 = @Output2
SET @Out3 = @Output3
SET @Out4 = @Output4
SET @Out5 = 5
	SET @Out6 = @BarcodeCella
	SET @Out7 = ''

 END 
ELSE
 BEGIN

	-- VERSA in area cella [IDMagazzino],[IDArea],[IDCella]
	INSERT INTO [Accettazione]  ([Tipo] ,[IDRiferimento],[NumeroPallet], Attributo
			   ,[IDMagazzino],[IDArea],[IDCella],[DataMagazzino]
			   ,[PesoUnitario],[Tara],[InsUtente],[InsDataOra])
     VALUES ('V',@IDRiferimento,@NumeroPallet, @BarcodePallet
           ,@IDMagazzino,@IDArea,@IDCella,GETDATE()
           ,@PesoUnitario,@Tara,@Nominativo,GETDATE())


	SET @RC = @@IDENTITY 

	SET @Out1 = 'ACCETTATO'
	SET @Out2 = @BarcodePallet
	SET @Out3 = @Nominativo
	SET @Out4 = CAST(GETDATE() as CHAR(16))
	SET @Out5 = 7
	SET @Out6 = @BarcodeCella
	SET @Out7 = ''

 END









GO
/****** Object:  StoredProcedure [dbo].[sp_xMagGestioneAccettazioneOLD]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO











CREATE PROCEDURE [dbo].[sp_xMagGestioneAccettazioneOLD]
@IDOperatore int, @BarcodeCella varchar(8), @BarcodePallet varchar(16), @NumeroCella int, @RC int OUTPUT
, @Out1 varchar(16) OUTPUT
, @Out2 varchar(16) OUTPUT
, @Out3 varchar(16) OUTPUT
, @Out4 varchar(16) OUTPUT
, @Out5 varchar(16) OUTPUT
, @Out6 varchar(16) OUTPUT
, @Out7 varchar(16) OUTPUT
 AS

--DECLARE @RC int
--DECLARE @IDOperatore int
--DECLARE @BarcodeCella varchar(8)
--DECLARE @BarcodePallet varchar(16)
--DECLARE @NumeroCella int
--SET @IDOperatore = 1
SET @BarcodeCella = '9009000'
--SET @BarcodePallet  = '100782'
SET @NumeroCella = 9000

SET @RC = 0
-- Recupera operatore
DECLARE @Output1 varchar(16)
DECLARE @Output2 varchar(16)
DECLARE @Output3 varchar(16)
DECLARE @Output4 varchar(16)

DECLARE @Nominativo varchar(50)
SELECT  @Nominativo = LOGIN FROM Operatori WHERE id = @IDOperatore

PRINT @Nominativo
PRINT SUBSTRING(@BarcodeCella,1,3)
PRINT SUBSTRING(@BarcodeCella,4,2)
PRINT SUBSTRING(@BarcodeCella,6,2)
-- Cerca la cella 
DECLARE @IDMagazzino int ,@IDArea int, @IDCella int, @NumeroPallet int, @NewIDCella int
SET @NewIDCella = -1*(9000000 - @NumeroCella)
SET @IDMagazzino = 0 
SET @IDArea = 0
SET @IDCella  = 0
SET @NumeroPallet = 0

SELECT @IDCella = [ID], @IDArea = [IDArea]
  FROM [Celle]
WHERE ID = @NewIDCella



if @IDArea > 0
	BEGIN
		-- Magazzino da area
		SELECT @IDMagazzino = [IDMagazzino]
		  FROM [Aree]
		WHERE [ID] = @IDArea
	END
ELSE
	BEGIN 
		-- se non trova la cella recupera il magazzino di default 
		SELECT TOP 1 @IDMagazzino = UnitaProduzione.IDMagVersamento
		FROM   Operatori INNER JOIN UnitaProduzione ON Operatori.CodiceUnita = UnitaProduzione.Codice
		WHERE  (Operatori.ID = @IDOperatore)

		SELECT @IDCella = [ID], @IDArea = [IDArea]
		FROM [Celle]
		WHERE ID = 9000

	END

-- Recupera il peso unitario e la tara
DECLARE  @PesoUnitario float, @Tara float, @IDRiferimento int
SET @PesoUnitario = 1
SET @Tara = 0
SET @IDRiferimento = 0

	SELECT TOP 1 @IDRiferimento = ID
	FROM [Accettazione]
	WHERE (IDMagazzino  = @IDMagazzino)	AND (Attributo = @BarcodePallet) -- AND (NumeroPallet = @NumeroPallet)
	AND  PesoUnitario > 0 AND Tipo = 'V'
	ORDER BY ID DESC


UPDATE [Accettazione]
   SET [ModUtente] = @Nominativo, [ModDataOra] = GETDATE()
WHERE ID = @IDRiferimento

--WHERE (IDMagazzino  = @IDMagazzino)  AND (Attributo = @BarcodePallet) -- AND (NumeroPallet = @NumeroPallet)
--AND  PesoUnitario > 0 AND Tipo = 'V'
--ORDER BY ID DESC

 IF @@ROWCOUNT > 0 -- SE TROVO QUALCOSA LA PRELIEVO - Poi Trasferisco in area transito
 BEGIN 
 
	INSERT INTO [Accettazione] ([Tipo],[IDRiferimento] ,[NumeroPallet], Attributo
			   ,[IDMagazzino] ,[IDArea] ,[IDCella] ,[DataMagazzino]
			   ,[PesoUnitario] ,[Tara],[InsUtente] ,[InsDataOra])
	

 
 	SELECT 'P' ,@IDRiferimento,[NumeroPallet], Attributo
		  ,[IDMagazzino] ,[IDArea] ,[IDCella] ,GETDATE()
		  ,[PesoUnitario],[Tara], @Nominativo	,GETDATE()
	FROM [Accettazione]
    WHERE ID = @IDRiferimento

	SET @RC = @@IDENTITY 

	SELECT @Output2 =  Attributo,
		  @Output3 =  [InsUtente]	, @Output4 = CAST([InsDataOra] as CHAR(16))
	FROM [Accettazione]
    WHERE ID = @IDRiferimento
    
    
  SET @Out1 = 'ATTENZIONE'
SET @Out2 = @Output2
SET @Out3 = @Output3
SET @Out4 = @Output4
SET @Out5 = 5
	SET @Out6 = @BarcodeCella
	SET @Out7 = ''

 END 
ELSE
 BEGIN

	-- VERSA in area cella [IDMagazzino],[IDArea],[IDCella]
	INSERT INTO [Accettazione]  ([Tipo] ,[IDRiferimento],[NumeroPallet], Attributo
			   ,[IDMagazzino],[IDArea],[IDCella],[DataMagazzino]
			   ,[PesoUnitario],[Tara],[InsUtente],[InsDataOra])
     VALUES ('V',@IDRiferimento,@NumeroPallet, @BarcodePallet
           ,@IDMagazzino,@IDArea,@IDCella,GETDATE()
           ,@PesoUnitario,@Tara,@Nominativo,GETDATE())


	SET @RC = @@IDENTITY 

	SET @Out1 = 'ACCETTATO'
	SET @Out2 = @BarcodePallet
	SET @Out3 = @Nominativo
	SET @Out4 = CAST(GETDATE() as CHAR(16))
	SET @Out5 = 7
	SET @Out6 = @BarcodeCella
	SET @Out7 = ''

 END










GO
/****** Object:  StoredProcedure [dbo].[sp_xMagGestioneMagazziniPallet]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO









CREATE PROCEDURE [dbo].[sp_xMagGestioneMagazziniPallet]
@IDOperatore int, @BarcodeCella varchar(8), @BarcodePallet varchar(16), @NumeroCella int, @RC int OUTPUT
 AS

--DECLARE @RC int
--DECLARE @IDOperatore int
--DECLARE @BarcodeCella varchar(8)
--DECLARE @BarcodePallet varchar(16)
--DECLARE @NumeroCella int
--SET @IDOperatore = 1
--SET @BarcodeCella = '   1056'
--SET @BarcodePallet  = '100782'
--SET @NumeroCella = 1056

SET @RC = 0
-- Recupera operatore
DECLARE @Nominativo varchar(50)
SELECT  @Nominativo = LOGIN FROM Operatori WHERE id = @IDOperatore

PRINT @Nominativo
PRINT SUBSTRING(@BarcodeCella,1,3)
PRINT SUBSTRING(@BarcodeCella,4,2)
PRINT SUBSTRING(@BarcodeCella,6,2)
-- Cerca la cella 
DECLARE @IDMagazzino int ,@IDArea int, @IDCella int, @NumeroPallet int, @NewIDCella int
SET @NewIDCella = -1*(9000000 - @NumeroCella)
SET @IDMagazzino = 0 
SET @IDArea = 0
SET @IDCella  = 0
SET @NumeroPallet = 0

SELECT @IDCella = [Celle].[ID], @IDArea = [IDArea], @IDMagazzino = IDMagazzino 
  FROM [Celle], Aree
WHERE [Celle].ID = @NewIDCella
		AND IDArea = Aree.ID


--WHERE  [Corsia] = SUBSTRING(@BarcodeCella,1,3)
--      AND [Colonna] = SUBSTRING(@BarcodeCella,4,2)
--      AND [Fila] = SUBSTRING(@BarcodeCella,6,2)
PRINT @NumeroCella 
print 'Barcode Cella' 
PRINT @NewIDCella 
print 'NEWNumero Cella' 

PRINT @IDCella 
print 'Numero Cella' 
PRINT @IDArea 
print 'Numero @IDArea' 


DECLARE @ID int
DECLARE @Code varchar(64)
DECLARE @Description varchar(255)
DECLARE @Message varchar(255)
DECLARE @IDResult int

SET @Code = @BarcodeCella +' - '+  @BarcodePallet  +' - '+  CAST(@NumeroCella as CHAR(10))
SET @Description = CAST(@IDArea as CHAR(10)) +' - '+  CAST(@IDCella as CHAR(10))
SET @ID= 0

-- TODO: impostare qui i valori dei parametri.

EXECUTE sp_LogOperation   @ID  ,@Code  ,@Description  ,@Message OUTPUT  ,@IDResult OUTPUT

SET @ID= @IDResult



if @IDArea > 0
	BEGIN
		-- Magazzino da area
		SELECT @IDMagazzino = [IDMagazzino]
		  FROM [Aree]
		WHERE [ID] = @IDArea
	END
ELSE
	BEGIN 
		-- se non trova la cella recupera il magazzino di default 
		SELECT TOP 1 @IDMagazzino = UnitaProduzione.IDMagVersamento
		FROM   Operatori INNER JOIN UnitaProduzione ON Operatori.CodiceUnita = UnitaProduzione.Codice
		WHERE  (Operatori.ID = @IDOperatore)

		SELECT @IDCella = [Celle].[ID], @IDArea = [IDArea], @IDMagazzino = IDMagazzino 
		FROM [Celle], Aree
		WHERE [Celle].ID = 9999
		AND IDArea = Aree.ID
	END

-- Recupera il peso unitario e la tara
DECLARE  @PesoUnitario float, @Tara float, @IDRiferimento int
SET @PesoUnitario = 1
SET @Tara = 0
SET @IDRiferimento = 0

	SELECT TOP 1 @IDRiferimento = ID
	FROM [MagazziniPallet]
	WHERE 1= 1
	--AND (IDMagazzino  = @IDMagazzino)	
	AND (Attributo = @BarcodePallet) -- AND (NumeroPallet = @NumeroPallet)
	AND  PesoUnitario > 0 AND Tipo = 'V'
	ORDER BY ID DESC


UPDATE [MagazziniPallet]
   SET [ModUtente] = @Nominativo, [ModDataOra] = GETDATE()
WHERE ID = @IDRiferimento

--WHERE (IDMagazzino  = @IDMagazzino)  AND (Attributo = @BarcodePallet) -- AND (NumeroPallet = @NumeroPallet)
--AND  PesoUnitario > 0 AND Tipo = 'V'
--ORDER BY ID DESC

 IF @@ROWCOUNT > 0 -- SE TROVO QUALCOSA LA PRELIEVO - Poi Trasferisco in area transito
 BEGIN 
 
	INSERT INTO [MagazziniPallet] ([Tipo],[IDRiferimento] ,[NumeroPallet], Attributo
			   ,[IDMagazzino] ,[IDArea] ,[IDCella] ,[DataMagazzino]
			   ,[PesoUnitario] ,[Tara],[InsUtente] ,[InsDataOra])
	
	SELECT 'P' ,@IDRiferimento,[NumeroPallet], Attributo
		  ,[IDMagazzino] ,[IDArea] ,[IDCella] ,GETDATE()
		  ,[PesoUnitario],[Tara], @Nominativo	,GETDATE()
	FROM [MagazziniPallet]
    WHERE ID = @IDRiferimento
--	WHERE (IDMagazzino  = @IDMagazzino)	AND (Attributo = @BarcodePallet) -- AND (NumeroPallet = @NumeroPallet)
--	AND  PesoUnitario > 0 AND Tipo = 'V'
--	ORDER BY ID DESC

	-- disimpegna
	UPDATE [Celle]    SET 
      [IDStato] = 0 , [ModUtente] = @Nominativo    ,[ModDataOra] = GETDATE()
 	WHERE [ID] = 	(SELECT [IDCella] 
	FROM [MagazziniPallet]
    WHERE ID = @IDRiferimento)

	-- Trasferisci in area cella [IDMagazzino],[IDArea],[IDCella]
	INSERT INTO [MagazziniPallet]  ([Tipo] ,[IDRiferimento],[NumeroPallet], Attributo
			   ,[IDMagazzino],[IDArea],[IDCella],[DataMagazzino]
			   ,[PesoUnitario],[Tara],[InsUtente],[InsDataOra])
     VALUES ('V',@IDRiferimento,@NumeroPallet, @BarcodePallet
           ,@IDMagazzino,@IDArea,@IDCella,GETDATE()
           ,@PesoUnitario,@Tara,@Nominativo,GETDATE())


	SET @RC = @@IDENTITY 


 END 
ELSE
 BEGIN

	-- VERSA in area cella [IDMagazzino],[IDArea],[IDCella]
	INSERT INTO [MagazziniPallet]  ([Tipo] ,[IDRiferimento],[NumeroPallet], Attributo
			   ,[IDMagazzino],[IDArea],[IDCella],[DataMagazzino]
			   ,[PesoUnitario],[Tara],[InsUtente],[InsDataOra])
     VALUES ('V',@IDRiferimento,@NumeroPallet, @BarcodePallet
           ,@IDMagazzino,@IDArea,@IDCella,GETDATE()
           ,@PesoUnitario,@Tara,@Nominativo,GETDATE())

	-- disimpegna
	--UPDATE [Celle]    SET 
    --  [IDStato] = 0 , [ModUtente] = @Nominativo    ,[ModDataOra] = GETDATE()
 	--WHERE [ID] = @IDCella

	SET @RC = @@IDENTITY 


 END


--EXECUTE [dbo].[sp_ControllaPrenotazionePackingListPallet] 
EXECUTE [dbo].[sp_ControllaPrenotazionePackingListPalletNew] 


EXECUTE sp_LogOperation   @ID  ,@Code  ,@Description  ,@Message OUTPUT  ,@IDResult OUTPUT






GO
/****** Object:  StoredProcedure [dbo].[spt_DeleteAree]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



CREATE Procedure [dbo].[spt_DeleteAree]
(  @ID int
, @DelUtente varchar(50 )
, @DelDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE Aree
SET ID = @ID
,DelUtente = @DelUtente
,DelDataOra = @DelDataOra
WHERE (ID = @ID) 

 





GO
/****** Object:  StoredProcedure [dbo].[spt_DeleteCelle]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO





CREATE Procedure [dbo].[spt_DeleteCelle]
(  @ID int
, @DelUtente varchar(50 )
, @DelDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE Celle
SET ID = @ID
,DelUtente = @DelUtente
,DelDataOra = @DelDataOra
WHERE (ID = @ID) 

 







GO
/****** Object:  StoredProcedure [dbo].[spt_DeleteCelleDimensione]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO





CREATE Procedure [dbo].[spt_DeleteCelleDimensione]
(  @ID int
, @DelUtente varchar(50 )
, @DelDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE CelleDimensione
SET ID = @ID
,DelUtente = @DelUtente
,DelDataOra = @DelDataOra
WHERE (ID = @ID) 

 







GO
/****** Object:  StoredProcedure [dbo].[spt_DeleteCelleStati]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO





CREATE Procedure [dbo].[spt_DeleteCelleStati]
(  @ID int
, @DelUtente varchar(50 )
, @DelDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE CelleStati
SET ID = @ID
,DelUtente = @DelUtente
,DelDataOra = @DelDataOra
WHERE (ID = @ID) 

 







GO
/****** Object:  StoredProcedure [dbo].[spt_DeleteDivisioni]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO




CREATE Procedure [dbo].[spt_DeleteDivisioni]
(  @Codice varchar(64 )
, @DelUtente varchar(50 )
, @DelDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE Divisioni
SET Codice = @Codice
,DelUtente = @DelUtente
,DelDataOra = @DelDataOra
WHERE (Codice = @Codice) 

 





GO
/****** Object:  StoredProcedure [dbo].[spt_DeleteMagazzini]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE Procedure [dbo].[spt_DeleteMagazzini]
(  @ID int
, @DelUtente varchar(50 )
, @DelDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE Magazzini
SET DelUtente = @DelUtente
,DelDataOra = @DelDataOra
WHERE (ID = @ID) 

 




GO
/****** Object:  StoredProcedure [dbo].[spt_DeleteOperatori]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE Procedure [dbo].[spt_DeleteOperatori]
(  @ID int
, @DelUtente varchar(50 )
, @DelDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE Operatori
SET DelUtente = @DelUtente
,DelDataOra = @DelDataOra
WHERE (ID = @ID) 

 



GO
/****** Object:  StoredProcedure [dbo].[spt_DeleteReparti]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO




CREATE Procedure [dbo].[spt_DeleteReparti]
(  @Codice varchar(64 )
, @DelUtente varchar(50 )
, @DelDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE Reparti
SET Codice = @Codice
,DelUtente = @DelUtente
,DelDataOra = @DelDataOra
WHERE (Codice = @Codice) 

 





GO
/****** Object:  StoredProcedure [dbo].[spt_DeleteStabilimenti]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO




CREATE Procedure [dbo].[spt_DeleteStabilimenti]
(  @Codice varchar(64 )
, @DelUtente varchar(50 )
, @DelDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE Stabilimenti
SET Codice = @Codice
,DelUtente = @DelUtente
,DelDataOra = @DelDataOra
WHERE (Codice = @Codice) 

 






GO
/****** Object:  StoredProcedure [dbo].[spt_DeleteUnitaProduzione]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO




CREATE Procedure [dbo].[spt_DeleteUnitaProduzione]
(  @Codice varchar(64 )
, @DelUtente varchar(50 )
, @DelDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE UnitaProduzione
SET Codice = @Codice
,DelUtente = @DelUtente
,DelDataOra = @DelDataOra
WHERE (Codice = @Codice) 

 





GO
/****** Object:  StoredProcedure [dbo].[spt_SaveAree]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE Procedure [dbo].[spt_SaveAree]
(  @ID int
, @IDMagazzino int
, @Descrizione varchar(64 )
, @InsUtente varchar(50 )
, @InsDataOra datetime
, @ModUtente varchar(50 )
, @ModDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE Aree
SET ID = @ID
,IDMagazzino = @IDMagazzino
,Descrizione = @Descrizione
,InsUtente = @InsUtente
,InsDataOra = @InsDataOra
,ModUtente = @ModUtente
,ModDataOra = @ModDataOra
 WHERE ID = @ID

 IF @@ROWCOUNT = 0 
 BEGIN 
 
INSERT INTO Aree
(ID
,IDMagazzino
,Descrizione
,InsUtente
,InsDataOra
,ModUtente
,ModDataOra
 ) VALUES 
( @ID
, @IDMagazzino
, @Descrizione
, @InsUtente
, @InsDataOra
, @ModUtente
, @ModDataOra
 ) 
 END 


GO
/****** Object:  StoredProcedure [dbo].[spt_SaveCelle]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE Procedure [dbo].[spt_SaveCelle]
(  @ID int
, @Descrizione varchar(32 )
, @IDArea int
, @IDDimensione int
, @IDStato int
, @Ordinamento float
, @X int
, @Y int
, @Z int
, @Corsia varchar(8  )
, @Colonna varchar(8  )
, @Fila varchar(8  )
, @PortataMassimaCella float
, @PortataMassimaColonna float
, @UnitaVolumeOccupata float
, @InsUtente varchar(50 )
, @InsDataOra datetime
, @ModUtente varchar(50 )
, @ModDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE Celle
SET ID = @ID
,Descrizione = @Descrizione
,IDArea = @IDArea
,IDDimensione = @IDDimensione
,IDStato = @IDStato
,Ordinamento = @Ordinamento
,X = @X
,Y = @Y
,Z = @Z
,Corsia = @Corsia
,Colonna = @Colonna
,Fila = @Fila
,PortataMassimaCella = @PortataMassimaCella
,PortataMassimaColonna = @PortataMassimaColonna
,UnitaVolumeOccupata = @UnitaVolumeOccupata
,InsUtente = @InsUtente
,InsDataOra = @InsDataOra
,ModUtente = @ModUtente
,ModDataOra = @ModDataOra
 WHERE ID = @ID

 IF @@ROWCOUNT = 0 
 BEGIN 
 
INSERT INTO Celle
(ID
,Descrizione
,IDArea
,IDDimensione
,IDStato
,Ordinamento
,X
,Y
,Z
,Corsia
,Colonna
,Fila
,PortataMassimaCella
,PortataMassimaColonna
,UnitaVolumeOccupata
,InsUtente
,InsDataOra
,ModUtente
,ModDataOra
 ) VALUES 
( @ID
, @Descrizione
, @IDArea
, @IDDimensione
, @IDStato
, @Ordinamento
, @X
, @Y
, @Z
, @Corsia
, @Colonna
, @Fila
, @PortataMassimaCella
, @PortataMassimaColonna
, @UnitaVolumeOccupata
, @InsUtente
, @InsDataOra
, @ModUtente
, @ModDataOra
 ) 
 END 


GO
/****** Object:  StoredProcedure [dbo].[spt_SaveCelleDimensione]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE Procedure [dbo].[spt_SaveCelleDimensione]
(  @ID int
, @Descrizione varchar(32 )
, @Dimensione float
, @UnitaVolume float
, @A float
, @B float
, @C float
, @IDSolido int
, @InsUtente varchar(50 )
, @InsDataOra datetime
, @ModUtente varchar(50 )
, @ModDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE CelleDimensione
SET 
Descrizione = @Descrizione
,Dimensione = @Dimensione
,UnitaVolume = @UnitaVolume
,A = @A
,B = @B
,C = @C
,IDSolido = @IDSolido
,InsUtente = @InsUtente
,InsDataOra = @InsDataOra
,ModUtente = @ModUtente
,ModDataOra = @ModDataOra
 WHERE ID = @ID

 IF @@ROWCOUNT = 0 
 BEGIN 
 
INSERT INTO CelleDimensione
(ID
,Descrizione
,Dimensione
,UnitaVolume
,A
,B
,C
,IDSolido
,InsUtente
,InsDataOra
,ModUtente
,ModDataOra
 ) VALUES 
( @ID
, @Descrizione
, @Dimensione
, @UnitaVolume
, @A
, @B
, @C
, @IDSolido
, @InsUtente
, @InsDataOra
, @ModUtente
, @ModDataOra
 ) 
 END 


GO
/****** Object:  StoredProcedure [dbo].[spt_SaveCelleStati]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE Procedure [dbo].[spt_SaveCelleStati]
(  @ID int
, @Descrizione varchar(32 )
, @InsUtente varchar(50 )
, @InsDataOra datetime
, @ModUtente varchar(50 )
, @ModDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE CelleStati
SET 
Descrizione = @Descrizione
,InsUtente = @InsUtente
,InsDataOra = @InsDataOra
,ModUtente = @ModUtente
,ModDataOra = @ModDataOra
 WHERE ID = @ID

 IF @@ROWCOUNT = 0 
 BEGIN 
 
INSERT INTO CelleStati
(ID
,Descrizione
,InsUtente
,InsDataOra
,ModUtente
,ModDataOra
 ) VALUES 
( @ID
, @Descrizione
, @InsUtente
, @InsDataOra
, @ModUtente
, @ModDataOra
 ) 
 END 


GO
/****** Object:  StoredProcedure [dbo].[spt_SaveDivisioni]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



CREATE Procedure [dbo].[spt_SaveDivisioni]
( @Codice varchar(16 )
, @Descrizione varchar(64 )
, @InsUtente varchar(50 )
, @InsDataOra datetime
, @ModUtente varchar(50 )
, @ModDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE Divisioni
SET Codice = @Codice
,Descrizione = @Descrizione
,InsUtente = @InsUtente
,InsDataOra = @InsDataOra
,ModUtente = @ModUtente
,ModDataOra = @ModDataOra
WHERE (Codice = @Codice) 

 IF @@ROWCOUNT = 0 
 BEGIN 
 
INSERT INTO Divisioni
(Codice 
,Descrizione
,InsUtente
,InsDataOra
,ModUtente
,ModDataOra
 ) VALUES 
( @Codice
, @Descrizione
, @InsUtente
, @InsDataOra
, @ModUtente
, @ModDataOra
 ) 
 END 




GO
/****** Object:  StoredProcedure [dbo].[spt_SaveLogOperation]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [dbo].[spt_SaveLogOperation]
(  @ID int
, @Code varchar(64 )
, @Description varchar(255)
, @IDInsUser int
, @InsDateTime datetime
, @IDModUser int
, @ModDateTime datetime
, @IDDelUser int
, @DelDateTime datetime
, @IDEvents int 
, @Message varchar(255) Output 
, @IDResult int Output 
)  
AS  
	SET  
	NOCOUNT ON  
	set @Message = ''

	set @IDResult = @ID 
 
	UPDATE LogOperation
	SET --ID = @ID
	Code = @Code
	,Description = @Description
	,IDModUser = @IDModUser
	,ModDateTime = @ModDateTime
	,IDDelUser = @IDDelUser
	,DelDateTime = @DelDateTime
	WHERE ID = @ID  

	IF @@ROWCOUNT = 0 
		BEGIN 
 
			INSERT INTO LogOperation
			(Code
			,Description
			,IDInsUser
			,InsDateTime
			 ) VALUES 
			( @Code
			, @Description
			, @IDInsUser
			, @InsDateTime
			 ) 
		set @IDResult = SCOPE_IDENTITY() 
	END 
GO
/****** Object:  StoredProcedure [dbo].[spt_SaveLogPackingList]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE Procedure [dbo].[spt_SaveLogPackingList]
(  @ID int
, @Code varchar(64 )
, @Description varchar(255)
, @IDInsUser int
, @InsDateTime datetime
, @IDModUser int
, @ModDateTime datetime
, @IDDelUser int
, @DelDateTime datetime
, @IDEvents int 
, @Message varchar(255) Output 
, @IDResult int Output 
)  
AS  
	SET  
	NOCOUNT ON  
	set @Message = ''

	set @IDResult = @ID 
 
	UPDATE LogPackingList
	SET --ID = @ID
	Code = @Code
	,Description = @Description
	,IDModUser = @IDModUser
	,ModDateTime = @ModDateTime
	,IDDelUser = @IDDelUser
	,DelDateTime = @DelDateTime
	WHERE ID = @ID  

	IF @@ROWCOUNT = 0 
		BEGIN 
 
			INSERT INTO LogPackingList
			(Code
			,Description
			,IDInsUser
			,InsDateTime
			 ) VALUES 
			( @Code
			, @Description
			, @IDInsUser
			, @InsDateTime
			 ) 
		set @IDResult = SCOPE_IDENTITY() 
	END 
GO
/****** Object:  StoredProcedure [dbo].[spt_SaveMagazzini]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE Procedure [dbo].[spt_SaveMagazzini]
(  @ID int
, @Codice varchar(16 )
, @CodiceDivisione varchar(8  )
, @NomeBreve varchar(32 )
, @Descrizione varchar(64 )
, @FreqRadio varchar(1  )
, @Informatizzato varchar(1  )
, @ProgressivoPallet int
, @CodiceStabilimento varchar(8  )
, @InsUtente varchar(50 )
, @InsDataOra datetime
, @ModUtente varchar(50 )
, @ModDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE Magazzini
SET ID = @ID
,Codice = @Codice
,CodiceDivisione = @CodiceDivisione
,NomeBreve = @NomeBreve
,Descrizione = @Descrizione
,FreqRadio = @FreqRadio
,Informatizzato = @Informatizzato
,ProgressivoPallet = @ProgressivoPallet
,CodiceStabilimento = @CodiceStabilimento
,InsUtente = @InsUtente
,InsDataOra = @InsDataOra
,ModUtente = @ModUtente
,ModDataOra = @ModDataOra
 WHERE ID = @ID

 IF @@ROWCOUNT = 0 
 BEGIN 
 
INSERT INTO Magazzini
(ID
,Codice
,CodiceDivisione
,NomeBreve
,Descrizione
,FreqRadio
,Informatizzato
,ProgressivoPallet
,CodiceStabilimento
,InsUtente
,InsDataOra
,ModUtente
,ModDataOra
 ) VALUES 
( @ID
, @Codice
, @CodiceDivisione
, @NomeBreve
, @Descrizione
, @FreqRadio
, @Informatizzato
, @ProgressivoPallet
, @CodiceStabilimento
, @InsUtente
, @InsDataOra
, @ModUtente
, @ModDataOra
 ) 
 END 


GO
/****** Object:  StoredProcedure [dbo].[spt_SaveOperatori]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE Procedure [dbo].[spt_SaveOperatori]
(  @ID int
, @Login varchar(16 )
, @Nominativo varchar(64 )
, @CodiceUnita varchar(16 )
, @InsUtente varchar(50 )
, @InsDataOra datetime
, @ModUtente varchar(50 )
, @ModDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE Operatori
SET ID = @ID
,Login = @Login
,Nominativo = @Nominativo
,CodiceUnita = @CodiceUnita
,InsUtente = @InsUtente
,InsDataOra = @InsDataOra
,ModUtente = @ModUtente
,ModDataOra = @ModDataOra
 WHERE ID = @ID

 IF @@ROWCOUNT = 0 
 BEGIN 
 
INSERT INTO Operatori
(ID
,Login
,Nominativo
,CodiceUnita
,InsUtente
,InsDataOra
,ModUtente
,ModDataOra
 ) VALUES 
( @ID
, @Login
, @Nominativo
, @CodiceUnita
, @InsUtente
, @InsDataOra
, @ModUtente
, @ModDataOra
 ) 
 END 

GO
/****** Object:  StoredProcedure [dbo].[spt_SaveReparti]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE Procedure [dbo].[spt_SaveReparti]
(  @Codice varchar(8  )
, @CodiceStabilimento varchar(8  )
, @CodiceDivisione varchar(8  )
, @Descrizione varchar(64 )
, @PrfxLotto int
, @PrfxAlias varchar(4  )
, @PrfxCodiceLotto varchar(4  )
, @IDFormato int
, @Nota varchar(128)
, @IndiceSSCC int
, @Prfx varchar(1  )
, @VistaCommesse varchar(10 )
, @CodiceAnagrafica varchar(16 )
, @InsUtente varchar(32 )
, @InsDataOra datetime
, @ModUtente varchar(32 )
, @ModDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE Reparti
SET Codice = @Codice
,CodiceStabilimento = @CodiceStabilimento
,CodiceDivisione = @CodiceDivisione
,Descrizione = @Descrizione
,PrfxLotto = @PrfxLotto
,PrfxAlias = @PrfxAlias
,PrfxCodiceLotto = @PrfxCodiceLotto
,IDFormato = @IDFormato
,Nota = @Nota
,IndiceSSCC = @IndiceSSCC
,Prfx = @Prfx
,VistaCommesse = @VistaCommesse
,CodiceAnagrafica = @CodiceAnagrafica
,InsUtente = @InsUtente
,InsDataOra = @InsDataOra
,ModUtente = @ModUtente
,ModDataOra = @ModDataOra

 WHERE Codice = @Codice

 IF @@ROWCOUNT = 0 
 BEGIN 
 
INSERT INTO Reparti
(Codice
,CodiceStabilimento
,CodiceDivisione
,Descrizione
,PrfxLotto
,PrfxAlias
,PrfxCodiceLotto
,IDFormato
,Nota
,IndiceSSCC
,Prfx
,VistaCommesse
,CodiceAnagrafica
,InsUtente
,InsDataOra
,ModUtente
,ModDataOra
 ) VALUES 
( @Codice
, @CodiceStabilimento
, @CodiceDivisione
, @Descrizione
, @PrfxLotto
, @PrfxAlias
, @PrfxCodiceLotto
, @IDFormato
, @Nota
, @IndiceSSCC
, @Prfx
, @VistaCommesse
, @CodiceAnagrafica
, @InsUtente
, @InsDataOra
, @ModUtente
, @ModDataOra
 ) 
 END 

GO
/****** Object:  StoredProcedure [dbo].[spt_SaveStabilimenti]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



CREATE Procedure [dbo].[spt_SaveStabilimenti]
( @Codice varchar(16 )
, @Descrizione varchar(64 )
, @InsUtente varchar(50 )
, @InsDataOra datetime
, @ModUtente varchar(50 )
, @ModDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE Stabilimenti
SET Codice = @Codice
,Descrizione = @Descrizione
,InsUtente = @InsUtente
,InsDataOra = @InsDataOra
,ModUtente = @ModUtente
,ModDataOra = @ModDataOra
WHERE (Codice = @Codice) 

 IF @@ROWCOUNT = 0 
 BEGIN 
 
INSERT INTO Stabilimenti
(Codice 
,Descrizione
,InsUtente
,InsDataOra
,ModUtente
,ModDataOra
 ) VALUES 
( @Codice
, @Descrizione
, @InsUtente
, @InsDataOra
, @ModUtente
, @ModDataOra
 ) 
 END 




GO
/****** Object:  StoredProcedure [dbo].[spt_SaveUnitaProduzione]    Script Date: 17/09/2025 15:32:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE Procedure [dbo].[spt_SaveUnitaProduzione]
(  @Codice varchar(8  )
, @CodiceReparto varchar(8  )
, @Descrizione varchar(64 )
, @IDMagPrelievo int
, @IDMagVersamento int
, @Nota varchar(128)
, @VistaCommesse varchar(10 )
, @VistaDocumenti varchar(50 )
, @VistaDettagli varchar(50 )
, @InsUtente varchar(32 )
, @InsDataOra datetime
, @ModUtente varchar(32 )
, @ModDataOra datetime
 ) 
 AS 
 SET 
 NOCOUNT ON 
 
UPDATE UnitaProduzione
SET Codice = @Codice
,CodiceReparto = @CodiceReparto
,Descrizione = @Descrizione
,IDMagPrelievo = @IDMagPrelievo
,IDMagVersamento = @IDMagVersamento
,Nota = @Nota
,VistaCommesse = @VistaCommesse
,VistaDocumenti = @VistaDocumenti
,VistaDettagli = @VistaDettagli
,InsUtente = @InsUtente
,InsDataOra = @InsDataOra
,ModUtente = @ModUtente
,ModDataOra = @ModDataOra
 WHERE Codice = @Codice

 IF @@ROWCOUNT = 0 
 BEGIN 
 
	INSERT INTO UnitaProduzione
	(Codice
	,CodiceReparto
	,Descrizione
	,IDMagPrelievo
	,IDMagVersamento
	,Nota
	,VistaCommesse
	,VistaDocumenti
	,VistaDettagli
	,InsUtente
	,InsDataOra
	,ModUtente
	,ModDataOra
	 ) VALUES 
	( @Codice
	, @CodiceReparto
	, @Descrizione
	, @IDMagPrelievo
	, @IDMagVersamento
	, @Nota
	, @VistaCommesse
	, @VistaDocumenti
	, @VistaDettagli
	, @InsUtente
	, @InsDataOra
	, @ModUtente
	, @ModDataOra
 ) 
 END 

GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "XMag_ViewPackingList"
            Begin Extent = 
               Top = 15
               Left = 96
               Bottom = 324
               Right = 424
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 600
         Width = 600
         Width = 600
         Width = 600
         Width = 600
         Width = 600
         Width = 600
         Width = 600
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'ViewPackingListRestante'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'ViewPackingListRestante'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "vPreparaPackingListSAMA1"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 222
               Right = 190
            End
            DisplayFlags = 280
            TopColumn = 3
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 12
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'vPreparaPackingList'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'vPreparaPackingList'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[41] 4[20] 2[10] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "NAZIONI (SAMA1.dbo)"
            Begin Extent = 
               Top = 124
               Left = 32
               Bottom = 319
               Right = 200
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "BAMTES (SAMA1.dbo)"
            Begin Extent = 
               Top = 6
               Left = 426
               Bottom = 222
               Right = 594
            End
            DisplayFlags = 280
            TopColumn = 18
         End
         Begin Table = "LOTTIBF (SAMA1.dbo)"
            Begin Extent = 
               Top = 19
               Left = 31
               Bottom = 134
               Right = 199
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "LOTSER (SAMA1.dbo)"
            Begin Extent = 
               Top = 22
               Left = 233
               Bottom = 137
               Right = 401
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "ARTICO (SAMA1.dbo)"
            Begin Extent = 
               Top = 246
               Left = 38
               Bottom = 361
               Right = 226
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "FATRIG (SAMA1.dbo)"
            Begin Extent = 
               Top = 246
               Left = 264
               Bottom = 361
               Right = 432
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "EXTUC (SAMA1.sam)"
            Begin Extent = 
               Top = 366
               Left = 38
               Bottom = 481' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'vPreparaPackingListSAMA1'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane2', @value=N'
               Right = 206
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "MTRASP (SAMA1.dbo)"
            Begin Extent = 
               Top = 6
               Left = 632
               Bottom = 121
               Right = 800
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 14
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 12
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'vPreparaPackingListSAMA1'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=2 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'vPreparaPackingListSAMA1'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "Celle"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 121
               Right = 250
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "XMag_GiacenzaPallet"
            Begin Extent = 
               Top = 6
               Left = 288
               Bottom = 121
               Right = 459
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "vPreparaPackingList"
            Begin Extent = 
               Top = 126
               Left = 38
               Bottom = 241
               Right = 206
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 12
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'XMag_ViewPackingList'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'XMag_ViewPackingList'
GO
USE [master]
GO
ALTER DATABASE [Mediseawall] SET  READ_WRITE 
GO
