private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { bool blnResult = false; BackgroundWorker bw = sender as BackgroundWorker; listACM = null; listADM = null; if (!string.IsNullOrEmpty(txtFilePath_ACM.Text)) { ACM_ReadPdfFile(txtFilePath_ACM.Text); } if (!string.IsNullOrEmpty(txtFilePath_ADM.Text)) { ADM_ReadPdfFile(txtFilePath_ADM.Text); } BSP_ReadPdfFile(txtFilePath_BSP.Text); if ((bool)e.Argument) { BSPActions.Guardar(semanaToImport, backgroundWorker1); } blnResult = true; e.Result = blnResult; }
public void BSP_ReadPdfFile_Cabecera(string fileName) { int page = 0, index = 0; string currentText = ""; int paginaInicial = 0; int IDAgencia = 0; Agencia oAgencia = null; Semana oSemanaAux = null; try { pageStart = 0; semanaToImport = null; if (File.Exists(fileName)) { PdfReader pdfReader = new PdfReader(fileName); Agencias agencias = new Agencias(); for (page = 1; page <= pdfReader.NumberOfPages && paginaInicial == 0; page++) { currentText = ""; currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, new SimpleTextExtractionStrategy()); currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText))); string[] arrLineas = currentText.Split(new char[] { '\n' }); if (BSPActions.EsAnalisisDeVenta(ref arrLineas)) { string Linea4 = arrLineas[4]; string strAgencia = Linea4.Substring(Linea4.IndexOf("LIQUIDACION DE :") + ("LIQUIDACION DE :").Length, 70).Trim(); if (int.TryParse(strAgencia.Substring(0, strAgencia.IndexOf(" ") + 2).Replace(" ", ""), out IDAgencia)) { oAgencia = agencias.GetByID(IDAgencia); if (oAgencia == null) { oAgencia = new Agencia { ID = IDAgencia, Nombre = strAgencia.Substring(11) }; agencias.Insertar(oAgencia); } string Linea2 = arrLineas[2]; string Linea3 = arrLineas[3]; oSemanaAux = new Semana { Periodo = Convert.ToDateTime(Linea2.Substring(Linea2.LastIndexOf("PERIODO: ") + ("PERIODO: ").Length, 10).Trim()), FechaDesde = Convert.ToDateTime(Linea3.Substring(Linea3.LastIndexOf("DEL: ") + ("DEL: ").Length, 8).Trim()), FechaHasta = Convert.ToDateTime(Linea3.Substring(Linea3.LastIndexOf("AL ") + ("AL ").Length, 8).Trim()), Agencia = oAgencia }; if (Publics.Semana != null && Publics.Semana.Agencia.ID == oSemanaAux.Agencia.ID && Publics.Semana.Periodo == oSemanaAux.Periodo) { oSemanaAux.ID = Publics.Semana.ID; oSemanaAux.BSPCargado = Publics.Semana.BSPCargado; oSemanaAux.BOCargado = Publics.Semana.BOCargado; oSemanaAux.TicketsBO = Publics.Semana.TicketsBO; } else { Semanas semanas = new Semanas(); Semana oSemanaVerif = semanas.GetByAgenciaPeriodo(oSemanaAux.Agencia.ID, oSemanaAux.Periodo); semanas.CloseConnection(); if (oSemanaVerif != null) { oSemanaAux.ID = oSemanaVerif.ID; oSemanaAux.BSPCargado = oSemanaVerif.BSPCargado; if (oSemanaVerif.BOCargado) { oSemanaAux.BOCargado = true; BO_Tickets BO_Tickets = new BO_Tickets(); oSemanaAux.TicketsBO = BO_Tickets.ObtenerPorSemana(oSemanaAux.ID); BO_Tickets.CloseConnection(); } } } paginaInicial = page; } } } agencias.CloseConnection(); } } catch (Exception Exception1) { TextToFile.Errores(TextToFile.Error(Exception1)); MessageBox.Show("Error: " + Exception1.Message + "\nfileName: " + fileName + "\npage: " + page + "\nline: " + index, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } //Semana actual: Semana del archivo seleccionado. semanaToImport = oSemanaAux; pageStart = paginaInicial; }
public void BSP_ReadPdfFile(string fileName) { int page = 0, index = 0; string currentText = ""; BSP_Ticket oBSP_Ticket = null; BSP_Ticket_Detalle oBSP_Ticket_Detalle = null; Companias companias = new Companias(); List <Compania> lstCompanias = companias.GetAll(); Compania oCompaniaActual = null; string strFinCompania = ""; List <Compania> lstNuevasCompanias = new List <Compania>(); Conceptos conceptos = new Conceptos(); List <Concepto> lstConceptos = conceptos.GetAll(); Concepto oConceptoActual = null; string strFinConcepto = ""; BSP_Rg Tipo = BSP_Rg.Ambas; int CompaniaID = 0; string Linea = ""; try { if (File.Exists(fileName)) { semanaToImport.TicketsBSP = new List <BSP_Ticket>(); PdfReader pdfReader = new PdfReader(fileName); for (page = pageStart; page <= pdfReader.NumberOfPages; page++) { currentText = ""; currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, new SimpleTextExtractionStrategy()); currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText))); string[] arrLineas = currentText.Split(new char[] { '\n' }); if (BSPActions.EsAnalisisDeVenta(ref arrLineas)) { string strTipo = arrLineas[3].Substring(21, 30).Trim(); Tipo = BSP_Rg.Ambas; if (strTipo == "DOMESTICO") { Tipo = BSP_Rg.Doméstico; } else if (strTipo == "INTERNACIONAL") { Tipo = BSP_Rg.Internacional; } if (Tipo != BSP_Rg.Ambas) //Si no estoy en el resumen final { for (index = 10; index < arrLineas.Length; index++) //10: Salteo cabecera { Linea = arrLineas[index]; if (Linea.Trim() != "") { if (oCompaniaActual == null && index == 10 && Linea.Length >= 3 && int.TryParse(Linea.Substring(0, 3), out CompaniaID)) //Si no estoy dentro de una compañía aerea... { oCompaniaActual = lstCompanias.Find(x => x.ID == CompaniaID); if (oCompaniaActual == null) { oCompaniaActual = new Compania() { ID = CompaniaID, Nombre = Linea.Substring(4).Trim() }; companias.Insertar(oCompaniaActual); lstCompanias.Add(oCompaniaActual); lstNuevasCompanias.Add(oCompaniaActual); } strFinCompania = "TOT " + Linea.Substring(4).Trim(); } else if (Linea.Length >= strFinCompania.Length && Linea.Substring(0, strFinCompania.Length) == strFinCompania) //Si estoy en el cierre de la compañía aerea... { oCompaniaActual = null; } else if (oConceptoActual == null) //Si estoy en la compañía aerea y no estoy dentro de un concepto... { oConceptoActual = lstConceptos.Find(x => x.Nombre.Length <= Linea.Length && x.Nombre.ToUpper() == Linea.Substring(0, x.Nombre.Length)); if (oConceptoActual != null) { strFinConcepto = "TOT " + (oConceptoActual.Nombre.Length >= 21 ? oConceptoActual.Nombre.Substring(0, 21) : oConceptoActual.Nombre).Trim().ToUpper(); } } else if (Linea.Length >= strFinConcepto.Length && Linea.Substring(0, strFinConcepto.Length) == strFinConcepto) //Si estoy en la compañía aerea y estoy en el cierre de un concepto { oConceptoActual = null; } else if (BSPActions.EsNuevoTicket(Linea)) //Si estoy en la compañía aerea y estoy en el concepto, y si los primeros 10 caracteres son long... { if (oBSP_Ticket != null) { semanaToImport.TicketsBSP.Add(oBSP_Ticket); oBSP_Ticket = null; } oBSP_Ticket = BSPActions.GetTicket(Linea, semanaToImport.Periodo.Year); oBSP_Ticket.Concepto = oConceptoActual; oBSP_Ticket.Compania = oCompaniaActual; oBSP_Ticket.Rg = Tipo; oBSP_Ticket_Detalle = BSPActions.GetTicketDetalle(Linea); //ACM y ADM if (listACM != null && oConceptoActual.Tipo == 'C') { ACM oACM = listACM.Find(x => x.Billete == oBSP_Ticket.Billete); if (oACM != null) { oBSP_Ticket_Detalle.Observaciones += (string.IsNullOrEmpty(oBSP_Ticket_Detalle.Observaciones) ? "" : "|") + oACM.Observaciones; } } if (listADM != null && oConceptoActual.Tipo == 'D') { ADM oADM = listADM.Find(x => x.Billete == oBSP_Ticket.Billete); if (oADM != null) { oBSP_Ticket_Detalle.Observaciones += (string.IsNullOrEmpty(oBSP_Ticket_Detalle.Observaciones) ? "" : "|") + oADM.Observaciones; } } oBSP_Ticket.Detalle.Add(oBSP_Ticket_Detalle); oBSP_Ticket_Detalle = null; } else if (oBSP_Ticket != null) { oBSP_Ticket_Detalle = BSPActions.GetTicketDetalle(Linea); oBSP_Ticket.Detalle.Add(oBSP_Ticket_Detalle); oBSP_Ticket_Detalle = null; } } } } else { Linea = arrLineas[10]; string strMoneda = Linea.Substring(("MONEDA: ").Length, 3); Moneda Moneda = strMoneda == "ARS" ? Moneda.Peso : Moneda.Dolar; foreach (BSP_Ticket bsp_ticket in semanaToImport.TicketsBSP.Where(x => x.Moneda == null)) { bsp_ticket.Moneda = Moneda; } if (oBSP_Ticket != null) { oBSP_Ticket.Moneda = Moneda; } } } } if (oBSP_Ticket != null) { semanaToImport.TicketsBSP.Add(oBSP_Ticket); oBSP_Ticket = null; } semanaToImport.TicketsBSP = semanaToImport.TicketsBSP.OrderBy(x => x.Compania.Codigo).ThenBy(x => x.Billete).ToList(); semanaToImport.BSPCargado = true; pdfReader.Close(); if (lstNuevasCompanias.Count > 0) { string mensaje = "Se han encontrado Compañías Aéreas que no estaban registradas en el sistema:\n\n"; lstNuevasCompanias.ForEach(x => mensaje += "ID: " + x.ID + " | Nombre: " + x.Nombre + "\n"); mensaje += "\nÉstas han sido guardadas automáticamente, pero el código alfanumérico deberá ser ingresado manualmente.\n"; mensaje += "Por favor, ingrese al \"ABM de Compañías\" y complete la información solicitada.\n"; mensaje += "Muchas gracias."; MessageBox.Show(mensaje, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } catch (Exception Exception1) { TextToFile.Errores(TextToFile.Error(Exception1)); MessageBox.Show("Error: " + Exception1.Message + "\nfileName: " + fileName + "\npage: " + page + "\nline: " + index, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { companias.CloseConnection(); conceptos.CloseConnection(); } }
public frmImportarBSP() { InitializeComponent(); BSPActions = new BSPActions(Application.CurrentCulture); }