Subversion Repositories SmartDukaan

Rev

Rev 11148 | Rev 11380 | Go to most recent revision | Details | Compare with Previous | 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>
11148 kshitij.so 91
            <p>System will automatically update commission on expiry</p>
11147 kshitij.so 92
            <table border="1" style="width:100%;">
93
            <thead>
94
            <tr><th>Product Name</th>
95
            <th>SP</th>
96
            <th>Current TP</th>
97
            <th>Future TP</th>
98
            <th>Current BE</th>
99
            <th>Future BE</th>
100
            <th>Old Com%</th>
11148 kshitij.so 101
            <th>Future Com%</th>
11147 kshitij.so 102
            <th>Old Margin</th>
103
            <th>New Margin</th>
104
            <th>Old Margin%</th>
105
            <th>New Margin%</th>
106
            <th>Expiry</th>
107
            </tr></thead>
108
            <tbody>"""
109
    for fkItem in fkItems:
110
        expiryItem = fkItem[0]
111
        flipkartObj = fkItem[1]
112
        mpItem = fkItem[2]
113
        comObj = fkItem[3]
114
        item = fkItem[4]
115
        message+="""<tr>
116
        <td style="text-align:center">"""+xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color)+"""</td>
117
        <td style="text-align:center">"""+str(mpItem.currentSp)+"""</td>
118
        <td style="text-align:center">"""+str(round(mpItem.currentTp))+"""</td>
119
        <td style="text-align:center">"""+str(getNewTp(mpItem,comObj))+"""</td>
120
        <td style="text-align:center">"""+str(mpItem.minimumPossibleSp)+"""</td>
121
        <td style="text-align:center">"""+str(getNewBreakEven(flipkartObj,mpItem,comObj))+"""</td>
122
        <td style="text-align:center">"""+str(mpItem.commission)+" %"+"""</td>
123
        <td style="text-align:center">"""+str(comObj.commission)+" %"+"""</td>
124
        <td style="text-align:center">"""+str(round(mpItem.currentTp-mpItem.minimumPossibleTp))+"""</td>
125
        <td style="text-align:center">"""+str(round(getNewTp(mpItem,comObj)-mpItem.minimumPossibleTp))+"""</td>
126
        <td style="text-align:center">"""+str(round(((mpItem.currentTp-mpItem.minimumPossibleTp)/mpItem.currentSp)*100,1))+"%"+"""</td>
127
        <td style="text-align:center">"""+str(round(((getNewTp(mpItem,comObj)-mpItem.minimumPossibleTp)/mpItem.currentSp)*100,1))+" %"+"""</td>
128
        <td style="text-align:center">"""+str(expiryItem.expiryDate)+"""</td>
129
        </tr>"""
130
    message+="""</tbody></table><h3>Commission Updated</h3><table border="1" style="width:100%;">
131
    <thead>
132
            <tr><th>Product Name</th>
133
            <th>Current SP</th>
134
            <th>Old TP</th>
135
            <th>New TP</th>
136
            <th>Old BE</th>
137
            <th>New BE</th>
138
            <th>Old Com %</th>
139
            <th>New Com %</th>
140
            <th>Old Margin%</th>
141
            <th>New Margin%</th>
142
            </tr></thead>
143
            <tbody>"""
144
    for syncedItem in syncedItems:
145
        item = syncedItem[0]
146
        mpItem = syncedItem[1]
147
        oldMpItem = syncedItem[2]
148
        message+="""<tr>
149
        <td style="text-align:center">"""+xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color)+"""</td>
150
        <td style="text-align:center">"""+str(mpItem.currentSp)+"""</td>
151
        <td style="text-align:center">"""+str(round(oldMpItem.currentTp))+"""</td>
152
        <td style="text-align:center">"""+str(round(mpItem.currentTp))+"""</td>
153
        <td style="text-align:center">"""+str(round(oldMpItem.minimumPossibleSp))+"""</td>
154
        <td style="text-align:center">"""+str(round(mpItem.minimumPossibleSp))+"""</td>
155
        <td style="text-align:center">"""+str(round(oldMpItem.commission))+"%"+"""</td>
156
        <td style="text-align:center">"""+str(round(mpItem.commission))+"%"+"""</td>
157
        <td style="text-align:center">"""+str(round(((oldMpItem.currentTp-oldMpItem.minimumPossibleTp)/oldMpItem.currentSp)*100,1))+"""</td>
158
        <td style="text-align:center">"""+str(round(((mpItem.currentTp-mpItem.minimumPossibleTp)/mpItem.currentSp)*100,1))+"""</td>
159
        </tr>"""
160
    message+="""</tbody></table></body></html>"""
161
 
162
    print message
163
    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
164
    mailServer.ehlo()
165
    mailServer.starttls()
166
    mailServer.ehlo()
167
 
168
    #recipients = ['kshitij.sood@saholic.com']
11149 kshitij.so 169
    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']
11147 kshitij.so 170
    msg = MIMEMultipart()
171
    msg['Subject'] = "IMPORTANT: Flipkart Commission Expiry" + ' - ' + str(datetime.now())
172
    msg['From'] = ""
173
    msg['To'] = ",".join(recipients)
174
    msg.preamble = "IMPORTANT: Flipkart Commission Expiry" + ' - ' + str(datetime.now())
175
    html_msg = MIMEText(message, 'html')
176
    msg.attach(html_msg)
177
    try:
178
        mailServer.login("build@shop2020.in", "cafe@nes")
179
        #mailServer.sendmail("cafe@nes", ['kshitij.sood@saholic.com'], msg.as_string())
180
        mailServer.sendmail("cafe@nes", recipients, msg.as_string())
181
    except Exception as e:
182
        print e
183
        print "Unable to send commission expiry mail.Lets try with local SMTP."
184
        smtpServer = smtplib.SMTP('localhost')
185
        smtpServer.set_debuglevel(1)
186
        sender = 'support@shop2020.in'
187
        try:
188
            smtpServer.sendmail(sender, recipients, msg.as_string())
189
            print "Successfully sent email"
190
        except:
191
            print "Error: unable to send email."
192
 
193
def syncCommission(diffItems):
194
    for diffItem in diffItems:
195
        mpItem = diffItem[1]
196
        comObj = diffItem[3]
197
        fkItemObj = FlipkartItem.get_by(item_id=mpItem.itemId)
198
        mpItem.currentTp = getNewTp(mpItem,comObj)
199
        fkItemObj.commissionValue = round((comObj.commission/100)*mpItem.currentSp,2)
200
        fkItemObj.serviceTaxValue = round((mpItem.serviceTax/100)*(fkItemObj.commissionValue+mpItem.courierCost),2)
201
        mpItem.minimumPossibleSp = getNewBreakEven(fkItemObj,mpItem,comObj)
202
        mpItem.commission = comObj.commission
203
    session.commit()
204
    return diffItems
205
 
206
 
207
def getNewTp(mpItem,comObj):
208
    newTp = mpItem.currentSp- mpItem.currentSp*(comObj.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))-(mpItem.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100))
209
    return round(newTp,2)
210
 
211
def getNewBreakEven(flipkartObj,mpItem,comObj):
212
    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));
213
    return round(lowestPossibleSp,2)
214
 
215
def main():
216
    diffItems = validateCommission()
217
    syncedItems = syncCommission(diffItems)
218
    fkItems = getFutureExpiry()
219
    sendMailAndSync(fkItems,syncedItems)
220
 
221
 
222
if __name__ == "__main__":
223
    main()