Subversion Repositories SmartDukaan

Rev

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

Rev 5712 Rev 5944
Line 1... Line 1...
1
'''
1
'''
2
Created on 27-Apr-2010
2
Created on 27-Apr-2010
3
 
3
 
4
@author: ashish
4
@author: ashish
5
'''
5
'''
6
from shop2020.clients.CatalogClient import CatalogClient
-
 
7
from shop2020.clients.PurchaseClient import PurchaseClient
-
 
8
from shop2020.config.client.ConfigClient import ConfigClient
6
from shop2020.config.client.ConfigClient import ConfigClient
9
from shop2020.model.v1.catalog.impl import DataService
-
 
10
from shop2020.model.v1.catalog.impl.Convertors import to_t_item, \
7
from shop2020.model.v1.catalog.impl.Convertors import to_t_item, \
11
    to_t_item_inventory, to_t_warehouse, to_t_vendor_item_pricing, to_t_category, \
-
 
12
    to_t_vendor, to_t_vendor_item_mapping, to_t_source_item_pricing, \
8
    to_t_category, to_t_source_item_pricing, to_t_product_notification_request, \
13
    to_t_product_notification_request, to_t_product_notification_request_count, \
9
    to_t_product_notification_request_count, to_t_voucher_item_mapping
14
    to_t_voucher_item_mapping
-
 
15
from shop2020.model.v1.catalog.impl.DataAcessors import add_item, add_warehouse, \
10
from shop2020.model.v1.catalog.impl.DataAcessors import add_item, \
16
    update_inventory, retire_warehouse, retire_item, change_item_status, get_item, \
-
 
17
    get_all_items, get_all_items_by_status, get_item_availability_for_warehouse, \
11
    retire_item, change_item_status, get_item, get_all_items, get_all_items_by_status, \
18
    get_item_availability_for_location, get_all_warehouses_by_status, get_Warehouse, \
-
 
19
    get_all_warehouses_for_item, get_all_items_for_warehouse, update_item, \
12
    update_item, start_item_on, close_session, retire_item_on, \
20
    start_item_on, close_session, add_vendor, retire_item_on, get_item_inventory_by_item_id, \
-
 
21
    get_items_by_catalog_id, get_best_deals, get_best_deals_catalog_ids, \
13
    get_items_by_catalog_id, get_best_deals, get_best_deals_catalog_ids, \
22
    get_best_deals_count, get_best_sellers, get_latest_arrivals, \
14
    get_best_deals_count, get_best_sellers, get_latest_arrivals, \
23
    get_latest_arrivals_catalog_ids, get_latest_arrivals_count, is_active, \
15
    get_latest_arrivals_catalog_ids, get_latest_arrivals_count, is_active, \
24
    get_best_sellers_catalog_ids, get_best_sellers_count, put_category_object, \
16
    get_best_sellers_catalog_ids, get_best_sellers_count, put_category_object, \
25
    get_category_object, mark_item_as_content_complete, reserve_item_in_warehouse, \
17
    get_category_object, mark_item_as_content_complete, generate_new_entity_id, \
26
    reduce_reservation_count, generate_new_entity_id, get_all_item_pricing, \
-
 
27
    add_vendor_pricing, get_item_pricing, get_category, get_all_categories, \
-
 
28
    add_category, get_item_status_description, get_all_vendors, get_item_mappings, \
18
    get_category, get_all_categories, add_category, get_item_status_description, \
29
    add_vendor_item_mapping, check_similar_item, change_risky_flag, \
-
 
30
    get_items_for_mastersheet, get_risky_items, get_similar_items_catalog_ids, \
19
    check_similar_item, change_risky_flag, get_items_for_mastersheet, get_risky_items, \
31
    add_product_notification, send_product_notifications, update_inventory_history, \
20
    get_similar_items_catalog_ids, add_product_notification, send_product_notifications, \
32
    get_all_brands_by_category, is_alive, get_item_pricing_by_source, \
21
    get_all_brands_by_category, is_alive, get_item_pricing_by_source, \
33
    add_source_item_pricing, get_all_sources, get_item_for_source, \
22
    add_source_item_pricing, get_all_sources, get_item_for_source, \
34
    get_all_source_pricing, get_item_count_by_status, search_items, \
23
    get_all_source_pricing, get_item_count_by_status, search_items, \
35
    get_search_result_count, get_pending_orders_inventory, get_product_notifications, \
24
    get_search_result_count, get_product_notifications, get_product_notification_request_count, \
36
    get_product_notification_request_count, add_inventory, \
