using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LLNet.GUI;

namespace LLNet.Tienda.MainForms.Forms.BacTasa0
{
    public static class Logica
    {
        public static String _facturarTasa0 = String.Empty;
        public static List<String> listaSkus = new List<String>();
        public static List<String> listaSkusCant = new List<String>();
        public static String Vencimiento;
        public static String Moneda;
        public static String Medio;
        public static String Autorizacion;
        public static String Tarjeta;
        public static String Modalidad;

        public static DataTable Init_Bac_ObtenerTiposTasa0(List<String> clasificaciones, Decimal montoCompra)
        {

            List<String> where = new List<String>();
            String condicion = "";
            for (int i = 0; i < clasificaciones.Count; i++)
            {
                if (clasificaciones[i] != "")
                {
                    where.Add("bt0_r_clas.C_CLASIFICACION = " + clasificaciones[i]);
                }

            }
            condicion = String.Join(" OR ", where.ToArray());

            String queryString = "SELECT bt0_cond.C_NOMBRE_TASA0,bt0_cond.SEQ_BAC_T0_CONDICION,bt0_cond.N_MONTOMINIMO_TASA0 FROM BAC_T0_CONDICIONES bt0_cond LEFT JOIN " +

                                  "BAC_T0_RANGO_CLASIFICACION bt0_r_clas on bt0_cond.N_ID_CLASIFICACION = bt0_r_clas.N_ID_CLASIFICACION " +

                                  "AND TO_CHAR(SYSDATE, 'yyyy/mm/dd') " +

                                  "BETWEEN TO_CHAR(bt0_cond.D_FECHAINICIO, 'yyyy/mm/dd') AND TO_CHAR(bt0_cond.D_FECHAFIN, 'yyyy/mm/dd') " +

                                  "where " + condicion + " or bt0_cond.N_ID_CLASIFICACION IS NULL GROUP BY bt0_cond.C_NOMBRE_TASA0,bt0_cond.SEQ_BAC_T0_CONDICION,bt0_cond.N_MONTOMINIMO_TASA0 " +

                                  "HAVING " + montoCompra + " >= MIN(bt0_cond.N_MONTOMINIMO_TASA0)";

            var dataTable = new DataTable();

            string connectionString = GUI.CApp.bll.ConnectionString;



            using (System.Data.OracleClient.OracleConnection connection = new System.Data.OracleClient.OracleConnection(connectionString))
            {
                System.Data.OracleClient.OracleCommand command = connection.CreateCommand();

                command.CommandText = queryString;

                try
                {
                    connection.Open();

                    System.Data.OracleClient.OracleDataReader reader = command.ExecuteReader();

                    dataTable.Load(reader);
                    reader.Close();
                    connection.Close();



                    command.Dispose();
                    if (dataTable.Rows.Count == 0)
                    {
                        _facturarTasa0 = "0";
                        //CApp.ShowMessage("No existen condicones Tasa 0 para elegir", "Bac Tasa 0", true);

                    }
                    else
                    {
                        _facturarTasa0 = "1";

                    }

                    return dataTable;

                }
                catch (Exception ex)
                {
                    command.Dispose();
                    connection.Close();
                    Console.WriteLine(ex.Message);
                    return dataTable;
                }
            }
        }
        public static DataTable Init_Bac_ObtenerMonedas()
        {

            String queryString = "SELECT  DISTINCT(c.rv_meaning) \"MONEDA\"" +
            " FROM    par_equivalencias a, cg_ref_codes b, cg_ref_codes c" +
            " WHERE   a.tipo_unidad = 'M'" +
            " AND a.unidad_origen = b.rv_low_value" +
            " AND a.unidad_destino = c.rv_low_value" +
            " AND b.rv_domain = 'MONEDA'" +
            " AND c.rv_domain = 'MONEDA' AND LOWER(c.rv_meaning) = 'colon' OR" +
            " a.tipo_unidad = 'M'" +
            " AND a.unidad_origen = b.rv_low_value" +
            " AND a.unidad_destino = c.rv_low_value" +
            " AND b.rv_domain = 'MONEDA'" +
            " AND c.rv_domain = 'MONEDA' AND LOWER(c.rv_meaning) = 'dolar'";

            var dataTable = new DataTable();

            string connectionString = GUI.CApp.bll.ConnectionString;



            using (System.Data.OracleClient.OracleConnection connection = new System.Data.OracleClient.OracleConnection(connectionString))
            {
                System.Data.OracleClient.OracleCommand command = connection.CreateCommand();

                command.CommandText = queryString;

                try
                {
                    connection.Open();

                    System.Data.OracleClient.OracleDataReader reader = command.ExecuteReader();

                    dataTable.Load(reader);
                    reader.Close();
                    connection.Close();



                    command.Dispose();
                    if (dataTable.Rows.Count == 0)
                    {
                        CApp.ShowMessage("No existen condicones Tasa 0 para elegir", "Bac Tasa 0", true);

                    }

                    return dataTable;

                }
                catch (Exception ex)
                {
                    command.Dispose();
                    connection.Close();
                    Console.WriteLine(ex.Message);
                    return dataTable;
                }
            }
        }
        public static DataTable Init_Bac_ObtenerMetodosPago()
        {

            String queryString = "SELECT C_METODO_PAGO,SEQ_BAC_T0_METODO_PAGO" +
            " FROM  BAC_T0_METODO_PAGO" +
            " WHERE C_ESTADO = 'A'";

            var dataTable = new DataTable();

            string connectionString = GUI.CApp.bll.ConnectionString;



            using (System.Data.OracleClient.OracleConnection connection = new System.Data.OracleClient.OracleConnection(connectionString))
            {
                System.Data.OracleClient.OracleCommand command = connection.CreateCommand();

                command.CommandText = queryString;

                try
                {
                    connection.Open();

                    System.Data.OracleClient.OracleDataReader reader = command.ExecuteReader();

                    dataTable.Load(reader);
                    reader.Close();
                    connection.Close();



                    command.Dispose();
                    if (dataTable.Rows.Count == 0)
                    {
                        CApp.ShowMessage("No existen condicones Tasa 0 para elegir", "Bac Tasa 0", true);

                    }

                    return dataTable;

                }
                catch (Exception ex)
                {
                    command.Dispose();
                    connection.Close();
                    Console.WriteLine(ex.Message);
                    return dataTable;
                }
            }
        }
        public static DataTable Init_Bac_ObtenerClasificaciones()
        {

            String queryString = "SELECT DISTINCT C_CLASIFICACION" +
            " FROM  BAC_T0_RANGO_CLASIFICACION";

            var dataTable = new DataTable();

            string connectionString = GUI.CApp.bll.ConnectionString;



            using (System.Data.OracleClient.OracleConnection connection = new System.Data.OracleClient.OracleConnection(connectionString))
            {
                System.Data.OracleClient.OracleCommand command = connection.CreateCommand();

                command.CommandText = queryString;

                try
                {
                    connection.Open();

                    System.Data.OracleClient.OracleDataReader reader = command.ExecuteReader();

                    dataTable.Load(reader);
                    reader.Close();
                    connection.Close();



                    command.Dispose();
                    if (dataTable.Rows.Count == 0)
                    {
                        CApp.ShowMessage("No existen condicones Tasa 0 para elegir", "Bac Tasa 0", true);

                    }

                    return dataTable;

                }
                catch (Exception ex)
                {
                    command.Dispose();
                    connection.Close();
                    Console.WriteLine(ex.Message);
                    return dataTable;
                }
            }
        }
        public static String Init_Bac_Validar_Sku_Conf_tasa0(List<String> listaClasificaciones, String Sku)
        {

            String queryString = "";
            for (int i = 0; i < listaClasificaciones.Count; i++)
            {
                queryString = "";
                queryString = "SELECT sku from wms_skus_x_clasificacion WHERE SKU = WMS_F_BUSCAR_SKU('" + Sku + "') AND " +
              "clasificacion in ( SELECT  clasificacion " +
              "FROM    wms_clasificaciones " +
              "START   WITH clasificacion = '" + listaClasificaciones[i] + "'" +
              " CONNECT BY PRIOR clasificacion = padre)";

                var dataTable = new DataTable();

                string connectionString = GUI.CApp.bll.ConnectionString;



                using (System.Data.OracleClient.OracleConnection connection = new System.Data.OracleClient.OracleConnection(connectionString))
                {
                    System.Data.OracleClient.OracleCommand command = connection.CreateCommand();

                    command.CommandText = queryString;

                    try
                    {
                        connection.Open();

                        System.Data.OracleClient.OracleDataReader reader = command.ExecuteReader();

                        dataTable.Load(reader);
                        reader.Close();
                        connection.Close();



                        command.Dispose();
                        if (dataTable.Rows.Count == 0)
                        {
                            //CApp.ShowMessage("No existen condicones Tasa 0 para elegir", "Bac Tasa 0", true);

                        }
                        else
                        {
                            String sku_validado = "";
                            foreach (DataRow row in dataTable.Rows)
                            {
                                sku_validado = row["sku"].ToString();
                            }
                            return sku_validado;
                        }



                    }
                    catch (Exception ex)
                    {
                        command.Dispose();
                        connection.Close();
                        Console.WriteLine(ex.Message);

                    }
                }

            }

            return "";

        }
        public static bool CompareList(List<String> list1, List<String> list2)
        {
            if (list1.Count != list2.Count)
                return false;

            for (int i = 0; i < list1.Count; i++)
            {
                if (list1[i] != list2[i])
                    return false;
            }

            return true;
        }
        public static DataTable Init_Bac_Obtener_Conf_tasa0(String SEQ_BAC_T0_CONDICION)
        {

            String queryString = "SELECT *" +
            " FROM  BAC_T0_CONDICIONES WHERE SEQ_BAC_T0_CONDICION = " + SEQ_BAC_T0_CONDICION;

            var dataTable = new DataTable();

            string connectionString = GUI.CApp.bll.ConnectionString;



            using (System.Data.OracleClient.OracleConnection connection = new System.Data.OracleClient.OracleConnection(connectionString))
            {
                System.Data.OracleClient.OracleCommand command = connection.CreateCommand();

                command.CommandText = queryString;

                try
                {
                    connection.Open();

                    System.Data.OracleClient.OracleDataReader reader = command.ExecuteReader();

                    dataTable.Load(reader);
                    reader.Close();
                    connection.Close();



                    command.Dispose();
                    if (dataTable.Rows.Count == 0)
                    {
                        CApp.ShowMessage("No existen condicones Tasa 0 para elegir", "Bac Tasa 0", true);

                    }

                    return dataTable;

                }
                catch (Exception ex)
                {
                    command.Dispose();
                    connection.Close();
                    Console.WriteLine(ex.Message);
                    return dataTable;
                }
            }
        }

