Subversion Repositories SmartDukaan

Rev

Rev 11148 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11147 kshitij.so 1
from elixir import *
2
from shop2020.config.client.ConfigClient import ConfigClient
3
from shop2020.model.v1.catalog.impl import DataService
4
from shop2020.model.v1.catalog.impl.DataService import FlipkartItem, MarketplaceItems, Item, Category, \
5
MarketPlaceUpdateHistory, SourcePercentageMaster, SourceItemPercentage, SourceCategoryPercentage
6
from shop2020.thriftpy.model.v1.order.ttypes import OrderSource
7
import sys
8
from time import sleep
9
import smtplib
10
from datetime import datetime, timedelta
11
from shop2020.utils import EmailAttachmentSender
12
from shop2020.utils.EmailAttachmentSender import get_attachment_part
13
from email.mime.text import MIMEText
14
import email
15
from email.mime.multipart import MIMEMultipart
16
import email.encoders
17
import copy
18
 
19
config_client = ConfigClient()
20
host = config_client.get_property('staging_hostname')
21
DataService.initialize(db_hostname=host)
22
 
23
 
24
 
25
def getFutureExpiry():
26
    fkItems = []
27
    timestampNow = datetime.now()
28
    timestamp = datetime.now() + timedelta(days=3)
29
    expiryItems = session.query(SourceItemPercentage).filter(SourceItemPercentage.source==OrderSource.FLIPKART).filter(SourceItemPercentage.expiryDate.between(timestampNow,timestamp)).all()
30
    print expiryItems
31
    for expiryItem in expiryItems:
32
        fkItem = FlipkartItem.get_by(item_id=expiryItem.item_id)
33
        mpItem = MarketplaceItems.get_by(itemId=expiryItem.item_id, source=OrderSource.FLIPKART)
34
        item = Item.get_by(id=expiryItem.item_id) 
35
        if fkItem is not None or mpItem is not None:
36
            temp = []
37
            temp.append(expiryItem)
38
            temp.append(fkItem)
39
            temp.append(mpItem)
40
            comObj = getFutureCommission(OrderSource.FLIPKART,item,timestamp)
41
            temp.append(comObj)
42
            temp.append(item)
43
            fkItems.append(temp)
44
    return fkItems
45
 
46
 
47
def validateCommission():
48
    diffItems = []
49
    items_mp = session.query(Item,MarketplaceItems).join((MarketplaceItems,Item.id==MarketplaceItems.itemId)).filter(MarketplaceItems.source==OrderSource.FLIPKART).all()
50
    for item_mp in items_mp:
51
        mpItem = item_mp[1]
52
        currentComObj = getCurrentValidCommission(OrderSource.FLIPKART,item_mp[0])
53
        if (currentComObj.commission!=mpItem.commission):
54
            temp=[]
55
            temp.append(item_mp[0]) #item
56
            temp.append(item_mp[1]) #mpItem
57
            temp.append(copy.deepcopy(item_mp[1])) #mpItemCopy
58
            temp.append(currentComObj) #comObj
59
            diffItems.append(temp)
60
    return diffItems
61
 
62
def getFutureCommission(source_id,item,time):
63
    sip = SourceItemPercentage.query.filter(SourceItemPercentage.item_id==item.id).filter(SourceItemPercentage.source==source_id).filter(SourceItemPercentage.startDate<=time).filter(SourceItemPercentage.expiryDate>=time).first()
64
    if sip is not None:
65
        return sip
66
    scp = SourceCategoryPercentage.query.filter(SourceCategoryPercentage.category_id==item.category).filter(SourceCategoryPercentage.source==source_id).filter(SourceCategoryPercentage.startDate<=time).filter(SourceCategoryPercentage.expiryDate>=time).first()
67
    if scp is not None:
68
        return scp
69
    else:
70
        spm = SourcePercentageMaster.get_by(source=source_id)
71
        return spm
72
 
73
def getCurrentValidCommission(source_id,item):
74
    time = datetime.now()
75
    sip = SourceItemPercentage.query.filter(SourceItemPercentage.item_id==item.id).filter(SourceItemPercentage.source==source_id).filter(SourceItemPercentage.startDate<=time).filter(SourceItemPercentage.expiryDate>=time).first()
