Subversion Repositories SmartDukaan

Rev

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

Rev 576 Rev 644
Line 1... Line 1...
1
'''
1
'''
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 Shipment, LogisticsInfo
6
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo,\
-
 
7
    LogisticsServiceException
7
from shop2020.logistics.service.impl.DataAccessor import add_empty_AWBs, get_empty_AWB,\
8
from shop2020.logistics.service.impl.DataAccessor import get_empty_AWB,\
8
    get_provider, get_shipment_info, get_shipments, get_todays_shipments, initialize,\
-
 
9
    get_providers, create_shipment, update_shipment_status, get_logistics_estimation, add_delivery_estimate,\
-
 
10
    add_pincode_warehouse_mapping, get_logistics_info
9
    get_shipment_info, initialize, get_logistics_estimation
11
from shop2020.logistics.service.impl.Converters import to_t_provider,\
10
from shop2020.logistics.service.impl.Converters import to_t_awbupdate
12
    to_t_shipment, to_t_shipmentstatusinfo, to_t_shipmentupdate, to_t_itemLogistics
-
 
13
from shop2020.clients.InventoryClient import InventoryClient
11
from shop2020.clients.InventoryClient import InventoryClient
14
from shop2020.config.client.ConfigClient import ConfigClient
12
from shop2020.config.client.ConfigClient import ConfigClient
15
from shop2020.utils.Utils import log_entry, to_py_date
-
 
16
import datetime
13
import datetime
17
import time
14
import math
18
 
-
 
19
 
15
 
20
class LogisticsServiceHandler:
16
class LogisticsServiceHandler:
21
    
17
    
22
    def __init__(self):
18
    def __init__(self):
23
        initialize()
19
        initialize()
-
 
20
        config_client = ConfigClient()
-
 
21
        self.cutoff_time = int(config_client.get_property('delivery_cutoff_time'))
-
 
22
        self.stock_threshold = int(config_client.get_property('inventory_stock_threshold'))
-
 
23
        self.time_to_producre = int(config_client.get_property('inventory_time_to_procure'))
24
 
24
 
25
    #FIXME: This method currently ignores sku_id. The implementation should make use of the functionality present in getLogisticsEstimation.
-
 
26
    def getLogisticsInfo(self, destination_pincode, sku_id):
25
    def getLogisticsInfo(self, destination_pincode, itemId):
27
        """
26
        """
28
        Parameters:
27
        Parameters:
29
         - destination_pincode
28
         - destination_pincode
30
         - sku_id
29
         - sku_id
31
        """
30
        """
32
        warehouse_id, provider_id, airway_billno, delivery_time = get_logistics_info(destination_pincode, sku_id)
-
 
33
        logistics_info = LogisticsInfo()
31
        logistics_info = self.getLogisticsEstimation(itemId, destination_pincode)
34
        logistics_info.airway_billno = airway_billno
32
        logistics_info.airway_billno = get_empty_AWB(logistics_info.providerId)
35
        logistics_info.warehouse_id = warehouse_id
-
 
36
        logistics_info.delivery_estimate = delivery_time
-
 
37
        logistics_info.provider_id = provider_id
-
 
38
        return logistics_info
33
        return logistics_info
39
    
-
 
40
    def addEmptyAWBs(self, numbers, provider_id):
-
 
41
        """
-
 
42
        Parameters:
-
 
43
         - numbers
-
 
44
         - provider_id
-
 
45
        """
-
 
46
        add_empty_AWBs(numbers, provider_id)
-
 
47
 
34
 
48
    def getEmptyAWB(self, provider_id):
35
    def getEmptyAWB(self, provider_id):
49
        """
36
        """
50
        Parameters:
37
        Parameters:
51
         - provider_id
38
         - provider_id
52
        """
39
        """
53
        return get_empty_AWB(provider_id)
40
        return get_empty_AWB(provider_id)
54
    
-
 
55
    def getProviders(self):
-
 
56
        """
-
 
57
        Parameters:
-
 
58
        """
-
 
59
        log_entry(self, "all providers requested")
-
 
60
        
-
 
61
        providers = get_providers()
-
 
62
        ret_providers = []
-
 
63
        for provider in providers:
-
 
64
            if provider:
-
 
65
                ret_providers.append(to_t_provider(provider))
-
 
66
        return ret_providers
-
 
67
        
-
 
68
    
-
 
69
    def getProvider(self, provider_id):
-
 
70
        """
-
 
71
        Parameters:
-
 
72
         - provider_id
-
 
73
        """
-
 
74
        return to_t_provider(get_provider(provider_id))
-
 
75
    
-
 
76
    def createShipment(self, shipment):
-
 
77
        """
-
 
78
        Parameters:
-
 
79
         - shipment
-
 
80
        """
-
 
81
        return create_shipment(shipment)
-
 
82
    
-
 
83
    
-
 
84
    def updateShipmentStatus(self, awb, shipment_update):
-
 
85
        """
-
 
86
        Parameters:
-
 
87
         - awb
-
 
88
         - shipment_update
-
 
89
        """
-
 
90
        return update_shipment_status(awb, shipment_update)
-
 
91
 
-
 
92
 
41
 
93
    def getShipmentInfo(self, awb):
42
    def getShipmentInfo(self, awb, providerId):
94
        """
43
        """
95
        Parameters:
44
        Parameters:
96
         - awb
45
         - awb
97
        """
46
        """
98
        return to_t_shipmentstatusinfo(get_shipment_info(awb))
47
        awb_updates = get_shipment_info(awb, providerId)
99
    
-
 
100
    def getShipments(self, warehouse_id, from_date, to_date, provider_id):
-
 
