Rev 731 | Rev 766 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 05-Aug-2010@author: ashish'''from shop2020.thriftpy.logistics.ttypes import LogisticsInfo,\LogisticsServiceExceptionfrom shop2020.logistics.service.impl.DataAccessor import get_empty_AWB,\get_shipment_info, initialize, get_logistics_estimation, get_provider,\get_providersfrom shop2020.logistics.service.impl.Converters import to_t_awbupdate,\to_t_providerfrom shop2020.clients.InventoryClient import InventoryClientfrom shop2020.config.client.ConfigClient import ConfigClientimport datetimeimport mathfrom shop2020.logistics.service.impl.DataService import ServiceableLocationDetailsclass LogisticsServiceHandler:def __init__(self):initialize()try:config_client = ConfigClient()self.cutoff_time = int(config_client.get_property('delivery_cutoff_time'))self.stock_threshold = int(config_client.get_property('inventory_stock_threshold'))self.time_to_producre = int(config_client.get_property('inventory_time_to_procure'))except:self.cutoff_time = 15self.stock_threshold = 2self.time_to_producre = 48def getProvider(self, providerId):"""Returns a provider for a given provider ID. Throws an exception if none found.Parameters:- providerId"""return to_t_provider(get_provider(providerId))def getAllProviders(self, ):"""Returns a list containing all the providers."""return [to_t_provider(provider) for provider in get_providers()]def getLogisticsInfo(self, destination_pincode, itemId):"""Parameters:- destination_pincode- item_id"""logistics_info = self.getLogisticsEstimation(itemId, destination_pincode)logistics_info.airway_billno = get_empty_AWB(logistics_info.providerId)return logistics_infodef getEmptyAWB(self, provider_id):"""Parameters:- provider_id"""return get_empty_AWB(provider_id)def getShipmentInfo(self, awb, providerId):"""Parameters:- awb"""awb_updates = get_shipment_info(awb, providerId)t_updates = []for update in awb_updates:t_updates.append(to_t_awbupdate(update))return t_updatesdef getLogisticsEstimation(self, itemId, destination_pin):"""Parameters:- itemId- destination_pin"""delivery_estimate = get_logistics_estimation(destination_pin)delivery_time = 24*delivery_estimate.delivery_timeif self.cutoff_time <= datetime.datetime.now().hour:delivery_time = delivery_time + 24warehouse_loc = delivery_estimate.warehouse_locationtry:client = InventoryClient().get_client()warehouse_id, items_in_inventory = client.getItemAvailabilityAtLocation(warehouse_loc, itemId)except:raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.")if items_in_inventory < self.stock_threshold :delivery_time = delivery_time + self.time_to_producredelivery_time = int(math.ceil(delivery_time/24.0))logistics_info = LogisticsInfo()logistics_info.deliveryTime = delivery_timelogistics_info.providerId = delivery_estimate.provider_idlogistics_info.warehouseId = warehouse_idreturn logistics_infodef getDestinationCode(self, providerId, pinCode):"""Returns the short three letter code of a pincode for the given provider.Raises an exception if the pin code is not serviced by the given provider.Parameters:- providerId- pinCode"""sld = ServiceableLocationDetails.get_by(provider_id = providerId, dest_pincode = pinCode)if sld != None:return sld.dest_codeelse:raise LogisticsServiceException(101, "The pincode " + pinCode + " is not serviced by this provider: " + str(providerId))