public List <AnexoHistoria> GetAnexoHistorias(
            int idHistoria,
            Constants.TipoRevision idTipoRevision,
            bool showAllTipoRevision,
            int idMuestra,
            string nombreAdjunto,
            DbTransaction parentTrans)
        {
            List <AnexoHistoria> anexoHistoriaList = new List <AnexoHistoria>();

            try
            {
                foreach (DataRow row in (InternalDataCollectionBase)this.GetList(idHistoria, idTipoRevision, showAllTipoRevision, idMuestra, nombreAdjunto, parentTrans).Rows)
                {
                    AnexoHistoria objToLoad = new AnexoHistoria();
                    this.LoadFromDataRow(ref objToLoad, row);
                    anexoHistoriaList.Add(objToLoad);
                }
            }
            catch (Exception ex)
            {
                this.error = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }
            return(anexoHistoriaList);
        }
        private AnexoHistoria GetByPK(int id)
        {
            DbConnection  connection = this.instance.CreateConnection();
            AnexoHistoria objToLoad  = new AnexoHistoria();

            try
            {
                connection.Open();
                DbCommand storedProcCommand = this.instance.GetStoredProcCommand("PA_GET_ANEXO_HISTORIA_BY_PK");
                storedProcCommand.Connection = connection;
                this.instance.AddInParameter(storedProcCommand, "@ID", DbType.Int32, (object)id);
                DbDataAdapter dataAdapter = this.instance.GetDataAdapter();
                DataTable     dataTable   = new DataTable();
                dataAdapter.SelectCommand = storedProcCommand;
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count > 0)
                {
                    this.LoadFromDataRow(ref objToLoad, dataTable.Rows[0]);
                }
            }
            catch (Exception ex)
            {
                this.error = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }
            finally
            {
                connection.Close();
            }
            return(objToLoad);
        }
        public bool Update(AnexoHistoria objToProcess, DbTransaction parentTrans)
        {
            bool flag = false;

            try
            {
                DbConnection dbConnection = (DbConnection)null;
                try
                {
                    DbCommand storedProcCommand = this.instance.GetStoredProcCommand("PA_ANEXO_HISTORIA_UPDATE");
                    this.instance.AddInParameter(storedProcCommand, "@ID", DbType.Int32, (object)objToProcess.Id);
                    this.instance.AddInParameter(storedProcCommand, "@ID_HISTORIA", DbType.Int32, (object)objToProcess.IdHistoria);
                    this.instance.AddInParameter(storedProcCommand, "@ID_TIPO_REVISION", DbType.Int16, (object)Convert.ToInt16((object)objToProcess.IdTipoRevision));
                    if (objToProcess.IdMuestra != int.MinValue)
                    {
                        this.instance.AddInParameter(storedProcCommand, "@ID_MUESTRA", DbType.Int32, (object)objToProcess.IdMuestra);
                    }
                    else
                    {
                        this.instance.AddInParameter(storedProcCommand, "@ID_MUESTRA", DbType.Int32, (object)DBNull.Value);
                    }
                    this.instance.AddInParameter(storedProcCommand, "@NOMBRE_ADJUNTO", DbType.String, (object)objToProcess.NombreAdjunto);
                    this.instance.AddInParameter(storedProcCommand, "@NOMBRE_ARCHIVO", DbType.String, (object)objToProcess.NombreArchivo);
                    this.instance.AddInParameter(storedProcCommand, "@ID_ULTIMA_MODIFICACION", DbType.Int32, (object)objToProcess.IdUltimaModificacion);
                    if (parentTrans != null)
                    {
                        storedProcCommand.Connection  = parentTrans.Connection;
                        storedProcCommand.Transaction = parentTrans;
                        this.instance.ExecuteNonQuery(storedProcCommand, parentTrans);
                    }
                    else
                    {
                        dbConnection = this.instance.CreateConnection();
                        dbConnection.Open();
                        storedProcCommand.Connection = dbConnection;
                        this.instance.ExecuteNonQuery(storedProcCommand);
                    }
                    flag = true;
                }
                catch (Exception ex)
                {
                    this.error = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                }
                finally
                {
                    if (parentTrans == null)
                    {
                        if (dbConnection != null)
                        {
                            dbConnection.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.error = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }
            return(flag);
        }
        protected void rbtnCargarArchivo_Click(object sender, EventArgs e)
        {
            if (this.rauFile.UploadedFiles.Count > 0)
            {
                AnexoHistoriaBll objBll    = new AnexoHistoriaBll();
                AnexoHistoria    objEntity = new AnexoHistoria();
                objEntity.IdHistoria     = this.IdHist;
                objEntity.IdMuestra      = this.IdMuestraRelacionada;
                objEntity.IdTipoRevision = this.TipoRevision;
                objEntity.IdCreacion     = this.IdUserCurrent;
                objEntity.NombreAdjunto  = this.rtxtNameFile.Text.Trim();

                int idTipoEx = 0;

                if (this.IdMuestraRelacionada != int.MinValue)
                {
                    MuestraLaboratorioBll objBllMl = new MuestraLaboratorioBll();
                    MuestraLaboratorio    objEntMl = objBllMl.Load(this.idMuestraRelacionada);
                    idTipoEx = Convert.ToInt32(objEntMl.IdTipoExamen);
                }

                Stream dataFoto = null;
                dataFoto = this.rauFile.UploadedFiles[0].InputStream;

                if (!objBll.GuardarNuevoArchivo(objEntity, dataFoto, this.PathFilesToAttach, this.rauFile.UploadedFiles[0].GetExtension()))
                {
                    RadScriptManager.RegisterClientScriptBlock(this, this.GetType(), "errSav", "alert('Se presento el siguiente error al cargar el archivo');", true);
                }
                else
                {
                    RadScriptManager.RegisterClientScriptBlock(this, this.GetType(), "okSav", "CargarPadre(" + Convert.ToInt32(this.tipoRevision) + ", " + idTipoEx + ");", true);
                }
            }
        }
        protected void rbtnConsultar_Click(object sender, EventArgs e)
        {
            if (this.rcbxEmbajada.SelectedValue != string.Empty && this.rcbxTipoVisa.SelectedValue != string.Empty && this.rtxtNumVisa.Text.Trim() != string.Empty)
            {
                HistoriaMedicaBll     objBll = new HistoriaMedicaBll();
                List <HistoriaMedica> lstHm  = objBll.GetHistoriasMedicas(int.MinValue, DateTime.MinValue, Convert.ToInt32(rcbxTipoVisa.SelectedValue), this.rtxtNumVisa.Text.Trim(), true, true, Constants.EstadoRevision.Normal, true, true, true,
                                                                          Constants.EstadoRevision.Normal, true, true, true, Constants.EstadoRevision.Normal, true, true, false, true, false, int.MinValue);

                if (lstHm.Count > 0)
                {
                    HistoriaMedica   objEntHm     = lstHm[0];
                    PacienteBll      objBllPac    = new PacienteBll();
                    Paciente         objEntPac    = objBllPac.Load(objEntHm.IdPaciente);
                    TipoDocumentoBll objBlltipDoc = new TipoDocumentoBll();
                    TipoDocumento    objEntTipDoc = objBlltipDoc.Load(objEntPac.IdTipoDocumento);

                    this.hfIdHist.Value   = objEntHm.Id.ToString();
                    this.lblCodSol.Text   = objEntHm.CodigoSolicitud;
                    this.lblFecIng.Text   = objEntHm.FechaIngreso.ToString("yyyy-MM-dd");
                    this.lblNumDoc.Text   = objEntPac.NumeroDocumento;
                    this.lblPaciente.Text = objEntPac.Nombres + " " + objEntPac.Apellidos;
                    this.lblTipDoc.Text   = objEntTipDoc.Nombre;
                    this.lblTipoCod.Text  = rcbxTipoVisa.SelectedItem.Text;

                    this.pnlInfoPac.Visible = true;

                    AnexoHistoriaBll objAhBll = new AnexoHistoriaBll();
                    int idHistoria            = objEntHm.Id;
                    List <AnexoHistoria> lst  = objAhBll.GetAnexoHistorias(idHistoria, Constants.TipoRevision.MedicinaGeneral, false, int.MinValue, Constants.NombreArchivoImagenPaciente);
                    if (lst.Count > 0)
                    {
                        AnexoHistoria objAhFileImage = lst[0];
                        if (Directory.Exists(this.PathFilesToAttach + idHistoria + "\\"))
                        {
                            StreamReader srImg  = new StreamReader(this.PathFilesToAttach + idHistoria + "\\" + objAhFileImage.NombreArchivo);
                            BinaryReader reader = new BinaryReader(srImg.BaseStream);
                            Byte[]       data   = reader.ReadBytes((int)srImg.BaseStream.Length);

                            string contentFile = Convert.ToBase64String(data);
                            this.imgPac.ImageUrl = String.Format("data:image/png;base64,{0}", contentFile);
                        }
                    }

                    /*
                     * this.Page.Controls.Add(new LiteralControl("<span>Nombre paciente: " + objEntPac.Nombres + " " + objEntPac.Apellidos + "</span>"));
                     */
                }
                else
                {
                    this.pnlInfoPac.Visible = false;
                    RadScriptManager.RegisterClientScriptBlock(this, this.GetType(), "notFounded", "alert('La historia con tipo " + rcbxTipoVisa.SelectedItem.Text + " y número " + this.rtxtNumVisa.Text.Trim() + " no se ha encontrado o aun no ha finalizado \\n\\nPara regresarla de la embajada');", true);
                }
            }
            else
            {
                this.pnlInfoPac.Visible = false;
                RadScriptManager.RegisterClientScriptBlock(this, this.GetType(), "notSelected", "alert('No ha seleccionado los datos para busqueda del paciente');", true);
            }
        }
        private void GetFile(int idAnexo)
        {
            AnexoHistoriaBll objBll = new AnexoHistoriaBll();
            AnexoHistoria    objEnt = objBll.Load(idAnexo);

            if (objEnt.Id != int.MinValue)
            {
                if (Directory.Exists(this.PathFilesToAttach))
                {
                    string pathHist = this.PathFilesToAttach + objEnt.IdHistoria + "\\";
                    if (Directory.Exists(pathHist))
                    {
                        if (File.Exists(pathHist + objEnt.NombreArchivo))
                        {
                            /*
                             * FileStream fileToGet = new FileStream(pathHist + objEnt.NombreArchivo, FileMode.Open, FileAccess.Read);
                             * byte[] contetnFile = new byte[Convert.ToInt32(fileToGet.Length)];
                             * fileToGet.Read(contetnFile, 0, contetnFile.Length);
                             */
                            Response.Clear();
                            Response.Buffer      = true;
                            Response.ContentType = "application/application/octet-stream";
                            Response.AddHeader("Content-Disposition", "attachment;filename=" + objEnt.NombreArchivo);
                            Response.Charset         = "UTF-8";
                            Response.ContentEncoding = Encoding.Default;
                            Response.WriteFile(pathHist + objEnt.NombreArchivo);
                            Response.Flush();
                            Response.End();
                        }
                    }
                    else
                    {
                        RadScriptManager.RegisterClientScriptBlock(this, this.GetType(), "dirHisNotFound", "FileNotFound();", true);
                    }
                }
                else
                {
                    RadScriptManager.RegisterClientScriptBlock(this, this.GetType(), "dirNotFound", "FileNotFound();", true);
                }
            }
            else
            {
                RadScriptManager.RegisterClientScriptBlock(this, this.GetType(), "filNotFound", "FileNotFound();", true);
            }
        }
        public bool Delete(AnexoHistoria objToProcess, DbTransaction parentTrans)
        {
            bool flag = false;

            try
            {
                DbCommand storedProcCommand = this.instance.GetStoredProcCommand("PA_ANEXO_HISTORIA_DELETE");
                storedProcCommand.Connection  = parentTrans.Connection;
                storedProcCommand.Transaction = parentTrans;
                this.instance.AddInParameter(storedProcCommand, "@ID", DbType.Int32, (object)objToProcess.Id);
                this.instance.ExecuteNonQuery(storedProcCommand, parentTrans);
                flag = true;
            }
            catch (Exception ex)
            {
                this.error = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }
            return(flag);
        }
        public void LoadImgPerfilPaciente(int idHistoria, Telerik.Web.UI.RadBinaryImage imgPrePhoto)
        {
            AnexoHistoriaBll     objAhBll = new AnexoHistoriaBll();
            List <AnexoHistoria> lst      = objAhBll.GetAnexoHistorias(idHistoria, Constants.TipoRevision.MedicinaGeneral, false, int.MinValue, Constants.NombreArchivoImagenPaciente);

            if (lst.Count > 0)
            {
                AnexoHistoria objAhFileImage = lst[0];
                if (Directory.Exists(this.PathFilesToAttach + idHistoria + "\\"))
                {
                    StreamReader srImg  = new StreamReader(this.PathFilesToAttach + idHistoria + "\\" + objAhFileImage.NombreArchivo);
                    BinaryReader reader = new BinaryReader(srImg.BaseStream);
                    Byte[]       data   = reader.ReadBytes((int)srImg.BaseStream.Length);

                    string contentFile = Convert.ToBase64String(data);
                    imgPrePhoto.ImageUrl = String.Format("data:image/png;base64,{0}", contentFile);
                }
            }
        }
 private void LoadFromDataRow(ref AnexoHistoria objToLoad, DataRow drData)
 {
     try
     {
         objToLoad.Id                      = Convert.ToInt32(drData["ID"]);
         objToLoad.IdHistoria              = Convert.ToInt32(drData["ID_HISTORIA"]);
         objToLoad.IdTipoRevision          = (Constants.TipoRevision)Convert.ToInt32(drData["ID_TIPO_REVISION"]);
         objToLoad.IdMuestra               = Convert.IsDBNull(drData["ID_MUESTRA"]) ? int.MinValue : Convert.ToInt32(drData["ID_MUESTRA"]);
         objToLoad.NombreAdjunto           = drData["NOMBRE_ADJUNTO"].ToString();
         objToLoad.NombreArchivo           = drData["NOMBRE_ARCHIVO"].ToString();
         objToLoad.IdCreacion              = Convert.ToInt32(drData["ID_CREACION"]);
         objToLoad.FechaCreacion           = Convert.ToDateTime(drData["FECHA_CREACION"]);
         objToLoad.IdUltimaModificacion    = Convert.ToInt32(drData["ID_ULTIMA_MODIFICACION"]);
         objToLoad.FechaUltimaModificacion = Convert.ToDateTime(drData["FECHA_ULTIMA_MODIFICACION"]);
     }
     catch (Exception ex)
     {
         objToLoad  = new AnexoHistoria();
         this.error = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
     }
 }
        public bool IngresarPaciente(
            Paciente objEntPac,
            HistoriaMedica objEntHisMed,
            ArrayList examenes,
            Stream fotoCargada,
            string pathFilesPac)
        {
            bool flag1 = false;

            try
            {
                DbTransaction transaction = BaseConnection.GetTransaction();
                string        path        = string.Empty;
                try
                {
                    PacienteDao       pacienteDao       = new PacienteDao();
                    HistoriaMedicaDao historiaMedicaDao = new HistoriaMedicaDao();
                    if (!(objEntPac.Id == int.MinValue ? pacienteDao.Create(objEntPac, transaction) : pacienteDao.Update(objEntPac, transaction)))
                    {
                        throw new Exception(pacienteDao.Error);
                    }
                    objEntHisMed.IdPaciente = objEntPac.Id;
                    bool flag2 = false;
                    bool flag3;
                    if (objEntHisMed.Id != int.MinValue)
                    {
                        flag3 = historiaMedicaDao.Update(objEntHisMed, transaction);
                        flag2 = true;
                    }
                    else
                    {
                        flag3 = historiaMedicaDao.Create(objEntHisMed, transaction);
                    }
                    if (!flag3)
                    {
                        throw new Exception(historiaMedicaDao.Error);
                    }
                    if (examenes.Count > 0)
                    {
                        DataTable dtEstructure = MuestraLaboratorioDao.DtEstructure;
                        foreach (int examene in examenes)
                        {
                            if (flag2)
                            {
                                if (new MuestraLaboratorioDao().GetList(objEntHisMed.Id, (Constants.ExamenLab)examene, false, string.Empty, DateTime.MinValue, short.MinValue, transaction).Rows.Count <= 0)
                                {
                                    DataRow row = dtEstructure.NewRow();
                                    row["ID_HISTORIA"]               = (object)objEntHisMed.Id;
                                    row["ESTADO"]                    = (object)Convert.ToInt16((object)Constants.EstadosMuestraLaboratorio.SinAplicar);
                                    row["ID_TIPO_EXAMEN"]            = (object)examene;
                                    row["RESULTADO"]                 = (object)"SIN REALIZAR";
                                    row["NUMERO_MUESTRA"]            = (object)1;
                                    row["ID_CREACION"]               = (object)objEntPac.IdUltimaModificacion;
                                    row["FECHA_CREACION"]            = (object)DateTime.Now;
                                    row["ID_ULTIMA_MODIFICACION"]    = (object)objEntPac.IdUltimaModificacion;
                                    row["FECHA_ULTIMA_MODIFICACION"] = (object)DateTime.Now;
                                    dtEstructure.Rows.Add(row);
                                }
                            }
                            else
                            {
                                DataRow row = dtEstructure.NewRow();
                                row["ID_HISTORIA"]               = (object)objEntHisMed.Id;
                                row["ESTADO"]                    = (object)Convert.ToInt16((object)Constants.EstadosMuestraLaboratorio.SinAplicar);
                                row["ID_TIPO_EXAMEN"]            = (object)examene;
                                row["RESULTADO"]                 = (object)"SIN REALIZAR";
                                row["NUMERO_MUESTRA"]            = (object)1;
                                row["ID_CREACION"]               = (object)objEntPac.IdUltimaModificacion;
                                row["FECHA_CREACION"]            = (object)DateTime.Now;
                                row["ID_ULTIMA_MODIFICACION"]    = (object)objEntPac.IdUltimaModificacion;
                                row["FECHA_ULTIMA_MODIFICACION"] = (object)DateTime.Now;
                                dtEstructure.Rows.Add(row);
                            }
                        }
                        if (dtEstructure.Rows.Count > 0)
                        {
                            MuestraLaboratorioDao muestraLaboratorioDao = new MuestraLaboratorioDao();
                            if (!muestraLaboratorioDao.CreateFromDt(dtEstructure, transaction))
                            {
                                throw new Exception(muestraLaboratorioDao.Error);
                            }
                        }
                    }
                    if (fotoCargada != null)
                    {
                        if (!Directory.Exists(pathFilesPac))
                        {
                            throw new Exception("La ruta " + pathFilesPac + " no existe o no es accesible desde el servidor.");
                        }
                        path = pathFilesPac + (object)objEntHisMed.Id + "/";
                        string str = "ImgPaciente.png";
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        byte[] buffer = new byte[fotoCargada.Length];
                        fotoCargada.Read(buffer, 0, buffer.Length);
                        FileStream fileStream = new FileStream(path + str, FileMode.Create, FileAccess.ReadWrite);
                        fileStream.Write(buffer, 0, buffer.Length);
                        fileStream.Flush();
                        fileStream.Close();
                        AnexoHistoriaBll     anexoHistoriaBll = new AnexoHistoriaBll();
                        List <AnexoHistoria> anexoHistorias   = anexoHistoriaBll.GetAnexoHistorias(objEntHisMed.Id, Constants.TipoRevision.MedicinaGeneral, false, int.MinValue, "Imagen Perfil Paciente");
                        AnexoHistoria        objEnt;
                        if (anexoHistorias.Count > 0)
                        {
                            objEnt = anexoHistorias[0];
                        }
                        else
                        {
                            objEnt               = new AnexoHistoria();
                            objEnt.IdCreacion    = objEntHisMed.IdUltimaModificacion;
                            objEnt.FechaCreacion = DateTime.Now;
                        }
                        objEnt.IdHistoria              = objEntHisMed.Id;
                        objEnt.NombreAdjunto           = "Imagen Perfil Paciente";
                        objEnt.IdTipoRevision          = Constants.TipoRevision.MedicinaGeneral;
                        objEnt.NombreArchivo           = str;
                        objEnt.IdUltimaModificacion    = objEntHisMed.IdUltimaModificacion;
                        objEnt.FechaUltimaModificacion = DateTime.Now;
                        if (!anexoHistoriaBll.Save(objEnt, transaction))
                        {
                            throw new Exception(anexoHistoriaBll.Error);
                        }
                    }
                    transaction.Commit();
                    flag1 = true;
                }
                catch (Exception ex)
                {
                    this.error = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                    transaction.Rollback();
                    if (path != null)
                    {
                        if (path != string.Empty)
                        {
                            if (Directory.Exists(path))
                            {
                                Directory.Delete(path, true);
                            }
                        }
                    }
                }
                finally
                {
                    transaction.Dispose();
                }
            }
            catch (Exception ex)
            {
                this.error = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }
            return(flag1);
        }