Subversion Repositories SmartDukaan

Rev

Rev 20641 | Rev 20745 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
442 rajveer 1
'''
2
Created on 13-Sep-2010
3
 
4
@author: rajveer
5
'''
6
 
16025 amit.gupta 7
from elixir import session
8
from shop2020.clients.CatalogClient import CatalogClient
9
from shop2020.clients.InventoryClient import InventoryClient
20724 kshitij.so 10
from shop2020.clients.TransactionClient import TransactionClient
442 rajveer 11
from shop2020.logistics.service.impl import DataService
20724 kshitij.so 12
from shop2020.logistics.service.impl import BluedartService
644 chandransh 13
from shop2020.logistics.service.impl.DataService import Awb, AwbUpdate, Provider, \
16025 amit.gupta 14
    DeliveryEstimate, WarehouseAllocation, PublicHolidays, \
19421 manish.sha 15
    ServiceableLocationDetails, PickupStore, Locations, ProviderCosting
16025 amit.gupta 16
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException, \
19421 manish.sha 17
    DeliveryType, LogisticsLocationInfo, LocationInfo, ProviderInfo, DeliveryEstimateAndCosting
19413 amit.gupta 18
from shop2020.thriftpy.model.v1.inventory.ttypes import Warehouse, BillingType, \
16025 amit.gupta 19
    WarehouseType
442 rajveer 20
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
5964 amar.kumar 21
from sqlalchemy.sql import or_
16025 amit.gupta 22
import datetime
3355 chandransh 23
import logging
13146 manish.sha 24
import os
16025 amit.gupta 25
import sys
26
import time
19413 amit.gupta 27
from collections import namedtuple
19483 manish.sha 28
import math
20724 kshitij.so 29
import traceback
19413 amit.gupta 30
 
3355 chandransh 31
logging.basicConfig(level=logging.DEBUG)
442 rajveer 32
 
19413 amit.gupta 33
PincodeProvider = namedtuple("PincodeProvider", ["dest_pin", "provider"])
34
 
3217 rajveer 35
warehouse_allocation_cache = {}
36
serviceable_location_cache = {}
37
delivery_estimate_cache = {}
16025 amit.gupta 38
warehouse_location_cache = {}
19413 amit.gupta 39
location_state={}
20275 amit.gupta 40
 
41
'Delhivery Bluedart, Fedex Surface, Fedex Air'
19413 amit.gupta 42
state_locations = {}
19421 manish.sha 43
provider_costing_sheet = {}
44
DELHIVERY = 3
6370 rajveer 45
 
19413 amit.gupta 46
#pincode location map is ab
47
#{'110001':{1:{"sameState":False, "providerInfo":{1:(1,1), 2:(1,1)}}},}
48
pincode_locations = {}
49
#{(dest_pin, provider):(otgAvailable,providerCodLimit, websiteCodLimit, storeCodLimit, providerPrepaidLimit)} 
50
serviceable_map = {}
51
 
52
#Need to identify a better way
53
statepinmap={'11':0, 
54
             '40':1,'41':1,'42':1,'43':1,'44':1,
55
             '56':2, '57':2, '58':2, '59':2,
56
             '12':3, '13':3, 
57
             '30':4, '31':4, '32':4, '33':4, '34':4,
58
             '36':6,'37':6,'38':6,'39':6
59
             }
60
 
3218 rajveer 61
'''
62
This class is for only data transfer. Never used outside this module.
63
'''
64
class _DeliveryEstimateObject:
6537 rajveer 65
    def __init__(self, delivery_time, delivery_delay, provider_id, codAllowed, otgAvailable):
3218 rajveer 66
        self.delivery_time = delivery_time
6537 rajveer 67
        self.delivery_delay = delivery_delay
3218 rajveer 68
        self.provider_id = provider_id
4866 rajveer 69
        self.codAllowed = codAllowed
6524 rajveer 70
        self.otgAvailable = otgAvailable
3218 rajveer 71
 
3187 rajveer 72
def initialize(dbname="logistics", db_hostname="localhost"):
442 rajveer 73
    log_entry("initialize@DataAccessor", "Initializing data service")
3187 rajveer 74
    DataService.initialize(dbname, db_hostname)
3218 rajveer 75
    print "Starting cache population at: " + str(datetime.datetime.now())
16025 amit.gupta 76
    #__cache_warehouse_allocation_table()
77
    __cache_warehouse_locations()
3217 rajveer 78
    __cache_serviceable_location_details_table()
79
    __cache_delivery_estimate_table()
20275 amit.gupta 80
    __cache_pincode_provider_serviceability()
19413 amit.gupta 81
    __cache_locations()
82
    __cache_pincode_estimates()    
19421 manish.sha 83
    __cache_courier_costing_table()
3217 rajveer 84
    close_session()
3218 rajveer 85
    print "Done cache population at: " + str(datetime.datetime.now())
442 rajveer 86
 
19413 amit.gupta 87
def __cache_locations():
88
    locations = Locations.query.all()
89
    for location in locations:
90
        location_state[location.id] = location.state_id
91
        if not state_locations.has_key(location.state_id):
92
            state_locations[location.state_id] = []
93
        state_locations[location.state_id] = state_locations[location.state_id].append(location.state_id)
94
 
95
def __cache_pincode_estimates():
96
    delivery_estimates = DeliveryEstimate.query.all()
97
    for delivery_estimate in delivery_estimates:
98
        if not pincode_locations.has_key(delivery_estimate.destination_pin):
99
            pincode_locations[delivery_estimate.destination_pin] = {}
100
        destination_pinMap = pincode_locations[delivery_estimate.destination_pin] 
101
        if not destination_pinMap.has_key(delivery_estimate.warehouse_location):
102
            destination_pinMap[delivery_estimate.warehouse_location] = {}
103
            state_id = location_state.get(delivery_estimate.warehouse_location)
104
            sameState = __getStateFromPin(delivery_estimate.destination_pin) == state_id
105
            destination_pinMap[delivery_estimate.warehouse_location]['sameState']=sameState
106
            destination_pinMap[delivery_estimate.warehouse_location]['providerInfo'] = {} 
107
        destination_pinMap[delivery_estimate.warehouse_location]['providerInfo'][delivery_estimate.provider_id] = delivery_estimate.delivery_time + delivery_estimate.delivery_delay