-
 
37
    get_all_similar_items_catalog_ids, add_similar_item_catalog_id, \
25
    get_all_similar_items_catalog_ids, add_similar_item_catalog_id, \
38
    delete_similar_item_catalog_id, add_authorization_log_for_item, \
26
    delete_similar_item_catalog_id, add_authorization_log_for_item, \
39
    get_thrift_item_list, get_all_brands, add_bad_inventory, \
27
    get_thrift_item_list, get_all_brands, get_coming_soon, get_coming_soon_catalog_ids, \
40
    mark_missed_inventory_updates_as_processed, update_vendor_string, \
-
 
41
    get_item_keys_to_be_processed, reset_availability, get_shipping_locations,\
-
 
42
    get_coming_soon, get_coming_soon_catalog_ids, get_coming_soon_count,\
-
 
43
    initialize, get_inventory_snapshot, clear_item_availability_cache, \
-
 
44
    reset_availability_for_warehouse, get_clearance_sale_catalog_ids, \
28
    get_coming_soon_count, initialize, get_clearance_sale_catalog_ids, \
45
    add_update_voucher_for_item, delete_voucher_for_item, get_voucher_amount, \
29
    add_update_voucher_for_item, delete_voucher_for_item, get_voucher_amount, \
46
    get_all_vouchers_for_item, is_valid_catalog_id
30
    get_all_vouchers_for_item, is_valid_catalog_id, check_risky_item
47
from shop2020.model.v1.catalog.impl.DataService import Warehouse, \
-
 
48
    MissedInventoryUpdate, VendorItemMapping
-
 
49
from shop2020.thriftpy.model.v1.catalog.ttypes import status, \
31
from shop2020.thriftpy.model.v1.catalog.ttypes import status
50
    InventoryServiceException, AvailableAndReservedStock, WarehouseType, InventoryType
-
 
51
from shop2020.thriftpy.purchase.ttypes import PurchaseOrder, LineItem
-
 
52
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
32
from shop2020.utils.Utils import log_entry, to_py_date
53
import datetime
33
import datetime
54
import xlrd
-
 
55
 
34
 
56
 
-
 
57
 
-
 
58
class InventoryServiceHandler:
35
class CatalogServiceHandler:
59
    '''
36
    '''
60
    classdocs
37
    classdocs
61
    '''
38
    '''
62
    
39
    
63
    def __init__(self, dbname='catalog', db_hostname='localhost'):
40
    def __init__(self, dbname='catalog', db_hostname='localhost'):
Line 85... Line 62...
85
        try:
62
        try:
86
            return add_item(item)
63
            return add_item(item)
87
        finally:
64
        finally:
88
            close_session()
65
            close_session()
89
 
66
 
90
    def addWarehouse(self, warehouse):
-
 
91
        """
-
 
92
        Parameters:
-
 
93
         - warehouse
-
 
94
        """
-
 
95
        log_entry(self, "addWarehouse called")
-
 
96
        try:
-
 
97
            return add_warehouse(warehouse)
-
 
98
        finally:
-
 
99
            close_session()
-
 
100
 
-
 
101
    def updateInventoryHistory(self, warehouse_id, timestamp, availability):
-
 
102
        """
-
 
103
        Stores the incremental warehouse updates of items.
-
 
104
        
-
 
105
        Parameters:
-
 
106
         - warehouse_id
-
 
107
         - timestamp
-
 
108
         - availability
-
 
109
        """
-
 
110
        try:
-
 
111
            return update_inventory_history(warehouse_id, timestamp, availability)
-
 
112
        finally:
-
 
113
            close_session()
-
 
114
 
-
 
115
    def updateInventory(self, warehouse_id, timestamp, availability):
-
 
116
        """
-
 
117
        Parameters:
-
 
118
         - warehouse_id
-
 
119
         - timestamp
-
 
120
         - availability
-
 
121
        """
-
 
122
        log_entry(self, "update Inventory called")
-
 
123
        try:
-
 
124
            return update_inventory(warehouse_id, timestamp, availability)
-
 
125
        finally:
-
 
126
            close_session()
-
 
127
 
-
 
128
    def addInventory(self, itemId, warehouseId, quantity):
-
 
129
        """
-
 
130
        Add the inventory to existing stock.
-
 
131
    
-
 
132
        Parameters:
-
 
133
         - itemId
-
 
134
         - warehouseId
-
 
135
         - quantity
-
 
136
        """
-
 
137
        log_entry(self, "add Inventory called")
-
 
138
        try:
-
 
