public DataSet GetAll(string empresaid, tb_me_listapreciosdet BE)
 {
     using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
     {
         using (SqlCommand cmd = new SqlCommand("gspTbMeListapreciosdet_SEARCH", cnx))
         {
             DataSet ds = new DataSet();
             {
                 cmd.CommandType = CommandType.StoredProcedure;
                 cmd.Parameters.Add("@listaprecid", SqlDbType.Int).Value = BE.listaprecid;
                 //cmd.Parameters.Add("@filtro", SqlDbType.Char, 1).Value = BE.filtro;
             }
             try
             {
                 cnx.Open();
                 using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                 {
                     da.Fill(ds);
                 }
                 return ds;
             }
             catch (Exception ex)
             {
                 throw new Exception(ex.Message);
             }
         }
     }
 }
 public bool Delete(string empresaid, tb_me_listapreciosdet BE)
 {
     using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
     {
         using (SqlCommand cmd = new SqlCommand("gspTbMeListapreciosdet_DELETE", cnx))
         {
             {
                 cmd.CommandType = CommandType.StoredProcedure;
                 cmd.Parameters.Add("@listaprecid", SqlDbType.Int).Value = BE.listaprecid;
                 cmd.Parameters.Add("@codigo", SqlDbType.VarChar, 13).Value = BE.codigo;
             }
             try
             {
                 cnx.Open();
                 if (cmd.ExecuteNonQuery() > 0)
                 {
                     return true;
                 }
                 else
                 {
                     return false;
                 }
             }
             catch (Exception ex)
             {
                 throw new Exception(ex.Message);
             }
         }
     }
 }
        public bool Update(string empresaid, tb_me_listapreciosdet BE)
        {
            using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
            {
                using (SqlCommand cmd = new SqlCommand("gspTbMeListapreciosdet_UPDATE", cnx))
                {
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@listaprecid", SqlDbType.Int).Value = BE.listaprecid;
                        cmd.Parameters.Add("@codigo", SqlDbType.VarChar, 13).Value = BE.codigo;
                        cmd.Parameters.Add("@precunit1", SqlDbType.Decimal).Value = BE.precunit1;
                        cmd.Parameters.Add("@precunit2", SqlDbType.Decimal).Value = BE.precunit2;
                        cmd.Parameters.Add("@usuar", SqlDbType.Char, 15).Value = BE.usuar;

                        //cmd.Parameters.Add("@XML", SqlDbType.Xml).Value = BE.GetItemXML();
                    }
                    try
                    {
                        cnx.Open();
                        if (cmd.ExecuteNonQuery() > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
        }
        public bool InsertDet(string empresaid, tb_me_listapreciosdet BE)
        {
            using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
            {
                using (SqlCommand cmd = new SqlCommand("gspTbMeListapreciosdet_INSERT_xml", cnx))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@listaprecid", SqlDbType.Int).Value = BE.listaprecid;
                    cmd.Parameters.Add("@XML", SqlDbType.Xml).Value = BE.GetItemXML();

                    try
                    {
                        cnx.Open();
                        if (cmd.ExecuteNonQuery() > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
        }
        public DataSet GetVigentes(string empresaid, tb_me_listapreciosdet BE)
        {
            using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
            {
                using (SqlCommand cmd = new SqlCommand("gspTbMeListaPreciosdet_SEARCH_Vigente", cnx))
                {
                    DataSet ds = new DataSet();
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        //cmd.Parameters.Add("@moduloid", SqlDbType.Char, 4).Value = BE.moduloid;
                        //cmd.Parameters.Add("@local", SqlDbType.Char,3).Value = BE.local;
                        //cmd.Parameters.Add("@fecha", SqlDbType.DateTime).Value = BE.fecha;

                    }
                    try
                    {
                        cnx.Open();
                        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                        {
                            da.Fill(ds);
                        }
                        return ds;
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
        }
 public bool InsertDet(string empresaid, tb_me_listapreciosdet BE)
 {
     return tablaDA.InsertDet(empresaid, BE);
 }
 public bool Update(string empresaid, tb_me_listapreciosdet BE)
 {
     return tablaDA.Update(empresaid, BE);
 }
 //public DataSet GetVigentes(string empresaid, tb_me_listapreciosdet BE)
 //{
 //    return tablaDA.GetVigentes(empresaid, BE);
 //}
 //public DataSet GetAll_datosdetalle(string empresaid, tb_me_listapreciosdet BE)
 //{
 //    return tablaDA.GetAll_datosdetalle(empresaid, BE);
 //}
 //public DataSet FiltroProducto(string empresaid, tb_me_listapreciosdet BE)
 //{
 //    return tablaDA.FiltroProducto(empresaid, BE);
 //}
 public DataSet GetAll_paginacion(string empresaid, tb_me_listapreciosdet BE)
 {
     return tablaDA.GetAll_paginacion(empresaid, BE);
 }
        private void Importar_Excel()
        {
            var CuadroDialogo = new OpenFileDialog();
            CuadroDialogo.DefaultExt = "xls";
            CuadroDialogo.Filter = "xls file(*.xls)|*.xlsx";
            CuadroDialogo.AddExtension = true;
            CuadroDialogo.RestoreDirectory = true;
            CuadroDialogo.Title = "Seleccionar Archivo";

            if (CuadroDialogo.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    var excelApp = new Excel.Application();
                    var excelBook = excelApp.Workbooks.Open(CuadroDialogo.FileName, Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    var excelWorksheet = (Excel._Worksheet)excelBook.Worksheets.get_Item(1);
                    excelApp.Visible = false;

                    var x = 2;
                    if (TablaprecioslistDet.Rows.Count > 0)
                    {
                        TablaprecioslistDet.Rows.Clear();
                    }
                    while (excelWorksheet.get_Range("A" + x).Value2 != null)
                    {
                    }

                    var BL = new tb_me_listapreciosdetBL();
                    var BE = new tb_me_listapreciosdet();

                    var Detalle = new tb_me_listapreciosdet.Item();
                    var ListaItems = new List<tb_me_listapreciosdet.Item>();

                    var item = 0;
                    foreach (DataRow fila in TablaprecioslistDet.Rows)
                    {
                        Detalle = new tb_me_listapreciosdet.Item();
                        item++;
                        Detalle.codigo = fila["codigo"].ToString();
                        Detalle.precunit1 = Convert.ToDecimal(fila["precunit1"].ToString().Trim());
                        Detalle.precunit2 = Convert.ToDecimal(fila["precunit2"].ToString().Trim());
                        Detalle.usuar = VariablesPublicas.Usuar;
                        Detalle.feact = Convert.ToDateTime(fila["feact"].ToString().Trim());
                        ListaItems.Add(Detalle);
                    }
                    if (ListaItems.Count == 0)
                    {
                        MessageBox.Show("Documento SIN DETALLE Y/O DETALLE INCORRECTO !!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    BE.ListaItems = ListaItems;

                    if (BL.InsertDet(EmpresaID, BE))
                    {
                        MessageBox.Show("Datos Grabados Correctamente !!!", "Confirmación", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        CargarDetalle();
                        Nuevo2(false);
                        btn_add.Enabled = true;
                    }

                    excelApp.Quit();
                }
                catch (Exception sms)
                {
                    MessageBox.Show(sms.Message);
                }
            }
        }
        private void CargarDetalle()
        {
            var BL = new tb_me_listapreciosdetBL();
            var BE = new tb_me_listapreciosdet();
            TablaprecioslistDet = new DataTable();
            if (listaprecid.Text.Trim().Length > 0)
            {
                BE.listaprecid = Convert.ToInt32(listaprecid.Text.Trim());
            }

            TablaprecioslistDet = BL.GetAll(EmpresaID, BE).Tables[0];
            if (TablaprecioslistDet.Rows.Count > 0)
            {
                dgb_listaPrecios.DataSource = TablaprecioslistDet;
            }
            else
            {
                dgb_listaPrecios.DataSource = TablaprecioslistDet;
            }
        }
        private void btn_save_Click(object sender, EventArgs e)
        {
            if (articid.Text.Length == 0)
            {
                MessageBox.Show("Ingrese Articulo ... !!!");
                return;
            }
            else
            {
                if (precunit1.Text.Length == 0)
                {
                    MessageBox.Show("Ingrese Precio ... !!!");
                    return;
                }
            }
            var BE = new tb_me_listapreciosdet();
            var BL = new tb_me_listapreciosdetBL();

            BE.listaprecid = Convert.ToInt32(listaprecid.Text.Trim());
            BE.codigo = articid.Text.Trim();
            BE.precunit1 = Convert.ToDecimal(precunit1.Text.Trim());
            BE.precunit2 = Convert.ToDecimal(precunit2.Text.Trim());
            BE.usuar = VariablesPublicas.Usuar.ToString().Trim();

            if (BL.Insert(EmpresaID, BE))
            {
                MessageBox.Show("Datos Grabados Correctamente !!!", "Confirmación", MessageBoxButtons.OK, MessageBoxIcon.Information);
                CargarDetalle();
                Nuevo2(false);
                btn_add.Enabled = true;
            }
        }
        private void btn_del_Click(object sender, EventArgs e)
        {
            if ((dgb_listaPrecios.RowCount != null))
            {
                if (listaprecid.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Falta Codigo de Lista de Precios", "Informacion", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    if (articid.Text.Trim().Length == 0)
                    {
                        MessageBox.Show("Falta Codigo de Artículo", "Informacion", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                var BL = new tb_me_listapreciosdetBL();
                var BE = new tb_me_listapreciosdet();
                BE.listaprecid = Convert.ToInt32(listaprecid.Text.Trim());
                BE.codigo = articid.Text.Trim();

                if (BL.Delete(EmpresaID, BE))
                {
                    CargarDetalle();
                    articid.Text = string.Empty;
                    articname.Text = string.Empty;
                    precunit1.Text = string.Empty;
                    precunit2.Text = string.Empty;
                    btn_del.Enabled = false;
                }
            }
        }