Rev 12462 | Blame | Compare with Previous | Last modification | View Log | RSS feed
from elixir import *from shop2020.config.client.ConfigClient import ConfigClientfrom shop2020.model.v1.catalog.impl import DataServicefrom shop2020.model.v1.catalog.impl.DataService import FlipkartItem, MarketplaceItems, Item, Category, \MarketPlaceUpdateHistory, SourcePercentageMaster, SourceItemPercentage, SourceCategoryPercentagefrom shop2020.thriftpy.model.v1.order.ttypes import OrderSourceimport sysfrom time import sleepimport smtplibfrom datetime import datetime, timedeltafrom shop2020.utils import EmailAttachmentSenderfrom shop2020.utils.EmailAttachmentSender import get_attachment_partfrom email.mime.text import MIMETextimport emailfrom email.mime.multipart import MIMEMultipartimport email.encodersimport copyconfig_client = ConfigClient()host = config_client.get_property('staging_hostname')DataService.initialize(db_hostname=host)def getFutureExpiry():fkItems = []timestampNow = datetime.now()timestamp = datetime.now() + timedelta(days=3)expiryItems = session.query(SourceItemPercentage).filter(SourceItemPercentage.source==OrderSource.FLIPKART).filter(SourceItemPercentage.expiryDate.between(timestampNow,timestamp)).all()print expiryItemsfor expiryItem in expiryItems:fkItem = FlipkartItem.get_by(item_id=expiryItem.item_id)mpItem = MarketplaceItems.get_by(itemId=expiryItem.item_id, source=OrderSource.FLIPKART)item = Item.get_by(id=expiryItem.item_id)if fkItem is not None or mpItem is not None:temp = []temp.append(expiryItem)temp.append(fkItem)temp.append(mpItem)comObj = getFutureCommission(OrderSource.FLIPKART,item,timestamp)temp.append(comObj)temp.append(item)fkItems.append(temp)return fkItemsdef validateCommission():diffItems = []items_mp = session.query(Item,MarketplaceItems).join((MarketplaceItems,Item.id==MarketplaceItems.itemId)).filter(MarketplaceItems.source==OrderSource.FLIPKART).all()for item_mp in items_mp:mpItem = item_mp[1]currentComObj = getCurrentValidCommission(OrderSource.FLIPKART,item_mp[0])if (currentComObj.commission!=mpItem.commission):temp=[]temp.append(item_mp[0]) #itemtemp.append(item_mp[1]) #mpItemtemp.append(copy.deepcopy(item_mp[1])) #mpItemCopytemp.append(currentComObj) #comObjdiffItems.append(temp)return diffItemsdef getFutureCommission(source_id,item,time):sip = SourceItemPercentage.query.filter(SourceItemPercentage.item_id==item.id).filter(SourceItemPercentage.source==source_id).filter(SourceItemPercentage.startDate<=time).filter(SourceItemPercentage.expiryDate>=time).first()if sip is not None:return sipscp = SourceCategoryPercentage.query.filter(SourceCategoryPercentage.category_id==item.category).filter(SourceCategoryPercentage.source==source_id).filter(SourceCategoryPercentage.startDate<=time).filter(SourceCategoryPercentage.expiryDate>=time).first()if scp is not None:return scpelse:spm = SourcePercentageMaster.get_by(source=source_id)return spmdef getCurrentValidCommission(source_id,item):time = datetime.now()sip = SourceItemPercentage.query.filter(SourceItemPercentage.item_id==item.id).filter(SourceItemPercentage.source==source_id).filter(SourceItemPercentage.startDate<=time).filter(SourceItemPercentage.expiryDate>=time).first()if sip is not None:return sipelse:scp = SourceCategoryPercentage.query.filter(SourceCategoryPercentage.category_id==item.category).filter(SourceCategoryPercentage.source==source_id).filter(SourceCategoryPercentage.startDate<=time).filter(SourceCategoryPercentage.expiryDate>=time).first()if scp is not None:return scpelse:spm = SourcePercentageMaster.get_by(source=source_id)return spmdef sendMailAndSync(fkItems,syncedItems):if len(fkItems) == 0 and len(syncedItems) ==0:print "Nothing to send"returnxstr = lambda s: s or ""message="""<html><body><h3>Commission about to get expire</h3><p>System will automatically update commission on expiry</p><table border="1" style="width:100%;"><thead><tr><th>Product Name</th><th>SP</th><th>Current TP</th><th>Future TP</th><th>Current BE</th><th>Future BE</th><th>Old Com%</th><th>Future Com%</th><th>Old Margin</th><th>New Margin</th><th>Old Margin%</th><th>New Margin%</th><th>Expiry</th></tr></thead><tbody>"""for fkItem in fkItems:expiryItem = fkItem[0]flipkartObj = fkItem[1]mpItem = fkItem[2]comObj = fkItem[3]item = fkItem[4]message+="""<tr><td style="text-align:center">"""+xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color)+"""</td><td style="text-align:center">"""+str(mpItem.currentSp)+"""</td><td style="text-align:center">"""+str(round(mpItem.currentTp))+"""</td><td style="text-align:center">"""+str(getNewTp(mpItem,comObj))+"""</td><td style="text-align:center">"""+str(mpItem.minimumPossibleSp)+"""</td><td style="text-align:center">"""+str(getNewBreakEven(flipkartObj,mpItem,comObj))+"""</td><td style="text-align:center">"""+str(mpItem.commission)+" %"+"""</td><td style="text-align:center">"""+str(comObj.commission)+" %"+"""</td><td style="text-align:center">"""+str(round(mpItem.currentTp-mpItem.minimumPossibleTp))+"""</td><td style="text-align:center">"""+str(round(getNewTp(mpItem,comObj)-mpItem.minimumPossibleTp))+"""</td><td style="text-align:center">"""+str(round(((mpItem.currentTp-mpItem.minimumPossibleTp)/mpItem.currentSp)*100,1))+"%"+"""</td><td style="text-align:center">"""+str(round(((getNewTp(mpItem,comObj)-mpItem.minimumPossibleTp)/mpItem.currentSp)*100,1))+" %"+"""</td><td style="text-align:center">"""+str(expiryItem.expiryDate)+"""</td></tr>"""message+="""</tbody></table><h3>Commission Updated</h3><table border="1" style="width:100%;"><thead><tr><th>Product Name</th><th>Current SP</th><th>Old TP</th><th>New TP</th><th>Old BE</th><th>New BE</th><th>Old Com %</th><th>New Com %</th><th>Old Margin%</th><th>New Margin%</th></tr></thead><tbody>"""for syncedItem in syncedItems:item = syncedItem[0]mpItem = syncedItem[1]oldMpItem = syncedItem[2]message+="""<tr><td style="text-align:center">"""+xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color)+"""</td><td style="text-align:center">"""+str(mpItem.currentSp)+"""</td><td style="text-align:center">"""+str(round(oldMpItem.currentTp))+"""</td><td style="text-align:center">"""+str(round(mpItem.currentTp))+"""</td><td style="text-align:center">"""+str(round(oldMpItem.minimumPossibleSp))+"""</td><td style="text-align:center">"""+str(round(mpItem.minimumPossibleSp))+"""</td><td style="text-align:center">"""+str(round(oldMpItem.commission))+"%"+"""</td><td style="text-align:center">"""+str(round(mpItem.commission))+"%"+"""</td><td style="text-align:center">"""+str(round(((oldMpItem.currentTp-oldMpItem.minimumPossibleTp)/oldMpItem.currentSp)*100,1))+"""</td><td style="text-align:center">"""+str(round(((mpItem.currentTp-mpItem.minimumPossibleTp)/mpItem.currentSp)*100,1))+"""</td></tr>"""message+="""</tbody></table></body></html>"""print messagemailServer = smtplib.SMTP("smtp.gmail.com", 587)mailServer.ehlo()mailServer.starttls()mailServer.ehlo()#recipients = ['kshitij.sood@saholic.com']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']msg = MIMEMultipart()msg['Subject'] = "IMPORTANT: Flipkart Commission Expiry" + ' - ' + str(datetime.now())msg['From'] = ""msg['To'] = ",".join(recipients)msg.preamble = "IMPORTANT: Flipkart Commission Expiry" + ' - ' + str(datetime.now())html_msg = MIMEText(message, 'html')msg.attach(html_msg)try:mailServer.login("build@shop2020.in", "cafe@nes")#mailServer.sendmail("cafe@nes", ['kshitij.sood@saholic.com'], msg.as_string())mailServer.sendmail("cafe@nes", recipients, msg.as_string())except Exception as e:print eprint "Unable to send commission expiry mail.Lets try with local SMTP."smtpServer = smtplib.SMTP('localhost')smtpServer.set_debuglevel(1)sender = 'support@shop2020.in'try:smtpServer.sendmail(sender, recipients, msg.as_string())print "Successfully sent email"except:print "Error: unable to send email."def syncCommission(diffItems):for diffItem in diffItems:mpItem = diffItem[1]comObj = diffItem[3]fkItemObj = FlipkartItem.get_by(item_id=mpItem.itemId)mpItem.currentTp = getNewTp(mpItem,comObj)fkItemObj.commissionValue = round((comObj.commission/100)*mpItem.currentSp,2)fkItemObj.serviceTaxValue = round((mpItem.serviceTax/100)*(fkItemObj.commissionValue+mpItem.courierCost),2)mpItem.minimumPossibleSp = getNewBreakEven(fkItemObj,mpItem,comObj)mpItem.commission = comObj.commissionsession.commit()return diffItemsdef getNewTp(mpItem,comObj):newTp = mpItem.currentSp- mpItem.currentSp*(comObj.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))-(mpItem.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100))return round(newTp,2)def getNewBreakEven(flipkartObj,mpItem,comObj):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));return round(lowestPossibleSp,2)def main():diffItems = validateCommission()syncedItems = syncCommission(diffItems)fkItems = getFutureExpiry()sendMailAndSync(fkItems,syncedItems)if __name__ == "__main__":main()