108
 
109
 
110
 
111
def __cache_pincode_provider_serviceability():
112
    pincode_providers = ServiceableLocationDetails.query.filter(ServiceableLocationDetails.exp==1)
113
    for pp in pincode_providers:
114
        serviceable_map[PincodeProvider(pp.dest_pincode, pp.provider_id)] = (pp.cod, pp.otgAvailable,pp.providerCodLimit, pp.websiteCodLimit, pp.storeCodLimit, pp.providerPrepaidLimit)
115
 
116
 
3217 rajveer 117
def __cache_delivery_estimate_table():
118
    delivery_estimates = DeliveryEstimate.query.all()
119
    for delivery_estimate in delivery_estimates:
7857 rajveer 120
        delivery_estimate_cache[delivery_estimate.destination_pin, delivery_estimate.provider_id, delivery_estimate.warehouse_location]\
19421 manish.sha 121
        =delivery_estimate.delivery_time, delivery_estimate.delivery_delay, delivery_estimate.providerZoneCode
3217 rajveer 122
 
123
def __cache_serviceable_location_details_table():
5964 amar.kumar 124
    serviceable_locations = ServiceableLocationDetails.query.filter(or_("exp!=0","cod!=0"))
3217 rajveer 125
    for serviceable_location in serviceable_locations:
126
        try:
127
            provider_pincodes = serviceable_location_cache[serviceable_location.provider_id]
128
        except:
129
            provider_pincodes = {}
130
            serviceable_location_cache[serviceable_location.provider_id] = provider_pincodes 
19474 manish.sha 131
        provider_pincodes[serviceable_location.dest_pincode] = serviceable_location.dest_code, serviceable_location.exp, serviceable_location.cod, serviceable_location.otgAvailable, serviceable_location.websiteCodLimit, serviceable_location.storeCodLimit, serviceable_location.providerPrepaidLimit, serviceable_location.providerCodLimit
3217 rajveer 132
 
133
def __cache_warehouse_allocation_table():
134
    warehouse_allocations = WarehouseAllocation.query.all()
135
    for warehouse_allocation in warehouse_allocations:
136
        warehouse_allocation_cache[warehouse_allocation.pincode]=warehouse_allocation.primary_warehouse_location
137
 
16025 amit.gupta 138
def __cache_warehouse_locations():
139
    client = InventoryClient().get_client()
19413 amit.gupta 140
    #client.getAllWarehouses(True)
16025 amit.gupta 141
    for warehouse in client.getAllWarehouses(True):
142
        warehouse_location_cache[warehouse.billingWarehouseId]=warehouse.logisticsLocation
19421 manish.sha 143
 
144
def __cache_courier_costing_table():
145
    allCostings = ProviderCosting.query.all()
146
    for costing in allCostings:
147
        if provider_costing_sheet.has_key(costing.provider_id):
148
            costingMap = provider_costing_sheet.get(costing.provider_id)
149
            costingMap[costing.zoneCode] = costing
150
            provider_costing_sheet[costing.provider_id] = costingMap
151
        else:
152
            costingMap = {}
153
            costingMap[costing.zoneCode] = costing
154
            provider_costing_sheet[costing.provider_id] = costingMap
155
 
16025 amit.gupta 156
 
644 chandransh 157
def get_provider(provider_id):
766 rajveer 158
    provider =  Provider.get_by(id=provider_id)
159
    return provider
644 chandransh 160
 
161
def get_providers():
5387 rajveer 162
    providers = Provider.query.filter_by(isActive = 1).all()
19413 amit.gupta 163
    return providers
644 chandransh 164
 
19413 amit.gupta 165
#it would return only states where our warehouses are present else return -1
166
def __getStateFromPin(pin):
167
    if pin[:3] in ['744', '682']:
168
        return -1
169
    elif statepinmap.has_key(pin[:2]):
170
        return statepinmap[pin[:2]]
171
    else:
172
        return -1
173
 
174
 
175
def get_logistics_locations(destination_pin, selling_price_list):
176
    #pincode_locations = {1:{"sameState":False, "providerInfo":{<provider_id>:<delay_days>}},}
177
    #pp.otgAvailable,pp.providerCodLimit, pp.websiteCodLimit, pp.storeCodLimit, pp.providerPrepaidLimit
178
    returnMap = {}
179
    if pincode_locations.has_key(destination_pin):
180
        locations = pincode_locations[destination_pin]
181
        for item_selling_price in selling_price_list:
182
            returnMap[item_selling_price] = {}
183
            for location_id, value in locations.iteritems():
184
                #put weight logic here
185
                otgAvailable = False
186
                codAvailable = False
187
                minDelay = -1
188
                maxDelay = -1
189
                serviceable = False
190
                for provider_id, delay_days in value['providerInfo'].iteritems():
19420 amit.gupta 191
                    pp = PincodeProvider(destination_pin, provider_id)
192
                    if not serviceable_map.has_key(pp):
193
                        continue
194
                    iscod, isotg, providerCodLimit, websiteCodLimit, storeCodLimit, providerPrepaidLimit = serviceable_map[pp]  
19413 amit.gupta 195
                    if item_selling_price <= providerPrepaidLimit:
196
                        if not serviceable:
20275 amit.gupta 197
                            returnMap[item_selling_price][location_id] = LocationInfo(locationId = location_id, sameState=value['sameState'])
19413 amit.gupta 198
                            serviceable = True
199
                            minDelay = delay_days
200
                            maxDelay = delay_days
201
                        else:
202
                            minDelay = min(delay_days,minDelay)
203
                            maxDelay = max(delay_days,maxDelay)
204
                        iscod = iscod and item_selling_price <= min(providerCodLimit, websiteCodLimit)
205
                        isotg = (isotg and item_selling_price >= 2000)
206
                        otgAvailable = otgAvailable or isotg
207
                        codAvailable = codAvailable or iscod
208
                if serviceable:        
20275 amit.gupta 209
                    returnMap[item_selling_price][location_id].isOtg = otgAvailable
210
                    returnMap[item_selling_price][location_id].isCod = codAvailable
19413 amit.gupta 211
                    returnMap[item_selling_price][location_id].minDelay = minDelay
212
                    returnMap[item_selling_price][location_id].maxDelay = maxDelay