76
    if sip is not None:
77
        return sip
78
    else:
79
        scp = SourceCategoryPercentage.query.filter(SourceCategoryPercentage.category_id==item.category).filter(SourceCategoryPercentage.source==source_id).filter(SourceCategoryPercentage.startDate<=time).filter(SourceCategoryPercentage.expiryDate>=time).first()
80
        if scp is not None:
81
            return scp
82
        else:
83
            spm = SourcePercentageMaster.get_by(source=source_id)
84
            return spm
85
 
86
def sendMailAndSync(fkItems,syncedItems):
87
    xstr = lambda s: s or ""
88
    message="""<html>
89
            <body>
90
            <h3>Commission about to get expire</h3>
91
            <table border="1" style="width:100%;">
92
            <thead>
93
            <tr><th>Product Name</th>
94
            <th>SP</th>
95
            <th>Current TP</th>
96
            <th>Future TP</th>
97
            <th>Current BE</th>
98
            <th>Future BE</th>
99
            <th>Old Com%</th>
100
            <th>Future Comm%</th>
101
            <th>Old Margin</th>
102
            <th>New Margin</th>
103
            <th>Old Margin%</th>
104
            <th>New Margin%</th>
105
            <th>Expiry</th>
106
            </tr></thead>
107
            <tbody>"""
108
    for fkItem in fkItems:
109
        expiryItem = fkItem[0]
110
        flipkartObj = fkItem[1]
111
        mpItem = fkItem[2]
112
        comObj = fkItem[3]
113
        item = fkItem[4]
114
        message+="""<tr>
115
        <td style="text-align:center">"""+xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color)+"""</td>
116
        <td style="text-align:center">"""+str(mpItem.currentSp)+"""</td>
117
        <td style="text-align:center">"""+str(round(mpItem.currentTp))+"""</td>
118
        <td style="text-align:center">"""+str(getNewTp(mpItem,comObj))+"""</td>
119
        <td style="text-align:center">"""+str(mpItem.minimumPossibleSp)+"""</td>
120
        <td style="text-align:center">"""+str(getNewBreakEven(flipkartObj,mpItem,comObj))+"""</td>
121
        <td style="text-align:center">"""+str(mpItem.commission)+" %"+"""</td>
122
        <td style="text-align:center">"""+str(comObj.commission)+" %"+"""</td>
123
        <td style="text-align:center">"""+str(round(mpItem.currentTp-mpItem.minimumPossibleTp))+"""</td>
124
        <td style="text-align:center">"""+str(round(getNewTp(mpItem,comObj)-mpItem.minimumPossibleTp))+"""</td>
125
        <td style="text-align:center">"""+str(round(((mpItem.currentTp-mpItem.minimumPossibleTp)/mpItem.currentSp)*100,1))+"%"+"""</td>
126
        <td style="text-align:center">"""+str(round(((getNewTp(mpItem,comObj)-mpItem.minimumPossibleTp)/mpItem.currentSp)*100,1))+" %"+"""</td>
127
        <td style="text-align:center">"""+str(expiryItem.expiryDate)+"""</td>
128
        </tr>"""
129
    message+="""</tbody></table><h3>Commission Updated</h3><table border="1" style="width:100%;">
130
    <thead>
131
            <tr><th>Product Name</th>
132
            <th>Current SP</th>
133
            <th>Old TP</th>
134
            <th>New TP</th>
135
            <th>Old BE</th>
136
            <th>New BE</th>
137
            <th>Old Com %</th>
138
            <th>New Com %</th>
139
            <th>Old Margin%</th>
140
            <th>New Margin%</th>
141
            </tr></thead>
142
            <tbody>"""
143
    for syncedItem in syncedItems:
144
        item = syncedItem[0]
145
        mpItem = syncedItem[1]
146
        oldMpItem = syncedItem[2]
