Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
122 ashish 1
'''
2
Created on 27-Apr-2010
3
 
4
@author: ashish
5
'''
6
from shop2020.model.v1.catalog.impl import DataService
4295 varun.gupt 7
from shop2020.utils.Utils import log_entry, to_py_date
122 ashish 8
from shop2020.model.v1.catalog.impl.DataAcessors import add_item, add_warehouse,\
9
    update_inventory, retire_warehouse, retire_item, change_item_status,\
635 rajveer 10
    get_item, get_all_items, get_all_items_by_status, get_item_inventoy,\
643 chandransh 11
    get_item_availability_for_warehouse, get_item_availability_for_location, get_all_warehouses_by_status,\
122 ashish 12
    get_Warehouse, get_all_warehouses_for_item, get_all_items_for_warehouse,\
766 rajveer 13
    update_item, start_item_on, close_session, \
635 rajveer 14
    retire_item_on, get_item_inventory_by_item_id, get_items_by_catalog_id,\
1308 chandransh 15
    get_best_deals, get_best_deals_catalog_ids, get_best_deals_count, get_best_sellers, get_latest_arrivals, get_latest_arrivals_catalog_ids, get_latest_arrivals_count,\
635 rajveer 16
    is_active, get_best_sellers_catalog_ids, get_best_sellers_count, put_category_object,\
871 chandransh 17
    get_category_object, mark_item_as_content_complete,\
1341 chandransh 18
    reserve_item_in_warehouse, reduce_reservation_count, generate_new_entity_id,\
2116 ankur.sing 19
    get_all_item_pricing, add_vendor_pricing, \
2035 rajveer 20
    get_item_pricing, get_category, get_all_categories, add_category,\
2116 ankur.sing 21
    get_item_status_description, get_all_vendors, check_vendor_item_mapping,\
2286 ankur.sing 22
    get_item_mappings, add_vendor_item_mapping, check_similar_item,\
2809 rajveer 23
    change_risky_flag, get_items_by_vendor_category, get_risky_items,\
3086 rajveer 24
    get_similar_items_catalog_ids, add_product_notification,\
3348 varun.gupt 25
    send_product_notifications, update_inventory_history,\
3557 rajveer 26
    get_all_brands_by_category, is_alive, get_item_pricing_by_source,\
27
    add_source_item_pricing, get_all_sources, get_item_for_source,\
4295 varun.gupt 28
    get_all_source_pricing, get_item_count_by_status, search_items, get_search_result_count,\
29
    get_pending_orders_inventory, get_product_notifications, get_product_notification_request_count
122 ashish 30
from shop2020.model.v1.catalog.impl.Convertors import to_t_item,\
4295 varun.gupt 31
    to_t_item_inventory, to_t_warehouse, to_t_vendor_item_pricing, to_t_category, to_t_vendor,\
32
    to_t_vendor_item_mapping, to_t_source_item_pricing, to_t_product_notification_request,\
33
    to_t_product_notification_request_count
122 ashish 34
from shop2020.thriftpy.model.v1.catalog.ttypes import status,\
4024 chandransh 35
    InventoryServiceException, AvailableAndReservedStock
599 chandransh 36
from shop2020.config.client.ConfigClient import ConfigClient
122 ashish 37
 
38
 
39
class InventoryServiceHandler:
40
    '''
41
    classdocs
42
    '''
599 chandransh 43
 
3187 rajveer 44
    def __init__(self, dbname='catalog', db_hostname='localhost'):
122 ashish 45
        '''
46
        Constructor
47
        '''
3187 rajveer 48
        DataService.initialize(dbname, db_hostname)
599 chandransh 49
        try:
621 chandransh 50
            config_client = ConfigClient()
51
            self.latest_arrivals_limit = int(config_client.get_property("LATEST_ARRIVALS_LIMIT"));
52
            self.best_sellers_limit = int(config_client.get_property("BEST_SELLERS_LIMIT"))
599 chandransh 53
        except:
621 chandransh 54
            self.latest_arrivals_limit = 50
55
            self.best_sellers_limit = 50
122 ashish 56
 
