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
'''
4370 anupam.sin 6
from shop2020.clients.CatalogClient import CatalogClient
4501 mandeep.dh 7
from shop2020.clients.PurchaseClient import PurchaseClient
5110 mandeep.dh 8
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, \
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, \
13
    to_t_product_notification_request, to_t_product_notification_request_count
14
from shop2020.model.v1.catalog.impl.DataAcessors import add_item, add_warehouse, \
15
    update_inventory, retire_warehouse, retire_item, change_item_status, get_item, \
16
    get_all_items, get_all_items_by_status, get_item_availability_for_warehouse, \
17
    get_item_availability_for_location, get_all_warehouses_by_status, get_Warehouse, \
18
    get_all_warehouses_for_item, get_all_items_for_warehouse, update_item, \
19
    start_item_on, close_session, add_vendor, retire_item_on, get_item_inventory_by_item_id, \
20
    get_items_by_catalog_id, get_best_deals, get_best_deals_catalog_ids, \
21
    get_best_deals_count, get_best_sellers, get_latest_arrivals, \
22
    get_latest_arrivals_catalog_ids, get_latest_arrivals_count, is_active, \
23
    get_best_sellers_catalog_ids, get_best_sellers_count, put_category_object, \
24
    get_category_object, mark_item_as_content_complete, reserve_item_in_warehouse, \
25
    reduce_reservation_count, generate_new_entity_id, get_all_item_pricing, \
26
    add_vendor_pricing, get_item_pricing, get_category, get_all_categories, \
27
    add_category, get_item_status_description, get_all_vendors, get_item_mappings, \
28
    add_vendor_item_mapping, check_similar_item, change_risky_flag, \
29
    get_items_for_mastersheet, get_risky_items, get_similar_items_catalog_ids, \
30
    add_product_notification, send_product_notifications, update_inventory_history, \
31
    get_all_brands_by_category, is_alive, get_item_pricing_by_source, \
32
    add_source_item_pricing, get_all_sources, get_item_for_source, \
33
    get_all_source_pricing, get_item_count_by_status, search_items, \
34
    get_search_result_count, get_pending_orders_inventory, get_product_notifications, \
35
    get_product_notification_request_count, add_inventory, \
36
    get_all_similar_items_catalog_ids, add_similar_item_catalog_id, \
37
    delete_similar_item_catalog_id, add_authorization_log_for_item, \
38
    get_thrift_item_list, get_all_brands, add_bad_inventory, \
39
    mark_missed_inventory_updates_as_processed, \
5217 amit.gupta 40
    get_item_keys_to_be_processed, reset_availability, get_shipping_locations,\
5295 rajveer 41
    get_coming_soon, get_coming_soon_catalog_ids, get_coming_soon_count,\
5437 mandeep.dh 42
    initialize, get_inventory_snapshot, clear_item_availability_cache, \
5460 phani.kuma 43
    reset_availability_for_warehouse, get_clearance_sale_catalog_ids
5110 mandeep.dh 44
from shop2020.model.v1.catalog.impl.DataService import Warehouse, \
5237 mandeep.dh 45
    MissedInventoryUpdate, VendorItemMapping
5110 mandeep.dh 46
from shop2020.thriftpy.model.v1.catalog.ttypes import status, \
47
    InventoryServiceException, AvailableAndReservedStock, WarehouseType, InventoryType
4501 mandeep.dh 48
from shop2020.thriftpy.purchase.ttypes import PurchaseOrder, LineItem
5110 mandeep.dh 49
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
50
import datetime
4370 anupam.sin 51
import xlrd
122 ashish 52
 
4370 anupam.sin 53
 
5110 mandeep.dh 54
 
122 ashish 55
class InventoryServiceHandler:
56
    '''
57
    classdocs
58
    '''
599 chandransh 59
 
3187 rajveer 60
    def __init__(self, dbname='catalog', db_hostname='localhost'):
122 ashish 61
        '''
62
        Constructor
