Subversion Repositories SmartDukaan

Rev

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, timedelta
from dtr.main import Store, sourceMap
from dtr.reports.amazonreco import generateAmazonReco
from dtr.storage import Mysql, DataService
from dtr.storage.DataService import Users, Clicks, FlipkartOrders,\
    All_user_addresses
from dtr.storage.Mysql import getOrdersAfterDate, getOrdersByTag
from dtr.utils import utils
from elixir import *
from sqlalchemy.sql.expression import func, func, or_
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from pymongo.mongo_client import MongoClient
from xlrd import open_workbook
from xlutils.copy import copy
from xlwt.Workbook import Workbook
import _mysql
import MySQLdb
import smtplib
import time
import xlwt
from dtr.utils import utils
from dtr.utils.utils import toTimeStamp
from dtr.reports import getdata
#from xlwt import 

DataService.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 = 587    

XLS_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 = True
boldStyle.font = f
i = -1

datetime_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)%12
cutOffYear = curDate.year - (0 if curDate.month-1 > 0 else 1)
cutOff = datetime(cutOffYear, cutOffMonth, 1)


def generateFlipkartReco():
    global i
    i = -1
    curs = getOrdersAfterDate(cutOff, 2)
    db = client.Dtr
    wb = xlwt.Workbook()
    #rb = open_workbook(XLS_F_FILENAME)
    #wb = copy(rb)
    #wb = xlwt.Workbook()
    row = 0
    row2 = 0
    row3 = 0
    row4 = 0
    worksheet = 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:
            continue
        saleTime = 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 += 1
            i=-1
            worksheet.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 += 1
                    i = -1 
                    worksheet1.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'])
                    break
            row3 += 1
            i=-1
            worksheet2.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=-1
    worksheet3.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 += 1
        i =-1
        worksheet3.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 i
    curs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 3)
    db = client.Dtr
    matchedList = []

    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 = 0
    row5=0
    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")    
            
        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"] = False
                    db.snapdealOrderAffiliateInfo1.save(aff)
            matchedList.append(order["merchantOrderId"])
            order['reconciled'] = True
            row += 1
            i=-1 
            worksheet.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 = i
            row -= 1
            for subOrder in order['subOrders']:
                i = k
                matched=False
                row += 1
                for 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=True
                        break
                if 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 += 1
            i=-1
            worksheet2.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 -=1
            k=i
            for subOrder in order['subOrders']:
                i = k
                row5 += 1
                worksheet2.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 = 0
    for offer in db.snapdealOrderAffiliateInfo1.find({"missingOrder":True, 
                                                     "saleDate":{"$gte":int(time.mktime(last30Days.timetuple())),
                                                                 "$lt":int(time.mktime(date.today().timetuple()))}} ):
        row += 1
        i=-1
        affNotReconciledSheet.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 + rejected
        if total ==0:
            continue
        tbody.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 message
    
def generateHomeShopReco():
    global i
    curs = getOrdersAfterDate(datetime.now() - timedelta(days=90), 7)
    db = client.Dtr
    notReconciled = {}
    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 = 0
    anotherrow = 0
    row2 = 0
    row5=0
    
    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()))  26 May 2015 02:12 PM
        aff = list(db.shopcluesOrderAffiliateInfo.find({"merchantOrderId":order["merchantOrderId"]}))
        anotherrow += 1
        try:
            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'] = True
            order['adId'] = aff[0].get("uniqueKey")
            db.merchantOrder.save(order)
            row += 1
            i=-1 
            
            worksheet.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 = i
            row -= 1
            anotherrow -= 1
            for subOrder in order['subOrders']:
                i = k
                row += 1
                anotherrow += 1
                worksheet.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=-1
            worksheet1.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 = i
            anotherrow -= 1
            for subOrder in order['subOrders']:
                anotherrow += 1
                i = k
                worksheet1.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 += 1
            i=-1
            worksheet2.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 -=1
            k=i
            for subOrder in order['subOrders']:
                i = k
                row5 += 1
                worksheet2.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 = 0
    for 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 = column
                    worksheet1.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)
                    break
                
    unreconciledOrders = 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 += 1
        i=-1
        affNotReconciledSheet.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 i
    curs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 5)
    db = client.Dtr
    notReconciled = {}
    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 = 0
    anotherrow = 0
    row2 = 0
    row5=0
    
    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()))  26 May 2015 02:12 PM
        aff = list(db.shopcluesOrderAffiliateInfo.find({"merchantOrderId":order["merchantOrderId"]}))
        anotherrow += 1
        try:
            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'] = True
            order['adId'] = aff[0].get("uniqueKey")
            db.merchantOrder.save(order)
            row += 1
            i=-1 
            
            worksheet.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 = i
            row -= 1
            anotherrow -= 1
            for subOrder in order['subOrders']:
                i = k
                row += 1
                anotherrow += 1
                worksheet.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=-1
            worksheet1.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 = i
            anotherrow -= 1
            for subOrder in order['subOrders']:
                anotherrow += 1
                i = k
                worksheet1.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 += 1
            i=-1
            worksheet2.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 -=1
            k=i
            for subOrder in order['subOrders']:
                i = k
                row5 += 1
                worksheet2.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 = 0
    for 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 = column
                    worksheet1.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)
                    break
                
    unreconciledOrders = 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 += 1
        i=-1
        affNotReconciledSheet.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 == "":
        return
    mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    
    # Create the container (outer) email message.
    msg = MIMEMultipart()
    msg['Subject'] = title
    msg.preamble = title
    html_msg = MIMEText(message, 'html')
    msg.attach(html_msg)
    
    #snapdeal more to be added here
    for 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 = email 
    mailServer.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 = True
    boldStyle.font = f
    row = 0
    global i
    i=-1    
    worksheet.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 = -1
    worksheet2.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 =-1
    affNotReconciledSheet.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 =-1
        worksheet3.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():
    pass

def 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.Dtr
    prevDate = cutOff
    results = Mysql.fetchResult("select * from allorder where created_on > %s", prevDate)
    row = 0
    i=-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=-1
    sh1.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 =-1
    sh2.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=0
    for r in results:
        row += 1
        column = 0
        for 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 += 1
        i=-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 += 1
        i=-1
        sh2.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 skuData
        
def inc():
    global i
    i+=1
    return i

def migrateFlipkartOrders():
    db = client.Dtr
    info = reversed(list(db.flipkartOrderAffiliateInfo.find()))
    #try:
    for order in info:
        userId = None
        subTagId = None
        email = None
        subTagId = order.get("subTagId")
        if subTagId:
            click = session.query(Clicks).filter_by(tag = subTagId).first()
            if click is not None:
                userId= click.user_id 
                user = session.query(Users.email).filter_by(id = userId).first()
                if user is not None:
                    email = user.email
        
        flipkartOrder = FlipkartOrders()
        flipkartOrder.user_id = userId
        flipkartOrder.identifier = order.get("identifier")
        flipkartOrder.email = email
        flipkartOrder.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.Dtr
    for 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 i
    i = 0
    sheet.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 + rejected
        if total ==0:
            continue
        tbody.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 message
def main2():
    db = client.Dtr
    curs = 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'] += 1
        
    for 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", saleTime
            continue
    
    for key,valmap in  datemap.iteritems():
        valmap['_id'] = key
        db.flSaleSnapshot.save(valmap)

if __name__ == '__main__':
    getdata.addToAllOrders(date.today()-timedelta(days=20))
    main2()
    main()