Se le agregan archivos de apoyo para el proyecto de Amazon

main
unknown 2 years ago
parent 9b7ef05f79
commit ac516bcef3
  1. 33
      AmazonReadFiles.py
  2. 197
      CreateFilesACK.py
  3. 87
      PedimentosPagadosWatchDog.py
  4. 82
      SyncReferences2IONOS.py
  5. 1
      WatchDog.bat
  6. 14
      config/config.json
  7. 2
      log/Referencias.txt

@ -5,8 +5,9 @@ import email.utils
from email.mime.text import MIMEText
import os
import pyodbc as po
file_path = os.path.realpath(__file__)
with open("./config/config.json") as json_data_file:
with open(file_path.replace('AmazonReadFiles.py', '')+"/config/config.json") as json_data_file:
config = json.load(json_data_file)
server = config['Server']
database = config['Database']
@ -296,6 +297,32 @@ def ProcessCorrelacion():
cnxn.close()
def ProcessCorrelacionPartesGEMCO():
try:
cnxn = po.connect(
"DRIVER={ODBC Driver 17 for SQL Server};SERVER="
+ server
+ ";DATABASE="
+ database
+ ";UID="
+ username
+ ";PWD="
+ password
)
cursor = cnxn.cursor()
storedProc = """\
SET NOCOUNT ON;
DECLARE @RC int;
EXEC @RC = [GEMCO].dbo.[Clientes.Amazon.Invoice.Detail.Correlacion.PartesGEMCO];"""
cursor.execute(storedProc)
print('Correlacion executed')
cnxn.commit()
except Exception as e:
print("Error: %s" % e)
finally:
cnxn.close()
def GetFileList():
for file in os.listdir(downloadedPath):
if os.path.isfile(os.path.join(downloadedPath, file)) and file.endswith('.xml'):
@ -572,5 +599,7 @@ for file in files:
row[24],
Archivo
)
ProcessCorrelacionPartesGEMCO()
ProcessCorrelacion()

