Subversion Repositories SmartDukaan

Rev

Rev 731 | Rev 766 | 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()
746 rajveer 23
        try:
24
            config_client = ConfigClient()
25
            self.cutoff_time = int(config_client.get_property('delivery_cutoff_time'))
26
            self.stock_threshold = int(config_client.get_property('inventory_stock_threshold'))
27
            self.time_to_producre = int(config_client.get_property('inventory_time_to_procure'))
28
        except:    
29
            self.cutoff_time = 15
30
            self.stock_threshold = 2
31
            self.time_to_producre = 48 
32
 
669 chandransh 33
    def getProvider(self, providerId):
675 chandransh 34
        """
35
        Returns a provider for a given provider ID. Throws an exception if none found.
36
 
37
        Parameters:
38
         - providerId
39
        """
40
        return to_t_provider(get_provider(providerId))
669 chandransh 41
 
675 chandransh 42
    def getAllProviders(self, ):
43
        """
44
        Returns a list containing all the providers.
45
        """
46
        return [to_t_provider(provider) for provider in get_providers()]
47
 
644 chandransh 48
    def getLogisticsInfo(self, destination_pincode, itemId):
483 rajveer 49
        """
50
        Parameters:
51
         - destination_pincode
716 rajveer 52
         - item_id
483 rajveer 53
        """
644 chandransh 54
        logistics_info = self.getLogisticsEstimation(itemId, destination_pincode)
55
        logistics_info.airway_billno = get_empty_AWB(logistics_info.providerId)
483 rajveer 56
        return logistics_info
412 ashish 57
 
58
    def getEmptyAWB(self, provider_id):
59
        """
60
        Parameters:
61
         - provider_id
62
        """
442 rajveer 63
        return get_empty_AWB(provider_id)
64
 
644 chandransh 65
    def getShipmentInfo(self, awb, providerId):
412 ashish 66
        """
67
        Parameters:
68
         - awb
69
        """
644 chandransh 70
        awb_updates = get_shipment_info(awb, providerId)
71
        t_updates = []
72
        for update in awb_updates:
73
            t_updates.append(to_t_awbupdate(update))
74
        return t_updates
412 ashish 75
 
644 chandransh 76
    def getLogisticsEstimation(self, itemId, destination_pin):
472 rajveer 77
        """
78
        Parameters:
79
         - itemId
80
         - destination_pin
81
        """
644 chandransh 82
        delivery_estimate = get_logistics_estimation(destination_pin)
83
        delivery_time = 24*delivery_estimate.delivery_time
84
        if self.cutoff_time <= datetime.datetime.now().hour:
494 rajveer 85
            delivery_time = delivery_time + 24
644 chandransh 86
        warehouse_loc = delivery_estimate.warehouse_location
87
        try:
88
            client = InventoryClient().get_client()
89
            warehouse_id, items_in_inventory = client.getItemAvailabilityAtLocation(warehouse_loc, itemId)
90
        except:
91
            raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.") 
92
        if items_in_inventory < self.stock_threshold :
93
            delivery_time = delivery_time + self.time_to_producre
94
        delivery_time = int(math.ceil(delivery_time/24.0))
472 rajveer 95
 
644 chandransh 96
        logistics_info = LogisticsInfo()
97
        logistics_info.deliveryTime = delivery_time
98
        logistics_info.providerId = delivery_estimate.provider_id
99
        logistics_info.warehouseId = warehouse_id
477 rajveer 100
 
731 chandransh 101
        return logistics_info
102
 
103
    def getDestinationCode(self, providerId, pinCode):
104
        """
105
        Returns the short three letter code of a pincode for the given provider.
106
        Raises an exception if the pin code is not serviced by the given provider.
107
 
108
        Parameters:
109
         - providerId
110
         - pinCode
111
        """
112
        sld = ServiceableLocationDetails.get_by(provider_id = providerId, dest_pincode = pinCode)
113
        if sld != None:
114
            return sld.dest_code
115
        else:
116
            raise LogisticsServiceException(101, "The pincode " + pinCode + " is not serviced by this provider: " + str(providerId))