Rev 17445 | Rev 18463 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on Mar 13, 2015@author: amit'''from datetime import date, datetime, timedeltafrom dtr.main import Store, sourceMapfrom dtr.reports.amazonreco import generateAmazonRecofrom dtr.storage import Mysql, DataServicefrom dtr.storage.DataService import Users, Clicks, FlipkartOrders,\All_user_addressesfrom dtr.storage.Mysql import getOrdersAfterDate, getOrdersByTagfrom dtr.utils import utilsfrom elixir import *from sqlalchemy.sql.expression import func, func, or_from email import encodersfrom email.mime.base import MIMEBasefrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom pymongo.mongo_client import MongoClientfrom xlrd import open_workbookfrom xlutils.copy import copyfrom xlwt.Workbook import Workbookimport _mysqlimport MySQLdbimport smtplibimport timeimport xlwtfrom dtr.utils import utilsfrom dtr.utils.utils import toTimeStampfrom dtr.reports import getdata#from xlwt importDataService.initialize()client = MongoClient('mongodb://localhost:27017/')SENDER = "cnc.center@shop2020.in"PASSWORD = "5h0p2o2o"SUBJECT = "DTR User Segmentation report for " + date.today().isoformat()SMTP_SERVER = "smtp.gmail.com"SMTP_PORT = 587XLS_FILENAME = "snapdealaffiliatereco.xls"XLS_O_FILENAME = "allorders.xls"XLS_F_FILENAME = "flipkartaffiliatereco.xls"XLS_A_FILENAME = "amazonaffiliatereco.xls"XLS_SC_FILENAME = "shopcluesaffiliatereco.xls"XLS_HS_FILENAME = "homeshopaffiliatereco.xls"boldStyle = xlwt.XFStyle()f = xlwt.Font()f.bold = TrueboldStyle.font = fi = -1datetime_format = xlwt.XFStyle()datetime_format.num_format_str = 'dd/mm/yyyy HH:MM AM/PM'categoryMap = {3:"Mobiles", 5:"Tablets"}curDate = date.today()cutOffMonth = (datetime.now().month + 11)%12cutOffYear = curDate.year - (0 if curDate.month-1 > 0 else 1)cutOff = datetime(cutOffYear, cutOffMonth, 1)def generateFlipkartReco():global ii = -1curs = getOrdersAfterDate(cutOff, 2)db = client.Dtrwb = xlwt.Workbook()#rb = open_workbook(XLS_F_FILENAME)#wb = copy(rb)#wb = xlwt.Workbook()row = 0row2 = 0row3 = 0row4 = 0worksheet = wb.add_sheet("All Orders")worksheet1 = wb.add_sheet("Orders Reconciled")worksheet2 = wb.add_sheet("Orders not Reconciled")worksheet3 = wb.add_sheet("Aff not Reconciled")worksheet.write(row, inc(), 'Order Id', boldStyle)worksheet1.write(row, i, 'Order Id', boldStyle)worksheet2.write(row, i, 'Order Id', boldStyle)worksheet.write(row, inc(), 'User Id', boldStyle)worksheet1.write(row, i, 'User Id', boldStyle)worksheet2.write(row, i, 'User Id', boldStyle)worksheet.write(row, inc(), 'Username', boldStyle)worksheet1.write(row, i, 'Username', boldStyle)worksheet2.write(row, i, 'Username', boldStyle)worksheet.write(row, inc(), 'Version code', boldStyle)worksheet1.write(row, i, 'Version code', boldStyle)worksheet2.write(row, i, 'Version code', boldStyle)worksheet.write(row, inc(), 'Device', boldStyle)worksheet1.write(row, i, 'Device', boldStyle)worksheet2.write(row, i, 'Device', boldStyle)worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)worksheet1.write(row, i, 'Merchant Order Id', boldStyle)worksheet2.write(row, i, 'Merchant Order Id', boldStyle)worksheet.write(row, inc(), 'Product Title', boldStyle)worksheet1.write(row, i, 'Product Title', boldStyle)worksheet2.write(row, i, 'Product Title', boldStyle)worksheet.write(row, inc(), 'Price', boldStyle)worksheet1.write(row, i, 'Price', boldStyle)worksheet2.write(row, i, 'Price', boldStyle)worksheet.write(row, inc(), 'Quantity', boldStyle)worksheet1.write(row, i, 'Quantity', boldStyle)worksheet2.write(row, i, 'Quantity', boldStyle)worksheet.write(row, inc(), 'Status', boldStyle)worksheet1.write(row, i, 'Status', boldStyle)worksheet2.write(row, i, 'Status', boldStyle)worksheet.write(row, inc(), 'Detailed Status', boldStyle)worksheet1.write(row, i, 'Detailed Status', boldStyle)worksheet2.write(row, i, 'Detailed Status', boldStyle)worksheet.write(row, inc(), 'Cashback Status', boldStyle)worksheet1.write(row, i, 'Cashback Status', boldStyle)worksheet2.write(row, i, 'Cashback Status', boldStyle)worksheet.write(row, inc(), 'Our CashBack', boldStyle)worksheet1.write(row, i, 'Our CashBack', boldStyle)worksheet2.write(row, i, 'Our CashBack', boldStyle)worksheet.write(row, inc(), 'Sale Date', boldStyle)worksheet1.write(row, i, 'Sale Date', boldStyle)worksheet2.write(row, i, 'Sale Date', boldStyle)worksheet.write(row, inc(), 'SubtagId', boldStyle)worksheet1.write(row, i, 'SubtagId', boldStyle)worksheet2.write(row, i, 'SubtagId', boldStyle)worksheet.write(row, inc(), 'Category', boldStyle)worksheet1.write(row, i, 'Category', boldStyle)worksheet.write(row, inc(), 'Aff PayOut', boldStyle)worksheet1.write(row, i, 'Aff PayOut', boldStyle)worksheet.write(row, inc(), 'Aff Sale Amount', boldStyle)worksheet1.write(row, i, 'Aff Sale Amount', boldStyle)worksheet.write(row, inc(), 'Aff Sale Date', boldStyle)worksheet1.write(row, i, 'Aff Sale Date', boldStyle)for row1 in curs:orderId = row1[0]order = db.merchantOrder.find_one({"orderId":orderId})if order is None:continuesaleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%d %B, %Y %I:%M %p").replace(hour=0, minute=0, second=0, microsecond=0).timetuple()))#saleTime1 = int(time.mktime((datetime.strptime(order['placedOn'], "%b %d, %Y %I:%M %p") + timedelta(seconds=-60)).timetuple()))reconciled_affiliates = []for subOrder in order['subOrders']:affs = list(db.flipkartOrderAffiliateInfo.find({"productCode":subOrder.get("productCode"), "saleDateInt":saleTime}))row += 1i=-1worksheet.write(row, inc(), orderId)worksheet.write(row, inc(), row1[1])worksheet.write(row, inc(), row1[-1])worksheet.write(row, inc(), row1[-2])worksheet.write(row, inc(), row1[-3])worksheet.write(row, inc(), order['merchantOrderId'])worksheet.write(row, inc(), subOrder['productTitle'])worksheet.write(row, inc(), subOrder['amountPaid'])worksheet.write(row, inc(), subOrder['quantity'])worksheet.write(row, inc(), subOrder['status'])worksheet.write(row, inc(), subOrder['detailedStatus'])worksheet.write(row, inc(), subOrder['cashBackStatus'])worksheet.write(row, inc(), subOrder['cashBackAmount'])worksheet.write(row, inc(), subOrder['placedOn'])worksheet.write(row, inc(), order['subTagId'])for aff in affs:if aff["_id"] not in reconciled_affiliates and aff['subTagId']== order['subTagId']:reconciled_affiliates.append(aff["_id"])row2 += 1i = -1worksheet1.write(row2, inc(), orderId)worksheet1.write(row2, inc(), row1[1])worksheet1.write(row2, inc(), row1[-1])worksheet1.write(row2, inc(), row1[-2])worksheet1.write(row2, inc(), row1[-3])worksheet1.write(row2, inc(), order['merchantOrderId'])worksheet1.write(row2, inc(), subOrder['productTitle'])worksheet1.write(row2, inc(), subOrder['amountPaid'])worksheet1.write(row2, inc(), subOrder['quantity'])worksheet1.write(row2, inc(), subOrder['status'])worksheet1.write(row2, inc(), subOrder['detailedStatus'])worksheet1.write(row2, inc(), subOrder['cashBackStatus'])worksheet1.write(row2, inc(), subOrder['cashBackAmount'])worksheet1.write(row2, inc(), subOrder['placedOn'])worksheet1.write(row2, inc(), order['subTagId'])db.flipkartOrderAffiliateInfo.update({"productCode":subOrder.get("productCode"), "saleDateInt":saleTime, "subTagId":aff['subTagId']},{"$set":{"reconciled":True}})db.merchantOrder.update({"merchantOrderId":order["merchantOrderId"]},{"$set":{"reconciled":True}})worksheet.write(row, inc(), aff['category'])worksheet1.write(row2, i, aff['category'])worksheet.write(row, inc(), aff['payOut'])worksheet1.write(row2, i, aff['payOut'])worksheet.write(row, inc(), aff['saleAmount'])worksheet1.write(row2, i, aff['saleAmount'])worksheet.write(row, inc(), aff['saleDate'])worksheet1.write(row2, i, aff['saleDate'])breakrow3 += 1i=-1worksheet2.write(row3, inc(), orderId)worksheet2.write(row3, inc(), row1[1])worksheet2.write(row3, inc(), row1[-1])worksheet2.write(row3, inc(), row1[-2])worksheet2.write(row3, inc(), row1[-3])worksheet2.write(row3, inc(), order['merchantOrderId'])worksheet2.write(row3, inc(), subOrder['productTitle'])worksheet2.write(row3, inc(), subOrder['amountPaid'])worksheet2.write(row3, inc(), subOrder['quantity'])worksheet2.write(row3, inc(), subOrder['status'])worksheet2.write(row3, inc(), subOrder['detailedStatus'])worksheet2.write(row3, inc(), subOrder['cashBackStatus'])worksheet2.write(row3, inc(), subOrder['cashBackAmount'])worksheet2.write(row3, inc(), subOrder['placedOn'])worksheet2.write(row3, inc(), order['subTagId'])i=-1worksheet3.write(0, inc(), "category",boldStyle)worksheet3.write(0, inc(), "conversionStatus",boldStyle)worksheet3.write(0, inc(), "Product Title",boldStyle)worksheet3.write(0, inc(), "price",boldStyle)worksheet3.write(0, inc(), "quantity",boldStyle)worksheet3.write(0, inc(), "payOut",boldStyle)worksheet3.write(0, inc(), "saleDate",boldStyle)worksheet3.write(0, inc(), "subTagId",boldStyle)for aff in db.flipkartOrderAffiliateInfo.find({"reconciled":{"$exists":False}}):row4 += 1i =-1worksheet3.write(row4, inc(), aff["category"])worksheet3.write(row4, inc(), aff["conversionStatus"])skuData = getSkuData(2, aff["productCode"])if skuData is None:worksheet3.write(row4, inc(), aff["productTitle"])else:worksheet3.write(row4, inc(), skuData["product_name"])worksheet3.write(row4, inc(), aff['price'])worksheet3.write(row4, inc(), aff['quantity'])worksheet3.write(row4, inc(), aff['payOut'])worksheet3.write(row4, inc(), aff['saleDate'])worksheet3.write(row4, inc(), aff['subTagId'])wb.save(XLS_F_FILENAME)def generateSnapDealReco():global icurs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 3)db = client.DtrmatchedList = []workbook = xlwt.Workbook()worksheet = workbook.add_sheet("Snapdeal Reconciled")worksheet2 = workbook.add_sheet("Snapdeal Orders not reconciled")affNotReconciledSheet = workbook.add_sheet("Snapdeal Aff not reconciled")worksheet3 = workbook.add_sheet("DateWise Sale Summary")worksheet1 = workbook.add_sheet("Snapdeal Reconciled All")setHeaders(worksheet, worksheet1, worksheet2, affNotReconciledSheet, worksheet3)row = 0row5=0for row1 in curs:orderId = row1[0]order = db.merchantOrder.find_one({"orderId":orderId})if order is None:continue#saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").timetuple()))try:saleTime = datetime.strptime(order['placedOn'], "%a, %d %b, %Y").strftime("%Y-%m-%d")except:saleTime = datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").strftime("%Y-%m-%d")try:formattedTimestamp = order['placedOn']timestamp1 = int(time.mktime(datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").timetuple()))except:timestamp1 = int(time.mktime(row1[-4].timetuple()))formattedTimestamp = datetime.strftime(utils.fromTimeStamp(timestamp1), "%b %d, %Y, %I:%M %p")affs = list(db.snapdealOrderAffiliateInfo1.find({"orderId":order["merchantOrderId"]}))if len(affs) > 0:for aff in affs:if aff.get("missingOrder"):aff["missingOrder"] = Falsedb.snapdealOrderAffiliateInfo1.save(aff)matchedList.append(order["merchantOrderId"])order['reconciled'] = Truerow += 1i=-1worksheet.write(row, inc(), orderId)worksheet.write(row, inc(), row1[1])worksheet.write(row, inc(), row1[-1])worksheet.write(row, inc(), row1[-2])worksheet.write(row, inc(), row1[-3])worksheet.write(row, inc(), order['merchantOrderId'])worksheet.write(row, inc(), order['subTagId'])worksheet.write(row, inc(), order['placedOn'])worksheet.write(row, inc(), formattedTimestamp)worksheet.write(row, inc(), int(order['paidAmount']))k = irow -= 1for subOrder in order['subOrders']:i = kmatched=Falserow += 1for aff in affs:if subOrder['productTitle']==aff['productCode']:worksheet.write(row, inc(), utils.fromTimeStamp(aff['saleDate']),datetime_format)worksheet.write(row, inc(), aff['unitPrice'])worksheet.write(row, inc(), 0 if not aff.get('payOut') else aff['payOut']/aff.get('quantity'))worksheet.write(row, inc(), aff.get('status'))subOrder['affStatus'] = aff.get('status')matched=Truebreakif not matched:inc()inc()inc()inc()worksheet.write(row, inc(), subOrder['productTitle'])worksheet.write(row, inc(), subOrder['amountPaid'])worksheet.write(row, inc(), subOrder['quantity'])worksheet.write(row, inc(), subOrder['status'])worksheet.write(row, inc(), subOrder['cashBackStatus'])worksheet.write(row, inc(), subOrder['cashBackAmount'])db.merchantOrder.save(order)else:row5 += 1i=-1worksheet2.write(row5, inc(), order["orderId"])worksheet2.write(row5, inc(), row1[1])worksheet2.write(row5, inc(), row1[-1])worksheet2.write(row5, inc(), row1[-2])worksheet2.write(row5, inc(), row1[-3])worksheet2.write(row5, inc(), order['merchantOrderId'])worksheet2.write(row5, inc(), order['subTagId'])worksheet2.write(row5, inc(), order['placedOn'])worksheet2.write(row5, inc(), formattedTimestamp)worksheet2.write(row5, inc(), int(order['paidAmount']))row5 -=1k=ifor subOrder in order['subOrders']:i = krow5 += 1worksheet2.write(row5, inc(), subOrder['productTitle'])worksheet2.write(row5, inc(), subOrder['amountPaid'])worksheet2.write(row5, inc(), subOrder['quantity'])worksheet2.write(row5, inc(), subOrder['status'])worksheet2.write(row5, inc(), subOrder['cashBackStatus'])worksheet2.write(row5, inc(), subOrder['cashBackAmount'])last30Days = date.today() - timedelta(days=30)int(time.mktime(last30Days.timetuple()))row = 0for offer in db.snapdealOrderAffiliateInfo1.find({"missingOrder":True,"saleDate":{"$gte":int(time.mktime(last30Days.timetuple())),"$lt":int(time.mktime(date.today().timetuple()))}} ):row += 1i=-1affNotReconciledSheet.write(row, inc(), offer.get("subTagId"))affNotReconciledSheet.write(row, inc(), offer.get("orderId"))affNotReconciledSheet.write(row, inc(), utils.fromTimeStamp(offer.get("saleDate")), datetime_format)affNotReconciledSheet.write(row, inc(), offer.get("saleAmount"))affNotReconciledSheet.write(row, inc(), offer.get("productCode"))affNotReconciledSheet.write(row, inc(), offer.get("quantity"))affNotReconciledSheet.write(row, inc(), offer.get("payOut"))mailBodyTemplate="""<html><body><table cellspacing="0" border="1" style="text-align:right"><thead><tr><th style="text-align:center">Date</th><th style="text-align:center">Sale</th><th style="text-align:center">Approved</th><th style="text-align:center">Approved %</th><th style="text-align:center">Rejected</th><th style="text-align:center">Rejected %</th><th style="text-align:center">Missing %</th><th style="text-align:center">Approved % against Sale</th></tr></thead><tbody>{0}</tbody></table></body></html>"""tbody=[]rowtemplate="<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td></tr>"for offer in db.flSaleSnapshot.find().limit(61).sort("_id",-1):approved = offer.get("approvedAmount")rejected = offer.get("rejectedAmount")sale = offer.get("subAmount")if not sale:sale = offer.get("paidAmount")total = approved + rejectedif total ==0:continuetbody.append(rowtemplate.format(offer.get("_id"), sale, approved, int((approved*100/total)), rejected, int((rejected*100/total)), int((sale-approved-rejected)*100/sale),int((approved)*100/sale)))message = mailBodyTemplate.format("".join(tbody))workbook.save(XLS_FILENAME)return messagedef generateHomeShopReco():global icurs = getOrdersAfterDate(datetime.now() - timedelta(days=90), 7)db = client.DtrnotReconciled = {}matchedList = []workbook = xlwt.Workbook()worksheet = workbook.add_sheet("HomeShop Reconciled")worksheet1 = workbook.add_sheet("HomeShop Reconciled All")worksheet2 = workbook.add_sheet("HomeShop Orders not reconciled")affNotReconciledSheet = workbook.add_sheet("HomeShop Aff not reconciled")setHeaders(worksheet, worksheet1, worksheet2, affNotReconciledSheet)row = 0anotherrow = 0row2 = 0row5=0for row1 in curs:orderId = row1[0]order = db.merchantOrder.find_one({"orderId":orderId})if order is None:continue#saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").timetuple())) 26 May 2015 02:12 PMaff = list(db.shopcluesOrderAffiliateInfo.find({"merchantOrderId":order["merchantOrderId"]}))anotherrow += 1try:formattedTimestamp = order['placedOn']timestamp1 = int(time.mktime(datetime.strptime(order['placedOn'], "%d %b %Y %I:%M %p").timetuple()))except:timestamp1 = int(time.mktime(row1[-4].timetuple()))formattedTimestamp = datetime.strftime(utils.fromTimeStamp(timestamp1), "%d %b %Y %I:%M %p")if len(aff) > 0 and order["subTagId"] not in matchedList and long(order["paidAmount"])==long(float(aff[0]["transactionValue"])):matchedList.append(order["subTagId"])db.shopcluesOrderAffiliateInfo.update({"merchantOrderId":aff[0].get("merchantOrderId")}, {"$set":{"reconciled":True, "orderId":orderId}}, multi=True)order['reconciled'] = Trueorder['adId'] = aff[0].get("uniqueKey")db.merchantOrder.save(order)row += 1i=-1worksheet.write(row, inc(), orderId)worksheet1.write(anotherrow, i, orderId)worksheet.write(row, inc(), row1[1])worksheet1.write(anotherrow, i, row1[1])worksheet.write(row, inc(), row1[-1])worksheet1.write(anotherrow, i, row1[-1])worksheet.write(row, inc(), row1[-2])worksheet1.write(anotherrow, i, row1[-2])worksheet.write(row, inc(), row1[-3])worksheet1.write(anotherrow, i, row1[-3])worksheet.write(row, inc(), order['merchantOrderId'])worksheet1.write(anotherrow, i, order['merchantOrderId'])worksheet.write(row, inc(), order['subTagId'])worksheet1.write(anotherrow, i, order['subTagId'])worksheet.write(row, inc(), order['placedOn'])worksheet1.write(anotherrow, i, order['placedOn'])worksheet.write(row, inc(), formattedTimestamp)worksheet1.write(anotherrow, i, formattedTimestamp)worksheet.write(row, inc(), int(order['paidAmount']))worksheet1.write(anotherrow, i, int(order['paidAmount']))worksheet.write(row, inc(), datetime.strftime(utils.fromTimeStamp(aff[0]['transactionTime']), "%d %b %Y %I:%M %p"))worksheet1.write(anotherrow, i, datetime.strftime(utils.fromTimeStamp(aff[0]['transactionTime']), "%d %b %Y %I:%M %p"))worksheet.write(row, inc(), aff[0]['transactionValue'])worksheet1.write(anotherrow, i, aff[0]['transactionValue'])worksheet.write(row, inc(), aff[0]['payOut'])worksheet1.write(anotherrow, i, aff[0]['payOut'])worksheet.write(row, inc(), "NA")worksheet1.write(anotherrow, i, "NA")k = irow -= 1anotherrow -= 1for subOrder in order['subOrders']:i = krow += 1anotherrow += 1worksheet.write(row, inc(), subOrder['productTitle'])worksheet1.write(anotherrow, i, subOrder['productTitle'])worksheet.write(row, inc(), subOrder['amountPaid'])worksheet1.write(anotherrow, i, subOrder['amountPaid'])worksheet.write(row, inc(), subOrder['quantity'])worksheet1.write(anotherrow, i, subOrder['quantity'])worksheet.write(row, inc(), subOrder['status'])worksheet1.write(anotherrow, i, subOrder['status'])worksheet.write(row, inc(), subOrder['cashBackStatus'])worksheet1.write(anotherrow, i, subOrder['cashBackStatus'])worksheet.write(row, inc(), subOrder['cashBackAmount'])worksheet1.write(anotherrow, i, subOrder['cashBackAmount'])else:i=-1worksheet1.write(anotherrow, inc(), orderId)worksheet1.write(anotherrow, inc(), row1[1])worksheet1.write(anotherrow, inc(), row1[-1])worksheet1.write(anotherrow, inc(), row1[-2])worksheet1.write(anotherrow, inc(), row1[-3])worksheet1.write(anotherrow, inc(), order['merchantOrderId'])worksheet1.write(anotherrow, inc(), order['subTagId'])worksheet1.write(anotherrow, inc(), order['placedOn'])worksheet1.write(anotherrow, inc(), formattedTimestamp)worksheet1.write(anotherrow, inc(), int(order['paidAmount']))notReconciled[orderId] = (anotherrow, i, int(order['paidAmount']), timestamp1, order["subTagId"])inc()inc()inc()inc()k = ianotherrow -= 1for subOrder in order['subOrders']:anotherrow += 1i = kworksheet1.write(anotherrow, inc(), subOrder['productTitle'])worksheet1.write(anotherrow, inc(), subOrder['amountPaid'])worksheet1.write(anotherrow, inc(), subOrder['quantity'])worksheet1.write(anotherrow, inc(), subOrder['status'])worksheet1.write(anotherrow, inc(), subOrder['cashBackStatus'])worksheet1.write(anotherrow, inc(), subOrder['cashBackAmount'])row5 += 1i=-1worksheet2.write(row5, inc(), order["orderId"])worksheet2.write(row5, inc(), row1[1])worksheet2.write(row5, inc(), row1[-1])worksheet2.write(row5, inc(), row1[-2])worksheet2.write(row5, inc(), row1[-3])worksheet2.write(row5, inc(), order['merchantOrderId'])worksheet2.write(row5, inc(), order['subTagId'])worksheet2.write(row5, inc(), order['placedOn'])worksheet2.write(row5, inc(), formattedTimestamp)worksheet2.write(row5, inc(), int(order['paidAmount']))row5 -=1k=ifor subOrder in order['subOrders']:i = krow5 += 1worksheet2.write(row5, inc(), subOrder['productTitle'])worksheet2.write(row5, inc(), subOrder['amountPaid'])worksheet2.write(row5, inc(), subOrder['quantity'])worksheet2.write(row5, inc(), subOrder['status'])worksheet2.write(row5, inc(), subOrder['cashBackStatus'])worksheet2.write(row5, inc(), subOrder['cashBackAmount'])last30Days = date.today() - timedelta(days=30)int(time.mktime(last30Days.timetuple()))row = 0for offer in db.homeshopOrderAffiliateInfo.find({"reconciled":{"$exists":False},"transactionTime":{"$gt":int(time.mktime(last30Days.timetuple())),"$lt":int(time.mktime(date.today().timetuple()))}} ):subTagId = offer.get("subTagId")saleTime = offer.get("transactionTime")orders = getOrdersByTag(subTagId, 7)for order in orders:if notReconciled.has_key(order[0]):(roww, column, paidAmount, timestamp1, oSubTagId) = notReconciled[order[0]]if paidAmount==int(offer.get("transactionValue")) and timestamp1 <= saleTime and timestamp1 + 120 >= saleTime:i = columnworksheet1.write(roww, inc(), datetime.strftime(utils.fromTimeStamp(offer.get("transactionTime")), "%d %b %Y %I:%M %p"))worksheet1.write(roww, inc(), offer.get("transactionValue"))worksheet1.write(roww, inc(), offer.get("payOut"))del notReconciled[order[0]]print "found match for user", order[1]db.homeshopOrderAffiliateInfo.update({"transactionId":offer.get("transactionId")}, {"$set":{"reconciled":True, "orderId":order[0]}}, multi=True)db.merchantOrder.update({"orderId":order[0]}, {"$set":{"reconciled":True, "adId":offer.get("uniqueKey")}}, multi=True)breakunreconciledOrders = notReconciled.keys()for offer in db.homeshopOrderAffiliateInfo.find({"reconciled":{"$exists":False},"transactionTime":{"$gt":int(time.mktime(last30Days.timetuple())),"$lt":int(time.mktime(date.today().timetuple()))}} ):row += 1i=-1affNotReconciledSheet.write(row, inc(), offer.get("uniqueKey"))affNotReconciledSheet.write(row, inc(), offer.get("subTagId"))affNotReconciledSheet.write(row, inc(), datetime.strftime(utils.fromTimeStamp(offer.get("transactionTime")), "%d %b %Y %I:%M %p"))affNotReconciledSheet.write(row, inc(), offer.get("transactionValue"))affNotReconciledSheet.write(row, inc(), offer.get("payOut"))affNotReconciledSheet.write(row, inc(), offer.get("status"))affNotReconciledSheet.write(row, inc(), 'NA')workbook.save(XLS_HS_FILENAME)def generateShopcluesReco():global icurs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 5)db = client.DtrnotReconciled = {}matchedList = []workbook = xlwt.Workbook()worksheet = workbook.add_sheet("Shopclues Reconciled")worksheet1 = workbook.add_sheet("Shopclues Reconciled All")worksheet2 = workbook.add_sheet("Shopclues Orders not reconciled")affNotReconciledSheet = workbook.add_sheet("Shopclues Aff not reconciled")setHeaders(worksheet, worksheet1, worksheet2, affNotReconciledSheet)row = 0anotherrow = 0row2 = 0row5=0for row1 in curs:orderId = row1[0]order = db.merchantOrder.find_one({"orderId":orderId})if order is None:continue#saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").timetuple())) 26 May 2015 02:12 PMaff = list(db.shopcluesOrderAffiliateInfo.find({"merchantOrderId":order["merchantOrderId"]}))anotherrow += 1try:formattedTimestamp = order['placedOn']timestamp1 = int(time.mktime(datetime.strptime(order['placedOn'], "%d %b %Y %I:%M %p").timetuple()))except:timestamp1 = int(time.mktime(row1[-4].timetuple()))formattedTimestamp = datetime.strftime(utils.fromTimeStamp(timestamp1), "%d %b %Y %I:%M %p")if len(aff) > 0 and order["subTagId"] not in matchedList and long(order["paidAmount"])==long(float(aff[0]["transactionValue"])):matchedList.append(order["subTagId"])db.shopcluesOrderAffiliateInfo.update({"merchantOrderId":aff[0].get("merchantOrderId")}, {"$set":{"reconciled":True, "orderId":orderId}}, multi=True)order['reconciled'] = Trueorder['adId'] = aff[0].get("uniqueKey")db.merchantOrder.save(order)row += 1i=-1worksheet.write(row, inc(), orderId)worksheet1.write(anotherrow, i, orderId)worksheet.write(row, inc(), row1[1])worksheet1.write(anotherrow, i, row1[1])worksheet.write(row, inc(), row1[-1])worksheet1.write(anotherrow, i, row1[-1])worksheet.write(row, inc(), row1[-2])worksheet1.write(anotherrow, i, row1[-2])worksheet.write(row, inc(), row1[-3])worksheet1.write(anotherrow, i, row1[-3])worksheet.write(row, inc(), order['merchantOrderId'])worksheet1.write(anotherrow, i, order['merchantOrderId'])worksheet.write(row, inc(), order['subTagId'])worksheet1.write(anotherrow, i, order['subTagId'])worksheet.write(row, inc(), order['placedOn'])worksheet1.write(anotherrow, i, order['placedOn'])worksheet.write(row, inc(), formattedTimestamp)worksheet1.write(anotherrow, i, formattedTimestamp)worksheet.write(row, inc(), int(order['paidAmount']))worksheet1.write(anotherrow, i, int(order['paidAmount']))worksheet.write(row, inc(), datetime.strftime(utils.fromTimeStamp(aff[0]['transactionTime']), "%d %b %Y %I:%M %p"))worksheet1.write(anotherrow, i, datetime.strftime(utils.fromTimeStamp(aff[0]['transactionTime']), "%d %b %Y %I:%M %p"))worksheet.write(row, inc(), aff[0]['transactionValue'])worksheet1.write(anotherrow, i, aff[0]['transactionValue'])worksheet.write(row, inc(), aff[0]['payOut'])worksheet1.write(anotherrow, i, aff[0]['payOut'])worksheet.write(row, inc(), "NA")worksheet1.write(anotherrow, i, "NA")k = irow -= 1anotherrow -= 1for subOrder in order['subOrders']:i = krow += 1anotherrow += 1worksheet.write(row, inc(), subOrder['productTitle'])worksheet1.write(anotherrow, i, subOrder['productTitle'])worksheet.write(row, inc(), subOrder['amountPaid'])worksheet1.write(anotherrow, i, subOrder['amountPaid'])worksheet.write(row, inc(), subOrder['quantity'])worksheet1.write(anotherrow, i, subOrder['quantity'])worksheet.write(row, inc(), subOrder['status'])worksheet1.write(anotherrow, i, subOrder['status'])worksheet.write(row, inc(), subOrder['cashBackStatus'])worksheet1.write(anotherrow, i, subOrder['cashBackStatus'])worksheet.write(row, inc(), subOrder['cashBackAmount'])worksheet1.write(anotherrow, i, subOrder['cashBackAmount'])else:i=-1worksheet1.write(anotherrow, inc(), orderId)worksheet1.write(anotherrow, inc(), row1[1])worksheet1.write(anotherrow, inc(), row1[-1])worksheet1.write(anotherrow, inc(), row1[-2])worksheet1.write(anotherrow, inc(), row1[-3])worksheet1.write(anotherrow, inc(), order['merchantOrderId'])worksheet1.write(anotherrow, inc(), order['subTagId'])worksheet1.write(anotherrow, inc(), order['placedOn'])worksheet1.write(anotherrow, inc(), formattedTimestamp)worksheet1.write(anotherrow, inc(), int(order['paidAmount']))notReconciled[orderId] = (anotherrow, i, int(order['paidAmount']), timestamp1, order["subTagId"])inc()inc()inc()inc()k = ianotherrow -= 1for subOrder in order['subOrders']:anotherrow += 1i = kworksheet1.write(anotherrow, inc(), subOrder['productTitle'])worksheet1.write(anotherrow, inc(), subOrder['amountPaid'])worksheet1.write(anotherrow, inc(), subOrder['quantity'])worksheet1.write(anotherrow, inc(), subOrder['status'])worksheet1.write(anotherrow, inc(), subOrder['cashBackStatus'])worksheet1.write(anotherrow, inc(), subOrder['cashBackAmount'])row5 += 1i=-1worksheet2.write(row5, inc(), order["orderId"])worksheet2.write(row5, inc(), row1[1])worksheet2.write(row5, inc(), row1[-1])worksheet2.write(row5, inc(), row1[-2])worksheet2.write(row5, inc(), row1[-3])worksheet2.write(row5, inc(), order['merchantOrderId'])worksheet2.write(row5, inc(), order['subTagId'])worksheet2.write(row5, inc(), order['placedOn'])worksheet2.write(row5, inc(), formattedTimestamp)worksheet2.write(row5, inc(), int(order['paidAmount']))row5 -=1k=ifor subOrder in order['subOrders']:i = krow5 += 1worksheet2.write(row5, inc(), subOrder['productTitle'])worksheet2.write(row5, inc(), subOrder['amountPaid'])worksheet2.write(row5, inc(), subOrder['quantity'])worksheet2.write(row5, inc(), subOrder['status'])worksheet2.write(row5, inc(), subOrder['cashBackStatus'])worksheet2.write(row5, inc(), subOrder['cashBackAmount'])last30Days = date.today() - timedelta(days=30)int(time.mktime(last30Days.timetuple()))row = 0for offer in db.shopcluesOrderAffiliateInfo.find({"reconciled":{"$exists":False},"transactionTime":{"$gt":int(time.mktime(last30Days.timetuple())),"$lt":int(time.mktime(date.today().timetuple()))}} ):subTagId = offer.get("subTagId")saleTime = offer.get("transactionTime")orders = getOrdersByTag(subTagId, 5)for order in orders:if notReconciled.has_key(order[0]):(roww, column, paidAmount, timestamp1, oSubTagId) = notReconciled[order[0]]if paidAmount==int(offer.get("transactionValue")) and timestamp1 <= saleTime and timestamp1 + 120 >= saleTime:i = columnworksheet1.write(roww, inc(), datetime.strftime(utils.fromTimeStamp(offer.get("transactionTime")), "%d %b %Y %I:%M %p"))worksheet1.write(roww, inc(), offer.get("transactionValue"))worksheet1.write(roww, inc(), offer.get("payOut"))del notReconciled[order[0]]print "found match for user", order[1]db.shopcluesOrderAffiliateInfo.update({"transactionId":offer.get("transactionId")}, {"$set":{"reconciled":True, "orderId":order[0]}}, multi=True)db.merchantOrder.update({"orderId":order[0]}, {"$set":{"reconciled":True, "adId":offer.get("uniqueKey")}}, multi=True)breakunreconciledOrders = notReconciled.keys()for offer in db.shopcluesOrderAffiliateInfo.find({"reconciled":{"$exists":False},"transactionTime":{"$gt":int(time.mktime(last30Days.timetuple())),"$lt":int(time.mktime(date.today().timetuple()))}} ):row += 1i=-1affNotReconciledSheet.write(row, inc(), offer.get("uniqueKey"))affNotReconciledSheet.write(row, inc(), offer.get("subTagId"))affNotReconciledSheet.write(row, inc(), datetime.strftime(utils.fromTimeStamp(offer.get("transactionTime")), "%d %b %Y %I:%M %p"))affNotReconciledSheet.write(row, inc(), offer.get("transactionValue"))affNotReconciledSheet.write(row, inc(), offer.get("payOut"))affNotReconciledSheet.write(row, inc(), offer.get("status"))affNotReconciledSheet.write(row, inc(), 'NA')workbook.save(XLS_SC_FILENAME)def sendmail(email, message, title, *varargs):if email == "":returnmailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)mailServer.ehlo()mailServer.starttls()mailServer.ehlo()# Create the container (outer) email message.msg = MIMEMultipart()msg['Subject'] = titlemsg.preamble = titlehtml_msg = MIMEText(message, 'html')msg.attach(html_msg)#snapdeal more to be added herefor fileName in varargs:snapdeal = MIMEBase('application', 'vnd.ms-excel')snapdeal.set_payload(file(fileName).read())encoders.encode_base64(snapdeal)snapdeal.add_header('Content-Disposition', 'attachment;filename=' + fileName)msg.attach(snapdeal)email.append('amit.gupta@shop2020.in')MAILTO = emailmailServer.login(SENDER, PASSWORD)mailServer.sendmail(SENDER, MAILTO, msg.as_string())def setHeaders(worksheet, worksheet1, worksheet2, affNotReconciledSheet, worksheet3=None):boldStyle = xlwt.XFStyle()f = xlwt.Font()f.bold = TrueboldStyle.font = frow = 0global ii=-1worksheet.write(row, inc(), 'Order Id', boldStyle)worksheet.write(row, inc(), 'User Id', boldStyle)worksheet.write(row, inc(), 'Username', boldStyle)worksheet.write(row, inc(), 'Version Code', boldStyle)worksheet.write(row, inc(), 'Device', boldStyle)worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)worksheet.write(row, inc(), 'SubtagId', boldStyle)worksheet.write(row, inc(), 'Sale Date', boldStyle)worksheet.write(row, inc(), 'Dtr Sale Date', boldStyle)worksheet.write(row, inc(), 'Sale Amount', boldStyle)worksheet.write(row, inc(), 'Aff Sale Date', boldStyle)worksheet.write(row, inc(), 'Aff Sale Amt', boldStyle)worksheet.write(row, inc(), 'Aff PayOut', boldStyle)worksheet.write(row, inc(), 'Aff Status', boldStyle)worksheet.write(row, inc(), 'Product Title', boldStyle)worksheet.write(row, inc(), 'Price', boldStyle)worksheet.write(row, inc(), 'Quantity', boldStyle)worksheet.write(row, inc(), 'Status', boldStyle)worksheet.write(row, inc(), 'Cashback Status', boldStyle)worksheet.write(row, inc(), 'CashBack', boldStyle)i = -1worksheet2.write(row, inc(), 'Order Id', boldStyle)worksheet2.write(row, inc(), 'User Id', boldStyle)worksheet2.write(row, inc(), 'Username', boldStyle)worksheet2.write(row, inc(), 'Version Code', boldStyle)worksheet2.write(row, inc(), 'Device', boldStyle)worksheet2.write(row, inc(), 'Merchant Order Id', boldStyle)worksheet2.write(row, inc(), 'SubtagId', boldStyle)worksheet2.write(row, inc(), 'Sale Date', boldStyle)worksheet2.write(row, inc(), 'Dtr Sale Date', boldStyle)worksheet2.write(row, inc(), 'Sale Amount', boldStyle)worksheet2.write(row, inc(), 'Product Title', boldStyle)worksheet2.write(row, inc(), 'Price', boldStyle)worksheet2.write(row, inc(), 'Quantity', boldStyle)worksheet2.write(row, inc(), 'Status', boldStyle)worksheet2.write(row, inc(), 'Cashback Status', boldStyle)worksheet2.write(row, inc(), 'CashBack', boldStyle)i =-1affNotReconciledSheet.write(row, inc(), 'Sub Tag Id', boldStyle)affNotReconciledSheet.write(row, inc(), 'Order Id', boldStyle)affNotReconciledSheet.write(row, inc(), 'Sale Date', boldStyle)affNotReconciledSheet.write(row, inc(), 'Sale Amount', boldStyle)affNotReconciledSheet.write(row, inc(), 'Product', boldStyle)affNotReconciledSheet.write(row, inc(), 'Quantity', boldStyle)affNotReconciledSheet.write(row, inc(), 'Pay Out', boldStyle)if worksheet3:i =-1worksheet3.write(row, inc(), 'Date', boldStyle)worksheet3.write(row, inc(), 'Sale Amount', boldStyle)worksheet3.write(row, inc(), 'Approved Sales', boldStyle)worksheet3.write(row, inc(), 'Rejected Sales', boldStyle)def getUserAmountReconciled():passdef getUserOrders():global i#worksheet1 = wb.add_sheet("Suspected Users")workbook = xlwt.Workbook("ISO8859-1")sh1 = workbook.add_sheet("All Orders")sh2 = workbook.add_sheet("Flipkart Orders")#worksheet = workbook.add_sheet("All Users")db=client.DtrprevDate = cutOffresults = Mysql.fetchResult("select * from allorder where created_on > %s", prevDate)row = 0i=-1# worksheet.write(row, inc(), 'User Id', boldStyle)# worksheet.write(row, inc(), 'User name', boldStyle)# worksheet.write(row, inc(), 'Order Id', boldStyle)# worksheet.write(row, inc(), 'Created On', boldStyle)# worksheet.write(row, inc(), 'Store Id', boldStyle)# worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)# worksheet.write(row, inc(), 'SubOrder Id', boldStyle)# worksheet.write(row, inc(), 'Product title', boldStyle)# worksheet.write(row, inc(), 'Amount Paid', boldStyle)# worksheet.write(row, inc(), 'Cashback', boldStyle)# worksheet.write(row, inc(), 'Order Status', boldStyle)# worksheet.write(row, inc(), 'Detailed Status', boldStyle)# worksheet.write(row, inc(), 'Cashback Status', boldStyle)# worksheet.write(row, inc(), 'Reconciled', boldStyle)# worksheet.write(row, inc(), 'IP', boldStyle)# worksheet.write(row, inc(), 'Aff Status', boldStyle)i=-1sh1.write(row, inc(), 'User Id', boldStyle)sh1.write(row, inc(), 'User name', boldStyle)sh1.write(row, inc(), 'Order Id', boldStyle)sh1.write(row, inc(), 'Created On', boldStyle)sh1.write(row, inc(), 'Store Id', boldStyle)sh1.write(row, inc(), 'Merchant Order Id', boldStyle)sh1.write(row, inc(), 'Status', boldStyle)sh1.write(row, inc(), 'Detailed Status', boldStyle)sh1.write(row, inc(), 'Product title', boldStyle)sh1.write(row, inc(), 'Referrer', boldStyle)sh1.write(row, inc(), 'Amount Paid', boldStyle)sh1.write(row, inc(), 'Catalog Id', boldStyle)sh1.write(row, inc(), 'Brand', boldStyle)sh1.write(row, inc(), 'Model', boldStyle)sh1.write(row, inc(), 'Category', boldStyle)sh1.write(row, inc(), 'Deal Rank', boldStyle)sh1.write(row, inc(), 'Max Nlc', boldStyle)sh1.write(row, inc(), 'Min Nlc', boldStyle)sh1.write(row, inc(), 'DP', boldStyle)sh1.write(row, inc(), 'Item status', boldStyle)sh1.write(row, inc(), 'City', boldStyle)sh1.write(row, inc(), 'State', boldStyle)sh1.write(row, inc(), 'Pincode', boldStyle)sh1.write(row, inc(), 'SubOrder Id', boldStyle)sh1.write(row, inc(), 'Cashback Status', boldStyle)sh1.write(row, inc(), 'Cashback Amount', boldStyle)sh1.write(row, inc(), 'Quantity', boldStyle)sh1.write(row, inc(), 'Reconciled', boldStyle)sh1.write(row, inc(), 'Aff Status', boldStyle)i =-1sh2.write(row, inc(), 'User Id', boldStyle)sh2.write(row, inc(), 'User name', boldStyle)sh2.write(row, inc(), 'Created On', boldStyle)sh2.write(row, inc(), 'Store', boldStyle)sh2.write(row, inc(), 'Status', boldStyle)sh2.write(row, inc(), 'Product title', boldStyle)sh2.write(row, inc(), 'Price', boldStyle)sh2.write(row, inc(), 'Quantity', boldStyle)sh2.write(row, inc(), 'Catalog Id', boldStyle)sh2.write(row, inc(), 'Brand', boldStyle)sh2.write(row, inc(), 'Model', boldStyle)sh2.write(row, inc(), 'Category', boldStyle)default_format = xlwt.XFStyle()date_format = xlwt.XFStyle()date_format.num_format_str = 'dd/mm/yyyy'datetime_format = xlwt.XFStyle()datetime_format.num_format_str = 'dd/mm/yyyy HH:MM AM/PM'number_format = xlwt.XFStyle()number_format.num_format_str = '#,##0'row=0for r in results:row += 1column = 0for data in r :sh1.write(row, column, int(data) if type(data) is float else data, datetime_format if type(data) is datetime else default_format)column += 1i=-1# worksheet.write(row, inc(), r[0])# worksheet.write(row, inc(), r[1])# worksheet.write(row, inc(), r[2])# worksheet.write(row, inc(), r[3], datetime_format)# worksheet.write(row, inc(), r[4])# worksheet.write(row, inc(), r[5])# worksheet.write(row, inc(), r[23])# worksheet.write(row, inc(), r[8])# worksheet.write(row, inc(), r[10])# worksheet.write(row, inc(), r[25])# worksheet.write(row, inc(), r[6])# worksheet.write(row, inc(), r[7])# worksheet.write(row, inc(), r[24])flipkartOrders = session.query(FlipkartOrders).order_by(FlipkartOrders.created.desc()).all()flipkartOrders.reverse()row3=0#try:for order in flipkartOrders:row3 += 1i=-1sh2.write(row3, inc(), order.user_id)sh2.write(row3, inc(), order.email)sh2.write(row3, inc(), order.created, datetime_format)sh2.write(row3, inc(), "Flipkart")sh2.write(row3, inc(), order.status)sh2.write(row3, inc(), order.title)sh2.write(row3, inc(), order.price)sh2.write(row3, inc(), order.quantity)sh2.write(row3, inc(), order.catalogId)sh2.write(row3, inc(), order.brand)sh2.write(row3, inc(), order.model)sh2.write(row3, inc(), order.category)workbook.save(XLS_O_FILENAME)def getSkuData(storeId, identifier):if storeId in (1,2,4,5,6):skuData = client.Catalog.MasterData.find_one({'identifier':identifier, 'source_id':storeId})elif storeId == 3:skuData = client.Catalog.MasterData.find_one({'secondaryIdentifier':identifier, 'source_id':storeId})return skuDatadef inc():global ii+=1return idef migrateFlipkartOrders():db = client.Dtrinfo = reversed(list(db.flipkartOrderAffiliateInfo.find()))#try:for order in info:userId = NonesubTagId = Noneemail = NonesubTagId = order.get("subTagId")if subTagId:click = session.query(Clicks).filter_by(tag = subTagId).first()if click is not None:userId= click.user_iduser = session.query(Users.email).filter_by(id = userId).first()if user is not None:email = user.emailflipkartOrder = FlipkartOrders()flipkartOrder.user_id = userIdflipkartOrder.identifier = order.get("identifier")flipkartOrder.email = emailflipkartOrder.subtagId = order.get("subTagId")flipkartOrder.created = datetime.strptime(order.get("saleDate"), "%Y-%m-%d")flipkartOrder.status = order.get("conversionStatus")flipkartOrder.title = order.get("productTitle")flipkartOrder.price = order.get("price")flipkartOrder.quantity = order.get("quantity")flipkartOrder.productCode = order.get("productCode")skuData = getSkuData(2, order.get("productCode"))if skuData is not None:flipkartOrder.catalogId = skuData.get("skuBundleId")flipkartOrder.brand = skuData.get("brand")flipkartOrder.model = skuData.get("model_name")flipkartOrder.category =categoryMap.get(skuData.get("category_id"))flipkartOrder.title =skuData.get("source_product_name")session.commit()def main1():db = client.Dtrfor offer in db.snapdealOrderAffiliateInfo.find({}):print offer.get("adId"), offer.get("saleAmount"), type(offer.get("saleAmount"))#obj = offer.get(saleTime)#obj['paidAmount'] += order["paidAmount"]def addHeaders(sheet):global ii = 0sheet.write(0, inc(), )def main():message = generateSnapDealReco()generateFlipkartReco()generateShopcluesReco()generateAmazonReco(XLS_A_FILENAME)message1 = getdata.summaryByBrandAndStore()getUserOrders()generateHomeShopReco()#sendmail(["amit.gupta@shop2020.in"], message, "Affiliate Reco", XLS_FILENAME)#sendmail(["naman.kumar@shop2020.in"], message1, "All DTR Orders", XLS_O_FILENAME)sendmail(["amit.gupta@shop2020.in", "rajneesh.arora@saholic.com", "chaitnaya.vats@saholic.com", "manoj.kumar@saholic.com", "manish.sharma@shop2020.in"], message1, "All DTR Orders", XLS_O_FILENAME)sendmail(["amit.gupta@shop2020.in","rajneesh.arora@saholic.com","manish.sharma@shop2020.in"], message, "Affiliate Reco", XLS_FILENAME, XLS_F_FILENAME, XLS_A_FILENAME, XLS_SC_FILENAME, XLS_HS_FILENAME)#sendmail(["amit.gupta@shop2020.in"], "", "Affiliate Reco", XLS_FILENAME, XLS_F_FILENAME, XLS_A_FILENAME, XLS_SC_FILENAME)def main3():db = client.Dtr#main()mailBodyTemplate="""<html><body><table cellspacing="0" border="1" style="text-align:right"><thead><tr><th style="text-align:center">Date</th><th style="text-align:center">Sale</th><th style="text-align:center">Approved</th><th style="text-align:center">Approved %</th><th style="text-align:center">Rejected</th><th style="text-align:center">Rejected %</th><th style="text-align:center">Missing %</th><th style="text-align:center">Approved % against Sale</th></tr></thead><tbody>{0}</tbody></table></body></html>"""tbody=[]rowtemplate="<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td></tr>"for offer in db.flSaleSnapshot.find().limit(61).sort("_id",-1):approved = offer.get("approvedAmount")rejected = offer.get("rejectedAmount")sale = offer.get("paidAmount")total = approved + rejectedif total ==0:continuetbody.append(rowtemplate.format(offer.get("_id"), sale, approved, int((approved*100/total)), rejected, int((rejected*100/total)), int((sale-approved-rejected)*100/sale),int((approved)*100/sale)))message = mailBodyTemplate.format("".join(tbody))print messagedef main2():db = client.Dtrcurs = getOrdersAfterDate(datetime.now() - timedelta(days=46), 3)datemap={}for row1 in curs:orderId = row1[0]order = db.merchantOrder.find_one({"orderId":orderId})if order is None:continue#saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").timetuple()))try:saleTime = datetime.strptime(order['placedOn'], "%a, %d %b, %Y").strftime("%Y-%m-%d")except:saleTime = datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").strftime("%Y-%m-%d")print "%s\t%s\t%s\t%s"%(orderId, order['placedOn'], saleTime, order['paidAmount'])if not datemap.has_key(saleTime):datemap[saleTime] = {"totalAmount":0, "paidAmount":0, "rejectedAmount":0, "approvedAmount":0, "count":0, "subAmount":0}obj = datemap.get(saleTime)obj['paidAmount'] += order["paidAmount"]for so in order['subOrders']:obj['subAmount'] += int(so['amountPaid'])obj['count'] += 1for offer in db.snapdealOrderAffiliateInfo1.find({"saleDate":{"$gte":toTimeStamp(datetime.now() - timedelta(days=46))}}):saleTime = utils.fromTimeStamp(offer.get('saleDate')).strftime("%Y-%m-%d")obj = datemap.get(saleTime)if obj is not None:try:if offer.get("payOut"):obj['approvedAmount'] += offer["saleAmount"]else:obj['rejectedAmount'] += offer["saleAmount"]except:print "Exception for orderId" , offer.get("orderId")else:print "Could not find sale date", saleTimecontinuefor key,valmap in datemap.iteritems():valmap['_id'] = keydb.flSaleSnapshot.save(valmap)if __name__ == '__main__':getdata.addToAllOrders(date.today()-timedelta(days=20))main2()main()