public void Execute(Empresa empresa, string[] conceptosFactura, string[] conceptosAbono, string[] codigosVenta, string[] codigosDevolucion, EventLog log) { int connDocos, dbResponse, fieldResponse; DateTime today = DateTime.Today; DateTime fromDate = today.AddYears(-1); DateTime toDate = today; DateTime dueDate = today; string sFromDate = fromDate.ToString("yyyyMMdd"); Dictionary <int, string> currencies = new Dictionary <int, string>(); Dictionary <int, DimClientes> customers = new Dictionary <int, DimClientes>(); int cancelado = 0, devuelto = 0, impreso = 0, conceptId = 0, companyId = 0, currencyId = 0, idAgente = 0; StringBuilder sbFechaDoc = new StringBuilder(9); StringBuilder sbFechaVto = new StringBuilder(9); string sFechaDoc, sFechaVto, companyCode, currencyName; double saldo = 0, cambio = 0; bool esVenta = false, esDevolucion = false, esCredito = false, esPago = false; connDocos = AdminPaqLib.dbLogIn("", empresa.Ruta); if (connDocos == 0) { throw new Exception("Unable to work with route [" + empresa.Ruta + "]"); } dbResponse = AdminPaqLib.dbGetTopNoLock(connDocos, TABLE_NAME, INDEX); log.WriteEntry("Downloading documents from AdminPaq"); if (dbResponse != 0) { AdminPaqLib.dbLogOut(connDocos); log.WriteEntry("No data found in database, the following exception code was reported: " + dbResponse.ToString() + " on path: " + empresa.Ruta, EventLogEntryType.Warning); throw new Exception("No data found in database, the following exception code was reported: " + dbResponse.ToString()); } int cancelados = 0, devueltos = 0, otros = 0, no_impresos = 0, no_co = 0, no_cliente = 0, valido = 0; while (dbResponse == 0) { esVenta = false; esDevolucion = false; esCredito = false; esPago = false; AdminPaqLib.dbFieldLong(connDocos, TABLE_NAME, 26, ref cancelado); if (cancelado != 0) { dbResponse = AdminPaqLib.dbSkip(connDocos, TABLE_NAME, INDEX, 1); cancelados++; continue; } AdminPaqLib.dbFieldLong(connDocos, TABLE_NAME, 27, ref devuelto); if (devuelto != 0) { dbResponse = AdminPaqLib.dbSkip(connDocos, TABLE_NAME, INDEX, 1); devueltos++; continue; } AdminPaqLib.dbFieldLong(connDocos, TABLE_NAME, 3, ref conceptId); DocumentConcept concept = GetDocumentConcept(conceptId, empresa.Ruta); if (concept == null) { dbResponse = AdminPaqLib.dbSkip(connDocos, TABLE_NAME, INDEX, 1); otros++; continue; } esVenta = codigosVenta != null && codigosVenta.Contains(concept.Code); esDevolucion = codigosDevolucion != null && codigosDevolucion.Contains(concept.Code); esCredito = conceptosFactura.Contains(concept.Code); esPago = conceptosAbono.Contains(concept.Code); if (!esVenta && !esDevolucion && !esCredito && !esPago) { dbResponse = AdminPaqLib.dbSkip(connDocos, TABLE_NAME, INDEX, 1); otros++; continue; } if (esVenta) { AdminPaqLib.dbFieldLong(connDocos, TABLE_NAME, 25, ref impreso); if (impreso == 0) { dbResponse = AdminPaqLib.dbSkip(connDocos, TABLE_NAME, INDEX, 1); no_impresos++; continue; } } if (esCredito) { AdminPaqLib.dbFieldDouble(connDocos, TABLE_NAME, 44, ref saldo); } else { AdminPaqLib.dbFieldDouble(connDocos, TABLE_NAME, 31, ref saldo); } AdminPaqLib.dbFieldLong(connDocos, TABLE_NAME, 15, ref currencyId); if (!currencies.ContainsKey(currencyId)) { currencyName = CurrencyName(currencyId, empresa.Ruta); if (currencyName != null) { currencies.Add(currencyId, currencyName); } } currencyName = currencies[currencyId]; if (!currencyName.ToUpper().Contains("PESO")) { AdminPaqLib.dbFieldDouble(connDocos, TABLE_NAME, 16, ref cambio); saldo = saldo * cambio; } if (esDevolucion) { saldo = saldo * -1; } AdminPaqLib.dbFieldChar(connDocos, TABLE_NAME, 6, sbFechaDoc, 9); sFechaDoc = sbFechaDoc.ToString().Substring(0, 8).Trim(); AdminPaqLib.dbFieldLong(connDocos, TABLE_NAME, 10, ref idAgente); AdminPaqDocument doco = new AdminPaqDocument(); doco.IsCredit = esCredito; doco.IsSale = esVenta || esDevolucion; doco.IsPayment = esPago; doco.Amount = saldo; doco.SellerId = idAgente; doco.DocumentDate = DateTime.ParseExact(sFechaDoc, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None); // SI ES CREDITO NECESITO LA FECHA DE VTO. if (esCredito) { AdminPaqLib.dbFieldChar(connDocos, TABLE_NAME, 11, sbFechaVto, 9); sFechaVto = sbFechaVto.ToString().Substring(0, 8).Trim(); doco.DueDate = DateTime.ParseExact(sFechaVto, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None); // detect client assignment fieldResponse = AdminPaqLib.dbFieldLong(connDocos, TABLE_NAME, 7, ref companyId); if (!customers.ContainsKey(companyId)) { companyCode = GetCompanyCode(companyId, empresa.Ruta); if (companyCode == null) { dbResponse = AdminPaqLib.dbSkip(connDocos, TABLE_NAME, INDEX, 1); no_co++; continue; } DimClientes cliente = DimClientes.GetCliente(empresa.Id, companyCode, null); if (cliente == null) { dbResponse = AdminPaqLib.dbSkip(connDocos, TABLE_NAME, INDEX, 1); no_cliente++; continue; } customers.Add(companyId, cliente); } DimClientes customer = customers[companyId]; doco.Client = customer; } OrganizeDoco(doco, log); valido++; dbResponse = AdminPaqLib.dbSkip(connDocos, TABLE_NAME, INDEX, 1); } log.WriteEntry("Data collection summary: cancelled=" + cancelados + ";returns=" + devueltos + ";other=" + otros + ";not printed=" + no_impresos + ";no_co=" + no_co + ";no_cliente=" + no_cliente + ";valid=" + valido); AdminPaqLib.dbLogOut(connDocos); }