Subversion Repositories SmartDukaan

Rev

Rev 5957 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5944 mandeep.dh 1
'''
2
Created on 27-Apr-2010
3
 
4
@author: ashish
5
'''
6
from shop2020.config.client.ConfigClient import ConfigClient
7
from shop2020.model.v1.inventory.impl import DataService
8
from shop2020.model.v1.inventory.impl.Convertors import \
9
    to_t_item_inventory, to_t_warehouse, to_t_vendor_item_pricing, \
10
    to_t_vendor, to_t_vendor_item_mapping
11
from shop2020.model.v1.inventory.impl.DataAcessors import add_warehouse, \
12
    update_inventory, retire_warehouse, get_item_availability_for_warehouse, \
13
    get_item_availability_for_location, get_all_warehouses_by_status, get_Warehouse, \
14
    get_all_items_for_warehouse, close_session, add_vendor, get_item_inventory_by_item_id, \
15
    reserve_item_in_warehouse, reduce_reservation_count, get_all_item_pricing, \
16
    add_vendor_pricing, get_item_pricing, get_all_vendors, get_item_mappings, \
17
    add_vendor_item_mapping, update_inventory_history, is_alive, add_inventory, \
18
    add_bad_inventory, mark_missed_inventory_updates_as_processed, update_vendor_string, \
19
    get_item_keys_to_be_processed, reset_availability, get_shipping_locations,\
20
    initialize, get_inventory_snapshot, clear_item_availability_cache, \
21
    reset_availability_for_warehouse, get_vendor
22
from shop2020.model.v1.inventory.impl.DataService import Warehouse, \
23
    MissedInventoryUpdate, VendorItemMapping
24
from shop2020.thriftpy.model.v1.inventory.ttypes import InventoryServiceException, WarehouseType, InventoryType
25
from shop2020.utils.Utils import log_entry, to_java_date
26
 
27
class InventoryServiceHandler:
28
    '''
29
    classdocs
30
    '''
31
 
32
    def __init__(self, dbname='catalog', db_hostname='localhost'):
33
        '''
34
        Constructor
35
        '''
36
        initialize(dbname, db_hostname)
37
        try:
38
            config_client = ConfigClient()
39
            self.latest_arrivals_limit = int(config_client.get_property("LATEST_ARRIVALS_LIMIT"));
40
            self.best_sellers_limit = int(config_client.get_property("BEST_SELLERS_LIMIT"))
41
        except:
42
            self.latest_arrivals_limit = 50
43
            self.best_sellers_limit = 50
44
 
45
    def addWarehouse(self, warehouse):
46
        """
47
        Parameters:
48
         - warehouse
49
        """
50
        log_entry(self, "addWarehouse called")
51
        try:
52
            return add_warehouse(warehouse)
53
        finally:
54
            close_session()
55
 
56
    def updateInventoryHistory(self, warehouse_id, timestamp, availability):
57
        """
58
        Stores the incremental warehouse updates of items.
59
 
60
        Parameters:
61
         - warehouse_id
62
         - timestamp
63
         - availability
64
        """
65
        try:
66
            return update_inventory_history(warehouse_id, timestamp, availability)
67
        finally:
68
            close_session()
69
 
70
    def updateInventory(self, warehouse_id, timestamp, availability):
71
        """
72
        Parameters:
73
         - warehouse_id
74
         - timestamp
75
         - availability
76
        """
77
        log_entry(self, "update Inventory called")
78
        try:
79
            return update_inventory(warehouse_id, timestamp, availability)
80
        finally:
81
            close_session()
82
 
83
    def addInventory(self, itemId, warehouseId, quantity):
84
        """
85
        Add the inventory to existing stock.
86
 
87
        Parameters:
88
         - itemId
89
         - warehouseId
90
         - quantity
91
        """
92
        log_entry(self, "add Inventory called")
93
        try:
94
            return add_inventory(itemId, warehouseId, quantity)
95
        finally:
96
            close_session()
97
 
98
    def retireWarehouse(self, warehouse_id):
99
        """
100
        Parameters:
101
         - warehouse_id
102
        """
103
        log_entry(self, "retire warehouse called")
104
        try:
105
            return retire_warehouse(warehouse_id)
106
        finally:
107
            close_session()
108
 
109
 
110
    def getItemInventoryByItemId(self, item_id):
111
        """
112
        Parameters:
113
         - item_id
114
        """
115
        log_entry(self, "item inventory requested")
116
        try:
117
            inventory = get_item_inventory_by_item_id(item_id)
