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