private void ShowLastUpdateValue() { dLastUSDValue = 0; // Load latest USD Exchange Rate string xSQL = "declare @xMaxFecha Date=(select MAX(FECHA) Fecha from FORTALEZA_PRD.dbo.CF_VALOR_DOLAR); \n" + "select @xMaxFecha dMaxFecha, VALOR fUSD from FORTALEZA_PRD.dbo.CF_VALOR_DOLAR where FECHA=@xMaxFecha; \n"; if (RSLib_ODBC.ReadSQL(xSQL, false)) { if (!DateTime.TryParse(RSLib_ODBC.GlobalReturnValue[0], out xLastUpdate)) { xLastUpdate = new DateTime(2012, 1, 1); } dLastUSDValue = RSLib_String.AToF(RSLib_ODBC.GlobalReturnValue[1]); lblLastUpdate.Text = "$" + dLastUSDValue.ToString("#,##0.0000") + " al " + xLastUpdate.ToString("dd/MM/yyyy"); lblLastUpdate.Visible = true; } else { RSLib_CriticalError.CriticalErrorHandler("Form Load on RS DOF ExchangeRate", "Err-xxx", "Failure Reading last Update Rate", "Falla leyendo el último Tipo de Cambio", "RS DOF ExhangeRate", xSQL); } }
private void UpdateFromDOF() { string xProvider = "http://dof.gob.mx/indicadores.xml"; WebRequest SendReq = HttpWebRequest.Create(xProvider); WebResponse GetRes = SendReq.GetResponse(); System.IO.Stream StreamRes = GetRes.GetResponseStream(); StreamReader ResStrmRdr = new StreamReader(StreamRes, Encoding.UTF8); string xDOFOriginal = ResStrmRdr.ReadToEnd(); StreamRes.Close(); GetRes.Close(); string xDOF = RSLib_String.MyStringProvider(xDOFOriginal, "<title>DOLAR</title>", "</item>"); xDOF = xDOF.Replace("\n", ""); xDOF = xDOF.Replace("\t", ""); xDOF = xDOF.Replace(" ", ""); string xValue = RSLib_String.MyStringProvider(xDOF, "<description>", "</description>"); string xDate = RSLib_String.MyStringProvider(xDOF, "<valueDate>", "</valueDate>"); double fValue = RSLib_String.AToF(xValue); DateTime xLocalClock = DateTime.Now; string xSQL = ""; if (!DateTime.TryParse(xDate, out xLocalClock) || fValue == 0) { xLocalClock = DateTime.Now; xSQL = "select distinct CONVERT(date,FECHA_FESTIVO) Festivo \n" + "from FORTALEZA_PRD.dbo.CF_DIAS_FESTIVOS \n" + "where FECHA_FESTIVO='" + xLocalClock.ToString(WhatIsDateFormat) + "' \n" + "; \n"; if (!RSLib_ODBC.ReadSQL(xSQL, false)) { if (xLocalClock.DayOfWeek == DayOfWeek.Saturday || xLocalClock.DayOfWeek == DayOfWeek.Sunday) { // IT is Saturday or Sunday - Nothing 2 save, then use the latest fValue = dLastUSDValue; xLocalClock = DateTime.Now; } else { // No Es feriado: Ignore RSLib_CriticalError.CriticalErrorHandler("Form UpdateFromDOF on RS DOF ExchangeRate", "Err-xxx", "Failure Getting Update Rate from DOF", "Falla leyendo el WebService", "RS DOF ExhangeRate", xDOFOriginal); return; } } else { // IT is feriado - Nothing 2 save, then use the latest fValue = dLastUSDValue; xLocalClock = DateTime.Now; } } xSQL = "insert into FORTALEZA_PRD.dbo.CF_VALOR_DOLAR (COD_EMPRESA,FECHA,VALOR) VALUES('00030','" + xLocalClock.ToString(WhatIsDateFormat) + "','" + fValue.ToString() + "'); \n"; if (!RSLib_ODBC.RunSQL(xSQL)) { RSLib_CriticalError.CriticalErrorHandler("Form UpdateFromDOF on RS DOF ExchangeRate", "Err-xxx", "Failure Updating Table", "Falla actualizando tabla", "RS DOF ExhangeRate", xSQL); } else { // Show ShowLastUpdateValue(); // Send eMail Notificaciones string xMessage = "Fecha: \t" + xLocalClock.ToString(WhatIsDateFormat) + "\n" + "Valor Tipo de Cambio: \t" + fValue.ToString("#,##0.0000") + "\n"; RSLib_ODBC.RunLOG("PLD-001", "Exchange Rate Updated -> " + xMessage.Replace("\n", " ").Replace("\t", "-")); string xBody = RSFormaTable(xMessage, "Tipo de Cambio - Sistema PLD", true, true, false, true, true); if (!RSLib_SendMail.SendMailMessage(RSLib_SendMail.SMTP_From, "Oficial.Cumplimiento" + WhatIsDomain, "", "Help.Desk" + WhatIsDomain, "[Tipo de Cambio] " + xLocalClock.ToString(WhatIsDateFormat), xBody, true)) { RSLib_CriticalError.CriticalErrorHandler("Form UpdateFromDOF on RS DOF ExchangeRate", "Err-xxx", "Failure sending email Notificacion", "Falla enviando correo de notificacion", "RS DOF ExhangeRate", ""); } } }