213
    return returnMap
214
 
7608 rajveer 215
def get_logistics_estimation(destination_pin, item_selling_price, weight, type, billingWarehouseId):
5692 rajveer 216
    logging.info("Getting logistics estimation for pincode:" + destination_pin )
1504 ankur.sing 217
 
7608 rajveer 218
    provider_id, codAllowed, otgAvailable = __get_logistics_provider_for_destination_pincode(destination_pin, item_selling_price, weight, type, billingWarehouseId)
9964 anupam.sin 219
 
17530 manish.sha 220
    logging.info("Provider Id:  " + str(provider_id) +" codAllowed: "+str(codAllowed)+" otgAvailable: "+str(otgAvailable)+ " item_selling_price: "+str(item_selling_price))
221
 
222
    if item_selling_price > 60000:# or item_selling_price <= 250:
9964 anupam.sin 223
        codAllowed = False
224
 
16025 amit.gupta 225
    warehouse_location = warehouse_location_cache.get(billingWarehouseId)
226
    if warehouse_location is None:
227
        warehouse_location = 0
7857 rajveer 228
 
3217 rajveer 229
    if not provider_id:
5692 rajveer 230
        raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin))
644 chandransh 231
    try:
16025 amit.gupta 232
        logging.info( "destination_pin %s, provider_id %s, warehouse_location %s"%(destination_pin, provider_id, warehouse_location))
19481 manish.sha 233
        logging.info( "Estimates %s, %s, %s"%(delivery_estimate_cache[destination_pin, provider_id, warehouse_location]))
7857 rajveer 234
        delivery_time = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][0]
235
        delivery_delay = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][1]
6537 rajveer 236
        delivery_estimate = _DeliveryEstimateObject(delivery_time, delivery_delay, provider_id, codAllowed, otgAvailable)
3218 rajveer 237
        return delivery_estimate
1504 ankur.sing 238
    except Exception as ex:
239
        print ex
644 chandransh 240
        raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
3150 rajveer 241
 
7608 rajveer 242
def __get_logistics_provider_for_destination_pincode(destination_pin, item_selling_price, weight, type, billingWarehouseId):
20641 amit.gupta 243
    #As of now we are dealing with Aramex and Bluedart. i.e. 1 and 2 and we have just one location i.e. gurgaon will generailise this 
244
    #Once bandwidth is available these things will be derived through improved logic.
245
    #As of now otg is set to False
246
    otg=False
247
    prepaidProvider=None
248
    if serviceable_location_cache.has_key(2) and serviceable_location_cache.get(2).has_key(destination_pin):
249
        dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit, providerCodLimit = serviceable_location_cache.get(2).get(destination_pin)
250
        if item_selling_price <= providerPrepaidLimit:
251
            iscod = iscod and item_selling_price <= websiteCodLimit
252
            if iscod:
253
                return 2, True, otg
254
        prepaidProvider=2
255
    if serviceable_location_cache.has_key(1) and serviceable_location_cache.get(1).has_key(destination_pin):
256
        dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit, providerCodLimit = serviceable_location_cache.get(1).get(destination_pin)
257
        if item_selling_price <= providerPrepaidLimit:
258
            iscod = iscod and item_selling_price <= websiteCodLimit
259
            if iscod:
260
                return 1, True, otg
261
        if prepaidProvider is None:
262
            prepaidProvider=1
263
 
264
    if prepaidProvider:
265
        return prepaidProvider, False, otg 
266
 
267
    return None, False, False
268
 
269
def __get_logistics_provider_for_destination_pincode_old(destination_pin, item_selling_price, weight, type, billingWarehouseId):
13357 manish.sha 270
    '''
271
    if serviceable_location_cache.get(6).has_key(destination_pin):
272
        if weight < 480 and serviceable_location_cache.get(3).has_key(destination_pin) and type == DeliveryType.PREPAID:
273
            dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit = serviceable_location_cache.get(3).get(destination_pin)
274
            if item_selling_price <= providerPrepaidLimit:
275
                iscod = iscod and item_selling_price <= websiteCodLimit
276
                otgAvailable = otgAvailable and item_selling_price >= 2000
277
                return 3, iscod, otgAvailable
278
        else:
279
            dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit = serviceable_location_cache.get(6).get(destination_pin)
280
            if item_selling_price <= providerPrepaidLimit:
281
                iscod = iscod and item_selling_price <= websiteCodLimit
282
                otgAvailable = otgAvailable and item_selling_price >= 2000
283
                return 6, iscod, otgAvailable
20641 amit.gupta 284
    '''        
285
    if serviceable_location_cache.has_key(3) and serviceable_location_cache.get(3).has_key(destination_pin):
286
        dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit, providerCodLimit = serviceable_location_cache.get(3).get(destination_pin)
287
        if item_selling_price <= providerPrepaidLimit:
288
            iscod = iscod and item_selling_price <= websiteCodLimit
289
            otgAvailable = otgAvailable and item_selling_price >= 2000
290
            return 3, iscod, otgAvailable
8575 rajveer 291
 
17992 manish.sha 292
    if billingWarehouseId not in [12,13] and serviceable_location_cache.has_key(7) and serviceable_location_cache.get(7).has_key(destination_pin):
19533 manish.sha 293
        if item_selling_price < 3000 and serviceable_location_cache.has_key(1) and serviceable_location_cache.get(1).has_key(destination_pin):
19476 manish.sha 294
            dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit, providerCodLimit = serviceable_location_cache.get(1).get(destination_pin)
8575 rajveer 295
            if item_selling_price <= providerPrepaidLimit:
296
                iscod = iscod and item_selling_price <= websiteCodLimit
297
                otgAvailable = otgAvailable and item_selling_price >= 2000
20275 amit.gupta 298
                if iscod:
299
                    return 1, iscod, otgAvailable
8575 rajveer 300
        else:
19476 manish.sha 301
            dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit, providerCodLimit = serviceable_location_cache.get(7).get(destination_pin)
8575 rajveer 302
            if item_selling_price <= providerPrepaidLimit:
303
                iscod = iscod and item_selling_price <= websiteCodLimit
304
                otgAvailable = otgAvailable and item_selling_price >= 2000
20275 amit.gupta 305
                if iscod:
306
                    return 7, iscod, otgAvailable