139
            return add_inventory(itemId, warehouseId, quantity)
-
 
140
        finally:
-
 
141
            close_session()
-
 
142
 
-
 
143
    def isActive(self, item_id):
67
    def isActive(self, item_id):
144
        """
68
        """
145
        Parameters:
69
        Parameters:
146
         - item_id
70
         - item_id
147
        """
71
        """
Line 157... Line 81...
157
        """
81
        """
158
        try:
82
        try:
159
            return get_item_status_description(itemId)
83
            return get_item_status_description(itemId)
160
        finally:
84
        finally:
161
            close_session()
85
            close_session()
162
 
-
 
163
    def retireWarehouse(self, warehouse_id):
-
 
164
        """
-
 
165
        Parameters:
-
 
166
         - warehouse_id
-
 
167
        """
-
 
168
        log_entry(self, "retire warehouse called")
-
 
169
        try:
-
 
170
            return retire_warehouse(warehouse_id)
-
 
171
        finally:
-
 
172
            close_session()
-
 
173
 
-
 
174
    
86
    
175
    def retireItem(self, item_id):
87
    def retireItem(self, item_id):
176
        """
88
        """
177
        Parameters:
89
        Parameters:
178
        - item_id
90
        - item_id
Line 293... Line 205...
293
                    ret_items.append(to_t_item(item))
205
                    ret_items.append(to_t_item(item))
294
            return ret_items
206
            return ret_items
295
        finally:
207
        finally:
296
            close_session()
208
            close_session()
297
 
209
 
298
    '''
-
 
299
    def getItemInventory(self, item_id):
-
 
300
        """
-
 
301
        Parameters:
-
 
302
        - item_id
-
 
303
        """
-
 
304
        log_entry(self, "item inventory requested")
-
 
305
        try:
-
 
306
            inventory = get_item_inventoy(item_id)
-
 
307
            return to_t_item_inventory(inventory, item_id)
-
 
308
        finally:
-
 
309
            close_session()
-
 
310
    '''
-
 
311
 
-
 
312
    def getItemInventoryByItemId(self, item_id):
-
 
313
        """
-
 
314
        Parameters:
-
 
315
         - item_id
-
 
316
        """
-
 
317
        log_entry(self, "item inventory requested")
-
 
318
        try:
-
 
319
            inventory = get_item_inventory_by_item_id(item_id)
-
 
320
            return to_t_item_inventory(inventory, item_id)
-
 
321
        finally:
-
 
322
            close_session()
-
 
323
 
-
 
324
    def getAllBrandsByCategory(self, categoryId):
210
    def getAllBrandsByCategory(self, categoryId):
325
        """
211
        """
326
        Parameters:
212
        Parameters:
327
         - categoryId
213
         - categoryId
328
        """
214
        """
Line 334... Line 220...
334
    def getAllBrands(self):
220
    def getAllBrands(self):
335
        try:
221
        try:
336
            return get_all_brands()
222
            return get_all_brands()
337
        finally:
223
        finally:
338
            close_session()
224
            close_session()
339
    
-
 
340
    '''
-
 
341
    def getItemInventoryByCatalogId(self, item_id):
-
 
342
        """
225
        
343
        Parameters:
-
 
344
         - item_id
-
 
345
        """
-
 
346
        log_entry(self, "item inventory requested")
-
 
347
        inventory = get_item_inventory_by_catalog_id(item_id)
-
 
348
        return to_t_item_inventory(inventory, item_id)
-
 
349
    '''
-
 
350
    
-
 
351
    def getAllItemsInRange(self, offset, limit):
226
    def getAllItemsInRange(self, offset, limit):
352
        """
227
        """
353
        Gets at most 'limit' items starting at the given offset. Returns an empty list if there are no more items at the given offset.
228
        Gets at most 'limit' items starting at the given offset. Returns an empty list if there are no more items at the given offset.
354
    
229
    
355
        Parameters:
230
        Parameters:
Line 389... Line 264...
389
        try:
264
        try:
390
            return get_item_count_by_status(useStatus, itemStatus)
265
            return get_item_count_by_status(useStatus, itemStatus)
391
        finally:
266
        finally:
392
            close_session()
267
            close_session()
393
 
268
 
394
    def getItemAvailabilityAtLocation(self, itemId):
-
 
