122 lines
3.3 KiB
Python
122 lines
3.3 KiB
Python
'''
|
|
Created on Nov 30, 2016
|
|
|
|
UNIFI e-mails us "Pack and Hold" data daily. Interface this into Jomar
|
|
|
|
@author: Wes
|
|
'''
|
|
import datetime, logging, sys, os, StringIO, csv
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
from operator import itemgetter
|
|
from itertools import groupby
|
|
|
|
RECIPIENTS = ['admin@example.com']
|
|
SUBJECT = 'Inbound UNIFI 846 Processed'
|
|
MESSAGE = None
|
|
|
|
TEST_MODE = True
|
|
GR_AP_ACCOUNT = '002528'
|
|
JOMAR_PO_ORDER_TYPE = 'JB'
|
|
|
|
logger = None
|
|
LOG_LEVEL = logging.DEBUG
|
|
|
|
UNIFI_PLANT = 0
|
|
PO = 1
|
|
LOT = 2
|
|
STM_COL = 3
|
|
WHSE_DATE = 4
|
|
QTY = 5
|
|
PACKAGES = 6
|
|
CASES = 7
|
|
|
|
engine = create_engine("mssql+pyodbc://jsisbprod", echo=False)
|
|
Base = declarative_base(engine)
|
|
metadata = Base.metadata
|
|
|
|
def loadSession():
|
|
global engine
|
|
Session = sessionmaker(bind=engine)
|
|
session = Session()
|
|
return session
|
|
|
|
def initializeLogger(name='log'):
|
|
global logger
|
|
global MESSAGE
|
|
#logger = logging.getLogger(__name__)
|
|
logger = logging.getLogger(name)
|
|
if len(logger.handlers) > 0:
|
|
return logger
|
|
logger.setLevel(LOG_LEVEL)
|
|
handler = logging.FileHandler('D:\\FTP\\logs\\' + name + '.log')
|
|
handler.setLevel(LOG_LEVEL)
|
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
handler.setFormatter(formatter)
|
|
logger.addHandler(handler)
|
|
|
|
#Also send logs > DEBUG to stdout
|
|
ch = logging.StreamHandler(sys.stdout)
|
|
ch.setLevel(logging.INFO)
|
|
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
|
ch.setFormatter(formatter)
|
|
logger.addHandler(ch)
|
|
|
|
#Capture log messages in a string for e-mail alert
|
|
MESSAGE = StringIO.StringIO()
|
|
sh = logging.StreamHandler(MESSAGE)
|
|
sh.setLevel(logging.DEBUG)
|
|
sh.setFormatter(formatter)
|
|
logger.addHandler(sh)
|
|
|
|
def process_UNIFI_846(inFile):
|
|
global MESSAGE
|
|
global session
|
|
os.chdir('D:\FTP')
|
|
initializeLogger('UNIFI_interface')
|
|
logger.info('****************************************')
|
|
fileName = inFile.split('\\')[-1]
|
|
rawLines = []
|
|
session = loadSession()
|
|
|
|
logger.info('Loading: %s', inFile)
|
|
try:
|
|
f = open(inFile, 'r')
|
|
except IOError:
|
|
logger.fatal('Unable to load file. Did you specify the full path? Exiting...')
|
|
return
|
|
filename = inFile.split('\\')[-1]
|
|
|
|
try:
|
|
reader = csv.reader(f)
|
|
for row in reader:
|
|
rawLines.append(row)
|
|
finally:
|
|
f.close()
|
|
|
|
rawLines.pop(0) #Remove first line as it is just column headers
|
|
|
|
if(len(rawLines) == 0):
|
|
logger.warning('They sent us an empty file...')
|
|
logger.info('Moving %s to D:\\FTP\\OLD\\UNIFI\\', inFile)
|
|
os.rename(inFile, 'OLD\\UNIFI\\' + fileName)
|
|
session.close()
|
|
return
|
|
|
|
rawLines.sort(key=itemgetter(PO)) #Ensure raw data is sorted by PO number
|
|
purchaseOrders = groupby(rawLines, itemgetter(PO)) #Create group by iterator based on PO number
|
|
|
|
#For each unique PO appearing in this transmission
|
|
for poNumber, lines in purchaseOrders:
|
|
print poNumber
|
|
|
|
MESSAGE.truncate(0)
|
|
|
|
if __name__ == '__main__':
|
|
if(len(sys.argv) < 2):
|
|
print 'Must supply fully qualified path to csv file ex:'
|
|
print 'python unifi_interface.py D:\\FTP\\INCOMING\\ipnet_07052017.file846.csv.010690400'
|
|
else:
|
|
process_UNIFI_846(sys.argv[1])
|