705 lines
28 KiB
Plaintext
705 lines
28 KiB
Plaintext
<%@ WebHandler Language="C#" Class="TareasJam" %>
|
|
|
|
using System;
|
|
using System.Web;
|
|
using Extensiones;
|
|
using System.Net;
|
|
using System.IO;
|
|
using System.Web.Services;
|
|
using System.Web.Services.Protocols;
|
|
using GemBox.Spreadsheet;
|
|
using System.Data;
|
|
using System.Text;
|
|
using System.Collections.Generic;
|
|
using ClosedXML;
|
|
|
|
public class TareasJam : IHttpHandler
|
|
{
|
|
|
|
public void ProcessRequest(HttpContext context)
|
|
{
|
|
context.Response.ContentType = "text/plain";
|
|
//context.Response.Write("Hello World");
|
|
bool retorno = false;
|
|
string Metodo = context.Request["Metodo"] ==null?"": context.Request["Metodo"];
|
|
int CveEmpresa =context.Request["CveEmpresa"] ==null?0: context.Request["CveEmpresa"].ToEntero();
|
|
switch (Metodo)
|
|
{
|
|
case "TestConexion":
|
|
retorno = PruebaConexionFTP("ftp://ftp.thalamuslive.com", "agrojam", "A12345678");
|
|
break;
|
|
case "EjecutaThalamus":
|
|
|
|
retorno = EjecutaThalamus(CveEmpresa, context);
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
if (retorno)
|
|
{
|
|
context.Response.Write("True");
|
|
|
|
}else
|
|
{
|
|
context.Response.Write("False");
|
|
|
|
}
|
|
context.Response.End();
|
|
|
|
}
|
|
private bool EjecutaThalamus(int CveEmpresa, HttpContext context)
|
|
{
|
|
|
|
|
|
|
|
|
|
this.GuardaLog("EjecutaThalamus: Inicio. ");
|
|
|
|
bool Respuesta = false;
|
|
StringBuilder SBLog = new StringBuilder();
|
|
|
|
ClsGenerales ObjGenerales = new ClsGenerales();
|
|
|
|
DataTable DTProductos = new DataTable();
|
|
DataTable DTVentas = new DataTable();
|
|
DataTable DTClientes = new DataTable();
|
|
DataTable DTAlmacenes = new DataTable();
|
|
|
|
string RutaProductos = $@"{ HttpContext.Current.Server.MapPath("~") }Temp\Productos{ClsPaginas.NowADS().ToString("yyyyMMdd")}.xlsx";
|
|
string RutaVentas = $@"{ HttpContext.Current.Server.MapPath("~") }Temp\Ventas{ClsPaginas.NowADS().ToString("yyyyMMdd")}.xlsx";
|
|
string RutaClientes = $@"{ HttpContext.Current.Server.MapPath("~") }Temp\Clientes{ClsPaginas.NowADS().ToString("yyyyMMdd")}.xlsx";
|
|
string RutaAlmacenes = $@"{ HttpContext.Current.Server.MapPath("~") }Temp\Almacenes{ClsPaginas.NowADS().ToString("yyyyMMdd")}.xlsx";
|
|
|
|
|
|
string FormatoVentas = $@"{ HttpContext.Current.Server.MapPath("~") }\TareasProgramadas\FormatosThalamus\FormatoVentas.xlsx";
|
|
string FormatoClientes = $@"{ HttpContext.Current.Server.MapPath("~") }\TareasProgramadas\FormatosThalamus\FormatoClientes.xlsx";
|
|
string FormatoAlmacenes = $@"{ HttpContext.Current.Server.MapPath("~") }\TareasProgramadas\FormatosThalamus\FormatoAlmacenes.xlsx";
|
|
string FormatoProductos = $@"{ HttpContext.Current.Server.MapPath("~") }\TareasProgramadas\FormatosThalamus\FormatoProductos.xlsx";
|
|
|
|
|
|
try
|
|
{
|
|
if (File.Exists(RutaProductos))
|
|
{
|
|
File.Delete(RutaProductos);
|
|
}
|
|
|
|
if (File.Exists(RutaVentas))
|
|
{
|
|
File.Delete(RutaVentas);
|
|
}
|
|
|
|
if (File.Exists(RutaClientes))
|
|
{
|
|
File.Delete(RutaClientes);
|
|
}
|
|
|
|
if (File.Exists(RutaAlmacenes))
|
|
{
|
|
File.Delete(RutaAlmacenes);
|
|
}
|
|
|
|
}
|
|
catch (Exception ex) {
|
|
SBLog.AppendLine("<b>Error al eliminar archivos</b>");
|
|
this.GuardaLog("EjecutaThalamus: Error Eliminado arrchivo " + ex.Message + " ");
|
|
}
|
|
|
|
|
|
//string FTPServer = "ftp://127.0.0.1/";
|
|
//string TFPUsr = "Toor";
|
|
//string FTPPass = "agj-3500";
|
|
|
|
|
|
string FTPServer = "ftp://ftp.thalamuslive.com";
|
|
string TFPUsr = "agrojam";
|
|
string FTPPass = "A12345678";
|
|
|
|
bool ErrorProceso = false;
|
|
|
|
SBLog.AppendLine("<b>Consultas</b><br>");
|
|
SBLog.AppendLine("<table border='1'><tr><th>Consulta</th><th>Registros</th></tr>");
|
|
|
|
|
|
|
|
this.GuardaLog("EjecutaThalamus: Consultando Prodcutos. ");
|
|
if (ObjGenerales.GetJAMCorveta(1))
|
|
{
|
|
DTProductos = ObjGenerales.DtResultados.Copy();
|
|
SBLog.AppendLine($"<tr><td>Productos</td><td>{DTProductos.Rows.Count}</td></tr>");
|
|
this.GuardaLog($"EjecutaThalamus: Consultando Prodcutos correcto {DTProductos.Rows.Count} registros. ");
|
|
}
|
|
else {
|
|
ErrorProceso = true;
|
|
SBLog.AppendLine($"<tr><td>Productos</td><td>Error al consultar</td></tr>");
|
|
this.GuardaLog($"EjecutaThalamus: Consultando Prodcutos Error. ");
|
|
}
|
|
|
|
this.GuardaLog($"EjecutaThalamus: Consultando Ventas. ");
|
|
if (ObjGenerales.GetJAMCorveta(2) & ErrorProceso == false)
|
|
{
|
|
DTVentas = ObjGenerales.DtResultados.Copy();
|
|
SBLog.AppendLine($"<tr>Ventas<td></td><td>{DTVentas.Rows.Count}</td></tr>");
|
|
this.GuardaLog($"EjecutaThalamus: Consultando Ventas correcto {DTVentas.Rows.Count} registros. ");
|
|
}
|
|
else
|
|
{
|
|
ErrorProceso = true;
|
|
SBLog.AppendLine($"<tr><td>Ventas</td><td>Error al consultar</td></tr>");
|
|
this.GuardaLog($"EjecutaThalamus: Consultando Ventas Error. ");
|
|
}
|
|
|
|
this.GuardaLog($"EjecutaThalamus: Consultando Clientes. ");
|
|
if (ObjGenerales.GetJAMCorveta(3) & ErrorProceso == false)
|
|
{
|
|
DTClientes = ObjGenerales.DtResultados.Copy();
|
|
SBLog.AppendLine($"<tr><td>Clientes</td><td>{DTClientes.Rows.Count}</td></tr>");
|
|
this.GuardaLog($"EjecutaThalamus: Consultando Clientes correco {DTClientes.Rows.Count} registros. ");
|
|
|
|
}
|
|
else {
|
|
ErrorProceso = true;
|
|
SBLog.AppendLine($"<tr><td>Clientes</td><td>Error al consultar</td></tr>");
|
|
this.GuardaLog($"EjecutaThalamus: Consultando Clientes Error. ");
|
|
}
|
|
|
|
this.GuardaLog($"EjecutaThalamus: Consultando Almacenes. ");
|
|
if (ObjGenerales.GetJAMCorveta(4) & ErrorProceso == false)
|
|
{
|
|
DTAlmacenes = ObjGenerales.DtResultados.Copy();
|
|
SBLog.AppendLine($"<tr><td>Almacenes</td><td>{DTAlmacenes.Rows.Count}</td></tr>");
|
|
this.GuardaLog($"EjecutaThalamus: Consultando Almacenes Correcto {DTAlmacenes.Rows.Count} registros. ");
|
|
}
|
|
else {
|
|
ErrorProceso = true;
|
|
SBLog.AppendLine($"<tr><td>Almacenes</td><td>Error al consultar</td></tr>");
|
|
this.GuardaLog($"EjecutaThalamus: Consultando Almacenes Error. ");
|
|
}
|
|
|
|
SBLog.AppendLine("</table>");
|
|
SBLog.AppendLine("<br>");
|
|
|
|
ObjGenerales.Dispose();
|
|
|
|
if (!ErrorProceso)
|
|
{
|
|
|
|
try
|
|
{
|
|
|
|
this.GuardaLog($"EjecutaThalamus: Renombrando columnas tabla productos. ");
|
|
DTProductos.Columns[0].ColumnName = "Código Producto Interno";
|
|
DTProductos.Columns[1].ColumnName = "Nombre del Producto";
|
|
DTProductos.Columns[2].ColumnName = "Presentación";
|
|
DTProductos.AcceptChanges();
|
|
|
|
|
|
this.GuardaLog($"EjecutaThalamus: Renombrando columnas tabla clientes. ");
|
|
DTClientes.Columns[4].ColumnName = "address.countryId";
|
|
DTClientes.Columns[5].ColumnName = "address.stateId";
|
|
DTClientes.Columns[6].ColumnName = "address.city";
|
|
DTClientes.Columns[7].ColumnName = "address.street1";
|
|
DTClientes.Columns[8].ColumnName = "address.street2";
|
|
DTClientes.Columns[9].ColumnName = "address.postalCode";
|
|
DTClientes.Columns[10].ColumnName = "phone.intCode";
|
|
DTClientes.Columns[11].ColumnName = "phone.areaCode";
|
|
DTClientes.Columns[12].ColumnName = "phone.number";
|
|
|
|
//DTClientes.Columns.RemoveAt(12);
|
|
//DTClientes.Columns.RemoveAt(11);
|
|
//DTClientes.Columns.RemoveAt(10);
|
|
//DTClientes.Columns.RemoveAt(8);
|
|
//DTClientes.Columns.RemoveAt(7);
|
|
DTClientes.AcceptChanges();
|
|
|
|
|
|
this.GuardaLog($"EjecutaThalamus: Renombrando columnas tabla ventas. ");
|
|
DTVentas.Columns.RemoveAt(7);
|
|
//DTVentas.Columns.RemoveAt(6);
|
|
|
|
DTVentas.Columns[3].ColumnName = "Sale Date";
|
|
DTVentas.Columns[6].ColumnName = "Transaction ID";
|
|
DTVentas.AcceptChanges();
|
|
|
|
|
|
this.GuardaLog($"EjecutaThalamus: Renombrando columnas tabla almacenes. ");
|
|
DTAlmacenes.Columns[2].ColumnName = "Description";
|
|
DTAlmacenes.Columns[3].ColumnName = "Stock Date";
|
|
DTAlmacenes.AcceptChanges();
|
|
|
|
|
|
|
|
this.GuardaLog($"EjecutaThalamus: procesando tabla de ventas DTVentas.Clone() para asiganación de tipo de dato fecha. ");
|
|
DataTable dtCloned = DTVentas.Clone();
|
|
dtCloned.Columns[3].DataType = typeof(DateTime);
|
|
|
|
foreach (DataRow row in DTVentas.Rows)
|
|
{
|
|
dtCloned.ImportRow(row);
|
|
}
|
|
|
|
DTVentas = dtCloned;
|
|
foreach (DataRow dRow in DTVentas.Rows)
|
|
{
|
|
DateTime tmpDate = dRow[3].ToString().ToDate().ToString("yyyy/MM/dd").ToDate();
|
|
dRow[3] = tmpDate;
|
|
}
|
|
|
|
DTVentas.AcceptChanges();
|
|
|
|
this.GuardaLog($"EjecutaThalamus: procesando tabla de ventas DTAlmacenes.Clone() para asiganación de tipo de dato fecha. ");
|
|
dtCloned = DTAlmacenes.Clone();
|
|
dtCloned.Columns[3].DataType = typeof(DateTime);
|
|
|
|
foreach (DataRow row in DTAlmacenes.Rows)
|
|
{
|
|
dtCloned.ImportRow(row);
|
|
}
|
|
|
|
DTAlmacenes = dtCloned;
|
|
foreach (DataRow dRow in DTAlmacenes.Rows)
|
|
{
|
|
DateTime tmpDate = dRow[3].ToString().ToDate().ToString("yyyy/MM/dd").ToDate();
|
|
dRow[3] = tmpDate;
|
|
}
|
|
DTAlmacenes.AcceptChanges();
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
SBLog.AppendLine("<b>Error al renombrar columnas</b>");
|
|
this.GuardaLog($"EjecutaThalamus: Error procesamiento de tablas " + e.StackTrace.ToString() + " ");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!ErrorProceso)
|
|
{
|
|
|
|
try
|
|
{
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
this.GuardaLog($"EjecutaThalamus: llenando archivo excel ventas. ");
|
|
int count = 2;
|
|
var wbVentas = new ClosedXML.Excel.XLWorkbook(FormatoVentas);
|
|
this.GuardaLog($"EjecutaThalamus: archivo excel ventas cargado. ");
|
|
this.GuardaLog($"EjecutaThalamus: archivo excel ventas creando sheet.");
|
|
var wsVentas = wbVentas.Worksheet(1);
|
|
this.GuardaLog($"EjecutaThalamus: archivo excel ventas sheet creada.");
|
|
|
|
foreach (DataRow DRow in DTVentas.Rows)
|
|
{
|
|
wsVentas.Cell(count, 1).Value = "'" + DRow["systemId"].ToString();
|
|
wsVentas.Cell(count, 2).Value = DRow["Branch"].ToString();
|
|
wsVentas.Cell(count, 3).Value = "'" + DRow["Code"].ToString();
|
|
wsVentas.Cell(count, 4).Value = DRow["Sale Date"].ToString();
|
|
wsVentas.Cell(count, 5).Value = DRow["Quantity"].ToString();
|
|
wsVentas.Cell(count, 6).Value = DRow["Amount"].ToString();
|
|
wsVentas.Cell(count, 7).Value = DRow["Transaction ID"].ToString();
|
|
count++;
|
|
}
|
|
this.GuardaLog($"EjecutaThalamus: guardando archivo excel ventas " + RutaVentas + " ");
|
|
wbVentas.SaveAs(RutaVentas);
|
|
|
|
var wbAlmacenes = new ClosedXML.Excel.XLWorkbook(FormatoAlmacenes);
|
|
var wsAlmacenes = wbAlmacenes.Worksheet(1);
|
|
|
|
count = 2;
|
|
|
|
this.GuardaLog($"EjecutaThalamus: llenando archivo excel almacenes. ");
|
|
foreach (DataRow DRow in DTAlmacenes.Rows)
|
|
{
|
|
wsAlmacenes.Cell(count, 1).Value = "'" + DRow["Code"].ToString();
|
|
wsAlmacenes.Cell(count, 2).Value = DRow["Quantity"].ToString();
|
|
wsAlmacenes.Cell(count, 3).Value = DRow["Description"].ToString();
|
|
wsAlmacenes.Cell(count, 4).Value = DRow["Stock Date"].ToString();
|
|
count++;
|
|
}
|
|
this.GuardaLog($"EjecutaThalamus: guardando archivo excel alamcenes " + RutaAlmacenes + " ");
|
|
wbAlmacenes.SaveAs(RutaAlmacenes);
|
|
|
|
var wbAClientes = new ClosedXML.Excel.XLWorkbook(FormatoClientes);
|
|
var wsClientes = wbAClientes.Worksheet(1);
|
|
|
|
count = 2;
|
|
|
|
this.GuardaLog($"EjecutaThalamus: llenando archivo excel clientes. ");
|
|
foreach (DataRow DRow in DTClientes.Rows)
|
|
{
|
|
wsClientes.Cell(count, 1).Value = "'" + DRow["systemId"].ToString();
|
|
wsClientes.Cell(count, 2).Value = DRow["fiscalIdMex"].ToString();
|
|
wsClientes.Cell(count, 3).Value = DRow["fiscalName"].ToString();
|
|
wsClientes.Cell(count, 4).Value = DRow["email"].ToString();
|
|
wsClientes.Cell(count, 5).Value = DRow["address.countryId"].ToString();
|
|
wsClientes.Cell(count, 6).Value = DRow["address.stateId"].ToString();
|
|
wsClientes.Cell(count, 7).Value = DRow["address.city"].ToString();
|
|
wsClientes.Cell(count, 8).Value = DRow["address.street1"].ToString();
|
|
wsClientes.Cell(count, 9).Value = DRow["address.street2"].ToString();
|
|
wsClientes.Cell(count, 10).Value = "'" + DRow["address.postalCode"].ToString();
|
|
wsClientes.Cell(count, 11).Value = DRow["phone.intCode"].ToString();
|
|
wsClientes.Cell(count, 12).Value = DRow["phone.areaCode"].ToString();
|
|
wsClientes.Cell(count, 13).Value = "'" + DRow["phone.number"].ToString();
|
|
wsClientes.Cell(count, 14).Value = DRow["salesman"].ToString();
|
|
|
|
count++;
|
|
}
|
|
|
|
this.GuardaLog($"EjecutaThalamus: guardando archivo excel clientes " + RutaClientes + " ");
|
|
wbAClientes.SaveAs(RutaClientes);
|
|
|
|
var wbProductos = new ClosedXML.Excel.XLWorkbook(FormatoProductos);
|
|
var wsProductos = wbProductos.Worksheet(1);
|
|
|
|
count = 2;
|
|
this.GuardaLog($"EjecutaThalamus: llenando archivo excel productos. ");
|
|
foreach (DataRow DRow in DTProductos.Rows)
|
|
{
|
|
wsProductos.Cell(count, 1).Value = "'" + DRow["Código Producto Interno"].ToString();
|
|
wsProductos.Cell(count, 2).Value = DRow["Nombre del Producto"].ToString();
|
|
wsProductos.Cell(count, 3).Value = DRow["Presentación"].ToString();
|
|
|
|
count++;
|
|
}
|
|
this.GuardaLog($"EjecutaThalamus: guardando archivo excel productos " + RutaProductos + " ");
|
|
wbProductos.SaveAs(RutaProductos);
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.GuardaLog($"EjecutaThalamus: Error procesamiento archivos exel " + ex.StackTrace.ToString() + " ");
|
|
ErrorProceso = true;
|
|
Respuesta = false;
|
|
}
|
|
|
|
|
|
//ErrorProceso = true;
|
|
|
|
|
|
if (!ErrorProceso)
|
|
{
|
|
var FInfoProductos = new FileInfo(RutaProductos);
|
|
var FInfoVentas = new FileInfo(RutaVentas);
|
|
var FInfoClientes = new FileInfo(RutaClientes);
|
|
var FInfoAlmacenes = new FileInfo(RutaAlmacenes);
|
|
|
|
bool CargaProductos = false;
|
|
bool CargaVentas = false;
|
|
bool CargaClientes = false;
|
|
bool CargaAlmacenes = false;
|
|
|
|
SBLog.AppendLine("<b>Cargas</b><br>");
|
|
SBLog.AppendLine("<table border='1'> <tr><th>Archivo</th><th>Estado</th></tr>");
|
|
|
|
|
|
this.GuardaLog($"EjecutaThalamus: Cargando archivo excel productos a FTP. ");
|
|
CargaProductos = CargarArchivos(FInfoProductos, $"{FTPServer}/productos", TFPUsr, FTPPass);
|
|
|
|
if (CargaProductos)
|
|
{
|
|
this.GuardaLog($"EjecutaThalamus: Verificando archivo productos. ");
|
|
CargaProductos = VerificarArchivo(FInfoProductos, FInfoProductos.Length, $"{FTPServer}/productos", TFPUsr, FTPPass);
|
|
}
|
|
|
|
if (CargaProductos)
|
|
{
|
|
SBLog.AppendLine($"<tr><td>Productos</td><td>ok</td></tr>");
|
|
}
|
|
else
|
|
{
|
|
SBLog.AppendLine($"<tr><td>Productos</td><td>Error carga</td></tr>");
|
|
}
|
|
|
|
if (CargaProductos)
|
|
{
|
|
this.GuardaLog($"EjecutaThalamus: Cargando archivo excel ventas a FTP. ");
|
|
CargaVentas = CargarArchivos(FInfoVentas, $"{FTPServer}/ventas", TFPUsr, FTPPass);
|
|
if (CargaVentas)
|
|
{
|
|
this.GuardaLog($"EjecutaThalamus: Verificando archivo ventas. ");
|
|
CargaVentas = VerificarArchivo(FInfoVentas, FInfoVentas.Length, $"{FTPServer}/ventas", TFPUsr, FTPPass);
|
|
}
|
|
}
|
|
|
|
if (CargaVentas)
|
|
{
|
|
SBLog.AppendLine($"<tr><td>Ventas</td><td>ok</td></tr>");
|
|
}
|
|
else
|
|
{
|
|
SBLog.AppendLine($"<tr><td>Ventas</td><td>Error carga</td></tr>");
|
|
}
|
|
|
|
if (CargaProductos & CargaVentas)
|
|
{
|
|
this.GuardaLog($"EjecutaThalamus: Cargando archivo excel clientes a FTP. ");
|
|
CargaClientes = CargarArchivos(FInfoClientes, $"{FTPServer}/clientes", TFPUsr, FTPPass);
|
|
if (CargaClientes)
|
|
{
|
|
this.GuardaLog($"EjecutaThalamus: Verificando archivo clientes. ");
|
|
CargaClientes = VerificarArchivo(FInfoClientes, FInfoClientes.Length, $"{FTPServer}/clientes", TFPUsr, FTPPass);
|
|
}
|
|
}
|
|
|
|
if (CargaClientes)
|
|
{
|
|
SBLog.AppendLine($"<tr><td>Clientes</td><td>ok</td></tr>");
|
|
}
|
|
else
|
|
{
|
|
SBLog.AppendLine($"<tr><td>Clientes</td><td>Error carga</td></tr>");
|
|
}
|
|
|
|
if (CargaProductos & CargaVentas & CargaClientes)
|
|
{
|
|
this.GuardaLog($"EjecutaThalamus: Cargando archivo excel almacenes a FTP. ");
|
|
CargaAlmacenes = CargarArchivos(FInfoAlmacenes, $"{FTPServer}/stock", TFPUsr, FTPPass);
|
|
if (CargaClientes)
|
|
{
|
|
this.GuardaLog($"EjecutaThalamus: Verificando archivo almacenes. ");
|
|
CargaAlmacenes = VerificarArchivo(FInfoAlmacenes, FInfoAlmacenes.Length, $"{FTPServer}/stock", TFPUsr, FTPPass);
|
|
}
|
|
}
|
|
|
|
if (CargaAlmacenes)
|
|
{
|
|
SBLog.AppendLine($"<tr><td>Almacenes</td><td>ok</td></tr>");
|
|
}
|
|
else
|
|
{
|
|
SBLog.AppendLine($"<tr><td>Almacenes</td><td>Error carga</td></tr>");
|
|
}
|
|
|
|
|
|
if (CargaProductos & CargaVentas & CargaClientes & CargaAlmacenes)
|
|
{
|
|
Respuesta = true;
|
|
}
|
|
else
|
|
{
|
|
Respuesta = false;
|
|
}
|
|
|
|
}else
|
|
{
|
|
Respuesta = false;
|
|
}
|
|
|
|
SBLog.AppendLine("</table>");
|
|
|
|
}
|
|
else
|
|
{
|
|
Respuesta = false;
|
|
}
|
|
|
|
|
|
string Cuerpo = Respuesta ? $"<b>Correcto</b><br>Tarea de carga de archios Corteva realizada correctamente<br> {SBLog.ToString()}" : $"<b>Error</b><br> Error tarea de carga de archios Corteva<br> {SBLog.ToString()}" ;
|
|
|
|
|
|
this.GuardaLog($"EjecutaThalamus: Enviado correo. ");
|
|
|
|
|
|
if (Respuesta)
|
|
{
|
|
MandaCorreo(Cuerpo, Respuesta, RutaProductos, RutaVentas, RutaClientes, RutaAlmacenes);
|
|
}
|
|
|
|
|
|
this.GuardaLog($"EjecutaThalamus: Tarea terminada se regresa respuesta {Respuesta.ToString()}. ");
|
|
return Respuesta;
|
|
|
|
}
|
|
|
|
|
|
private void MandaCorreo(string Cuerpo, bool ErrorProceso, string RutaProductos, string RutaVentas, string RutaClientes, string RutaAlmacenes)
|
|
{
|
|
|
|
try
|
|
{
|
|
|
|
List<ClsSisMail.ArchivoMail> ListaArchivos = new List<ClsSisMail.ArchivoMail>();
|
|
|
|
ClsSisMail.ArchivoMail Productos = new ClsSisMail.ArchivoMail();
|
|
ClsSisMail.ArchivoMail Ventas = new ClsSisMail.ArchivoMail();
|
|
ClsSisMail.ArchivoMail Clientes = new ClsSisMail.ArchivoMail();
|
|
ClsSisMail.ArchivoMail Almacenes = new ClsSisMail.ArchivoMail();
|
|
|
|
Productos.Ruta = RutaProductos;
|
|
Ventas.Ruta = RutaVentas;
|
|
Clientes.Ruta = RutaClientes;
|
|
Almacenes.Ruta = RutaAlmacenes;
|
|
|
|
ListaArchivos.Add(Productos);
|
|
ListaArchivos.Add(Ventas);
|
|
ListaArchivos.Add(Clientes);
|
|
ListaArchivos.Add(Almacenes);
|
|
|
|
|
|
ClsSisMail Correo = new ClsSisMail();
|
|
string Para = "julian@adssistemas.mx;rene.molina@adssistemas.mx;oscar.irigoyen@adssistemas.mx";
|
|
// string Para = "oscar.irigoyen@adssistemas.mx";
|
|
string Asunto = ErrorProceso ? $"Carga Layout Corteva {ClsPaginas.NowADS().ToString("yyyy-MM-dd")}" : $"Carga Layout Corteva Error {ClsPaginas.NowADS().ToString("yyyy-MM-dd")}";
|
|
|
|
Correo.SendMail(ClsPaginas.EmpresaNotificaciones, Para, Asunto, Cuerpo, Archivos:ListaArchivos);
|
|
this.GuardaLog($"EjecutaThalamus: MandaCorreo() correo enviado. ");
|
|
|
|
}catch(Exception ex)
|
|
{
|
|
Extensiones.ModExtensiones.GuardaLog(null,$"Error Tareas Programadas 119 error enviar mail --> {ex.Message} ");
|
|
this.GuardaLog($"EjecutaThalamus: MandaCorreo() Error {ex.StackTrace.ToString()}. ");
|
|
}
|
|
}
|
|
|
|
|
|
private bool CargarArchivos(FileInfo FInfo, string FTPServer, string FTPUsr, string FTPPass )
|
|
{
|
|
bool Respuesta;
|
|
|
|
if (FInfo.Exists)
|
|
{
|
|
this.GuardaLog($"EjecutaThalamus: CargarArchivos() Conectando a TFP. ");
|
|
|
|
FtpWebRequest request = (FtpWebRequest)WebRequest.Create($"{FTPServer}/{FInfo.Name}");
|
|
request.Method = WebRequestMethods.Ftp.UploadFile;
|
|
int tamanio = 1024;
|
|
var buffer = new byte[tamanio + 1];
|
|
int byteRead = 0;
|
|
bool Conected = false;
|
|
request.Credentials = new NetworkCredential(FTPUsr, FTPPass);
|
|
try
|
|
{
|
|
request.GetResponse();
|
|
Conected = true;
|
|
this.GuardaLog($"EjecutaThalamus: CargarArchivos() conexión a TFP correcta. ");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Extensiones.ModExtensiones.GuardaLog(null,$"Error Tareas Programadas 119 Conexión a servidor FTP archivo { FInfo.Name } {ex.Message} ");
|
|
this.GuardaLog($"EjecutaThalamus: CargarArchivos() FTP Error Tareas Programadas 119 Conexión a servidor FTP archivo { FInfo.Name } {ex.Message}. ");
|
|
}
|
|
|
|
if (Conected)
|
|
{
|
|
Stream ftpStream;
|
|
FileStream f;
|
|
ftpStream = request.GetRequestStream();
|
|
f = File.OpenRead(FInfo.FullName);
|
|
try
|
|
{
|
|
do
|
|
{
|
|
byteRead = f.Read(buffer, 0, tamanio);
|
|
ftpStream.Write(buffer, 0, byteRead);
|
|
}
|
|
while (byteRead != 0);
|
|
Respuesta = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Respuesta = false;
|
|
Extensiones.ModExtensiones.GuardaLog(null,$"Error Tareas Programadas 119 cargando archivo { FInfo.Name } {ex.Message} ");
|
|
this.GuardaLog($"EjecutaThalamus: CargarArchivos() Conectando a TFP Error Tareas Programadas 119 cargando archivo { FInfo.Name } {ex.Message}. ");
|
|
}
|
|
|
|
|
|
if (f is object)
|
|
{
|
|
f.Close();
|
|
}
|
|
|
|
this.GuardaLog($"EjecutaThalamus: CargarArchivos() TFP cerrrando conxión. ");
|
|
if (ftpStream is object)
|
|
{
|
|
ftpStream.Close();
|
|
this.GuardaLog($"EjecutaThalamus: CargarArchivos() TFP conxión cerrada. ");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Respuesta = false;
|
|
// no se conecto al ftp probablemente credenciales erroneas
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
Respuesta = false;
|
|
// El archivo no existe en el disco local
|
|
}
|
|
|
|
return Respuesta;
|
|
}
|
|
|
|
|
|
private bool VerificarArchivo(FileInfo FInfo, long Tamanio, string FTPServer, string FTPUsr, string FTPPass)
|
|
{
|
|
bool Respuesta = false;
|
|
|
|
|
|
try
|
|
{
|
|
this.GuardaLog($"EjecutaThalamus: VerificarArchivo() conectando a FTP .");
|
|
FtpWebRequest request = (FtpWebRequest)WebRequest.Create($"{FTPServer}/{FInfo.Name}");
|
|
request.Credentials = new NetworkCredential(FTPUsr, FTPPass);
|
|
request.Method = WebRequestMethods.Ftp.GetFileSize;
|
|
request.UseBinary = true;
|
|
FtpWebResponse loginresponse = (FtpWebResponse)request.GetResponse();
|
|
FtpWebResponse respSize = (FtpWebResponse)request.GetResponse();
|
|
respSize = (FtpWebResponse)request.GetResponse();
|
|
long size = respSize.ContentLength;
|
|
respSize.Close();
|
|
|
|
this.GuardaLog($"EjecutaThalamus: VerificarArchivo() tamaño local {Tamanio} ftp { size}. ");
|
|
if (Tamanio == size)
|
|
{
|
|
Respuesta = true;
|
|
this.GuardaLog($"EjecutaThalamus: VerificarArchivo() tamaño ok. ");
|
|
}
|
|
|
|
}catch(Exception ex)
|
|
{
|
|
|
|
Extensiones.ModExtensiones.GuardaLog(null,$"Error Tareas Programadas 119 verificando archivo { FInfo.Name } {ex.Message} ");
|
|
this.GuardaLog($"EjecutaThalamus: VerificarArchivo() Error {ex.StackTrace.ToString()}. " );
|
|
|
|
Respuesta = false;
|
|
}
|
|
|
|
return Respuesta;
|
|
}
|
|
|
|
private bool PruebaConexionFTP( string FTPServer, string FTPUsr, string FTPPass)
|
|
{
|
|
bool Respuesta = false;
|
|
|
|
FtpWebRequest request =(FtpWebRequest)WebRequest.Create($"{FTPServer}/clientes/Test.xlsx");
|
|
request.Method = WebRequestMethods.Ftp.ListDirectory;
|
|
|
|
request.Credentials = new NetworkCredential(FTPUsr, FTPPass);
|
|
|
|
try
|
|
{
|
|
request.GetResponse();
|
|
Respuesta = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Extensiones.ModExtensiones.GuardaLog(null, $"Error Tareas Programadas 119 error Prueba de Conexión a servidor FTP archivo {ex.Message}. ");
|
|
}
|
|
|
|
return Respuesta;
|
|
|
|
}
|
|
|
|
|
|
public bool IsReusable
|
|
{
|
|
get { return false; }
|
|
}
|
|
|
|
} |