Subversion Repositories SmartDukaan

Rev

Rev 10430 | Rev 10432 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10401 amar.kumar 1
from elixir import *
2
from shop2020.config.client.ConfigClient import ConfigClient
3
from shop2020.model.v1.catalog.impl import DataService
10414 kshitij.so 4
from shop2020.model.v1.catalog.impl.DataService import SnapdealItem, MarketplaceItems, Item, Category
10401 amar.kumar 5
from shop2020.thriftpy.model.v1.order.ttypes import OrderSource
6
import mechanize
7
import sys
8
import cookielib
9
from time import sleep
10
import json
11
import smtplib
10414 kshitij.so 12
import xlwt
10401 amar.kumar 13
from datetime import datetime
14
from shop2020.utils import EmailAttachmentSender
15
from shop2020.utils.EmailAttachmentSender import get_attachment_part
16
from email.mime.text import MIMEText
17
import email
18
from email.mime.multipart import MIMEMultipart
19
import email.encoders
20
 
21
config_client = ConfigClient()
22
host = config_client.get_property('staging_hostname')
23
DataService.initialize(db_hostname=host)
24
 
25
class __SnapdealInfo:
10414 kshitij.so 26
    def __init__(self, sellingPrice, weight, transferPrice, commission, commissionPercentage, courierCost, sellingPriceSnapdeal, transferPriceSnapdeal, fixedMargin, \
27
                 fixedMarginPercentage, collectionCharges, logisticCostSnapdeal, weightSnapdeal, supc, itemId, parentCategory, productGroup ,brand, modelName, modelNumber, \
10428 kshitij.so 28
                 color,reason):
10401 amar.kumar 29
 
30
        self.sellingPrice = sellingPrice
31
        self.weight = weight
32
        self.transferPrice = transferPrice
33
        self.commission = commission
34
        self.commissionPercentage = commissionPercentage
35
        self.courierCost = courierCost
36
        self.sellingPriceSnapdeal = sellingPriceSnapdeal
37
        self.transferPriceSnapdeal = transferPriceSnapdeal
38
        self.fixedMargin = fixedMargin
39
        self.fixedMarginPercentage = fixedMarginPercentage
40
        self.collectionCharges = collectionCharges
41
        self.logisticCostSnapdeal = logisticCostSnapdeal
42
        self.weightSnapdeal = weightSnapdeal
43
        self.supc = supc
44
        self.itemId = itemId
10414 kshitij.so 45
        self.parentCategory = parentCategory
46
        self.productGroup = productGroup
47
        self.brand = brand
48
        self.modelName = modelName
49
        self.modelNumber = modelNumber
10428 kshitij.so 50
        self.color = color
51
        self.reason = reason 
10414 kshitij.so 52
 
10401 amar.kumar 53
 
54
def getBrowserObject():
55
    br = mechanize.Browser(factory=mechanize.RobustFactory())
56
    cj = cookielib.LWPCookieJar()
57
    br.set_cookiejar(cj)
58
    br.set_handle_equiv(True)
59
    br.set_handle_redirect(True)
60
    br.set_handle_referer(True)
61
    br.set_handle_robots(False)
62
    br.set_debug_http(False)
63
    br.set_debug_redirects(False)
64
    br.set_debug_responses(False)
65
 
66
    br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
67
 
68
    br.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'),
69
                     ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
70
                     ('Accept-Encoding', 'gzip,deflate,sdch'),                  
71
                     ('Accept-Language', 'en-US,en;q=0.8'),                     
72
                     ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')]
73
    return br
74
 
75
def login(url):
76
    br = getBrowserObject()
77
    br.open(url)
78
    response = br.open(url)
79
    br.select_form(nr=0)
80
    br.form['username'] = "saholic-snapdeal@saholic.com"
81
    br.form['password'] = "bc452ce4"
82
    print "Trying to login"
83
    response = br.submit()
84
    return br
85
 
86
def populateStuff(br):
87
    exceptionList = []
88
    fetchedItems = []
10414 kshitij.so 89
    items = session.query(SnapdealItem,MarketplaceItems,Item).join((MarketplaceItems,SnapdealItem.item_id==MarketplaceItems.itemId)).join((Item,SnapdealItem.item_id==Item.id)).filter(MarketplaceItems.source==OrderSource.SNAPDEAL).all()
10401 amar.kumar 90
    for item in items:
91
        snapdealItem = item[0]