63
        '''
5295 rajveer 64
        initialize(dbname, db_hostname)
599 chandransh 65
        try:
621 chandransh 66
            config_client = ConfigClient()
67
            self.latest_arrivals_limit = int(config_client.get_property("LATEST_ARRIVALS_LIMIT"));
68
            self.best_sellers_limit = int(config_client.get_property("BEST_SELLERS_LIMIT"))
599 chandransh 69
        except:
621 chandransh 70
            self.latest_arrivals_limit = 50
71
            self.best_sellers_limit = 50
122 ashish 72
 
73
    #DONE
74
    def addItem(self, item):
75
        """
76
        Availability and inventory attributes
77
 
78
        Parameters:
79
         - item
80
        """
81
        log_entry(self, "addItem called")
785 rajveer 82
        try:
83
            return add_item(item)
84
        finally:
85
            close_session()
122 ashish 86
 
87
    def addWarehouse(self, warehouse):
88
        """
89
        Parameters:
90
         - warehouse
91
        """
92
        log_entry(self, "addWarehouse called")
785 rajveer 93
        try:
94
            return add_warehouse(warehouse)
95
        finally:
96
            close_session()
122 ashish 97
 
3325 chandransh 98
    def updateInventoryHistory(self, warehouse_id, timestamp, availability):
99
        """
100
        Stores the incremental warehouse updates of items.
101
 
102
        Parameters:
103
         - warehouse_id
104
         - timestamp
105
         - availability
106
        """
107
        try:
108
            return update_inventory_history(warehouse_id, timestamp, availability)
109
        finally:
110
            close_session()
483 rajveer 111
 
112
    def updateInventory(self, warehouse_id, timestamp, availability):
122 ashish 113
        """
114
        Parameters:
115
         - warehouse_id
116
         - timestamp
483 rajveer 117
         - availability
122 ashish 118
        """
119
        log_entry(self, "update Inventory called")
785 rajveer 120
        try:
121
            return update_inventory(warehouse_id, timestamp, availability)
122
        finally:
123
            close_session()
124
 
4320 rajveer 125
    def addInventory(self, itemId, warehouseId, quantity):
126
        """
127
        Add the inventory to existing stock.
128
 
129
        Parameters:
130
         - itemId
131
         - warehouseId
132
         - quantity
133
        """
134
        log_entry(self, "add Inventory called")
135
        try:
136
            return add_inventory(itemId, warehouseId, quantity)
137
        finally:
138
            close_session()
785 rajveer 139
 
635 rajveer 140
    def isActive(self, item_id):
563 chandransh 141
        """
142
        Parameters:
635 rajveer 143
         - item_id
563 chandransh 144
        """
785 rajveer 145
        try:
146
            return is_active(item_id)
147
        finally:
148
            close_session()
122 ashish 149
 
2035 rajveer 150
    def getItemStatusDescription(self, itemId):
151
        """
152
        Parameters:
153
         - itemId
154
        """
155
        try:
156
            return get_item_status_description(itemId)
157
        finally:
158
            close_session()
159
 
122 ashish 160
    def retireWarehouse(self, warehouse_id):
161
        """
162
        Parameters:
163
         - warehouse_id
164
        """
165
        log_entry(self, "retire warehouse called")
785 rajveer 166
        try:
167
            return retire_warehouse(warehouse_id)
168
        finally:
169
            close_session()
170
 
122 ashish 171
 
172
    def retireItem(self, item_id):
173
        """
174
        Parameters:
175
        - item_id
176
        """
177
        log_entry(self, "retire item called")
785 rajveer 178
        try:
179
            return retire_item(item_id)
180
        finally:
181
            close_session()
122 ashish 182
 
183
    def startItemOn(self, item_id, timestamp):
184
        """
185
        Parameters:
186
         - item_id
187
         - timestamp
188
        """
785 rajveer 189
        try:
190
            start_item_on(item_id, timestamp)
191
        finally:
192
            close_session()
122 ashish 193
 
194
    def retireItemOn(self, item_id, timestamp):
195
        """
196
        Parameters:
197
         - item_id
198
         - timestamp
199
        """
785 rajveer 200
        try:
201
            retire_item_on(item_id, timestamp)
202
        finally:
203
            close_session()
122 ashish 204
 
205
    def changeItemStatus(self, item_id, timestamp, newstatus):
206
        """
207
        Parameters:
208
         - item_id
209
         - timestamp
210
         - newstatus
211
        """
212
        log_entry(self, "change item status called")
785 rajveer 213
        try:
214
            return change_item_status(item_id, newstatus)
215
        finally:
216
            close_session()
122 ashish 217
 
785 rajveer 218
 
122 ashish 219
    def getItem(self, item_id):
220
        """
221
        Parameters:
222
         - item_id
223
        """
224
        log_entry(self, "item requested")
785 rajveer 225
        try:
2065 ankur.sing 226
            item = to_t_item( get_item(item_id))
227
            return item
785 rajveer 228
        finally:
229
            close_session()
379 ashish 230
 
635 rajveer 231
    def getItemsByCatalogId(self, catalog_item_id):
379 ashish 232
        """
233
        Parameters:
234
        - catalog_item_id
235
        """
386 ashish 236
        log_entry(self, "item requested")
766 rajveer 237
        try:
4934 amit.gupta 238
            return get_thrift_item_list(get_items_by_catalog_id(catalog_item_id))
766 rajveer 239
        finally:
240
            close_session()
122 ashish 241
 
4934 amit.gupta 242
    def getValidItemsByCatalogId(self, catalog_item_id):
243
        """
244
        Parameters:
245
         - catalog_item_id
246
        """
247
        log_entry(self, "items requested for CatalogItem")
248
        try:
249
            return get_thrift_item_list(filter(is_valid, get_items_by_catalog_id(catalog_item_id)))
250
        finally:
251
            close_session()
252
 
122 ashish 253
    def getAllItems(self, isActive):
254
        """
255
        Parameters:
256
         - isActive
257
        """
258
        log_entry(self, "all items requested")
766 rajveer 259
        try:
260
            items = get_all_items(isActive)
261
            ret_items = []
262
            for item in items:
263
                if item:
264
                    ret_items.append(to_t_item(item))
265
            return ret_items
266
        finally:
267
            close_session()
268
 
122 ashish 269
    def getAllItemsByStatus(self, itemStatus):
270
        """
271
        Parameters:
272
         - itemStatus
273
        """
274
        log_entry(self, "all items requested")
766 rajveer 275
        try:
276
            items = get_all_items_by_status(itemStatus)
277
            ret_items = []
278
            for item in items:
279
                if item:
280
                    ret_items.append(to_t_item(item))
281
            return ret_items
282
        finally:
283
            close_session()
122 ashish 284
 
4431 phani.kuma 285
    '''
122 ashish 286
    def getItemInventory(self, item_id):
