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