92
        marketplaceItem = item[1]
10414 kshitij.so 93
        ds_item = item[2] 
94
        category = Category.query.filter_by(id=ds_item.category).one()
95
        parent_category = Category.query.filter_by(id=category.parent_category_id).first()
10404 kshitij.so 96
        try:
97
            snapdealInfo = fetchData(br,snapdealItem.supc)
98
        except Exception as e:
99
            exceptionList.append(item)
100
            print "Unable to fetch details ",e
101
            continue
102
        snapdealInfo.sellingPrice = snapdealItem.sellingPrice
103
        snapdealInfo.courierCost = snapdealItem.courierCost
104
        snapdealInfo.transferPrice = snapdealItem.transferPrice
105
        snapdealInfo.commissionPercentage = marketplaceItem.commission
106
        snapdealInfo.commission = snapdealItem.commission
107
        snapdealInfo.itemId = snapdealItem.item_id
10414 kshitij.so 108
        snapdealInfo.parentCategory = parent_category.display_name
109
        snapdealInfo.productGroup = ds_item.product_group
110
        snapdealInfo.brand = ds_item.brand
111
        snapdealInfo.modelName = ds_item.model_name
112
        snapdealInfo.modelNumber = ds_item.model_number
113
        snapdealInfo.color = ds_item.color
114
        snapdealInfo.weight = ds_item.weight
10404 kshitij.so 115
        fetchedItems.append(snapdealInfo)
10401 amar.kumar 116
    return exceptionList, fetchedItems
117
 
118
def fetchData(br,supc):
119
    url="http://seller.snapdeal.com/pricing/search?searchType=SUPC&searchValue=%s&gridType=normal&_search=false&nd=1396007375971&rows=30&page=1&sidx=&sord=asc"%(supc)
120
    sleep(1)
121
    response = br.open(url)
122
    dataform = str(response.read()).strip("'<>() ").replace('\'', '\"')
123
    struct = json.loads(dataform)
124
    sdObj = struct['rows'][0]
125
    print sdObj
10414 kshitij.so 126
    snapdealInfo = __SnapdealInfo(None,None,None,None,None,None,sdObj['sellingPrice'],sdObj['netSellerPayable'],sdObj['fixedMarginAmount'],sdObj['fixedMarginPercent'],sdObj['collectionCharges'],sdObj['logisticCost'],sdObj['deadWeight'],supc,None, \
10428 kshitij.so 127
                    None, None, None, None, None, None, None)
10401 amar.kumar 128
    return snapdealInfo
129
 
130
def filterData(fetchedItems):
10414 kshitij.so 131
    filterList = []
10401 amar.kumar 132
    for data in fetchedItems:
133
        if ( data.transferPrice - data.transferPriceSnapdeal >= -3 ) and (data.transferPrice - data.transferPriceSnapdeal <= 3):
10414 kshitij.so 134
            continue
135
        filterList.append(data)
136
    return filterList
10401 amar.kumar 137
 
138
def sendMail(filteredData,exceptionList):
139
    xstr = lambda s: s or ""
140
    message="""<html>
141
            <body>
142
            <h3>Low TP On Snapdeal</h3>
143
            <table border="1" style="width:100%;">
144
            <thead>
145
            <tr><th>Item Id</th>
146
            <th>Product Name</th>
147
            <th>Our System Selling Price</th>
148
            <th>Selling Price Snapdeal</th>
149
            <th>Our System Transfer Price</th>
150
            <th>Snapdeal Transfer Price</th>
151
            <th>Our System Commission</th>
152
            <th>Snapdeal Commission</th>
153
            <th>Our System Commission %</th>
154
            <th>Snapdeal Commission %</th>
155
            <th>Our System Weight</th>
156
            <th>Snapdeal Weight</th>
157
            <th>Our Courier Cost</th>
158
            <th>Snapdeal Courier Charges</th>
10428 kshitij.so 159
            <th>Reason</th>
10401 amar.kumar 160
            </tr></thead>
161
            <tbody>"""
162
    for data in filteredData:
163
        if data.transferPriceSnapdeal < data.transferPrice:
164
            message+="""<tr>
165
            <td style="text-align:center">"""+str(data.itemId)+"""</td>
10414 kshitij.so 166
            <td style="text-align:center">"""+xstr(data.brand)+" "+xstr(data.modelName)+" "+xstr(data.modelNumber)+" "+xstr(data.color)+"""</td>
10401 amar.kumar 167
            <td style="text-align:center">"""+str(data.sellingPrice)+"""</td>
168
            <td style="text-align:center">"""+str(data.sellingPriceSnapdeal)+"""</td>
169
            <td style="text-align:center">"""+str(data.transferPrice)+"""</td>
170
            <td style="text-align:center">"""+str(data.transferPriceSnapdeal)+"""</td>
10414 kshitij.so 171
            <td style="text-align:center">"""+str(round(data.commission*1.1236,2))+"""</td>
10401 amar.kumar 172
            <td style="text-align:center">"""+str(round(float(data.fixedMargin)+float(data.collectionCharges),2))+"""</td>
173
            <td style="text-align:center">"""+str(data.commissionPercentage)+"%"+"""</td>
174
            <td style="text-align:center">"""+str(round(float(data.fixedMarginPercentage)/1.1236,2))+"%"+"""</td>
10414 kshitij.so 175
            <td style="text-align:center">"""+str(data.weight*1000)+" gms"+"""</td>
10401 amar.kumar 176
            <td style="text-align:center">"""+str(data.weightSnapdeal)+" gms"+"""</td>
10414 kshitij.so 177
            <td style="text-align:center">"""+str(round(data.courierCost*1.1236,2))+"""</td>
178
            <td style="text-align:center">"""+str(round(data.logisticCostSnapdeal,2))+"""</td>
10428 kshitij.so 179
            <td style="text-align:center">"""+getReason(data)+"""</td>
10401 amar.kumar 180
            </tr>"""
181
    message+="""</tbody></table>"""
182
    message+="""
183
            <h3>High TP On Snapdeal</h3>
184
            <table border="1" style="width:100%;">
185
            <thead>
186
            <tr><th>Item Id</th>
187
            <th>Product Name</th>
188
            <th>Our System Selling Price</th>
189
            <th>Selling Price Snapdeal</th>
190
            <th>Our System Transfer Price</th>
191
            <th>Snapdeal Transfer Price</th>
192
            <th>Our System Commission</th>
193
            <th>Snapdeal Commission</th>
194
            <th>Our System Commission %</th>
195
            <th>Snapdeal Commission %</th>
196
            <th>Our System Weight</th>
197
            <th>Snapdeal Weight</th>
198
            <th>Our Courier Cost</th>
199
            <th>Snapdeal Courier Charges</th>
10428 kshitij.so 200
            <th>Reason</th>
10401 amar.kumar 201
            </tr></thead>
202
            <tbody>"""
203
    for data in filteredData:
204
        if data.transferPriceSnapdeal >= data.transferPrice:
205
            message+="""<tr>
206
            <td style="text-align:center">"""+str(data.itemId)+"""</td>
10414 kshitij.so 207
            <td style="text-align:center">"""+xstr(data.brand)+" "+xstr(data.modelName)+" "+xstr(data.modelNumber)+" "+xstr(data.color)+"""</td>
10401 amar.kumar 208
            <td style="text-align:center">"""+str(data.sellingPrice)+"""</td>
209
            <td style="text-align:center">"""+str(data.sellingPriceSnapdeal)+"""</td>
210
            <td style="text-align:center">"""+str(data.transferPrice)+"""</td>
211
            <td style="text-align:center">"""+str(data.transferPriceSnapdeal)+"""</td>
10414 kshitij.so 212
            <td style="text-align:center">"""+str(round(data.commission*1.1236,2))+"""</td>
10401 amar.kumar 213
            <td style="text-align:center">"""+str(round(float(data.fixedMargin)+float(data.collectionCharges),2))+"""</td>
214
            <td style="text-align:center">"""+str(data.commissionPercentage)+"%"+"""</td>
215
            <td style="text-align:center">"""+str(round(float(data.fixedMarginPercentage)/1.1236,2))+"%"+"""</td>
10414 kshitij.so 216
            <td style="text-align:center">"""+str(data.weight*1000)+" gms"+"""</td>
10401 amar.kumar 217
            <td style="text-align:center">"""+str(data.weightSnapdeal)+" gms"+"""</td>
10414 kshitij.so 218
            <td style="text-align:center">"""+str(round(data.courierCost*1.1236,2))+"""</td>
219
            <td style="text-align:center">"""+str(round(data.logisticCostSnapdeal,2))+"""</td>
10428 kshitij.so 220
            <td style="text-align:center">"""+getReason(data)+"""</td>
10401 amar.kumar 221
            </tr>"""
