File: D:/bin/scripts/python3.4/expressmusic/expressmusic_stock_update - Copy.py
# (SS,27/08/15) Python script to update Express Music stock levels from separate MySQL databased called seanic
# Version of Python must be 3.4 or greater
import datetime
import mysql.connector
import sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import email.utils
def do_stock_update():
# get the source records
LSQL = """SELECT Stock_Levels.System_Code, SUM(Stock_Level - Allocated) AS StockLevel, RRP
FROM Stock_Levels
INNER JOIN Product ON Product.System_Code = Stock_Levels.System_Code
GROUP BY System_Code"""
cursor_source.execute(LSQL)
# empty the existing destination table
cursor_destin.execute("TRUNCATE TABLE seanic_stock_levels")
# copy the records from source to destination
for row in cursor_source.fetchall():
LSQL = "INSERT INTO seanic_stock_levels VALUES " + str(tuple(row));
cursor_destin.execute(LSQL)
# update the stocks levels in products table if different to seanic and product isn't disabled
LSQL = """UPDATE products p, seanic_stock_levels s
SET p.NumInStock = s.Stock_Level
WHERE s.System_Code = p.ProductCode AND
NOT p.ProductDisabled AND p.NumInStock <> s.Stock_Level"""
cursor_destin.execute(LSQL)
print_log("Product records updated: " + str(cursor_destin.rowcount))
# count products where prices don't match seanic
LSQL = """SELECT COUNT(*) FROM products p
INNER JOIN seanic_stock_levels s ON s.System_Code = p.ProductCode
WHERE NOT p.ProductDisabled AND p.StdPrice <> s.RRP AND COALESCE(p.SalePrice, 0) <> s.RRP"""
cursor_destin.execute(LSQL)
result = cursor_destin.fetchone()
print_log("Product records with different prices: " + str(result[0]))
def set_alert(AAlert):
global FAlert # required to modify a global
FAlert = AAlert
def get_alert():
return FAlert
def print_log(AText):
global FPrintLog # required to modify a global
print(AText)
FPrintLog = FPrintLog + AText + '\r\n'
def send_email_text(ABody, ASubject, AFrom, ATo, ABcc):
# Create a text/plain message
msg = MIMEText(ABody)
# header
if get_alert():
msg['Subject'] = 'ALERT!!! ' + ASubject
else:
msg['Subject'] = ASubject
msg['From'] = AFrom
msg['To'] = ATo
msg['Date'] = email.utils.formatdate(localtime=True)
if get_alert():
msg['X-Priority'] = '2'
# send the messsage using localhost
s = smtplib.SMTP('localhost')
s.sendmail(AFrom, [ATo, ABcc], msg.as_string())
s.quit()
FAlert = False # global used to set alert
FPrintLog = "" # global used to hold log that is also the email body
start = datetime.datetime.now()
print_log('Started: ' + start.strftime('%Y-%m-%d %H:%M:%S'))
print_log('')
try:
# open source connection seanic database
cnx_source = None
cursor_source = None
cnx_source = mysql.connector.connect(user='elvis_presley', password='Su5p1ci0u5',
host='data.seanicretail.co.uk',
database='Express_Music')
cursor_source = cnx_source.cursor()
# open destination connection
cnx_destin = None
cursor_destin = None
cnx_destin = mysql.connector.connect(user='expressmusic', password='bsharp',
host='localhost',
database='expressmusic')
cursor_destin = cnx_destin.cursor()
do_stock_update();
except:
set_alert(True)
print_log("Error:")
print_log(str(sys.exc_info()))
#raise
finally:
# close connections if they were opened
if cursor_destin:
cursor_destin.close()
if cnx_destin:
cnx_destin.commit()
cnx_destin.close()
if cursor_source:
cursor_source.close()
if cnx_source:
cnx_source.close()
finish = datetime.datetime.now()
time_taken = finish - start
print_log('')
print_log('Finished: ' + finish.strftime('%Y-%m-%d %H:%M:%S'))
print_log('Time taken: %0.3f seconds' % (time_taken.seconds + time_taken.microseconds / 1E6))
# send the text as an email, from address stock-update@expressmusicstore.co.uk doesn't actually exist as valid mailbox
send_email_text(FPrintLog, 'Express Music Stock Level Update Result', 'stock-update@expressmusicstore.co.uk', 'surinder@itpartnership.com', '')