        public static String Guardar_Compra_BAC_T0(List<String> listaSkus, List<String> listaSkusCant, String P_N_DOCUMENTO,
            String P_C_NOMBRE_TASA0, String P_N_ID_CLASIFICACION, String P_C_REGISTRADO_POR, String P_C_MONEDA_PAGO,
            String P_C_METODO_PAGO, String P_D_FECHA_VENCE_TARJETA, String P_C_AUTORIZACION, String P_N_MONTO_COMPRA, String P_C_TARJETA, String P_C_PLAZO)
        {

            Utils.QueryParams qp = new LLNet.Utils.QueryParams();

            try
            {

                for (int i = 0; i < listaSkus.Count; i++)
                {
                    qp = new LLNet.Utils.QueryParams();
                    qp.AddReplaceParam("P_C_LISTA_SKUS", listaSkus[i]);
                    qp.AddReplaceParam("P_C_CANT_COMPRA_X_SKU", listaSkusCant[i]);
                    qp.AddReplaceParam("P_N_DOCUMENTO", P_N_DOCUMENTO);
                    qp.AddReplaceParam("P_C_NOMBRE_TASA0", P_C_NOMBRE_TASA0);
                    qp.AddReplaceParam("P_N_ID_CLASIFICACION", P_N_ID_CLASIFICACION);
                    qp.AddReplaceParam("P_C_REGISTRADO_POR", P_C_REGISTRADO_POR);
                    qp.AddReplaceParam("P_C_MONEDA_PAGO", P_C_MONEDA_PAGO);
                    qp.AddReplaceParam("P_C_METODO_PAGO", P_C_METODO_PAGO);
                    qp.AddReplaceParam("P_C_TARJETA", P_C_TARJETA);
                    qp.AddReplaceParam("P_D_FECHA_VENCE_TARJETA", P_D_FECHA_VENCE_TARJETA);
                    qp.AddReplaceParam("P_C_AUTORIZACION", P_C_AUTORIZACION);
                    qp.AddReplaceParam("P_N_MONTO_COMPRA", P_N_MONTO_COMPRA);
                    qp.AddReplaceParam("P_C_PLAZO", P_C_PLAZO);

                    GUI.CApp.bll.ExecuteSql("BAC_I_HIS_T0", qp, "PDV");
                }





            }
            catch (Exception ex)
            {

                return "ERROR";

            }

            return "OK";
        }

