//public bool Eliminar(string empresaid, DataTable TablaDatos)
 //{
 //    //return tablaDA.Delete(empresaid, BE);
 //    bool zreturn = tablaDA.Eliminar(empresaid, TablaDatos);
 //    Sql_Error = tablaDA.Sql_Error;
 //    return zreturn;
 //}
 public DataSet GetAll(string empresaid, tb_cm_ordendecompradoc BE)
 {
     DataSet xreturn = null;
     xreturn = tablaDA.GetAll(empresaid, BE);
     Sql_Error = tablaDA.Sql_Error;
     return xreturn;
 }
 public bool Insert(string empresaid, tb_cm_ordendecompradoc BE)
 {
     return tablaDA.Insert(empresaid,BE);
     //bool zreturn = tablaDA.Insert(empresaid, BE);
     //Sql_Error = tablaDA.Sql_Error;
     //return zreturn;
 }
 public bool Delete(string empresaid, tb_cm_ordendecompradoc BE)
 {
     using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
     {
         using (SqlCommand cmd = new SqlCommand("gspTbCmOrdencompradoc_ELIMINAR", cnx))
         {
             {
                 cmd.CommandType = CommandType.StoredProcedure;
                 cmd.Parameters.Add("@item", SqlDbType.Char, 2).Value = BE.items;
                 cmd.Parameters.Add("@moduloid", SqlDbType.Char, 4).Value = BE.moduloid;
                 cmd.Parameters.Add("@local", SqlDbType.Char, 3).Value = BE.local;
                 cmd.Parameters.Add("@moduloiddes", SqlDbType.Char, 4).Value = BE.moduloiddes;
                 cmd.Parameters.Add("@serdoc", SqlDbType.Char, 4).Value = BE.serdoc;
                 cmd.Parameters.Add("@numdoc", SqlDbType.Char, 10).Value = BE.numdoc;
             }
             try
             {
                 cnx.Open();
                 if (cmd.ExecuteNonQuery() > 0)
                 {
                     return true;
                 }
                 else
                 {
                     return false;
                 }
             }
             catch (Exception ex)
             {
                 Sql_Error = ex.Message;
                 throw new Exception(ex.Message);
             }
         }
     }
 }
 //public bool Update(string empresaid, tb_cm_ordendecompra BE)
 //{
 //    //return tablaDA.Update(empresaid, BE);
 //    bool zreturn = tablaDA.Update(empresaid, BE);
 //    Sql_Error = tablaDA.Sql_Error;
 //    return zreturn;
 //}
 public bool Delete(string empresaid, tb_cm_ordendecompradoc BE)
 {
     //return tablaDA.Delete(empresaid, BE);
     bool zreturn = tablaDA.Delete(empresaid, BE);
     Sql_Error = tablaDA.Sql_Error;
     return zreturn;
 }
 public DataSet GetAll_MaxCodigo(string empresaid, tb_cm_ordendecompradoc BE)
 {
     //return tablaDA.GetAll_MaxRubro(empresaid, BE);
     DataSet xreturn = null;
     xreturn = tablaDA.GetAll_MaxCodigo(empresaid, BE);
     Sql_Error = tablaDA.Sql_Error;
     return xreturn;
 }
 public DataSet GetAll_CONSULTAIR(string empresaid, tb_cm_ordendecompradoc BE)
 {
     //return tablaDA.GetAll_CONSULTAIR(empresaid, BE);
       DataSet xreturn = null;
       xreturn = tablaDA.GetAll_CONSULTAIR(empresaid, BE);
       Sql_Error = tablaDA.Sql_Error;
       return xreturn;
 }
        private void Accion(int naccion)
        {
            switch (naccion)
            {
                case 1:
                    u_n_opsel = 1;
                    U_RefrescaControles();
                    Blanquear();
                    var BL = new tb_cm_ordendecompradocBL();
                    var BE = new tb_cm_ordendecompradoc();

                    BE.moduloid = VariablesPublicas.Moduloid;
                    BE.local = VariablesPublicas.Local;
                    BE.moduloiddes = moduloiddes;
                    BE.serdoc = serdoc;
                    BE.numdoc = numdoc;

                    txtcodigo.Text = BL.GetAll_MaxCodigo(VariablesPublicas.EmpresaID.ToString(), BE).Tables[0].Rows[0]["maximo_codigo"].ToString();

                    if (dgvfileatached.CurrentRow != null)
                    {
                        dgvfileatached.CurrentRow.Selected = false;
                    }
                    break;
                case 2:
                    POnedatos();
                    u_n_opsel = 2;
                    U_RefrescaControles();
                    dgvfileatached.CurrentRow.Selected = true;
                    break;
                case 3:
                    xnomcampo = "";
                    if (dgvfileatached.CurrentRow != null)
                    {
                        var BLIR = new tb_cm_ordendecompradocBL();
                        var BEIR = new tb_cm_ordendecompradoc();
                        BEIR.items = dgvfileatached.Rows[dgvfileatached.CurrentRow.Index].Cells["item"].Value.ToString().Trim();
                        tmpcursor = BLIR.GetAll_CONSULTAIR(VariablesPublicas.EmpresaID, BEIR).Tables[0];
                        if (BLIR.Sql_Error.Length > 0)
                        {
                            xnomcampo = BLIR.Sql_Error;
                            Frm_Class.ShowError(BLIR.Sql_Error, this);
                        }
                        else
                        {
                            if (tmpcursor.Rows.Count == 0)
                            {
                            }
                            else
                            {
                                for (lc_cont = 0; lc_cont <= tmpcursor.Rows.Count - 1; lc_cont++)
                                {
                                    xnomcampo = xnomcampo + tmpcursor.Rows[lc_cont]["relacion"] + "\r";
                                    if (lc_cont + 1 == 10)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (xnomcampo.Length == 0)
                    {
                        var message = "Desea Eliminar Registro  " + dgvfileatached.Rows[dgvfileatached.CurrentRow.Index].Cells["item"].Value.ToString().Trim() + "-" + dgvfileatached.Rows[dgvfileatached.CurrentRow.Index].Cells["docglosa"].Value.ToString().Trim() + " ...?";
                        var caption = "Mensaje del Sistema";
                        var buttons = MessageBoxButtons.YesNo;
                        DialogResult result;
                        result = MessageBox.Show(this, message, caption, buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                        if (result == DialogResult.Yes)
                        {
                            var BLE = new tb_cm_ordendecompradocBL();
                            var BEE = new tb_cm_ordendecompradoc();
                            BEE.items = dgvfileatached.Rows[dgvfileatached.CurrentRow.Index].Cells["item"].Value.ToString().Trim();
                            BEE.norden = 1;
                            BEE.ver_blanco = 0;
                            BEE.vista = 1;

                            BEE.moduloid = VariablesPublicas.Moduloid;
                            BEE.local = VariablesPublicas.Local;
                            BEE.moduloiddes = moduloiddes;
                            BEE.numdoc = numdoc;
                            BEE.serdoc = serdoc;

                            tmpcursor = BLE.GetAll(VariablesPublicas.EmpresaID, BEE).Tables[0];

                            if (BLE.Sql_Error.Length == 0)
                            {
                                if (BLE.Delete(VariablesPublicas.EmpresaID, BEE))
                                {
                                    for (lc_cont = 0; lc_cont <= Tabla.Rows.Count - 1; lc_cont++)
                                    {
                                        if (Tabla.Rows[lc_cont]["item"] == dgvfileatached.Rows[dgvfileatached.CurrentRow.Index].Cells["item"].Value)
                                        {
                                            Tabla.Rows[lc_cont].Delete();
                                            Tabla.AcceptChanges();
                                            break;
                                        }
                                    }
                                    dgvfileatached.Refresh();
                                }
                                else
                                {
                                    Frm_Class.ShowError(BLE.Sql_Error, this);
                                }
                            }
                            else
                            {
                                Frm_Class.ShowError(BLE.Sql_Error, this);
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show(xnomcampo, "IMPOSIBLE ELIMINAR DOCUMENTO");
                    }
                    break;
            }
        }
 public bool Insert_Update(string empresaid, tb_cm_ordendecompradoc BE, DataTable Detalle)
 {
     //return tablaDA.Insert(empresaid, BE);
     Sql_Error = tablaDA.Sql_Error;
      bool zreturn = tablaDA.Insert_Update(empresaid, BE, Detalle);
     return zreturn;
 }
 public DataSet GetAll_CONSULTAIR(string empresaid, tb_cm_ordendecompradoc BE)
 {
     using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
     {
         using (SqlCommand cmd = new SqlCommand("gspTbmaattachedfile_IR", cnx))
         {
             DataSet ds = new DataSet();
             {
                 cmd.CommandType = CommandType.StoredProcedure;
                 cmd.Parameters.AddWithValue("@tipocontratoid", string.IsNullOrEmpty(BE.items) ? (object)DBNull.Value : BE.items);
             }
             try
             {
                 cnx.Open();
                 using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                 {
                     da.Fill(ds);
                 }
                 return ds;
             }
             catch (Exception ex)
             {
                 Sql_Error = ex.Message;
                 throw new Exception(ex.Message);
             }
         }
     }
 }
        public DataSet GetAll(string empresaid, tb_cm_ordendecompradoc BE)
        {
            using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
            {
                using (SqlCommand cmd = new SqlCommand("gspTbCmOrdencompradoc_SEARCH", 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("@moduloiddes", SqlDbType.Char, 4).Value = BE.moduloiddes;
                        cmd.Parameters.Add("@numdoc", SqlDbType.Char, 10).Value = BE.numdoc;
                        cmd.Parameters.Add("@serdoc", SqlDbType.Char, 4).Value = BE.serdoc;

                        //cmd.Parameters.Add("@plantillaword", SqlDbType.Image).Value = BE.plantillaword;
                    }
                    try
                    {
                        cnx.Open();
                        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                        {
                            //if (ds.Tables.Count > 0)
                            //{
                                da.Fill(ds);
                            //}
                            //else return null;

                        }
                        return ds;
                    }
                    catch (Exception ex)
                    {
                        Sql_Error = ex.Message;
                        throw new Exception(ex.Message);
                    }
                }
            }
        }
 public bool Update(string empresaid, tb_cm_ordendecompradoc BE)
 {
     using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
     {
         using (SqlCommand cmd = new SqlCommand("gspTb60attachedfile_UPDATE", cnx))
         {
             {
                 cmd.CommandTimeout = 0;
                 cmd.CommandType = CommandType.StoredProcedure;
                 //cmd.Parameters.Add("@tipocontratoid", SqlDbType.Char, 2).Value = BE.tipocontratoid;
                 //cmd.Parameters.Add("@tipocontratoname", SqlDbType.VarChar, 100).Value = BE.tipocontratoname;
                 //cmd.Parameters.Add("@nomplantillaword", SqlDbType.VarChar, 100).Value = BE.nomplantillaword;
                 //cmd.Parameters.Add("@plantillaword", SqlDbType.Image).Value = BE.plantillaword;
             }
             try
             {
                 cnx.Open();
                 if (cmd.ExecuteNonQuery() > 0)
                 {
                     return true;
                 }
                 else
                 {
                     return false;
                 }
             }
             catch (Exception ex)
             {
                 Sql_Error = ex.Message;
                 throw new Exception(ex.Message);
             }
         }
     }
 }
        public bool Insert_Update(string empresaid, tb_cm_ordendecompradoc BE, DataTable Detalle)
        {
            using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
            {
                bool xreturn = true;
                int lcont = 0;
                for (lcont = 0; lcont <= Detalle.Rows.Count - 1; lcont++)
                {
                    using (SqlCommand cmd = new SqlCommand("gspTbCmOrdencompradoc_INSERT", cnx))
                    {
                        cmd.CommandTimeout = 0;
                        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("@moduloiddes", SqlDbType.Char, 4).Value = BE.moduloiddes;
                        cmd.Parameters.Add("@serdoc", SqlDbType.Char, 4).Value = BE.serdoc;
                        cmd.Parameters.Add("@numdoc", SqlDbType.Char, 10).Value = BE.numdoc;

                        cmd.Parameters.Add("@item", SqlDbType.Char, 2).Value = Detalle.Rows[lcont]["item"];
                        cmd.Parameters.Add("@docglosa", SqlDbType.VarChar, 100).Value = Detalle.Rows[lcont]["docglosa"];
                        cmd.Parameters.Add("@docname", SqlDbType.VarChar, 100).Value = Detalle.Rows[lcont]["docname"];
                        cmd.Parameters.Add("@documen", SqlDbType.Image).Value = Detalle.Rows[lcont]["documen"];
                        try
                        {
                            cnx.Open();
                            if (cmd.ExecuteNonQuery() > 0)
                            {
                                xreturn = true;
                            }
                            else
                            {
                                xreturn = false;
                            }
                            cnx.Close();
                        }
                        catch (Exception ex)
                        {
                            Sql_Error = ex.Message;
                            //throw new Exception(ex.Message);
                            xreturn = false;
                            cnx.Close();
                        }
                    }
                }
                return xreturn;
            }
        }
        public bool Insert(string empresaid, tb_cm_ordendecompradoc BE)
        {
            using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
            {
                using (SqlCommand cmd = new SqlCommand("gspTb60Movimientosdoc_INSERT", cnx))
                {
                    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("@tipodoc", SqlDbType.Char, 2).Value = BE.tipodoc;
                    cmd.Parameters.Add("@serdoc", SqlDbType.Char, 4).Value = BE.serdoc;
                    cmd.Parameters.Add("@numdoc", SqlDbType.Char, 10).Value = BE.numdoc;

                    cmd.Parameters.Add("@item", SqlDbType.Char, 2).Value = BE.items;
                    //cmd.Parameters.Add("@docglosa", SqlDbType.VarChar, 100).Value = BE.docglosa;
                    //cmd.Parameters.Add("@docname", SqlDbType.VarChar, 100).Value = BE.docname;
                    //cmd.Parameters.Add("@documen", SqlDbType.Image).Value = BE.documen;

                    //cmd.Parameters.Add("@tipocontratoid", SqlDbType.Char, 2).Value = BE.tipocontratoid;
                    //cmd.Parameters.Add("@tipocontratoname", SqlDbType.VarChar, 100).Value = BE.tipocontratoname;
                    //cmd.Parameters.Add("@nomplantillaword", SqlDbType.VarChar, 100).Value = BE.nomplantillaword;
                    //cmd.Parameters.Add("@plantillaword", SqlDbType.VarBinary).Value = BE.plantillaword;

                    try
                    {
                        cnx.Open();
                        if (cmd.ExecuteNonQuery() > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception ex)
                    {
                        Sql_Error = ex.Message;
                        throw new Exception(ex.Message);
                    }
                }
            }
        }
        public DataSet GetOne(string empresaid, tb_cm_ordendecompradoc BE)
        {
            using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
            {
                using (SqlCommand cmd = new SqlCommand("gspTb60attachedfile_SELECT", cnx))
                {
                    DataSet ds = new DataSet();
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@tipocontratoid", SqlDbType.Char, 2).Value = BE.items;
                    }

                    try
                    {
                        cnx.Open();
                        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                        {
                            da.Fill(ds);
                        }
                        return ds;
                    }
                    catch (Exception ex)
                    {
                        Sql_Error = ex.Message;
                        throw new Exception(ex.Message);
                    }
                }
            }
        }
        private void CargaDatos()
        {
            var sorted = default(SortOrder);
            var xnomcolumna = string.Empty;
            if (dgvfileatached.SortedColumn != null)
            {
                xnomcolumna = dgvfileatached.Columns[dgvfileatached.SortedColumn.Index].Name;
                sorted = dgvfileatached.SortOrder;
            }

            var BL2 = new tb_cm_ordendecompradocBL();
            var BE2 = new tb_cm_ordendecompradoc();

            BE2.moduloid = VariablesPublicas.Moduloid;
            BE2.local = VariablesPublicas.Local;
            BE2.moduloiddes  = moduloiddes;
            BE2.numdoc = numdoc;
            BE2.serdoc = serdoc;

            Tabla = BL2.GetAll(VariablesPublicas.EmpresaID, BE2).Tables[0];

            if (Tabla.Rows.Count > 0)
            {
                dgvfileatached.AutoGenerateColumns = false;
                dgvfileatached.DataSource = Tabla;

                if (xnomcolumna.Trim().Length > 0)
                {
                    if (sorted == SortOrder.Ascending)
                    {
                        dgvfileatached.Sort(dgvfileatached.Columns[xnomcolumna], System.ComponentModel.ListSortDirection.Ascending);
                    }
                    else
                    {
                        dgvfileatached.Sort(dgvfileatached.Columns[xnomcolumna], System.ComponentModel.ListSortDirection.Descending);
                    }
                }
                else
                {
                    dgvfileatached.Sort(dgvfileatached.Columns["item"], System.ComponentModel.ListSortDirection.Ascending);
                }
            }
        }
        private void save()
        {
            if (u_Validate())
            {
                var BL = new tb_cm_ordendecompradocBL();
                var BE = new tb_cm_ordendecompradoc();
                BE.norden = 1;
                BE.ver_blanco = 0;
                BE.vista = 1;

                BE.moduloid = VariablesPublicas.Moduloid;
                BE.local = VariablesPublicas.Local;

                BE.moduloiddes = moduloiddes;
                BE.serdoc = serdoc;
                BE.numdoc = numdoc;

                tmpcursor = BL.GetAll(VariablesPublicas.EmpresaID, BE).Tables[0];

                var ofila = tmpcursor.NewRow();

                ofila = VariablesPublicas.InsertIntoTable(tmpcursor);

                if (u_n_opsel == 1)
                {
                    BE.items = txtcodigo.Text.Trim();

                    BE.moduloid = VariablesPublicas.Moduloid;
                    BE.local = VariablesPublicas.Local;
                    BE.moduloiddes = moduloiddes;
                    BE.serdoc = serdoc;
                    BE.numdoc = numdoc;

                    tmptabla = BL.GetAll(VariablesPublicas.EmpresaID, BE).Tables[0];

                    if (tmptabla.Rows.Count > 0)
                    {
                        var BLMX = new tb_cm_ordendecompradocBL();
                        var BEMX = new tb_cm_ordendecompradoc();

                        BEMX.moduloid = VariablesPublicas.Moduloid;
                        BEMX.local = VariablesPublicas.Local;
                        BEMX.moduloiddes = moduloiddes;
                        BEMX.serdoc = serdoc;
                        BEMX.numdoc = numdoc;

                        ofila["item"] = BLMX.GetAll_MaxCodigo(VariablesPublicas.EmpresaID.ToString(), BEMX).Tables[0].Rows[0]["maximo_codigo"].ToString();
                    }
                    else
                    {
                        ofila["item"] = txtcodigo.Text;
                    }
                }
                else
                {
                    ofila["item"] = dgvfileatached.Rows[dgvfileatached.CurrentRow.Index].Cells["item"].Value;
                    for (lc_cont = 0; lc_cont <= dgvfileatached.ColumnCount - 1; lc_cont++)
                    {
                        xnomcampo = dgvfileatached.Columns[lc_cont].Name;
                        ofila[xnomcampo] = dgvfileatached.Rows[dgvfileatached.CurrentRow.Index].Cells[lc_cont].Value;
                    }
                }

                ofila["item"] = txtcodigo.Text.Trim();
                ofila["docglosa"] = txtdescripcion.Text.Trim();
                ofila["docname"] = txtnombrearchivo.Text.Trim();
                ofila["documen"] = vmContenidoFile;
                tmpcursor.Rows.Add(ofila);

                BE.moduloid = VariablesPublicas.Moduloid;
                BE.local = VariablesPublicas.Local;
                BE.moduloiddes = moduloiddes;
                BE.serdoc = serdoc;
                BE.numdoc = numdoc;

                if (BL.Insert_Update(VariablesPublicas.EmpresaID, BE, tmpcursor))
                {
                    U_CancelarEdicion(0);
                }
                else
                {
                    Frm_Class.ShowError(BL.Sql_Error, this);
                }
            }
        }
 public DataSet GetAll_CONSULTA(string empresaid, tb_cm_ordendecompradoc BE)
 {
     using (SqlConnection cnx = new SqlConnection(conex.empConexion(empresaid)))
     {
         using (SqlCommand cmd = new SqlCommand("gspTbcmattachedfile_CONSULTA", cnx))
         {
             DataSet ds = new DataSet();
             {
                 cmd.CommandType = CommandType.StoredProcedure;
                 cmd.Parameters.AddWithValue("@item", string.IsNullOrEmpty(BE.items) ? (object)DBNull.Value : BE.items);
                 cmd.Parameters.AddWithValue("@nomlike1", string.IsNullOrEmpty(BE.nomlike1) ? (object)DBNull.Value : BE.nomlike1);
                 cmd.Parameters.AddWithValue("@nomlike2", string.IsNullOrEmpty(BE.nomlike2) ? (object)DBNull.Value : BE.nomlike2);
                 cmd.Parameters.AddWithValue("@nomlike3", string.IsNullOrEmpty(BE.nomlike3) ? (object)DBNull.Value : BE.nomlike3);
                 cmd.Parameters.Add("@norden", SqlDbType.Int).Value = BE.norden;
                 cmd.Parameters.Add("@ver_blanco", SqlDbType.Int).Value = BE.ver_blanco;
                 cmd.Parameters.Add("@vista", SqlDbType.Int).Value = BE.vista;
             }
             try
             {
                 cnx.Open();
                 using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                 {
                     da.Fill(ds);
                 }
                 return ds;
             }
             catch (Exception ex)
             {
                 Sql_Error = ex.Message;
                 throw new Exception(ex.Message);
             }
         }
     }
 }