306 lines
10 KiB
C#
306 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.UI;
|
|
using System.Web.UI.WebControls;
|
|
using Extensiones;
|
|
using System.Data;
|
|
using System.IO;
|
|
|
|
public partial class OI_DocumentoDetalle : ClsPaginas
|
|
{
|
|
private int CveDocumento
|
|
{
|
|
get
|
|
{
|
|
return ViewState["CveDocumento"].ToString().ToEntero();
|
|
}
|
|
|
|
set
|
|
{
|
|
ViewState["CveDocumento"] = value;
|
|
}
|
|
}
|
|
|
|
private string ErrorMensaje
|
|
{
|
|
get
|
|
{
|
|
return ViewState["ErrorMensaje"].ToString();
|
|
}
|
|
|
|
set
|
|
{
|
|
ViewState["ErrorMensaje"] = value;
|
|
}
|
|
}
|
|
|
|
private string ArchivoAdjuntoNombre
|
|
{
|
|
get
|
|
{
|
|
return ViewState["ArchivoAdjuntoNombre"].ToString();
|
|
}
|
|
|
|
set
|
|
{
|
|
ViewState["ArchivoAdjuntoNombre"] = value;
|
|
}
|
|
}
|
|
|
|
protected void Page_Load(object sender, EventArgs e)
|
|
{
|
|
if (!IsPostBack)
|
|
{
|
|
Cargar();
|
|
}
|
|
}
|
|
#region "CARGAS"
|
|
private void CargarCveDocumento()
|
|
{
|
|
string CveDocumentoAux = Page.Request["CveDocumento"];
|
|
if (CveDocumentoAux != null)
|
|
{
|
|
this.CveDocumento = CveDocumentoAux.ToEntero();
|
|
}
|
|
else
|
|
{
|
|
this.CveDocumento = -1;
|
|
}
|
|
}
|
|
|
|
private void CargarUsuarios()
|
|
{
|
|
GvUsuarios.DataSource = get_GlobalDTUsuarios(CveEmpresa: UsuarioActual.CveEmpresa);
|
|
GvUsuarios.DataBind();
|
|
}
|
|
|
|
private void CargarSubCategoria()
|
|
{
|
|
ClsOIEventos ObjOI = new ClsOIEventos();
|
|
if (ObjOI.GetDocumentoSubCategoria())
|
|
{
|
|
DdlSubCategoria.DataSource = ObjOI.DtResultados;
|
|
DdlSubCategoria.DataBind();
|
|
}
|
|
ObjOI.Dispose();
|
|
}
|
|
|
|
private void CargarResponsable()
|
|
{
|
|
TxtResponsable.Text = UsuarioActual.Nombre;
|
|
}
|
|
|
|
private void CargarDocumentoYaExistente()
|
|
{
|
|
ClsOIEventos ObjOI = new ClsOIEventos();
|
|
if (ObjOI.GetDocumento(this.CveDocumento, UsuarioActual.CveEmpresa))
|
|
{
|
|
if (ObjOI.DtResultados.Rows.Count > 0)
|
|
{
|
|
DataRow Documento = ObjOI.DtResultados.Rows[0];
|
|
|
|
TxtDescripcion.Text = Documento["Descripcion"].ToString();
|
|
DdlSubCategoria.SelectedValue = Documento["CveSubCategoria"].ToString();
|
|
DdlEstatus.SelectedValue = Documento["CveEstatus"].ToString();
|
|
TxtVersion.Text = Documento["Version"].ToString();
|
|
TxtResponsable.Text = Documento["Responsable"].ToString();
|
|
TxtObs.InnerText = Documento["Observacion"].ToString();
|
|
|
|
ArchivoAdjuntoNombre = Documento["ArchivoFisico"].ToString();
|
|
string[] ArchivoAux = Documento["ArchivoFisico"].ToString().Split('_');
|
|
string ArchivoAdjunto = ArchivoAux[2];
|
|
LkbDescargarArchivo.Text = string.Format("Descargar Archivo: {0}",ArchivoAdjunto);
|
|
}
|
|
}
|
|
ObjOI.Dispose();
|
|
}
|
|
|
|
private void CargarUsuariosAutorizo()
|
|
{
|
|
ClsOIEventos ObjOI = new ClsOIEventos();
|
|
if (ObjOI.GetDocumentoAutorizacion(this.CveDocumento))
|
|
{
|
|
DataTable UsuariosAutorizo = ObjOI.DtResultados;
|
|
GridViewRowCollection UsuariosRowCollection = GvUsuarios.Rows;
|
|
foreach (GridViewRow UsuarioRow in UsuariosRowCollection)
|
|
{
|
|
int CveUsuarioRow = UsuarioRow.DataKey("CveUsuario").ToEntero();
|
|
UsuarioRow.FCCheckBox("ChkUnir").Checked = UsuariosAutorizo.COUNT("CveUsuario", string.Format("CveUsuario = {0}", CveUsuarioRow)) > 0;
|
|
}
|
|
}
|
|
ObjOI.Dispose();
|
|
}
|
|
|
|
private void ConfiguracionActualizar()
|
|
{
|
|
LkbGuardar.Text = "Actualizar";
|
|
}
|
|
|
|
private void Cargar()
|
|
{
|
|
CargarUsuarios();
|
|
CargarSubCategoria();
|
|
CargarResponsable();
|
|
CargarCveDocumento();
|
|
if (this.CveDocumento > 0)
|
|
{
|
|
CargarDocumentoYaExistente();
|
|
CargarUsuariosAutorizo();
|
|
ConfiguracionActualizar();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region "GENERAR DOCUMENTO"
|
|
private string GuardarArchivo(int CveDocumento)
|
|
{
|
|
string RutaArchivo = "-1";
|
|
List<Archivo> Filesgral = TxtDocumentoFile.getfiles().Archivos;
|
|
if (TxtDocumentoFile.Text != "")
|
|
{
|
|
foreach (Archivo fileItem in Filesgral)
|
|
{
|
|
try
|
|
{
|
|
string Version = TxtVersion.Text;
|
|
string NombreArchivo = fileItem.filenameO;
|
|
//Preparamos la ruta donde moveremos el archivo cargado
|
|
string NombreArchivoGuardado = string.Format("{0}_{1}_{2}", CveDocumento, Version, NombreArchivo);
|
|
string RutaDestino = UsuarioActual.Empresa.Ruta + @"DocumentosRepo\" + NombreArchivoGuardado;
|
|
|
|
//Si no existe la carpeta donde se guardan se crea
|
|
if (!Directory.Exists(UsuarioActual.Empresa.Ruta + @"\DocumentosRepo"))
|
|
{
|
|
Directory.CreateDirectory(UsuarioActual.Empresa.Ruta + @"\DocumentosRepo");
|
|
}
|
|
//Movemos el archivo de la carpeta temporal hacia la carpeta de la empresa
|
|
System.IO.File.Move(fileItem.FileUrl, RutaDestino);
|
|
RutaArchivo = NombreArchivoGuardado;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Alert(e.Message);
|
|
}
|
|
}
|
|
}
|
|
return RutaArchivo;
|
|
}
|
|
|
|
protected void LblGuardar_Click(object sender, EventArgs e)
|
|
{
|
|
ClsOIEventos ObjOI = new ClsOIEventos();
|
|
ObjOI.IniciaTransacSQL();
|
|
bool TieneError = false;
|
|
|
|
//Esta variable es requerida más adelante
|
|
int CveDocumento = 0;
|
|
string RutaArchivo = "-1";
|
|
|
|
CveDocumento = GuardarDocumento(ref ObjOI, ref TieneError);//Se crea el Documento
|
|
if (!TieneError) { RutaArchivo = AsignarArchivoAlDocumento(CveDocumento, ref ObjOI, ref TieneError); }//Se guarda el archivo fisico y se asigna al Documento generado
|
|
if (!TieneError) { GuardarUsuariosQueAutorizan(CveDocumento, RutaArchivo, ref ObjOI, ref TieneError); }//asignamos a los usuarios que participaran en aprovar este documento
|
|
|
|
|
|
//Gestión de errores y resultados
|
|
if (!TieneError)
|
|
{
|
|
ObjOI.Commit();
|
|
Alert("Documento guardado correctamente");
|
|
}
|
|
else
|
|
{
|
|
ObjOI.RollBack();
|
|
Alert(this.ErrorMensaje, ModExtensiones.TipoMensaje.MsgError);
|
|
this.CveDocumento = CveDocumento;
|
|
Cargar();
|
|
UpCliente.Update();
|
|
}
|
|
ObjOI.Dispose();
|
|
}
|
|
|
|
private int GuardarDocumento(ref ClsOIEventos ObjOI, ref bool TieneError)
|
|
{
|
|
int CveDocumentoPadre = this.CveDocumento > 0 ? this.CveDocumento : -1; //Asociamos el nuevo insert con un CveDocumento ya generado en caso de ser una nueva versión del documento
|
|
|
|
int CveDocumento = ObjOI.AddDocumento(DdlSubCategoria.SelectedValue.ToEntero(), TxtDescripcion.Text, TxtObs.InnerText, TxtVersion.Text, DdlEstatus.SelectedValue.ToEntero(),
|
|
"", TxtResponsable.Text, CveDocumentoPadre, UsuarioActual.CveUsuario, UsuarioActual.CveEmpresa);
|
|
if (!(CveDocumento > 0))
|
|
{
|
|
TieneError = true;
|
|
this.ErrorMensaje = "Hubo un error al guardar el Documento";
|
|
}
|
|
return CveDocumento;
|
|
}
|
|
|
|
private string AsignarArchivoAlDocumento(int CveDocumento, ref ClsOIEventos ObjOI, ref bool TieneError)
|
|
{
|
|
string RutaArchivo = GuardarArchivo(CveDocumento);
|
|
if (RutaArchivo != "-1")
|
|
{
|
|
if (!ObjOI.UpdateDocumento(CveDocumento, -1, "-1", "-1", "-1", -1, RutaArchivo, "-1", ModExtensiones.MinFecha, -1, -1, -1))
|
|
{
|
|
TieneError = true;
|
|
this.ErrorMensaje = "Hubo un error al asignar el archivo";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
TieneError = true;
|
|
this.ErrorMensaje = "Hubo un error al guardar el archivo";
|
|
}
|
|
return RutaArchivo;
|
|
}
|
|
|
|
private void GuardarUsuariosQueAutorizan(int CveDocumento, string RutaArchivo, ref ClsOIEventos ObjOI, ref bool TieneError)
|
|
{
|
|
GridViewRowCollection GridUsuarios = GvUsuarios.Rows;
|
|
foreach (GridViewRow GvRow in GridUsuarios)
|
|
{
|
|
if (GvRow.FCCheckBox("ChkUnir").Checked && !TieneError)
|
|
{
|
|
int CveUsuario = GvRow.DataKey("CveUsuario").ToEntero();
|
|
if (!ObjOI.AddDocumentoAutorizacion(CveDocumento, CveUsuario, RutaArchivo))
|
|
{
|
|
TieneError = true;
|
|
this.ErrorMensaje = "Hubo un error al asignar los usuarios a este documento";
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region "DESCARGAR ARCHIVO"
|
|
protected void LkbSubirNuevoArchivo_Click(object sender,EventArgs e)
|
|
{
|
|
DivDescargarArchivo.Visible = false;
|
|
DivSubirArchivo.Visible = true;
|
|
UpDerecha.Update();
|
|
}
|
|
|
|
protected void LkbDescargarNuevoArchivo_Click(object sender,EventArgs e)
|
|
{
|
|
DivDescargarArchivo.Visible = true;
|
|
DivSubirArchivo.Visible = false;
|
|
UpDerecha.Update();
|
|
}
|
|
|
|
protected void LkbDescargarArchivo_Click(object sender,EventArgs e)
|
|
{
|
|
string Ruta = string.Format("{0}Empresas/{1}/Temp/RH/{2}", ModExtensiones.DirectorioBaseEmpresas, UsuarioActual.CveEmpresa, ArchivoAdjuntoNombre);
|
|
byte[] archivoBytes = File.ReadAllBytes(Ruta);
|
|
|
|
HttpContext.Current.Response.Clear();
|
|
HttpContext.Current.Response.ClearContent();
|
|
HttpContext.Current.Response.ContentType = "application/octet-stream";
|
|
HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}", ArchivoAdjuntoNombre));
|
|
HttpContext.Current.Response.AddHeader("Content-Length", archivoBytes.Length.ToString());
|
|
HttpContext.Current.Response.OutputStream.Write(archivoBytes, 0, archivoBytes.Length);
|
|
HttpContext.Current.Response.End();
|
|
}
|
|
#endregion
|
|
|
|
|
|
} |