        public static String FechaServer()
        {
            return GUI.CApp.bll.GetSingleValue("pos_F_CG_F_SYSDATE", "PDV").ToString();
        }

        public static bool Between(DateTime input, DateTime date1, DateTime date2)
        {
            return (input >= date1 && input <= date2);
        }

        public static String Init_Bac_Validar_Clasificacion_Conf_tasa0(List<String> listaClasificaciones, String Sku)
        {
            List<String> clasificacionesValidas = new List<String>();
            String queryString = "";
            for (int i = 0; i < listaClasificaciones.Count; i++)
            {
                queryString = "";
                queryString = "SELECT sku from wms_skus_x_clasificacion WHERE SKU = WMS_F_BUSCAR_SKU('" + Sku + "') AND " +
              "clasificacion in ( SELECT  clasificacion " +
              "FROM    wms_clasificaciones " +
              "START   WITH clasificacion = '" + listaClasificaciones[i] + "'" +
              " CONNECT BY PRIOR clasificacion = padre)";

                var dataTable = new DataTable();

                string connectionString = GUI.CApp.bll.ConnectionString;



                using (System.Data.OracleClient.OracleConnection connection = new System.Data.OracleClient.OracleConnection(connectionString))
                {
                    System.Data.OracleClient.OracleCommand command = connection.CreateCommand();

                    command.CommandText = queryString;

                    try
                    {
                        connection.Open();

                        System.Data.OracleClient.OracleDataReader reader = command.ExecuteReader();

                        dataTable.Load(reader);
                        reader.Close();
                        connection.Close();



                        command.Dispose();
                        if (dataTable.Rows.Count == 0)
                        {
                            //CApp.ShowMessage("No existen condicones Tasa 0 para elegir", "Bac Tasa 0", true);

                        }
                        else
                        {
                            return listaClasificaciones[i];

                        }



                    }
                    catch (Exception ex)
                    {
                        command.Dispose();
                        connection.Close();
                        Console.WriteLine(ex.Message);
                        return "error";

                    }
                }

            }

            return "";

        }

