using Exferia_Aplicacion.General;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Exferia_Aplicacion.General.Enumerados;

namespace Exferia_EntityFramework.Script_Actualizacion
{
    public class Scripts_Version_1144
    {
        public static Version G_VRS_VERSION = new Version("1.1.4.4");
        public static long G_LNG_VERSION = 1144;
        public static int G_INT_TIPO = 0;
        public static string G_STR_SCRIPT =
                                    //CREAR TABLA INV_InventarioCabecera
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'INV_InventarioCabecera' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE [INV_InventarioCabecera] " +
                                        " ( " +
                                            " [id][bigint] IDENTITY(1,1) NOT NULL, " +
                                            " [numeroDocumento] [bigint]NOT NULL, " +
                                            " [descripcion] [varchar](50) NOT NULL, " +
                                            " [idDelegacion] [bigint]NOT NULL, " +
                                            " [idEmpresa] [bigint]NOT NULL, " +
                                            " [fecha] [datetime]NOT NULL, " +
                                            " [guid] [varchar](250) NULL, " +
                                            " [fechaBorrado][datetime]NULL, " +
                                            " [estado][varchar](1) NULL, " +
                                            " [idEmpleado][bigint]NULL, " +
                                        " CONSTRAINT[PK_INV_InventarioCabecera] 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] " +
                                        //Relaciones

                                        " ALTER TABLE [INV_InventarioCabecera] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioCabecera_GEN_Delegaciones] FOREIGN KEY([idDelegacion])REFERENCES [GEN_Delegaciones] ([id]) " +
                                        " ALTER TABLE [INV_InventarioCabecera] CHECK CONSTRAINT[FK_INV_InventarioCabecera_GEN_Delegaciones] " +

                                        " ALTER TABLE [INV_InventarioCabecera] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioCabecera_GEN_Empresas] FOREIGN KEY([idEmpresa]) REFERENCES [GEN_Empresas] ([id]) " +
                                        " ALTER TABLE [INV_InventarioCabecera] CHECK CONSTRAINT[FK_INV_InventarioCabecera_GEN_Empresas] " +

                                        " ALTER TABLE [INV_InventarioCabecera] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioCabecera_MAE_Empleados] FOREIGN KEY([idEmpleado]) REFERENCES [MAE_Empleados] ([id]) " +
                                        " ALTER TABLE [INV_InventarioCabecera] CHECK CONSTRAINT[FK_INV_InventarioCabecera_MAE_Empleados] " +

                                    " END " +
                                    //CREAR TABLA INV_InventarioLineas
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'INV_InventarioLineas' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE [INV_InventarioLineas] " +
                                        " ( " +
                                            " [id][bigint] IDENTITY(1,1) NOT NULL, " +
                                            " [idInventarioCabecera] [bigint]NOT NULL, " +
                                            " [idArticulo] [bigint]NOT NULL, " +
                                            " [numeroLinea][int] NOT NULL, " +        
                                            " [idAlmacen][bigint]NOT NULL, " +
                                            " [idUbicacion][bigint]NULL, " +
                                            " [cantidad][decimal](20, 3) NOT NULL , " +
                                            " [idEmbalaje] [bigint]NOT NULL, " +
                                            " [relacion] [decimal](14, 3) NOT NULL, " +
                                            " [operacion] [varchar](1) NULL, " +
                                            " [segundaUnidad_Precio_Cantidad_SegundaUnidad] [int] NULL, " +
                                            " [segundaUnidad_relacion] [decimal](14, 3) NULL, " +
                                            " [segundaUnidad_idEmbalaje] [bigint] NULL, " +
                                            " [segundaUnidad_cantidad] [decimal](20, 3) NULL, " +
                                            " [embalajePrincipalDelArticulo_idEmbalaje] [bigint]NOT NULL, " +
                                            " [embalajePrincipalDelArticulo_relacion] [decimal](14, 3) NOT NULL, " +
                                            " [embalajePrincipalDelArticulo_operacion] [varchar](1) NULL, " +
                                            " [embalajePrincipalDelArticulo_segundaUnidad_Precio_Cantidad_SegundaUnidad] [int] NULL, " +
                                            " [embalajePrincipalDelArticulo_segundaUnidad_relacion] [decimal](14, 3) NULL, " +
                                            " [embalajePrincipalDelArticulo_segundaUnidad_idEmbalaje] [bigint] NULL, " +
                                            " [fechaBorrado][datetime]NULL, " +
                                        " CONSTRAINT[PK_INV_InventarioLineas] 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] " +
                                            //Relaciones
                                            " ALTER TABLE [INV_InventarioLineas] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioLineas_INV_InventarioCabecera] FOREIGN KEY([idInventarioCabecera])REFERENCES [INV_InventarioCabecera] ([id])ON DELETE CASCADE " +
                                            " ALTER TABLE [INV_InventarioLineas] CHECK CONSTRAINT[FK_INV_InventarioLineas_INV_InventarioCabecera] " +

                                            " ALTER TABLE [INV_InventarioLineas] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioLineas_MAE_Articulos] FOREIGN KEY([idArticulo])REFERENCES [MAE_Articulos] ([id]) " +
                                            " ALTER TABLE [INV_InventarioLineas] CHECK CONSTRAINT[FK_INV_InventarioLineas_MAE_Articulos] " +

                                            " ALTER TABLE [INV_InventarioLineas] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioLineas_GEN_Almacenes] FOREIGN KEY([idAlmacen])REFERENCES [GEN_Almacenes] ([id]) " +
                                            " ALTER TABLE [INV_InventarioLineas] CHECK CONSTRAINT[FK_INV_InventarioLineas_GEN_Almacenes] " +

