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_1134
    {
        public static Version G_VRS_VERSION = new Version("1.1.3.4");
        public static long G_LNG_VERSION = 1134;
        public static int G_INT_TIPO = 0;
        public static string G_STR_SCRIPT =
                                    //BORRAR el campo codigo_Micro de la tabla MAE_Articulos
                                    "IF exists " +
                                      "( " +
                                      "    SELECT * " +
                                      "    FROM INFORMATION_SCHEMA.COLUMNS " +
                                      "    WHERE COLUMN_NAME='codigo_Micro' AND TABLE_NAME = 'MAE_Articulos' " +
                                      ") " +
                                    "BEGIN " +
                                        " ALTER TABLE [MAE_Articulos] DROP COLUMN codigo_Micro " +
                                    "END " +
                                    //BORRAR TABLA MAE_Articulos_Embalajes_Precios
                                    " IF exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MAE_Articulos_Embalajes_Precios' " +
                                    " ) " +
                                    "BEGIN " +
                                        "DROP TABLE MAE_Articulos_Embalajes_Precios" +
                                    " END " +
                                    //CREAR TABLA MAE_Articulos_EmbalajesPrincipal_Precios
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MAE_Articulos_EmbalajesPrincipal_Precios' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE [MAE_Articulos_EmbalajesPrincipal_Precios] " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY(1,1) NOT NULL, " +
                                            " [idArticulo_Embalaje] [bigint] NOT NULL, " +
                                            " [idNomenclaturaPrecio] [bigint] NOT NULL, " +
                                            " [precio] [decimal](14, 3) NOT NULL, " +
                                            " [actualizarPreciosCompras_Cuando] [int] NULL, " +
                                            " [actualizarPreciosCompras_Preguntar] [bit] NULL, " +
                                            " [actualizarPreciosCompras_Tipo] [int] NULL, " +
                                        " CONSTRAINT[PK_MAE_Articulos_EmbalajesPrincipal_Precios] 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 [MAE_Articulos_EmbalajesPrincipal_Precios] WITH CHECK ADD CONSTRAINT[FK_MAE_Articulos_EmbalajesPrincipal_Precios_MAE_Articulos_Embalajes] FOREIGN KEY([idArticulo_Embalaje]) REFERENCES[dbo].[MAE_Articulos_Embalajes] ([id]) ON DELETE CASCADE " +
                                        " ALTER TABLE [MAE_Articulos_EmbalajesPrincipal_Precios] CHECK CONSTRAINT[FK_MAE_Articulos_EmbalajesPrincipal_Precios_MAE_Articulos_Embalajes] " +

                                        " ALTER TABLE [MAE_Articulos_EmbalajesPrincipal_Precios] WITH CHECK ADD CONSTRAINT[FK_MAE_Articulos_EmbalajesPrincipal_Precios_MAE_NomenclaturasPrecios] FOREIGN KEY([idNomenclaturaPrecio]) REFERENCES[dbo].[MAE_NomenclaturasPrecios] ([id]) " +
                                        " ALTER TABLE [MAE_Articulos_EmbalajesPrincipal_Precios] CHECK CONSTRAINT[FK_MAE_Articulos_EmbalajesPrincipal_Precios_MAE_NomenclaturasPrecios] " +
                                    " END " +
                                    //BORRAR TABLA MAE_Articulos_Embalajes_Precios_Historico
                                    " IF exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MAE_Articulos_Embalajes_Precios_Historico' " +
                                    " ) " +
                                    "BEGIN " +
                                        "DROP TABLE MAE_Articulos_Embalajes_Precios_Historico" +
                                    " END " +
                                    //CREAR TABLA MAE_Articulos_EmbalajesPrincipal_Precios_Historico
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MAE_Articulos_EmbalajesPrincipal_Precios_Historico' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE [MAE_Articulos_EmbalajesPrincipal_Precios_Historico] " +
                                        " ( " +
                                            " [id][bigint] IDENTITY(1,1) NOT NULL, " +
                                            " [idArticulo] [bigint]NOT NULL, " +
                                            " [idEmbalaje] [bigint]NOT NULL, " +
                                            " [idNomenclaturaPrecio] [bigint]NOT NULL, " +
                                            " [idEmpresa] [bigint]NOT NULL, " +
                                            " [fechaHora] [datetime]NOT NULL, " +
                                            " [precio] [decimal](14, 3) NOT NULL, " +
                                        " CONSTRAINT[PK_MAE_Articulos_EmbalajesPrincipal_Precios_Historico] 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 [MAE_Articulos_EmbalajesPrincipal_Precios_Historico] WITH CHECK ADD CONSTRAINT[FK_MAE_Articulos_EmbalajesPrincipal_Precios_Historico_GEN_Empresas] FOREIGN KEY([idEmpresa]) REFERENCES [GEN_Empresas] ([id]) " +
                                        " ALTER TABLE [MAE_Articulos_EmbalajesPrincipal_Precios_Historico] CHECK CONSTRAINT[FK_MAE_Articulos_EmbalajesPrincipal_Precios_Historico_GEN_Empresas] " +
                                                    
                                        " ALTER TABLE [MAE_Articulos_EmbalajesPrincipal_Precios_Historico] WITH CHECK ADD CONSTRAINT[FK_MAE_Articulos_EmbalajesPrincipal_Precios_Historico_MAE_Articulos] FOREIGN KEY([idArticulo]) REFERENCES [MAE_Articulos] ([id]) ON DELETE CASCADE " +
                                        " ALTER TABLE [MAE_Articulos_EmbalajesPrincipal_Precios_Historico] CHECK CONSTRAINT[FK_MAE_Articulos_EmbalajesPrincipal_Precios_Historico_MAE_Articulos] " +
                                                     
                                        " ALTER TABLE [MAE_Articulos_EmbalajesPrincipal_Precios_Historico] WITH CHECK ADD CONSTRAINT[FK_MAE_Articulos_EmbalajesPrincipal_Precios_Historico_MAE_Embalajes] FOREIGN KEY([idEmbalaje]) REFERENCES [MAE_Embalajes] ([id]) " +
                                        " ALTER TABLE [MAE_Articulos_EmbalajesPrincipal_Precios_Historico] CHECK CONSTRAINT[FK_MAE_Articulos_EmbalajesPrincipal_Precios_Historico_MAE_Embalajes] " +
                                                  
                                        " ALTER TABLE [MAE_Articulos_EmbalajesPrincipal_Precios_Historico] WITH CHECK ADD CONSTRAINT[FK_MAE_Articulos_EmbalajesPrincipal_Precios_Historico_MAE_NomenclaturasPrecios] FOREIGN KEY([idNomenclaturaPrecio]) REFERENCES [MAE_NomenclaturasPrecios] ([id]) " +
                                        " ALTER TABLE [MAE_Articulos_EmbalajesPrincipal_Precios_Historico] CHECK CONSTRAINT[FK_MAE_Articulos_EmbalajesPrincipal_Precios_Historico_MAE_NomenclaturasPrecios] " +
                                    " END ";

    }
}
