Subversion Repositories SmartDukaan

Rev

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

Rev 3044 Rev 3064
Line 5... Line 5...
5
'''
5
'''
6
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo,\
6
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo,\
7
    LogisticsServiceException, DeliveryType
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
    is_cod_allowed
11
from shop2020.logistics.service.impl.Converters import to_t_awbupdate,\
12
from shop2020.logistics.service.impl.Converters import to_t_awbupdate,\
12
    to_t_provider
13
    to_t_provider
13
from shop2020.clients.InventoryClient import InventoryClient
14
from shop2020.clients.InventoryClient import InventoryClient
14
from shop2020.config.client.ConfigClient import ConfigClient
15
from shop2020.config.client.ConfigClient import ConfigClient
15
import datetime
16
import datetime
Line 23... Line 24...
23
    def __init__(self, dbname='logistics'):
24
    def __init__(self, dbname='logistics'):
24
        initialize(dbname)
25
        initialize(dbname)
25
        try:
26
        try:
26
            config_client = ConfigClient()
27
            config_client = ConfigClient()
27
            self.cutoff_time = int(config_client.get_property('delivery_cutoff_time'))
28
            self.cutoff_time = int(config_client.get_property('delivery_cutoff_time'))
-
 
29
            self.cod_cutoff_time = 0 #int(config_client.get_property('delivery_cutoff_time'))
28
            self.stock_threshold = int(config_client.get_property('inventory_stock_threshold'))
30
            self.stock_threshold = int(config_client.get_property('inventory_stock_threshold'))
29
            self.time_to_producre = int(config_client.get_property('inventory_time_to_procure'))
31
            self.time_to_procure = int(config_client.get_property('inventory_time_to_procure'))
30
            self.default_pincode = int(config_client.get_property('default_pincode'))
32
            self.default_pincode = int(config_client.get_property('default_pincode'))
31
        except Exception as ex:
33
        except Exception as ex:
32
            print "[ERROR] Unexpected config error:", sys.exc_info()[0]
34
            print "[ERROR] Unexpected config error:", sys.exc_info()[0]
33
            self.cutoff_time = 15
35
            self.cutoff_time = 15
-
 
36
            self.cod_cutoff_time = 0
34
            self.stock_threshold = 2
37
            self.stock_threshold = 2
35
            self.time_to_producre = 48
38
            self.time_to_procure = 48
36
            self.default_pincode = "110001" 
39
            self.default_pincode = "110001" 
37
        
40
        
38
        ##FIXME 
41
        ##FIXME
-
 
42
        '''
39
        self.kanwaar_effected_pins_by_two_days = ['244713', '247667', '248001', '248002', '248003', '248005', '248006', 
43
        self.kanwaar_effected_pins_by_two_days = ['244713', '247667', '248001', '248002', '248003', '248005', '248006', 
40
                                             '248008', '248009', '248110', '248146', '248171', '248179', '249201', 
44
                                             '248008', '248009', '248110', '248146', '248171', '248179', '249201', 
41
                                             '249401', '249403', '249404', '249407', '249408', '249409', '249410', 
45
                                             '249401', '249403', '249404', '249407', '249408', '249409', '249410', 
42
                                             '249411', '262405', '263139', '263153']
46
                                             '249411', '262405', '263139', '263153']
43
        self.kanwaar_effected_pins_by_one_day = ['201001', '201002', '201003', '201004', '201005', '201007', '201009', 
47
        self.kanwaar_effected_pins_by_one_day = ['201001', '201002', '201003', '201004', '201005', '201007', '201009', 
44
                                            '201010', '201011', '201012', '201014', '201204', '202001', '202002', 
48
                                            '201010', '201011', '201012', '201014', '201204', '202001', '202002', 
45
                                            '203001', '203131', '204101']
49
                                            '203001', '203131', '204101']
-
 
50
        '''
46
            
51
         
47
    def getProvider(self, providerId):
52
    def getProvider(self, providerId):
48
        """
53
        """
49
        Returns a provider for a given provider ID. Throws an exception if none found.
54
        Returns a provider for a given provider ID. Throws an exception if none found.
50
        
55
        
51
        Parameters:
56
        Parameters:
Line 126... Line 131...
126
            client = InventoryClient().get_client()
131
            client = InventoryClient().get_client()
127
            item = client.getItem(itemId)
132
            item = client.getItem(itemId)
128
        except Exception as ex:
133
        except Exception as ex:
129
            raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.")
134
            raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.")
130
 
135
 
131
        delivery_estimate = get_logistics_estimation(destination_pin, item.sellingPrice)
136
        delivery_estimate = get_logistics_estimation(destination_pin, item.sellingPrice, None, type)
132
        if delivery_estimate is None:
137
        if delivery_estimate is None:
133
            raise LogisticsServiceException(104, "Unable to fetch delivery estimate for this pincode.")
138
            raise LogisticsServiceException(104, "Unable to fetch delivery estimate for this pincode.")
134
        
139
        
135
        warehouse_loc = delivery_estimate.warehouse_location
140
        warehouse_loc = delivery_estimate.warehouse_location
136
        try:
141
        try:
Line 139... Line 144...
139
        except Exception as ex:
144
        except Exception as ex:
140
            raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.")
145
            raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.")
141
        
146
        
142
        # We are revising the estimated based on the actual warehouse that will be assigned to this order.
147
        # 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.
148
        # 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)
149
        delivery_estimate = get_logistics_estimation(destination_pin, item.sellingPrice, warehouse_loc, type)
145
        if delivery_estimate is None:
150
        if delivery_estimate is None:
146
            raise LogisticsServiceException(105, "Unable to fetch delivery estimate for pincode: " + destination_pin + " and revised location: " + str(warehouse_loc))
151
            raise LogisticsServiceException(105, "Unable to fetch delivery estimate for pincode: " + destination_pin + " and revised location: " + str(warehouse_loc))
147
        
152
        
148
        delivery_time = 24*delivery_estimate.delivery_time
153
        delivery_time = 24*delivery_estimate.delivery_time
149
        
154
        
150
        # Increase the estimate if the actual stock is less than the threshold 
155
        # Increase the estimate if the actual stock is less than the threshold 
151
        if items_in_inventory < self.stock_threshold :
156
        if items_in_inventory < self.stock_threshold :
152
            delivery_time = delivery_time + self.time_to_producre
157
            delivery_time = delivery_time + self.time_to_procure
153
        
158
        
154
        #Further increase the estimate if it's late in the day
159
        #Further increase the estimate if it's late in the day
155
        if self.cutoff_time <= datetime.datetime.now().hour:
160
        current_hour = datetime.datetime.now().hour
-
 
161
        if type == DeliveryType.PREPAID and self.cutoff_time <= current_hour:
-
 
162
            delivery_time = delivery_time + 24
-
 
163
        elif type == DeliveryType.COD and self.cod_cutoff_time <= current_hour:
156
            delivery_time = delivery_time + 24
164
            delivery_time = delivery_time + 24
157
        
165
        
158
        delivery_time = int(math.ceil(delivery_time/24.0))
166
        delivery_time = int(math.ceil(delivery_time/24.0))
159
        
167
        
160
        
168
        '''
161
        ## FIXME Due to Kanwaar on the delhi Dehradun route deliveries on following pin codes will be effected.
169
        ## FIXME Due to Kanwaar on the delhi Dehradun route deliveries on following pin codes will be effected.
162
        ## Changing as per ticket number #462
170
        ## Changing as per ticket number #462
163
        if destination_pin in self.kanwaar_effected_pins_by_two_days:
171
        if destination_pin in self.kanwaar_effected_pins_by_two_days:
164
            delivery_time += 2;
172
            delivery_time += 2
165
        elif destination_pin in self.kanwaar_effected_pins_by_one_day:
173
        elif destination_pin in self.kanwaar_effected_pins_by_one_day:
166
            delivery_time += 1;
174
            delivery_time += 1
167
        ## FIXME Remove the above code once Kanwaar season is over    
175
        ## FIXME Remove the above code once Kanwaar season is over
-
 
176
        '''
168
        
177
        
169
        logistics_info = LogisticsInfo()
178
        logistics_info = LogisticsInfo()
170
        logistics_info.deliveryTime = delivery_time
179
        logistics_info.deliveryTime = delivery_time
171
        logistics_info.providerId = delivery_estimate.provider_id
180
        logistics_info.providerId = delivery_estimate.provider_id
172
        logistics_info.warehouseId = warehouse_id
181
        logistics_info.warehouseId = warehouse_id
Line 216... Line 225...
216
        """
225
        """
217
        try:
226
        try:
218
            return get_holidays(fromDate, toDate)
227
            return get_holidays(fromDate, toDate)
219
        finally:
228
        finally:
220
            close_session()
229
            close_session()
-
 
230
    
-
 
231
    def isCodAllowed(self, destination_pincode):
-
 
232
        """
-
 
233
        Returns true if COD is allowed for this destination pincode
-
 
234
        
-
 
235
        Parameters:
-
 
236
         - destination_pincode
-
 
237
        """
-
 
238
        try:
-
 
239
            return is_cod_allowed(destination_pincode)
-
 
240
        finally:
-
 
241
            close_session()
221
        
242
        
222
    def closeSession(self, ):
243
    def closeSession(self, ):
223
        close_session()
244
        close_session()