|
|
|
@ -10,8 +10,6 @@ import os.path |
|
|
|
|
import sys |
|
|
|
|
import datetime as dt |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CurrentPath = os.path.dirname(os.path.realpath(__file__)) |
|
|
|
|
log_file = "./log/Referencias.txt" |
|
|
|
|
|
|
|
|
@ -26,7 +24,6 @@ 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: |
|
|
|
@ -41,8 +38,6 @@ with open(CurrentPath+"/config/config.json") as json_data_file: |
|
|
|
|
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 |
|
|
|
@ -51,7 +46,6 @@ def ZipReference(foldername, TempFolder, Cliente): |
|
|
|
|
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: |
|
|
|
@ -67,7 +61,7 @@ def WriteToLog(referencia): |
|
|
|
|
referencia = referencia.replace('[', '') |
|
|
|
|
referencia = referencia.replace(']', '') |
|
|
|
|
if referencia in processed_files: |
|
|
|
|
print(f"El archivo {referencia} ya ha sido procesado.") |
|
|
|
|
print(f"El archivo {referencia} ya ha sido trabajada .") |
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
with open('./log/Referencias.txt', 'a') as f: |
|
|
|
@ -77,13 +71,11 @@ def WriteToLog(referencia): |
|
|
|
|
log_file = "./log/fechayHora.txt" |
|
|
|
|
WriteToLogDate(log_file, referencia) |
|
|
|
|
|
|
|
|
|
print(f"El archivo {referencia} ha sido registrado.") |
|
|
|
|
print(f"El archivo {referencia} ha sido registrado en la carpeta Log en los respectivos archivos.") |
|
|
|
|
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: |
|
|
|
@ -94,7 +86,7 @@ def CreateFolderContent(): |
|
|
|
|
# Obtener archivos a procesar |
|
|
|
|
files2process = [] |
|
|
|
|
cnxn = po.connect( |
|
|
|
|
"DRIVER={ODBC Driver 17 for SQL Server};SERVER=" + ServerSIR |
|
|
|
|
"DRIVER={SQL Server};SERVER=" + ServerSIR |
|
|
|
|
+ ";DATABASE=" + DatabaseSIR |
|
|
|
|
+ ";UID=" + UsernameSIR |
|
|
|
|
+ ";PWD=" + PasswordSIR |
|
|
|
@ -103,7 +95,7 @@ def CreateFolderContent(): |
|
|
|
|
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()-2 AS Date )""") |
|
|
|
|
WHERE P.ClienteClave=2096 and p.[Pedimento FechaPago]>=CAST( GETDATE()-5 AS Date ) """) |
|
|
|
|
for row in cursor: |
|
|
|
|
referencia = row[0] |
|
|
|
|
archivoM = row[1] |
|
|
|
@ -115,33 +107,25 @@ def CreateFolderContent(): |
|
|
|
|
clave = row[7] |
|
|
|
|
cliente = row [8] |
|
|
|
|
referenceFolder = TempFolder + referencia |
|
|
|
|
if referencia in processed_references: |
|
|
|
|
print(f"La referencia {referencia} ya se ha trabajado.") |
|
|
|
|
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): |
|
|
|
|
# Verificar si la referencia ya ha sido procesada |
|
|
|
|
if referencia in processed_references: |
|
|
|
|
print(f"La referencia {referencia} ya ha sido procesada.") |
|
|
|
|
else: |
|
|
|
|
files2process.append(zip_file) |
|
|
|
|
else: |
|
|
|
|
ZipReference(referencia, referenceFolder, cliente) |
|
|
|
|
files2process.append(zip_file) |
|
|
|
|
|
|
|
|
|
for file in files2process: |
|
|
|
|
# Obtener referencia del archivo |
|
|
|
|
filename = os.path.basename(file) |
|
|
|
|
reference = filename.split('_____')[0] |
|
|
|
|
|
|
|
|
|
if os.path.exists(zip_file) == False: |
|
|
|
|
ZipReference(referencia, referenceFolder, cliente) |
|
|
|
|
# Procesar archivo |
|
|
|
|
|
|
|
|
|
URL = 'http://localhost:5000/api/AmazonInvoice/UploadSIRReference?ArchivoM=' +\ |
|
|
|
|
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', (file.replace(TempFolder,''), open(file, 'rb'), 'application/zip'))) |
|
|
|
|
multiple_files.append(('files', (zip_file.replace(TempFolder,''), open(zip_file, 'rb'), 'application/zip'))) |
|
|
|
|
data = { |
|
|
|
|
"ArchivoM": archivoM, |
|
|
|
|
'aduana': aduana, |
|
|
|
@ -150,25 +134,9 @@ def CreateFolderContent(): |
|
|
|
|
'fPago' : str(fPago), |
|
|
|
|
'tipoCambio' : str(tipoCambio), |
|
|
|
|
'clave': clave |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
print( f"la referencia {reference} ya ha sido posteada." ) |
|
|
|
|
r = requests.post(url=URL,files=multiple_files, verify=False, timeout=9000) |
|
|
|
|
print(r.text.strip('["]')) |
|
|
|
|
response_text = r.text.strip('[]\"') |
|
|
|
|
if response_text != 'false': |
|
|
|
|
WriteToLog(reference) |
|
|
|
|
processed_references.append(reference) |
|
|
|
|
else: |
|
|
|
|
print(f"La referencia {reference} no se pudo procesar. Respuesta del servidor: {response_text}") |
|
|
|
|
|
|
|
|
|
# if r.text.strip('false'): |
|
|
|
|
# WriteToLog(reference) |
|
|
|
|
# processed_references.append(reference) |
|
|
|
|
# if WriteToLog(reference): |
|
|
|
|
# processed_references.append(reference) |
|
|
|
|
|
|
|
|
|
print( f"la referencia {referencia} esta siendo enviada al servidor." ) |
|
|
|
|
r = requests.post(url=URL,files=multiple_files, verify=False, timeout=30) |
|
|
|
|
except Exception as e: |
|
|
|
|
print("Error: %s" % e) |
|
|
|
|
# WriteToLog(r.text) |
|
|
|
@ -176,5 +144,3 @@ def CreateFolderContent(): |
|
|
|
|
''' 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() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|