Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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