                                            " ALTER TABLE [INV_InventarioLineas] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioLineas_MAE_Ubicaciones] FOREIGN KEY([idUbicacion])REFERENCES [MAE_Ubicaciones] ([id]) " +
                                            " ALTER TABLE [INV_InventarioLineas] CHECK CONSTRAINT[FK_INV_InventarioLineas_MAE_Ubicaciones] " +

                                            " ALTER TABLE [INV_InventarioLineas] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioLineas_MAE_Embalajes] FOREIGN KEY([idEmbalaje])REFERENCES [MAE_Embalajes] ([id]) " +
                                            " ALTER TABLE [INV_InventarioLineas] CHECK CONSTRAINT[FK_INV_InventarioLineas_MAE_Embalajes] " +

                                            " ALTER TABLE [INV_InventarioLineas] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioLineas_MAE_Embalajes_2] FOREIGN KEY([segundaUnidad_idEmbalaje])REFERENCES [MAE_Embalajes] ([id]) " +
                                            " ALTER TABLE [INV_InventarioLineas] CHECK CONSTRAINT[FK_INV_InventarioLineas_MAE_Embalajes_2] " +

                                            " ALTER TABLE [INV_InventarioLineas] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioLineas_MAE_Embalajes_3] FOREIGN KEY([embalajePrincipalDelArticulo_idEmbalaje]) REFERENCES [MAE_Embalajes] ([id]) " +
                                            " ALTER TABLE [INV_InventarioLineas] CHECK CONSTRAINT[FK_INV_InventarioLineas_MAE_Embalajes_3] " +

                                            " ALTER TABLE [INV_InventarioLineas] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioLineas_MAE_Embalajes_4] FOREIGN KEY([embalajePrincipalDelArticulo_segundaUnidad_idEmbalaje]) REFERENCES [MAE_Embalajes] ([id]) " +
                                            " ALTER TABLE [INV_InventarioLineas] CHECK CONSTRAINT[FK_INV_InventarioLineas_MAE_Embalajes_4] " +

                                    " END " +
                                    // CREAR TABLA INV_InventarioLineas_Detalle
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'INV_InventarioLineas_Detalle' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE [INV_InventarioLineas_Detalle] " +
                                        " ( " +
                                            " [id][bigint] IDENTITY(1,1) NOT NULL, " +
                                            " [idInventarioLinea] [bigint]NOT NULL, " +
                                            " [idNomenclaturaPrecio] [bigint]NOT NULL, " +
                                            " [precio] [decimal](20,3) NOT NULL, " +
                                            " [importe][decimal](20, 2) NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                        " CONSTRAINT[PK_INV_InventarioLineas_Detalle] 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] " +
                                        //Relaciones
                                        " ALTER TABLE [INV_InventarioLineas_Detalle] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioLineas_Detalle_INV_InventarioLineas] FOREIGN KEY([idInventarioLinea])REFERENCES [INV_InventarioLineas] ([id])ON DELETE CASCADE " +
                                        " ALTER TABLE [INV_InventarioLineas_Detalle] CHECK CONSTRAINT[FK_INV_InventarioLineas_Detalle_INV_InventarioLineas] " +

                                        " ALTER TABLE [INV_InventarioLineas_Detalle] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioLineas_Detalle_MAE_NomenclaturasPrecios] FOREIGN KEY([idNomenclaturaPrecio])REFERENCES [MAE_NomenclaturasPrecios] ([id]) " +
                                        " ALTER TABLE [INV_InventarioLineas_Detalle] CHECK CONSTRAINT[FK_INV_InventarioLineas_Detalle_MAE_NomenclaturasPrecios] " +

                                    " END " +
                                    // CREAR TABLA INV_InventarioLineas_Trazabilidad
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'INV_InventarioLineas_Trazabilidad' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE [INV_InventarioLineas_Trazabilidad] " +
                                        " ( " +
                                            " [id][bigint] IDENTITY(1,1) NOT NULL, " +
                                            " [idArticulo_Trazabilidad] [bigint]NOT NULL, " +
                                            " [idInventarioLinea] [bigint]NOT NULL, " +
                                            " [cantidad] [decimal](20, 3) NOT NULL, " +
                                            " [segundaUnidad_cantidad] [decimal](20, 3) NULL, " +
                                            " [fechaBorrado][datetime]NULL, " +
                                        " CONSTRAINT[PK_INV_InventarioLineas_Trazabilidad] 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] " +
                                        //Releaciones
                                        " ALTER TABLE [INV_InventarioLineas_Trazabilidad] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioLineas_Trazabilidad_INV_InventarioLineas] FOREIGN KEY([idInventarioLinea])REFERENCES [INV_InventarioLineas] ([id])ON DELETE CASCADE " +
                                        " ALTER TABLE [INV_InventarioLineas_Trazabilidad] CHECK CONSTRAINT[FK_INV_InventarioLineas_Trazabilidad_INV_InventarioLineas] " +

                                        " ALTER TABLE [INV_InventarioLineas_Trazabilidad] WITH CHECK ADD CONSTRAINT[FK_INV_InventarioLineas_Trazabilidad_MAE_Articulos_Trazabilidad] FOREIGN KEY([idArticulo_Trazabilidad])REFERENCES [MAE_Articulos_Trazabilidad] ([id]) " +
                                        " ALTER TABLE [INV_InventarioLineas_Trazabilidad] CHECK CONSTRAINT[FK_INV_InventarioLineas_Trazabilidad_MAE_Articulos_Trazabilidad] " +
                                    " END ";

    }
}