147
        message+="""<tr>
148
        <td style="text-align:center">"""+xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color)+"""</td>
149
        <td style="text-align:center">"""+str(mpItem.currentSp)+"""</td>
150
        <td style="text-align:center">"""+str(round(oldMpItem.currentTp))+"""</td>
151
        <td style="text-align:center">"""+str(round(mpItem.currentTp))+"""</td>
152
        <td style="text-align:center">"""+str(round(oldMpItem.minimumPossibleSp))+"""</td>
153
        <td style="text-align:center">"""+str(round(mpItem.minimumPossibleSp))+"""</td>
154
        <td style="text-align:center">"""+str(round(oldMpItem.commission))+"%"+"""</td>
155
        <td style="text-align:center">"""+str(round(mpItem.commission))+"%"+"""</td>
156
        <td style="text-align:center">"""+str(round(((oldMpItem.currentTp-oldMpItem.minimumPossibleTp)/oldMpItem.currentSp)*100,1))+"""</td>
157
        <td style="text-align:center">"""+str(round(((mpItem.currentTp-mpItem.minimumPossibleTp)/mpItem.currentSp)*100,1))+"""</td>
158
        </tr>"""
159
    message+="""</tbody></table></body></html>"""
160
 
161
    print message
162
    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
163
    mailServer.ehlo()
164
    mailServer.starttls()
165
    mailServer.ehlo()
166
 
167
    #recipients = ['kshitij.sood@saholic.com']
168
    recipients = ['rajneesh.arora@saholic.com','anikendra.das@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','sandeep.sachdeva@saholic.com']
169
    msg = MIMEMultipart()
170
    msg['Subject'] = "IMPORTANT: Flipkart Commission Expiry" + ' - ' + str(datetime.now())
171
    msg['From'] = ""
172
    msg['To'] = ",".join(recipients)
173
    msg.preamble = "IMPORTANT: Flipkart Commission Expiry" + ' - ' + str(datetime.now())
174
    html_msg = MIMEText(message, 'html')
175
    msg.attach(html_msg)
176
    try:
177
        mailServer.login("build@shop2020.in", "cafe@nes")
178
        #mailServer.sendmail("cafe@nes", ['kshitij.sood@saholic.com'], msg.as_string())
179
        mailServer.sendmail("cafe@nes", recipients, msg.as_string())
180
    except Exception as e:
181
        print e
182
        print "Unable to send commission expiry mail.Lets try with local SMTP."
183
        smtpServer = smtplib.SMTP('localhost')
184
        smtpServer.set_debuglevel(1)
185
        sender = 'support@shop2020.in'
186
        try:
187
            smtpServer.sendmail(sender, recipients, msg.as_string())
188
            print "Successfully sent email"
189
        except:
190
            print "Error: unable to send email."
191
 
192
def syncCommission(diffItems):
193
    for diffItem in diffItems:
194
        mpItem = diffItem[1]
195
        comObj = diffItem[3]
196
        fkItemObj = FlipkartItem.get_by(item_id=mpItem.itemId)
197
        mpItem.currentTp = getNewTp(mpItem,comObj)
198
        fkItemObj.commissionValue = round((comObj.commission/100)*mpItem.currentSp,2)
199
        fkItemObj.serviceTaxValue = round((mpItem.serviceTax/100)*(fkItemObj.commissionValue+mpItem.courierCost),2)
200
        mpItem.minimumPossibleSp = getNewBreakEven(fkItemObj,mpItem,comObj)
201
        mpItem.commission = comObj.commission
202
    session.commit()
203
    return diffItems
204
 
205
 
206
def getNewTp(mpItem,comObj):
207
    newTp = mpItem.currentSp- mpItem.currentSp*(comObj.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))-(mpItem.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100))
208
    return round(newTp,2)
209
 
210
def getNewBreakEven(flipkartObj,mpItem,comObj):
211
    lowestPossibleSp = (flipkartObj.maxNlc+(mpItem.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100))*(1+(mpItem.vat/100))+(15+mpItem.otherCost)*(1+(mpItem.vat)/100))/(1-(comObj.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))*(1+(mpItem.vat)/100)-(mpItem.returnProvision/100)*(1+(mpItem.vat)/100));
212
    return round(lowestPossibleSp,2)
213
 
214
def main():
215
    diffItems = validateCommission()
216
    syncedItems = syncCommission(diffItems)
217
    fkItems = getFutureExpiry()
218
    sendMailAndSync(fkItems,syncedItems)
219
 
220
 
221
if __name__ == "__main__":
222
    main()