public DataSet GetAll_paginacion(string empresaid, tb_cm_liquidacab BE)
 {
     using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
     {
         using (SqlCommand cmd = new SqlCommand("gspTbCmLiquidacab_SEARCH_paginacion", 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("@tipdoc", SqlDbType.Char, 2).Value = BE.tipdoc;
                 cmd.Parameters.Add("@serdoc", SqlDbType.Char, 4).Value = BE.serdoc;
                 cmd.Parameters.Add("@numdoc", SqlDbType.Char, 10).Value = BE.numdoc;
                 cmd.Parameters.Add("@status", SqlDbType.Char, 1).Value = BE.status;
                 cmd.Parameters.Add("@posicion", SqlDbType.VarChar, 10).Value = BE.posicion;
             }
             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_cm_liquidacab BE)
 {
     using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
     {
         using (SqlCommand cmd = new SqlCommand("gspTbCmequivalencia_DELETE", cnx))
         {
             {
                 cmd.CommandType = CommandType.StoredProcedure;
                 //cmd.Parameters.Add("@Equiv_id", SqlDbType.Char, 2).Value = BE.Equiv_id;
             }
             try
             {
                 cnx.Open();
                 if (cmd.ExecuteNonQuery() > 0)
                 {
                     return true;
                 }
                 else
                 {
                     return false;
                 }
             }
             catch (Exception ex)
             {
                 throw new Exception(ex.Message);
             }
         }
     }
 }
        private void Insert()
        {
            try
            {
                var BE = new tb_cm_liquidacab();
                var BL = new tb_cm_liquidacabBL();

                BE.tipdoc = tipdoc.Text.Trim();
                BE.serdoc = moduloiddes.SelectedValue.ToString();
                BE.numdoc = serdoc.Text.Trim() + numdoc.Text.Trim();
                BE.moduloid = moduloiddes.SelectedValue.ToString();
                BE.local = localdes.SelectedValue.ToString();
                BE.tipref = tipguia.Text.Trim();
                BE.serref = serguia.Text.Trim();
                BE.numref = numguia.Text.Trim();
                BE.fecre = Convert.ToDateTime(fechdoc.Text);
                BE.cif = Convert.ToDecimal(cif.Text.Trim());
                BE.dua = Convert.ToDecimal(dua.Text.Trim());
                BE.gas_impo = Convert.ToDecimal(gas_impo.Text.Trim());
                BE.gas_financ = Convert.ToDecimal(gas_financ.Text.Trim());
                BE.tot_bimp = Convert.ToDecimal(tot_bimp.Text.Trim());
                BE.factorincre = Convert.ToDecimal(factorincre.Text.Trim());
                BE.usuar = VariablesPublicas.Usuar.Trim();
                BE.status = "0";

                if (TablaDetalleImportacion2 != null)
                {
                    TablaDetalleImportacion2.Clear();
                }

                for (var i = 0; i < dgb_importacion1.RowCount; i++)
                {
                    row = TablaDetalleImportacion2.NewRow();
                    row["tipdoc"] = tipdoc.Text.Trim();
                    row["serdoc"] = moduloiddes.SelectedValue.ToString();
                    row["numdoc"] = serdoc.Text.Trim() + numdoc.Text.Trim();

                    row["conceptoid"] = dgb_importacion1.GetRowCellValue(i, "conceptoid").ToString().Trim();
                    row["ctacte"] = dgb_importacion1.GetRowCellValue(i, "ctacte").ToString().Trim();
                    row["tipbou"] = dgb_importacion1.GetRowCellValue(i, "tipbou").ToString().Trim();
                    row["serbou"] = dgb_importacion1.GetRowCellValue(i, "serbou").ToString().Trim();
                    row["numbou"] = dgb_importacion1.GetRowCellValue(i, "numbou").ToString().Trim();
                    var fecha = dgb_importacion1.GetRowCellValue(i, "fechbou").ToString().Trim();
                    if (fecha.ToString().Trim().Length > 0 && fecha.ToString().Trim() != "-")
                    {
                        row["fechbou"] = dgb_importacion1.GetRowCellValue(i, "fechbou").ToString().Trim();
                    }
                    row["bimp_sunat2"] = dgb_importacion1.GetRowCellValue(i, "bimp_sunat2").ToString().Trim();
                    row["tipcamb"] = dgb_importacion1.GetRowCellValue(i, "tipcamb").ToString().Trim();
                    row["bimp_sunat1"] = dgb_importacion1.GetRowCellValue(i, "bimp_sunat1").ToString().Trim();
                    row["pven"] = dgb_importacion1.GetRowCellValue(i, "pven").ToString().Trim();
                    row["status"] = "0".ToString().Trim();
                    row["usuar"] = VariablesPublicas.Usuar.Trim();
                    TablaDetalleImportacion2.Rows.Add(row);
                }
                if (BL.Insert(EmpresaID, BE, TablaDetalleImportacion2))
                {
                    MessageBox.Show("Datos Grabados Correctamente !!!", "InformaciĆ³n", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    form_bloqueado(false);
                    tipdoc.Enabled = false;

                    btn_nuevo.Enabled = true;
                    btn_imprimir.Enabled = true;

                    btn_primero.Enabled = true;
                    btn_anterior.Enabled = true;
                    btn_siguiente.Enabled = true;
                    btn_ultimo.Enabled = true;

                    btn_salir.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void form_cargar_datos(String posicion)
        {
            try
            {
                limpiar_documento();
                var BL = new tb_cm_liquidacabBL();
                var BE = new tb_cm_liquidacab();
                var dt = new DataTable();

                BE.moduloid = moduloiddes.SelectedValue.ToString();
                BE.local = local.Trim();
                BE.tipdoc = tipdoc.Text.Trim();
                BE.serdoc = moduloiddes.SelectedValue.ToString();

                if (serdoc.Text.Trim().Length == 0)
                {
                    if (posicion.Trim().Length > 0)
                    {
                        MessageBox.Show("Seleccionar el Tipo de Documento !!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    return;
                }

                var n_doc = string.Empty;
                if (numdoc.Text.Trim().Length > 0)
                {
                    n_doc = numdoc.Text.Trim().PadLeft(6, '0');
                }

                BE.numdoc = serdoc.Text.ToString().Trim() + n_doc.ToString().Trim();
                BE.posicion = posicion.Trim();

                dt = BL.GetAll_paginacion(EmpresaID, BE).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    activeDoc = false;
                    limpiar_documento();
                    form_bloqueado(false);
                    ssModo = "EDIT";

                    var BL2 = new tb_60movimientoscabBL();
                    var BE2 = new tb_60movimientoscab();
                    var dt2 = new DataTable();
                    var n = dt.Rows[0]["numdoc"].ToString().Trim();

                    serdoc.Text = Equivalencias.Left(n, 4);
                    numdoc.Text = Equivalencias.Right(n, 6);

                    tipguia.Text = dt.Rows[0]["tipref"].ToString().Trim();
                    serguia.Text = dt.Rows[0]["serref"].ToString().Trim();
                    numguia.Text = dt.Rows[0]["numref"].ToString().Trim();

                    cif.Text = dt.Rows[0]["cif"].ToString().Trim();
                    dua.Text = dt.Rows[0]["dua"].ToString().Trim();
                    gas_impo.Text = dt.Rows[0]["gas_impo"].ToString().Trim();
                    gas_financ.Text = dt.Rows[0]["gas_financ"].ToString().Trim();
                    tot_bimp.Text = dt.Rows[0]["tot_bimp"].ToString().Trim();
                    factorincre.Text = dt.Rows[0]["factorincre"].ToString().Trim();

                    moduloiddes.SelectedValue = dt.Rows[0]["moduloid"].ToString().Trim();
                    data_cbo_localdes(dominioiddes, moduloiddes.SelectedValue.ToString());
                    localdes.SelectedValue = dt.Rows[0]["local"].ToString().Trim();

                    BE2.moduloid = moduloiddes.SelectedValue.ToString();
                    BE2.local = localdes.SelectedValue.ToString();
                    BE2.tipodoc = tipguia.Text.ToString();
                    BE2.serdoc = serguia.Text.ToString();
                    BE2.numdoc = numguia.Text;

                    dt2 = BL2.GetAll2(EmpresaID, BE2).Tables[0];

                    if (dt2.Rows.Count > 0)
                    {
                        tipfac.Text = dt2.Rows[0]["tipfac"].ToString();
                        serfac.Text = dt2.Rows[0]["serfac"].ToString();
                        numfac.Text = dt2.Rows[0]["numfac"].ToString();

                        tipref.Text = dt2.Rows[0]["tipref"].ToString();
                        numdococ1.Text = Equivalencias.Left(dt2.Rows[0]["numref"].ToString(), 4);
                        numdococ.Text = Equivalencias.Right(dt2.Rows[0]["numref"].ToString(), 6);
                        ctacte.Text = dt2.Rows[0]["ctacte"].ToString();
                        ctactename.Text = dt2.Rows[0]["ctactename"].ToString();
                    }

                    _CargarDetalleImportacion();

                    if (dt.Rows[0]["status"].ToString().Trim() != "9")
                    {
                        btn_nuevo.Enabled = true;
                        btn_editar.Enabled = true;
                        btn_eliminar.Enabled = true;
                        btn_imprimir.Enabled = true;
                        btn_primero.Enabled = true;
                        btn_anterior.Enabled = true;
                        btn_siguiente.Enabled = true;
                        btn_ultimo.Enabled = true;
                        btn_salir.Enabled = true;
                        pictureEdit1.Visible = false;
                    }
                    else
                    {
                        ssModo = "ANULADO";
                        if (TablaDetalleImportacion2.Rows.Count > 0)
                        {
                            TablaDetalleImportacion2.Clear();
                        }
                        dgb_importacion2.DataSource = TablaDetalleImportacion2;
                        pictureEdit1.Visible = true;
                        btn_editar.Enabled = false;
                        btn_eliminar.Enabled = false;
                        btn_nuevo.Enabled = true;
                        btn_imprimir.Enabled = true;

                        btn_primero.Enabled = true;
                        btn_anterior.Enabled = true;
                        btn_siguiente.Enabled = true;
                        btn_ultimo.Enabled = true;
                        btn_salir.Enabled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public bool Update(string empresaid, tb_cm_liquidacab BE, DataTable DatDetalle)
        {
            using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
            {
                using (SqlCommand cmd = new SqlCommand("gspTbCmLiquida_UPDATE", cnx))
                {

                    cmd.CommandType = CommandType.StoredProcedure;

                    // Parametros de Cabecera
                    cmd.Parameters.Add("@moduloid", SqlDbType.Char, 4).Value = BE.moduloid;
                    cmd.Parameters.Add("@local", SqlDbType.Char, 3).Value = BE.local;
                    cmd.Parameters.Add("@tipdoc", SqlDbType.Char, 2).Value = BE.tipdoc;
                    cmd.Parameters.Add("@serdoc", SqlDbType.Char, 4).Value = BE.serdoc;
                    cmd.Parameters.Add("@numdoc", SqlDbType.Char, 10).Value = BE.numdoc;
                    cmd.Parameters.Add("@tipref", SqlDbType.Char, 2).Value = BE.tipref;
                    cmd.Parameters.Add("@serref", SqlDbType.Char, 4).Value = BE.serref;
                    cmd.Parameters.Add("@numref", SqlDbType.Char, 10).Value = BE.numref;
                    cmd.Parameters.Add("@fecre", SqlDbType.DateTime).Value = BE.fecre;
                    cmd.Parameters.Add("@tot_bimp", SqlDbType.Decimal).Value = BE.tot_bimp;
                    cmd.Parameters.Add("@cif", SqlDbType.Decimal).Value = BE.cif;
                    cmd.Parameters.Add("@dua", SqlDbType.Decimal).Value = BE.dua;
                    cmd.Parameters.Add("@gas_impo", SqlDbType.Decimal).Value = BE.gas_impo;
                    cmd.Parameters.Add("@gas_financ", SqlDbType.Decimal).Value = BE.gas_financ;
                    cmd.Parameters.Add("@factorincre", SqlDbType.Decimal).Value = BE.factorincre;
                    cmd.Parameters.Add("@usuar", SqlDbType.Char, 15).Value = BE.usuar;
                    cmd.Parameters.Add("@status", SqlDbType.Char, 1).Value = BE.status;

                    // Detalle
                    cmd.Parameters.AddWithValue("@DetalleImp", DatDetalle);

                    try
                    {
                        cnx.Open();
                        if (cmd.ExecuteNonQuery() > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
        }
        public List<tb_cm_liquidacab> Get_xcodigo(string empresaid, tb_cm_liquidacab BE)
        {
            using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
            {
                using (SqlCommand cmd = new SqlCommand("gspTbCmequivalencia_SEARCH", cnx))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    //cmd.Parameters.AddWithValue("@Equiv_id", BE.Equiv_id);
                    List<tb_cm_liquidacab> list = new List<tb_cm_liquidacab>();
                    try
                    {
                        cnx.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader())
                        {
                            if (dr.Read())
                            {
                                tb_cm_liquidacab be = new tb_cm_liquidacab();

                                //be.Equiv_id = Convert.ToInt32(dr["Equiv_id"]);
                                //be.Equiv_name = Convert.ToString(dr["Equiv_name"]);
                                //be.Equivalencia = Convert.ToDecimal(dr["equivalencia"]);
                                //be.Unmed1 = Convert.ToString(dr["unmed1"]);
                                //be.Unmed2 = Convert.ToString(dr["unmed2"]);

                                //be.descripcion1 = Convert.ToString(dr["descripcion1"]);
                                //be.descripcion2 = Convert.ToString(dr["descripcion2"]);

                                list.Add(be);
                            }
                            return list;
                        }

                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
        }
        public List<tb_cm_liquidacab> Get_equivalencia(string empresaid, tb_cm_liquidacab BE)
        {
            using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
               {
               using (SqlCommand cmd = new SqlCommand("gspTbCmequivalencia_SEARCH", cnx))
               {

                   //cmd.CommandType = CommandType.StoredProcedure;
                   //cmd.Parameters.Add("@Equiv_id", SqlDbType.Decimal,10).Value = BE.Equiv_id;

                   List<tb_cm_liquidacab> list = new List<tb_cm_liquidacab>().ToList();
                   try
                   {
                       cnx.Open();
                       using (SqlDataReader dr = cmd.ExecuteReader())
                       {
                           if (dr.Read())
                           {
                               //tb_cm_liquidacab be  = new tb_cm_liquidacab();
                               //be.Equivalencia = Convert.ToInt32(dr["equivalencia"]);
                               //Int32 a = Convert.ToInt32(dr["equivalencia"]);
                               //list.Add(be);
                                // return BE;
                           }

                           return list;

                       }

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