Subversion Repositories SmartDukaan

Rev

Rev 2680 | Rev 3064 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2680 Rev 3044
Line 2... Line 2...
2
Created on 05-Aug-2010
2
Created on 05-Aug-2010
3
 
3
 
4
@author: ashish
4
@author: ashish
5
'''
5
'''
6
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo,\
6
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo,\
7
    LogisticsServiceException
7
    LogisticsServiceException, DeliveryType
8
from shop2020.logistics.service.impl.DataAccessor import get_empty_AWB,\
8
from shop2020.logistics.service.impl.DataAccessor import get_empty_AWB,\
9
    get_shipment_info, initialize, get_logistics_estimation, get_provider,\
9
    get_shipment_info, initialize, get_logistics_estimation, get_provider,\
10
    get_providers, close_session, get_free_awb_count, get_holidays
10
    get_providers, close_session, get_free_awb_count, get_holidays
11
from shop2020.logistics.service.impl.Converters import to_t_awbupdate,\
11
from shop2020.logistics.service.impl.Converters import to_t_awbupdate,\
12
    to_t_provider
12
    to_t_provider
Line 67... Line 67...
67
        try:
67
        try:
68
            return [to_t_provider(provider) for provider in get_providers()]
68
            return [to_t_provider(provider) for provider in get_providers()]
69
        finally:
69
        finally:
70
            close_session()
70
            close_session()
71
            
71
            
72
    def getLogisticsInfo(self, destination_pincode, itemId):
72
    def getLogisticsInfo(self, destination_pincode, itemId, type):
73
        """
73
        """
74
        Parameters:
74
        Parameters:
75
         - destination_pincode
75
         - destination_pincode
76
         - item_id
76
         - item_id
-
 
77
         - type
77
        """
78
        """
78
        try:
79
        try:
79
            logistics_info = self.getLogisticsEstimation(itemId, destination_pincode)
80
            logistics_info = self.get_logistics_estimation_with_type(itemId, destination_pincode, type)
80
            logistics_info.airway_billno = get_empty_AWB(logistics_info.providerId)
81
            logistics_info.airway_billno = get_empty_AWB(logistics_info.providerId, type)
81
            return logistics_info
82
            return logistics_info
82
        finally:
83
        finally:
83
            close_session()
84
            close_session()
84
            
85
            
85
    def getEmptyAWB(self, provider_id):
86
    def getEmptyAWB(self, provider_id):
Line 104... Line 105...
104
            for update in awb_updates:
105
            for update in awb_updates:
105
                t_updates.append(to_t_awbupdate(update))
106
                t_updates.append(to_t_awbupdate(update))
106
            return t_updates
107
            return t_updates
107
        finally:
108
        finally:
108
            close_session()
109
            close_session()
109
            
110
    
110
    def getLogisticsEstimation(self, itemId, destination_pin):
111
    def getLogisticsEstimation(self, itemId, destination_pin):
111
        """
112
        """
112
        Parameters:
113
        Parameters:
113
         - itemId
114
         - itemId
114
         - destination_pin
115
         - destination_pin
115
        """
116
        """
116
        try:
117
        try:
117
            if not destination_pin:
-
 
118
                destination_pin = self.default_pincode
-
 
119
            try:
-
 
120
                client = InventoryClient().get_client()
-
 
121
                item = client.getItem(itemId)
-
 
122
            except Exception as ex:
-
 
123
                raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.")
-
 
124
 
-
 
125
            delivery_estimate = get_logistics_estimation(destination_pin, item.sellingPrice)
118
            return self.get_logistics_estimation_with_type(itemId, destination_pin)
126
            if delivery_estimate is None:
-
 
127
                raise LogisticsServiceException(104, "Unable to fetch delivery estimate for this pincode.")
-
 
128
            
-
 
129
            warehouse_loc = delivery_estimate.warehouse_location
-
 
130
            try:
-
 
131
                #Get the id and location of actual warehouse that'll be used to fulfill this order.
-
 
132
                warehouse_loc, warehouse_id, items_in_inventory = client.getItemAvailabilityAtLocation(warehouse_loc, itemId)
-
 
133
            except Exception as ex:
-
 
134
                raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.")
-
 
135
            
-
 
136
            # We are revising the estimated based on the actual warehouse that will be assigned to this order.
-
 
137
            # This warehouse may be located in a different zone that the one we allocated for this pincode.
-
 
138
            delivery_estimate = get_logistics_estimation(destination_pin, item.sellingPrice, warehouse_loc)
-
 
139
            if delivery_estimate is None:
-
 
140
                raise LogisticsServiceException(105, "Unable to fetch delivery estimate for pincode: " + destination_pin + " and revised location: " + str(warehouse_loc))
-
 
141
            
-
 
142
            delivery_time = 24*delivery_estimate.delivery_time
-
 
143
            
-
 
144
            # Increase the estimate if the actual stock is less than the threshold 
-
 
145
            if items_in_inventory < self.stock_threshold :
-
 
