Subversion Repositories SmartDukaan

Rev

Rev 766 | Rev 796 | 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,\
    LogisticsServiceException
from shop2020.logistics.service.impl.DataAccessor import get_empty_AWB,\
    get_shipment_info, initialize, get_logistics_estimation, get_provider,\
    get_providers, close_session
from shop2020.logistics.service.impl.Converters import to_t_awbupdate,\
    to_t_provider
from shop2020.clients.InventoryClient import InventoryClient
from shop2020.config.client.ConfigClient import ConfigClient
import datetime
import math
from shop2020.logistics.service.impl.DataService import ServiceableLocationDetails

class 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'))
            self.default_pincode = int(config_client.get_property('default_pincode'))
        except:    
            self.cutoff_time = 15
            self.stock_threshold = 2
            self.time_to_producre = 48
            self.default_pincode = "110001" 
            
    def 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_info

    def getEmptyAWB(self, provider_id):
        """
        Parameters:
         - provider_id
        """
        return get_empty_AWB(provider_id)

    def getShipmentInfo(self, awb, providerId):
        """
        Parameters:
         - awb
         - providerId
        """
        awb_updates = get_shipment_info(awb, providerId)
        t_updates = []
        for update in awb_updates:
            t_updates.append(to_t_awbupdate(update))
        return t_updates

    def getLogisticsEstimation(self, itemId, destination_pin):
        """
        Parameters:
         - itemId
         - destination_pin
        """
        if not destination_pin:
            destination_pin = self.default_pincode
        delivery_estimate = get_logistics_estimation(destination_pin)
        if delivery_estimate is None:
            raise LogisticsServiceException(103, "Unable to fetch delivery estimate for this pincode.")
        delivery_time = 24*delivery_estimate.delivery_time
        if self.cutoff_time <= datetime.datetime.now().hour:
            delivery_time = delivery_time + 24
        warehouse_loc = delivery_estimate.warehouse_location
        try:
            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_producre
        delivery_time = int(math.ceil(delivery_time/24.0))
        
        logistics_info = LogisticsInfo()
        logistics_info.deliveryTime = delivery_time
        logistics_info.providerId = delivery_estimate.provider_id
        logistics_info.warehouseId = warehouse_id
        
        return logistics_info
    
    def 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_code
        else:
            raise LogisticsServiceException(101, "The pincode " + pinCode + " is not serviced by this provider: " + str(providerId))
     
    def closeSession(self, ):
        close_session()