118
            return to_t_item_inventory(inventory, item_id)
119
        finally:
120
            close_session()
121
 
122
    def getItemAvailabilityAtLocation(self, itemId):
123
        """
124
        Determines the warehouse that should be used to fulfil an order for the given item.
125
        It first checks all the warehouses which are in the logistics location given by the
126
        warehouse_loc parameter. If none of the warehouses there have any inventory, then the
127
        preferred warehouse for the item is used.
128
 
129
        Returns an ordered list of size 4 with following elements in the given order:
130
        1. Logistics location of the warehouse which was finally picked up to ship the order.
131
        2. Id of the warehouse which was finally picked up.
132
        3. Inventory size in the selected warehouse.
133
        4. Expected delay added by the category manager.
134
 
135
        Parameters:
136
         - itemId
137
        """
138
        try:
139
            return get_item_availability_for_location(itemId)
140
        finally:
141
            close_session()
142
 
143
    def getItemAvailibilityAtWarehouse(self, warehouse_id, item_id):
144
        """
145
        Parameters:
146
         - warehouse_id
147
         - item_id
148
        """
149
        log_entry(self, "item availability at warehouse requested")
150
        try:
151
            return get_item_availability_for_warehouse(warehouse_id, item_id)
152
        finally:
153
            close_session()
154
 
155
    def getItemInventoryHistory(self, id, item_id, warehouse_id, from_date, to_date):
156
        """
157
        Parameters:
158
         - id
159
         - item_id
160
         - warehouse_id
161
         - from_date
162
         - to_date
163
        """
164
        pass
165
 
166
    def getAllWarehouses(self, isActive):
167
        """
168
        Parameters:
169
        - isActive
170
        """
171
        try:
172
            if isActive:
173
                warehouses = get_all_warehouses_by_status(3)
174
            else:
175
                warehouses = get_all_warehouses_by_status(None)
176
 
177
            ret_warehouses = []
178
 
179
            for warehouse in warehouses:
180
                ret_warehouses.append(to_t_warehouse(warehouse))
181
            return ret_warehouses
182
        finally:
183
            close_session()
184
 
185
    def getWarehouse(self, warehouse_id):
186
        """
187
        Parameters:
188
         - warehouse_id
189
        """
190
        log_entry(self, "get warehouse call")
191
        try:
192
            warehouse = get_Warehouse(warehouse_id)
193
            if warehouse:
194
                return to_t_warehouse(warehouse)
195
            else:
196
                raise InventoryServiceException(101, "no such warehouse: " + str(warehouse_id))
197
        finally:
198
            close_session()
199
 
200
    def getAllItemsForWarehouse(self, warehouse_id):
201
        """
202
        Parameters:
203
         - warehouse_id
204
        """
205
        log_entry(self, "getting all items for warehouse")
206
        try:
207
            all_items = get_all_items_for_warehouse(warehouse_id)
208
            t_items = []
209
            for item in all_items:
210
                t_items.append(item.id)
211
            return t_items
212
        finally:
213
            close_session()
214
 
215
    def reserveItemInWarehouse(self, itemId, warehouseId, quantity):
216
        """
217
        Increases the reservation count for an item in a warehouse. Should always succeed normally.
218
 
219
        Parameters:
220
         - itemId
221
         - warehouseId
222
        """
223
        log_entry(self, "reserveItemInWarehouse called")
224
        try:
225
            return reserve_item_in_warehouse(itemId, warehouseId, quantity)
226
        finally:
227
            close_session()
228
 
229
    def reduceReservationCount(self, itemId, warehouseId, quantity):
230
        """
231
        Decreases the reservation count for an item in a warehouse. Should always succeed normally.
232
 
233
        Parameters:
234
         - itemId
235
         - warehouseId
236
        """
237
        log_entry(self, "reduceReservationCount called")
238
        try:
239
            return reduce_reservation_count(itemId, warehouseId, quantity)
240
        finally:
241
            close_session()
242
 
243
    def getItemPricing(self, itemId, vendorId):
244
        """
245
        Returns the pricing information of an item associated with the vendor of the given warehouse.
246
        Raises an exception if either the item, vendor or the associated pricing information can't be found.
247
 
248
        Parameters:
249
         - itemId
250
         - warehouseId
251
        """
252
        try:
253
            return to_t_vendor_item_pricing(get_item_pricing(itemId, vendorId))
254
        finally:
255
            close_session()
256
 
257
 
258
    def getAllItemPricing(self, itemId):
