Subversion Repositories SmartDukaan

Rev

Rev 16025 | Rev 17992 | 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
442 rajveer 10
from shop2020.logistics.service.impl import DataService
644 chandransh 11
from shop2020.logistics.service.impl.DataService import Awb, AwbUpdate, Provider, \
16025 amit.gupta 12
    DeliveryEstimate, WarehouseAllocation, PublicHolidays, \
13
    ServiceableLocationDetails, PickupStore
14
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException, \
7786 manish.sha 15
    DeliveryType, LogisticsLocationInfo
16025 amit.gupta 16
from shop2020.thriftpy.model.v1.inventory.ttypes import Warehouse, BillingType,\
17
    WarehouseType
442 rajveer 18
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
5964 amar.kumar 19
from sqlalchemy.sql import or_
16025 amit.gupta 20
import datetime
3355 chandransh 21
import logging
13146 manish.sha 22
import os
16025 amit.gupta 23
import sys
24
import time
3355 chandransh 25
logging.basicConfig(level=logging.DEBUG)
442 rajveer 26
 
3217 rajveer 27
warehouse_allocation_cache = {}
28
serviceable_location_cache = {}
29
delivery_estimate_cache = {}
16025 amit.gupta 30
warehouse_location_cache = {}
6370 rajveer 31
 
3218 rajveer 32
'''
33
This class is for only data transfer. Never used outside this module.
34
'''
35
class _DeliveryEstimateObject:
6537 rajveer 36
    def __init__(self, delivery_time, delivery_delay, provider_id, codAllowed, otgAvailable):
3218 rajveer 37
        self.delivery_time = delivery_time
6537 rajveer 38
        self.delivery_delay = delivery_delay
3218 rajveer 39
        self.provider_id = provider_id
4866 rajveer 40
        self.codAllowed = codAllowed
6524 rajveer 41
        self.otgAvailable = otgAvailable
3218 rajveer 42
 
3187 rajveer 43
def initialize(dbname="logistics", db_hostname="localhost"):
442 rajveer 44
    log_entry("initialize@DataAccessor", "Initializing data service")
3187 rajveer 45
    DataService.initialize(dbname, db_hostname)
3218 rajveer 46
    print "Starting cache population at: " + str(datetime.datetime.now())
16025 amit.gupta 47
    #__cache_warehouse_allocation_table()
48
    __cache_warehouse_locations()
3217 rajveer 49
    __cache_serviceable_location_details_table()
50
    __cache_delivery_estimate_table()
51
    close_session()
3218 rajveer 52
    print "Done cache population at: " + str(datetime.datetime.now())
442 rajveer 53
 
3217 rajveer 54
def __cache_delivery_estimate_table():
55
    delivery_estimates = DeliveryEstimate.query.all()
56
    for delivery_estimate in delivery_estimates:
7857 rajveer 57
        delivery_estimate_cache[delivery_estimate.destination_pin, delivery_estimate.provider_id, delivery_estimate.warehouse_location]\
6537 rajveer 58
        =delivery_estimate.delivery_time, delivery_estimate.delivery_delay
3217 rajveer 59
 
60
def __cache_serviceable_location_details_table():
5964 amar.kumar 61
    serviceable_locations = ServiceableLocationDetails.query.filter(or_("exp!=0","cod!=0"))
3217 rajveer 62
    for serviceable_location in serviceable_locations:
63
        try:
64
            provider_pincodes = serviceable_location_cache[serviceable_location.provider_id]
65
        except:
66
            provider_pincodes = {}
67
            serviceable_location_cache[serviceable_location.provider_id] = provider_pincodes 
7627 rajveer 68
        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
3217 rajveer 69
 
70
def __cache_warehouse_allocation_table():
71
    warehouse_allocations = WarehouseAllocation.query.all()
72
    for warehouse_allocation in warehouse_allocations:
73
        warehouse_allocation_cache[warehouse_allocation.pincode]=warehouse_allocation.primary_warehouse_location
74
 
16025 amit.gupta 75
def __cache_warehouse_locations():
76
    client = InventoryClient().get_client()
77
    client.getAllWarehouses(True)
78
    #for WarehouseAllocation.query.all()