222
    message+="""</tbody></table>"""
223
    message+="""
224
            <h3>Unable To Fetch Items</h3>
225
            <table border="1" style="width:100%;">
226
            <thead>
227
            <tr><th>Item Id</th>
228
            <th>Product Name</th>
229
            <th>Our System Selling Price</th>
230
            <th>Our System Transfer Price</th>
231
            <th>Our System Commission</th>
232
            <th>Our System Commission %</th>
233
            <th>Our System Weight</th>
234
            <th>Our Courier Cost</th>
235
            </tr></thead>
236
            <tbody>"""
237
    for data in exceptionList:
238
        snapdealItem = data[0]
239
        marketplaceItem = data[1]
10414 kshitij.so 240
        ds_item = data[2]
10401 amar.kumar 241
        message+="""<tr>
10414 kshitij.so 242
            <td style="text-align:center">"""+str(ds_item.id)+"""</td>
243
            <td style="text-align:center">"""+xstr(ds_item.brand)+" "+xstr(ds_item.model_name)+" "+xstr(ds_item.model_number)+" "+xstr(ds_item.color)+"""</td>
10401 amar.kumar 244
            <td style="text-align:center">"""+str(snapdealItem.sellingPrice)+"""</td>
245
            <td style="text-align:center">"""+str(snapdealItem.transferPrice)+"""</td>
10419 kshitij.so 246
            <td style="text-align:center">"""+str(round(snapdealItem.commission*1.1236,2))+"""</td>
10414 kshitij.so 247
            <td style="text-align:center">"""+str(marketplaceItem.commission)+"%"+"""</td>
248
            <td style="text-align:center">"""+str(ds_item.weight*1000)+" gms"+"""</td>
10419 kshitij.so 249
            <td style="text-align:center">"""+str(round(snapdealItem.courierCost*1.1236,2))+"""</td>
10401 amar.kumar 250
            </tr>"""
251
    message+="""</tbody></table></body></html>"""
252
    print message
253
    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
254
    mailServer.ehlo()
255
    mailServer.starttls()
256
    mailServer.ehlo()
257
 
258
    recipients = ['kshitij.sood@saholic.com']
259
    #recipients = ['rajneesh.arora@saholic.com','rajveer.singh@saholic.com','vikram.raghav@saholic.com','kshitij.sood@saholic.com','khushal.bhatia@saholic.com','chaitnaya.vats@saholic.com','chandan.kumar@saholic.com','manoj.kumar@saholic.com','yukti.jain@saholic.com','ankush.dhingra@saholic.com','manoj.pal@saholic.com']
260
    msg = MIMEMultipart()
261
    msg['Subject'] = "Snapdeal TP Reconciliation" + ' - ' + str(datetime.now())
262
    msg['From'] = ""
263
    msg['To'] = ",".join(recipients)
264
    msg.preamble = "Snapdeal TP Reconciliation" + ' - ' + str(datetime.now())
265
    html_msg = MIMEText(message, 'html')
266
    msg.attach(html_msg)
267
    try:
268
        mailServer.login("build@shop2020.in", "cafe@nes")
269
        #mailServer.sendmail("cafe@nes", ['kshitij.sood@saholic.com'], msg.as_string())
270
        mailServer.sendmail("cafe@nes", recipients, msg.as_string())
271
    except Exception as e:
272
        print e
273
        print "Unable to send Snapdeal TP Reconciliation mail.Lets try with local SMTP."
274
        smtpServer = smtplib.SMTP('localhost')
275
        smtpServer.set_debuglevel(1)
276
        sender = 'support@shop2020.in'
10420 kshitij.so 277
        try:
278
            smtpServer.sendmail(sender, recipients, msg.as_string())
279
            print "Successfully sent email"
280
        except:
281
            print "Error: unable to send email."
10401 amar.kumar 282
 
10414 kshitij.so 283
def write_report(filteredData,exceptionList):
284
    wbk = xlwt.Workbook()
285
    sheet = wbk.add_sheet('Low TP SD')
286
    xstr = lambda s: s or ""
287
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
288
 
289
    excel_integer_format = '0'
290
    integer_style = xlwt.XFStyle()
291
    integer_style.num_format_str = excel_integer_format
292
 