395
        """
-
 
396
        Determines the warehouse that should be used to fulfil an order for the given item.
-
 
397
        It first checks all the warehouses which are in the logistics location given by the
-
 
398
        warehouse_loc parameter. If none of the warehouses there have any inventory, then the
-
 
399
        preferred warehouse for the item is used.
-
 
400
        
-
 
401
        Returns an ordered list of size 4 with following elements in the given order:
-
 
402
        1. Logistics location of the warehouse which was finally picked up to ship the order.
-
 
403
        2. Id of the warehouse which was finally picked up.
-
 
404
        3. Inventory size in the selected warehouse.
-
 
405
        4. Expected delay added by the category manager.
-
 
406
        
-
 
407
        Parameters:
-
 
408
         - itemId
-
 
409
        """
-
 
410
        try:
-
 
411
            return get_item_availability_for_location(itemId)
-
 
412
        finally:
-
 
413
            close_session()
-
 
414
            
-
 
415
    def getItemAvailibilityAtWarehouse(self, warehouse_id, item_id):
-
 
416
        """
-
 
417
        Parameters:
-
 
418
         - warehouse_id
-
 
419
         - item_id
-
 
420
        """
-
 
421
        log_entry(self, "item availability at warehouse requested")
-
 
422
        try:
-
 
423
            return get_item_availability_for_warehouse(warehouse_id, item_id)
-
 
424
        finally:
-
 
425
            close_session()
-
 
426
            
-
 
427
    def markItemAsContentComplete(self, entityId, category, brand, modelName, modelNumber):
269
    def markItemAsContentComplete(self, entityId, category, brand, modelName, modelNumber):
428
        """
270
        """
429
        Parameters:
271
        Parameters:
430
         - entityId
272
         - entityId
431
         - category
273
         - category
Line 435... Line 277...
435
        """
277
        """
436
        try:
278
        try:
437
            return mark_item_as_content_complete(entityId, category, brand, modelName, modelNumber)
279
            return mark_item_as_content_complete(entityId, category, brand, modelName, modelNumber)
438
        finally:
280
        finally:
439
            close_session()
281
            close_session()
440
 
-
 
441
 
-
 
442
    def getItemInventoryHistory(self, id, item_id, warehouse_id, from_date, to_date):
-
 
443
        """
-
 
444
        Parameters:
-
 
445
         - id
-
 
446
         - item_id
-
 
447
         - warehouse_id
-
 
448
         - from_date
-
 
449
         - to_date
-
 
450
        """
-
 
451
        pass
-
 
452
 
-
 
453
    def getAllWarehouses(self, isActive):
-
 
454
        """
-
 
455
        Parameters:
-
 
456
        - isActive
-
 
457
        """
-
 
458
        try:
-
 
459
            if isActive:
-
 
460
                warehouses = get_all_warehouses_by_status(status.ACTIVE)
-
 
461
            else:
-
 
462
                warehouses = get_all_warehouses_by_status(None)
-
 
463
            
-
 
464
            ret_warehouses = []
-
 
465
            
-
 
466
            for warehouse in warehouses:
-
 
467
                ret_warehouses.append(to_t_warehouse(warehouse))
-
 
468
            return ret_warehouses
-
 
469
        finally:
-
 
470
            close_session()
-
 
471
            
-
 
472
    def getWarehouse(self, warehouse_id):
-
 
473
        """
-
 
474
        Parameters:
-
 
475
         - warehouse_id
-
 
476
        """
-
 
477
        log_entry(self, "get warehouse call")
-
 
478
        try:
-
 
479
            warehouse = get_Warehouse(warehouse_id)
-
 
480
            if warehouse:
-
 
481
                return to_t_warehouse(warehouse)
-
 
482
            else:
-
 
483
                raise InventoryServiceException(101, "no such warehouse: " + str(warehouse_id))
-
 
484
        finally:
-
 
485
            close_session()
-
 
486
            
-
 
487
    def getAllWarehousesForItem(self, item_id):
-
 
488
        """
-
 
489
        Parameters:
-
 
490
         - item_id
-
 
491
        """
-
 
492
        log_entry(self, "getting all warehouses for item")
-
 
493
        try:
-
 
494
            all_warehouses = get_all_warehouses_for_item(item_id)
-
 
495
            t_warehouses = []
-
 
496
            for warehouse in all_warehouses:
-
 
497
                t_warehouses.append(to_t_warehouse(warehouse))
-
 
498
            return t_warehouses
-
 
499
        finally:
-
 
500
            close_session()
-
 
501
            
-
 
502
    def getAllItemsForWarehouse(self, warehouse_id):
-
 
503
        """
-
 
504
        Parameters:
-
 
505
         - warehouse_id
-
 
506
        """
-
 
507
        log_entry(self, "getting all items for warehouse")
-
 
508
        try:
