2024-03-14 08:20:47 -07:00
|
|
|
#!/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
|
2024-08-20 09:43:11 -07:00
|
|
|
import pprint
|
2024-03-14 08:20:47 -07:00
|
|
|
|
|
|
|
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():
|
2024-08-20 09:43:11 -07:00
|
|
|
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'],
|
|
|
|
)
|
2024-03-14 08:20:47 -07:00
|
|
|
|
|
|
|
def process_received_867s():
|
2024-08-20 09:43:11 -07:00
|
|
|
#pull out the control number on the GS line and the BOL# on PTD
|
2024-03-14 08:20:47 -07:00
|
|
|
for file in RECEIVED_867_DIRECTORY.iterdir():
|
|
|
|
control_number = ''
|
|
|
|
transaction_date = ''
|
2024-08-20 09:43:11 -07:00
|
|
|
picking_number = ''
|
|
|
|
customer_po = ''
|
|
|
|
shandex_so = ''
|
|
|
|
data_set = []
|
2024-03-14 08:20:47 -07:00
|
|
|
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]
|
2024-08-20 09:43:11 -07:00
|
|
|
# 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,
|
2024-03-14 08:20:47 -07:00
|
|
|
}
|
2024-08-20 09:43:11 -07:00
|
|
|
if data not in data_set:
|
|
|
|
data_set.append(data)
|
|
|
|
import_received_867s(data)
|
|
|
|
|
2024-03-14 08:20:47 -07:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|