79
    warehouse = Warehouse()
80
    for warehouse in client.getAllWarehouses(True):
81
        warehouse_location_cache[warehouse.billingWarehouseId]=warehouse.logisticsLocation
82
 
644 chandransh 83
def get_provider(provider_id):
766 rajveer 84
    provider =  Provider.get_by(id=provider_id)
85
    return provider
644 chandransh 86
 
87
def get_providers():
5387 rajveer 88
    providers = Provider.query.filter_by(isActive = 1).all()
766 rajveer 89
    return providers 
644 chandransh 90
 
7608 rajveer 91
def get_logistics_estimation(destination_pin, item_selling_price, weight, type, billingWarehouseId):
5692 rajveer 92
    logging.info("Getting logistics estimation for pincode:" + destination_pin )
1504 ankur.sing 93
 
7608 rajveer 94
    provider_id, codAllowed, otgAvailable = __get_logistics_provider_for_destination_pincode(destination_pin, item_selling_price, weight, type, billingWarehouseId)
9964 anupam.sin 95
 
17530 manish.sha 96
    logging.info("Provider Id:  " + str(provider_id) +" codAllowed: "+str(codAllowed)+" otgAvailable: "+str(otgAvailable)+ " item_selling_price: "+str(item_selling_price))
97
 
98
    if item_selling_price > 60000:# or item_selling_price <= 250:
9964 anupam.sin 99
        codAllowed = False
100
 
16025 amit.gupta 101
    warehouse_location = warehouse_location_cache.get(billingWarehouseId)
102
    if warehouse_location is None:
103
        warehouse_location = 0
7857 rajveer 104
 
3217 rajveer 105
    if not provider_id:
5692 rajveer 106
        raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin))
644 chandransh 107
    try:
16025 amit.gupta 108
        logging.info( "destination_pin %s, provider_id %s, warehouse_location %s"%(destination_pin, provider_id, warehouse_location))
109
        logging.info( "Estimates %s, %s"%(delivery_estimate_cache[destination_pin, provider_id, warehouse_location]))
7857 rajveer 110
        delivery_time = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][0]
111
        delivery_delay = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][1]
6537 rajveer 112
        delivery_estimate = _DeliveryEstimateObject(delivery_time, delivery_delay, provider_id, codAllowed, otgAvailable)
3218 rajveer 113
        return delivery_estimate
1504 ankur.sing 114
    except Exception as ex:
115
        print ex
644 chandransh 116
        raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
3150 rajveer 117
 
7608 rajveer 118
def __get_logistics_provider_for_destination_pincode(destination_pin, item_selling_price, weight, type, billingWarehouseId):
13357 manish.sha 119
    '''
120
    if serviceable_location_cache.get(6).has_key(destination_pin):
121
        if weight < 480 and serviceable_location_cache.get(3).has_key(destination_pin) and type == DeliveryType.PREPAID:
122
            dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit = serviceable_location_cache.get(3).get(destination_pin)
123
            if item_selling_price <= providerPrepaidLimit:
124
                iscod = iscod and item_selling_price <= websiteCodLimit
125
                otgAvailable = otgAvailable and item_selling_price >= 2000
126
                return 3, iscod, otgAvailable
127
        else:
128
            dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit = serviceable_location_cache.get(6).get(destination_pin)
129
            if item_selling_price <= providerPrepaidLimit:
130
                iscod = iscod and item_selling_price <= websiteCodLimit
131
                otgAvailable = otgAvailable and item_selling_price >= 2000
132
                return 6, iscod, otgAvailable
133
    '''        
7981 rajveer 134
    if serviceable_location_cache.get(3).has_key(destination_pin):
135
        dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit = serviceable_location_cache.get(3).get(destination_pin)
7627 rajveer 136
        if item_selling_price <= providerPrepaidLimit:
7608 rajveer 137
            iscod = iscod and item_selling_price <= websiteCodLimit
138
            otgAvailable = otgAvailable and item_selling_price >= 2000
7981 rajveer 139
            return 3, iscod, otgAvailable
8575 rajveer 140
 
141
    if billingWarehouseId not in [12,13] and serviceable_location_cache.get(7).has_key(destination_pin):
