using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LLNet.Utils;
using LLNet.GUI;
using System.Data.OracleClient;
using System.IO;
using System.Data.OleDb;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace LLNet.Tienda.MainForms.Marcas___Tecapro
{
    public partial class frm_Marcas : Form
    {
        public static string fecha;
        public static string modulo_edit_focus;
        public static string file_location;
        public static string h_ent;
        public static string h_sal;
        public static string consec_mj;
        public static string reporte_selected;
        public static string consulta_rep;

        public frm_Marcas()
        {
            InitializeComponent();
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_Marcas_Load(object sender, EventArgs e)
        {
            #region Variables de Uso Local
            string conexion;
            string orc_query;
            #endregion

            if (fecha == null)
            {
                fecha = Convert.ToString(DateTime.Now);
            }
            /*/FIND EMPLOYEES WITHOUT REGISTER BY SELECTED DAY/*/ 
           
            /*/EMANUEL CARVAJAL MARIN/*/

            #region Conexion + Fill de Datos
            conexion = CApp.bll.ConnectionString;
            OracleConnection orc = new OracleConnection(conexion);

            orc_query = "SELECT "+" "+
                            "EMAIL, "+" "+
                            "NOMBRES, "+" "+
                            "TIPO_USUARIO, "+" "+
                            "ENTRADA, "+" "+
                            "ENTRADA_JEFE, "+" "+
                            "TIPO_ENTRADA, "+" "+
                            "SALIDA, "+" "+
                            "SALIDA_JEFE, "+" "+
                            "TIPO_SALIDA, "+" "+
                            "MARCA_JEFE, "+" "+
                            "CONSECUTIVO, "+" "+
                            "OBSERVACIONES"+" "+
                        "FROM "+" "+
                            "RH_MARCAS_V "+" "+
                        "WHERE "+" "+
                            "TRUNC(ENTRADA) = TRUNC (to_date('"+fecha+"', 'dd/mm/yyyy hh:mi:ss a.m.')) AND ANULADO_EL IS NULL";

            OracleDataAdapter da = new OracleDataAdapter(orc_query, orc);
            System.Data.DataSet ds = new System.Data.DataSet();
            da.Fill(ds, "RH_MARCAS_V");

            gridControl1.DataSource = ds;
            gridControl1.DataMember = "RH_MARCAS_V";
           

            #endregion

            #region Carga Combo - Usuarios

            /*/GET BODEGA ID/*/
            string parameter_result;
            CApp.bll.ExecuteSql("GET_BODEGA_VALUE", "PDV");
            parameter_result = Convert.ToString(CApp.bll.GetSingleValue("GET_BODEGA_VALUE", "PDV"));
            /*/EMANUEL CARVAJAL MARIN/*/

            string query_users;
            OracleConnection orc1 = new OracleConnection(conexion);
            query_users = "SELECT "+" "+ 
                          "WMS_USUARIOS.EMAIL FROM WMS_USUARIOS, WMS_USUARIOS_X_BODEGA "+" "+ 
                          "WHERE "+" "+ 
                          "WMS_USUARIOS.EMAIL IS NOT NULL AND WMS_USUARIOS.VENDEDOR <> 'N' AND"+" "+
                          "WMS_USUARIOS.USUARIO = WMS_USUARIOS_X_BODEGA.USUARIO AND"+" "+
                          "WMS_USUARIOS_X_BODEGA.BODEGA ="+parameter_result;
            OracleDataAdapter da1 = new OracleDataAdapter(query_users, conexion);
            Marcas___Tecapro.dsMarcas_pos ds1 = new dsMarcas_pos();

            da1.Fill(ds1, "WMS_USUARIOS");

            comboBoxEdit3.Properties.Items.Clear();
            comboBoxEdit2.Properties.Items.Clear();
            comboBoxEdit6.Properties.Items.Clear();

            string valor_item;
            string valor_item_2;
            string valor_item_3;
            string valor_item_4;

            for (int i = 0; i < ds1.WMS_USUARIOS.Rows.Count; i++)
            {
                valor_item = Convert.ToString(ds1.WMS_USUARIOS.Rows[i]["EMAIL"]);
                comboBoxEdit3.Properties.Items.Add(valor_item);

            }

            for (int i = 0; i < ds1.WMS_USUARIOS.Rows.Count; i++)
            {
                valor_item_2 = Convert.ToString(ds1.WMS_USUARIOS.Rows[i]["EMAIL"]);
                comboBoxEdit2.Properties.Items.Add(valor_item_2);

            }

            for (int i = 0; i < ds1.WMS_USUARIOS.Rows.Count; i++)
            {
                valor_item_3 = Convert.ToString(ds1.WMS_USUARIOS.Rows[i]["EMAIL"]);
                comboBoxEdit4.Properties.Items.Add(valor_item_3);

            }

            for (int i = 0; i < ds1.WMS_USUARIOS.Rows.Count; i++)
            {
                valor_item_4 = Convert.ToString(ds1.WMS_USUARIOS.Rows[i]["EMAIL"]);
                comboBoxEdit6.Properties.Items.Add(valor_item_4);

            }

            #endregion
            
            #region Carga Combo - Tipos Marcas
            carga_combo_tipos_marcas();
            #endregion

            #region Carga Combo - Tipos Entradas 
            carga_combo_tipos_entradas();
            #endregion

            #region Carga Combo Tipos Salidas
            carga_combo_tipos_salidas();
            #endregion

            #region Configura Acceso - Reportes
            int val_rtn;

            Utils.QueryParams qp117 = new LLNet.Utils.QueryParams();
            qp117.AddReplaceParam("Param1", GUI.CApp.usuario);
            val_rtn = Convert.ToInt32(GUI.CApp.bll.GetSingleValue("VALIDATE_USER_REPORTS_ACCESS", qp117, "PDV"));

            comboBoxEdit5.Properties.Items.Clear();

            if (val_rtn >= 1)
            {
               comboBoxEdit5.Properties.Items.Add("Control HRS - Tienda");
               comboBoxEdit5.Properties.Items.Add("Control HRS - Consolidado");
               comboBoxEdit5.Properties.Items.Add("Control HRS - Segmentado");
               comboBoxEdit5.Properties.Items.Add("Control HRS - Empleado");
               comboBoxEdit5.Properties.Items.Add("Control HRS - Total Horas");
               simpleButton14.Enabled = true;
               crystalReportViewer1.ShowExportButton = true;
               crystalReportViewer1.ShowPrintButton = true;

            }
            else
            {
               comboBoxEdit5.Properties.Items.Add("Control HRS - Tienda");
               comboBoxEdit5.Properties.Items.Add("Control HRS - Total Horas");
               simpleButton14.Enabled = false;
               crystalReportViewer1.ShowExportButton = false;
               crystalReportViewer1.ShowPrintButton = false;


            }

            #endregion

            marcas_jefe_enable_controls();
            validate_access();
            get_libres();
            ParametroRuta();
        }

        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            fecha = Convert.ToString(monthCalendar1.SelectionRange.Start);
            frm_Marcas_Load(null, null);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            xtraTabControl1.SelectedTabPage = xtraTabPage2;
        }

        public void move_into_form()
        {
            xtraTabControl1.SelectedTabPage = xtraTabPage2;
        }

        public void validate_access()
        {
            if (user_validate.user_log_into_sys == null)
            {
                user_validate frm = new user_validate();
                frm.ShowDialog();
                if (user_validate.chk_ok == true)
                {
                    xtraTabControl1.Enabled = true;
                }
                else
                {
                    this.Close();
                    xtraTabControl1.Enabled = false;
                    return;
                }
            }
            else
            {
                //do nothing
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            xtraTabControl1.SelectedTabPage = xtraTabPage4;
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            xtraTabControl1.SelectedTabPage = xtraTabPage5;
        }

        private void frm_Marcas_FormClosing(object sender, FormClosingEventArgs e)
        {
            user_validate.user_log_into_sys = null;
            user_validate.user_pass_code = null;
            user_validate.chk_ok = false;

            if (GUI.CApp.ShowMessage("Desea Salir del Mòdulo de Marcas?", "Mòdulo de Marcas - Consolidadas", true, true) == DialogResult.OK)
            {
                this.Dispose();
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void marcas_jefe_enable_controls()
        {
            comboBoxEdit2.Enabled = false;
            comboBoxEdit1.Enabled = false;
            dateEdit1.Enabled = false;
            checkedListBoxControl1.Enabled = false;
            comboBoxEdit6.Enabled = false;


            /*/SE LIMPIAN LOS CAMPOS DE EDICION/*/ 
            //comboBoxEdit2.Text = "";
            //comboBoxEdit1.Text = "";
            //dateEdit2.Text = "";
            //textEdit2.Text = "";
            /*/EMANUEL CARVAJAL MARIN/*/

            simpleButton10.Enabled = false;
            simpleButton9.Enabled = true;
            simpleButton7.Enabled = true;
            simpleButton2.Enabled = true;
            simpleButton12.Enabled = false;
            gridView2.OptionsBehavior.Editable = false;

            /*/HABILITA LA SECCION DE BÙESQUEDA/*/
            comboBoxEdit3.Enabled = true;
            dateEdit2.Enabled = true;
            simpleButton6.Enabled = true;
            /*/FIN-COD BLOQ/*/

        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            #region Vatiables de Uso Local
      
            string usuario;
            string fecha;
            
            #endregion 
            if (checkEdit1.Checked == true)
            {
                #region Conexion + Fill de Datos

                string conexion;
                string orc_query;

                conexion = CApp.bll.ConnectionString;
                OracleConnection orc = new OracleConnection(conexion);

                orc_query = "SELECT "+" "+
                                "NOMBRES, "+" "+ 
                                "USUARIO, "+" "+
                                "EMAIL, "+" "+
                                "ENTRADA, "+" "+
                                "ENTRADA_JEFE, "+" "+
                                "TIPO_ENTRADA, "+" "+
                                "SALIDA, "+" "+
                                "SALIDA_JEFE, "+" "+
                                "TIPO_SALIDA, "+" "+
                                "MARCA_JEFE, "+" "+
                                "CREADO_EL, " + " " +
                                "CONSECUTIVO, " + " " +
                                "OBSERVACIONES "+" "+
                            "FROM "+" "+ 
                                "RH_MARCAS_V "+" "+
                            "WHERE "+" "+
                                "EMAIL ="+comboBoxEdit3.Text+" "+
                                "AND ANULADO_EL IS NULL "+" "+
                                "AND INTERFASADO_EL IS NULL"+" ";
                               
                OracleDataAdapter da = new OracleDataAdapter(orc_query, orc);
                System.Data.DataSet ds = new System.Data.DataSet();
                da.Fill(ds, "RH_MARCAS_V");

                gridControl2.DataSource = ds;
                gridControl2.DataMember = "RH_MARCAS_V";
                #endregion

            }
            else
            {
                if (comboBoxEdit3.Text == "")
                {
                    GUI.CApp.ShowMessage("Verfique que el campo Usuario no se encuentre vacio", "Error en Bùsqueda", true);
                    return;
                }
                else if (dateEdit2.Text == "")
                {
                    GUI.CApp.ShowMessage("Verfique que el campo Fecha no se encuentre vacio", "Error en Bùsqueda", true);
                    return;
                }

                usuario = comboBoxEdit3.Text;
                fecha = dateEdit2.Text;

                /*/FILL DE DATOS POR BÙSQUEDA/*/
                finder_by_user_params(usuario, fecha);
                /*/FIN-EMANUEL CARVAJAL MARÌN/*/
            }
        }

        private void gridControl2_Click(object sender, EventArgs e)
        {  
        }

        private void simpleButton9_Click(object sender, EventArgs e)
        {
            foreach (int handle in gridView2.GetSelectedRows())
            {
                h_ent = Convert.ToString(gridView2.GetRowCellValue(handle, col_Entrada_mj));
                h_sal = Convert.ToString(gridView2.GetRowCellValue(handle, col_Salida_mj));
                consec_mj = Convert.ToString(gridView2.GetRowCellValue(handle, colConsecutivo_mj));
            }

            int v_modify_m;

            QueryParams qpOF1117 = new QueryParams();
            qpOF1117.AddReplaceParam("Param1", h_ent);
            CApp.bll.ExecuteSql("VALIDATE_MODIFY_M", qpOF1117, "PDV");
            v_modify_m = Convert.ToInt32(CApp.bll.GetSingleValue("VALIDATE_MODIFY_M", qpOF1117, "PDV"));

            if (v_modify_m >= 1)
            {
                GUI.CApp.ShowMessage("La Marca Seleccionada no se puede editar, dado a que la fecha ya transcurrio", "Error al Editar Marca", true);
                return;
            }
            else
            {
                int result = 0;
                QueryParams qpOF = new QueryParams();
                qpOF.AddReplaceParam("Param1", consec_mj);
                CApp.bll.ExecuteSql("GET_VALIDATION_F_E", qpOF, "PDV");
                result = Convert.ToInt32(CApp.bll.GetSingleValue("GET_VALIDATION_F_E", qpOF, "PDV"));

                if (result < 1)
                {
                    GUI.CApp.ShowMessage("Imposible editar esta Marca, La Marca ya fue Interfasada", "Error al Editar", true);
                    return;
                }

                else
                {
                    modulo_edit_focus = "Editar Marca";

                    #region Estado de Controles
                    /*/PERMISOS DE EDICION DEL GRID/*/
                    gridView2.OptionsBehavior.Editable = true;
                    col_Usuario_mj.OptionsColumn.AllowEdit = false;
                    col_Tipo_Entrada_mj.OptionsColumn.AllowEdit = false;
                    col_Tipo_Salida_mj.OptionsColumn.AllowEdit = false;
                    col_Marca_Jefe_mj.OptionsColumn.AllowEdit = false;
                    colCreada_el_mj.OptionsColumn.AllowEdit = false;
                    col_Entrada_mj.OptionsColumn.AllowEdit = false;
                    col_Salida_mj.OptionsColumn.AllowEdit = false;
                    /*/FIN/*/
                    simpleButton7.Enabled = false;
                    simpleButton10.Enabled = true;
                    simpleButton2.Enabled = false;
                    simpleButton9.Enabled = false;
                    /*/SE BLOQUEA LA SECCION DE BÙESQUEDA/*/
                    comboBoxEdit3.Enabled = false;
                    dateEdit2.Enabled = false;
                    simpleButton6.Enabled = false;
                    /*/FIN-COD BLOQ/*/
                    #endregion
                }
            }
        }

        private void simpleButton10_Click(object sender, EventArgs e)
        {
            marcas_jefe_enable_controls();

            #region Variables de Uso Local
            string tipo_marca;
            string value_tipo_marca;
            string value_tipo_marca_2;
            string value_tipo_marca_5;
            int value_tipo_marca_3;
            int value_tipo_marca_4;
            int v_modify_m;
            #endregion

            QueryParams qpOF1117 = new QueryParams();
            qpOF1117.AddReplaceParam("Param1", dateEdit1.Text);
            CApp.bll.ExecuteSql("VALIDATE_MODIFY_M", qpOF1117, "PDV");
            v_modify_m = Convert.ToInt32(CApp.bll.GetSingleValue("VALIDATE_MODIFY_M", qpOF1117, "PDV"));

            if (v_modify_m >= 1)
            {
                GUI.CApp.ShowMessage("Imposible Registrar Marca, dado a que la fecha ya transcurrio", "Error al Ingresar Nueva Marca", true);
                return;
            }
            else
            {
                tipo_marca = comboBoxEdit1.Text;

                QueryParams qpOF = new QueryParams();
                qpOF.AddReplaceParam("Param", tipo_marca);
                CApp.bll.ExecuteSql("TIPO_MARCAS", qpOF, "PDV");
                value_tipo_marca = Convert.ToString(CApp.bll.GetSingleValue("TIPO_MARCAS", qpOF, "PDV"));

                switch (modulo_edit_focus)
                {
                    case "Nueva Marca":
                        if (comboBoxEdit2.Text == "")
                        {
                            MessageBox.Show("Seleccione el Usuario");
                        }
                        else
                        {
                            #region Nueva Marca - Modulo
                            switch (comboBoxEdit1.Text)
                            {
                                #region Salida
                                case "Salida":

                                    QueryParams qpOF2 = new QueryParams();
                                    qpOF2.AddReplaceParam("Param1", dateEdit1.Text);
                                    qpOF2.AddReplaceParam("Param2", comboBoxEdit2.Text);
                                    qpOF2.AddReplaceParam("Param3", value_tipo_marca);

                                    CApp.bll.ExecuteSql("VALIDA_MARCA_ENT_GEN", qpOF2, "PDV");
                                    value_tipo_marca_2 = Convert.ToString(CApp.bll.GetSingleValue("VALIDA_MARCA_ENT_GEN", qpOF2, "PDV"));

                                    if (value_tipo_marca_2 != null)
                                    {
                                        string param_5 = "01";

                                        QueryParams qpOF3 = new QueryParams();
                                        qpOF3.AddReplaceParam("Param1", dateEdit1.Text);
                                        qpOF3.AddReplaceParam("Param2", value_tipo_marca);
                                        qpOF3.AddReplaceParam("Param3", comboBoxEdit2.Text);
                                        qpOF3.AddReplaceParam("Param4", user_validate.user_log_into_sys);
                                        qpOF3.AddReplaceParam("Param5", param_5);
                                        qpOF3.AddReplaceParam("Param6", value_tipo_marca_2);

                                        CApp.bll.ExecuteSql("UPDATE_MARCA_JEFE_S", qpOF3, "PDV");
                                        GUI.CApp.ShowMessage("Salida " + dateEdit1.Text + " del usuario " + comboBoxEdit2.Text + " resgistrada con èxito", "Marca Jefe - Salida", true);
                                        finder_by_user_params(comboBoxEdit2.Text, dateEdit1.Text);


                                        /*/LIMPIA LOS CAMPOS /*/
                                        comboBoxEdit2.Text = "";
                                        comboBoxEdit1.Text = "";
                                        dateEdit1.Text = "";
                                        /*/FIN/*/


                                        /*/LIMPIA CAMPOS DE BÙSQUEDA/*/
                                        comboBoxEdit3.Text = "";
                                        dateEdit2.Text = "";
                                        /*/FIN/*/
                                    }

                                    else
                                    {
                                        GUI.CApp.ShowMessage("Imposible Registrar una Salida para la Fecha indicada, el usuario no posee entradas o las marcas ya fueron Interfasadas", "Error Marca Jefe - Salida", true);

                                        /*/LIMPIA LOS CAMPOS /*/
                                        comboBoxEdit2.Text = "";
                                        comboBoxEdit1.Text = "";
                                        dateEdit1.Text = "";
                                        /*/FIN/*/


                                        /*/LIMPIA CAMPOS DE BÙSQUEDA/*/
                                        comboBoxEdit3.Text = "";
                                        dateEdit2.Text = "";
                                        /*/FIN/*/

                                        return;
                                    }

                                    break;
                                #endregion

                                #region Entrada
                                case "Entrada":

                                    QueryParams qpOF5 = new QueryParams();
                                    qpOF5.AddReplaceParam("Param1", comboBoxEdit2.Text);
                                    qpOF5.AddReplaceParam("Param2", dateEdit1.Text);
                                    qpOF5.AddReplaceParam("Param3", value_tipo_marca);

                                    CApp.bll.ExecuteSql("VALIDA_MARCA_JEFE_ENTRADAS", qpOF5, "PDV");
                                    value_tipo_marca_3 = Convert.ToInt32(CApp.bll.GetSingleValue("VALIDA_MARCA_JEFE_ENTRADAS", qpOF5, "PDV"));

                                    if (value_tipo_marca_3 > 0 && value_tipo_marca_3 < 2)
                                    {
                                        GUI.CApp.ShowMessage("El Usuario " + comboBoxEdit2.Text + " posee una MARCA DE ENTRADA sin SALIDA previamente registrada en la fecha indicada, es necesario que registre la SALIDA de esa anterior ENTRADA antes de registrar una nueva", "Error Marca Jefe - Entrada", true);

                                        /*/LIMPIA LOS CAMPOS /*/
                                        comboBoxEdit2.Text = "";
                                        comboBoxEdit1.Text = "";
                                        dateEdit1.Text = "";
                                        /*/FIN/*/


                                        /*/LIMPIA CAMPOS DE BÙSQUEDA/*/
                                        comboBoxEdit3.Text = "";
                                        dateEdit2.Text = "";
                                        /*/FIN/*/

                                        return;
                                    }
                                    else
                                    {
                                        QueryParams qpOF4 = new QueryParams();
                                        qpOF4.AddReplaceParam("Param1", comboBoxEdit2.Text);
                                        qpOF4.AddReplaceParam("Param2", dateEdit1.Text);
                                        qpOF4.AddReplaceParam("Param3", value_tipo_marca);
                                        qpOF4.AddReplaceParam("Param4", user_validate.user_log_into_sys);
                                        CApp.bll.ExecuteSql("INSERT_MARCA_JEFE_E", qpOF4, "PDV");

                                        GUI.CApp.ShowMessage("Entrada " + dateEdit1.Text + " del usuario " + comboBoxEdit2.Text + " resgistrada con èxito", "Marca Jefe - Entrada", true);

                                        finder_by_user_params(comboBoxEdit2.Text, dateEdit1.Text);


                                        /*/LIMPIA LOS CAMPOS /*/
                                        comboBoxEdit2.Text = "";
                                        comboBoxEdit1.Text = "";
                                        dateEdit1.Text = "";
                                        /*/FIN/*/


                                        /*/LIMPIA CAMPOS DE BÙSQUEDA/*/
                                        comboBoxEdit3.Text = "";
                                        dateEdit2.Text = "";
                                        /*/FIN/*/
                                    }

                                    break;
                                #endregion

                                #region Entrada Almuerzo
                                case "Entrada Almuerzo":

                                    QueryParams qpOF7 = new QueryParams();
                                    qpOF7.AddReplaceParam("Param1", comboBoxEdit2.Text);
                                    qpOF7.AddReplaceParam("Param2", dateEdit1.Text);
                                    qpOF7.AddReplaceParam("Param3", value_tipo_marca);

                                    CApp.bll.ExecuteSql("VALIDA_MARCA_JEFE_ENTRADAS", qpOF7, "PDV");
                                    value_tipo_marca_4 = Convert.ToInt32(CApp.bll.GetSingleValue("VALIDA_MARCA_JEFE_ENTRADAS", qpOF7, "PDV"));

                                    if (value_tipo_marca_4 > 0 && value_tipo_marca_4 < 2)
                                    {
                                        GUI.CApp.ShowMessage("El Usuario " + comboBoxEdit2.Text + " posee una MARCA DE ENTRADA DE ALMUERZO sin REGRESO DE ALMUERZO previamente registrado en la fecha indicada, es necesario que registre la REGRESO de esa anterior ENTRADA antes de registrar una nueva", "Error Marca Jefe - Entrada Almuerzo", true);

                                        /*/LIMPIA LOS CAMPOS /*/
                                        comboBoxEdit2.Text = "";
                                        comboBoxEdit1.Text = "";
                                        dateEdit1.Text = "";
                                        /*/FIN/*/


                                        /*/LIMPIA CAMPOS DE BÙSQUEDA/*/
                                        comboBoxEdit3.Text = "";
                                        dateEdit2.Text = "";
                                        /*/FIN/*/

                                        return;
                                    }
                                    else
                                    {
                                        QueryParams qpOF8 = new QueryParams();
                                        qpOF8.AddReplaceParam("Param1", comboBoxEdit2.Text);
                                        qpOF8.AddReplaceParam("Param2", dateEdit1.Text);
                                        qpOF8.AddReplaceParam("Param3", value_tipo_marca);
                                        qpOF8.AddReplaceParam("Param4", user_validate.user_log_into_sys);
                                        CApp.bll.ExecuteSql("INSERT_MARCA_JEFE_LUNCH", qpOF8, "PDV");

                                        GUI.CApp.ShowMessage("Entrada a Almuerzo " + dateEdit1.Text + " del usuario " + comboBoxEdit2.Text + " resgistrada con èxito", "Marca Jefe - Entrada Almuerzo", true);

                                        finder_by_user_params(comboBoxEdit2.Text, dateEdit1.Text);


                                        /*/LIMPIA LOS CAMPOS /*/
                                        comboBoxEdit2.Text = "";
                                        comboBoxEdit1.Text = "";
                                        dateEdit1.Text = "";
                                        /*/FIN/*/


                                        /*/LIMPIA CAMPOS DE BÙSQUEDA/*/
                                        comboBoxEdit3.Text = "";
                                        dateEdit2.Text = "";
                                        /*/FIN/*/
                                    }


                                    break;
                                #endregion

                                #region Regreso Almuerzo
                                case "Regreso Almuerzo":

                                    QueryParams qpOF9 = new QueryParams();
                                    qpOF9.AddReplaceParam("Param1", dateEdit1.Text);
                                    qpOF9.AddReplaceParam("Param2", comboBoxEdit2.Text);
                                    qpOF9.AddReplaceParam("Param3", value_tipo_marca);

                                    CApp.bll.ExecuteSql("VALIDA_MARCA_ENT_GEN", qpOF9, "PDV");
                                    value_tipo_marca_5 = Convert.ToString(CApp.bll.GetSingleValue("VALIDA_MARCA_ENT_GEN", qpOF9, "PDV"));

                                    if (value_tipo_marca_5 != null)
                                    {
                                        string param_5 = "02";
                                        QueryParams qpOF10 = new QueryParams();
                                        qpOF10.AddReplaceParam("Param1", dateEdit1.Text);
                                        qpOF10.AddReplaceParam("Param2", value_tipo_marca);
                                        qpOF10.AddReplaceParam("Param3", comboBoxEdit2.Text);
                                        qpOF10.AddReplaceParam("Param4", user_validate.user_log_into_sys);
                                        qpOF10.AddReplaceParam("Param5", param_5);
                                        qpOF10.AddReplaceParam("Param6", value_tipo_marca_5);


                                        CApp.bll.ExecuteSql("UPDATE_MARCA_JEFE_S", qpOF10, "PDV");
                                        GUI.CApp.ShowMessage("Regreso Almuerzo " + dateEdit1.Text + " del usuario " + comboBoxEdit2.Text + " resgistrado con èxito", "Marca Jefe -Regreso Almuerzo", true);

                                        finder_by_user_params(comboBoxEdit2.Text, dateEdit1.Text);


                                        /*/LIMPIA LOS CAMPOS /*/
                                        comboBoxEdit2.Text = "";
                                        comboBoxEdit1.Text = "";
                                        dateEdit1.Text = "";
                                        /*/FIN/*/


                                        /*/LIMPIA CAMPOS DE BÙSQUEDA/*/
                                        comboBoxEdit3.Text = "";
                                        dateEdit2.Text = "";
                                        /*/FIN/*/
                                    }

                                    else
                                    {
                                        GUI.CApp.ShowMessage("Imposible Registrar un Regreso de Almuerzo para la Fecha indicada, el usuario no posee Entradas a Almuerzo o las marcas ya fueron sincronizadas", "Error Marca Jefe - Regreso Almuerzo", true);

                                        /*/LIMPIA LOS CAMPOS /*/
                                        comboBoxEdit2.Text = "";
                                        comboBoxEdit1.Text = "";
                                        dateEdit1.Text = "";
                                        /*/FIN/*/


                                        /*/LIMPIA CAMPOS DE BÙSQUEDA/*/
                                        comboBoxEdit3.Text = "";
                                        dateEdit2.Text = "";
                                        /*/FIN/*/

                                        return;
                                    }


                                    break;
                                #endregion

                            }

                            /*/LIMPIA LOS CAMPOS /*/
                            comboBoxEdit2.Text = "";
                            comboBoxEdit1.Text = "";
                            dateEdit1.Text = "";
                            /*/FIN/*/

                            /*/LIMPIA CAMPOS DE BÙSQUEDA/*/
                            comboBoxEdit3.Text = "";
                            dateEdit2.Text = "";
                            /*/FIN/*/
                            #endregion
                        }
                        break;
                    case "Editar Marca":
                        #region Editar Marca
                        string entrada_date = "";
                        string salida_date = "";
                        string user_on_selected_row = "";
                        string creado_el = "";

                        foreach (int handle in gridView2.GetSelectedRows())
                        {
                            entrada_date = Convert.ToString(gridView2.GetRowCellValue(handle, colEntradaJefe));
                            salida_date = Convert.ToString(gridView2.GetRowCellValue(handle, colSalidaJefe));
                            user_on_selected_row = Convert.ToString(gridView2.GetRowCellValue(handle, col_Usuario_mj));
                            creado_el = Convert.ToString(gridView2.GetRowCellValue(handle, colCreada_el_mj));
                        }

                        QueryParams qpOF11 = new QueryParams();
                        qpOF11.AddReplaceParam("Param1", entrada_date);
                        qpOF11.AddReplaceParam("Param2", salida_date);
                        qpOF11.AddReplaceParam("Param3", user_validate.user_log_into_sys);
                        qpOF11.AddReplaceParam("Param4", h_ent);
                        qpOF11.AddReplaceParam("Param6", user_on_selected_row);
                        qpOF11.AddReplaceParam("Param7", creado_el);



                        CApp.bll.ExecuteSql("UPDATE_MARCAS", qpOF11, "PDV");

                        GUI.CApp.ShowMessage("Cambios Actualizados con Èxito", "Marca Jefe - Editar Marcas", true);
                        finder_by_user_params(user_on_selected_row, entrada_date);



                        #endregion
                        break;
                }
            }
        }

        private void simpleButton7_Click(object sender, EventArgs e)
        {
            #region Config Controles
            simpleButton9.Enabled = false;
            simpleButton10.Enabled = true;
            comboBoxEdit2.Enabled = true;
            comboBoxEdit1.Enabled = true;
            dateEdit1.Enabled = true;
            simpleButton2.Enabled = false;
            simpleButton7.Enabled = false;

            /*/SE BLOQUEA LA SECCION DE BÙESQUEDA/*/
            comboBoxEdit3.Enabled = false;
            comboBoxEdit3.Text = "";
            dateEdit2.Enabled = false;
            dateEdit2.Text = "";
            simpleButton6.Enabled = false;
            /*/FIN-COD BLOQ/*/
            #endregion 


            modulo_edit_focus = "Nueva Marca";

        }

        private void simpleButton8_Click(object sender, EventArgs e)
        {
            marcas_jefe_enable_controls();
            frm_Marcas_Load(null, null);
            xtraTabControl1.SelectedTabPage = xtraTabPage1;

        }

        private void simpleButton2_Click_1(object sender, EventArgs e)
        {
            modulo_edit_focus = "Borrar Marca";
             
                #region Borrar Marca

                    string entrada_date_d = "";
                    string creado_el_date_d = "";
                    string user_on_selected_row_d = "";

                    foreach (int handle in gridView2.GetSelectedRows())
                    {
                        entrada_date_d = Convert.ToString(gridView2.GetRowCellValue(handle, col_Entrada_mj));
                        creado_el_date_d = Convert.ToString(gridView2.GetRowCellValue(handle, colCreada_el_mj));
                        user_on_selected_row_d = Convert.ToString(gridView2.GetRowCellValue(handle, col_Usuario_mj));
                    }

                    if (GUI.CApp.ShowMessage("Desea anular esta Marca?", "Marca Jefe - Anular Marca", true, true) == DialogResult.OK)
                    {
                        QueryParams qpOF12 = new QueryParams();
                        qpOF12.AddReplaceParam("Param1", Convert.ToString(DateTime.Now));
                        qpOF12.AddReplaceParam("Param2", user_validate.user_log_into_sys);
                        qpOF12.AddReplaceParam("Param3", entrada_date_d);
                        qpOF12.AddReplaceParam("Param4", creado_el_date_d);
                        qpOF12.AddReplaceParam("Param5", user_on_selected_row_d);
                        CApp.bll.ExecuteSql("ANULADO_EL", qpOF12, "PDV");

                        GUI.CApp.ShowMessage("Marca Anulada con Èxito", "Marca Jefe - Anular Marca", true);
                        finder_by_user_params(user_on_selected_row_d, entrada_date_d);


                    }
                    else
                    {
                        return;
                    }

                #endregion
           
        }

        private void carga_combo_tipos_marcas()
        {
        
            string query_tipo_m;
            OracleConnection orc2 = new OracleConnection(CApp.bll.ConnectionString);
            query_tipo_m = "SELECT RV_MEANING FROM CG_REF_CODES WHERE RV_DOMAIN = 'TIPOS_MARCAS_ENTRADAS' UNION SELECT RV_MEANING FROM CG_REF_CODES WHERE RV_DOMAIN = 'TIPOS_MARCAS_SALIDAS'";
            OracleDataAdapter da2 = new OracleDataAdapter(query_tipo_m, CApp.bll.ConnectionString);
            Marcas___Tecapro.dsMarcas_pos ds2 = new dsMarcas_pos();

            da2.Fill(ds2,"CG_REF_CODES");

            comboBoxEdit1.Properties.Items.Clear();

            string valor_item_3;


            for (int i = 0; i < ds2.CG_REF_CODES.Rows.Count; i++)
            {
                valor_item_3 = Convert.ToString(ds2.CG_REF_CODES.Rows[i]["RV_MEANING"]);
                comboBoxEdit1.Properties.Items.Add(valor_item_3);

            }

        }

        private void carga_combo_tipos_entradas()
        {

            string query_tipo_m;
            OracleConnection orc2 = new OracleConnection(CApp.bll.ConnectionString);
            query_tipo_m = "SELECT RV_MEANING FROM CG_REF_CODES WHERE RV_DOMAIN = 'TIPOS_MARCAS_ENTRADAS'";
            OracleDataAdapter da2 = new OracleDataAdapter(query_tipo_m, CApp.bll.ConnectionString);
            Marcas___Tecapro.dsMarcas_pos ds2 = new dsMarcas_pos();

            da2.Fill(ds2, "CG_REF_CODES");

            repositoryItemComboBox1.Items.Clear();

            string valor_item_3;


            for (int i = 0; i < ds2.CG_REF_CODES.Rows.Count; i++)
            {
                valor_item_3 = Convert.ToString(ds2.CG_REF_CODES.Rows[i]["RV_MEANING"]);
                repositoryItemComboBox1.Items.Add(valor_item_3);

            }

        }

        private void carga_combo_tipos_salidas()
        {
            string query_tipo_m;
            OracleConnection orc2 = new OracleConnection(CApp.bll.ConnectionString);
            query_tipo_m = "SELECT RV_MEANING FROM CG_REF_CODES WHERE RV_DOMAIN = 'TIPOS_MARCAS_SALIDAS'";
            OracleDataAdapter da2 = new OracleDataAdapter(query_tipo_m, CApp.bll.ConnectionString);
            Marcas___Tecapro.dsMarcas_pos ds2 = new dsMarcas_pos();

            da2.Fill(ds2, "CG_REF_CODES");

            repositoryItemComboBox2.Items.Clear();

            string valor_item_3;


            for (int i = 0; i < ds2.CG_REF_CODES.Rows.Count; i++)
            {
                valor_item_3 = Convert.ToString(ds2.CG_REF_CODES.Rows[i]["RV_MEANING"]);
                repositoryItemComboBox2.Items.Add(valor_item_3);

            }


        }

        private void finder_by_user_params(string usuario, string fecha)
        {
            #region Conexion + Fill de Datos
            
            string conexion;
            string orc_query;

            conexion = CApp.bll.ConnectionString;
            OracleConnection orc = new OracleConnection(conexion);

            orc_query = "SELECT " + " " +
                                "NOMBRES, " + " " +
                                "USUARIO, " + " " +
                                "EMAIL, " + " " +
                                "ENTRADA, " + " " +
                                "ENTRADA_JEFE, " + " " +
                                "TIPO_ENTRADA, " + " " +
                                "SALIDA, " + " " +
                                "SALIDA_JEFE, " + " " +
                                "TIPO_SALIDA, " + " " +
                                "MARCA_JEFE, " + " " +
                                "CREADO_EL, " + " " +
                                "CONSECUTIVO, " + " " +
                                "OBSERVACIONES "+" "+
                            "FROM " + " " +
                                "RH_MARCAS_V " + " " +
                            "WHERE " + " " +
                            "NVL(TRUNC(ENTRADA),TRUNC(ENTRADA_JEFE)) = TRUNC (to_date('" + fecha + "', 'dd/mm/yyyy hh:mi:ss a.m.'))AND EMAIL ='" + usuario + "' AND ANULADO_EL IS NULL";

            OracleDataAdapter da = new OracleDataAdapter(orc_query, orc);
            System.Data.DataSet ds = new System.Data.DataSet();
            da.Fill(ds, "RH_MARCAS_V");

            gridControl2.DataSource = ds;
            gridControl2.DataMember = "RH_MARCAS_V";
            #endregion

        }

        private void simpleButton11_Click_1(object sender, EventArgs e)
        {
            if (GUI.CApp.ValidateAccess("RutaMarcas", "Ruta del Archivo de Marcas"))
            {//Consulta la ubicación del Archivo del Parametro en la BD
                file_location = Convert.ToString(SelectTextFile(""));

                if (file_location != null)
                {
                    textEdit1.Text = file_location;
                    //Actualiza el parámetro de la ubicación del Archivo en la BD
                    Utils.QueryParams qp = new LLNet.Utils.QueryParams();
                    qp.AddReplaceParam("pRuta", file_location);
                    // Llama el procedimiento que almacena los datos de la recarga en la BD
                    GUI.CApp.bll.ExecuteSql("UPDATE_RUT_ARCH_MARCAS", qp, "PDV");

                    simpleButton12.Enabled = true;
                    simpleButton11.Enabled = true;
                }
                else
                {//Consulta la ubicación del Archivo del Parametro en la BD
                    ParametroRuta();
                }
            }
            else
            {//Consulta la ubicación del Archivo del Parametro en la BD
                ParametroRuta();                     
            }
        }

        /// <summary>Consulta la ubicación del Archivo desde el Parametro en la BD
        /// </summary>
        private void ParametroRuta()
        {//DLQG
            Utils.QueryParams qp = new LLNet.Utils.QueryParams();
            //CONSULTA LA RUTA DE LA BD           
            file_location = Convert.ToString(GUI.CApp.bll.GetSingleValue("RUT_ARCH_MARCAS", "PDV"));
            if (file_location != null)
            {
                textEdit1.Text = file_location;
                simpleButton12.Enabled = true;
                simpleButton11.Enabled = true; //False
            }
        }

        private string SelectTextFile(string initialDirectory)
        {
           

            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter =
               "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            dialog.InitialDirectory = initialDirectory;
            dialog.Title = "Seleccione el Archivo de Marcas Correspondiente";
            return (dialog.ShowDialog() == DialogResult.OK)
            ? dialog.FileName : null;
          
      
        }

        private void simpleButton12_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            string line;

            simpleButton12.Enabled = false;
            simpleButton11.Enabled = true;
            //textEdit1.Text = "";

            CApp.bll.ExecuteSql("DELETE_TMP_TABLE", "PDV");
            try
            {
                StreamReader sr = new StreamReader(file_location);
                line = sr.ReadLine();

                while (line != null)
                {

                    QueryParams qpOF12 = new QueryParams();
                    qpOF12.AddReplaceParam("Param1", line);
                    CApp.bll.ExecuteSql("INSERT_CARGA_TMP", qpOF12, "PDV");

                    line = sr.ReadLine();
                    
                }

                sr.Close();

            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message);
               
            }

            finally
            {
                CApp.bll.ExecuteSql("PL_RH_MARCAS", "PDV");
                GUI.CApp.ShowMessage("Marcas Cargadas con Èxito", "Carga - Marcas - Sistema", true);
                file_location = null;
                simpleButton11.Enabled = true;
                //textEdit1.Text = "";
                simpleButton12.Enabled = false;


                #region Cargas - Grid - Docto Cargado
                        string conexion;
                        string orc_query;

                        conexion = CApp.bll.ConnectionString;
                        OracleConnection orc = new OracleConnection(conexion);

                        orc_query = "SELECT " + " " +
                                "NOMBRES, " + " " +
                                "USUARIO, " + " " +
                                "EMAIL, " + " " +
                                "ENTRADA, " + " " +
                                "ENTRADA_JEFE, " + " " +
                                "TIPO_ENTRADA, " + " " +
                                "SALIDA, " + " " +
                                "SALIDA_JEFE, " + " " +
                                "TIPO_SALIDA, " + " " +
                                "MARCA_JEFE, " + " " +
                                "CREADO_EL, " + " " +
                                "OBSERVACIONES "+" "+
                            "FROM " + " " +
                                "RH_MARCAS_V " + " " +
                            "WHERE " + " " +
                                "TRUNC(CREADO_EL) = TRUNC(SYSDATE)";

                        OracleDataAdapter da = new OracleDataAdapter(orc_query, orc);
                        System.Data.DataSet ds = new System.Data.DataSet();
                        da.Fill(ds, "RH_MARCAS_V");

                        gridControl3.DataSource = ds;
                        gridControl3.DataMember = "RH_MARCAS_V";
                        #endregion

                                     
            }
            Cursor.Current = Cursors.Default; 
        }

        private void simpleButton13_Click(object sender, EventArgs e)
        {
            xtraTabControl1.SelectedTabPage = xtraTabPage1;

            gridControl3.DataSource = null;
            gridControl3.DataMember = null;

            /*/Back to Original State - Loading Tab /*/
            simpleButton11.Enabled = true;
            simpleButton12.Enabled = false;
            textEdit1.Text = "";
            /*/FIN/*/
        }

        private void simpleButton14_Click(object sender, EventArgs e)
        {
            int marcas_existentes=0;
            
            /*
            QueryParams qpOF1117 = new QueryParams();
            qpOF1117.AddReplaceParam("Param1", dateEdit1.Text);
            CApp.bll.ExecuteSql("VALIDATE_MODIFY_M", qpOF1117, "PDV");
            */

            if (GUI.CApp.ShowMessage("Va a Exportar todas las Marcas Pendientes del dia " + Convert.ToDateTime(fecha).ToString("dd/MM/yyyy") + " a un Archivo MDB, desea continuar?", "Exportar Marcas", true, true) == DialogResult.OK)
            {

                CApp.bll.ExecuteSql("INTERFASADAS","PDV");
                marcas_existentes = Convert.ToInt32(CApp.bll.GetSingleValue("INTERFASADAS","PDV"));

                if (marcas_existentes > 0)
                {
                    Cursor.Current = Cursors.WaitCursor;

                    string conexion = CApp.bll.ConnectionString;
                    string conexion_2 = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "C:/ekono/POS Retail 8.0.0/Marcas/hov_tecapro.mdb" + "");

                    string query = "SELECT * FROM EKONO_CONSOLIDADO.V_EXPORTACION_TEC WHERE F_MARCA = '" + Convert.ToDateTime(fecha).ToString("dd/MM/yyyy") + "'";
                    string query_2 = "SELECT * FROM hovRELOJMARCAS";

                    /*/SE ELIMINAN LOS REGISTROS DE LA TABLA PREVIAMENTE - ACCESS/*/
                    delete_access_exp_file();
                    /*/FIN-EMANUEL CARVAJAL MARIN/*/
 
                    OracleConnection orc = new OracleConnection(conexion);
                    OracleDataAdapter da = new OracleDataAdapter(query, conexion);
                    da.AcceptChangesDuringFill = false;
                    System.Data.DataSet ds = new System.Data.DataSet();
                    da.Fill(ds, "hovRELOJMARCAS");

                    OleDbDataAdapter da_a = new OleDbDataAdapter(query_2, conexion_2);
                    OleDbCommandBuilder cmdb = new OleDbCommandBuilder(da_a);
                    da_a.Update(ds, "hovRELOJMARCAS");

                    /*/SET INTERFASADAS/*/
                    QueryParams qpFEC = new QueryParams();
                    qpFEC.AddReplaceParam("Param1", Convert.ToDateTime(fecha).ToString("dd/MM/yyyy"));
                    CApp.bll.ExecuteSql("SET_INTERFASADAS",qpFEC,"PDV");
                    /*/EMANUEL CARVAJAL MARIN/*/

                    #region Copia el Archivo - Log
                    /*/ENVIAMOS EL ARCHIVO DE CREADOS A UN LOG DE CARPETAS/*/
                    string activeDir = @"C:\ekono\POS Retail 8.0.0\Marcas\Procesadas";
                    string log_file_name;
                    log_file_name = Convert.ToDateTime(fecha).ToString("dd/MM/yyyy") + "Generado " + Convert.ToString(System.DateTime.Now);
                    log_file_name = log_file_name.Replace("/", ".");
                    log_file_name = log_file_name.Replace(":", ".");

                    string newPath = System.IO.Path.Combine(activeDir, log_file_name);
                    System.IO.Directory.CreateDirectory(newPath);

                    string fileName = "hov_tecapro.mdb";
                    string sourcePath = @"C:\ekono\POS Retail 8.0.0\Marcas";
                    string targetPath = @"C:\ekono\POS Retail 8.0.0\Marcas\Procesadas\" + log_file_name;

                    string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
                    string destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(sourceFile, destFile, true);
                    /*/FIN-EMANUEL CARVAJAL MARIN/*/
                    #endregion

                    Cursor.Current = Cursors.Default;

                    GUI.CApp.ShowMessage("Exportaciòn de Marcas, Exitosa!", "Generar Archivo MDB-Tecapro", true);

                    
                }
                else
                {
                    Cursor.Current = Cursors.Default;

                    GUI.CApp.ShowMessage("No Existen Marcas Pendientes, Valide la Carga e Intente Nuevamente", "Error al Generar MDB", true);

                }
            }
            else
            {
                return;
            }

        }

        private void frm_Marcas_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void delete_access_exp_file()
        {
            string del_query = "DELETE * FROM hovRELOJMARCAS";
            string conexion = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "C:/ekono/POS Retail 8.0.0/Marcas/hov_tecapro.mdb" + "");
            OleDbConnection cnn = new OleDbConnection(conexion);

            OleDbCommand dc = new OleDbCommand(del_query, cnn);
            
            dc.Connection.Open();
            dc.ExecuteNonQuery();
            dc.Connection.Close();

        }

        private void checkEdit1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEdit1.Checked == true)
            {
                //comboBoxEdit3.Enabled = false;
                dateEdit2.Enabled = false;
                //comboBoxEdit3.Text = "";
                dateEdit2.Text = "";

            }
            else if(checkEdit1.Checked == false)
            {
                //comboBoxEdit3.Enabled = true;
                dateEdit2.Enabled = true;
                //comboBoxEdit3.Text = "";
                dateEdit2.Text = "";

            }
        }

        private void comboBoxEdit3_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name_cod_empleado;

            QueryParams qpOF12 = new QueryParams();
            qpOF12.AddReplaceParam("Param1", comboBoxEdit3.Text);
            CApp.bll.ExecuteSql("GET_EMP_NAME", qpOF12, "PDV");
            name_cod_empleado = Convert.ToString(CApp.bll.GetSingleValue("GET_EMP_NAME", qpOF12, "PDV"));

            textEdit3.Text = name_cod_empleado;

        }

        private void comboBoxEdit2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name_cod_empleado;

            QueryParams qpOF12 = new QueryParams();
            qpOF12.AddReplaceParam("Param1", comboBoxEdit2.Text);
            CApp.bll.ExecuteSql("GET_EMP_NAME", qpOF12, "PDV");
            name_cod_empleado = Convert.ToString(CApp.bll.GetSingleValue("GET_EMP_NAME", qpOF12, "PDV"));

            textEdit2.Text = name_cod_empleado;

        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            string usuario="";
            string fecha_a="";
            string employee="";
            string consecutivo="";

            foreach (int handle in gridView1.GetSelectedRows())
            {
                usuario = Convert.ToString(gridView1.GetRowCellValue(handle, colUsuario));
                fecha_a = Convert.ToString(gridView1.GetRowCellValue(handle, colEntrada));
                consecutivo = Convert.ToString(gridView1.GetRowCellValue(handle, colConsecutivo));
            }

            QueryParams qpOF12 = new QueryParams();
            qpOF12.AddReplaceParam("Param1", usuario);
            CApp.bll.ExecuteSql("GET_EMP_USER", qpOF12, "PDV");
            employee = Convert.ToString(CApp.bll.GetSingleValue("GET_EMP_USER", qpOF12, "PDV"));

            find_edit_by_d_click(fecha_a, employee, consecutivo);
            xtraTabControl1.SelectedTabPage = xtraTabPage2;

        }

        private void find_edit_by_d_click(string fecha_d, string empleado_d, string consecutivo_d)
        {

            #region Conexion + Fill de Datos
            string orc_query;
            string conexion;
            conexion = CApp.bll.ConnectionString;
            
            OracleConnection orc = new OracleConnection(conexion);

            orc_query = "SELECT " + " " +
                                "NOMBRES, " + " " +
                                "USUARIO, " + " " +
                                "EMAIL, " + " " +
                                "ENTRADA, " + " " +
                                "ENTRADA_JEFE, " + " " +
                                "TIPO_ENTRADA, " + " " +
                                "SALIDA, " + " " +
                                "SALIDA_JEFE, " + " " +
                                "TIPO_SALIDA, " + " " +
                                "MARCA_JEFE, " + " " +
                                "CREADO_EL, " + " " +
                                "OBSERVACIONES "+" "+
                        "FROM " + " " +
                            "RH_MARCAS_V " + " " +
                        "WHERE " + " " +
                        "to_char(NVL(ENTRADA, ENTRADA_JEFE), 'dd/mm/yyyy hh:mi:ss a.m.')='" + fecha_d + "' AND USUARIO='" + empleado_d + "' AND CONSECUTIVO='" + consecutivo_d + "'";

            OracleDataAdapter da = new OracleDataAdapter(orc_query, orc);
            System.Data.DataSet ds = new System.Data.DataSet();
            da.Fill(ds, "RH_MARCAS_V");

            gridControl2.DataSource = ds;
            gridControl2.DataMember = "RH_MARCAS_V";


            #endregion

        }

        private void xtraTabControl1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F2:
                    xtraTabControl1.SelectedTabPage = xtraTabPage1;
                    break;
                case Keys.F3:
                    xtraTabControl1.SelectedTabPage = xtraTabPage2;
                    break;
                case Keys.F4:
                    xtraTabControl1.SelectedTabPage = xtraTabPage4;
                    break;
                case Keys.F5:
                    xtraTabControl1.SelectedTabPage = xtraTabPage5;
                    break;
                case Keys.F6:
                    this.Close();
                    break;
                case Keys.F7:
                    xtraTabControl1.SelectedTabPage = xtraTabPage3;
                    break;
            }
        }

        private void frm_Marcas_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F2:
                    xtraTabControl1.SelectedTabPage = xtraTabPage1;
                    break;
                case Keys.F3:
                    simpleButton1_Click(null, null);
                    break;
                case Keys.F4:
                    simpleButton3_Click(null, null);
                    break;
                case Keys.F5:
                    simpleButton4_Click(null, null);
                    break;
                case Keys.F6:
                    this.Close();
                    break;
                case Keys.F7:
                    simpleButton15_Click(null, null);
                    break;
                case Keys.F8:
                    simpleButton14_Click(null, null);
                    break;
                case Keys.F9:
                    sbtnBitacora_Click(null, null);
                    break;
            }
        }

        private void simpleButton15_Click(object sender, EventArgs e)
        {
            xtraTabControl1.SelectedTabPage = xtraTabPage3;
        }

        private void comboBoxEdit4_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name_cod_empleado;

            QueryParams qpOF12 = new QueryParams();
            qpOF12.AddReplaceParam("Param1", comboBoxEdit4.Text);
            CApp.bll.ExecuteSql("GET_EMP_NAME", qpOF12, "PDV");
            name_cod_empleado = Convert.ToString(CApp.bll.GetSingleValue("GET_EMP_NAME", qpOF12, "PDV"));

            textEdit5.Text = name_cod_empleado;
        }

        private void dateEdit3_EditValueChanged(object sender, EventArgs e)
        {
            get_libres();
            get_libres_main_form_grid(dateEdit3.Text);
        }

        public void get_libres()
        {
            /*/GET BODEGA VALUE INTO PARAMETER/*/
            string parameter_result;
            CApp.bll.ExecuteSql("GET_BODEGA_VALUE", "PDV");
            parameter_result = Convert.ToString(CApp.bll.GetSingleValue("GET_BODEGA_VALUE", "PDV"));
            /*/EMANUEL CARVAJAL MARIN/*/
            
            string conexion;
            string fecha = dateEdit3.Text;
            if (fecha == "")
            {
                fecha = Convert.ToString(System.DateTime.Now);
            }

            string query = "SELECT A.EMAIL AS USUARIO FROM WMS_USUARIOS A, WMS_USUARIOS_X_BODEGA B WHERE NVL(VENDEDOR,'N') <> 'N' " + " " +
            "AND A.USUARIO = B.USUARIO " + " " +
            "AND B.BODEGA ="+parameter_result+" " +
            "AND A.EMAIL IS NOT NULL "+" "+
            "AND A.USUARIO NOT IN  ( " + " " +
            "SELECT USUARIO FROM RH_MARCAS WHERE USUARIO = A.USUARIO AND TRUNC(NVL(RH_MARCAS.ENTRADA,RH_MARCAS.ENTRADA_JEFE)) = TRUNC (to_date('" +fecha+ "', 'dd/mm/yyyy hh:mi:ss a.m.')) " + " " +
            ")";

            conexion = CApp.bll.ConnectionString;
            OracleConnection orc = new OracleConnection(conexion);

            OracleDataAdapter da = new OracleDataAdapter(query, orc);
            System.Data.DataSet ds = new System.Data.DataSet();
            da.Fill(ds, "RH_MARCAS");

            gridControl5.DataSource = ds;
            gridControl5.DataMember = "RH_MARCAS";

        }

        private void simpleButton17_Click(object sender, EventArgs e)
        {
            /*/CLEAN CONTROLS/*/
            dateEdit3.Text = "";
            comboBoxEdit4.Text = "";
            textEdit5.Text = "";
            /*/EMANUEL C./*/

            //Move to the Main Tab//
            xtraTabControl1.SelectedTabPage = xtraTabPage1;
        }

        private void simpleButton16_Click(object sender, EventArgs e)
        {
            if (dateEdit3.Text == "")
            {
                GUI.CApp.ShowMessage("Imposible Registrar el dìa Libre, debe de seleccionar una fecha a registrar antes de continuar", "Error en Registro", true);
                return;
            }
            else if (comboBoxEdit4.Text == "")
            {
                GUI.CApp.ShowMessage("Imposible Registrar el dìa Libre, debe de seleccionar un Còdigo de Empleado a registrar antes de continuar", "Error en Registro", true);
                return;
            }

            if (GUI.CApp.ShowMessage("Desea registrar a el Usuario "+textEdit5.Text+"("+comboBoxEdit4.Text+") como Libre en la fecha seleccionada?", "Asistente - Libres", true, true) == DialogResult.OK)
            {
                string entrada;
                string salida;

                entrada = dateEdit3.Text + " 08:00:00 a.m.";
                salida = dateEdit3.Text + " 05:00:00 p.m.";

                QueryParams qpOF17 = new QueryParams();
                qpOF17.AddReplaceParam("Param1", comboBoxEdit4.Text);
                qpOF17.AddReplaceParam("Param2", entrada);
                qpOF17.AddReplaceParam("Param3", salida);
                qpOF17.AddReplaceParam("Param4", user_validate.user_log_into_sys);
                qpOF17.AddReplaceParam("Param5", entrada);
                qpOF17.AddReplaceParam("Param6", salida);
                CApp.bll.ExecuteSql("INSERT_DIA_LIBRE_PER_USER", qpOF17, "PDV");

              

                /*/RE-FILL GRID-PENDINGS/*/
                get_libres();
                /*/EMANUEL CARVAJAL MARIN/*/
                get_libres_main_form_grid(dateEdit3.Text);
            }
            else
            {
                return;
            }


        }

        public void get_libres_main_form_grid(string fecha)
        {
            string conexion;
            string query;
            
            query = "SELECT "+" "+ 
                        "EMAIL ,"+" "+ 
                        "TIPO_USUARIO ,"+" "+ 
                        "NOMBRES ,"+" "+ 
                        "ENTRADA ,"+" "+ 
                        "ENTRADA_JEFE ,"+" "+ 
                        "TIPO_ENTRADA ,"+" "+
                        "SALIDA ,"+" "+ 
                        "SALIDA_JEFE ,"+" "+ 
                        "TIPO_SALIDA ,"+" "+ 
                        "(SELECT RV_MEANING FROM CG_REF_CODES WHERE RV_LOW_VALUE = '03' AND RV_DOMAIN = 'TIPO_MARCA' ) AS TIPO_MARCA " + " " +
                    "FROM RH_MARCAS_V " + " " +
                    "WHERE TIPO_MARCA = '03' AND " + " " +
                    "TRUNC(NVL(ENTRADA,ENTRADA_JEFE)) = TRUNC (to_date('"+fecha+"', 'dd/mm/yyyy hh:mi:ss a.m.'))";
            conexion = CApp.bll.ConnectionString;

            OracleConnection orc = new OracleConnection(conexion);

            OracleDataAdapter da = new OracleDataAdapter(query, orc);
            System.Data.DataSet ds = new System.Data.DataSet();
            da.Fill(ds, "RH_MARCAS_V");

            gridControl4.DataSource = ds;
            gridControl4.DataMember = "RH_MARCAS_V";
        }

        private void gridControl5_Click(object sender, EventArgs e)
        {
            string c_empleado="";
            string result_emp;

            foreach (int handle in gridView6.GetSelectedRows())
            {
                c_empleado = Convert.ToString(gridView6.GetRowCellValue(handle, col_User));
            }

            QueryParams qpOF17 = new QueryParams();
            qpOF17.AddReplaceParam("Param1", c_empleado);
            CApp.bll.ExecuteSql("GET_EMPLOYEE_NAME_ON_H", qpOF17, "PDV");
            result_emp = Convert.ToString(CApp.bll.GetSingleValue("GET_EMPLOYEE_NAME_ON_H", qpOF17, "PDV"));

            textEdit5.Text = result_emp;
            comboBoxEdit4.Text = c_empleado;
        }

        private void xtraTabControl1_Click(object sender, EventArgs e)
        {

        }

        private void simpleButton18_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            if (comboBoxEdit5.Text == "")
            {
                GUI.CApp.ShowMessage("Debe de Seleccionar un Tipo Reporte para Continuar!","Error al Generar Reporte",true);
            }
            else if (dateEdit4.Text == "")
            {
                GUI.CApp.ShowMessage("Debe de Seleccionar Una Fecha Inicial para Continuar!", "Error al Generar Reporte", true); 
            }
            else if (dateEdit5.Text == "")
            {
                GUI.CApp.ShowMessage("Debe de Seleccionar Una Fecha Final Para Continuar!", "Error al Genrar Reporte", true);
            }

            else
            {
                if (comboBoxEdit5.Text == "Control HRS - Tienda")
                {
                    consulta_rep = string.Format("SELECT * FROM V_REPORTE_X_TIENDA WHERE NVL(TRUNC(ENTRADA),TRUNC(ENTRADA_JEFE)) BETWEEN TRUNC(TO_DATE('{0}','DD/MM/YYYY')) AND TRUNC(TO_DATE('{1}','DD/MM/YYYY')) ORDER BY ENTRADA", dateEdit4.Text, dateEdit5.Text);
                    carga_reporte(reporte_selected, consulta_rep, "V_REPORTE_X_TIENDA", 0);
                }
                else if (comboBoxEdit5.Text == "Control HRS - Total Horas")
                {
                    consulta_rep = string.Format("SELECT * FROM V_REPORTE_X_TIENDA WHERE NVL(TRUNC(ENTRADA),TRUNC(ENTRADA_JEFE)) BETWEEN TRUNC(TO_DATE('{0}','DD/MM/YYYY')) AND TRUNC(TO_DATE('{1}','DD/MM/YYYY')) ORDER BY ENTRADA", dateEdit4.Text, dateEdit5.Text);
                    carga_reporte(reporte_selected, consulta_rep, "V_REPORTE_X_TIENDA", 0);
                }
                else if (comboBoxEdit5.Text == "Control HRS - Consolidado")
                {
                    consulta_rep = string.Format("SELECT * FROM EKONO_CONSOLIDADO.V_REPORTE_X_TIENDA_C WHERE NVL(TRUNC(ENTRADA),TRUNC(ENTRADA_JEFE)) BETWEEN TRUNC(TO_DATE('{0}','DD/MM/YYYY')) AND TRUNC(TO_DATE('{1}','DD/MM/YYYY')) ORDER BY NOMBRES, NVL(ENTRADA_JEFE,ENTRADA)", dateEdit4.Text, dateEdit5.Text);
                    carga_reporte(reporte_selected, consulta_rep, "V_REPORTE_X_TIENDA_C", 1);
                }
                else if (comboBoxEdit5.Text == "Control HRS - Segmentado")
                {
                    /*/VARIABLES DE ENTORNO/*/
                    string query_builder;
                    string consolidated_query ="";
                    string consolidated_query_param;
                    bool writer_validation;
                    /*/FIN-EMANUEL CARVAJAL MARIN/*/

                    query_builder = "";
                    consolidated_query = "";
                    consolidated_query_param = "";

                    /*/DEFINE LOS PARÀMETROS DE DATOS A TRAER/*/
                    for (int i = 0; i < checkedListBoxControl1.ItemCount; i++)
                    {
                        writer_validation = Convert.ToBoolean(checkedListBoxControl1.GetItemCheckState(i));

                        if (writer_validation == true)
                        {
                            query_builder = Convert.ToString(checkedListBoxControl1.GetItemValue(i));
                            query_builder = "'" + query_builder + "'";
                            consolidated_query = consolidated_query + query_builder + ",";
                        }

                        else
                        {
                            //Do Nothing
                        }
                    }
                    /*/FIN/*/


                    consolidated_query_param = consolidated_query.Remove(consolidated_query.Length-1,1);
                    consulta_rep = String.Format("SELECT * FROM EKONO_CONSOLIDADO.V_REPORTE_X_TIENDA_C WHERE NVL(TRUNC(ENTRADA),TRUNC(ENTRADA_JEFE)) BETWEEN TRUNC(TO_DATE('{0}','DD/MM/YYYY')) AND TRUNC(TO_DATE('{1}','DD/MM/YYYY')) AND NOMBRE_TIENDA IN({2})ORDER BY NOMBRES, NVL(ENTRADA_JEFE,ENTRADA)", dateEdit4.Text, dateEdit5.Text,consolidated_query_param);
                    carga_reporte(reporte_selected, consulta_rep,"V_REPORTE_X_TIENDA_C", 1);
                }
                else if (comboBoxEdit5.Text == "Control HRS - Empleado")
                {
                    //consulta_rep = string.Format("SELECT * FROM EKONO_CONSOLIDADO.V_REPORTE_X_TIENDA_C WHERE NVL(TRUNC(ENTRADA),TRUNC(ENTRADA_JEFE)) BETWEEN TRUNC(TO_DATE('{0}','DD/MM/YYYY')) AND TRUNC(TO_DATE('{1}','DD/MM/YYYY'))AND EMAIL = '{2}' ORDER BY ENTRADA", dateEdit4.Text, dateEdit5.Text, comboBoxEdit6.Text);
                    consulta_rep = string.Format("SELECT * FROM V_REPORTE_X_TIENDA_C@CONSOLIDADO WHERE NVL(TRUNC(ENTRADA),TRUNC(ENTRADA_JEFE)) BETWEEN TRUNC(TO_DATE('{0}','DD/MM/YYYY')) AND TRUNC(TO_DATE('{1}','DD/MM/YYYY'))AND EMAIL = '{2}' ORDER BY ENTRADA", dateEdit4.Text, dateEdit5.Text, comboBoxEdit6.Text);
                    carga_reporte(reporte_selected, consulta_rep, "V_REPORTE_X_TIENDA_C", 1);
                }
            }

            /*/BACK TO CONTROLS SEPECIAL SECURITY STATE /*/
            checkedListBoxControl1.Enabled = false;
            comboBoxEdit6.Enabled = false;
            /*/EMANUEL CARVAJAL MARIN /*/

            Cursor.Current = Cursors.Default;
        }

        private void simpleButton19_Click(object sender, EventArgs e)
        {

            /*/BACK TO CONTROLS SEPECIAL SECURITY STATE /*/
            checkedListBoxControl1.Enabled = false;
            comboBoxEdit6.Enabled = false;
            comboBoxEdit6.Text = "";
            textEdit4.Text="";
            /*/EMANUEL CARVAJAL MARIN /*/

            xtraTabControl1.SelectedTabPage = xtraTabPage1;
        }

        private void carga_reporte(string cr_report, string sql_query, string source, int position)
        {
            string conexion = CApp.bll.ConnectionString;
            OracleConnection cnc;
            cnc = new OracleConnection(conexion);
            cnc.Open();
            OracleDataAdapter odp = new OracleDataAdapter(sql_query, cnc);

            Marcas___Tecapro.Reports_Marcas.ds_reports ds = new LLNet.Tienda.MainForms.Marcas___Tecapro.Reports_Marcas.ds_reports();
            odp.Fill(ds, source);
            ReportDocument objRpt = new ReportDocument();
            objRpt.Load(GUI.CApp.ExeDir + cr_report);

            objRpt.SetDataSource(ds.Tables[position]);
            crystalReportViewer1.ReportSource = objRpt;
            crystalReportViewer1.Refresh();

        }

        private void comboBoxEdit5_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxEdit5.Text == "Control HRS - Tienda")
            {
                reporte_selected = "rep_horas.rpt";
                checkedListBoxControl1.Enabled = false;
                comboBoxEdit6.Enabled = false;
                comboBoxEdit6.Text = "";
                textEdit4.Text = "";
            }
            if (comboBoxEdit5.Text == "Control HRS - Total Horas")
            {
                reporte_selected = "rep_horas.rpt";
                checkedListBoxControl1.Enabled = false;
                comboBoxEdit6.Enabled = false;
                comboBoxEdit6.Text = "";
                textEdit4.Text = "";
            }
            else if (comboBoxEdit5.Text == "Control HRS - Consolidado")
            {
                reporte_selected = "rep_cons_hrs.rpt";
                checkedListBoxControl1.Enabled = false;
                comboBoxEdit6.Enabled = false;
                comboBoxEdit6.Text = "";
                textEdit4.Text = "";
            }
            else if (comboBoxEdit5.Text == "Control HRS - Segmentado")
            {
                reporte_selected = "rep_cons_hrs.rpt";
                checkedListBoxControl1.Enabled = true;
                comboBoxEdit6.Enabled = false;
                comboBoxEdit6.Text = "";
                textEdit4.Text = "";
            }
            else if (comboBoxEdit5.Text == "Control HRS - Empleado")
            {
                reporte_selected = "rep_cons_hrs.rpt";
                comboBoxEdit6.Enabled = true;
                checkedListBoxControl1.Enabled = false;
                comboBoxEdit6.Text = "";
                textEdit4.Text = "";
            }

        }

        private void comboBoxEdit6_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name_cod_empleado;

            QueryParams qpOF12 = new QueryParams();
            qpOF12.AddReplaceParam("Param1", comboBoxEdit6.Text);
            CApp.bll.ExecuteSql("GET_EMP_NAME", qpOF12, "PDV");
            name_cod_empleado = Convert.ToString(CApp.bll.GetSingleValue("GET_EMP_NAME", qpOF12, "PDV"));

            textEdit4.Text = name_cod_empleado;
        }

        private void checkedListBoxControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void simpleButton14_Click_1(object sender, EventArgs e)
        {
            simpleButton14_Click(null, null);
        }

        private void groupControl7_Paint(object sender, PaintEventArgs e)
        {

        }

        private void sbtnBitacora_Click(object sender, EventArgs e)
        {
            if (GUI.CApp.ValidateAccess("MarcasBitacora", "Bitacora de marcas"))
            {
                FrmBitacora bitacora = new FrmBitacora();//Formulario Marcas - Bitacora
                bitacora.ShowDialog();
            }
        }

    }
}