Rev 7594 | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 25-Jun-2012@author: anupam'''#!/usr/bin/pythonimport jsonimport urllib2, cookielibimport MySQLdbimport datetimeimport sysimport smtplibfrom email import encodersfrom email.mime.text import MIMETextfrom email.mime.base import MIMEBasefrom email.mime.multipart import MIMEMultipartfrom pyExcelerator import Workbook, Font, XFStylefrom datetime import date, timedelta# Initialize db connection settings.DB_HOST = "localhost"DB_USER = "root"DB_PASSWORD = "shop2020"DB_NAME = "warehouse"MAILTO = ['khushal.bhatia@shop2020.in', 'chaitnaya.vats@shop2020.in', 'rajneesh.arora@shop2020.in', 'chandan.kumar@shop2020.in', 'sandeep.sachdeva@shop2020.in', 'manish.sharma@shop2020.in', 'kshitij.sood@shop2020.in', 'manoj.kumar@shop2020.in', 'chandan.kumar@shop2020.in']SENDER = "cnc.center@shop2020.in"PASSWORD = "5h0p2o2o"SUBJECT = "Vendor Fulfilment Report"SMTP_SERVER = "smtp.gmail.com"SMTP_PORT = 587TMP_FILE="/tmp/vendor_fulfilment.xls"def getDbConnection():return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)def closeConnection(conn):conn.close()def getProductSaleData(fromDate, toDate):selectSql = '''SELECT s.name,CONCAT_WS(' ', l.brand, ifnull(l.modelname, ''), l.modelnumber, ifnull(l.color, '')) AS Product,SUM(l.quantity) AS `Qty Ordered`,SUM(l.unfulfilledQuantity) AS `Qty Unfulfilled`FROM lineitem lJOIN purchaseorder p ON l.purchaseOrder_id = p.idJOIN supplier s ON p.supplierId = s.idWHERE l.createdAt BETWEEN \'''' + fromDate + '''\' AND \'''' + toDate + '''\' GROUP BY s.name, l.itemId;'''conn = getDbConnection()#data = {}try:# prepare a cursor object using cursor() methodcursor = conn.cursor()# Execute the SQL command# Fetch source id.cursor.execute(selectSql)result = cursor.fetchall()except Exception as e:print "Error: unable to fetch data"print ereturn resultdef createXlsReport(wb, data, sheetNumber):if sheetNumber == 1:sheetName = "Yesterday"if sheetNumber == 2:sheetName = "MTD"worksheet = wb.add_sheet(sheetName)boldStyle = XFStyle()f = Font()f.bold = TrueboldStyle.font = frow = 0worksheet.write(row, 0, "VENDOR", boldStyle)worksheet.write(row, 1, "PRODUCT", boldStyle)worksheet.write(row, 2, "QTY REQUIRED", boldStyle)worksheet.write(row, 3, "QTY UNFULFILLED", boldStyle)row += 2for datum in data:worksheet.write(row, 0, datum[0])worksheet.write(row, 1, datum[1])worksheet.write(row, 2, datum[2])worksheet.write(row, 3, datum[3])row += 1wb.save(TMP_FILE)def sendmail():mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)mailServer.ehlo()mailServer.starttls()mailServer.ehlo()# Create the container (outer) email message.msg = MIMEMultipart()msg['Subject'] = SUBJECT + ' - ' + date.today().isoformat()msg['From'] = "PO@saholic.com"msg['To'] = 'sku-recipients@saholic.com'msg.preamble = SUBJECT + ' - ' + date.today().isoformat()fileMsg = MIMEBase('application','vnd.ms-excel')fileMsg.set_payload(file(TMP_FILE).read())encoders.encode_base64(fileMsg)fileMsg.add_header('Content-Disposition','attachment;filename=Vendor-Fulfilment' + ' - ' + date.today().isoformat() + '.xls')msg.attach(fileMsg)mailServer.login(SENDER, PASSWORD)mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())def main():workbook = Workbook()timeNow = datetime.datetime.now()toDate = timeNow.strftime('%Y-%m-%d %H:%M:%S')fromDate = (timeNow - timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S')data = getProductSaleData(fromDate, toDate)createXlsReport(workbook, data, 1)fromDate = datetime.datetime(datetime.datetime.now().year, datetime.datetime.now().month, 1, 0, 0, 0, 0)data = getProductSaleData(str(fromDate), str(toDate))createXlsReport(workbook, data, 2)sendmail()if __name__ == '__main__':main()