259
        """
260
        Returns list of the pricing information of an item associated an item.
261
        Raises an exception if the item can't be found.
262
 
263
        Parameters:
264
         - itemId
265
        """
266
        try:
267
            return [to_t_vendor_item_pricing(vid) for vid in get_all_item_pricing(itemId)]
268
        finally:
269
            close_session()
270
 
271
    def addVendorItemPricing(self, vendorItemPricing):
272
        """
273
        Adds vendor prices corresponding to the item. If pricing already exists then updates the prices. 
274
        Raises an exception if either the item or vendor can't be found corresponding to their ids.
275
 
276
        Parameters:
277
         - vendorItemPricing
278
        """
279
        log_entry(self, "updateVendorItemPricing called")
280
        try:
281
            return add_vendor_pricing(vendorItemPricing)
282
        finally:
283
            close_session()
284
 
285
    def getVendor(self, vendorId):
286
        """
287
        Return a vendor for id
288
        """
289
        try:
290
            return to_t_vendor(get_vendor(vendorId))
291
        finally:
292
            close_session()
293
 
294
 
295
    def getAllVendors(self, ):
296
        """
297
        Return list of all vendors
298
        """
299
        try:
300
            return [to_t_vendor(v) for v in get_all_vendors()]
301
        finally:
302
            close_session()
303
 
304
    def addVendorItemMapping(self, key, vendorItemMapping):
305
        """
306
        Adds VendorItemMapping. Updates VendorItemMapping if exists corresponding to the item key.
307
 
308
        Parameters:
309
         - key
310
         - vendorItemMapping
311
        """
312
        log_entry(self, "addVendorItemMapping called")
313
        try:
314
            return add_vendor_item_mapping(key, vendorItemMapping)
315
        finally:
316
            close_session()
317
 
318
    def getVendorItemMappings(self, itemId):
319
        """
320
        Returns the list of vendor item mapping corresponding to itemId passed as parameter.
321
        Raises an exception if item not found corresponding to itemId
322
 
323
        Parameters:
324
         - itemId
325
        """
326
        try:
327
            item_mappings = get_item_mappings(itemId)
328
            return [to_t_vendor_item_mapping(vim) for vim in item_mappings]
329
        finally:
330
            close_session()
331
 
332
    def isAlive(self, ):
333
        """
334
        For checking weather service is active alive or not. It also checks connectivity with database
335
        """
336
        try:
337
            return is_alive()
338
        finally:
339
            close_session()
340
 
341
    def closeSession(self, ):
342
        """
343
        For closing the open session in sqlalchemy
344
        """
345
        close_session()
346
 
347
    def addVendor(self, vendor):
348
        """
349
        add a new vendor
350
        """
351
        try:
352
            return add_vendor(vendor)
353
        finally:
354
            close_session()
355
 
356
    def getWarehouses(self, warehouseType, inventoryType, vendorId, billingWarehouseId, shippingWarehouseId):
357
        """
358
        This method returns all warehouses for a given warehosueType, inventoryType, vendor, billingWarehouse and shippingWarehouse.
359
        getWarehouses(WarehouseType.OURS, InventoryType.GOOD, 3, 7, 7) would return ours warehouse with GOOD type inventory for vendor 1 for billing warehouse 7 and shipping warehouse 7
360
        getWarehouses(WarehouseType.OURS, InventoryType.GOOD, 3, 7, 7) would return ours warehouse with GOOD type inventory for vendor 3 for billing warehouse 7 and shipping warehouse 7
361
        getWarehouses(null, null, 3, 7, 7) would return all type warehouses with all type inventory for vendor 3 for billing warehouse 7 and shipping warehouse 7
362
        getWarehouses(null, null, 0, 0, 7) would return all type warehouses with all type inventory for all vendors for all billing warehouses at shipping warehouse 7
363
 
364
        Parameters:
365
         - warehouseType
366
         - inventoryType
367
         - vendorId
368
         - billingWarehouseId
369
         - shippingWarehouseId
370
        """
371
        try:
372
            query = Warehouse.query
373
            if warehouseType is not None:
374
                query = query.filter_by(warehouseType = WarehouseType._VALUES_TO_NAMES[warehouseType])
375
            if inventoryType is not None:
376
                query = query.filter_by(inventoryType = InventoryType._VALUES_TO_NAMES[inventoryType])
377
            if vendorId:
378
                query = query.filter_by(vendor_id = vendorId)
379
            if billingWarehouseId:
380
                query = query.filter_by(billingWarehouseId = billingWarehouseId)
381
            if shippingWarehouseId:
382
                query = query.filter_by(shippingWarehouseId = shippingWarehouseId)
383
 
384
            return [to_t_warehouse(w) for w in query.all()]
385
        finally:
386
            close_session()
387
 
388
    def getItemKeysToBeProcessed(self, warehouseId):
389
        """
390
        Returns the list of item keys which need to be processed for a given warehouse.
391
        This is currently used by Support application to send item keys whose inventory needs
392
        to be updated from PLB
393
 
394
        Parameters:
395
         - warehouseId
396
        """
397
        try:
398
            return get_item_keys_to_be_processed(warehouseId)
399
        finally:
400
            close_session()
401
 
402
    def markMissedInventoryUpdatesAsProcessed(self, itemKey, warehouseId):
403
        """
404
        Marks/Deletes missed inventory updates for a given key and warehouse.
405
        This generally happens when updates from PLB are applied on the currentinventorysnapshot for an item
406
 
407
        Parameters:
408
         - itemKey
409
         - warehouseId
410
        """
411
        try:
412
            mark_missed_inventory_updates_as_processed(itemKey, warehouseId)
413
        finally:
414
            close_session()
415
 
416
    def resetAvailability(self, itemKey, vendorId, quantity, warehouseId):
417
        """
418
        Resets availability of an item to the quantity mentioned in a warehouse.
419
 
420
        Parameters:
421
         - itemKey
422
         - vendorId
423
         - quantity
424
         - warehouseId
425
        """
426
        try:
427
            reset_availability(itemKey, vendorId, quantity, warehouseId)
428
        finally:
429
            close_session()
430
 
431
    def resetAvailabilityForWarehouse(self, warehouseId):
432
        """
433
        Resets availability of a warehouse to zero.
434
 
435
        Parameters:
436
         - warehouseId
437
        """
438
        try:
439
            reset_availability_for_warehouse(warehouseId)
440
        finally:
441
            close_session()
442
 
443
    def getIgnoredItemKeys(self, ):
444
        """
445
        Returns all the item key mappings that have been ignored until date. Value of map has the warehouse id
446
        and the timestamp from where alert was raised.
447
        """
448
        try:
449
            itemKeysMap = {}
450
            for key in MissedInventoryUpdate.query.filter_by(isIgnored = 1).all():
451
                itemKeysMap[key.itemKey] = { key.warehouseId : to_java_date(key.timestamp) }
452
 
453
            return itemKeysMap
454
        finally:
455
            close_session()
456
 
457
    def addBadInventory(self, itemId, warehouseId, quantity):
458
        """
459
        Add the BAD type inventory to existing stock.
460
 
461
        Parameters:
462
         - itemId
463
         - warehouseId
464
         - quantity
465
        """
466
        try:
467
            add_bad_inventory(itemId, warehouseId, quantity)
468
        finally:
469
            close_session()
470
 
471
    def getShippingLocations(self, ):
472
        """
473
        Returns all shipping locations
474
        """
475
        try:
476
            return [to_t_warehouse(w) for w in get_shipping_locations()]
477
        finally:
478
            close_session()
479
 
480
    def getAllVendorItemMappings(self, ):
481
        """
482
        Fetches all the vendor item mappings present.
483
        """
484
        try:
485
            return [to_t_vendor_item_mapping(m) for m in VendorItemMapping.query.all()]
486
        finally:
487
            close_session()
488
 
489
    def getInventorySnapshot(self, warehouseId):
490
        """
491
        Gets items' inventory for a warehouse
492
        If warehouse is passed as zero, items' inventory across all warehouses is sent
493
 
494
        Parameters:
495
         - warehouseId
496
        """
497
        try:
498
            resultMap = {}
499
            itemInventoryMap = get_inventory_snapshot(warehouseId)
500
            for key, value in itemInventoryMap.items():
501
                resultMap[key] = to_t_item_inventory(value, key)
502
 
503
            return resultMap
504
        finally:
505
            close_session()
506
 
507
    def clearItemAvailabilityCache(self, ):
508
        """
509
        Clear item availability cache.
510
        """
511
        try:
512
            clear_item_availability_cache()
513
        finally:
514
            close_session()
515
 
516
    def updateVendorString(self, warehouseId, vendorString):
517
        """
518
        Parameters:
519
         - warehouseId
520
         - vendorString
521
        """
522
        try:
523
            update_vendor_string(warehouseId, vendorString)
524
        finally:
525
            close_session()
526