57
    #DONE
58
    def addItem(self, item):
59
        """
60
        Availability and inventory attributes
61
 
62
        Parameters:
63
         - item
64
        """
65
        log_entry(self, "addItem called")
785 rajveer 66
        try:
67
            return add_item(item)
68
        finally:
69
            close_session()
122 ashish 70
 
71
    def addWarehouse(self, warehouse):
72
        """
73
        Parameters:
74
         - warehouse
75
        """
76
        log_entry(self, "addWarehouse called")
785 rajveer 77
        try:
78
            return add_warehouse(warehouse)
79
        finally:
80
            close_session()
122 ashish 81
 
3325 chandransh 82
    def updateInventoryHistory(self, warehouse_id, timestamp, availability):
83
        """
84
        Stores the incremental warehouse updates of items.
85
 
86
        Parameters:
87
         - warehouse_id
88
         - timestamp
89
         - availability
90
        """
91
        try:
92
            return update_inventory_history(warehouse_id, timestamp, availability)
93
        finally:
94
            close_session()
483 rajveer 95
 
96
    def updateInventory(self, warehouse_id, timestamp, availability):
122 ashish 97
        """
98
        Parameters:
99
         - warehouse_id
100
         - timestamp
483 rajveer 101
         - availability
122 ashish 102
        """
103
        log_entry(self, "update Inventory called")
785 rajveer 104
        try:
105
            return update_inventory(warehouse_id, timestamp, availability)
106
        finally:
107
            close_session()
108
 
109
 
635 rajveer 110
    def isActive(self, item_id):
563 chandransh 111
        """
112
        Parameters:
635 rajveer 113
         - item_id
563 chandransh 114
        """
785 rajveer 115
        try:
116
            return is_active(item_id)
117
        finally:
118
            close_session()
122 ashish 119
 
2035 rajveer 120
    def getItemStatusDescription(self, itemId):
121
        """
122
        Parameters:
123
         - itemId
124
        """
125
        try:
126
            return get_item_status_description(itemId)
127
        finally:
128
            close_session()
129
 
122 ashish 130
    def retireWarehouse(self, warehouse_id):
131
        """
132
        Parameters:
133
         - warehouse_id
134
        """
135
        log_entry(self, "retire warehouse called")
785 rajveer 136
        try:
137
            return retire_warehouse(warehouse_id)
138
        finally:
139
            close_session()
140
 
122 ashish 141
 
142
    def retireItem(self, item_id):
143
        """
144
        Parameters:
145
        - item_id
146
        """
147
        log_entry(self, "retire item called")
785 rajveer 148
        try:
149
            return retire_item(item_id)
150
        finally:
151
            close_session()
122 ashish 152
 
153
    def startItemOn(self, item_id, timestamp):
154
        """
155
        Parameters:
156
         - item_id
157
         - timestamp
158
        """
785 rajveer 159
        try:
160
            start_item_on(item_id, timestamp)
161
        finally:
162
            close_session()
122 ashish 163
 
164
    def retireItemOn(self, item_id, timestamp):
165
        """
166
        Parameters:
167
         - item_id
168
         - timestamp
169
        """
785 rajveer 170
        try:
171
            retire_item_on(item_id, timestamp)
172
        finally:
173
            close_session()
122 ashish 174
 
175
    def changeItemStatus(self, item_id, timestamp, newstatus):
176
        """
177
        Parameters:
178
         - item_id
179
         - timestamp
180
         - newstatus
181
        """
182
        log_entry(self, "change item status called")
785 rajveer 183
        try:
184
            return change_item_status(item_id, newstatus)
185
        finally:
186
            close_session()
122 ashish 187
 
785 rajveer 188
 
122 ashish 189
    def getItem(self, item_id):
190
        """
191
        Parameters:
192
         - item_id
193
        """
194
        log_entry(self, "item requested")
785 rajveer 195
        try:
2065 ankur.sing 196
            item = to_t_item( get_item(item_id))
197
            return item
785 rajveer 198
        finally:
199
            close_session()
379 ashish 200
 