19533 manish.sha 307
 
308
    if billingWarehouseId not in [12,13] and serviceable_location_cache.has_key(46) and serviceable_location_cache.get(46).has_key(destination_pin):
309
        if item_selling_price < 3000 and serviceable_location_cache.has_key(1) and serviceable_location_cache.get(1).has_key(destination_pin):
310
            dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit, providerCodLimit = serviceable_location_cache.get(1).get(destination_pin)
311
            if item_selling_price <= providerPrepaidLimit:
312
                iscod = iscod and item_selling_price <= websiteCodLimit
313
                otgAvailable = otgAvailable and item_selling_price >= 2000
20275 amit.gupta 314
                if iscod:
315
                    return 1, iscod, otgAvailable
19533 manish.sha 316
        else:
317
            dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit, providerCodLimit = serviceable_location_cache.get(46).get(destination_pin)
318
            if item_selling_price <= providerPrepaidLimit:
319
                iscod = iscod and item_selling_price <= websiteCodLimit
320
                otgAvailable = otgAvailable and item_selling_price >= 2000
20275 amit.gupta 321
                if iscod:
322
                    return 46, iscod, otgAvailable
8575 rajveer 323
 
19533 manish.sha 324
    if serviceable_location_cache.has_key(1) and serviceable_location_cache.get(1).has_key(destination_pin):
19476 manish.sha 325
        dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit, providerCodLimit = serviceable_location_cache.get(1).get(destination_pin)
7627 rajveer 326
        if item_selling_price <= providerPrepaidLimit:
7608 rajveer 327
            iscod = iscod and item_selling_price <= websiteCodLimit
328
            otgAvailable = otgAvailable and item_selling_price >= 2000
20275 amit.gupta 329
            if iscod:
330
                return 1, iscod, otgAvailable
7981 rajveer 331
 
20275 amit.gupta 332
    if serviceable_location_cache.has_key(3) and serviceable_location_cache.get(3).has_key(destination_pin):
333
        dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit, providerCodLimit = serviceable_location_cache.get(3).get(destination_pin)
334
        if item_selling_price <= providerPrepaidLimit:
335
            iscod = iscod and item_selling_price <= websiteCodLimit
336
            otgAvailable = otgAvailable and item_selling_price >= 2000
337
            return 3, iscod, otgAvailable
10185 amit.gupta 338
    return None, False, False    
5278 rajveer 339
 
6017 amar.kumar 340
def get_destination_code(providerId, pinCode):
341
    serviceableLocationDetail = ServiceableLocationDetails.query.filter_by(provider_id = providerId, dest_pincode = pinCode).one()
342
    return serviceableLocationDetail.dest_code
343
 
644 chandransh 344
def add_empty_AWBs(numbers, provider_id, type):
442 rajveer 345
    for number in numbers:
644 chandransh 346
        query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
444 rajveer 347
        try:
348
            query.one()
349
        except:    
644 chandransh 350
            awb = Awb()
444 rajveer 351
            awb.awb_number = number
644 chandransh 352
            awb.provider_id = provider_id
444 rajveer 353
            awb.is_available = True
644 chandransh 354
            awb.type = type
355
    session.commit()
442 rajveer 356
 
444 rajveer 357
def set_AWB_as_used(awb_number):
644 chandransh 358
    query = Awb.query.filter_by(awb_number = awb_number)
444 rajveer 359
    awb = query.one() 
360
    awb.is_available=False
361
    session.commit()
362
 
20724 kshitij.so 363
#def get_empty_AWB(provider_id, logisticsTransactionId):
364
#    query = Awb.query.with_lockmode("update").filter_by(provider_id = provider_id, is_available = True)  #check the provider thing
365
#    if type == DeliveryType.PREPAID:
366
#        query = query.filter_by(type="Prepaid")
367
#    if type == DeliveryType.COD:
368
#        query = query.filter_by(type="COD")
369
#    
370
#    additionalQuery = query
371
#    query = query.filter_by(awbUsedFor=0)
372
#    
373
#    try:
374
#        awb = query.first()
375
#        if awb is None:
376
#            additionalQuery = additionalQuery.filter_by(awbUsedFor=2)
377
#            awb = additionalQuery.first()
378
#        awb.is_available = False
379
#        session.commit()
380
#        return awb.awb_number
381
#    except:
382
#        raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(provider_id))
383
 
384
def get_empty_AWB(provider_id, logisticsTransactionId):
442 rajveer 385
    try:
20724 kshitij.so 386
        if logisticsTransactionId is None:
387
            return ""
388
        if provider_id ==1:
389
            client = TransactionClient().get_client()
390
            orders_list = client.getGroupOrdersByLogisticsTxnId(logisticsTransactionId)
391
            return BluedartService.generate_awb(orders_list)
442 rajveer 392
    except:
20724 kshitij.so 393
        traceback.print_exc()
644 chandransh 394
        raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(provider_id))
20724 kshitij.so 395
 
1137 chandransh 396
 
3103 chandransh 397
def get_free_awb_count(provider_id, type):
398
    count = Awb.query.filter_by(provider_id = provider_id, is_available = True, type=type).count()
1137 chandransh 399
    if count == None:
400
        count = 0
401
    return count
442 rajveer 402
 
644 chandransh 403
def get_shipment_info(awb_number, provider_id):
404
    awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
405
    query = AwbUpdate.query.filter_by(awb = awb)
766 rajveer 406
    info = query.all()
407
    return info
408
 
6643 rajveer 409
def store_shipment_info(update):
410
    updates = AwbUpdate.query.filter_by(providerId = update.providerId, awbNumber  = update.awbNumber, location = update.location, date = to_py_date(update.date), status = update.status, description = update.description).all()
411
    if not updates:
412
        dupdate = AwbUpdate()
413
        dupdate.providerId = update.providerId
414
        dupdate.awbNumber  = update.awbNumber
415
        dupdate.location = update.location
416
        dupdate.date = to_py_date(update.date)
417
        dupdate.status = update.status
418
        dupdate.description = update.description
419
        session.commit()
420
 
1730 ankur.sing 421
def get_holidays(start_date, end_date):
422
    query = PublicHolidays.query
423
    if start_date != -1:
424
        query = query.filter(PublicHolidays.date >= to_py_date(start_date))
