97 lines
3.3 KiB
Python
97 lines
3.3 KiB
Python
'''
|
|
Melissa asked to be e-mailed the total yardage of our un-shipped, un-invoiced finish goods inventory.
|
|
|
|
@author: Wes
|
|
'''
|
|
from alert import send_alert_html
|
|
|
|
import time, logging, sys, os, StringIO, csv
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
RECIPIENTS = ['admin@example.com', 'user1@example.com']
|
|
SUBJECT = 'FGI Report'
|
|
MESSAGE = None
|
|
|
|
TEST_MODE = False
|
|
logger = None
|
|
LOG_LEVEL = logging.DEBUG
|
|
|
|
SUM_QUERY = 'select SUM(RTPLG) [total_yards_produced] from TXRYRT00 WHERE (RTSLG = 0.0 AND RTILG = 0.0)'
|
|
DETAIL_QUERY = 'select RTALN [order_num], RTRNR [roll_num], RTANR [item_num], xartbz [item_desc], RTLOC [warehouse], RTLPL [bin], RTPLG [yards_produced], RTSLG [yards_shipped], RTILG [yards_invoiced] from TXRYRT00 LEFT JOIN txmyat03 ON RTANR = XANR where (RTSLG = 0.0 AND RTILG = 0.0) ORDER BY RTALN, RTRNR'
|
|
|
|
engine = create_engine("mssql+pyodbc://jsisbprod", echo=False)
|
|
Base = declarative_base(engine)
|
|
metadata = Base.metadata
|
|
|
|
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.DEBUG)
|
|
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.INFO)
|
|
sh.setFormatter(formatter)
|
|
logger.addHandler(sh)
|
|
|
|
def loadSession():
|
|
global engine
|
|
Session = sessionmaker(bind=engine)
|
|
session = Session()
|
|
return session
|
|
|
|
def run_FGI_report():
|
|
os.chdir('D:\FTP')
|
|
initializeLogger('FGI_Report')
|
|
logger.debug('Opening connection with DB...')
|
|
session = loadSession()
|
|
|
|
logger.debug('Running SUM query...')
|
|
for row in session.execute(SUM_QUERY):
|
|
logger.info('Total Finished Goods Inventory: %s yards', "{0:.3f}".format(row.total_yards_produced))
|
|
|
|
#Open file for writing
|
|
logger.debug('Creating Aaron\'s csv file...')
|
|
fname = time.strftime("FGI_%Y%m%d.csv")
|
|
logger.debug('Creating %s. Using \'excel\' dialect for csv writer.', fname)
|
|
f = open(fname, 'wb')
|
|
writer = csv.writer(f, dialect='excel')
|
|
writer.writerow(['order_num', 'roll_num', 'item_num', 'item_desc', 'warehouse', 'bin', 'yards_produced', 'yards_shipped', 'yards_invoiced'])
|
|
|
|
logger.debug('Running detail query...')
|
|
for row in session.execute(DETAIL_QUERY):
|
|
writer.writerow(row)
|
|
|
|
f.close()
|
|
|
|
try:
|
|
logger.debug('Sending e-mail...')
|
|
send_alert_html(RECIPIENTS, SUBJECT, attachments=[fname], message=MESSAGE.getvalue())
|
|
except:
|
|
logger.error('Unable to send e-mail. Is the internet out?')
|
|
|
|
logger.debug('Deleting %s', fname)
|
|
os.remove(fname)
|
|
|
|
if __name__ == '__main__':
|
|
run_FGI_report()
|