142
        if item_selling_price < 3000 and serviceable_location_cache.get(1).has_key(destination_pin):
143
            dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit = serviceable_location_cache.get(1).get(destination_pin)
144
            if item_selling_price <= providerPrepaidLimit:
145
                iscod = iscod and item_selling_price <= websiteCodLimit
146
                otgAvailable = otgAvailable and item_selling_price >= 2000
147
                return 1, iscod, otgAvailable
148
        else:
149
            dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit = serviceable_location_cache.get(7).get(destination_pin)
150
            if item_selling_price <= providerPrepaidLimit:
151
                iscod = iscod and item_selling_price <= websiteCodLimit
152
                otgAvailable = otgAvailable and item_selling_price >= 2000
153
                return 7, iscod, otgAvailable
154
 
7627 rajveer 155
    if serviceable_location_cache.get(1).has_key(destination_pin):
156
        dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit, providerPrepaidLimit = serviceable_location_cache.get(1).get(destination_pin)
157
        if item_selling_price <= providerPrepaidLimit:
7608 rajveer 158
            iscod = iscod and item_selling_price <= websiteCodLimit
159
            otgAvailable = otgAvailable and item_selling_price >= 2000
7627 rajveer 160
            return 1, iscod, otgAvailable
7981 rajveer 161
 
10185 amit.gupta 162
    return None, False, False    
5278 rajveer 163
 
6017 amar.kumar 164
def get_destination_code(providerId, pinCode):
165
    serviceableLocationDetail = ServiceableLocationDetails.query.filter_by(provider_id = providerId, dest_pincode = pinCode).one()
166
    return serviceableLocationDetail.dest_code
167
 
644 chandransh 168
def add_empty_AWBs(numbers, provider_id, type):
442 rajveer 169
    for number in numbers:
644 chandransh 170
        query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
444 rajveer 171
        try:
172
            query.one()
173
        except:    
644 chandransh 174
            awb = Awb()
444 rajveer 175
            awb.awb_number = number
644 chandransh 176
            awb.provider_id = provider_id
444 rajveer 177
            awb.is_available = True
644 chandransh 178
            awb.type = type
179
    session.commit()
442 rajveer 180
 
444 rajveer 181
def set_AWB_as_used(awb_number):
644 chandransh 182
    query = Awb.query.filter_by(awb_number = awb_number)
444 rajveer 183
    awb = query.one() 
184
    awb.is_available=False
185
    session.commit()
186
 
3044 chandransh 187
def get_empty_AWB(provider_id, type = DeliveryType.PREPAID):
2342 chandransh 188
    query = Awb.query.with_lockmode("update").filter_by(provider_id = provider_id, is_available = True)  #check the provider thing
3044 chandransh 189
    if type == DeliveryType.PREPAID:
190
        query = query.filter_by(type="Prepaid")
191
    if type == DeliveryType.COD:
192
        query = query.filter_by(type="COD")
193
 
13158 manish.sha 194
    additionalQuery = query
195
    query = query.filter_by(awbUsedFor=0)
196
 
442 rajveer 197
    try:
644 chandransh 198
        awb = query.first()
13158 manish.sha 199
        if awb is None:
200
            additionalQuery = additionalQuery.filter_by(awbUsedFor=2)
201
            awb = additionalQuery.first()
644 chandransh 202
        awb.is_available = False
203
        session.commit()
444 rajveer 204
        return awb.awb_number
442 rajveer 205
    except:
644 chandransh 206
        raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(provider_id))
1137 chandransh 207
 
3103 chandransh 208
def get_free_awb_count(provider_id, type):
209
    count = Awb.query.filter_by(provider_id = provider_id, is_available = True, type=type).count()
1137 chandransh 210
    if count == None:
211
        count = 0
212
    return count
442 rajveer 213
 
644 chandransh 214
def get_shipment_info(awb_number, provider_id):
215
    awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
216
    query = AwbUpdate.query.filter_by(awb = awb)
766 rajveer 217
    info = query.all()
218
    return info
219
 
6643 rajveer 220
def store_shipment_info(update):
221
    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()
222
    if not updates:
223
        dupdate = AwbUpdate()
224
        dupdate.providerId = update.providerId
225
        dupdate.awbNumber  = update.awbNumber
226
        dupdate.location = update.location
227
        dupdate.date = to_py_date(update.date)
228
        dupdate.status = update.status
229
        dupdate.description = update.description
230
        session.commit()
231
 
1730 ankur.sing 232
def get_holidays(start_date, end_date):
233
    query = PublicHolidays.query
234
    if start_date != -1:
235
        query = query.filter(PublicHolidays.date >= to_py_date(start_date))
236
    if end_date != -1:
237
        query = query.filter(PublicHolidays.date <= to_py_date(end_date))
238
    holidays = query.all()
239
    holiday_dates = [to_java_date(datetime.datetime(*time.strptime(str(holiday.date), '%Y-%m-%d')[:3])) for holiday in holidays] 
240
    return holiday_dates
241
 
5527 anupam.sin 242
def get_provider_for_pickup_type(pickUp):
243
    return Provider.query.filter(Provider.pickup == pickUp).first().id
244
 
766 rajveer 245
def close_session():
246
    if session.is_active:
247
        print "session is active. closing it."
2823 chandransh 248
        session.close()
3376 rajveer 249
 
250
def is_alive():
251
    try:
252
        session.query(Awb.id).limit(1).one()
253
        return True
254
    except:
5555 rajveer 255
        return False
256
 
257
def get_all_pickup_stores():
5572 anupam.sin 258
    pickupStores = PickupStore.query.all()
259
    return pickupStores
5555 rajveer 260
 
261
def get_pickup_store(storeId):
5719 rajveer 262
    return PickupStore.query.filter_by(id = storeId).one()
263
 
264
def get_pickup_store_by_hotspot_id(hotspotId):
265
    return PickupStore.query.filter_by(hotspotId = hotspotId).one()
6322 amar.kumar 266
 
6524 rajveer 267
def add_pincode(provider, pincode, destCode, exp, cod, stationType, otgAvailable):
7914 rajveer 268
    provider = Provider.query.filter_by(id = provider).one()
6322 amar.kumar 269
    serviceableLocationDetails = ServiceableLocationDetails()
270
    serviceableLocationDetails.provider = provider
271
    serviceableLocationDetails.dest_pincode = pincode
272
    serviceableLocationDetails.dest_code = destCode
273
    serviceableLocationDetails.exp = exp
274
    serviceableLocationDetails.cod = cod
275
    serviceableLocationDetails.station_type = stationType
6524 rajveer 276
    serviceableLocationDetails.otgAvailable = otgAvailable
7733 manish.sha 277
    if provider.id == 1:
7627 rajveer 278
        codlimit = 10000
279
        prepaidlimit = 50000
7733 manish.sha 280
    elif provider.id == 3:
7627 rajveer 281
        codlimit = 25000
282
        prepaidlimit = 100000
7733 manish.sha 283
    elif provider.id == 6:
7627 rajveer 284
        codlimit = 25000
285
        prepaidlimit = 100000
286
    serviceableLocationDetails.providerPrepaidLimit = prepaidlimit
287
    serviceableLocationDetails.providerCodLimit = codlimit
288
    serviceableLocationDetails.websiteCodLimit = codlimit
289
    serviceableLocationDetails.storeCodLimit = codlimit
6322 amar.kumar 290
    session.commit()
291
 
6524 rajveer 292
def update_pincode(providerId, pincode, exp, cod, otgAvailable):
6615 amar.kumar 293
    serviceableLocationDetails = ServiceableLocationDetails.get_by(provider_id = providerId, dest_pincode = pincode)
6322 amar.kumar 294
    serviceableLocationDetails.exp = exp
295
    serviceableLocationDetails.cod = cod
6524 rajveer 296
    serviceableLocationDetails.otgAvailable = otgAvailable
7256 rajveer 297
    session.commit()
7567 rajveer 298
 
13146 manish.sha 299
def add_new_awbs(provider_id, isCod, awbs, awbUsedFor):
7567 rajveer 300
    provider = get_provider(provider_id)
301
    if isCod:
302
        dtype = 'Cod'