@ -0,0 +1,197 @@
import pyodbc as po
import json
import base64
import time
import shutil
import datetime
from pathlib import Path
with open("./config/config.json") as json_data_file:
config = json.load(json_data_file)
server = config['Server']
database = config['Database']
username = config['DBUser']
password = config['DBPassword']
ServerSIR = config['ServerSIR']
DatabaseSIR = config['DatabaseSIR']
UsernameSIR = config['DBUserSIR']
PasswordSIR = config['DBPasswordSIR']
ACKPathFiles = config['ACKPathFiles']
RepositoryArchivoElectronicoOficial = config['RepositoryArchivoElectronicoOficial']
def GetMFile(Referencia):
MFile=''
try:
cnxn = po.connect(
"DRIVER={ODBC Driver 17 for 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 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 ) and sReferencia='23-00253I'""")
for row in cursor:
Pedimento = time.strftime("%y", time.localtime())+'-'+row[2]+'0-'+row[3]+'-'+row[4]
PaymentDate = row[5]
ExchangeRate = row[6]
CustomFillingCode = row[7]
Referencia = row[0]
MFile = row[1]
print('Se encontro un registro que conincide con la ocurrence')
DestinyFile = ACKPathFiles+MFile
print('Pedimento='+Pedimento)
today = datetime.date.today()
YEAR = today.year
shutil.copy2(RepositoryArchivoElectronicoOficial + str(YEAR) + '/' + Referencia + '/' + MFile, DestinyFile)
path_to_file = DestinyFile
path = Path(path_to_file)
if path.is_file():
with open(DestinyFile, "r") as file:
MFileBase64 = file.read().encode('utf-8')
MFileBase64 = base64.b64encode(MFileBase64)
SidelinePath = GetFilePath(Referencia, 'SIDELINE')
GlosaPath = GetFilePath(Referencia, 'GLOSA')
AcknowledgementPath = GetFilePath(Referencia, 'Acknowledgement')
with open(SidelinePath, 'r') as file:
filedata = file.read()
filedata = filedata.replace('?Pedimento?', Pedimento)
filedata = filedata.replace('?ExchangeRate?', str(ExchangeRate))
with open(SidelinePath, 'w') as file:
file.write(filedata)
with open(GlosaPath, 'r') as file:
filedata = file.read()
filedata = filedata.replace('?Pedimento?', Pedimento)
filedata = filedata.replace('?ExchangeRate?', str(ExchangeRate))
with open(GlosaPath, 'w') as file:
file.write(filedata)
with open(AcknowledgementPath, 'r') as file:
filedata = file.read()
filedata = filedata.replace('<Document>', '<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">')
filedata = filedata.replace('?Pedimento?', Pedimento)
filedata = filedata.replace('?CustomFillingCode?', CustomFillingCode)
filedata = filedata.replace('?PaymentDate?', str(PaymentDate)[:10])
filedata = filedata.replace('?ExchangeRate?', str(ExchangeRate))
filedata = filedata.replace('?ArchivoM?', MFileBase64.decode('utf-8'))
with open(AcknowledgementPath, 'w') as file:
file.write(filedata)
else:
print('El archivo M no existe, por lo tanto no se pueden procesar los archivos ACK, estan incompletos')
except Exception as e:
print("Error: %s" % e)
finally:
cnxn.close()
def GetFilePath(Referencia,mode):
try:
cnxn = po.connect(
"DRIVER={ODBC Driver 17 for SQL Server};SERVER=" + server
+ ";DATABASE=" + database
+ ";UID=" + username
+ ";PWD=" + password
)
cursor = cnxn.cursor()
storedProc = """\
SET NOCOUNT ON;
DECLARE @RC int;
EXEC @RC = [GEMCO].dbo.[Clientes.Amazon.ACKs.Generate] @Referencia = ?;
SELECT @RC AS rc;"""
params = (Referencia)
cursor.execute(storedProc, params)
rc = cursor.fetchval()
cnxn.commit()
cursor.execute('SELECT * FROM [Clientes.Amazon.ACKFiles]')
for row in cursor:
if row[2] == 'SIDELINE' and 'SIDELINE'==mode:
FilePath = row[3]
if row[2] == 'GLOSA' and 'GLOSA'==mode:
FilePath = row[3]
if row[2] == 'Acknowledgement' and 'Acknowledgement' == mode:
FilePath = row[3]
except Exception as e:
print("Error: %s" % e)
finally:
cnxn.close()
return FilePath
def CreateACKs(Referencia):
try:
cnxn = po.connect(
"DRIVER={ODBC Driver 17 for SQL Server};SERVER=" + server
+ ";DATABASE=" + database
+ ";UID=" + username
+ ";PWD=" + password
)
cursor = cnxn.cursor()
storedProc = """\
SET NOCOUNT ON;
DECLARE @RC int;
EXEC @RC = [GEMCO].dbo.[Clientes.Amazon.ACKs.Generate] @Referencia = ?;
SELECT @RC AS rc;"""
params = (Referencia)
cursor.execute(storedProc, params)
rc = cursor.fetchval()
cnxn.commit()
cursor.execute('SELECT * FROM [Clientes.Amazon.ACKFiles]')
for row in cursor:
if row[2] == 'SIDELINE':
SidelinePath = row[3]
SidelineDocumentId = row[4]
SidelineUniqueReferenceId = row[5]
if row[2] == 'GLOSA':
GlosaPath = row[3]
GlosaDocumentId = row[4]
GlosaUniqueReferenceId = row[5]
if row[2] == 'Acknowledgement':
AcknowledgementPath = row[3]
with open(SidelinePath, "r") as file:
SidelineBase64 = file.read().encode('utf-8')
SidelineBase64 = base64.b64encode(SidelineBase64)
with open(GlosaPath, "r") as file:
GlosaBase64 = file.read().encode('utf-8')
GlosaBase64 = base64.b64encode(GlosaBase64)
with open(AcknowledgementPath, 'r') as file:
filedata = file.read()
filedata = filedata.replace(
'?SidelineContent?', SidelineBase64.decode('utf-8'))
filedata = filedata.replace('?SidelineDocId?', SidelineDocumentId)
filedata = filedata.replace(
'?SidelineUniqueRef?', SidelineUniqueReferenceId)
with open(AcknowledgementPath, 'w') as file:
file.write(filedata)
with open(AcknowledgementPath, 'r') as file:
filedata = file.read()
filedata = filedata.replace(
'?GlosaContent?', GlosaBase64.decode('utf-8'))
filedata = filedata.replace('?GlosaDocId?', GlosaDocumentId)
filedata = filedata.replace(
'?GlosaUniqueRef?', GlosaUniqueReferenceId)
with open(AcknowledgementPath, 'w') as file:
file.write(filedata)
except Exception as e:
print("Error: %s" % e)
finally:
cnxn.close()
return AcknowledgementPath
GetMFile('23-00253I')
CreateACKs('23-00253I')