635 rajveer 201
    def getItemsByCatalogId(self, catalog_item_id):
379 ashish 202
        """
203
        Parameters:
204
        - catalog_item_id
205
        """
386 ashish 206
        log_entry(self, "item requested")
766 rajveer 207
        try:
208
            return get_items_by_catalog_id(catalog_item_id)
209
        finally:
210
            close_session()
122 ashish 211
 
483 rajveer 212
 
122 ashish 213
    def getAllItems(self, isActive):
214
        """
215
        Parameters:
216
         - isActive
217
        """
218
        log_entry(self, "all items requested")
766 rajveer 219
        try:
220
            items = get_all_items(isActive)
221
            ret_items = []
222
            for item in items:
223
                if item:
224
                    ret_items.append(to_t_item(item))
225
            return ret_items
226
        finally:
227
            close_session()
228
 
122 ashish 229
    def getAllItemsByStatus(self, itemStatus):
230
        """
231
        Parameters:
232
         - itemStatus
233
        """
234
        log_entry(self, "all items requested")
766 rajveer 235
        try:
236
            items = get_all_items_by_status(itemStatus)
237
            ret_items = []
238
            for item in items:
239
                if item:
240
                    ret_items.append(to_t_item(item))
241
            return ret_items
242
        finally:
243
            close_session()
122 ashish 244
 
245
    def getItemInventory(self, item_id):
246
        """
247
        Parameters:
248
        - item_id
249
        """
250
        log_entry(self, "item inventory requested")
766 rajveer 251
        try:
252
            inventory = get_item_inventoy(item_id)
253
            return to_t_item_inventory(inventory, item_id)
254
        finally:
255
            close_session()
122 ashish 256
 
635 rajveer 257
    def getItemInventoryByItemId(self, item_id):
258
        """
259
        Parameters:
260
         - item_id
261
        """
262
        log_entry(self, "item inventory requested")
766 rajveer 263
        try:
264
            inventory = get_item_inventory_by_item_id(item_id)
265
            return to_t_item_inventory(inventory, item_id)
266
        finally:
267
            close_session()
635 rajveer 268
 
3348 varun.gupt 269
    def getAllBrandsByCategory(self, categoryId):
270
        """
271
        Parameters:
272
         - categoryId
273
        """
274
        try:
275
            return get_all_brands_by_category(categoryId)
276
        finally:
277
            close_session()
278
 
635 rajveer 279
    '''
379 ashish 280
    def getItemInventoryByCatalogId(self, item_id):
281
        """
282
        Parameters:
283
         - item_id
284
        """
383 ashish 285
        log_entry(self, "item inventory requested")
286
        inventory = get_item_inventory_by_catalog_id(item_id)
287
        return to_t_item_inventory(inventory, item_id)
635 rajveer 288
    '''
289
 
3849 chandransh 290
    def getAllItemsInRange(self, offset, limit):
291
        """
292
        Gets at most 'limit' items starting at the given offset. Returns an empty list if there are no more items at the given offset.
293
 
294
        Parameters:
295
         - offset
296
         - limit
297
        """
298
        try:
299
            items = get_all_items(False, offset, limit)
300
            return [to_t_item(item) for item in items]
301
        finally:
302
            close_session()
379 ashish 303
 
3849 chandransh 304
    def getAllItemsByStatusInRange(self, itemStatus, offset, limit):
305
        """
306
        Gets at most 'limit' items starting at the given offset in the given status. Returns an empty list if there are no more items at the given offset.
307
 
308
        Parameters:
309
         - itemStatus
310
         - offset
311
         - limit
312
        """
313
        log_entry(self, "all items requested")
314
        try:
315
            items = get_all_items_by_status(itemStatus, offset, limit)
316
            return [to_t_item(item) for item in items]
317
        finally:
318
            close_session()
319
 
320
    def getItemCountByStatus(self, useStatus, itemStatus):
321
        """
322
        Gets a count of all items by status
323
 
324
        Parameters:
325
         - useStatus
326
         - itemStatus
327
        """
328
        try:
329
            return get_item_count_by_status(useStatus, itemStatus)
330
        finally:
331
            close_session()
332
 
643 chandransh 333
    def getItemAvailabilityAtLocation(self, warehouse_loc, item_id):
334
        """
3355 chandransh 335
        Determines the warehouse that should be used to fulfil an order for the given item.
336
        It first checks all the warehouses which are in the logistics location given by the
337
        warehouse_loc parameter. If none of the warehouses there have any inventory, then the
338
        preferred warehouse for the item is used.
643 chandransh 339
 
3355 chandransh 340
        Returns an ordered list of size 4 with following elements in the given order:
341
        1. Logistics location of the warehouse which was finally picked up to ship the order.
342
        2. Id of the warehouse which was finally picked up.
343
        3. Inventory size in the selected warehouse.
344
        4. Expected delay added by the category manager.
345
 
643 chandransh 346
        Parameters:
347
         - warehouse_loc
348
         - item_id
349
        """
766 rajveer 350
        try:
351
            return get_item_availability_for_location(warehouse_loc, item_id)
352
        finally:
353
            close_session()
354
 
122 ashish 355
    def getItemAvailibilityAtWarehouse(self, warehouse_id, item_id):
356
        """
357
        Parameters:
358
         - warehouse_id
359
         - item_id
360
        """
361
        log_entry(self, "item availability at warehouse requested")
766 rajveer 362
        try:
363
            return get_item_availability_for_warehouse(warehouse_id, item_id)
364
        finally:
365
            close_session()
366
 
2080 rajveer 367
    def markItemAsContentComplete(self, entityId, category, brand, modelName, modelNumber):
368
        """
369
        Parameters:
370
         - entityId
371
         - category
372
         - brand
373
         - modelName
374
         - modelNumber
375
        """
376
        try:
377
            return mark_item_as_content_complete(entityId, category, brand, modelName, modelNumber)
378
        finally:
379
            close_session()
2075 rajveer 380
 
381
 
122 ashish 382
    def getItemInventoryHistory(self, id, item_id, warehouse_id, from_date, to_date):
383
        """
384
        Parameters:
385
         - id
386
         - item_id
387
         - warehouse_id
388
         - from_date
389
         - to_date
390
        """
391
        pass
392
 
393
    def getAllWarehouses(self, isActive):
394
        """
395
        Parameters:
396
        - isActive
397
        """
766 rajveer 398
        try:
399
            if isActive:
400
                warehouses = get_all_warehouses_by_status(status.ACTIVE)
401
            else:
402
                warehouses = get_all_warehouses_by_status(None)
403
 
404
            ret_warehouses = []
405
 
406
            for warehouse in warehouses:
407
                ret_warehouses.append(to_t_warehouse(warehouse))
408
            return ret_warehouses
409
        finally:
410
            close_session()
411
 
122 ashish 412
    def getWarehouse(self, warehouse_id):
413
        """
414
        Parameters:
415
         - warehouse_id
416
        """
417
        log_entry(self, "get warehouse call")
766 rajveer 418
        try:
419
            warehouse = get_Warehouse(warehouse_id)
420
            if warehouse:
421
                return to_t_warehouse(warehouse)
422
            else:
423
                raise InventoryServiceException(101, "no such warehouse")
424
        finally:
425
            close_session()
426
 
122 ashish 427
    def getAllWarehousesForItem(self, item_id):
428
        """
429
        Parameters:
430
         - item_id
431
        """
432
        log_entry(self, "getting all warehouses for item")
766 rajveer 433
        try:
434
            all_warehouses = get_all_warehouses_for_item(item_id)
435
            t_warehouses = []
436
            for warehouse in all_warehouses:
437
                t_warehouses.append(to_t_warehouse(warehouse))
438
            return t_warehouses
439
        finally:
440
            close_session()
441
 
122 ashish 442
    def getAllItemsForWarehouse(self, warehouse_id):
443
        """
444
        Parameters:
445
         - warehouse_id
446
        """
447
        log_entry(self, "getting all items for warehouse")
766 rajveer 448
        try:
449
            all_items = get_all_items_for_warehouse(warehouse_id)
450
            t_items = []
