Subversion Repositories SmartDukaan

Rev

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