Subversion Repositories SmartDukaan

Rev

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