287
        """
288
        Parameters:
289
        - item_id
290
        """
291
        log_entry(self, "item inventory requested")
766 rajveer 292
        try:
293
            inventory = get_item_inventoy(item_id)
294
            return to_t_item_inventory(inventory, item_id)
295
        finally:
296
            close_session()
4431 phani.kuma 297
    '''
122 ashish 298
 
635 rajveer 299
    def getItemInventoryByItemId(self, item_id):
300
        """
301
        Parameters:
302
         - item_id
303
        """
304
        log_entry(self, "item inventory requested")
766 rajveer 305
        try:
306
            inventory = get_item_inventory_by_item_id(item_id)
307
            return to_t_item_inventory(inventory, item_id)
308
        finally:
309
            close_session()
635 rajveer 310
 
3348 varun.gupt 311
    def getAllBrandsByCategory(self, categoryId):
312
        """
313
        Parameters:
314
         - categoryId
315
        """
316
        try:
317
            return get_all_brands_by_category(categoryId)
318
        finally:
319
            close_session()
320
 
4957 phani.kuma 321
    def getAllBrands(self):
322
        try:
323
            return get_all_brands()
324
        finally:
325
            close_session()
326
 
635 rajveer 327
    '''
379 ashish 328
    def getItemInventoryByCatalogId(self, item_id):
329
        """
330
        Parameters:
331
         - item_id
332
        """
383 ashish 333
        log_entry(self, "item inventory requested")
334
        inventory = get_item_inventory_by_catalog_id(item_id)
335
        return to_t_item_inventory(inventory, item_id)
