From 4ad4ad79a56261811dab22e1c3b37f9929de4218 Mon Sep 17 00:00:00 2001 From: bleeson Date: Thu, 14 Mar 2024 08:20:47 -0700 Subject: [PATCH] Change to tracking number format. Added "dashboard" information. --- edi_867.py | 4 +-- update_shandex_dashboard.py | 68 +++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 update_shandex_dashboard.py diff --git a/edi_867.py b/edi_867.py index 30d6e0d..26a1bad 100644 --- a/edi_867.py +++ b/edi_867.py @@ -40,7 +40,7 @@ def main(): if SOURCE_867_FILENAME_RE.match(edi_filename.name): process_file(edi_filename) #TODO respond with 997? - #shutil.move(edi_filename, X12_DIRECTORY / "archive" / edi_filename.name)#TODO uncomment + #shutil.move(edi_filename, THIS_DIRECTORY / "processed_867s" / edi_filename.name)#TODO uncomment combine_zship867s() @@ -114,7 +114,7 @@ def process_file(edi_filename: pathlib.Path): for fields in tokens_from_edi_file(edi_filename): if fields[0] == "GS": control_number = fields[5] - warehouse_shipment.header.ylicplate = f'SHANDEX-{control_number}' + warehouse_shipment.header.ylicplate = f'{control_number}' if fields[0] == "DTM": date_field = fields[2] warehouse_shipment.header.shidat = datetime.datetime.strptime( diff --git a/update_shandex_dashboard.py b/update_shandex_dashboard.py new file mode 100644 index 0000000..c741b3f --- /dev/null +++ b/update_shandex_dashboard.py @@ -0,0 +1,68 @@ +#!/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 + +THIS_DIRECTORY = pathlib.Path(__file__).parent +RECEIVED_867_DIRECTORY = THIS_DIRECTORY / "processed_867s" +ARCHIVE = RECEIVED_867_DIRECTORY / "archive" + + +INSERT_867s = """\ + execute [PROD].[insert_shandex_867_data] + :ctrlnum, + :credat; + """ + + +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.bulk_query(INSERT_867s, data) + + +def process_received_867s(): + #pull out the control number on the GS line + for file in RECEIVED_867_DIRECTORY.iterdir(): + control_number = '' + transaction_date = '' + 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] + data = { + "ctrlnum":control_number, + "credat":transaction_date + } + import_received_867s(data) + + +if __name__ == "__main__": + main()