425
    if end_date != -1:
426
        query = query.filter(PublicHolidays.date <= to_py_date(end_date))
427
    holidays = query.all()
428
    holiday_dates = [to_java_date(datetime.datetime(*time.strptime(str(holiday.date), '%Y-%m-%d')[:3])) for holiday in holidays] 
429
    return holiday_dates
430
 
5527 anupam.sin 431
def get_provider_for_pickup_type(pickUp):
432
    return Provider.query.filter(Provider.pickup == pickUp).first().id
433
 
766 rajveer 434
def close_session():
435
    if session.is_active:
436
        print "session is active. closing it."
2823 chandransh 437
        session.close()
3376 rajveer 438
 
439
def is_alive():
440
    try:
441
        session.query(Awb.id).limit(1).one()
442
        return True
443
    except:
5555 rajveer 444
        return False
445
 
446
def get_all_pickup_stores():
5572 anupam.sin 447
    pickupStores = PickupStore.query.all()
448
    return pickupStores
5555 rajveer 449
 
450
def get_pickup_store(storeId):
5719 rajveer 451
    return PickupStore.query.filter_by(id = storeId).one()
452
 
453
def get_pickup_store_by_hotspot_id(hotspotId):
454
    return PickupStore.query.filter_by(hotspotId = hotspotId).one()
6322 amar.kumar 455
 
6524 rajveer 456
def add_pincode(provider, pincode, destCode, exp, cod, stationType, otgAvailable):
7914 rajveer 457
    provider = Provider.query.filter_by(id = provider).one()
6322 amar.kumar 458
    serviceableLocationDetails = ServiceableLocationDetails()
459
    serviceableLocationDetails.provider = provider
460
    serviceableLocationDetails.dest_pincode = pincode
461
    serviceableLocationDetails.dest_code = destCode
462
    serviceableLocationDetails.exp = exp
463
    serviceableLocationDetails.cod = cod
464
    serviceableLocationDetails.station_type = stationType
6524 rajveer 465
    serviceableLocationDetails.otgAvailable = otgAvailable
7733 manish.sha 466
    if provider.id == 1:
7627 rajveer 467
        codlimit = 10000
468
        prepaidlimit = 50000
7733 manish.sha 469
    elif provider.id == 3:
7627 rajveer 470
        codlimit = 25000
471
        prepaidlimit = 100000
7733 manish.sha 472
    elif provider.id == 6:
7627 rajveer 473
        codlimit = 25000
474
        prepaidlimit = 100000
475
    serviceableLocationDetails.providerPrepaidLimit = prepaidlimit
476
    serviceableLocationDetails.providerCodLimit = codlimit
477
    serviceableLocationDetails.websiteCodLimit = codlimit
478
    serviceableLocationDetails.storeCodLimit = codlimit
6322 amar.kumar 479
    session.commit()
480
 
6524 rajveer 481
def update_pincode(providerId, pincode, exp, cod, otgAvailable):
6615 amar.kumar 482
    serviceableLocationDetails = ServiceableLocationDetails.get_by(provider_id = providerId, dest_pincode = pincode)
6322 amar.kumar 483
    serviceableLocationDetails.exp = exp
484
    serviceableLocationDetails.cod = cod
6524 rajveer 485
    serviceableLocationDetails.otgAvailable = otgAvailable
7256 rajveer 486
    session.commit()
7567 rajveer 487
 
13146 manish.sha 488
def add_new_awbs(provider_id, isCod, awbs, awbUsedFor):
7567 rajveer 489
    provider = get_provider(provider_id)
490
    if isCod:
491
        dtype = 'Cod'
492
    else:
493
        dtype = 'Prepaid'
494
    for awb in awbs:
495
        a = Awb()
496
        a.type =  dtype
497
        a.is_available = True
498
        a.awb_number = awb
13146 manish.sha 499
        a.awbUsedFor = awbUsedFor
7567 rajveer 500
        a.provider = provider 
501
    session.commit()
7608 rajveer 502
    return True
7256 rajveer 503
 
504
def adjust_delivery_time(start_time, delivery_days):
505
    '''
506
    Returns the actual no. of days which will pass while 'delivery_days'
507
    no. of business days will pass since 'order_time'. 
508
    '''
509
    start_date = start_time.date()
510
    end_date = start_date + datetime.timedelta(days = delivery_days)
7272 amit.gupta 511
    holidays = get_holidays(to_java_date(start_time), -1)
7256 rajveer 512
    holidays = [to_py_date(holiday).date() for holiday in holidays]
513
 
514
    while start_date <= end_date:
515
        if start_date.weekday() == 6 or start_date in holidays:
516
            delivery_days = delivery_days + 1
517
            end_date = end_date + datetime.timedelta(days = 1)
518
        start_date = start_date + datetime.timedelta(days = 1)
519
 
520
    return delivery_days
7275 rajveer 521
 
522
def get_min_advance_amount(itemId, destination_pin, providerId, codAllowed):
523
    client = CatalogClient().get_client()
524
    sp = client.getStorePricing(itemId)
525
 
526
    if codAllowed:
527
        return sp.minAdvancePrice, True
528
    else:
20275 amit.gupta 529
        dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit, providerCodLimit  = serviceable_location_cache.get(providerId).get(destination_pin)
7275 rajveer 530
        if iscod:
531
            if providerId == 6:
7489 rajveer 532
                return max(sp.minAdvancePrice, sp.recommendedPrice - storeCodLimit), True
7275 rajveer 533
            if providerId == 3:
7489 rajveer 534
                return max(sp.minAdvancePrice, sp.recommendedPrice - storeCodLimit), True
7275 rajveer 535
            if providerId == 1:
7489 rajveer 536
                return max(sp.minAdvancePrice, sp.recommendedPrice - storeCodLimit), True
7275 rajveer 537
        else:
7425 rajveer 538
            return sp.recommendedPrice, False
7733 manish.sha 539
#Start:- Added by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
540
 
7786 manish.sha 541
def run_Logistics_Location_Info_Update(logisticsLocationInfoList, runCompleteUpdate):
542
    if runCompleteUpdate == True :
543
        serviceableLocationDetails = ServiceableLocationDetails.query.all()
544
        for serviceableLocationDetail in serviceableLocationDetails:
7841 manish.sha 545
            serviceableLocationDetail.exp = False
546
            serviceableLocationDetail.cod = False
7786 manish.sha 547
    for logisticsLocationInfo in logisticsLocationInfoList:
7841 manish.sha 548
        serviceableLocationDetail = ServiceableLocationDetails.get_by(provider_id = logisticsLocationInfo.providerId, dest_pincode = logisticsLocationInfo.pinCode)
7914 rajveer 549
        provider = Provider.query.filter_by(id = logisticsLocationInfo.providerId).one()
7841 manish.sha 550
        if serviceableLocationDetail:
551
            serviceableLocationDetail.exp = logisticsLocationInfo.expAvailable
552
            serviceableLocationDetail.cod = logisticsLocationInfo.codAvailable
553
            serviceableLocationDetail.otgAvailable = logisticsLocationInfo.otgAvailable
554
            serviceableLocationDetail.providerCodLimit = logisticsLocationInfo.codLimit
555
            serviceableLocationDetail.providerPrepaidLimit = logisticsLocationInfo.prepaidLimit
556
            serviceableLocationDetail.storeCodLimit = logisticsLocationInfo.codLimit
8219 manish.sha 557
            serviceableLocationDetail.websiteCodLimit = min(26000,logisticsLocationInfo.codLimit)
7841 manish.sha 558
        else:
559
            serviceableLocationDetail= ServiceableLocationDetails()
560
            serviceableLocationDetail.provider = provider
561
            serviceableLocationDetail.dest_pincode = logisticsLocationInfo.pinCode
562
            serviceableLocationDetail.dest_code = logisticsLocationInfo.destinationCode
563
            serviceableLocationDetail.exp = logisticsLocationInfo.expAvailable
564
            serviceableLocationDetail.cod = logisticsLocationInfo.codAvailable
565
            serviceableLocationDetail.station_type = 0
566
            serviceableLocationDetail.otgAvailable = logisticsLocationInfo.otgAvailable
567
            serviceableLocationDetail.providerCodLimit = logisticsLocationInfo.codLimit
568
            serviceableLocationDetail.providerPrepaidLimit = logisticsLocationInfo.prepaidLimit
569
            serviceableLocationDetail.storeCodLimit = logisticsLocationInfo.codLimit
8219 manish.sha 570
            serviceableLocationDetail.websiteCodLimit = min(26000,logisticsLocationInfo.codLimit)
7841 manish.sha 571
 
7870 rajveer 572
        deliveryEstimate = DeliveryEstimate.get_by(provider_id = logisticsLocationInfo.providerId, destination_pin = logisticsLocationInfo.pinCode, warehouse_location = logisticsLocationInfo.warehouseId) 
7841 manish.sha 573
        if deliveryEstimate:
574
            deliveryEstimate.warehouse_location= logisticsLocationInfo.warehouseId
575
            deliveryEstimate.delivery_time= logisticsLocationInfo.deliveryTime
576
            deliveryEstimate.delivery_delay= logisticsLocationInfo.delivery_delay
19421 manish.sha 577
            deliveryEstimate.providerZoneCode = logisticsLocationInfo.zoneCode
7841 manish.sha 578
        else:
579
            deliveryEstimate = DeliveryEstimate()
580
            deliveryEstimate.destination_pin= logisticsLocationInfo.pinCode
581
            deliveryEstimate.provider = provider
582
            deliveryEstimate.warehouse_location= logisticsLocationInfo.warehouseId
583
            deliveryEstimate.delivery_time= logisticsLocationInfo.deliveryTime
584
            deliveryEstimate.delivery_delay= logisticsLocationInfo.delivery_delay
19421 manish.sha 585
            deliveryEstimate.providerZoneCode = logisticsLocationInfo.zoneCode
7733 manish.sha 586
    session.commit()
7786 manish.sha 587
 
7733 manish.sha 588
#End:- Added by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013             
7275 rajveer 589
 
12895 manish.sha 590
def get_first_delivery_estimate_for_wh_location(pincode, whLocation):
591
    delEstimate = DeliveryEstimate.query.filter(DeliveryEstimate.destination_pin == pincode).filter(DeliveryEstimate.warehouse_location == whLocation).first()
592
    if delEstimate is None:
593
        return -1
594
    else:
595
        return 1
13146 manish.sha 596
 
597
def get_provider_limit_details_for_pincode(provider, pincode):
598
    serviceAbleDetails = ServiceableLocationDetails.get_by(provider_id = provider, dest_pincode = pincode)
599
    returnDataMap = {}
600
    if serviceAbleDetails:
601
        returnDataMap["websiteCodLimit"] = str(serviceAbleDetails.websiteCodLimit)
19230 manish.sha 602
        returnDataMap["providerCodLimit"] = str(serviceAbleDetails.providerCodLimit)
13146 manish.sha 603
        returnDataMap["providerPrepaidLimit"] = str(serviceAbleDetails.providerPrepaidLimit)
604
 
605
    return returnDataMap
606
 
607
def get_new_empty_awb(providerId, type, orderQuantity):
608
    query = Awb.query.with_lockmode("update").filter_by(provider_id = providerId, is_available = True)  #check the provider thing
609
    if type == DeliveryType.PREPAID:
610
        query = query.filter_by(type="Prepaid")
611
    if type == DeliveryType.COD:
612
        query = query.filter_by(type="COD")
613
 
614
    additionalQuery = query
615
    if orderQuantity == 1:
616
        query = query.filter_by(awbUsedFor=0)
617
    if orderQuantity >1:
618
        query = query.filter_by(awbUsedFor=1) 
619
 
620
    try:
621
        awb = query.first()
622
        if awb is None:
623
            additionalQuery = additionalQuery.filter_by(awbUsedFor=2)
624
            awb = additionalQuery.first()
625
        awb.is_available = False
626
        session.commit()
627
        return awb.awb_number
628
    except:
629
        raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(providerId))
19421 manish.sha 630
 
19474 manish.sha 631
def get_costing_and_delivery_estimate_for_pincode(pincode, transactionAmount, isCod, weight, billingWarehouseId, isCompleteTxn):
632
    print pincode, transactionAmount, isCod, weight, billingWarehouseId
19421 manish.sha 633
    deliveryEstimate = {}
634
    logsiticsCosting = {}
