Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
412 ashish 1
'''
2
Created on 05-Aug-2010
3
 
4
@author: ashish
5
'''
644 chandransh 6
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo,\
7
    LogisticsServiceException
8
from shop2020.logistics.service.impl.DataAccessor import get_empty_AWB,\
675 chandransh 9
    get_shipment_info, initialize, get_logistics_estimation, get_provider,\
10
    get_providers
669 chandransh 11
from shop2020.logistics.service.impl.Converters import to_t_awbupdate,\
12
    to_t_provider
494 rajveer 13
from shop2020.clients.InventoryClient import InventoryClient
14
from shop2020.config.client.ConfigClient import ConfigClient
15
import datetime
644 chandransh 16
import math
731 chandransh 17
from shop2020.logistics.service.impl.DataService import ServiceableLocationDetails
472 rajveer 18
 
412 ashish 19
class LogisticsServiceHandler:
20
 
442 rajveer 21
    def __init__(self):
22
        initialize()
644 chandransh 23
        config_client = ConfigClient()
24
        self.cutoff_time = int(config_client.get_property('delivery_cutoff_time'))
25
        self.stock_threshold = int(config_client.get_property('inventory_stock_threshold'))
26
        self.time_to_producre = int(config_client.get_property('inventory_time_to_procure'))
483 rajveer 27
 
669 chandransh 28
    def getProvider(self, providerId):
675 chandransh 29
        """
30
        Returns a provider for a given provider ID. Throws an exception if none found.
31
 
32
        Parameters:
33
         - providerId
34
        """
35
        return to_t_provider(get_provider(providerId))
669 chandransh 36
 
675 chandransh 37
    def getAllProviders(self, ):
38
        """
39
        Returns a list containing all the providers.
40
        """
41
        return [to_t_provider(provider) for provider in get_providers()]
42
 
644 chandransh 43
    def getLogisticsInfo(self, destination_pincode, itemId):
483 rajveer 44
        """
45
        Parameters:
46
         - destination_pincode
716 rajveer 47
         - item_id
483 rajveer 48
        """
644 chandransh 49
        logistics_info = self.getLogisticsEstimation(itemId, destination_pincode)
50
        logistics_info.airway_billno = get_empty_AWB(logistics_info.providerId)
483 rajveer 51
        return logistics_info
412 ashish 52
 
53
    def getEmptyAWB(self, provider_id):
54
        """
55
        Parameters:
56
         - provider_id
57
        """
442 rajveer 58
        return get_empty_AWB(provider_id)
59
 
644 chandransh 60
    def getShipmentInfo(self, awb, providerId):
412 ashish 61
        """
62
        Parameters:
63
         - awb
64
        """
644 chandransh 65
        awb_updates = get_shipment_info(awb, providerId)
66
        t_updates = []
67
        for update in awb_updates:
68
            t_updates.append(to_t_awbupdate(update))
69
        return t_updates
412 ashish 70
 
644 chandransh 71
    def getLogisticsEstimation(self, itemId, destination_pin):
472 rajveer 72
        """
73
        Parameters:
74
         - itemId
75
         - destination_pin
76
        """
644 chandransh 77
        delivery_estimate = get_logistics_estimation(destination_pin)
78
        delivery_time = 24*delivery_estimate.delivery_time
79
        if self.cutoff_time <= datetime.datetime.now().hour:
494 rajveer 80
            delivery_time = delivery_time + 24
644 chandransh 81
        warehouse_loc = delivery_estimate.warehouse_location
82
        try:
83
            client = InventoryClient().get_client()
84
            warehouse_id, items_in_inventory = client.getItemAvailabilityAtLocation(warehouse_loc, itemId)
85
        except:
86
            raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.") 
87
        if items_in_inventory < self.stock_threshold :
88
            delivery_time = delivery_time + self.time_to_producre
89
        delivery_time = int(math.ceil(delivery_time/24.0))
472 rajveer 90
 
644 chandransh 91
        logistics_info = LogisticsInfo()
92
        logistics_info.deliveryTime = delivery_time
93
        logistics_info.providerId = delivery_estimate.provider_id
94
        logistics_info.warehouseId = warehouse_id
477 rajveer 95
 
731 chandransh 96
        return logistics_info
97
 
98
    def getDestinationCode(self, providerId, pinCode):
99
        """
100
        Returns the short three letter code of a pincode for the given provider.
101
        Raises an exception if the pin code is not serviced by the given provider.
102
 
103
        Parameters:
104
         - providerId
105
         - pinCode
106
        """
107
        sld = ServiceableLocationDetails.get_by(provider_id = providerId, dest_pincode = pinCode)
108
        if sld != None:
109
            return sld.dest_code
110
        else:
111
            raise LogisticsServiceException(101, "The pincode " + pinCode + " is not serviced by this provider: " + str(providerId))