public void InsertarCatArchivos(CatArchivos entidad)
        {
			try
			{
				//Obtener DbCommand para ejcutar el Store Procedure
				using (DbCommand com = db.GetStoredProcCommand("NombreDelStrore"))
				{
					//Parametros
					//db.AddInParameter(com, "@Parametro", DbType.Tipo, entidad.Atributo);
				
					//Ejecucion de la Consulta
					db.ExecuteNonQuery(com);

					//Cierre de la conexion y liberacion de memoria
					com.Dispose();
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
        }
        public static string GeneraArchivoAfiliacion(int idBanco)
        {
            string Mensaje = "";
            CatArchivos LayoutGral = new CatArchivos();
            List<CatArchivosDetalle> LayoutDet = new List<CatArchivosDetalle>();
            List<CatArchivosDetalle> LayoutSDet = new List<CatArchivosDetalle>();
            List<ClientesAfiliacion> cuentasAfiliar = new List<ClientesAfiliacion>();

            LayoutGral = ObtenerCatArchivos(idBanco, "A", "S");
            LayoutDet = CatArchivosDetalleBll.ObtenerCatArchivosDetalle(LayoutGral.IdArchivo, "D");
            LayoutSDet = CatArchivosDetalleBll.ObtenerCatArchivosDetalle(LayoutGral.IdArchivo, "S");
            
            cuentasAfiliar = Creditos.ClientesAfiliacionBll.ObtenerCuentas(1);
            List<string> lineas = new List<string>();

            string linea = "";
            string campo = "";
            string Relleno = "";
            int longitud = 0;
            int longitudCampo = 0;

            foreach (ClientesAfiliacion cuenta in cuentasAfiliar)
            {
                linea = String.Empty;
                foreach (CatArchivosDetalle detalle in LayoutDet)
                {

                    switch (detalle.IdCampo)
                    {
                        case 0:
                            campo = detalle.TextiFijo;
                            break;
                        case 1:
                            campo = cuenta.ClaveId;
                            break;
                        case 3:
                            campo = cuenta.Nombre;
                            break;
                        case 4:
                            campo = cuenta.RFC;
                            break;
                        case 5:
                            campo = cuenta.Telefono;
                            break;
                        
                    }
                    Relleno = detalle.Relleno;
                    longitud = detalle.Longitud;
                    
                    if (campo != null)
                    {
                        longitudCampo = campo.Length;
                    }
                    else
                    {
                        longitudCampo = 0;
                    }

                    if (detalle.IdTipoCampo == "N")
                    {
                        campo = Convert.ToString(decimal.Round(Convert.ToDecimal(campo.ToString()), detalle.Decimales));
                        if (detalle.Incluyepunto == false)
                        {
                            campo.Replace(".", "");

                        }
                        if (detalle.Alineacion == "I")
                        {
                            for (int i = 1; i <= detalle.Longitud-longitudCampo; i++)
                            {
                                campo = campo + Relleno;
                            }
                        }
                        else
                        {
                            for (int i = 1; i <= detalle.Enteros-longitudCampo; i++)
                            {
                                campo = Relleno + campo;
                            }
                        }
                    }
                    if (detalle.IdTipoCampo == "T")
                    {
                        
                        
                        if (detalle.Alineacion == "I")
                        {
                            if (detalle.Longitud > 0)
                            {

                                for (int i = 1; i <= detalle.Longitud - longitudCampo; i++)
                                {
                                    campo = campo + Relleno;
                                }
                            }
                        }
                        else
                        {
                            if (detalle.Longitud > 0)
                            {
                                for (int i = 1; i <= detalle.Longitud - longitudCampo; i++)
                                {
                                    campo = Relleno + campo;
                                }
                            }
                        }
                    }
                    campo = String.Format("{0}\t", campo);
                    linea = linea + campo;
                    
                    
                    

                }
                linea = linea.Substring(0, linea.Length - 1);
                lineas.Add(linea);
                linea = String.Empty;
                foreach (CatArchivosDetalle detalle in LayoutSDet)
                {

                    switch (detalle.IdCampo)
                    {
                        case 0:
                            campo = detalle.TextiFijo;
                            break;
                        case 1:
                            campo = cuenta.ClaveId;
                            break;
                        case 6:
                            campo = cuenta.TipoCuenta;
                            break;
                        case 7:
                            campo = cuenta.Moneda;
                            break;
                        case 8:
                            campo = cuenta.Banco;
                            break;
                        case 9:
                            campo = cuenta.Clabe;
                            break;

                    }
                    Relleno = detalle.Relleno;

                    if (detalle.IdCampo == 9 && cuenta.TipoCuenta == "001")
                    {
                        detalle.Longitud = 10;
                    }
                   
                        longitud = detalle.Longitud;
                   
                    if (campo != null)
                    {
                       
                       
                            longitudCampo = campo.Length;
                       
                    }
                    else
                    {
                        longitudCampo = 0;
                    }

                    if (detalle.IdTipoCampo == "N")
                    {
                        campo = Convert.ToString(decimal.Round(Convert.ToDecimal(campo.ToString()), detalle.Decimales));
                        if (detalle.Incluyepunto == false)
                        {
                            campo.Replace(".", "");

                        }
                        if (detalle.Alineacion == "I")
                        {
                            if (detalle.Longitud > 0)
                            {
                                for (int i = 1; i <= detalle.Longitud - longitudCampo; i++)
                                {
                                    campo = campo + Relleno;
                                }
                            }
                        }
                        else
                        {
                            if (detalle.Longitud > 0)
                            {
                                for (int i = 1; i <= detalle.Enteros - longitudCampo; i++)
                                {
                                    campo = Relleno + campo;
                                }
                            }
                        }
                    }
                    if (detalle.IdTipoCampo == "T")
                    {


                        if (detalle.Alineacion == "I")
                        {
                            if (detalle.Longitud > 0)
                            {
                                for (int i = 1; i <= detalle.Longitud - longitudCampo; i++)
                                {
                                    campo = campo + Relleno;
                                }
                            }
                        }
                        else
                        {
                            if (detalle.Longitud > 0)
                            {
                                for (int i = 1; i <= detalle.Longitud - longitudCampo; i++)
                                {
                                    campo = Relleno + campo;
                                }
                            }
                        }
                    }
                    campo = String.Format("{0}\t", campo);
                    linea = linea + campo;




                }
                linea = linea.Substring(0, linea.Length - 1);
                lineas.Add(linea);
            }
            System.IO.File.WriteAllLines(@"F:\AfiliacionBanorte_SOPF\" + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + ".txt", lineas);
            
            


            MailMessage mail = new MailMessage();
            SmtpClient client = new SmtpClient("smtpout.secureserver.net");
            client.Port = 3535;
            client.EnableSsl = false;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("*****@*****.**", "notifica2013");
            mail.From = new MailAddress("*****@*****.**");
            mail.To.Add(new MailAddress("*****@*****.**"));
            mail.Subject = "Archivo de Afiliacion.";
            mail.Body = "Archivo de Afiliacion";

            Attachment Data = new Attachment(@"F:\AfiliacionBanorte_SOPF\" + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + ".txt");
            mail.Attachments.Add(Data);

            client.Send(mail);

            return Mensaje;
        }
 public static void InsertarCatArchivos(CatArchivos entidad)
 {
     dal.InsertarCatArchivos(entidad);
 }
        public static string GeneraArchivoDispersion(int Accion, int idBanco, int idCorte)
        {
            string Mensaje = "";
            CatArchivos LayoutGral = new CatArchivos();
            List<CatArchivosDetalle> LayoutDet = new List<CatArchivosDetalle>();
            List<CatArchivosDetalle> LayoutSDet = new List<CatArchivosDetalle>();
            List<CuentasDispersion> cuentasDispersar = new List<CuentasDispersion>();

            LayoutGral = ObtenerCatArchivos(idBanco, "T", "S");
            LayoutDet = CatArchivosDetalleBll.ObtenerCatArchivosDetalle(LayoutGral.IdArchivo, "D");
            //LayoutSDet = CatArchivosDetalleBll.ObtenerCatArchivosDetalle(LayoutGral.IdArchivo, "S");

            cuentasDispersar = Creditos.DispersionCuentasBll.ObtenerCuentas(Accion, idBanco, idCorte);
            List<string> lineas = new List<string>();

            string linea = "";
            string campo = "";
            string Relleno = "";
            int longitud = 0;
            int longitudCampo = 0;
            try
            {
                foreach (CuentasDispersion cuenta in cuentasDispersar)
                {
                    linea = String.Empty;
                    foreach (CatArchivosDetalle detalle in LayoutDet)
                    {

                        switch (detalle.IdCampo)
                        {
                            case 0:
                                campo = detalle.TextiFijo;
                                break;
                            case 1:
                                campo = cuenta.ClaveId;
                                break;
                            case 4:
                                campo = cuenta.RFC;
                                break;
                            case 10:
                                campo = cuenta.Operacion;
                                break;
                            case 11:
                                campo = cuenta.CuentaOrigen;
                                break;
                            case 12:
                                campo = cuenta.CuentaDestino;
                                break;
                            case 13:
                                campo = cuenta.MontoTransferencia.ToString();
                                break;
                            case 14:
                                campo = cuenta.Referencia;
                                break;
                            case 15:
                                campo = cuenta.Descripcion;
                                break;
                            case 16:
                                campo = cuenta.RFC;
                                break;
                            case 17:
                                campo = cuenta.iva.ToString();
                                break;
                            case 18:
                                campo = cuenta.fecha;
                                break;
                            case 19:
                                campo = cuenta.instruccion;
                                break;
                        }
                        Relleno = detalle.Relleno;


                        if (detalle.IdCampo == 12 && cuenta.Operacion == "02")
                        {
                            detalle.Longitud = 10;
                        }

                        longitud = detalle.Longitud;

                        if (campo != null)
                        {
                            longitudCampo = campo.Length;
                        }
                        else
                        {
                            longitudCampo = 0;
                        }

                        if (detalle.IdTipoCampo == "N")
                        {
                            campo = Convert.ToString(decimal.Round(Convert.ToDecimal(campo.ToString()), detalle.Decimales));
                            if (detalle.Incluyepunto == false)
                            {
                                campo.Replace(".", "");

                            }
                            if (detalle.Alineacion == "I")
                            {
                                for (int i = 1; i <= detalle.Longitud - longitudCampo; i++)
                                {
                                    campo = campo + Relleno;
                                }
                            }
                            else
                            {
                                for (int i = 1; i <= detalle.Enteros - longitudCampo; i++)
                                {
                                    campo = Relleno + campo;
                                }
                            }
                        }
                        if (detalle.IdTipoCampo == "T")
                        {


                            if (detalle.Alineacion == "I")
                            {
                                if (detalle.Longitud > 0)
                                {

                                    for (int i = 1; i <= detalle.Longitud - longitudCampo; i++)
                                    {
                                        campo = campo + Relleno;
                                    }
                                }
                            }
                            else
                            {
                                if (detalle.Longitud > 0)
                                {
                                    for (int i = 1; i <= detalle.Longitud - longitudCampo; i++)
                                    {
                                        campo = Relleno + campo;
                                    }
                                }
                            }
                        }
                        campo = String.Format("{0}\t", campo);
                        linea = linea + campo;




                    }
                    linea = linea.Substring(0, linea.Length - 1);
                    lineas.Add(linea);
                    linea = String.Empty;


                }
                //System.IO.File.WriteAllLines(@"C:\Users\Juan Carlos\Documents\DispersionBanorte" + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + ".txt", lineas);
                System.IO.File.WriteAllLines(@"F:\AfiliacionBanorte_SOPF\Dispersion" + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + ".txt", lineas);
                Mensaje = "1-Archivo Generado con Exito";

                ArchivosDispersion archivo = new ArchivosDispersion();

                archivo.NombreArchivo = "Dispersion" + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + ".txt";
                archivo.IdCorte = idCorte;
                archivo.Estatus = 0;

                dalA.InsertarArchivosDispersion(archivo);

                MailMessage mail = new MailMessage();
                SmtpClient client = new SmtpClient("smtpout.secureserver.net");
                client.Port = 3535;
                client.EnableSsl = false;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential("*****@*****.**", "notifica2013");
                mail.From = new MailAddress("*****@*****.**");
                mail.To.Add(new MailAddress("*****@*****.**"));
                mail.Subject = "Archivo de Dispersion.";
                mail.Body = "Archivo de Dispersion";

                //Attachment Data = new Attachment(@"C:\Users\Juan Carlos\Documents\DispersionBanorte" + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + ".txt");
                Attachment Data = new Attachment(@"F:\AfiliacionBanorte_SOPF\Dispersion" + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + ".txt");
                mail.Attachments.Add(Data); 

                client.Send(mail);
                //System.IO.File.Delete(@"C:\Users\Juan Carlos\Documents\DispersionBanorte\Dispersion" + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + ".txt");

                return Mensaje;
            }
            catch(Exception ex)
            {
                Mensaje = ex.Message;
                return Mensaje;

            }
        }
        public CatArchivos ObtenerCatArchivos()
        {
			CatArchivos resultado = null;
			try
			{
				//Obtener DbCommand para ejcutar el Store Procedure
				using (DbCommand com = db.GetStoredProcCommand("NombreDelStrore"))
				{
					//Parametros
					//db.AddInParameter(com, "@Parametro", DbType.Tipo, entidad.Atributo);
				
					//Ejecucion de la Consulta
					using (IDataReader reader = db.ExecuteReader(com))
					{
						if (reader != null)
						{
							resultado = new CatArchivos();
							//Lectura de los datos del ResultSet
						}

						reader.Dispose();
					}

					//Cierre de la conexion y liberacion de memoria
					com.Dispose();
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return resultado;
        }
        public CatArchivos ObtenerCatArchivos(int IdBanco,  string TipoTran, string Tipo)
        {
			CatArchivos resultado = null;
			try
			{
				//Obtener DbCommand para ejcutar el Store Procedure
                using (DbCommand com = db.GetStoredProcCommand("Tesoreria.Sel_CatArchivoGral"))
                {
                    //Parametros
                    db.AddInParameter(com, "@IdBanco", DbType.Int32, IdBanco);
                    db.AddInParameter(com, "@TipoTran", DbType.String,TipoTran);
                    db.AddInParameter(com, "@Tipo", DbType.String,Tipo);

                    //Ejecucion de la Consulta
                    using (IDataReader reader = db.ExecuteReader(com))
                    {
                        if (reader != null)
                        {
                            resultado = new CatArchivos();
                            //Lectura de los datos del ResultSet


                            if (reader != null)
                            {

                                while (reader.Read())
                                {

                                    if (!reader.IsDBNull(0)) resultado.IdArchivo = Convert.ToInt32(reader[0]);
                                    if (!reader.IsDBNull(1)) resultado.NombreArchivo = reader[1].ToString();
                                    if (!reader.IsDBNull(2)) resultado.TipoArchivo = reader[2].ToString();
                                    if (!reader.IsDBNull(3)) resultado.Delimitador = reader[3].ToString();
                                    if (!reader.IsDBNull(4)) resultado.FinRegistro = reader[4].ToString();
                                    if (!reader.IsDBNull(5)) resultado.Nombre = reader[5].ToString();
                                    if (!reader.IsDBNull(6)) resultado.Extension = reader[6].ToString();
                                    if (!reader.IsDBNull(7)) resultado.Tipo = reader[7].ToString();
                                    if (!reader.IsDBNull(8)) resultado.Consecutivo = Convert.ToInt32(reader[8]);

                                }


                            }

                            reader.Dispose();
                        }

                        //Cierre de la conexion y liberacion de memoria
                        com.Dispose();
                    }
                }
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return resultado;
        }