303
    else:
304
        dtype = 'Prepaid'
305
    for awb in awbs:
306
        a = Awb()
307
        a.type =  dtype
308
        a.is_available = True
309
        a.awb_number = awb
13146 manish.sha 310
        a.awbUsedFor = awbUsedFor
7567 rajveer 311
        a.provider = provider 
312
    session.commit()
7608 rajveer 313
    return True
7256 rajveer 314
 
315
def adjust_delivery_time(start_time, delivery_days):
316
    '''
317
    Returns the actual no. of days which will pass while 'delivery_days'
318
    no. of business days will pass since 'order_time'. 
319
    '''
320
    start_date = start_time.date()
321
    end_date = start_date + datetime.timedelta(days = delivery_days)
7272 amit.gupta 322
    holidays = get_holidays(to_java_date(start_time), -1)
7256 rajveer 323
    holidays = [to_py_date(holiday).date() for holiday in holidays]
324
 
325
    while start_date <= end_date:
326
        if start_date.weekday() == 6 or start_date in holidays:
327
            delivery_days = delivery_days + 1
328
            end_date = end_date + datetime.timedelta(days = 1)
329
        start_date = start_date + datetime.timedelta(days = 1)
330
 
331
    return delivery_days
7275 rajveer 332
 
333
def get_min_advance_amount(itemId, destination_pin, providerId, codAllowed):
334
    client = CatalogClient().get_client()
335
    sp = client.getStorePricing(itemId)
336
 
337
    if codAllowed:
338
        return sp.minAdvancePrice, True
339
    else:
7857 rajveer 340
        dest_code, exp, iscod, otgAvailable, websiteCodLimit, storeCodLimit = serviceable_location_cache.get(providerId).get(destination_pin)
7275 rajveer 341
        if iscod:
342
            if providerId == 6:
7489 rajveer 343
                return max(sp.minAdvancePrice, sp.recommendedPrice - storeCodLimit), True
7275 rajveer 344
            if providerId == 3:
7489 rajveer 345
                return max(sp.minAdvancePrice, sp.recommendedPrice - storeCodLimit), True
7275 rajveer 346
            if providerId == 1:
7489 rajveer 347
                return max(sp.minAdvancePrice, sp.recommendedPrice - storeCodLimit), True
7275 rajveer 348
        else:
7425 rajveer 349
            return sp.recommendedPrice, False
7733 manish.sha 350
#Start:- Added by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013
351
 
7786 manish.sha 352
def run_Logistics_Location_Info_Update(logisticsLocationInfoList, runCompleteUpdate):
353
    if runCompleteUpdate == True :
354
        serviceableLocationDetails = ServiceableLocationDetails.query.all()
355
        for serviceableLocationDetail in serviceableLocationDetails:
7841 manish.sha 356
            serviceableLocationDetail.exp = False
357
            serviceableLocationDetail.cod = False
7786 manish.sha 358
    for logisticsLocationInfo in logisticsLocationInfoList:
7841 manish.sha 359
        serviceableLocationDetail = ServiceableLocationDetails.get_by(provider_id = logisticsLocationInfo.providerId, dest_pincode = logisticsLocationInfo.pinCode)
7914 rajveer 360
        provider = Provider.query.filter_by(id = logisticsLocationInfo.providerId).one()
7841 manish.sha 361
        if serviceableLocationDetail:
362
            serviceableLocationDetail.exp = logisticsLocationInfo.expAvailable
363
            serviceableLocationDetail.cod = logisticsLocationInfo.codAvailable
364
            serviceableLocationDetail.otgAvailable = logisticsLocationInfo.otgAvailable
365
            serviceableLocationDetail.providerCodLimit = logisticsLocationInfo.codLimit
366
            serviceableLocationDetail.providerPrepaidLimit = logisticsLocationInfo.prepaidLimit
367
            serviceableLocationDetail.storeCodLimit = logisticsLocationInfo.codLimit
8219 manish.sha 368
            serviceableLocationDetail.websiteCodLimit = min(26000,logisticsLocationInfo.codLimit)
7841 manish.sha 369
        else:
370
            serviceableLocationDetail= ServiceableLocationDetails()
