Subversion Repositories SmartDukaan

Rev

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