-
 
509
            all_items = get_all_items_for_warehouse(warehouse_id)
-
 
510
            t_items = []
-
 
511
            for item in all_items:
-
 
512
                t_items.append(to_t_item(item))
-
 
513
            return t_items
-
 
514
        finally:
-
 
515
            close_session()
-
 
516
 
-
 
517
    def reserveItemInWarehouse(self, itemId, warehouseId, quantity):
-
 
518
        """
-
 
519
        Increases the reservation count for an item in a warehouse. Should always succeed normally.
-
 
520
        
-
 
521
        Parameters:
-
 
522
         - itemId
-
 
523
         - warehouseId
-
 
524
        """
-
 
525
        log_entry(self, "reserveItemInWarehouse called")
-
 
526
        try:
-
 
527
            return reserve_item_in_warehouse(itemId, warehouseId, quantity)
-
 
528
        finally:
-
 
529
            close_session()
-
 
530
    
-
 
531
    def reduceReservationCount(self, itemId, warehouseId, quantity):
-
 
532
        """
-
 
533
        Decreases the reservation count for an item in a warehouse. Should always succeed normally.
-
 
534
        
-
 
535
        Parameters:
-
 
536
         - itemId
-
 
537
         - warehouseId
-
 
538
        """
-
 
539
        log_entry(self, "reduceReservationCount called")
-
 
540
        try:
-
 
541
            return reduce_reservation_count(itemId, warehouseId, quantity)
-
 
542
        finally:
-
 
543
            close_session()
-
 
544
            
282
            
545
    def updateItem(self, item):
283
    def updateItem(self, item):
546
        """
284
        """
547
        Parameters:
285
        Parameters:
548
         - item
286
         - item
Line 661... Line 399...
661
        try:
399
        try:
662
            return get_latest_arrivals_count(self.latest_arrivals_limit)
400
            return get_latest_arrivals_count(self.latest_arrivals_limit)
663
        finally:
401
        finally:
664
            close_session()
402
            close_session()
665
 
403
 
666
    def getItemPricing(self, itemId, vendorId):
-
 
667
        """
-
 
668
        Returns the pricing information of an item associated with the vendor of the given warehouse.
-
 
669
        Raises an exception if either the item, vendor or the associated pricing information can't be found.
-
 
670
        
-
 
671
        Parameters:
-
 
672
         - itemId
-
 
673
         - warehouseId
-
 
674
        """
-
 
675
        try:
-
 
676
            return to_t_vendor_item_pricing(get_item_pricing(itemId, vendorId))
-
 
677
        finally:
-
 
678
            close_session()
-
 
679
            
-
 
680
            
-
 
681
    def getAllItemPricing(self, itemId):
-
 
682
        """
-
 
683
        Returns list of the pricing information of an item associated an item.
-
 
684
        Raises an exception if the item can't be found.
-
 
685
        
-
 
686
        Parameters:
-
 
687
         - itemId
-
 
688
        """
-
 
689
        try:
-
 
690
            return [to_t_vendor_item_pricing(vid) for vid in get_all_item_pricing(itemId)]
-
 
691
        finally:
-
 
692
            close_session()
-
 
693
 
-
 
694
    def generateNewEntityID(self, ):
404
    def generateNewEntityID(self, ):
695
        try:
405
        try:
696
            return generate_new_entity_id()
406
            return generate_new_entity_id()
697
        finally:
407
        finally:
698
            close_session()
408
            close_session()
Line 737... Line 447...
737
        try:
447
        try:
738
            categories = get_all_categories()
448
            categories = get_all_categories()
739
            return [to_t_category(category) for category in categories]
449
            return [to_t_category(category) for category in categories]
740
        finally:
450
        finally:
741
            close_session()
451
            close_session()
742
 
-
 
743
 
-
 
744
    def addVendorItemPricing(self, vendorItemPricing):
-
 
745
        """
-
 
746
        Adds vendor prices corresponding to the item. If pricing already exists then updates the prices. 
-
 
747
        Raises an exception if either the item or vendor can't be found corresponding to their ids.
-
 
748
        
-
 
749
        Parameters:
-
 
750
         - vendorItemPricing
-
 
751
        """
-
 
752
        log_entry(self, "updateVendorItemPricing called")
-
 
753
        try:
-
 
754
            return add_vendor_pricing(vendorItemPricing)
-
 
755
        finally:
-
 
756
            close_session()
-
 
757
            
-
 
758
    def getAllVendors(self, ):
-
 
759
        """
-
 
760
        Return list of all vendors
-
 
761
        """