293
    sheet.write(0, 0, "Item ID", heading_xf)
294
    sheet.write(0, 1, "Category", heading_xf)
295
    sheet.write(0, 2, "Product Group.", heading_xf)
296
    sheet.write(0, 3, "SUPC", heading_xf)
297
    sheet.write(0, 4, "Brand", heading_xf)
298
    sheet.write(0, 5, "Product Name", heading_xf)
299
    sheet.write(0, 6, "Our System SP", heading_xf)
300
    sheet.write(0, 7, "Snapdeal SP", heading_xf)
301
    sheet.write(0, 8, "Our TP", heading_xf)
302
    sheet.write(0, 9, "Snapdeal TP", heading_xf)
303
    sheet.write(0, 10, "Our System Commission", heading_xf)
304
    sheet.write(0, 11, "Snapdeal Commission", heading_xf)
305
    sheet.write(0, 12, "Our System Commission %", heading_xf)
306
    sheet.write(0, 13, "Snapdeal Commission %", heading_xf)
307
    sheet.write(0, 14, "Our System Weight (gms)", heading_xf)
308
    sheet.write(0, 15, "Snapdeal Weight", heading_xf)
309
    sheet.write(0, 16, "Our Courier Cost", heading_xf)
310
    sheet.write(0, 17, "Snapdeal Courier Cost", heading_xf)
10428 kshitij.so 311
    sheet.write(0, 18, "Reason", heading_xf)
10414 kshitij.so 312
 
313
    sheet_iterator=1
314
    for data in filteredData:
315
        if data.transferPriceSnapdeal < data.transferPrice:
316
            sheet.write(sheet_iterator, 0, data.itemId)
317
            sheet.write(sheet_iterator, 1, data.parentCategory)
10420 kshitij.so 318
            sheet.write(sheet_iterator, 2, data.productGroup)
319
            sheet.write(sheet_iterator, 3, data.supc)
10414 kshitij.so 320
            sheet.write(sheet_iterator, 4, data.brand)
321
            sheet.write(sheet_iterator, 5, xstr(data.brand)+" "+xstr(data.modelName)+" "+xstr(data.modelNumber)+" "+xstr(data.color))
322
            sheet.write(sheet_iterator, 6, data.sellingPrice)
323
            sheet.write(sheet_iterator, 7, data.sellingPriceSnapdeal)
324
            sheet.write(sheet_iterator, 8, data.transferPrice)
325
            sheet.write(sheet_iterator, 9, data.transferPriceSnapdeal)
326
            sheet.write(sheet_iterator, 10, round(data.commission*1.1236,2))
327
            sheet.write(sheet_iterator, 11, round(float(data.fixedMargin)+float(data.collectionCharges),2))
328
            sheet.write(sheet_iterator, 12, data.commissionPercentage)
329
            sheet.write(sheet_iterator, 13, round(float(data.fixedMarginPercentage)/1.1236,2))
330
            sheet.write(sheet_iterator, 14, data.weight*1000)
331
            sheet.write(sheet_iterator, 15, data.weightSnapdeal)
332
            sheet.write(sheet_iterator, 16, round(data.courierCost*1.1236,2))
333
            sheet.write(sheet_iterator, 17, round(data.logisticCostSnapdeal,2))
10428 kshitij.so 334
            sheet.write(sheet_iterator, 18, getReason(data))
10414 kshitij.so 335
            sheet_iterator+=1
336
 
337
    sheet = wbk.add_sheet('High TP SD')
338
 
339
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
340
 
341
    excel_integer_format = '0'
342
    integer_style = xlwt.XFStyle()
343
    integer_style.num_format_str = excel_integer_format
344
    xstr = lambda s: s or ""
345
 
346
    sheet.write(0, 0, "Item ID", heading_xf)
347
    sheet.write(0, 1, "Category", heading_xf)
348
    sheet.write(0, 2, "Product Group.", heading_xf)
349
    sheet.write(0, 3, "SUPC", heading_xf)
350
    sheet.write(0, 4, "Brand", heading_xf)
351
    sheet.write(0, 5, "Product Name", heading_xf)
352
    sheet.write(0, 6, "Our System SP", heading_xf)
353
    sheet.write(0, 7, "Snapdeal SP", heading_xf)
354
    sheet.write(0, 8, "Our TP", heading_xf)
355
    sheet.write(0, 9, "Snapdeal TP", heading_xf)
