Subversion Repositories SmartDukaan

Rev

Rev 23249 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python

'''
Loads the static logic of assigning provider to a particular pincode
into the DestinationProviderAllocation table.

@author: Amit Gupta
'''
from datetime import *
from elixir import *
from shop2020.logistics.service.impl import DataService, EcomExpressService
from shop2020.logistics.service.impl.DataAccessor import add_empty_AWBs
from shop2020.logistics.service.impl.DataService import \
    ServiceableLocationDetails, Awb
from shop2020.logistics.service.impl.EcomExpressService import generate_awb
from shop2020.logistics.service.script import AwbNumberLoader
from shop2020.thriftpy.logistics.ttypes import DeliveryType
from shop2020.utils.EmailAttachmentSender import get_attachment_part, mail
import optparse

if __name__ == '__main__' and __package__ is None:
    import sys
    import os
    sys.path.insert(0, os.getcwd())



def main():
    parser = optparse.OptionParser()
    parser.add_option("-H", "--host", dest="hostname",
                  default="localhost",
                  type="string", help="The HOST where the DB server is running",
                  metavar="HOST")
    parser.add_option("-P", "--provider_id", dest="provider_id",
                  default="localhost",
                  type="string", help="The HOST where the DB server is running",
                  metavar="HOST")
    (options, args) = parser.parse_args()
    if len(args) != 0:
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
    provider_id = options.provider_id
    hostname = options.hostname
    if provider_id is None or hostname is None:
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
    
    DataService.initialize(dbname='logistics', db_hostname=hostname)
    if provider_id=="49":
        subject = "Tat is missing for Ecom Express active pincodes"
        missingPincodes = []
        try:
            serviceabilityPincodeList = EcomExpressService.getServiceablePinCodes(datetime.now()-timedelta(days=15))
            #print "serviceabilityPincodeList", serviceabilityPincodeList
            for serviceabilityPincode in serviceabilityPincodeList:
                pincode = str(serviceabilityPincode["pincode"])
                serviceableLocationDetails = ServiceableLocationDetails.query.filter_by(provider_id=int(provider_id)).filter_by(dest_pincode=pincode).first()
                if serviceableLocationDetails:
                    if serviceabilityPincode['active']:
                        serviceableLocationDetails.exp=1
                        serviceableLocationDetails.cod=1
                    else:
                        serviceableLocationDetails.exp=0
                        serviceableLocationDetails.cod=0
                        
                elif serviceabilityPincode['active']:
                    missingPincodes.append(pincode)
            print missingPincodes
            if missingPincodes:
                subject += " - " + ", ".join(missingPincodes)
                mail("cnc.center@shop2020.in", "5h0p2o2o", ["deena.nath@smartdukaan.com"], subject, text="")
            
            session.commit()
        except:
            print "problem updating pincodes" 
        print "After Error"
            #add_new_awbs(provider_id, isCod, awbs, awbUsedFor)
        prepaidCount = Awb.query.filter_by(provider_id = provider_id, is_available = True, type='Prepaid').count()
        if prepaidCount < 100:
            prepaidAwbs = EcomExpressService.generate_awb(DeliveryType.PREPAID)
            add_empty_AWBs(prepaidAwbs, 49, "Prepaid")
        
        codCount = Awb.query.filter_by(provider_id = provider_id, is_available = True, type='COD').count()
        if codCount < 100:
            codAwbs = EcomExpressService.generate_awb(DeliveryType.COD)
            add_empty_AWBs(codAwbs, 49, "COD")
            
    session.close()
                
        
if __name__ == '__main__':
    main()