635
    providerCostingMap = {}
636
    serviceability = {}
637
 
638
    for providerId in serviceable_location_cache.keys():
639
        if serviceable_location_cache.has_key(providerId) and serviceable_location_cache.get(providerId).has_key(pincode):
19474 manish.sha 640
            dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit, providerCodLimit = serviceable_location_cache.get(providerId).get(pincode)
19421 manish.sha 641
            if isCod and iscod:
19474 manish.sha 642
                if not isCompleteTxn:
643
                    if transactionAmount <= providerCodLimit:
644
                        serviceability[providerId] = serviceable_location_cache.get(providerId).get(pincode)
645
                    else:
646
                        continue
647
                else:
648
                    serviceability[providerId] = serviceable_location_cache.get(providerId).get(pincode)
19421 manish.sha 649
            elif isCod and not iscod:
650
                continue
651
            else:
19474 manish.sha 652
                if not isCompleteTxn:
653
                    if transactionAmount <= providerPrepaidLimit:
654
                        serviceability[providerId] = serviceable_location_cache.get(providerId).get(pincode)
655
                    else:
656
                        continue
657
                else:
658
                    serviceability[providerId] = serviceable_location_cache.get(providerId).get(pincode)
19421 manish.sha 659
 
660
    warehouse_location = warehouse_location_cache.get(billingWarehouseId)
661
    if warehouse_location is None:
662
        warehouse_location = 0
663
 
664
    noCostFoundCount = 0
665
    if len(serviceability)>0:
666
        for providerId, serviceableDetails in serviceability.items():
667
            delivery_time = delivery_estimate_cache[pincode, providerId, warehouse_location][0]
668
            delivery_delay = delivery_estimate_cache[pincode, providerId, warehouse_location][1]
669
            zoneCode = delivery_estimate_cache[pincode, providerId, warehouse_location][2]
670
            sla = delivery_time + delivery_delay
671
            if deliveryEstimate.has_key(sla):
672
                estimate = deliveryEstimate.get(sla)
673
                estimate.append(providerId)
674
                deliveryEstimate[sla] = estimate
675
            else:
676
                estimate = []
677
                estimate.append(providerId)
678
                deliveryEstimate[sla] = estimate
679
 
680
            logisticsCost = 0
681
            codCollectionCharges = 0    
682
            logisticsCostObj = None 
683
            if provider_costing_sheet.has_key(providerId) and provider_costing_sheet.get(providerId).has_key(zoneCode):
684
                logisticsCostObj = provider_costing_sheet.get(providerId).get(zoneCode)
685
                logisticsCost = logisticsCostObj.initialLogisticsCost
686
                if weight > logisticsCostObj.initialWeightUpperLimit:
687
                    additionalWeight = weight-logisticsCostObj.initialWeightUpperLimit
19663 manish.sha 688
                    additionalWeight = round(additionalWeight,0)
19483 manish.sha 689
                    additionalCostMultiplier = math.ceil(additionalWeight/logisticsCostObj.additionalUnitWeight)
19421 manish.sha 690
                    additionalCost = additionalCostMultiplier*logisticsCostObj.additionalUnitLogisticsCost
691
                    logisticsCost = logisticsCost + additionalCost
692
                if isCod:
693
                    if logisticsCostObj.codCollectionFactor:
19474 manish.sha 694
                        codCollectionCharges = max(logisticsCostObj.minimumCodCollectionCharges, (transactionAmount*logisticsCostObj.codCollectionFactor)/100)
695
                        logisticsCost = logisticsCost + max(logisticsCostObj.minimumCodCollectionCharges, (transactionAmount*logisticsCostObj.codCollectionFactor)/100)
19421 manish.sha 696
                    else:
697
                        codCollectionCharges = logisticsCostObj.minimumCodCollectionCharges
698
                        logisticsCost = logisticsCost + logisticsCostObj.minimumCodCollectionCharges
699
 
19474 manish.sha 700
                if logsiticsCosting.has_key(round(logisticsCost,0)):
701
                    costingList = logsiticsCosting.get(round(logisticsCost,0))
19421 manish.sha 702
                    costingList.append(providerId)
19474 manish.sha 703
                    logsiticsCosting[round(logisticsCost,0)] = costingList
19421 manish.sha 704
                else:
705
                    costingList = []
706
                    costingList.append(providerId)
19474 manish.sha 707
                    logsiticsCosting[round(logisticsCost,0)] = costingList                    
19421 manish.sha 708
 
709
                providerCostingMap[providerId] = [logisticsCost,codCollectionCharges]
710
            else:
711
                noCostFoundCount = noCostFoundCount +1
712
                continue
713
    else:
19474 manish.sha 714
        print "No provider assigned for pincode: " + str(pincode)
19535 manish.sha 715
        raise LogisticsServiceException(103, "Unable to get Provider for given pincode: " + str(pincode)+" Amount:- "+str(transactionAmount)+" Weight:- "+ str(weight))
19421 manish.sha 716
 
717
 
718
    if noCostFoundCount == len(serviceability):
19474 manish.sha 719
        print "No Costing Found for this pincode: " + str(pincode) +" for any of the provider"
19535 manish.sha 720
        raise LogisticsServiceException(103, "Unable to get Provider for given pincode: " + str(pincode)+" Amount:- "+str(transactionAmount)+" Weight:- "+ str(weight)+". Due to no costing found")
19421 manish.sha 721
 
722
    allSla = sorted(deliveryEstimate.keys())
723
    if len(allSla)==1:
724
        allEligibleProviders = deliveryEstimate.get(allSla[0])
725
        if len(allEligibleProviders)==1:
19474 manish.sha 726
            try:
19421 manish.sha 727
                costingDetails = providerCostingMap.get(allEligibleProviders[0])
728
                delEstCostObj = DeliveryEstimateAndCosting()
729
                delEstCostObj.logistics_provider_id = allEligibleProviders[0]
730
                delEstCostObj.pincode = pincode
731
                delEstCostObj.deliveryTime = delivery_estimate_cache[pincode, allEligibleProviders[0], warehouse_location][0]
732
                delEstCostObj.delivery_delay = delivery_estimate_cache[pincode, allEligibleProviders[0], warehouse_location][1]
733
                delEstCostObj.codAllowed = serviceability[allEligibleProviders[0]][2]