356
    sheet.write(0, 10, "Our System Commission", heading_xf)
357
    sheet.write(0, 11, "Snapdeal Commission", heading_xf)
358
    sheet.write(0, 12, "Our System Commission %", heading_xf)
359
    sheet.write(0, 13, "Snapdeal Commission %", heading_xf)
360
    sheet.write(0, 14, "Our System Weight (gms)", heading_xf)
361
    sheet.write(0, 15, "Snapdeal Weight", heading_xf)
362
    sheet.write(0, 16, "Our Courier Cost", heading_xf)
363
    sheet.write(0, 17, "Snapdeal Courier Cost", heading_xf)
10428 kshitij.so 364
    sheet.write(0, 18, "Reason", heading_xf)
10414 kshitij.so 365
 
366
    sheet_iterator=1
367
    for data in filteredData:
368
        if data.transferPriceSnapdeal > data.transferPrice:
369
            sheet.write(sheet_iterator, 0, data.itemId)
370
            sheet.write(sheet_iterator, 1, data.parentCategory)
10420 kshitij.so 371
            sheet.write(sheet_iterator, 2, data.productGroup)
372
            sheet.write(sheet_iterator, 3, data.supc)
10414 kshitij.so 373
            sheet.write(sheet_iterator, 4, data.brand)
374
            sheet.write(sheet_iterator, 5, xstr(data.brand)+" "+xstr(data.modelName)+" "+xstr(data.modelNumber)+" "+xstr(data.color))
375
            sheet.write(sheet_iterator, 6, data.sellingPrice)
376
            sheet.write(sheet_iterator, 7, data.sellingPriceSnapdeal)
377
            sheet.write(sheet_iterator, 8, data.transferPrice)
378
            sheet.write(sheet_iterator, 9, data.transferPriceSnapdeal)
379
            sheet.write(sheet_iterator, 10, round(data.commission*1.1236,2))
380
            sheet.write(sheet_iterator, 11, round(float(data.fixedMargin)+float(data.collectionCharges),2))
381
            sheet.write(sheet_iterator, 12, data.commissionPercentage)
382
            sheet.write(sheet_iterator, 13, round(float(data.fixedMarginPercentage)/1.1236,2))
383
            sheet.write(sheet_iterator, 14, data.weight*1000)
384
            sheet.write(sheet_iterator, 15, data.weightSnapdeal)
385
            sheet.write(sheet_iterator, 16, round(data.courierCost*1.1236,2))
386
            sheet.write(sheet_iterator, 17, round(data.logisticCostSnapdeal,2))
10428 kshitij.so 387
            sheet.write(sheet_iterator, 18, getReason(data))
10414 kshitij.so 388
            sheet_iterator+=1
389
 
390
    sheet = wbk.add_sheet('Exceptions')
391
 
392
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
393
 
394
    excel_integer_format = '0'
395
    integer_style = xlwt.XFStyle()
396
    integer_style.num_format_str = excel_integer_format
397
    xstr = lambda s: s or ""
398
 
399
    sheet.write(0, 0, "Item ID", heading_xf)
400
    sheet.write(0, 1, "SUPC", heading_xf)
401
    sheet.write(0, 2, "Brand", heading_xf)
402
    sheet.write(0, 3, "Product Name", heading_xf)
403
    sheet.write(0, 4, "Our System SP", heading_xf)
404
    sheet.write(0, 5, "Our TP", heading_xf)
405
    sheet.write(0, 6, "Our System Commission", heading_xf)
406
    sheet.write(0, 7, "Our System Commission %", heading_xf)
407
    sheet.write(0, 8, "Our System Weight (gms)", heading_xf)
408
    sheet.write(0, 9, "Our Courier Cost", heading_xf)
409
 
410
    sheet_iterator=1
411
    for data in exceptionList:
412
        snapdealItem = data[0]
413
        marketplaceItem = data[1]
414
        ds_item = data[2]
415
        sheet.write(sheet_iterator, 0, ds_item.id)
416
        sheet.write(sheet_iterator, 1, snapdealItem.supc)
417
        sheet.write(sheet_iterator, 2, ds_item.brand)
10415 kshitij.so 418
        sheet.write(sheet_iterator, 3, xstr(ds_item.brand)+" "+xstr(ds_item.model_name)+" "+xstr(ds_item.model_number)+" "+xstr(ds_item.color))