371
            serviceableLocationDetail.provider = provider
372
            serviceableLocationDetail.dest_pincode = logisticsLocationInfo.pinCode
373
            serviceableLocationDetail.dest_code = logisticsLocationInfo.destinationCode
374
            serviceableLocationDetail.exp = logisticsLocationInfo.expAvailable
375
            serviceableLocationDetail.cod = logisticsLocationInfo.codAvailable
376
            serviceableLocationDetail.station_type = 0
377
            serviceableLocationDetail.otgAvailable = logisticsLocationInfo.otgAvailable
378
            serviceableLocationDetail.providerCodLimit = logisticsLocationInfo.codLimit
379
            serviceableLocationDetail.providerPrepaidLimit = logisticsLocationInfo.prepaidLimit
380
            serviceableLocationDetail.storeCodLimit = logisticsLocationInfo.codLimit
8219 manish.sha 381
            serviceableLocationDetail.websiteCodLimit = min(26000,logisticsLocationInfo.codLimit)
7841 manish.sha 382
 
7870 rajveer 383
        deliveryEstimate = DeliveryEstimate.get_by(provider_id = logisticsLocationInfo.providerId, destination_pin = logisticsLocationInfo.pinCode, warehouse_location = logisticsLocationInfo.warehouseId) 
7841 manish.sha 384
        if deliveryEstimate:
385
            deliveryEstimate.warehouse_location= logisticsLocationInfo.warehouseId
386
            deliveryEstimate.delivery_time= logisticsLocationInfo.deliveryTime
387
            deliveryEstimate.delivery_delay= logisticsLocationInfo.delivery_delay
388
        else:
389
            deliveryEstimate = DeliveryEstimate()
390
            deliveryEstimate.destination_pin= logisticsLocationInfo.pinCode
391
            deliveryEstimate.provider = provider
392
            deliveryEstimate.warehouse_location= logisticsLocationInfo.warehouseId
393
            deliveryEstimate.delivery_time= logisticsLocationInfo.deliveryTime
394
            deliveryEstimate.delivery_delay= logisticsLocationInfo.delivery_delay
7733 manish.sha 395
    session.commit()
7786 manish.sha 396
 
7733 manish.sha 397
#End:- Added by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013             
7275 rajveer 398
 
12895 manish.sha 399
def get_first_delivery_estimate_for_wh_location(pincode, whLocation):
400
    delEstimate = DeliveryEstimate.query.filter(DeliveryEstimate.destination_pin == pincode).filter(DeliveryEstimate.warehouse_location == whLocation).first()
401
    if delEstimate is None:
402
        return -1
403
    else:
404
        return 1
13146 manish.sha 405
 
406
def get_provider_limit_details_for_pincode(provider, pincode):
407
    serviceAbleDetails = ServiceableLocationDetails.get_by(provider_id = provider, dest_pincode = pincode)
408
    returnDataMap = {}
409
    if serviceAbleDetails:
410
        returnDataMap["websiteCodLimit"] = str(serviceAbleDetails.websiteCodLimit)
411
        returnDataMap["providerPrepaidLimit"] = str(serviceAbleDetails.providerPrepaidLimit)
412
 
413
    return returnDataMap
414
 
415
def get_new_empty_awb(providerId, type, orderQuantity):
416
    query = Awb.query.with_lockmode("update").filter_by(provider_id = providerId, is_available = True)  #check the provider thing
417
    if type == DeliveryType.PREPAID:
418
        query = query.filter_by(type="Prepaid")
419
    if type == DeliveryType.COD:
420
        query = query.filter_by(type="COD")
421
 
422
    additionalQuery = query
423
    if orderQuantity == 1:
424
        query = query.filter_by(awbUsedFor=0)
425
    if orderQuantity >1:
426
        query = query.filter_by(awbUsedFor=1) 
427
 
428
    try:
429
        awb = query.first()
430
        if awb is None:
431
            additionalQuery = additionalQuery.filter_by(awbUsedFor=2)
432
            awb = additionalQuery.first()
433
        awb.is_available = False
434
        session.commit()
435
        return awb.awb_number
436
    except:
437
        raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(providerId))
438