734
                delEstCostObj.otgAvailable = serviceability[allEligibleProviders[0]][3]
735
                delEstCostObj.logisticsCost = costingDetails[0]-costingDetails[1]
736
                delEstCostObj.codCollectionCharges = costingDetails[1]
737
                return delEstCostObj
19474 manish.sha 738
            except:
19535 manish.sha 739
                raise LogisticsServiceException(103, "Unable to get Provider for given pincode: " + str(pincode)+" Amount:- "+str(transactionAmount)+" Weight:- "+ str(weight)+".")
19421 manish.sha 740
        else:
741
            costingListOrder = []
742
            for providerId in allEligibleProviders:
19474 manish.sha 743
                costingDetails = providerCostingMap.get(providerId)
744
                if costingDetails and costingDetails[0] not in costingListOrder:
745
                    costingListOrder.append(round(costingDetails[0],0))
746
 
747
            costingListOrder = sorted(costingListOrder)
748
            eligibleProviders = logsiticsCosting.get(costingListOrder[0])
749
            for providerId in eligibleProviders:
750
                if providerCostingMap.has_key(providerId):
751
                    costingDetails = providerCostingMap.get(providerId)
19421 manish.sha 752
                    delEstCostObj = DeliveryEstimateAndCosting()
19474 manish.sha 753
                    delEstCostObj.logistics_provider_id = providerId
19421 manish.sha 754
                    delEstCostObj.pincode = pincode
19474 manish.sha 755
                    delEstCostObj.deliveryTime = delivery_estimate_cache[pincode, providerId, warehouse_location][0]
756
                    delEstCostObj.delivery_delay = delivery_estimate_cache[pincode, providerId, warehouse_location][1]
757
                    delEstCostObj.codAllowed = serviceability[providerId][2]
758
                    delEstCostObj.otgAvailable = serviceability[providerId][3]
19421 manish.sha 759
                    delEstCostObj.logisticsCost = costingDetails[0]-costingDetails[1]
760
                    delEstCostObj.codCollectionCharges = costingDetails[1]
761
                    return delEstCostObj
19474 manish.sha 762
                else:
763
                    continue
19535 manish.sha 764
            raise LogisticsServiceException(103, "Unable to get Provider for given pincode: " + str(pincode)+" Amount:- "+str(transactionAmount)+" Weight:- "+ str(weight)+".")
19474 manish.sha 765
    else:
766
        allEligibleProviders = []
767
        virtualDelayCostMap = {}
768
        virtualCommercialsCostMap = {} 
769
        for sla in allSla:
770
            if sla-allSla[0]<=1:
771
                consideredProviders = deliveryEstimate.get(sla)
772
                for providerId in consideredProviders:
773
                    if isCod and providerId in [7,46]:
774
                        virtualCommercialsCostMap[providerId] = 10
775
                    if providerId not in allEligibleProviders:
776
                        allEligibleProviders.append(providerId)
777
                    virtualDelayCostMap[providerId] = 0
778
            elif sla-allSla[0]==2:
779
                consideredProviders = deliveryEstimate.get(sla)
780
                for providerId in consideredProviders:
781
                    if isCod and providerId in [7,46]:
782
                        virtualCommercialsCostMap[providerId] = 10
783
                    if providerId not in allEligibleProviders:
784
                        allEligibleProviders.append(providerId)
785
                    virtualDelayCostMap[providerId] = 20+round((0.3*transactionAmount)/100,0)
786
            elif sla-allSla[0]==3:
787
                consideredProviders = deliveryEstimate.get(sla)
788
                for providerId in consideredProviders:
789
                    if isCod and providerId in [7,46]:
790
                        virtualCommercialsCostMap[providerId] = 10
791
                    if providerId not in allEligibleProviders:
792
                        allEligibleProviders.append(providerId)
793
                    virtualDelayCostMap[providerId] = 40+round((0.6*transactionAmount)/100,0)
794
 
795
        costingListOrder = []
796
        costingListMap = {}
797
        for providerId in allEligibleProviders:
798
            costingDetails = providerCostingMap.get(providerId)
799
            if costingDetails:
800
                cost = round(costingDetails[0],0)
801
                if virtualDelayCostMap.has_key(providerId):
802
                    cost = cost + virtualDelayCostMap.get(providerId)
803
                if virtualCommercialsCostMap.has_key(providerId):
804
                    cost = cost + virtualCommercialsCostMap.get(providerId)
805
                if costingListMap.has_key(cost):
806
                    providers = costingListMap.get(cost)
807
                    providers.append(providerId)                       
808
                    costingListMap[cost] = providers 
809
                else:
810
                    providers = []
811
                    providers.append(providerId)                       
812
                    costingListMap[cost] = providers  
813
                if cost not in costingListOrder:
814
                    costingListOrder.append(cost)
815
 
816
        costingListOrder = sorted(costingListOrder)
817
 
818
        print costingListMap
819
        eligibleProviders = costingListMap.get(costingListOrder[0])
820
        for providerId in eligibleProviders:
821
            if providerCostingMap.has_key(providerId):
822
                costingDetails = providerCostingMap.get(providerId)
19421 manish.sha 823
                delEstCostObj = DeliveryEstimateAndCosting()
19474 manish.sha 824
                delEstCostObj.logistics_provider_id = providerId
19421 manish.sha 825
                delEstCostObj.pincode = pincode
19474 manish.sha 826
                delEstCostObj.deliveryTime = delivery_estimate_cache[pincode, providerId, warehouse_location][0]
827
                delEstCostObj.delivery_delay = delivery_estimate_cache[pincode, providerId, warehouse_location][1]
828
                delEstCostObj.codAllowed = serviceability[providerId][2]
829
                delEstCostObj.otgAvailable = serviceability[providerId][3]
19421 manish.sha 830
                delEstCostObj.logisticsCost = costingDetails[0]-costingDetails[1]
831
                delEstCostObj.codCollectionCharges = costingDetails[1]
832
                return delEstCostObj
19474 manish.sha 833
            else:
834
                continue
19535 manish.sha 835
        raise LogisticsServiceException(103, "Unable to get Provider for given pincode: " + str(pincode)+" Amount:- "+str(transactionAmount)+" Weight:- "+ str(weight)+".")