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