예제 #1
0
    private string Procesar(int nAnomes, string strDatos)
    {
        string        sRes = "OK", sIdFicepi = Session["IDFICEPI_ENTRADA"].ToString(), sNomArchivoZip = "";
        StringBuilder strb = new StringBuilder();

        try
        {
            DataSet ds = NODO.FicherosIAP(null, nAnomes, strDatos);

            #region Crear directorio
            //string path = @"D:\cursos\ficheros";

            //if (!impersonateValidUser("aplicacion_super", @"ibdatdo\aplicacion_super", "@pL1suP3r!"))
            //    return "Error@#@Error en la impersonación";
            //string path = ConfigurationManager.AppSettings["pathFicherosIAP"].ToString();
            string path = Request.PhysicalApplicationPath + "TempImagesGraficos\\FicheroIAP\\" + Session["IDRED"];

            //SUPER.DAL.Log.Insertar("Verificar si el path existe");

            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }
            Directory.CreateDirectory(path);
            #endregion

            #region creacion de ficheros
            StringBuilder result = new StringBuilder();
            foreach (DataRow oProyecto in ds.Tables[0].Rows)//Recorro tabla de proyectos
            {
                for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                {
                    result.Append(oProyecto[i].ToString());
                    result.Append(i == ds.Tables[0].Columns.Count - 1 ? "\r\n" : "\t");
                }
                //result.AppendLine();
            }

            StreamWriter swProyecto = new StreamWriter(path + @"\proyecto.txt", false);
            swProyecto.WriteLine(result.ToString());
            swProyecto.Close();
            //SUPER.DAL.Log.Insertar("Fin tabla Proyectos");

            result.Length = 0;

            foreach (DataRow oTarea in ds.Tables[1].Rows)//Recorro tabla de tarea
            {
                for (int i = 0; i < ds.Tables[1].Columns.Count; i++)
                {
                    result.Append(oTarea[i].ToString());
                    result.Append(i == ds.Tables[1].Columns.Count - 1 ? "\r\n" : "\t");
                }
            }
            StreamWriter swTarea = new StreamWriter(path + @"\tarea.txt", false);
            swTarea.WriteLine(result.ToString());
            swTarea.Close();

            result.Length = 0;

            foreach (DataRow oAtributo in ds.Tables[2].Rows)//Recorro tabla de Atributo
            {
                for (int i = 0; i < ds.Tables[2].Columns.Count; i++)
                {
                    result.Append(oAtributo[i].ToString());
                    result.Append(i == ds.Tables[2].Columns.Count - 1 ? "\r\n" : "\t");
                }
            }

            StreamWriter swAtributo = new StreamWriter(path + @"\atributo.txt", false);
            swAtributo.WriteLine(result.ToString());
            swAtributo.Close();

            result.Length = 0;

            foreach (DataRow oConsumo in ds.Tables[3].Rows)//Recorro tabla de Consumo
            {
                for (int i = 0; i < ds.Tables[3].Columns.Count; i++)
                {
                    result.Append(oConsumo[i].ToString());
                    result.Append(i == ds.Tables[3].Columns.Count - 1 ? "\r\n" : "\t");
                }
            }

            StreamWriter swConsumo = new StreamWriter(path + @"\consumo.txt", false);
            swConsumo.WriteLine(result.ToString());
            swConsumo.Close();

            result.Length = 0;

            foreach (DataRow oCliente in ds.Tables[4].Rows)//Recorro tabla de Cliente
            {
                for (int i = 0; i < ds.Tables[4].Columns.Count; i++)
                {
                    result.Append(oCliente[i].ToString());
                    result.Append(i == ds.Tables[4].Columns.Count - 1 ? "\r\n" : "\t");
                }
            }

            StreamWriter swCliente = new StreamWriter(path + @"\cliente.txt", false);
            swCliente.WriteLine(result.ToString());
            swCliente.Close();

            result.Length = 0;

            foreach (DataRow oEmpleado in ds.Tables[5].Rows)//Recorro tabla de Empleado
            {
                for (int i = 0; i < ds.Tables[5].Columns.Count; i++)
                {
                    result.Append(oEmpleado[i].ToString());
                    result.Append(i == ds.Tables[5].Columns.Count - 1 ? "\r\n" : "\t");
                }
            }

            StreamWriter swEmpleado = new StreamWriter(path + @"\empleado.txt", false);
            swEmpleado.WriteLine(result.ToString());
            swEmpleado.Close();

            result.Length = 0;

            foreach (DataRow oOrdenFact in ds.Tables[6].Rows)//Recorro tabla de Ordenes de facturación
            {
                for (int i = 0; i < ds.Tables[6].Columns.Count; i++)
                {
                    result.Append(oOrdenFact[i].ToString());
                    result.Append(i == ds.Tables[6].Columns.Count - 1 ? "\r\n" : "\t");
                }
            }

            StreamWriter swOrdenFact = new StreamWriter(path + @"\ordenesFacturacion.txt", false);
            swOrdenFact.WriteLine(result.ToString());
            swOrdenFact.Close();
            #endregion

            #region Genero un zip con todos los archivos de la carpeta
            sNomArchivoZip = CompressFile(path);
            FileInfo fInfo = new FileInfo(sNomArchivoZip);
            long     lTamFich = fInfo.Length;
            long     iTamEnBytes = fInfo.Length, TamMaxPermitido = 104857600;//100Mb
            string   sTamMax = ConfigurationManager.AppSettings["TamMaxPack"];
            if (sTamMax != "")
            {
                TamMaxPermitido = long.Parse(sTamMax) * 1024 * 1024;//Paso de Mb a bytes
            }
            if (iTamEnBytes > TamMaxPermitido)
            {
                return("TAMANO_EXCEDIDO@#@" + iTamEnBytes.ToString());
            }
            #endregion

            #region Envío los ficheros al usuario
            //Si el fichero < 10Mb -> envío por correo sino por PaqExpress
            #region Establezco el tamaño del archivo
            double     dTamMax     = 10;
            bool       bPaqExpress = false;
            FileStream fsFichero   = System.IO.File.OpenRead(sNomArchivoZip);
            if ((fsFichero.Length / 1048576) > dTamMax)
            {//Si el resultado es > 10Mb enviarlo por PaqExpress
                bPaqExpress = true;
            }
            #endregion

            if (bPaqExpress)
            {
                #region Envío por PaqExpress
                svcSendPack.SendPackClient oPaq = new svcSendPack.SendPackClient();
                try
                {
                    strb.Append("<Pack>");
                    strb.Append("<User>PAQEXPRESS</User>");
                    strb.Append("<Clave>XRJ001-WCF-SUPER-CV.</Clave>");
                    strb.Append("<IdFicepi>" + sIdFicepi + "</IdFicepi>");
                    strb.Append("<FPedido>" + DateTime.Now.ToString() + "</FPedido>");
                    strb.Append("<Profesionales></Profesionales>");
                    strb.Append("<Obs>Paquete de ficheros para IAP</Obs>");
                    strb.Append("<Ref></Ref>");//Nº de referencia para tracking
                    strb.Append("</Pack>");
                    //oPaq.CrearPaqueteCV(new FileInfo(nombreDoc + extension).Name, strb.ToString(), File.OpenRead(pathDirectory + trackingId + @"\" + nombreDoc + extension));
                    FileStream fAux     = File.OpenRead(sNomArchivoZip);
                    string     sAuxName = new FileInfo(sNomArchivoZip).Name;
                    oPaq.CrearPaqueteCV(sAuxName, strb.ToString(), fAux);
                }
                catch (FaultException <svcSendPack.PackException> cex)
                {
                    string sError = "Error: Código:" + cex.Detail.ErrorCode + ". Descripción: " + cex.Detail.Message;// +" " + cex.Detail.InnerMessage;
                    if (cex.Detail.InnerMessage != "")
                    {
                        sError += "\r\nInnerMessage: " + cex.Detail.InnerMessage;
                    }
                    sRes = sError + "\r\n";
                }
                catch (Exception ex)
                {
                    sRes = ex.Message;
                }
                finally
                {
                    //Cierre del canal
                    if (oPaq != null && oPaq.State != System.ServiceModel.CommunicationState.Closed)
                    {
                        if (oPaq.State != System.ServiceModel.CommunicationState.Faulted)
                        {
                            oPaq.Close();
                        }
                        else if (oPaq.State != System.ServiceModel.CommunicationState.Closed)
                        {
                            oPaq.Abort();
                        }
                    }
                }
                #endregion
            }
            else
            {
                #region Envío por correo
                string    strAsunto     = "Ficheros generados para IAP";
                string    strMensaje    = "Se adjunta archivo comprimido que contiene los archivos generados para IAP";
                string    sDestinatario = Session["IDRED"].ToString();
                string[]  aMail         = { strAsunto, strMensaje, sDestinatario, sNomArchivoZip };
                ArrayList aListCorreo   = new ArrayList();
                aListCorreo.Add(aMail);
                SUPER.Capa_Negocio.Correo.EnviarCorreosCita(aListCorreo);
                #endregion
            }
            #endregion

            return(sRes + "@#@" + path);
        }
        catch (Exception ex)
        {
            return("Error@#@" + Errores.mostrarError("Error al obtener los datos.", ex));
        }
    }