635 rajveer 336
    '''
337
 
3849 chandransh 338
    def getAllItemsInRange(self, offset, limit):
339
        """
340
        Gets at most 'limit' items starting at the given offset. Returns an empty list if there are no more items at the given offset.
341
 
342
        Parameters:
343
         - offset
344
         - limit
345
        """
346
        try:
347
            items = get_all_items(False, offset, limit)
348
            return [to_t_item(item) for item in items]
349
        finally:
350
            close_session()
379 ashish 351
 
3849 chandransh 352
    def getAllItemsByStatusInRange(self, itemStatus, offset, limit):
353
        """
354
        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.
355
 
356
        Parameters:
357
         - itemStatus
358
         - offset
359
         - limit
360
        """
361
        log_entry(self, "all items requested")
362
        try:
363
            items = get_all_items_by_status(itemStatus, offset, limit)
364
            return [to_t_item(item) for item in items]
365
        finally:
366
            close_session()
367
 
368
    def getItemCountByStatus(self, useStatus, itemStatus):
369
        """
370
        Gets a count of all items by status
371
 
372
        Parameters:
373
         - useStatus
374
         - itemStatus
375
        """
376
        try:
377
            return get_item_count_by_status(useStatus, itemStatus)
378
        finally:
379
            close_session()
380
 
5295 rajveer 381
    def getItemAvailabilityAtLocation(self, itemId):
643 chandransh 382
        """
3355 chandransh 383
        Determines the warehouse that should be used to fulfil an order for the given item.
384
        It first checks all the warehouses which are in the logistics location given by the
385
        warehouse_loc parameter. If none of the warehouses there have any inventory, then the
386
        preferred warehouse for the item is used.
643 chandransh 387
 
3355 chandransh 388
        Returns an ordered list of size 4 with following elements in the given order:
389
        1. Logistics location of the warehouse which was finally picked up to ship the order.
390
        2. Id of the warehouse which was finally picked up.
391
        3. Inventory size in the selected warehouse.
392
        4. Expected delay added by the category manager.
393
 
643 chandransh 394
        Parameters:
5295 rajveer 395
         - itemId
643 chandransh 396
        """
766 rajveer 397
        try:
5295 rajveer 398
            return get_item_availability_for_location(itemId)
766 rajveer 399
        finally:
400
            close_session()
401
 
122 ashish 402
    def getItemAvailibilityAtWarehouse(self, warehouse_id, item_id):
403
        """
404
        Parameters:
405
         - warehouse_id
406
         - item_id
407
        """
408
        log_entry(self, "item availability at warehouse requested")
766 rajveer 409
        try:
410
            return get_item_availability_for_warehouse(warehouse_id, item_id)
411
        finally:
412
            close_session()
413
 
2080 rajveer 414
    def markItemAsContentComplete(self, entityId, category, brand, modelName, modelNumber):
415
        """
416
        Parameters:
417
         - entityId
418
         - category
419
         - brand
420
         - modelName
421
         - modelNumber
422
        """
423
        try:
424
            return mark_item_as_content_complete(entityId, category, brand, modelName, modelNumber)
425
        finally:
426
            close_session()
2075 rajveer 427
 
428
 
122 ashish 429
    def getItemInventoryHistory(self, id, item_id, warehouse_id, from_date, to_date):
430
        """
431
        Parameters:
432
         - id
433
         - item_id
434
         - warehouse_id
435
         - from_date
436
         - to_date
437
        """
438
        pass
439
 
440
    def getAllWarehouses(self, isActive):
441
        """
442
        Parameters:
443
        - isActive
444
        """
766 rajveer 445
        try:
446
            if isActive:
447
                warehouses = get_all_warehouses_by_status(status.ACTIVE)
448
            else:
449
                warehouses = get_all_warehouses_by_status(None)
450
 
451
            ret_warehouses = []
452
 
453
            for warehouse in warehouses:
454
                ret_warehouses.append(to_t_warehouse(warehouse))
455
            return ret_warehouses
456
        finally:
457
            close_session()
458
 
122 ashish 459
    def getWarehouse(self, warehouse_id):
460
        """
461
        Parameters:
462
         - warehouse_id
463
        """
464
        log_entry(self, "get warehouse call")
766 rajveer 465
        try:
466
            warehouse = get_Warehouse(warehouse_id)
467
            if warehouse:
468
                return to_t_warehouse(warehouse)
469
            else:
5110 mandeep.dh 470
                raise InventoryServiceException(101, "no such warehouse: " + str(warehouse_id))
766 rajveer 471
        finally:
472
            close_session()
473
 
122 ashish 474
    def getAllWarehousesForItem(self, item_id):
475
        """
476
        Parameters:
477
         - item_id
478
        """
479
        log_entry(self, "getting all warehouses for item")
766 rajveer 480
        try:
481
            all_warehouses = get_all_warehouses_for_item(item_id)
482
            t_warehouses = []
483
            for warehouse in all_warehouses:
484
                t_warehouses.append(to_t_warehouse(warehouse))
485
            return t_warehouses
486
        finally:
487
            close_session()
488
 
122 ashish 489
    def getAllItemsForWarehouse(self, warehouse_id):
490
        """
491
        Parameters:
492
         - warehouse_id
493
        """
494
        log_entry(self, "getting all items for warehouse")
766 rajveer 495
        try:
496
            all_items = get_all_items_for_warehouse(warehouse_id)
497
            t_items = []
498
            for item in all_items:
499
                t_items.append(to_t_item(item))
500
            return t_items
501
        finally:
502
            close_session()
871 chandransh 503
 
504
    def reserveItemInWarehouse(self, itemId, warehouseId, quantity):
505
        """
506
        Increases the reservation count for an item in a warehouse. Should always succeed normally.
507
 
508
        Parameters:
509
         - itemId
510
         - warehouseId
511
        """
512
        log_entry(self, "reserveItemInWarehouse called")
513
        try:
514
            return reserve_item_in_warehouse(itemId, warehouseId, quantity)
515
        finally:
516
            close_session()
517
 
518
    def reduceReservationCount(self, itemId, warehouseId, quantity):
519
        """
520
        Decreases the reservation count for an item in a warehouse. Should always succeed normally.
521
 
522
        Parameters:
523
         - itemId
524
         - warehouseId
525
        """
526
        log_entry(self, "reduceReservationCount called")
527
        try:
528
            return reduce_reservation_count(itemId, warehouseId, quantity)
529
        finally:
530
            close_session()
766 rajveer 531
 
122 ashish 532
    def updateItem(self, item):
533
        """
534
        Parameters:
535
         - item
536
        """
537
        log_entry(self, "addItem called")
766 rajveer 538
        try:
539
            return update_item(item)
540
        finally:
541
            close_session()
542
 
626 chandransh 543
    def getBestSellers(self, start_index=0, total_items=None, category=-1):
766 rajveer 544
        try:
545
            if total_items is None:
546
                total_items = self.best_sellers_limit
547
            stop_index = start_index + total_items
548
            return get_best_sellers(start_index, stop_index, category)
549
        finally:
550
            close_session()
551
 
1926 rajveer 552
    def getBestSellersCatalogIds(self, start_index, total_items, brand, category=-1):
548 rajveer 553
        """
554
        Parameters:
555
         - beginIndex
556
         - totalItems
557
        """
766 rajveer 558
        try:
559
            if total_items is None:
560
                total_items = self.best_sellers_limit
561
            stop_index = start_index + total_items
1926 rajveer 562
            return get_best_sellers_catalog_ids(start_index, stop_index, brand, category)
766 rajveer 563
        finally:
564
            close_session()
565
 
591 chandransh 566
    def getBestSellersCount(self, ):
766 rajveer 567
        try:
2093 chandransh 568
            return get_best_sellers_count()
766 rajveer 569
        finally:
570
            close_session()
571
 
591 chandransh 572
    def getBestDeals(self,):
766 rajveer 573
        try:
574
            return get_best_deals()
575
        finally:
576
            close_session()
577
 
1926 rajveer 578
    def getBestDealsCatalogIds(self, start_index, total_items, brand,category=-1):
548 rajveer 579
        """
580
        Parameters:
581
         - beginIndex
582
         - totalItems
583
        """
766 rajveer 584
        try:
585
            stop_index = start_index + total_items
1926 rajveer 586
            return get_best_deals_catalog_ids(start_index, stop_index, brand, category)
766 rajveer 587
        finally:
588
            close_session()
589
 
591 chandransh 590
    def getBestDealsCount(self, ):
766 rajveer 591
        try:
592
            return get_best_deals_count()
593
        finally:
594
            close_session()
595
 
5217 amit.gupta 596
    def getComingSoon(self,):
597
        try:
598
            return get_coming_soon()
599
        finally:
600
            close_session()
601
 
602
    def getComingSoonCatalogIds(self, start_index, total_items, brand,category=-1):
603
        """
604
        Parameters:
605
         - beginIndex
606
         - totalItems
607
        """
608
        try:
609
            stop_index = start_index + total_items
610
            return get_coming_soon_catalog_ids(start_index, stop_index, brand, category)
611
        finally:
612
            close_session()
613
 
614
    def getComingSoonCount(self, ):
615
        try:
616
            return get_coming_soon_count()
617
        finally:
618
            close_session()
619
 
591 chandransh 620
    def getLatestArrivals(self,):
766 rajveer 621
        try:
622
            return get_latest_arrivals(self.latest_arrivals_limit)
623
        finally:
624
            close_session()
2975 chandransh 625
 
626
    def getLatestArrivalsCatalogIds(self, beginIndex, totalItems, brand, categories):
548 rajveer 627
        """
2975 chandransh 628
        Returns the list of catalog ids of latest arrivals in the given categories of the given brand.
629
        To ignore the categories, pass the list as empty. To ignore brand, pass it as null.
630
 
548 rajveer 631
        Parameters:
632
         - beginIndex
633
         - totalItems
2975 chandransh 634
         - brand
635
         - categories
548 rajveer 636
        """
766 rajveer 637
        try:
2975 chandransh 638
            if beginIndex > self.latest_arrivals_limit:
766 rajveer 639
                return []
2975 chandransh 640
            stopIndex = beginIndex + totalItems
641
            if stopIndex > self.latest_arrivals_limit:
642
                stopIndex = self.latest_arrivals_limit
643
            return get_latest_arrivals_catalog_ids(beginIndex, stopIndex, brand, categories)
766 rajveer 644
        finally:
645
            close_session()
646
 
591 chandransh 647
    def getLatestArrivalsCount(self, ):
766 rajveer 648
        try:
649
            return get_latest_arrivals_count(self.latest_arrivals_limit)
650
        finally:
651
            close_session()
591 chandransh 652
 
4283 anupam.sin 653
    def getItemPricing(self, itemId, vendorId):
1334 chandransh 654
        """
655
        Returns the pricing information of an item associated with the vendor of the given warehouse.
656
        Raises an exception if either the item, vendor or the associated pricing information can't be found.
657
 
658
        Parameters:
659
         - itemId
660
         - warehouseId
661
        """
1341 chandransh 662
        try:
4283 anupam.sin 663
            return to_t_vendor_item_pricing(get_item_pricing(itemId, vendorId))
1341 chandransh 664
        finally:
665
            close_session()
1991 ankur.sing 666
 
667
 
668
    def getAllItemPricing(self, itemId):
669
        """
670
        Returns list of the pricing information of an item associated an item.
671
        Raises an exception if the item can't be found.
672
 
673
        Parameters:
674
         - itemId
675
        """
676
        try:
677
            return [to_t_vendor_item_pricing(vid) for vid in get_all_item_pricing(itemId)]
678
        finally:
679
            close_session()
1334 chandransh 680
 
1155 rajveer 681
    def generateNewEntityID(self, ):
682
        try:
683
            return generate_new_entity_id()
684
        finally:
685
            close_session()
686
 
635 rajveer 687
    def putCategoryObject(self, object):
688
        """
689
        Parameters:
690
         - object
691
        """
766 rajveer 692
        try:
693
            return put_category_object(object)
694
        finally:
695
            close_session()
696
 
635 rajveer 697
    def getCategoryObject(self, ):
766 rajveer 698
        try:
699
            return get_category_object()
700
        finally:
701
            close_session()
1970 rajveer 702
 
703
    def addCategory(self, category):
704
        """
705
        Parameters:
706
         - category
707
        """
708
        try:
709
            return add_category(category)
710
        finally:
711
            close_session()
712
 
713
    def getCategory(self, id):
714
        """
715
        Parameters:
716
         - id
717
        """
718
        try:
719
            return to_t_category(get_category(id))
720
        finally:
721
            close_session()
722
 
723
    def getAllCategories(self, ):
724
        try:
725
            categories = get_all_categories()
726
            return [to_t_category(category) for category in categories]
727
        finally:
728
            close_session()
729
 
730
 
2116 ankur.sing 731
    def addVendorItemPricing(self, vendorItemPricing):
1991 ankur.sing 732
        """
2116 ankur.sing 733
        Adds vendor prices corresponding to the item. If pricing already exists then updates the prices. 
1991 ankur.sing 734
        Raises an exception if either the item or vendor can't be found corresponding to their ids.
735
 
736
        Parameters:
737
         - vendorItemPricing
738
        """
739
        log_entry(self, "updateVendorItemPricing called")
740
        try:
2116 ankur.sing 741
            return add_vendor_pricing(vendorItemPricing)
1991 ankur.sing 742
        finally:
743
            close_session()
744
 
2065 ankur.sing 745
    def getAllVendors(self, ):
746
        """
747
        Return list of all vendors
748
        """
4432 rajveer 749
        try:
750
            return [to_t_vendor(v) for v in get_all_vendors()]
751
        finally:
752
            close_session()
753
 
4423 phani.kuma 754
    def getAllSimilarItems(self, itemId):
755
        """
756
        Returns list of similar items.
757
 
758
        Parameters:
759
         - itemId
760
        """
4432 rajveer 761
        try:
762
            return get_all_similar_items_catalog_ids(itemId)
763
        finally:
764
            close_session()
765
 
4423 phani.kuma 766
    def addSimilarItem(self, itemId, catalogItemId):
767
        """
768
        add similar item.
769
 
770
        Parameters:
771
         - itemId, catalogItemId
772
        """
773
        try:
774
            return add_similar_item_catalog_id(itemId, catalogItemId)
775
        finally:
776
            close_session()
777
 
778
    def deleteSimilarItem(self, itemId, catalogItemId):
779
        """
780
        delete similar items.
781
 
782
        Parameters:
783
         - itemId, catalogItemIds
784
        """
785
        try:
786
            return delete_similar_item_catalog_id(itemId, catalogItemId)
787
        finally:
788
            close_session()
4762 phani.kuma 789
 
2358 ankur.sing 790
    def addVendorItemMapping(self, key, vendorItemMapping):
2116 ankur.sing 791
        """
2358 ankur.sing 792
        Adds VendorItemMapping. Updates VendorItemMapping if exists corresponding to the item key.
2116 ankur.sing 793
 
794
        Parameters:
2358 ankur.sing 795
         - key
2116 ankur.sing 796
         - vendorItemMapping
797
        """
798
        log_entry(self, "addVendorItemMapping called")
799
        try:
2358 ankur.sing 800
            return add_vendor_item_mapping(key, vendorItemMapping)
2116 ankur.sing 801
        finally:
802
            close_session()
803
 
804
    def getVendorItemMappings(self, itemId):
805
        """
806
        Returns the list of vendor item mapping corresponding to itemId passed as parameter.
807
        Raises an exception if item not found corresponding to itemId
808
 
809
        Parameters:
810
         - itemId
811
        """
812
        try:
2120 ankur.sing 813
            item_mappings = get_item_mappings(itemId)
814
            return [to_t_vendor_item_mapping(vim) for vim in item_mappings]
2116 ankur.sing 815
        finally:
816
            close_session()
817
 
4725 phani.kuma 818
    def checkSimilarItem(self, brand, modelNumber, modelName, color):
2116 ankur.sing 819
        """
4725 phani.kuma 820
        Checks if similar item exists (with same Brand, ModelNumber, ModelName, Color)
2116 ankur.sing 821
        If yes, returns the itemId else returns 0
822
 
823
        Parameters:
824
         - brand
825
         - modelNumber
4725 phani.kuma 826
         - modelName
2116 ankur.sing 827
         - color
828
        """
829
        try:
4725 phani.kuma 830
            return check_similar_item(brand, modelNumber, modelName, color)
2116 ankur.sing 831
        finally:
832
            close_session()
833
 
2286 ankur.sing 834
    def changeItemRiskyFlag(self, itemId, risky):
835
        """
836
        Marks/Unmarks an item as risky. This flag is used for automatic marking of an item as INACTIVE in case of zero inventory.
837
 
838
        Parameters:
839
         - itemId
840
         - risky
841
        """
842
        try:
843
            change_risky_flag(itemId, risky)
844
        finally:
845
            close_session()
2116 ankur.sing 846
 
4957 phani.kuma 847
    def getItemsForMasterSheet(self, category, brand):
2358 ankur.sing 848
        """
4957 phani.kuma 849
        Returns list of items with any status except PHASED_OUT and filtered by category, brand.
2358 ankur.sing 850
 
851
        Parameters:
4762 phani.kuma 852
         - category
4957 phani.kuma 853
         - vendor
854
         - brand
2358 ankur.sing 855
        """
856
        try:
4957 phani.kuma 857
            return [to_t_item(item) for item in get_items_for_mastersheet(category, brand)]
2358 ankur.sing 858
        finally:
859
            close_session()
860
 
861
    def getItemsByRiskyFlag(self, ):
862
        """
863
        Returns list of items marked as risky.
864
        """
865
        try:
866
            return [to_t_item(item) for item in get_risky_items()]
867
        finally:
868
            close_session()
2809 rajveer 869
 
870
    def getSimilarItemsCatalogIds(self, beginIndex, totalItems, itemId):
871
        """
872
        Returns list of catalog ids of items with same similarity index as of the given itemId
873
 
874
        Parameters:
875
         - beginIndex
876
         - totalItems
877
         - itemId
878
        """
879
        try:
880
            return get_similar_items_catalog_ids(beginIndex, beginIndex+totalItems, itemId)
881
        finally:
882
            close_session()
3079 rajveer 883
 
884
    def addProductNotification(self, itemId, email):
885
        """
886
        Add user requests for out of stock items. Once user will ask for notify me an entry will
887
 
888
        Parameters:
889
        - itemId
890
        - email
891
        """
892
        try:
893
            return add_product_notification(itemId, email)
894
        finally:
895
            close_session()
2358 ankur.sing 896
 
3086 rajveer 897
    def sendProductNotifications(self, ):
898
        """
899
        Send the product notifications to the users for items which has stock.
900
        """
901
        try:
902
            return send_product_notifications()
903
        finally:
904
            close_session()
3557 rajveer 905
 
906
 
907
    def getAllSources(self, ):
908
        """
909
        Return list of all sources
910
        """
911
        try:
912
            return get_all_sources()
913
        finally:
914
            close_session()
915
 
916
    def getItemPricingBySource(self, itemId, sourceId):
917
        """
918
        Returns the pricing information of an item. If no information is found, exception will be thrown.
3376 rajveer 919
 
3557 rajveer 920
        Parameters:
921
         - itemId
922
         - sourceId
923
        """
924
        try:
925
            return to_t_source_item_pricing(get_item_pricing_by_source(itemId, sourceId))
926
        finally:
927
            close_session()
928
 
929
    def addSourceItemPricing(self, sourceItemPricing):
930
        """
931
        Adds prices to be displayed corresponding to the item if user comes from a source.
932
 
933
        Parameters:
934
         - sourceItemPricing
935
        """
936
        try:
937
            add_source_item_pricing(sourceItemPricing)
938
        finally:
939
            close_session()
940
 
941
    def getAllSourcePricing(self, itemId):
942
        """
943
        Returns the list of source pricing information of an item.
944
        Raises an exception if item not found corresponding to itemId
945
 
946
        Parameters:
947
         - itemId
948
        """
949
        try:
950
            return [to_t_source_item_pricing(source_item_pricing) for source_item_pricing in get_all_source_pricing(itemId)]
951
#        return [to_t_item(item) for item in get_risky_items()]
952
        finally:
953
            close_session()
954
 
955
    def getItemForSource(self, item_id, sourceId):
956
        """
957
        Parameters:
958
         - item_id
959
         - sourceId
960
        """
961
        try:
962
            return to_t_item(get_item_for_source(item_id, sourceId))
963
        finally:
964
            close_session()
3872 chandransh 965
 
966
    def searchItemsInRange(self, searchTerms, offset, limit):
967
        """
968
        Searches items matching the the given terms in the catalog
3557 rajveer 969
 
3872 chandransh 970
        Parameters:
971
         - searchTerms
972
        """
973
        try:
974
            return [to_t_item(item) for item in search_items(searchTerms, offset, limit)]
975
        finally:
976
            close_session()
977
 
978
    def getSearchResultCount(self, searchTerms):
979
        """
980
        Gets the count of search results for the given search terms so that the user can go through all the pages.
981
 
982
        Parameters:
983
         - searchTerms
984
        """
985
        try:
986
            return get_search_result_count(searchTerms)
987
        finally:
988
            close_session()
989
 
4063 chandransh 990
    def getPendingOrdersInventory(self, vendorId):
4024 chandransh 991
        """
4063 chandransh 992
        Returns a list of inventory stock for items for which there are pending orders for the given vendor.
4024 chandransh 993
        """
994
        try:
4063 chandransh 995
            pending_items_inventory = get_pending_orders_inventory(vendorId)
4024 chandransh 996
            return [AvailableAndReservedStock(itemId = i[0], available=i[1], reserved=i[2], minimumStock=0) for i in pending_items_inventory]
997
        finally:
998
            close_session()
999
 
4295 varun.gupt 1000
    def getProductNotifications(self, startDateTime):
1001
        '''
