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