451
            for item in all_items:
452
                t_items.append(to_t_item(item))
453
            return t_items
454
        finally:
455
            close_session()
871 chandransh 456
 
457
    def reserveItemInWarehouse(self, itemId, warehouseId, quantity):
458
        """
459
        Increases the reservation count for an item in a warehouse. Should always succeed normally.
460
 
461
        Parameters:
462
         - itemId
463
         - warehouseId
464
        """
465
        log_entry(self, "reserveItemInWarehouse called")
466
        try:
467
            return reserve_item_in_warehouse(itemId, warehouseId, quantity)
468
        finally:
469
            close_session()
470
 
471
    def reduceReservationCount(self, itemId, warehouseId, quantity):
472
        """
473
        Decreases the reservation count for an item in a warehouse. Should always succeed normally.
474
 
475
        Parameters:
476
         - itemId
477
         - warehouseId
478
        """
479
        log_entry(self, "reduceReservationCount called")
480
        try:
481
            return reduce_reservation_count(itemId, warehouseId, quantity)
482
        finally:
483
            close_session()
766 rajveer 484
 
122 ashish 485
    def updateItem(self, item):
486
        """
487
        Parameters:
488
         - item
489
        """
490
        log_entry(self, "addItem called")
766 rajveer 491
        try:
492
            return update_item(item)
493
        finally:
494
            close_session()
495
 
626 chandransh 496
    def getBestSellers(self, start_index=0, total_items=None, category=-1):
766 rajveer 497
        try:
498
            if total_items is None:
499
                total_items = self.best_sellers_limit
500
            stop_index = start_index + total_items
501
            return get_best_sellers(start_index, stop_index, category)
502
        finally:
503
            close_session()
504
 
1926 rajveer 505
    def getBestSellersCatalogIds(self, start_index, total_items, brand, category=-1):
548 rajveer 506
        """
507
        Parameters:
508
         - beginIndex
509
         - totalItems
510
        """
766 rajveer 511
        try:
512
            if total_items is None:
513
                total_items = self.best_sellers_limit
514
            stop_index = start_index + total_items
1926 rajveer 515
            return get_best_sellers_catalog_ids(start_index, stop_index, brand, category)
766 rajveer 516
        finally:
517
            close_session()
518
 
591 chandransh 519
    def getBestSellersCount(self, ):
766 rajveer 520
        try:
2093 chandransh 521
            return get_best_sellers_count()
766 rajveer 522
        finally:
523
            close_session()
524
 
591 chandransh 525
    def getBestDeals(self,):
766 rajveer 526
        try:
527
            return get_best_deals()
528
        finally:
529
            close_session()
530
 
1926 rajveer 531
    def getBestDealsCatalogIds(self, start_index, total_items, brand,category=-1):
548 rajveer 532
        """
533
        Parameters:
534
         - beginIndex
535
         - totalItems
536
        """
766 rajveer 537
        try:
538
            stop_index = start_index + total_items
1926 rajveer 539
            return get_best_deals_catalog_ids(start_index, stop_index, brand, category)
766 rajveer 540
        finally:
541
            close_session()
542
 
591 chandransh 543
    def getBestDealsCount(self, ):
766 rajveer 544
        try:
545
            return get_best_deals_count()
546
        finally:
547
            close_session()
548
 
591 chandransh 549
    def getLatestArrivals(self,):
766 rajveer 550
        try:
551
            return get_latest_arrivals(self.latest_arrivals_limit)
552
        finally:
553
            close_session()
2975 chandransh 554
 
555
    def getLatestArrivalsCatalogIds(self, beginIndex, totalItems, brand, categories):
548 rajveer 556
        """
2975 chandransh 557
        Returns the list of catalog ids of latest arrivals in the given categories of the given brand.
558
        To ignore the categories, pass the list as empty. To ignore brand, pass it as null.
559
 
548 rajveer 560
        Parameters:
561
         - beginIndex
562
         - totalItems
2975 chandransh 563
         - brand
564
         - categories
548 rajveer 565
        """
766 rajveer 566
        try:
2975 chandransh 567
            if beginIndex > self.latest_arrivals_limit:
766 rajveer 568
                return []
2975 chandransh 569
            stopIndex = beginIndex + totalItems
570
            if stopIndex > self.latest_arrivals_limit:
571
                stopIndex = self.latest_arrivals_limit
572
            return get_latest_arrivals_catalog_ids(beginIndex, stopIndex, brand, categories)
766 rajveer 573
        finally:
574
            close_session()
575
 
591 chandransh 576
    def getLatestArrivalsCount(self, ):
766 rajveer 577
        try:
578
            return get_latest_arrivals_count(self.latest_arrivals_limit)
579
        finally:
580
            close_session()
591 chandransh 581
 
4283 anupam.sin 582
    def getItemPricing(self, itemId, vendorId):
1334 chandransh 583
        """
584
        Returns the pricing information of an item associated with the vendor of the given warehouse.
585
        Raises an exception if either the item, vendor or the associated pricing information can't be found.
586
 
587
        Parameters:
588
         - itemId
589
         - warehouseId
590
        """
1341 chandransh 591
        try:
4283 anupam.sin 592
            return to_t_vendor_item_pricing(get_item_pricing(itemId, vendorId))
1341 chandransh 593
        finally:
594
            close_session()
1991 ankur.sing 595
 
596
 
597
    def getAllItemPricing(self, itemId):
598
        """
599
        Returns list of the pricing information of an item associated an item.
600
        Raises an exception if the item can't be found.
601
 
602
        Parameters:
603
         - itemId
604
        """
605
        try:
606
            return [to_t_vendor_item_pricing(vid) for vid in get_all_item_pricing(itemId)]
607
        finally:
608
            close_session()
1334 chandransh 609
 
1155 rajveer 610
    def generateNewEntityID(self, ):
611
        try:
612
            return generate_new_entity_id()
613
        finally:
614
            close_session()
615
 
635 rajveer 616
    def putCategoryObject(self, object):
617
        """
618
        Parameters:
619
         - object
620
        """
766 rajveer 621
        try:
622
            return put_category_object(object)
623
        finally:
624
            close_session()
625
 
635 rajveer 626
    def getCategoryObject(self, ):
766 rajveer 627
        try:
628
            return get_category_object()
629
        finally:
630
            close_session()
1970 rajveer 631
 
632
    def addCategory(self, category):
633
        """
634
        Parameters:
635
         - category
636
        """
637
        try:
638
            return add_category(category)
639
        finally:
640
            close_session()
641
 
642
    def getCategory(self, id):
643
        """
644
        Parameters:
645
         - id
646
        """
647
        try:
648
            return to_t_category(get_category(id))
649
        finally:
650
            close_session()
651
 
652
    def getAllCategories(self, ):
653
        try:
654
            categories = get_all_categories()
655
            return [to_t_category(category) for category in categories]
656
        finally:
657
            close_session()
658
 
659
 
2116 ankur.sing 660
    def addVendorItemPricing(self, vendorItemPricing):
1991 ankur.sing 661
        """
2116 ankur.sing 662
        Adds vendor prices corresponding to the item. If pricing already exists then updates the prices. 
1991 ankur.sing 663
        Raises an exception if either the item or vendor can't be found corresponding to their ids.
664
 
665
        Parameters:
666
         - vendorItemPricing
667
        """
668
        log_entry(self, "updateVendorItemPricing called")
669
        try:
2116 ankur.sing 670
            return add_vendor_pricing(vendorItemPricing)
1991 ankur.sing 671
        finally:
672
            close_session()
673
 
2065 ankur.sing 674
    def getAllVendors(self, ):
675
        """
676
        Return list of all vendors
677
        """
678
        return [to_t_vendor(v) for v in get_all_vendors()]
2116 ankur.sing 679
 
680
    def itemExists(self, productGroup, brand, modelNumber, color, vendorId, category):
681
        try:
682
            return check_vendor_item_mapping(productGroup, brand, modelNumber, color, vendorId, category)
683
        finally:
684
            close_session()
2065 ankur.sing 685
 