101
        """
-
 
102
        Parameters:
-
 
103
         - warehouse_id
-
 
104
         - from_date
-
 
105
         - to_date
-
 
106
         - provider_id
-
 
107
        """
-
 
108
        log_entry(self, "all shipments requested")
-
 
109
        
-
 
110
        from_datetime = to_py_date(from_date)
-
 
111
        to_datetime = to_py_date(to_date)
-
 
112
        
-
 
113
        shipments = get_shipments(warehouse_id, from_datetime, to_datetime, provider_id)
-
 
114
        ret_shipments = []
48
        t_updates = []
115
        for shipment in shipments:
49
        for update in awb_updates:
116
            if shipment:
-
 
117
                ret_shipments.append(to_t_shipment(shipment))
50
            t_updates.append(to_t_awbupdate(update))
118
        return ret_shipments
51
        return t_updates
119
 
52
 
120
    def getLogisticsEstimation(self, itemId, destination_pin, provider_id):
53
    def getLogisticsEstimation(self, itemId, destination_pin):
121
        """
54
        """
122
        Parameters:
55
        Parameters:
123
         - itemId
56
         - itemId
124
         - destination_pin
57
         - destination_pin
125
        """
58
        """
126
        
-
 
127
        client = InventoryClient().get_client()
-
 
128
        config_client = ConfigClient()
-
 
129
        
-
 
130
        cutoff_time = int(config_client.get_property('delivery_cutoff_time'))
-
 
131
        stock_threshold = int(config_client.get_property('inventory_stock_threshold'))
-
 
132
        
-
 
133
        
-
 
134
        delivery_estimate, warehouse_id = get_logistics_estimation(itemId, destination_pin, provider_id)
59
        delivery_estimate = get_logistics_estimation(destination_pin)
135
        delivery_time = delivery_estimate.delivery_time
60
        delivery_time = 24*delivery_estimate.delivery_time
136
        
-
 
137
        items_in_inventory = client.getItemAvailibilityAtWarehouse(warehouse_id, itemId)
-
 
138
        now = datetime.datetime.now()
61
        if self.cutoff_time <= datetime.datetime.now().hour:
139
        print now.hour
-
 
140
        if cutoff_time < now.hour:
-
 
141
            delivery_time = delivery_time + 24
62
            delivery_time = delivery_time + 24
142
        if items_in_inventory < stock_threshold :
-
 
143
            time_to_procure = int(config_client.get_property('inventory_time_to_procure'))
-
 
144
            delivery_time = delivery_time + time_to_procure
63
        warehouse_loc = delivery_estimate.warehouse_location
145
 
-
 
146
        return to_t_itemLogistics(delivery_time, items_in_inventory, warehouse_id)
-
 
147
 
-
 
148
    def addDeliveryEstimate(self, warahouse_id, destination_pin, provider_id, delivery_time, reliability):
-
 
149
        """
64
        try:
150
        Parameters:
-
 
151
         - warahouse_id
-
 
152
         - destination_pin
65
            client = InventoryClient().get_client()
153
         - provider_id
-
 
154
         - delivery_time
-
 
155
        """
-
 
156
        add_delivery_estimate(warahouse_id, destination_pin, provider_id, delivery_time, reliability)
66
            warehouse_id, items_in_inventory = client.getItemAvailabilityAtLocation(warehouse_loc, itemId)
157
        
67
        except:
158
    def addPincodeWarehouseMapping(self, pincode, warehouse_id, warehouse_pin):
68
            raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.") 
159
        """
-
 
160
        Parameters:
-
 
161
         - pincode
-
 
162
         - warehouse_id
69
        if items_in_inventory < self.stock_threshold :
163
         - warehouse_name
70
            delivery_time = delivery_time + self.time_to_producre
164
        """
-
 
165
        add_pincode_warehouse_mapping(pincode, warehouse_id, warehouse_pin)
71
        delivery_time = int(math.ceil(delivery_time/24.0))
166
        
72
        
167
    def getTodaysShipments(self, warehouse_id, provider_id):
-
 
168
        """
-
 
169
        Parameters:
-
 
170
         - warehouse_id
-
 
171
         - provider_id
-
 
172
        """
-
 
173
        
-
 
174
        log_entry(self, "all shipments requested for today")
-
 
175
        
-
 
176
        shipments = get_todays_shipments(warehouse_id, provider_id)
-
 
177
        ret_shipments = []
-
 
178
        for shipment in shipments:
-
 
179
            if shipment:
-
 
180
                ret_shipments.append(to_t_shipment(shipment))
-
 
181
        return ret_shipments
-
 
182
 
-
 
183
'''
-
 
184
        shipments = []
-
 
185
        i = 0
-
 
186
        while i < 100:
-
 
187
            shipment = Shipment()
73
        logistics_info = LogisticsInfo()
188
            shipment.warehouse_name = "Seattle"
-
 
189
            shipment.awb="1234"+str(i)
-
 
190
            shipment.destination="JP"
-
 
191
            shipment.origin = "US"
-
 
192
            shipment.recepient_address = "1, Microsoft Way, Redmond"
-
 
193
            shipment.recepient_name = "Steve Balmer"
74
        logistics_info.deliveryTime = delivery_time
194
            shipment.recepient_phone = "+14356368790"
-
 
195
            shipment.recepient_pincode = "110001"
75
        logistics_info.providerId = delivery_estimate.provider_id
196
            shipment.shipment_contents = "iPhone 4G"
76
        logistics_info.warehouseId = warehouse_id
197
            shipment.shipment_weight = "300 gms"
-
 
198
            shipments.append(shipment)
-
 
199
            i = i+1
-
 
200
        return shipments
-
 
201
        '''
-
 
202
        
77
        
203
        
-
 
204
78
        return logistics_info
-
 
79
205
80