shandex_edi_2024/update_shandex_dashboard.py

94 lines
2.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""
Update X3 with information from middleware
To reprocess a file, move it to the correct folder on the FTP
"""
import shutil
import yamamotoyama
import pathlib
import pprint
THIS_DIRECTORY = pathlib.Path(__file__).parent
RECEIVED_867_DIRECTORY = THIS_DIRECTORY / "processed_867s"
ARCHIVE = RECEIVED_867_DIRECTORY / "archive"
def main():
"""
Do it!
"""
process_received_867s()
remove_completed_867s()
def remove_completed_867s():
"""
Clean up all of the files received from the FTP
"""
for file in RECEIVED_867_DIRECTORY.iterdir():
if file.name.endswith('.edi'):
shutil.move(file,ARCHIVE / file.name)
def import_received_867s(data):
with yamamotoyama.get_connection() as data_base:
with data_base.transaction():
data_base.query(
"""
EXECUTE [PROD].[insert_shandex_867_data]
:ctrlnum,
:ylicplate,
:credat,
:customer_po,
:shandex_so
""",
ctrlnum=data['ctrlnum']+'-'+data['ylicplate'],
ylicplate=data['ylicplate'],
credat=data['credat'],
customer_po=data['customer_po'],
shandex_so=data['shandex_so'],
)
def process_received_867s():
#pull out the control number on the GS line and the BOL# on PTD
for file in RECEIVED_867_DIRECTORY.iterdir():
control_number = ''
transaction_date = ''
picking_number = ''
customer_po = ''
shandex_so = ''
data_set = []
if file.name.endswith('.edi'):
with file.open(encoding="utf-8", newline="") as edi_file:
for record in edi_file.read().split("~"):
fields = record.split("*")
if fields[0] == 'GS':
control_number = fields[6]
if fields[0] == 'BPT':
transaction_date = fields[3]
# REF*PO*3L49287E
if fields[0] == 'REF' and fields[1] == 'PO':
customer_po = fields[2]
# REF*IL*O0215276
if fields[0] == 'REF' and fields[1] == 'IL':
shandex_so = fields[2]
if fields[0] == "PTD" and len(fields) > 2:
picking_number = fields[5]
data = {
"ctrlnum":control_number,
"ylicplate":picking_number,
"credat":transaction_date,
"customer_po":customer_po,
"shandex_so":shandex_so,
}
if data not in data_set:
data_set.append(data)
import_received_867s(data)
if __name__ == "__main__":
main()