@ -0,0 +1,87 @@
import pyodbc as po
import email
import pymssql
import pandas as pd
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import time
import json
from os.path import basename
from email.mime.application import MIMEApplication
from email.utils import COMMASPACE, formatdate
import requests
with open("./config/config.json") as json_config_file:
config = json.load(json_config_file)
ServerSIR = config['ServerSIR']
DatabaseSIR = config['DatabaseSIR']
UsernameSIR = config['DBUserSIR']
PasswordSIR = config['DBPasswordSIR']
EmailServer = config['EmailServer']
EmailPort = config['EmailPort']
EmailUser = config['EmailUser']
EmailPassword = config['EmailPassword']
def IsNewPedimentoPayed():
try:
cnxn = po.connect(
"DRIVER={ODBC Driver 17 for 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 
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() AS Date )""")
for row in cursor:
print(row)
SendEmail(
["agarcia@gemcousa.com"],
"Aviso de pago de pedimento del cliente Amazon",
"Estimado colaborador se le comunica mediante este email, que se ha pagado un nuevo pedimento del cliente Amazon",
[]
)
requests.get(
url='https://localhost:5001/api/Notificaciones/SendWhattsapp?IdUsuario=30&Concepto=Nuevo pedimento pagado de Amazon&IdGrupo=1', verify=False)
except Exception as e:
print("Error: %s" % e)
finally:
cnxn.close()
return ''
def SendEmail(emailList, header, content, files):
for Receiver in emailList:
msg = MIMEMultipart()
msg['To'] = Receiver
msg['From'] = email.utils.formataddr(
('GEMCO informa : '+header, EmailUser))
msg['Subject'] = content
msg.attach(MIMEText(header))
sender = EmailUser
smtpObj = smtplib.SMTP(EmailServer, EmailPort)
''' for f in files or []:
with open(f, "rb") as fil:
part = MIMEApplication(fil.read(), Name=basename(f))
part["Content-Disposition"] = 'attachment; filename="%s"' % basename(
f)
msg.attach(part) '''
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login(EmailUser, EmailPassword)
try:
smtpObj.sendmail(sender, Receiver, msg.as_string())
print("Successfully sent email")
except:
print("Error: unable to send email")
IsNewPedimentoPayed()

@ -0,0 +1,82 @@
import pyodbc as po
import json
import shutil
import os
import datetime
import zipfile
import sys
import requests
import urllib3
import os.path
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 WriteToLog(Referencias):
with open('./log/Referencias.txt', 'a') as f:
Referencias = Referencias.replace('[', '')
Referencias = Referencias.replace(']', '')
f.write(f"{Referencias}\n")
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 CreateFolderContent():
try:
Files2Process = []
cnxn = po.connect(
"DRIVER={ODBC Driver 17 for 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]
Cliente = row[8]
ReferenceFolder = TempFolder + Referencia
if os.path.exists(ReferenceFolder): shutil.rmtree(ReferenceFolder)
shutil.copytree(RepositoryArchivoElectronicoOficial + str(YEAR) + '/' + Referencia, ReferenceFolder)
Files2Process.append(ReferenceFolder+'_____'+Cliente+'.zip')
ZipReference(Referencia, ReferenceFolder, Cliente)
URL = 'https://localhost:5001/api/AmazonInvoice/UploadSIRReference'
multiple_files=[]
for file in Files2Process:
print(file)
if os.path.exists(file):
multiple_files.append(('files', (file.replace(TempFolder,''), open(file, 'rb'), 'application/zip')))
r = requests.post(url=URL, files=multiple_files, verify=False, timeout=30)
print(r.text)
WriteToLog(r.text)
except Exception as e:
print("Error: %s" % e)
finally:
cnxn.close()
''' 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()

@ -0,0 +1 @@
C:\Python3112\python.exe C:\Projects\Python\Amazon\SyncReferences2IONOS.py

@ -4,8 +4,14 @@
"DBUser": "sa",
"DBPassword": "toor1234",
"downloadFolder": "C:/data/Clientes/2096/Invoices",
"email": "noreply@gemcousa.com",
"emailPassword": "N#16rm87",
"emailServer": "gemcousa-com.mail.protection.outlook.com",
"emailPort": 25
"EmailUser": "noreply@gemcousa.mx",
"EmailPassword": "Mx77lf%02",
"EmailServer": "smtp.ionos.com",
"EmailPort": 587,
"ServerSIR" : "192.168.100.11",
"DatabaseSIR": "SIR",
"DBUserSIR": "usersir",
"DBPasswordSIR": "S1r22$Us",
"ACKPathFiles": "C:/data/Clientes/2096/MFile/",
"RepositoryArchivoElectronicoOficial": "Z:/Recursos/ControlDocumentos/IMPORTADORA AMAZON MEXICO S DE RL DE CV/"
}

@ -0,0 +1,2 @@
"23-00253I","23-00197I","23-00180I","23-00251I","23-00257I"
"23-00253I","23-00197I","23-00180I","23-00251I","23-00257I"
Loading…
Cancel
Save