        public static List<String> RevisarInformacion_Bac_T0(Object rowVENCIMIENTO, Object rowMONEDA
            , Object rowMEDIO, Object rowAUTORIZACION, Object rowTARJETA, Object rowMODALIDAD)
        {
            List<String> codigosMensaje = new List<String>();

            if (rowVENCIMIENTO == null)
            {
                codigosMensaje.Add("1");
            }
            if (rowMONEDA == null)
            {
                codigosMensaje.Add("2");
            }
            if (rowMEDIO == null)
            {
                codigosMensaje.Add("3");
            }
            if (rowAUTORIZACION == null)
            {
                codigosMensaje.Add("4");
            }
            if (rowTARJETA == null)
            {
                codigosMensaje.Add("5");
            }

            if (rowMODALIDAD == null)
            {
                codigosMensaje.Add("6");
            }
            return codigosMensaje;
        }

        public static String GET_CG_PARAMETERS_VALUE(String PARAMETER)
        {
            List<String> clasificacionesValidas = new List<String>();
            String queryString = "";


            queryString = "SELECT VALUE FROM CG_PARAMETERS WHERE PARAMETER = " + "'" + PARAMETER + "'";

            var dataTable = new DataTable();

            string connectionString = GUI.CApp.bll.ConnectionString;



            using (System.Data.OracleClient.OracleConnection connection = new System.Data.OracleClient.OracleConnection(connectionString))
            {
                System.Data.OracleClient.OracleCommand command = connection.CreateCommand();

                command.CommandText = queryString;

                try
                {
                    connection.Open();

                    System.Data.OracleClient.OracleDataReader reader = command.ExecuteReader();

                    dataTable.Load(reader);
                    reader.Close();
                    connection.Close();



                    command.Dispose();
                    if (dataTable.Rows.Count == 0)
                    {
                        return "";

                    }

                    foreach (DataRow row in dataTable.Rows)
                    {
                        return row["VALUE"].ToString();
                    }





                }
                catch (Exception ex)
                {
                    command.Dispose();
                    connection.Close();
                    Console.WriteLine(ex.Message);
                    return "error";

                }
            }



            return "";

        }






    }
}