1002
        Returns a list of Product Notification objects each representing user requests for notification
1003
        '''
1004
        try:
1005
            return [to_t_product_notification_request(notification) for notification in get_product_notifications(to_py_date(startDateTime))]
1006
        finally:
1007
            close_session()
1008
 
1009
    def getProductNotificationRequestCount(self, startDateTime):
1010
        '''
1011
        Returns list of items and the counts of product notification requests
1012
        '''
1013
        try:
1014
            notification_request_counts = [(notification.item, count) for notification, count in get_product_notification_request_count(to_py_date(startDateTime))]
1015
 
1016
            return [to_t_product_notification_request_count(count) for count in notification_request_counts]
1017
 
1018
        finally:
1019
            close_session()
1020
 
3376 rajveer 1021
    def isAlive(self, ):
1022
        """
1023
        For checking weather service is active alive or not. It also checks connectivity with database
1024
        """
1025
        try:
1026
            return is_alive()
1027
        finally:
1028
            close_session()
1029
 
766 rajveer 1030
    def closeSession(self, ):
3376 rajveer 1031
        """
1032
        For closing the open session in sqlalchemy
1033
        """
4332 anupam.sin 1034
        close_session()
1035
 
1036
    def addVendor(self, vendor):
1037
        """
1038
        add a new vendor
1039
        """
1040
        try:
1041
            return add_vendor(vendor)
1042
        finally:
1043
            close_session()
1044
 
4649 phani.kuma 1045
    def addAuthorizationLog(self, itemId, username, reason):
1046
        """
1047
        add a log to authorize table.
1048
 
1049
        Parameters:
1050
         - itemId, username, reason
1051
        """
1052
        try:
1053
            return add_authorization_log_for_item(itemId, username, reason)
1054
        finally:
4934 amit.gupta 1055
            close_session()
1056
 
5110 mandeep.dh 1057
    def getWarehouses(self, warehouseType, inventoryType, vendorId, billingWarehouseId, shippingWarehouseId):
1058
        """
1059
        This method returns all warehouses for a given warehosueType, inventoryType, vendor, billingWarehouse and shippingWarehouse.
1060
        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
1061
        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
1062
        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
1063
        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
1064
 
1065
        Parameters:
1066
         - warehouseType
1067
         - inventoryType
1068
         - vendorId
1069
         - billingWarehouseId
1070
         - shippingWarehouseId
1071
        """
1072
        try:
1073
            query = Warehouse.query
1074
            if warehouseType is not None:
1075
                query = query.filter_by(warehouseType = WarehouseType._VALUES_TO_NAMES[warehouseType])
1076
            if inventoryType is not None:
1077
                query = query.filter_by(inventoryType = InventoryType._VALUES_TO_NAMES[inventoryType])
1078
            if vendorId:
1079
                query = query.filter_by(vendor_id = vendorId)
1080
            if billingWarehouseId:
1081
                query = query.filter_by(billingWarehouseId = billingWarehouseId)
1082
            if shippingWarehouseId:
1083
                query = query.filter_by(shippingWarehouseId = shippingWarehouseId)
1084
 
1085
            return [to_t_warehouse(w) for w in query.all()]
1086
        finally:
1087
            close_session()
1088
 
4985 mandeep.dh 1089
    def getItemKeysToBeProcessed(self, warehouseId):
1090
        """
1091
        Returns the list of item keys which need to be processed for a given warehouse.
1092
        This is currently used by Support application to send item keys whose inventory needs
1093
        to be updated from PLB
1094
 
1095
        Parameters:
1096
         - warehouseId
1097
        """
1098
        try:
1099
            return get_item_keys_to_be_processed(warehouseId)
1100
        finally:
1101
            close_session()
1102
 
1103
    def markMissedInventoryUpdatesAsProcessed(self, itemKey, warehouseId):
1104
        """
1105
        Marks/Deletes missed inventory updates for a given key and warehouse.
1106
        This generally happens when updates from PLB are applied on the currentinventorysnapshot for an item
1107
 
1108
        Parameters:
1109
         - itemKey
1110
         - warehouseId
1111
        """
1112
        try:
1113
            mark_missed_inventory_updates_as_processed(itemKey, warehouseId)
1114
        finally:
1115
            close_session()
1116
 
1117
    def resetAvailability(self, itemKey, vendorId, quantity, warehouseId):
1118
        """
1119
        Resets availability of an item to the quantity mentioned in a warehouse.
1120
 
1121
        Parameters:
1122
         - itemKey
1123
         - vendorId
1124
         - quantity
1125
         - warehouseId
1126
        """
1127
        try:
1128
            reset_availability(itemKey, vendorId, quantity, warehouseId)
1129
        finally:
1130
            close_session()
1131
 
5437 mandeep.dh 1132
    def resetAvailabilityForWarehouse(self, warehouseId):
1133
        """
1134
        Resets availability of a warehouse to zero.
1135
 
1136
        Parameters:
1137
         - warehouseId
1138
        """
1139
        try:
1140
            reset_availability_for_warehouse(warehouseId)
1141
        finally:
1142
            close_session()
1143
 
5110 mandeep.dh 1144
    def getIgnoredItemKeys(self, ):
1145
        """
1146
        Returns all the item key mappings that have been ignored until date. Value of map has the warehouse id
1147
        and the timestamp from where alert was raised.
1148
        """
1149
        try:
1150
            itemKeysMap = {}
1151
            for key in MissedInventoryUpdate.query.filter_by(isIgnored = 1).all():
1152
                itemKeysMap[key.itemKey] = { key.warehouseId : to_java_date(key.timestamp) }
1153
 
1154
            return itemKeysMap
1155
        finally:
1156
            close_session()
1157
 
1158
    def addBadInventory(self, itemId, warehouseId, quantity):
1159
        """
1160
        Add the BAD type inventory to existing stock.
1161
 
1162
        Parameters:
1163
         - itemId
1164
         - warehouseId
1165
         - quantity
1166
        """
1167
        try:
1168
            add_bad_inventory(itemId, warehouseId, quantity)
1169
        finally:
1170
            close_session()
1171
 
5185 mandeep.dh 1172
    def getShippingLocations(self, ):
1173
        """
1174
        Returns all shipping locations
1175
        """
1176
        try:
1177
            return [to_t_warehouse(w) for w in get_shipping_locations()]
1178
        finally:
1179
            close_session()
5110 mandeep.dh 1180
 
5237 mandeep.dh 1181
    def getAllVendorItemMappings(self, ):
1182
        """
1183
        Fetches all the vendor item mappings present.
1184
        """
1185
        try:
1186
            return [to_t_vendor_item_mapping(m) for m in VendorItemMapping.query.all()]
1187
        finally:
1188
            close_session()
1189
 
5313 mandeep.dh 1190
    def getInventorySnapshot(self, warehouseId):
1191
        """
1192
        Gets items' inventory for a warehouse
1193
        If warehouse is passed as zero, items' inventory across all warehouses is sent
1194
 
1195
        Parameters:
1196
         - warehouseId
1197
        """
1198
        try:
1199
            resultMap = {}
1200
            itemInventoryMap = get_inventory_snapshot(warehouseId)
1201
            for key, value in itemInventoryMap.items():
1202
                resultMap[key] = to_t_item_inventory(value, key)
1203
 
1204
            return resultMap
1205
        finally:
1206
            close_session()
1207
 
5336 rajveer 1208
    def clearItemAvailabilityCache(self, ):
1209
        """
1210
        Clear item availability cache.
1211
        """
1212
        try:
1213
            clear_item_availability_cache()
1214
        finally:
1215
            close_session()
5460 phani.kuma 1216
 
1217
    def getClearanceSaleCatalogIds(self):
1218
        """
1219
        clearance sale products
1220
        """
1221
        try:
1222
            return get_clearance_sale_catalog_ids()
1223
        finally:
1224
            close_session()
5336 rajveer 1225
 
4934 amit.gupta 1226
def is_valid(item):
1227
    if item.status in [status.ACTIVE, status.PAUSED, status.PAUSED_BY_RISK]:
4936 amit.gupta 1228
        if item.startDate:
1229
            return not(datetime.datetime.now() < item.startDate or item.sellingPrice == 0)
1230
        else:
1231
            return True