From a1d8d50d7002847daf9c43e85a8222ea7f5c2f71 Mon Sep 17 00:00:00 2001 From: Albert Davila Date: Mon, 15 May 2023 16:50:15 -0500 Subject: [PATCH] Actualizacion 2023-05-15 --- SyncReferences2IONOS.py | 146 +++++++++++++++++++++++++++++++ config/config.json | 22 +++++ log/202305121942/Referencias.txt | 65 ++++++++++++++ log/202305121942/fechayHora.txt | 67 ++++++++++++++ log/Referencias.txt | 17 ++++ log/fechayHora.txt | 24 +++++ 6 files changed, 341 insertions(+) create mode 100644 SyncReferences2IONOS.py create mode 100644 config/config.json create mode 100644 log/202305121942/Referencias.txt create mode 100644 log/202305121942/fechayHora.txt create mode 100644 log/Referencias.txt create mode 100644 log/fechayHora.txt diff --git a/SyncReferences2IONOS.py b/SyncReferences2IONOS.py new file mode 100644 index 0000000..a4db21d --- /dev/null +++ b/SyncReferences2IONOS.py @@ -0,0 +1,146 @@ +import pyodbc as po +import json +import shutil +import os +import datetime +import zipfile +import requests +import urllib3 +import os.path +import sys +import datetime as dt + +CurrentPath = os.path.dirname(os.path.realpath(__file__)) +log_file = "./log/Referencias.txt" + +if not os.path.isfile(CurrentPath + log_file): + with open(CurrentPath + log_file, 'w') as f: + f.write("") + +CurrentPath = os.path.dirname(os.path.realpath(__file__)) +log_file1 = "./log/fechayHora.txt" + +if not os.path.isfile(CurrentPath + log_file1): + with open(CurrentPath + log_file1, 'w') as f: + f.write("") + +urllib3.disable_warnings() +CurrentPath = os.path.dirname(os.path.realpath(__file__)) +with open(CurrentPath+"/config/config.json") as json_data_file: + config = json.load(json_data_file) + ServerSIR = config['ServerSIR'] + DatabaseSIR = config['DatabaseSIR'] + UsernameSIR = config['DBUserSIR'] + PasswordSIR = config['DBPasswordSIR'] + RepositoryArchivoElectronicoOficial = config['RepositoryArchivoElectronicoOficial'] + '''RepositoryArchivoElectronicoOficial = 'c:/data/temp/''' + TempFolder= 'c:/tmp/' + today = datetime.date.today() + YEAR = today.year + +def ZipReference(foldername, TempFolder, Cliente): + zipobj = zipfile.ZipFile(TempFolder+ '_____'+ Cliente + '.zip', 'w', zipfile.ZIP_DEFLATED) + rootlen = len(TempFolder) + 1 + for base, dirs, files in os.walk(TempFolder): + for file in files: + fn = os.path.join(base, file) + zipobj.write(fn, fn[rootlen:]) + +def WriteToLogDate(log_file, referencia): + fecha = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + with open(log_file, 'a') as f: + f.write(f"{fecha} - {referencia}\n") + f.flush() + +def WriteToLog(referencia): + processed_files = [] + with open('./log/Referencias.txt', 'r') as f: + for line in f: + processed_files.append(line.strip()) + + referencia = referencia.replace('[', '') + referencia = referencia.replace(']', '') + if referencia in processed_files: + print(f"El archivo {referencia} ya ha sido procesado.") + return False + + with open('./log/Referencias.txt', 'a') as f: + f.write(f"{referencia}\n") + f.flush() + + log_file = "./log/fechayHora.txt" + WriteToLogDate(log_file, referencia) + + print(f"El archivo {referencia} ha sido registrado.") + return True + +def CreateFolderContent(): + try: + # Leer archivo de registro y obtener lista de referencias procesadas + processed_references = [] + with open('./log/Referencias.txt', 'r') as f: + for line in f: + reference = line.strip() + processed_references.append(reference) + + # Obtener archivos a procesar + files2process = [] + cnxn = po.connect( + "DRIVER={SQL Server};SERVER=" + ServerSIR + + ";DATABASE=" + DatabaseSIR + + ";UID=" + UsernameSIR + + ";PWD=" + PasswordSIR + ) + cursor = cnxn.cursor() + cursor.execute("""SELECT P.sReferencia, p.Archivo, P.Aduana, P.Patente, P.Pedimento, P.[Pedimento Fecha Pago], Peds.nTipoCambio, + P.Clave, P.Cliente FROM SIR.Admin.SIR_VT_PedimPagados P + INNER JOIN SIR.SIR.SIR_149_PEDIMENTO AS Peds ON Peds.nIdPedimento149=ID + WHERE P.ClienteClave=2096 and p.[Pedimento FechaPago]>=CAST( GETDATE()-5 AS Date ) """) + for row in cursor: + referencia = row[0] + archivoM = row[1] + aduana = row[2] + patente = row[3] + pedimento = row[4] + fPago = row[5] + tipoCambio = row[6] + clave = row[7] + cliente = row [8] + referenceFolder = TempFolder + referencia + if referencia in processed_references: + print(f"La referencia {referencia} ya ha sido procesada.") + continue + if os.path.exists(referenceFolder): + shutil.rmtree(referenceFolder) + shutil.copytree(RepositoryArchivoElectronicoOficial + str(YEAR) + '/' + referencia, referenceFolder) + zip_file = referenceFolder+'_____'+cliente+'.zip' + + if os.path.exists(zip_file) == False: + ZipReference(referencia, referenceFolder, cliente) + # Procesar archivo + if WriteToLog(referencia): + processed_references.append(referencia) + URL = 'https://www.gemcousa.mx/GEMCOBackend/api/AmazonInvoice/UploadSIRReference?ArchivoM=' +\ + archivoM+'&aduana='+aduana+'&patente='+patente + \ + '&pedimento='+pedimento+'&fPago=' + \ + str(fPago)+'&TipoCambio='+str(tipoCambio)+'&clave='+clave + multiple_files = [] + multiple_files.append(('files', (zip_file.replace(TempFolder,''), open(zip_file, 'rb'), 'application/zip'))) + data = { + "ArchivoM": archivoM, + 'aduana': aduana, + 'patente': patente, + 'pedimento': pedimento, + 'fPago' : str(fPago), + 'tipoCambio' : str(tipoCambio), + 'clave': clave + } + print( f"la referencia {referencia} ya ha sido posteada." ) + r = requests.post(url=URL,files=multiple_files, verify=False, timeout=30) + except Exception as e: + print("Error: %s" % e) + # WriteToLog(r.text) + +''' contents = [os.path.join(TempFolder, i) for i in os.listdir(TempFolder)] +[shutil.rmtree(i) if os.path.isdir(i) and not os.path.islink(i) else os.remove(i) for i in contents] ''' +CreateFolderContent() diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..bec49ce --- /dev/null +++ b/config/config.json @@ -0,0 +1,22 @@ +{ + "server": ".", + "database": "GEMCO", + "username": "sa", + "password": "Chispa10", + "smtp_server": "smtp.ionos.com", + "smtp_port": 587, + "smtp_username": "noreply@gemcousa.mx", + "smtp_password": "Mx77lf%02", + "sender": "noreply@gemcousa.mx", + "recipient": "lrendon@gemcousa.com", + "ServerSIR": "192.168.100.11", + "DatabaseSIR": "SIR", + "DBUserSIR": "usersir", + "DBPasswordSIR": "S1r22$Us", + + "RepositoryArchivoElectronicoOficial": "Z:/Recursos/ControlDocumentos/IMPORTADORA AMAZON MEXICO S DE RL DE CV/" + + +} + + diff --git a/log/202305121942/Referencias.txt b/log/202305121942/Referencias.txt new file mode 100644 index 0000000..7f12806 --- /dev/null +++ b/log/202305121942/Referencias.txt @@ -0,0 +1,65 @@ +23-00401I +23-00402I +23-00322I +23-00447I +23-00321I +23-00495I +23-00544I +23-00542I +23-00309I +23-00307I + +23-00429I +23-00648I + + +23-00527I +23-00653I +23-00553I +23-00657I +23-00494I +23-00541I +23-00665I + +23-00664I +23-00669I +23-00661I +23-00678I +23-00682I + +23-00704I +23-00400I +23-00552I +23-00738I +23-00726I + +23-00730I +23-00427I + + +23-00714I +23-00432I + + +23-00732I +23-00733I +23-00492I +23-00493I +23-00681I +23-00543I + +23-00823I +23-00539I +23-00430I +23-00540I + +23-00548I + +23-00827I +23-00457I +23-00814I +23-00816I +23-00817I + +23-00723I +23-00725I diff --git a/log/202305121942/fechayHora.txt b/log/202305121942/fechayHora.txt new file mode 100644 index 0000000..37db652 --- /dev/null +++ b/log/202305121942/fechayHora.txt @@ -0,0 +1,67 @@ +2023-03-30 17:02:18 - 23-00401I +2023-03-30 17:02:18 - 23-00402I +2023-03-30 17:02:18 - 23-00322I +2023-03-31 13:00:12 - 23-00447I +2023-03-31 13:00:13 - 23-00321I +2023-03-31 17:15:15 - 23-00495I +2023-04-04 13:00:02 - 23-00544I +2023-04-05 17:30:04 - 23-00542I +2023-04-07 13:00:07 - 23-00309I +2023-04-10 14:00:03 - 23-00307I + +2023-04-11 17:25:59 - 23-00429I +2023-04-11 17:26:01 - 23-00648I + + +2023-04-12 15:22:43 - 23-00527I +2023-04-13 16:30:09 - 23-00653I +2023-04-13 17:30:10 - 23-00553I +2023-04-14 17:30:11 - 23-00657I +2023-04-15 12:00:10 - 23-00494I +2023-04-17 18:00:06 - 23-00541I +2023-04-17 18:45:07 - 23-00665I +2023-04-18 12:45:07 - 23-00664I +2023-04-18 15:25:41 - 23-00664I +2023-04-19 18:30:06 - 23-00669I +2023-04-20 16:00:08 - 23-00661I +2023-04-20 17:15:08 - 23-00678I +2023-04-20 18:15:10 - 23-00682I +2023-04-21 13:15:08 - 23-00704I +2023-04-21 13:34:43 - 23-00704I +2023-04-24 12:45:04 - 23-00400I +2023-04-25 12:45:04 - 23-00552I +2023-04-25 18:30:05 - 23-00738I +2023-04-26 12:00:06 - 23-00726I + +2023-04-26 17:45:08 - 23-00730I +2023-04-27 15:00:11 - 23-00427I +2023-04-27 15:15:11 - 23-00732I + +2023-04-28 16:45:16 - 23-00714I +2023-04-29 10:00:13 - 23-00432I +2023-04-29 11:33:07 - 23-00732I + +2023-04-29 11:35:55 - 23-00732I +2023-05-03 10:15:03 - 23-00733I +2023-05-03 13:30:05 - 23-00492I +2023-05-03 16:15:06 - 23-00493I +2023-05-03 18:15:09 - 23-00681I +2023-05-04 13:00:10 - 23-00543I +2023-05-05 16:30:12 - 23-00823I +2023-05-06 12:01:45 - 23-00823I +2023-05-08 18:00:04 - 23-00539I +2023-05-09 16:15:09 - 23-00430I +2023-05-09 16:15:11 - 23-00540I + +2023-05-09 17:30:11 - 23-00548I + + + +2023-05-10 14:19:06 - 23-00827I +2023-05-12 13:42:16 - 23-00457I +2023-05-12 13:42:21 - 23-00814I +2023-05-12 13:42:25 - 23-00816I +2023-05-12 13:42:29 - 23-00817I + +2023-05-12 17:04:45 - 23-00723I +2023-05-12 17:11:58 - 23-00725I diff --git a/log/Referencias.txt b/log/Referencias.txt new file mode 100644 index 0000000..ef8554d --- /dev/null +++ b/log/Referencias.txt @@ -0,0 +1,17 @@ +23-00823I +23-00539I +23-00430I +23-00540I + +23-00548I + +23-00827I +23-00457I +23-00814I + +23-00817I + +23-00723I +23-00725I +23-00737I +23-00816I diff --git a/log/fechayHora.txt b/log/fechayHora.txt new file mode 100644 index 0000000..9fc6926 --- /dev/null +++ b/log/fechayHora.txt @@ -0,0 +1,24 @@ +2023-05-03 16:15:06 - 23-00493I +2023-05-03 18:15:09 - 23-00681I +2023-05-04 13:00:10 - 23-00543I +2023-05-05 16:30:12 - 23-00823I +2023-05-06 12:01:45 - 23-00823I +2023-05-08 18:00:04 - 23-00539I +2023-05-09 16:15:09 - 23-00430I +2023-05-09 16:15:11 - 23-00540I + +2023-05-09 17:30:11 - 23-00548I + + + +2023-05-10 14:19:06 - 23-00827I +2023-05-12 13:42:16 - 23-00457I +2023-05-12 13:42:21 - 23-00814I +2023-05-12 13:42:25 - 23-00816I +2023-05-12 13:42:29 - 23-00817I + +2023-05-12 17:04:45 - 23-00723I +2023-05-12 17:11:58 - 23-00725I +2023-05-12 19:43:52 - 23-00737I +2023-05-13 14:07:25 - 23-00816I +2023-05-13 14:22:39 - 23-00816I