2358 ankur.sing 686
    def addVendorItemMapping(self, key, vendorItemMapping):
2116 ankur.sing 687
        """
2358 ankur.sing 688
        Adds VendorItemMapping. Updates VendorItemMapping if exists corresponding to the item key.
2116 ankur.sing 689
 
690
        Parameters:
2358 ankur.sing 691
         - key
2116 ankur.sing 692
         - vendorItemMapping
693
        """
694
        log_entry(self, "addVendorItemMapping called")
695
        try:
2358 ankur.sing 696
            return add_vendor_item_mapping(key, vendorItemMapping)
2116 ankur.sing 697
        finally:
698
            close_session()
699
 
700
    def getVendorItemMappings(self, itemId):
701
        """
702
        Returns the list of vendor item mapping corresponding to itemId passed as parameter.
703
        Raises an exception if item not found corresponding to itemId
704
 
705
        Parameters:
706
         - itemId
707
        """
708
        try:
2120 ankur.sing 709
            item_mappings = get_item_mappings(itemId)
710
            return [to_t_vendor_item_mapping(vim) for vim in item_mappings]
2116 ankur.sing 711
        finally:
712
            close_session()
713
 
714
    def checkSimilarItem(self, productGroup, brand, modelNumber, color):
715
        """
716
        Checks if similar item exists (with same ProductGroup, Brand, ModelNumber, Color)
717
        If yes, returns the itemId else returns 0
718
 
719
        Parameters:
720
         - productGroup
721
         - brand
722
         - modelNumber
723
         - color
724
        """
725
        try:
726
            return check_similar_item(productGroup, brand, modelNumber, color)
727
        finally:
728
            close_session()
729
 
2286 ankur.sing 730
    def changeItemRiskyFlag(self, itemId, risky):
731
        """
732
        Marks/Unmarks an item as risky. This flag is used for automatic marking of an item as INACTIVE in case of zero inventory.
733
 
734
        Parameters:
735
         - itemId
736
         - risky
737
        """
738
        try:
739
            change_risky_flag(itemId, risky)
740
        finally:
741
            close_session()
2116 ankur.sing 742
 
2358 ankur.sing 743
    def getItemsByVendorCategory(self, vendorCategory):
744
        """
745
        Return list of items filtered by vendor category.
746
        Raises exception if vendorCategory is null.
747
 
748
        Parameters:
749
         - vendorCategory
750
        """
751
        try:
752
            return [to_t_item(item) for item in get_items_by_vendor_category(vendorCategory)]
753
        finally:
754
            close_session()
755
 
756
    def getItemsByRiskyFlag(self, ):
757
        """
758
        Returns list of items marked as risky.
759
        """
760
        try:
761
            return [to_t_item(item) for item in get_risky_items()]
762
        finally:
763
            close_session()
2809 rajveer 764
 
765
    def getSimilarItemsCatalogIds(self, beginIndex, totalItems, itemId):
766
        """
767
        Returns list of catalog ids of items with same similarity index as of the given itemId
768
 
769
        Parameters:
770
         - beginIndex
771
         - totalItems
772
         - itemId
773
        """
774
        try:
775
            return get_similar_items_catalog_ids(beginIndex, beginIndex+totalItems, itemId)
776
        finally:
777
            close_session()
3079 rajveer 778
 
779
    def addProductNotification(self, itemId, email):
780
        """
781
        Add user requests for out of stock items. Once user will ask for notify me an entry will
782
 
783
        Parameters:
784
        - itemId
785
        - email
786
        """
787
        try:
788
            return add_product_notification(itemId, email)
789
        finally:
790
            close_session()
2358 ankur.sing 791
 
3086 rajveer 792
    def sendProductNotifications(self, ):
793
        """
794
        Send the product notifications to the users for items which has stock.
795
        """
796
        try:
797
            return send_product_notifications()
798
        finally:
799
            close_session()
3557 rajveer 800
 
801
 
802
    def getAllSources(self, ):
803
        """
804
        Return list of all sources
805
        """
806
        try:
807
            return get_all_sources()
808
        finally:
809
            close_session()