146
                delivery_time = delivery_time + self.time_to_producre
-
 
147
            
-
 
148
            #Further increase the estimate if it's late in the day
-
 
149
            if self.cutoff_time <= datetime.datetime.now().hour:
-
 
150
                delivery_time = delivery_time + 24
-
 
151
            
-
 
152
            delivery_time = int(math.ceil(delivery_time/24.0))
-
 
153
            
-
 
154
            
-
 
155
            ## FIXME Due to Kanwaar on the delhi Dehradun route deliveries on following pin codes will be effected.
-
 
156
            ## Changing as per ticket number #462
-
 
157
            if destination_pin in self.kanwaar_effected_pins_by_two_days:
-
 
158
                delivery_time += 2;
-
 
159
            elif destination_pin in self.kanwaar_effected_pins_by_one_day:
-
 
160
                delivery_time += 1;
-
 
161
            ## FIXME Remove the above code once Kanwaar season is over    
-
 
162
            
-
 
163
            logistics_info = LogisticsInfo()
-
 
164
            logistics_info.deliveryTime = delivery_time
-
 
165
            logistics_info.providerId = delivery_estimate.provider_id
-
 
166
            logistics_info.warehouseId = warehouse_id
-
 
167
            
-
 
168
            return logistics_info
-
 
169
        finally:
119
        finally:
170
            close_session()
120
            close_session()
-
 
121
    
-
 
122
    def get_logistics_estimation_with_type(self, itemId, destination_pin, type = DeliveryType.PREPAID):
-
 
123
        if not destination_pin:
-
 
124
            destination_pin = self.default_pincode
-
 
125
        try:
-
 
126
            client = InventoryClient().get_client()
-
 
127
            item = client.getItem(itemId)
-
 
128
        except Exception as ex:
-
 
129
            raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.")
-
 
130
 
-
 
131
        delivery_estimate = get_logistics_estimation(destination_pin, item.sellingPrice)
-
 
132
        if delivery_estimate is None:
-
 
133
            raise LogisticsServiceException(104, "Unable to fetch delivery estimate for this pincode.")
-
 
134
        
-
 
135
        warehouse_loc = delivery_estimate.warehouse_location
-
 
136
        try:
-
 
137
            #Get the id and location of actual warehouse that'll be used to fulfill this order.
-
 
138
            warehouse_loc, warehouse_id, items_in_inventory = client.getItemAvailabilityAtLocation(warehouse_loc, itemId)
-
 
139
        except Exception as ex:
-
 
140
            raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.")
-
 
141
        
-
 
142
        # We are revising the estimated based on the actual warehouse that will be assigned to this order.
-
 
143
        # This warehouse may be located in a different zone that the one we allocated for this pincode.
-
 
144
        delivery_estimate = get_logistics_estimation(destination_pin, item.sellingPrice, warehouse_loc)
-
 
145
        if delivery_estimate is None:
-
 
146
            raise LogisticsServiceException(105, "Unable to fetch delivery estimate for pincode: " + destination_pin + " and revised location: " + str(warehouse_loc))
-
 
147
        
-
 
148
        delivery_time = 24*delivery_estimate.delivery_time
-
 
149
        
-
 
150
        # Increase the estimate if the actual stock is less than the threshold 
-
 
151
        if items_in_inventory < self.stock_threshold :
-
 
152
            delivery_time = delivery_time + self.time_to_producre
-
 
153
        
-
 
154
        #Further increase the estimate if it's late in the day
-
 
155
        if self.cutoff_time <= datetime.datetime.now().hour:
-
 
156
            delivery_time = delivery_time + 24
-
 
157
        
-
 
158
        delivery_time = int(math.ceil(delivery_time/24.0))
-
 
159
        
-
 
160
        
-
 
161
        ## FIXME Due to Kanwaar on the delhi Dehradun route deliveries on following pin codes will be effected.
-
 
162
        ## Changing as per ticket number #462
-
 
163
        if destination_pin in self.kanwaar_effected_pins_by_two_days:
-
 
164
            delivery_time += 2;
-
 
165
        elif destination_pin in self.kanwaar_effected_pins_by_one_day:
171
                    
166
            delivery_time += 1;
-
 
167
        ## FIXME Remove the above code once Kanwaar season is over    
-
 
168
        
-
 
169
        logistics_info = LogisticsInfo()
-
 
170
        logistics_info.deliveryTime = delivery_time
-
 
171
        logistics_info.providerId = delivery_estimate.provider_id
-
 
172
        logistics_info.warehouseId = warehouse_id
-
 
173
        
-
 
174
        return logistics_info
-
 
175
        
172
    def getDestinationCode(self, providerId, pinCode):
176
    def getDestinationCode(self, providerId, pinCode):
173
        """
177
        """
174
        Returns the short three letter code of a pincode for the given provider.
178
        Returns the short three letter code of a pincode for the given provider.
175
        Raises an exception if the pin code is not serviced by the given provider.
179
        Raises an exception if the pin code is not serviced by the given provider.
176
        
180