This commit is contained in:
Rakcsle Supertramp
2020-06-23 14:39:56 -07:00
parent 91f7085ae1
commit a1edf2e402
6 changed files with 265 additions and 113 deletions

View File

@@ -21,7 +21,11 @@ public class TareasJam : IHttpHandler
string Metodo = context.Request["Metodo"] ==null?"": context.Request["Metodo"];
int CveEmpresa =context.Request["CveEmpresa"] ==null?0: context.Request["CveEmpresa"].ToEntero();
switch (Metodo)
{
{
case "TestConexion":
//PruebaConexionFTP("ftp://127.0.0.1/", "Toor", "agj-3500");
retorno = PruebaConexionFTP("ftp://us2.hostedftp.com/", "agrojam", "A12345678");
break;
case "EjecutaThalamus":
retorno =EjecutaThalamus(CveEmpresa);
break;
@@ -40,150 +44,259 @@ public class TareasJam : IHttpHandler
context.Response.End();
//ModExtensiones.GuardaLogAD(string.Format("{3} CveEmpresa={0} cveTareaProgramada={1} CveTareaProgramadaHistorial={2}", CveEmpresa, cveTareaProgramada, CveTareaProgramadaHistorial, Metodo), "Tareas Programadas");
}
private bool EjecutaThalamus(int CveEmpresa)
private bool EjecutaThalamus(int CveEmpresa)
{
ClsGenerales ObjGenerales = new ClsGenerales();
DataTable DTProductos = new DataTable();
DataTable DTVentas = new DataTable();
DataTable DTClientes = new DataTable();
bool ErrorProceso = false;
if (ObjGenerales.GetJAMCorveta(1))
{
DTProductos = ObjGenerales.DtResultados.Copy();
}
else { ErrorProceso = true; }
if (ObjGenerales.GetJAMCorveta(2))
{
DTVentas = ObjGenerales.DtResultados.Copy();
}
else { ErrorProceso = true; }
string RutaArchivoVentas = $@"C:\Temp\Ventas{DateTime.Now.ToString("yyyyMMdd")}.xlsx";
var DTVentas = new DataTable();
DTVentas.Columns.Add("systemId");
DTVentas.Columns.Add("Branch");
DTVentas.Columns.Add("Code");
DTVentas.Columns.Add("Sale Date");
DTVentas.Columns.Add("Quantity");
DTVentas.Columns.Add("Amount");
DTVentas.Columns.Add("Transaction ID");
for (int x = 0; x <= 50; x++)
{
DataRow dr = DTVentas.NewRow();
dr[0] = $"systemId {x}";
dr[1] = $"Branch {x}";
dr[2] = $"{x}";
dr[3] = $"{ DateTime.Now.ToString("yyyy-MM-dd")}";
dr[4] = $"{x}";
dr[5] = $"{x}.00";
dr[6] = $"{x}";
DTVentas.Rows.Add(dr);
}
if (ObjGenerales.GetJAMCorveta(3))
{
DTClientes = ObjGenerales.DtResultados.Copy();
}
else { ErrorProceso = true; }
ObjGenerales.Dispose();
if (!ErrorProceso)
{
string RutaProductos = $@"{ HttpContext.Current.Server.MapPath("~") }Temp\Productos{DateTime.Now.ToString("yyyyMMdd")}.xlsx";
string RutaVentas = $@"{ HttpContext.Current.Server.MapPath("~") }Temp\Ventas{DateTime.Now.ToString("yyyyMMdd")}.xlsx";
string RutaClientes = $@"{ HttpContext.Current.Server.MapPath("~") }Temp\Clientes{DateTime.Now.ToString("yyyyMMdd")}.xlsx";
string FTPServer = "ftp://127.0.0.1/";
string TFPUsr = "Toor";
string FTPPass = "agj-3500";
SpreadsheetInfo.SetLicense(Extensiones.ModExtensiones.SerialGemBoxSpreadsheet());
var workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("Ventas");
worksheet.InsertDataTable(DTVentas, new InsertDataTableOptions()
var workbookProductos = new ExcelFile();
var worksheetProductos = workbookProductos.Worksheets.Add("Ventas");
worksheetProductos.InsertDataTable(DTProductos, new InsertDataTableOptions()
{
ColumnHeaders = true,
StartRow = 0
});
workbook.Save(RutaArchivoVentas);
string FTPServer = "ftp://127.0.0.1/";
string TFPUsr = "Toor";
string FTPPass = "agj-3500";
var FInfo = new FileInfo(RutaArchivoVentas);
bool Carga = CargarArchivos(FInfo.FullName, FTPServer, TFPUsr, FTPPass);
if (Carga)
workbookProductos.Save(RutaProductos);
var workbookVentas = new ExcelFile();
var worksheetVentas = workbookVentas.Worksheets.Add("Ventas");
worksheetVentas.InsertDataTable(DTVentas, new InsertDataTableOptions()
{
Carga = VerificarArchivo(FInfo.Name, FInfo.Length, FTPServer, TFPUsr, FTPPass);
ColumnHeaders = true,
StartRow = 0
});
workbookVentas.Save(RutaVentas);
var workbookClientes = new ExcelFile();
var worksheetClientes = workbookClientes.Worksheets.Add("Ventas");
worksheetClientes.InsertDataTable(DTClientes, new InsertDataTableOptions()
{
ColumnHeaders = true,
StartRow = 0
});
workbookClientes.Save(RutaClientes);
var FInfoProductos = new FileInfo(RutaProductos);
var FInfoVentas = new FileInfo(RutaVentas);
var FInfoClientes = new FileInfo(RutaClientes);
bool CargaProductos = false;
bool CargaVentas = false;
bool CargaClientes = false;
CargaProductos = CargarArchivos(FInfoProductos, FTPServer, TFPUsr, FTPPass);
if (CargaProductos)
{
CargaProductos = VerificarArchivo(FInfoProductos, FInfoProductos.Length, FTPServer, TFPUsr, FTPPass);
}
return Carga;
if (CargaProductos)
{
CargaVentas = CargarArchivos(FInfoVentas, FTPServer, TFPUsr, FTPPass);
if (CargaVentas)
{
CargaVentas = VerificarArchivo(FInfoVentas, FInfoVentas.Length, FTPServer, TFPUsr, FTPPass);
}
}
if (CargaProductos & CargaVentas)
{
CargaClientes = CargarArchivos(FInfoClientes, FTPServer, TFPUsr, FTPPass);
if (CargaClientes)
{
CargaClientes = VerificarArchivo(FInfoClientes, FInfoClientes.Length, FTPServer, TFPUsr, FTPPass);
}
}
if (CargaProductos & CargaVentas & CargaClientes)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
private bool CargarArchivos(string RutaArchivo, string FTPServer, string FTPUsr, string FTPPass)
{
bool Respuesta;
var FInfo = new FileInfo(RutaArchivo);
if (FInfo.Exists)
private bool CargarArchivos(FileInfo FInfo, string FTPServer, string FTPUsr, string FTPPass)
{
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
bool Respuesta;
//var FInfo = new FileInfo(RutaArchivo);
if (FInfo.Exists)
{
request.GetResponse();
Conected = true;
}
catch (Exception ex)
{
}
if (Conected)
{
Stream ftpStream;
FileStream f;
ftpStream = request.GetRequestStream();
f = File.OpenRead(FInfo.FullName);
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
{
do
{
byteRead = f.Read(buffer, 0, tamanio);
ftpStream.Write(buffer, 0, byteRead);
}
while (byteRead != 0);
Respuesta = true;
request.GetResponse();
Conected = true;
}
// validar si existe archivo si coincide en tamaño
catch (Exception ex)
{
Extensiones.ModExtensiones.GuardaLog(null,$"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} ");
}
if (f is object)
{
f.Close();
}
if (ftpStream is object)
{
ftpStream.Close();
}
}
else
{
Respuesta = false;
// no se conecto al ftp probablemente credenciales erroneas
}
if (f is object)
{
f.Close();
}
if (ftpStream is object)
{
ftpStream.Close();
}
}
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(string Archivo, long Tamanio, string FTPServer, string FTPUsr, string FTPPass)
{
bool Respuesta = false;
FtpWebRequest reqSize = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPServer + Archivo));
reqSize.Credentials = new NetworkCredential(FTPUsr, FTPPass);
reqSize.Method = WebRequestMethods.Ftp.GetFileSize;
reqSize.UseBinary = true;
FtpWebResponse loginresponse = (FtpWebResponse)reqSize.GetResponse();
FtpWebResponse respSize = (FtpWebResponse)reqSize.GetResponse();
respSize = (FtpWebResponse)reqSize.GetResponse();
long size = respSize.ContentLength;
respSize.Close();
if (Tamanio == size)
{
Respuesta = true;
// El archivo no existe en el disco local
}
return Respuesta;
}
public bool IsReusable
{
get { return false; }
private bool VerificarArchivo(FileInfo FInfo, long Tamanio, string FTPServer, string FTPUsr, string FTPPass)
{
bool Respuesta = false;
try
{
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();
if (Tamanio == size)
{
Respuesta = true;
}
}catch(Exception ex)
{
Extensiones.ModExtensiones.GuardaLog(null,$"Error Tareas Programadas 119 verificando archivo { FInfo.Name } {ex.Message} ");
Respuesta = false;
}
return Respuesta;
}
private bool PruebaConexionFTP( string FTPServer, string FTPUsr, string FTPPass)
{
bool Respuesta = false;
FtpWebRequest request =(FtpWebRequest)WebRequest.Create($"{FTPServer}/Test.log");
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; }
}
}