Better report for 944s. Customer maps for 867s.
parent
b297ffe68a
commit
bb8cf98dbb
|
@ -67,6 +67,8 @@ X3_CUSTOMER_MAPPING = {
|
|||
'SAMP1000_0000' : 'YARI0001',
|
||||
'PURI1000_PURIQC' : 'PURI0005',
|
||||
'JIVA1000_JIVABC' : 'JIVA0002',
|
||||
'WELL1000_WELL' : 'WELL0002',
|
||||
'WELL1000_WELCAL' : 'WELL0003',
|
||||
}
|
||||
|
||||
def main():
|
||||
|
@ -182,6 +184,7 @@ def process_file(edi_filename: pathlib.Path):
|
|||
shipping_date = ''
|
||||
previous_picking_number = ''
|
||||
po_number = ''
|
||||
cust_po_number = ''
|
||||
warehouse_shipment = WarehouseShipment()
|
||||
for fields in tokens_from_edi_file(edi_filename):
|
||||
if fields[0] == "DTM":
|
||||
|
@ -190,7 +193,7 @@ def process_file(edi_filename: pathlib.Path):
|
|||
picking_number = fields[5]
|
||||
warehouse_shipment.header.ylicplate = f'{previous_picking_number}'
|
||||
if po_number != '':
|
||||
warehouse_shipment.header.yclippership = warehouse_shipment.header.ylicplate
|
||||
warehouse_shipment.header.yclippership = cust_po_number
|
||||
warehouse_shipment.header.ylicplate = f'{po_number}'
|
||||
if picking_number != previous_picking_number and previous_picking_number != '':
|
||||
if warehouse_shipment.header.bpdnam != 'Shandex Group':
|
||||
|
@ -206,6 +209,8 @@ def process_file(edi_filename: pathlib.Path):
|
|||
po_number = ''
|
||||
warehouse_shipment.header.ylicplate = f'{picking_number}'
|
||||
previous_picking_number = picking_number
|
||||
if fields[0] =='REF' and fields[1] == 'PO':
|
||||
cust_po_number = fields[2]
|
||||
if fields[0] =='REF' and fields[1] == 'IL':
|
||||
po_number = fields[2]
|
||||
if fields[0] == "N1" and fields[1] == 'ST':
|
||||
|
|
87
edi_944.py
87
edi_944.py
|
@ -14,6 +14,9 @@ import re
|
|||
import shutil
|
||||
import typing
|
||||
import pprint
|
||||
import smtplib
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
|
||||
import records # type: ignore
|
||||
|
||||
|
@ -26,14 +29,14 @@ IMPORTS_DIRECTORY = THIS_DIRECTORY / "x3_imports"
|
|||
EDI_997_DIRECTORY = THIS_DIRECTORY / "997_processing"
|
||||
|
||||
SHANDEX_944_FILENAME_RE = re.compile(
|
||||
r"\A 944_YAMAMOTOYAMA_ \S+ [.]edi \Z", re.X | re.M | re.S
|
||||
r"\A 944_STASH-YAMAMOTOYAMA_ \S+ [.]edi \Z", re.X | re.M | re.S
|
||||
)
|
||||
|
||||
def main():
|
||||
"""
|
||||
Do it!
|
||||
"""
|
||||
for edi_filename in X12_DIRECTORY.iterdir(): #TODO uncomment and review
|
||||
for edi_filename in X12_DIRECTORY.iterdir():
|
||||
if SHANDEX_944_FILENAME_RE.match(edi_filename.name):
|
||||
process_file(edi_filename)
|
||||
# file moved to 997 processing folder to be sent later
|
||||
|
@ -41,6 +44,33 @@ def main():
|
|||
combine_zpthis()
|
||||
|
||||
|
||||
def new_944_alert(sdhnum, pohnum, rcpdat):
|
||||
msg = MIMEMultipart()
|
||||
msg['Subject'] = 'New Receipt from Shandex'
|
||||
msg['Precedence'] = 'bulk'
|
||||
msg['From'] = 'x3report@stashtea.com'
|
||||
msg['To'] = 'isenn@yamamotoyama.com'
|
||||
msg['CC'] = 'bleeson@stashtea.com'
|
||||
emailtext = f'Delivery: {sdhnum}\nPO: {pohnum}\nDate: {rcpdat}'
|
||||
msg.attach(MIMEText(emailtext, 'plain'))
|
||||
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp:
|
||||
smtp.login(user='x3reportmk2@yamamotoyama.com', password=r'n</W<7fr"VD~\2&[pZc5')
|
||||
smtp.send_message(msg)
|
||||
|
||||
|
||||
def validation_alert(sdhnum):
|
||||
msg = MIMEMultipart()
|
||||
msg['Subject'] = 'New Receipt from Shandex'
|
||||
msg['Precedence'] = 'bulk'
|
||||
msg['From'] = 'x3report@stashtea.com'
|
||||
#msg['To'] = 'isenn@yamamotoyama.com'
|
||||
msg['CC'] = 'bleeson@stashtea.com'
|
||||
emailtext = f'A Shandex receipt for {sdhnum} could not be loaded into X3 because the shipment is not validated.'
|
||||
msg.attach(MIMEText(emailtext, 'plain'))
|
||||
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp:
|
||||
smtp.login(user='x3reportmk2@yamamotoyama.com', password=r'n</W<7fr"VD~\2&[pZc5')
|
||||
smtp.send_message(msg)
|
||||
|
||||
def combine_zpthis():
|
||||
"""
|
||||
Collect all ZPTHI imports into a single file for easy import.
|
||||
|
@ -73,7 +103,7 @@ def tokens_from_edi_file(
|
|||
with edi_filename.open(encoding="utf-8", newline="") as edi_file:
|
||||
for record in edi_file.read().split("~"):
|
||||
fields = record.split("*")
|
||||
if fields[0] in { #TODO see if there are more fields used in vendor EDI
|
||||
if fields[0] in {
|
||||
"ISA",
|
||||
"ST",
|
||||
"N2",
|
||||
|
@ -85,15 +115,53 @@ def tokens_from_edi_file(
|
|||
yield fields
|
||||
|
||||
|
||||
def find_shipment_line(sdhnum, itmref):
|
||||
with yamamotoyama.get_connection() as database:
|
||||
result = database.query(
|
||||
"""
|
||||
select
|
||||
SDDLIN_0
|
||||
from PROD.SDELIVERYD
|
||||
where
|
||||
SDHNUM_0 = :sdhnum
|
||||
and ITMREF_0 = :itmref
|
||||
""",
|
||||
sdhnum=sdhnum,
|
||||
itmref=itmref
|
||||
).first()['SDDLIN_0']
|
||||
return result
|
||||
|
||||
|
||||
def check_shipment_status(delivery):
|
||||
with yamamotoyama.get_connection() as database:
|
||||
result = database.query(
|
||||
"""
|
||||
select
|
||||
SDH.SDHNUM_0,
|
||||
CFMFLG_0
|
||||
from PROD.SDELIVERY SDH
|
||||
where SDH.SDHNUM_0 = :sdhnum
|
||||
""",
|
||||
sdhnum=delivery
|
||||
).first()['CFMFLG_0']
|
||||
if result == 2:
|
||||
return True
|
||||
return False
|
||||
|
||||
def process_file(edi_filename: pathlib.Path):
|
||||
"""
|
||||
Convert a specific EDI file into an import file.
|
||||
"""
|
||||
warehouse_receipt = Receipt()
|
||||
pohnum = ''
|
||||
for fields in tokens_from_edi_file(edi_filename):
|
||||
if fields[0] == "W17":
|
||||
_, _, rcpdat, _, sohnum, sdhnum = fields[:6]
|
||||
warehouse_receipt.sdhnum = sdhnum
|
||||
validated = check_shipment_status(sdhnum)
|
||||
if not validated:
|
||||
validation_alert(sdhnum)
|
||||
break
|
||||
warehouse_receipt.header.rcpdat = datetime.datetime.strptime(
|
||||
rcpdat, "%Y%m%d"
|
||||
).date() # 20230922
|
||||
|
@ -109,7 +177,8 @@ def process_file(edi_filename: pathlib.Path):
|
|||
)
|
||||
if fields[0] == 'N9' and fields[1] == 'LI':
|
||||
# N9*LI*1000
|
||||
line = fields[2]
|
||||
#line = fields[2] #This line isn't the line number from X3, it needs to be looked up
|
||||
line = find_shipment_line(warehouse_receipt.sdhnum, itmref)
|
||||
warehouse_receipt.append(
|
||||
ReceiptDetail(
|
||||
sdhnum=warehouse_receipt.sdhnum,
|
||||
|
@ -121,7 +190,7 @@ def process_file(edi_filename: pathlib.Path):
|
|||
subdetail,
|
||||
)
|
||||
time_stamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
|
||||
new_944_alert(sdhnum, pohnum, warehouse_receipt.header.rcpdat)
|
||||
with yamamotoyama.x3_imports.open_import_file(
|
||||
IMPORTS_DIRECTORY / f"ZPTHI_{warehouse_receipt.sdhnum}_{time_stamp}.dat"
|
||||
) as import_file:
|
||||
|
@ -146,7 +215,7 @@ class ReceiptSubDetail:
|
|||
"""
|
||||
Convert grouped lot quantities into individual STOJOU records to fit on receipt
|
||||
"""
|
||||
with yamamotoyama.get_connection('test') as database: #todo remove 'test'
|
||||
with yamamotoyama.get_connection() as database:
|
||||
details = (
|
||||
database.query(
|
||||
"""
|
||||
|
@ -158,7 +227,7 @@ class ReceiptSubDetail:
|
|||
[STJ].[LOT_0],
|
||||
'' [BPSLOT_0],
|
||||
'' [SERNUM_0]
|
||||
from [FY23TEST].[STOJOU] [STJ] --TODO change to PROD
|
||||
from [PROD].[STOJOU] [STJ]
|
||||
where
|
||||
[STJ].[VCRNUM_0] = :sdhnum
|
||||
and [STJ].[ITMREF_0] = :itmref
|
||||
|
@ -427,7 +496,7 @@ class Receipt:
|
|||
"""
|
||||
Fetch shipment from X3 database.
|
||||
"""
|
||||
with yamamotoyama.get_connection('test') as db_connection:#todo remove 'test'
|
||||
with yamamotoyama.get_connection() as db_connection:
|
||||
return db_connection.query(
|
||||
"""
|
||||
select
|
||||
|
@ -437,7 +506,7 @@ class Receipt:
|
|||
[SDH].[BPCORD_0],
|
||||
[SDH].[CUR_0],
|
||||
[SDH].[SOHNUM_0]
|
||||
from [FY23TEST].[SDELIVERY] [SDH]--TODO change back to [PROD]
|
||||
from [PROD].[SDELIVERY] [SDH]
|
||||
where
|
||||
[SDH].[SDHNUM_0] = :shipment
|
||||
""",
|
||||
|
|
Loading…
Reference in New Issue