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