-
 
762
        try:
-
 
763
            return [to_t_vendor(v) for v in get_all_vendors()]
-
 
764
        finally:
-
 
765
            close_session()
-
 
766
                
452
                
767
    def getAllSimilarItems(self, itemId):
453
    def getAllSimilarItems(self, itemId):
768
        """
454
        """
769
        Returns list of similar items.
455
        Returns list of similar items.
770
        
456
        
Line 846... Line 532...
846
        """
532
        """
847
        try:
533
        try:
848
            return delete_voucher_for_item(catalogItemId, voucherType)
534
            return delete_voucher_for_item(catalogItemId, voucherType)
849
        finally:
535
        finally:
850
            close_session()
536
            close_session()
851
    
-
 
852
    def addVendorItemMapping(self, key, vendorItemMapping):
-
 
853
        """
-
 
854
        Adds VendorItemMapping. Updates VendorItemMapping if exists corresponding to the item key.
-
 
855
        
-
 
856
        Parameters:
-
 
857
         - key
-
 
858
         - vendorItemMapping
-
 
859
        """
-
 
860
        log_entry(self, "addVendorItemMapping called")
-
 
861
        try:
-
 
862
            return add_vendor_item_mapping(key, vendorItemMapping)
-
 
863
        finally:
-
 
864
            close_session()
-
 
865
 
-
 
866
    def getVendorItemMappings(self, itemId):
-
 
867
        """
-
 
868
        Returns the list of vendor item mapping corresponding to itemId passed as parameter.
-
 
869
        Raises an exception if item not found corresponding to itemId
-
 
870
        
-
 
871
        Parameters:
-
 
872
         - itemId
-
 
873
        """
-
 
874
        try:
-
 
875
            item_mappings = get_item_mappings(itemId)
-
 
876
            return [to_t_vendor_item_mapping(vim) for vim in item_mappings]
-
 
877
        finally:
-
 
878
            close_session()
-
 
879
 
537
 
880
    def checkSimilarItem(self, brand, modelNumber, modelName, color):
538
    def checkSimilarItem(self, brand, modelNumber, modelName, color):
881
        """
539
        """
882
        Checks if similar item exists (with same Brand, ModelNumber, ModelName, Color)
540
        Checks if similar item exists (with same Brand, ModelNumber, ModelName, Color)
883
        If yes, returns the itemId else returns 0
541
        If yes, returns the itemId else returns 0
Line 1047... Line 705...
1047
        try:
705
        try:
1048
            return get_search_result_count(searchTerms)
706
            return get_search_result_count(searchTerms)
1049
        finally:
707
        finally:
1050
            close_session()
708
            close_session()
1051
    
709
    
1052
    def getPendingOrdersInventory(self, vendorId):
-
 
1053
        """
-
 
1054
        Returns a list of inventory stock for items for which there are pending orders for the given vendor.
-
 
1055
        """
-
 
1056
        try:
-
 
1057
            pending_items_inventory = get_pending_orders_inventory(vendorId)
-
 
1058
            return [AvailableAndReservedStock(itemId = i[0], available=i[1], reserved=i[2], minimumStock=0) for i in pending_items_inventory]
-
 
1059
        finally:
-
 
1060
            close_session()
-
 
1061
    
-
 
1062
    def getProductNotifications(self, startDateTime):
710
    def getProductNotifications(self, startDateTime):
1063
        '''
711
        '''
1064
        Returns a list of Product Notification objects each representing user requests for notification
712
        Returns a list of Product Notification objects each representing user requests for notification
1065
        '''
713
        '''
1066
        try:
714
        try:
Line 1093... Line 741...
1093
        """
741
        """
1094
        For closing the open session in sqlalchemy
742
        For closing the open session in sqlalchemy
1095
        """
743
        """
1096
        close_session()
744
        close_session()
1097
        
745
        
1098
    def addVendor(self, vendor):
-
 
1099
        """
-
 
1100
        add a new vendor
-
 
1101
        """
-
 
1102
        try:
-
 
1103
            return add_vendor(vendor)
-
 
1104
        finally:
-
 
1105
            close_session()
-
 
1106
    
-
 
1107
    def addAuthorizationLog(self, itemId, username, reason):
746
    def addAuthorizationLog(self, itemId, username, reason):
1108
        """
747
        """
1109
        add a log to authorize table.
748
        add a log to authorize table.
1110
        
749
        
1111
        Parameters:
750
        Parameters:
Line 1113... Line 752...
1113
        """
752
        """
1114
        try:
753
        try:
1115
            return add_authorization_log_for_item(itemId, username, reason)
754
            return add_authorization_log_for_item(itemId, username, reason)
1116
        finally:
755
        finally:
1117
            close_session()
756
            close_session()
1118
 
-
 
1119
    def getWarehouses(self, warehouseType, inventoryType, vendorId, billingWarehouseId, shippingWarehouseId):
-
 
1120
        """
-
 
1121
        This method returns all warehouses for a given warehosueType, inventoryType, vendor, billingWarehouse and shippingWarehouse.
-
 
1122
        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
-
 
1123
        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
-
 
1124
        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
-
 
1125
        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
-
 
1126
 
-
 
1127
        Parameters:
-
 
1128
         - warehouseType
-
 
1129
         - inventoryType
-
 
1130
         - vendorId
-
 
1131
         - billingWarehouseId
-
 
1132
         - shippingWarehouseId
-
 
1133
        """
-
 
1134
        try:
-
 
1135
            query = Warehouse.query
-
 
1136
            if warehouseType is not None:
-
 
1137
                query = query.filter_by(warehouseType = WarehouseType._VALUES_TO_NAMES[warehouseType])
-
 
1138
            if inventoryType is not None:
-
 
1139
                query = query.filter_by(inventoryType = InventoryType._VALUES_TO_NAMES[inventoryType])
-
 
1140
            if vendorId:
-
 
1141
                query = query.filter_by(vendor_id = vendorId)
-
 
1142
            if billingWarehouseId:
-
 
1143
                query = query.filter_by(billingWarehouseId = billingWarehouseId)
-
 
1144
            if shippingWarehouseId:
-
 
1145
                query = query.filter_by(shippingWarehouseId = shippingWarehouseId)
-
 
1146
 
-
 
1147
            return [to_t_warehouse(w) for w in query.all()]
-
 
1148
        finally:
-
 
1149
            close_session()
-
 
1150
 
-
 
1151
    def getItemKeysToBeProcessed(self, warehouseId):
-
 
1152
        """
-
 
1153
        Returns the list of item keys which need to be processed for a given warehouse.
-
 
1154
        This is currently used by Support application to send item keys whose inventory needs
-
 
1155
        to be updated from PLB
-
 
1156
 
-
 
1157
        Parameters:
-
 
1158
         - warehouseId
-
 
1159
        """
-
 
1160
        try:
-
 
1161
            return get_item_keys_to_be_processed(warehouseId)
-
 
1162
        finally:
-
 
1163
            close_session()
-
 
1164
 
-
 
1165
    def markMissedInventoryUpdatesAsProcessed(self, itemKey, warehouseId):
-
 
1166
        """
-
 
1167
        Marks/Deletes missed inventory updates for a given key and warehouse.
-
 
1168
        This generally happens when updates from PLB are applied on the currentinventorysnapshot for an item
-
 
1169
 
-
 
1170
        Parameters:
-
 
1171
         - itemKey
-
 
1172
         - warehouseId
-
 
1173
        """
-
 
1174
        try:
-
 
1175
            mark_missed_inventory_updates_as_processed(itemKey, warehouseId)
-
 
1176
        finally:
-
 
1177
            close_session()
-
 
1178
 
-
 
1179
    def resetAvailability(self, itemKey, vendorId, quantity, warehouseId):
-
 
1180
        """
-
 
1181
        Resets availability of an item to the quantity mentioned in a warehouse.
-
 
1182
 
-
 
1183
        Parameters:
-
 
1184
         - itemKey
-
 
1185
         - vendorId
-
 
1186
         - quantity
-
 
1187
         - warehouseId
-
 
1188
        """
-
 
1189
        try:
-
 
1190
            reset_availability(itemKey, vendorId, quantity, warehouseId)
-
 
1191
        finally:
-
 
1192
            close_session()
-
 
1193
 
-
 
1194
    def resetAvailabilityForWarehouse(self, warehouseId):
-
 
1195
        """
-
 
1196
        Resets availability of a warehouse to zero.
-
 
1197
 
-
 
1198
        Parameters:
-
 
1199
         - warehouseId
-
 
1200
        """
-
 
1201
        try:
-
 
1202
            reset_availability_for_warehouse(warehouseId)
-
 
1203
        finally:
-
 
1204
            close_session()
-
 
1205
 
-
 
1206
    def getIgnoredItemKeys(self, ):
-
 
1207
        """
-
 
1208
        Returns all the item key mappings that have been ignored until date. Value of map has the warehouse id
-
 
1209
        and the timestamp from where alert was raised.
-
 
1210
        """