10414 kshitij.so 419
        sheet.write(sheet_iterator, 4, snapdealItem.sellingPrice)
420
        sheet.write(sheet_iterator, 5, snapdealItem.transferPrice)
421
        sheet.write(sheet_iterator, 6, round(snapdealItem.commission*1.1236,2))
422
        sheet.write(sheet_iterator, 7, marketplaceItem.commission)
423
        sheet.write(sheet_iterator, 8, (ds_item.weight*1000))
10417 kshitij.so 424
        sheet.write(sheet_iterator, 9, round(snapdealItem.courierCost*1.1236,2))
10419 kshitij.so 425
        sheet_iterator+=1
10414 kshitij.so 426
 
427
    filename = "/tmp/snapdeal-tp-reconciliation-" + str(datetime.now()) + ".xls"
428
    wbk.save(filename)
429
 
430
    try:
431
        EmailAttachmentSender.mail("build@shop2020.in", "cafe@nes", ["kshitij.sood@saholic.com"], " Snapdeal TP Reconciliation "+ str(datetime.now()), "", [get_attachment_part(filename)], [""], [])
432
        #EmailAttachmentSender.mail("build@shop2020.in", "cafe@nes", ["chandan.kumar@saholic.com","manoj.kumar@saholic.com","yukti.jain@saholic.com","ankush.dhingra@saholic.com","manoj.pal@saholic.com"], " Snapdeal TP Reconciliation "+ str(datetime.now()), "", [get_attachment_part(filename)], ["rajneesh.arora@saholic.com","rajveer.singh@saholic.com","vikram.raghav@saholic.com","kshitij.sood@saholic.com","chaitnaya.vats@saholic.com","khushal.bhatia@saholic.com"], [])
433
    except Exception as e:
434
        print e
435
        print "Unable to send report.Trying with local SMTP"
436
        smtpServer = smtplib.SMTP('localhost')
437
        smtpServer.set_debuglevel(1)
438
        sender = 'support@shop2020.in'
439
        recipients = ['kshitij.sood@saholic.com']
440
        msg = MIMEMultipart()
441
        msg['Subject'] = "Snapdeal TP Reconciliation" + ' - ' + str(datetime.now())
442
        msg['From'] = sender
443
        #recipients = ['rajneesh.arora@saholic.com','rajveer.singh@saholic.com','vikram.raghav@saholic.com','kshitij.sood@saholic.com','khushal.bhatia@saholic.com','chaitnaya.vats@saholic.com','chandan.kumar@saholic.com','manoj.kumar@saholic.com','yukti.jain@saholic.com','ankush.dhingra@saholic.com','manoj.pal@saholic.com']
444
        msg['To'] = ",".join(recipients)
445
        fileMsg = email.mime.base.MIMEBase('application','vnd.ms-excel')
446
        fileMsg.set_payload(file(filename).read())
447
        email.encoders.encode_base64(fileMsg)
10419 kshitij.so 448
        fileMsg.add_header('Content-Disposition','attachment;filename=snapdeal_tp_recon.xls')
10414 kshitij.so 449
        msg.attach(fileMsg)
450
        try:
451
            smtpServer.sendmail(sender, recipients, msg.as_string())
452
            print "Successfully sent email"
453
        except:
454
            print "Error: unable to send email."
10428 kshitij.so 455
 
456
def getReason(data):
457
    reason=""
458
    if data.sellingPrice!=data.sellingPriceSnapdeal:
459
        reason+="Selling Price is different."
10431 kshitij.so 460
    if data.commissionPercentage!= round(float(data.fixedMargin)/1.1236,2):
10430 kshitij.so 461
        reason+="Commission is different."
462
    if round(data.courierCost*1.1236)!=round(data.logisticCostSnapdeal):
10428 kshitij.so 463
        reason+="Courier Cost is different-Check Weight."
464
    return reason
10414 kshitij.so 465
 
466
 
10401 amar.kumar 467
def main():
468
    print "Opening snapdeal seller login page"
469
    br = login("http://selleraccounts.snapdeal.com/")
470
    exceptionList, fetchedItems = populateStuff(br)
471
    filteredData = filterData(fetchedItems)
472
    sendMail(filteredData,exceptionList)
10414 kshitij.so 473
    write_report(filteredData,exceptionList)
10401 amar.kumar 474
 
475
 
476
if __name__ == "__main__":
477
    main()
478
 
479