810
 
811
    def getItemPricingBySource(self, itemId, sourceId):
812
        """
813
        Returns the pricing information of an item. If no information is found, exception will be thrown.
3376 rajveer 814
 
3557 rajveer 815
        Parameters:
816
         - itemId
817
         - sourceId
818
        """
819
        try:
820
            return to_t_source_item_pricing(get_item_pricing_by_source(itemId, sourceId))
821
        finally:
822
            close_session()
823
 
824
    def addSourceItemPricing(self, sourceItemPricing):
825
        """
826
        Adds prices to be displayed corresponding to the item if user comes from a source.
827
 
828
        Parameters:
829
         - sourceItemPricing
830
        """
831
        try:
832
            add_source_item_pricing(sourceItemPricing)
833
        finally:
834
            close_session()
835
 
836
    def getAllSourcePricing(self, itemId):
837
        """
838
        Returns the list of source pricing information of an item.
839
        Raises an exception if item not found corresponding to itemId
840
 
841
        Parameters:
842
         - itemId
843
        """
844
        try:
845
            return [to_t_source_item_pricing(source_item_pricing) for source_item_pricing in get_all_source_pricing(itemId)]
846
#        return [to_t_item(item) for item in get_risky_items()]
847
        finally:
848
            close_session()
849
 
850
    def getItemForSource(self, item_id, sourceId):
851
        """
852
        Parameters:
853
         - item_id
854
         - sourceId
855
        """
856
        try:
857
            return to_t_item(get_item_for_source(item_id, sourceId))
858
        finally:
859
            close_session()
3872 chandransh 860
 
861
    def searchItemsInRange(self, searchTerms, offset, limit):
862
        """
863
        Searches items matching the the given terms in the catalog
3557 rajveer 864
 
3872 chandransh 865
        Parameters:
866
         - searchTerms
867
        """
868
        try:
869
            return [to_t_item(item) for item in search_items(searchTerms, offset, limit)]
870
        finally:
871
            close_session()
872
 
873
    def getSearchResultCount(self, searchTerms):
874
        """
875
        Gets the count of search results for the given search terms so that the user can go through all the pages.
876
 
877
        Parameters:
878
         - searchTerms
879
        """
880
        try:
881
            return get_search_result_count(searchTerms)
882
        finally:
883
            close_session()
884
 
4063 chandransh 885
    def getPendingOrdersInventory(self, vendorId):
4024 chandransh 886
        """
4063 chandransh 887
        Returns a list of inventory stock for items for which there are pending orders for the given vendor.
4024 chandransh 888
        """
889
        try:
4063 chandransh 890
            pending_items_inventory = get_pending_orders_inventory(vendorId)
4024 chandransh 891
            return [AvailableAndReservedStock(itemId = i[0], available=i[1], reserved=i[2], minimumStock=0) for i in pending_items_inventory]
892
        finally:
893
            close_session()
894
 
4295 varun.gupt 895
    def getProductNotifications(self, startDateTime):
896
        '''
897
        Returns a list of Product Notification objects each representing user requests for notification
898
        '''
899
        try:
900
            return [to_t_product_notification_request(notification) for notification in get_product_notifications(to_py_date(startDateTime))]
901
        finally:
902
            close_session()
903
 
904
    def getProductNotificationRequestCount(self, startDateTime):
905
        '''
906
        Returns list of items and the counts of product notification requests
907
        '''
908
        try:
909
            notification_request_counts = [(notification.item, count) for notification, count in get_product_notification_request_count(to_py_date(startDateTime))]
910
 
911
            return [to_t_product_notification_request_count(count) for count in notification_request_counts]
912
 
913
        finally:
914
            close_session()
915
 
3376 rajveer 916
    def isAlive(self, ):
917
        """
918
        For checking weather service is active alive or not. It also checks connectivity with database
919
        """
920
        try:
921
            return is_alive()
922
        finally:
923
            close_session()
924
 
766 rajveer 925
    def closeSession(self, ):
3376 rajveer 926
        """
927
        For closing the open session in sqlalchemy
928
        """
929
        close_session()        
930