-
 
1211
        try:
-
 
1212
            itemKeysMap = {}
-
 
1213
            for key in MissedInventoryUpdate.query.filter_by(isIgnored = 1).all():
-
 
1214
                itemKeysMap[key.itemKey] = { key.warehouseId : to_java_date(key.timestamp) }
-
 
1215
    
-
 
1216
            return itemKeysMap
-
 
1217
        finally:
-
 
1218
            close_session()
-
 
1219
 
-
 
1220
    def addBadInventory(self, itemId, warehouseId, quantity):
-
 
1221
        """
-
 
1222
        Add the BAD type inventory to existing stock.
-
 
1223
 
-
 
1224
        Parameters:
-
 
1225
         - itemId
-
 
1226
         - warehouseId
-
 
1227
         - quantity
-
 
1228
        """
-
 
1229
        try:
-
 
1230
            add_bad_inventory(itemId, warehouseId, quantity)
-
 
1231
        finally:
-
 
1232
            close_session()
-
 
1233
        
-
 
1234
    def getShippingLocations(self, ):
-
 
1235
        """
-
 
1236
        Returns all shipping locations
-
 
1237
        """
-
 
1238
        try:
-
 
1239
            return [to_t_warehouse(w) for w in get_shipping_locations()]
-
 
1240
        finally:
-
 
1241
            close_session()
-
 
1242
 
-
 
1243
    def getAllVendorItemMappings(self, ):
-
 
1244
        """
-
 
1245
        Fetches all the vendor item mappings present.
-
 
1246
        """
-
 
1247
        try:
-
 
1248
            return [to_t_vendor_item_mapping(m) for m in VendorItemMapping.query.all()]
-
 
1249
        finally:
-
 
1250
            close_session()
-
 
1251
 
-
 
1252
    def getInventorySnapshot(self, warehouseId):
-
 
1253
        """
-
 
1254
        Gets items' inventory for a warehouse
-
 
1255
        If warehouse is passed as zero, items' inventory across all warehouses is sent
-
 
1256
 
-
 
1257
        Parameters:
-
 
1258
         - warehouseId
-
 
1259
        """
-
 
1260
        try:
-
 
1261
            resultMap = {}
-
 
1262
            itemInventoryMap = get_inventory_snapshot(warehouseId)
-
 
1263
            for key, value in itemInventoryMap.items():
-
 
1264
                resultMap[key] = to_t_item_inventory(value, key)
-
 
1265
 
-
 
1266
            return resultMap
-
 
1267
        finally:
-
 
1268
            close_session()
-
 
1269
 
-
 
1270
    def clearItemAvailabilityCache(self, ):
-
 
1271
        """
-
 
1272
        Clear item availability cache.
-
 
1273
        """
-
 
1274
        try:
-
 
1275
            clear_item_availability_cache()
-
 
1276
        finally:
-
 
1277
            close_session()
-
 
1278
    
757
    
1279
    def getClearanceSaleCatalogIds(self):
758
    def getClearanceSaleCatalogIds(self):
1280
        """
759
        """
1281
        clearance sale products
760
        clearance sale products
1282
        """
761
        """
1283
        try:
762
        try:
1284
            return get_clearance_sale_catalog_ids()
763
            return get_clearance_sale_catalog_ids()
1285
        finally:
764
        finally:
1286
            close_session()
765
            close_session()
1287
 
766
    
1288
    def updateVendorString(self, warehouseId, vendorString):
767
    def validateRiskyStatus(self, itemId):
1289
        """
768
        """
1290
        Parameters:
-
 
1291
         - warehouseId
-
 
1292
         - vendorString
769
        Check wether item is risky and change status if inventory is not available for risky items
1293
        """
770
        """
1294
        try:
771
        try:
1295
            update_vendor_string(warehouseId, vendorString)
772
            return check_risky_item(itemId)
1296
        finally:
773
        finally:
1297
            close_session()
774
            close_session()
1298
            
775
    
1299
def is_valid(item):
776
def is_valid(item):
1300
    if item.status in [status.ACTIVE, status.PAUSED, status.PAUSED_BY_RISK]:
777
    if item.status in [status.ACTIVE, status.PAUSED, status.PAUSED_BY_RISK]:
1301
        if item.startDate:
778
        if item.startDate:
1302
            return not(datetime.datetime.now() < item.startDate or item.sellingPrice == 0)
779
            return not(datetime.datetime.now() < item.startDate or item.sellingPrice == 0)
1303
        else:
780
        else: