Subversion Repositories SmartDukaan

Rev

Rev 4708 | Rev 4748 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 ashish 1
'''
2
Created on 23-Mar-2010
3
 
4
@author: ashish
5
'''
6
from elixir import *
7
from shop2020.model.v1.catalog.impl import DataService
122 ashish 8
from shop2020.model.v1.catalog.impl.DataService import Item, \
103 ashish 9
    Warehouse, ItemInventoryHistory, CurrentInventorySnapshot, ItemInfo,\
1368 chandransh 10
    ItemChangeLog, Category, EntityIDGenerator, VendorItemPricing,\
4649 phani.kuma 11
    VendorItemMapping, Vendor, SimilarItems, ProductNotification, Source, SourceItemPricing, AuthorizationLog
122 ashish 12
from shop2020.thriftpy.model.v1.catalog.ttypes import \
2983 chandransh 13
    InventoryServiceException, status, ItemShippingInfo
2065 ankur.sing 14
from shop2020.model.v1.catalog.impl.Convertors import to_t_item,\
3557 rajveer 15
    to_t_vendor_item_pricing, to_t_source
94 ashish 16
import datetime
871 chandransh 17
import sys
2286 ankur.sing 18
from shop2020.utils.Utils import log_entry, to_py_date, log_risky_flag
621 chandransh 19
from sqlalchemy import desc, asc
3244 chandransh 20
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
1970 rajveer 21
from shop2020.model.v1.catalog.impl.CategoryManager import CategoryManager
3872 chandransh 22
from sqlalchemy.sql.expression import and_, or_, distinct, func
3086 rajveer 23
from string import Template
24
from shop2020.clients.HelperClient import HelperClient
3924 rajveer 25
import urllib2
26
from urllib2 import HTTPBasicAuthHandler
27
from shop2020.config.client.ConfigClient import ConfigClient
4318 rajveer 28
from shop2020.utils.EmailAttachmentSender import mail
4400 rajveer 29
from shop2020.clients.TransactionClient import TransactionClient
30
from shop2020.thriftpy.model.v1.order.ttypes import AlertType
94 ashish 31
 
32
def initialize():
33
    DataService.initialize()
34
 
3849 chandransh 35
def get_all_items_by_status(status, offset=0, limit=None):
36
    query = Item.query
4539 rajveer 37
    if status is not None:
3849 chandransh 38
        query = query.filter_by(status=status)
39
    query = query.order_by(Item.product_group, Item.brand, Item.model_number, Item.model_name).offset(offset)
40
    if limit:
41
        query = query.limit(limit)
42
    items = query.all()
43
    return items
44
 
45
def get_all_items(is_active, offset=0, limit=None):
103 ashish 46
    if is_active:
3849 chandransh 47
        items = get_all_items_by_status(status.ACTIVE, offset, limit)
103 ashish 48
    else:
3849 chandransh 49
        items = get_all_items_by_status(None, offset, limit)
766 rajveer 50
    return items
51
 
3849 chandransh 52
def get_item_count_by_status(use_status, status):
53
    if use_status:
54
        return Item.query.filter_by(status=status).count()
103 ashish 55
    else:
3849 chandransh 56
        return Item.query.count()
103 ashish 57
 
635 rajveer 58
def get_item(item_id):
766 rajveer 59
    item = Item.get_by(id=item_id)
60
    return item
94 ashish 61
 
635 rajveer 62
def get_items_by_catalog_id(catalog_id):
447 rajveer 63
    query = Item.query.filter_by(catalog_item_id=catalog_id)
437 rajveer 64
    try:
635 rajveer 65
        items = query.all()
66
        return get_thrift_item_list(items)
1399 rajveer 67
    except Exception as ex:
68
        print ex
437 rajveer 69
        raise InventoryServiceException(109, "Item not found")
70
 
576 chandransh 71
def is_active(item_id):
2983 chandransh 72
    t_item_shipping_info = ItemShippingInfo()
576 chandransh 73
    try:
635 rajveer 74
        item = get_item(item_id)
3281 chandransh 75
        t_item_shipping_info.isRisky = item.risky
4708 anupam.sin 76
        warehouse_ids = None
77
        if item.isWarehousePreferenceSticky :
78
            warehouse_ids = [item.preferredWarehouse]
79
        availability = __get_item_availability(item, warehouse_ids)
3281 chandransh 80
        if item.risky and availability <= 0 and item.status == status.ACTIVE:
81
            add_status_change_log(item, status.PAUSED_BY_RISK)
82
            item.status = status.PAUSED_BY_RISK
83
            item.status_description = "This item is currently out of stock"
84
            session.commit()
3924 rajveer 85
            #This will clear cache from tomcat
86
            __clear_homepage_cache()
2983 chandransh 87
        t_item_shipping_info.isActive = (item.status == status.ACTIVE)
3281 chandransh 88
        t_item_shipping_info.quantity = availability
576 chandransh 89
    except InventoryServiceException:
2983 chandransh 90
        print "[ERROR] Unexpected error:", sys.exc_info()[0]
91
    return t_item_shipping_info
92
 
2035 rajveer 93
def get_item_status_description(itemId):
94
    item = get_item(itemId)
95
    return item.status_description
563 chandransh 96
 
94 ashish 97
def get_Warehouse(warehouse_id):
98
    return Warehouse.get_by(id=warehouse_id)
99
 
4332 anupam.sin 100
def get_Vendor(vendorId):
101
    return Vendor.get_by(id=vendorId)
102
 
122 ashish 103
def get_all_warehouses_by_status(status):
104
    if not status:
766 rajveer 105
        warehouses = Warehouse.query.all()
122 ashish 106
    else:
851 chandransh 107
        warehouses = Warehouse.query.filter_by(status=status).all()
108
    return warehouses
122 ashish 109
 
94 ashish 110
def get_all_warehouses_for_item(item_id):
635 rajveer 111
    item = get_item(item_id)
94 ashish 112
    if not item:
122 ashish 113
        raise InventoryServiceException(108, "Some unforeseen error while obtaining item")
114
    return item.get_all_warehouses
94 ashish 115
 
122 ashish 116
def get_all_items_for_warehouse(warehouse_id):
117
    warehouse = get_Warehouse(warehouse_id)
118
    if not warehouse:
119
        raise InventoryServiceException(108, "bad warehouse")
120
    return warehouse.all_items
121
 
94 ashish 122
def add_warehouse(warehouse):
103 ashish 123
    if not warehouse:
122 ashish 124
        raise InventoryServiceException(108, "Bad warehouse")
103 ashish 125
    if get_Warehouse(warehouse.id):
126
        #warehouse is already present.
122 ashish 127
        raise InventoryServiceException(101, "Warehouse already present")
103 ashish 128
 
94 ashish 129
    ds_warehouse = Warehouse()
130
    ds_warehouse.id = warehouse.id
131
    ds_warehouse.location = warehouse.location
122 ashish 132
    ds_warehouse.status = status.ACTIVE
103 ashish 133
    ds_warehouse.addedOn = datetime.datetime.now()
483 rajveer 134
    ds_warehouse.lastCheckedOn = datetime.datetime.now()
135
    ds_warehouse.tinNumber = warehouse.tinNumber
136
    ds_warehouse.pincode = warehouse.pincode
137
    if warehouse.vendorString:
138
        ds_warehouse.vendorString = warehouse.vendorString
94 ashish 139
    session.commit()
103 ashish 140
    return ds_warehouse.id
94 ashish 141
 
122 ashish 142
def update_item(item):
143
    if not item:
144
        raise InventoryServiceException(108, "Bad item in request")
145
 
146
    if not item.id:
609 chandransh 147
        raise InventoryServiceException(101, "Missing id for update")
122 ashish 148
 
2120 ankur.sing 149
    validate_item_prices(item)
2065 ankur.sing 150
 
635 rajveer 151
    ds_item = get_item(item.id)
122 ashish 152
 
153
    if not ds_item:
609 chandransh 154
        raise InventoryServiceException(101, "Item missing in our database")
122 ashish 155
 
963 chandransh 156
    if item.productGroup:
157
        ds_item.product_group = item.productGroup 
158
    if item.brand:
159
        ds_item.brand = item.brand
511 rajveer 160
    if item.modelNumber:
161
        ds_item.model_number = item.modelNumber
2497 ankur.sing 162
    ds_item.color = item.color
163
    ds_item.model_name = item.modelName
164
    ds_item.category = item.category
165
    ds_item.comments = item.comments
511 rajveer 166
 
2497 ankur.sing 167
    ds_item.catalog_item_id = item.catalogItemId
483 rajveer 168
 
2129 ankur.sing 169
    ds_item.mrp = item.mrp
170
    ds_item.sellingPrice = item.sellingPrice
2497 ankur.sing 171
 
2174 ankur.sing 172
    ds_item.weight = item.weight
2129 ankur.sing 173
 
2358 ankur.sing 174
    if ds_item.status != item.itemStatus:
2402 rajveer 175
        add_status_change_log(ds_item, item.itemStatus)
2358 ankur.sing 176
        ds_item.status = item.itemStatus
2035 rajveer 177
    if item.status_description:
178
        ds_item.status_description = item.status_description
122 ashish 179
 
511 rajveer 180
    if item.startDate:
2116 ankur.sing 181
        ds_item.startDate = to_py_date(item.startDate)
2497 ankur.sing 182
    else:
183
        ds_item.startDate = None
511 rajveer 184
    if item.retireDate:
2116 ankur.sing 185
        ds_item.retireDate = to_py_date(item.retireDate)
2497 ankur.sing 186
    else:
187
        ds_item.retireDate = None
511 rajveer 188
 
2497 ankur.sing 189
    ds_item.feature_id = item.featureId
190
    ds_item.feature_description = item.featureDescription
122 ashish 191
 
2129 ankur.sing 192
    ds_item.bestDealText = item.bestDealText
193
    ds_item.bestDealValue = item.bestDealValue
2065 ankur.sing 194
    ds_item.bestSellingRank = item.bestSellingRank
2497 ankur.sing 195
 
2065 ankur.sing 196
    ds_item.defaultForEntity = item.defaultForEntity
2251 ankur.sing 197
    ds_item.risky = item.risky
3359 chandransh 198
 
3459 chandransh 199
    if item.expectedDelay is not None:
3359 chandransh 200
        ds_item.expectedDelay = item.expectedDelay
201
 
202
    if item.preferredWarehouse:
203
        ds_item.preferredWarehouse = item.preferredWarehouse
4413 anupam.sin 204
 
205
    if item.defaultWarehouse:
206
        ds_item.defaultWarehouse = item.defaultWarehouse
4506 phani.kuma 207
 
208
    if item.preferredVendor:
209
        ds_item.preferredVendor = item.preferredVendor
4413 anupam.sin 210
 
211
    ds_item.isWarehousePreferenceSticky = item.isWarehousePreferenceSticky
3359 chandransh 212
 
2347 ankur.sing 213
    ds_item.updatedOn = datetime.datetime.now()
2065 ankur.sing 214
 
122 ashish 215
    session.commit();
216
    return ds_item.id
94 ashish 217
 
2116 ankur.sing 218
def check_vendor_item_mapping(product_group, brand, model_number, color, vendor_id, vendor_category):
219
    key = product_group.strip().lower() + '|' + brand.strip().lower() + '|' + model_number.strip().lower() + '|' + color.strip().lower()
220
    try:
221
        vim = VendorItemMapping.query.filter_by(vendor_id=vendor_id, item_key=key, vendor_category=vendor_category).one()
222
        return True
223
    except:
224
        return False
225
 
103 ashish 226
def add_item(item):
227
    if not item:
122 ashish 228
        raise InventoryServiceException(108, "Bad item in request")
635 rajveer 229
    if get_item(item.id):
122 ashish 230
        raise InventoryServiceException(101, "Item already exists")
2120 ankur.sing 231
 
232
    validate_item_prices(item)
233
 
103 ashish 234
    ds_item = Item()
963 chandransh 235
    if item.productGroup:
236
        ds_item.product_group = item.productGroup
237
    if item.brand:
238
        ds_item.brand = item.brand
515 rajveer 239
    if item.modelName:
240
        ds_item.model_name = item.modelName
241
    if item.modelNumber:
242
        ds_item.model_number = item.modelNumber
609 chandransh 243
    if item.color:
244
        ds_item.color = item.color
2116 ankur.sing 245
    if item.hotspotCategory:
246
        ds_item.hotspotCategory = item.hotspotCategory
247
        if item.hotspotCategory == 'Handsets':
2403 ankur.sing 248
            ds_item.preferredWarehouse = 1
2116 ankur.sing 249
        else:
2403 ankur.sing 250
            ds_item.preferredWarehouse = 2
2116 ankur.sing 251
 
483 rajveer 252
    if item.category:
253
        ds_item.category = item.category
254
    if item.comments:
255
        ds_item.comments = item.comments
256
 
103 ashish 257
    ds_item.addedOn = datetime.datetime.now()
609 chandransh 258
    ds_item.updatedOn = datetime.datetime.now()
2116 ankur.sing 259
    if item.startDate:
260
        ds_item.startDate = to_py_date(item.startDate)
261
    if item.retireDate:
262
        ds_item.retireDate = to_py_date(item.retireDate)
609 chandransh 263
 
483 rajveer 264
    if item.mrp:
265
        ds_item.mrp = item.mrp
266
    if item.sellingPrice:
267
        ds_item.sellingPrice = item.sellingPrice
122 ashish 268
    if item.weight:
269
        ds_item.weight = item.weight
270
 
271
    if item.featureId:
272
        ds_item.feature_id = item.featureId
273
    if item.featureDescription:
274
        ds_item.feature_description = item.featureDescription
275
 
103 ashish 276
    if item.otherInfo:
277
        for k,v in item.otherInfo.iteritems():
278
            info = ItemInfo()
279
            info.key = k
280
            info.value = v
281
            ds_item.iteminfo.append(info)
2116 ankur.sing 282
 
103 ashish 283
    #check if categories present. If yes, add them to system
122 ashish 284
 
609 chandransh 285
    if item.bestDealValue:
286
        ds_item.bestDealValue = item.bestDealValue
287
    if item.bestDealText:
288
        ds_item.bestDealText = item.bestDealText
2116 ankur.sing 289
    if item.bestSellingRank:
290
        ds_item.bestSellingRank = item.bestSellingRank
291
    ds_item.defaultForEntity = item.defaultForEntity
2251 ankur.sing 292
    ds_item.risky = item.risky
609 chandransh 293
 
3467 chandransh 294
    if item.expectedDelay is not None:
3359 chandransh 295
        ds_item.expectedDelay = item.expectedDelay
3467 chandransh 296
    else:
297
        ds_item.expectedDelay = 0
3359 chandransh 298
 
299
    if item.preferredWarehouse:
300
        ds_item.preferredWarehouse = item.preferredWarehouse
301
 
2116 ankur.sing 302
    # Check if a similar item already exists in our database
4725 phani.kuma 303
    similar_item = Item.query.filter_by(brand=item.brand, model_number=item.modelNumber, model_name=item.modelName).first()
304
    print "[SIMILAR ITEM FOUND:] FOR {0} {1} {2}".format(item.brand, item.modelNumber, item.modelName)
2116 ankur.sing 305
 
306
    if similar_item is None or similar_item.catalog_item_id is None:
307
        # If there is no similar item in the database from before,
308
        # use the entity_id_generator
309
        entity_id = EntityIDGenerator.query.first()
310
        ds_item.catalog_item_id = entity_id.id + 1
311
        ds_item.status = status.IN_PROCESS
312
        ds_item.status_description = "This item is in process."
313
        entity_id.id = entity_id.id  + 1
4725 phani.kuma 314
        if similar_item is not None and similar_item.catalog_item_id is None:
315
            similar_item.catalog_item_id = entity_id.id
2116 ankur.sing 316
    else:
317
        #If a similar item already exists for a product group, brand and model_number, set it as same.
318
        ds_item.catalog_item_id = similar_item.catalog_item_id
319
        ds_item.category = similar_item.category
320
        ds_item.status = similar_item.status
321
        ds_item.status_description = similar_item.status_description
322
 
103 ashish 323
    session.commit();
3325 chandransh 324
    return ds_item.id
325
 
326
def update_inventory_history(warehouse_id, timestamp, availability):
327
    warehouse = get_Warehouse(warehouse_id)
328
    if not warehouse:
329
        raise InventoryServiceException(107, "Warehouse? Where?")
4368 rajveer 330
    vendors = get_vendors_for_warehouse(warehouse_id)
331
    if len(vendors) > 1:
332
        raise InventoryServiceException(110, "Multiple vendors found for warehouse !")
333
    vendor = vendors[0]
3325 chandransh 334
    time = datetime.datetime.now()
335
    for item_key, quantity in availability.iteritems():
336
        try:
337
            vendor_item_mapping = VendorItemMapping.query.filter_by(vendor=vendor, item_key=item_key).one();
338
            item = vendor_item_mapping.item
339
        except:
340
            continue  
341
        try:
342
            item_inventory_history = ItemInventoryHistory()
343
            item_inventory_history.warehouse = warehouse
344
            item_inventory_history.item = item
345
            item_inventory_history.timestamp = time
346
            item_inventory_history.availibility = quantity
347
        except:
348
            raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
349
    session.commit()
103 ashish 350
 
483 rajveer 351
def update_inventory(warehouse_id, timestamp, availability):
352
    warehouse = get_Warehouse(warehouse_id)
353
    if not warehouse:
354
        raise InventoryServiceException(107, "Warehouse? Where?")
355
 
356
    time = datetime.datetime.now()
357
    warehouse.lastCheckedOn = time
358
    warehouse.vendorString = timestamp
4368 rajveer 359
    vendors = get_vendors_for_warehouse(warehouse_id)
360
    if len(vendors) > 1:
361
        raise InventoryServiceException(110, "Multiple vendors found for warehouse !")
362
    vendor = vendors[0]
2368 ankur.sing 363
    session.commit()
1368 chandransh 364
    for item_key, quantity in availability.iteritems():
483 rajveer 365
        try:
1368 chandransh 366
            vendor_item_mapping = VendorItemMapping.query.filter_by(vendor=vendor, item_key=item_key).one();
367
            item = vendor_item_mapping.item
494 rajveer 368
        except:
369
            continue  
370
        try:
483 rajveer 371
            current_inventory_snapshot = CurrentInventorySnapshot.get_by(item=item, warehouse=warehouse)
372
            if not current_inventory_snapshot:
373
                current_inventory_snapshot = CurrentInventorySnapshot()
374
                current_inventory_snapshot.item = item
375
                current_inventory_snapshot.warehouse = warehouse
376
                current_inventory_snapshot.availibility = 0
871 chandransh 377
                current_inventory_snapshot.reserved = 0
483 rajveer 378
            # added the difference in the current inventory    
379
            current_inventory_snapshot.availibility = current_inventory_snapshot.availibility + quantity
4400 rajveer 380
            try:
381
                if quantity > 0 and __get_item_reserved(item) > 0:
382
                    cl = TransactionClient().get_client()
4448 rajveer 383
                    #FIXME hardcoding for warehouse id 
384
                    cl.addAlert(AlertType.NEW_INVENTORY_ALERT, 5, "Inventory received for item " + item.brand + " " + item.model_name + " " + item.model_number + " " +  item.color)
4400 rajveer 385
            except:
386
                print "Not able to raise alert for incoming inventory" 
483 rajveer 387
        except:
388
            raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
2368 ankur.sing 389
        session.commit() 
390
        check_risky_item(item)
483 rajveer 391
 
4320 rajveer 392
def add_inventory(itemId, warehouseId, quantity):
393
    current_inventory_snapshot = CurrentInventorySnapshot.get_by(item_id=itemId, warehouse_id=warehouseId)
394
    if not current_inventory_snapshot:
395
        current_inventory_snapshot = CurrentInventorySnapshot()
396
        current_inventory_snapshot.item_id = itemId
397
        current_inventory_snapshot.warehouse_id = warehouseId
398
        current_inventory_snapshot.availibility = 0
399
        current_inventory_snapshot.reserved = 0
400
    # added the difference in the current inventory    
401
    current_inventory_snapshot.availibility = current_inventory_snapshot.availibility + quantity
402
    session.commit() 
403
 
4431 phani.kuma 404
'''
94 ashish 405
def get_item_inventoy(item_id):
406
 
407
    inventory = Item.get_by(id=item_id).currentInventory
408
    if not inventory:
122 ashish 409
        raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
94 ashish 410
    return inventory
4431 phani.kuma 411
'''
94 ashish 412
 
635 rajveer 413
def get_item_inventory_by_item_id(item_id):
414
    inventory = Item.get_by(id=item_id).currentInventory
379 ashish 415
    if not inventory:
416
        raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
417
    return inventory
418
 
103 ashish 419
 
420
def retire_warehouse(warehouse_id):
421
    if not warehouse_id:
122 ashish 422
        raise InventoryServiceException(101, "Bad warehouse id")
103 ashish 423
    warehouse = get_Warehouse(warehouse_id)
424
    if not warehouse:
122 ashish 425
        raise InventoryServiceException(108, "warehouse id not present")
426
    warehouse.status = status.DELETED;
103 ashish 427
    session.commit()
428
 
429
def retire_item(item_id):
430
    if not item_id:
122 ashish 431
        raise InventoryServiceException(101, "bad item id")
635 rajveer 432
    item = get_item(item_id)
103 ashish 433
    if not item:
122 ashish 434
        raise InventoryServiceException(108, "item id not present")
435
    item.status = status.PHASED_OUT
436
    item.retireDate = datetime.datetime.now()
103 ashish 437
    session.commit()
122 ashish 438
 
439
#need to implement threads based solution here
103 ashish 440
def start_item_on(item_id, timestamp):
441
    if not item_id:
122 ashish 442
        raise InventoryServiceException(101, "bad item id")
635 rajveer 443
    item = get_item(item_id)
103 ashish 444
    if not item:
122 ashish 445
        raise InventoryServiceException(108, "item id not present")
103 ashish 446
 
122 ashish 447
    item.status = status.ACTIVE
448
    item.startDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
449
    add_status_change_log(item, status.ACTIVE)
103 ashish 450
    session.commit()
451
 
122 ashish 452
#need to implement threads here
103 ashish 453
def retire_item_on(item_id, timestamp):
454
    if not item_id:
122 ashish 455
        raise InventoryServiceException(101, "bad item id")
635 rajveer 456
    item = get_item(item_id)
103 ashish 457
    if not item:
122 ashish 458
        raise InventoryServiceException(108, "item id not present")
103 ashish 459
 
122 ashish 460
    item.status = status.PHASED_OUT
461
    item.retireDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
462
    add_status_change_log(item, status.PHASED_OUT)
103 ashish 463
    session.commit()
464
 
465
def add_status_change_log(item, new_status):
466
    item_change_log = ItemChangeLog()
467
    item_change_log.new_status = new_status
468
    item_change_log.old_status = item.status
469
    item_change_log.timestamp = datetime.datetime.now()
470
    item_change_log.item = item
471
    session.commit()
472
 
473
def change_item_status(item_id, new_status):
474
    if not item_id:
122 ashish 475
        raise InventoryServiceException(101, "bad item id")
635 rajveer 476
    item = get_item(item_id)
103 ashish 477
    if not item:
122 ashish 478
        raise InventoryServiceException(108, "item id not present")
2116 ankur.sing 479
    add_status_change_log(item, new_status)
122 ashish 480
    item.status = new_status
2251 ankur.sing 481
    if item.status == status.PHASED_OUT:
482
        item.status_description = "This item has been phased out"
483
    elif item.status == status.DELETED:
484
        item.status_description = "This item has been deleted"
3924 rajveer 485
    elif item.status == status.PAUSED:
486
        item.status_description = "This item is currently out of stock"      
487
    elif item.status == status.PAUSED_BY_RISK:
488
        item.status_description = "This item is currently out of stock"
489
        #This will clear cache from tomcat
490
        __clear_homepage_cache()  
2251 ankur.sing 491
    elif item.status == status.ACTIVE:
492
        item.status_description = "This item is active"
493
    elif item.status == status.IN_PROCESS:
494
        item.status_description = "This item is in process"
495
    elif item.status == status.CONTENT_COMPLETE:
496
        item.status_description = "This item is in process"
103 ashish 497
    session.commit()
498
 
499
def get_item_availability_for_warehouse(warehouse_id, item_id):
500
    if not warehouse_id:
122 ashish 501
        raise InventoryServiceException(101, "bad warehouse_id")
103 ashish 502
    if not item_id:
122 ashish 503
        raise InventoryServiceException(101, "bad item_id")
103 ashish 504
 
505
    warehouse = get_Warehouse(warehouse_id)
506
    if not warehouse:
122 ashish 507
        raise InventoryServiceException(108, "warehouse does not exist")
635 rajveer 508
    item = get_item(item_id)
504 rajveer 509
 
103 ashish 510
    if not item:
766 rajveer 511
        raise InventoryServiceException(108, "item does not exist")
122 ashish 512
 
494 rajveer 513
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id)
504 rajveer 514
    query = query.filter_by(item_id = item.id)
494 rajveer 515
    try:
516
        current_inventory_snapshot = query.one()
871 chandransh 517
        return current_inventory_snapshot.availibility - current_inventory_snapshot.reserved
494 rajveer 518
    except:
519
        return 0
520
    """
521
    current_inventory_snapshot = CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse_id == warehouse_id, CurrentInventorySnapshot.item_id == item_id).one()
103 ashish 522
    if not current_inventory_snapshot:
523
        return 0
524
    else:
525
        return current_inventory_snapshot.availibility
494 rajveer 526
    """
643 chandransh 527
 
2368 ankur.sing 528
def check_risky_item(item):
2251 ankur.sing 529
    if not item.risky:
530
        return
4406 anupam.sin 531
    availability = __get_item_availability(item, None)
2983 chandransh 532
    if availability <= 0:
2251 ankur.sing 533
        if item.status == status.ACTIVE:
2984 rajveer 534
            change_item_status(item.id, status.PAUSED_BY_RISK)
4318 rajveer 535
            try:
4374 rajveer 536
                mail("cnc.center@shop2020.in", "5h0p2o2o", ["chaitnaya.vats@shop2020.in", "abhishek.mathur@shop2020.in"], "Item is out of stock. ID: " + str(item.id)  + " " + item.brand +  " " + item.model_name + " " + item.model_number, " ", [])
4318 rajveer 537
            except:
538
                print "Unable to send the report"
2251 ankur.sing 539
    else:
2984 rajveer 540
        if item.status == status.PAUSED_BY_RISK:
2251 ankur.sing 541
            change_item_status(item.id, status.ACTIVE)
2368 ankur.sing 542
    session.commit()
2251 ankur.sing 543
 
4406 anupam.sin 544
'''
545
This method returns quantity of a particular item across all warehouses whose ids is provided
546
if warehouse_ids is null it checks for inventory in all warehouses.
547
'''
548
def __get_item_availability(item, warehouse_ids):
549
    if warehouse_ids is None:
550
        all_inventory = CurrentInventorySnapshot.query.filter_by(item = item).all()
551
        availability = 0
552
        reserved = 0
553
        for currInv in all_inventory:
554
            availability = availability + currInv.availibility
555
            reserved = reserved + currInv.reserved
556
        return availability - reserved
557
    else:
558
        total_availability = 0
559
        for warehouse_id in warehouse_ids:
560
            try:
561
                current_inventory_snapshot = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item.id).one()
562
                availability = current_inventory_snapshot.availibility - current_inventory_snapshot.reserved
563
            except Exception as e:
564
                print e
565
                availability = 0    
566
            total_availability = total_availability + availability
567
        return total_availability 
4400 rajveer 568
 
569
def __get_item_reserved(item):
570
    all_inventory = CurrentInventorySnapshot.query.filter_by(item = item).all()
571
    reserved = 0
572
    for currInv in all_inventory:
573
        reserved = reserved + currInv.reserved
574
    return reserved
2983 chandransh 575
 
871 chandransh 576
def reserve_item_in_warehouse(item_id, warehouse_id, quantity):    
577
    if not warehouse_id:
578
        raise InventoryServiceException(101, "bad warehouse_id")
2251 ankur.sing 579
    item = get_item(item_id)
580
    if not item:
871 chandransh 581
        raise InventoryServiceException(101, "bad item_id")
582
 
583
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id)
584
    try:
585
        current_inventory_snapshot = query.one()
586
    except:
4103 chandransh 587
        current_inventory_snapshot = CurrentInventorySnapshot()
588
        current_inventory_snapshot.warehouse_id = warehouse_id
589
        current_inventory_snapshot.item_id = item_id
590
        current_inventory_snapshot.availibility = 0
591
        current_inventory_snapshot.reserved = 0
592
 
593
    current_inventory_snapshot.reserved = current_inventory_snapshot.reserved + quantity
594
    session.commit()
595
    check_risky_item(item)
596
    return True
871 chandransh 597
 
598
def reduce_reservation_count(item_id, warehouse_id, quantity):
599
    if not warehouse_id:
600
        raise InventoryServiceException(101, "bad warehouse_id")
2251 ankur.sing 601
    item = get_item(item_id)
602
    if not item:
871 chandransh 603
        raise InventoryServiceException(101, "bad item_id")
604
 
605
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id)
606
    try:
607
        current_inventory_snapshot = query.one()
608
        current_inventory_snapshot.reserved = current_inventory_snapshot.reserved - quantity
4318 rajveer 609
        ##FIXME In case of our own warehouse reduce availability also
610
        if warehouse_id == 7:
4335 rajveer 611
            current_inventory_snapshot.availibility = current_inventory_snapshot.availibility - quantity
871 chandransh 612
        session.commit()
2368 ankur.sing 613
        check_risky_item(item)
871 chandransh 614
        return True
615
    except:
616
        print "Unexpected error:", sys.exc_info()[0]
617
        return False
618
 
2075 rajveer 619
def mark_item_as_content_complete(entity_id, category, brand, modelName, modelNumber):
2828 rajveer 620
    '''
621
    Get all the items for this entityID and update category, brand, modelName and modelNumber for all.
622
    Update Status for only IN_PROCESS items to CONTENT_COMPLETE
623
    '''
723 chandransh 624
    content_complete_status = status.CONTENT_COMPLETE
2828 rajveer 625
    items = Item.query.filter_by(catalog_item_id=entity_id).all()
723 chandransh 626
    current_timestamp = datetime.datetime.now()
627
    for item in items:
2828 rajveer 628
        if item.status == status.IN_PROCESS:
629
            item.status = content_complete_status
630
            item_change_log = ItemChangeLog()
631
            item_change_log.old_status = item.status
632
            item_change_log.new_status = content_complete_status
633
            item_change_log.timestamp = current_timestamp
634
            item_change_log.item = item
723 chandransh 635
 
2075 rajveer 636
        item.category = category
637
        item.brand = brand
2081 rajveer 638
        item.model_name = modelName
639
        item.model_number = modelNumber
723 chandransh 640
        item.updatedOn = current_timestamp
641
    session.commit()
642
    return True
1294 chandransh 643
 
643 chandransh 644
def get_item_availability_for_location(warehouse_loc, item_id):
3355 chandransh 645
    """
646
    Determines the warehouse that should be used to fulfil an order for the given item.
4406 anupam.sin 647
    It first checks whether the preferred warehouse for that item is set or not. If set 
648
    and preference is sticky then that warehouse is used for fulfilment.
649
    If preference is not sticky then we see if item is available in the preferred WH,
650
    in which case that warehouse is used otherwise we use that warehouse which has the max
651
    availability. And if there is no inventory for that item in any warehouses then we use
652
    default warehouse.
653
    If preference is not set in that case we just find the warehouse with max availability
654
    and in case of zero inventory we use default warehouse
3355 chandransh 655
 
656
    Returns an ordered list of size 4 with following elements in the given order:
657
    1. Logistics location of the warehouse which was finally picked up to ship the order.
658
    2. Id of the warehouse which was finally picked up.
659
    3. Inventory size in the selected warehouse.
660
    4. Expected delay added by the category manager.
661
 
662
    Parameters:
663
     - warehouse_loc
664
     - item_id
665
    """
643 chandransh 666
    if warehouse_loc is None:
667
        raise InventoryServiceException(101, "Bad Warehouse Location")
668
    if not item_id:
1416 chandransh 669
        raise InventoryServiceException(101, "Bad Item id")
643 chandransh 670
 
1416 chandransh 671
    item = Item.get_by(id=item_id)
4406 anupam.sin 672
    logisticsLocation = warehouse_loc
673
    warehouses = Warehouse.query.filter_by(logisticsLocation=logisticsLocation).all()
643 chandransh 674
    warehouse_ids = [warehouse.id for warehouse in warehouses]
675
    warehouse_retid = -1
4406 anupam.sin 676
 
3503 chandransh 677
    total_availability = 0
4406 anupam.sin 678
    '''
679
    If warehouse preference is set and it is sticky then we should fulfil this order from this warehouse only.
680
    But we still need to calculate total availability across warehouses.
681
    '''
682
    if (item.isWarehousePreferenceSticky and item.preferredWarehouse is not None) :
683
        warehouse_retid = item.preferredWarehouse
684
        warehouse = Warehouse.get_by(id=warehouse_retid)
685
        logisticsLocation = warehouse.logisticsLocation
4476 anupam.sin 686
        total_availability = __get_item_availability(item, [warehouse_retid])
4406 anupam.sin 687
 
688
    #If preference is not sticky then this order should be fulfilled from preferred WH if inventory available
689
    #otherwise we should check for its availability elsewhere and fulfil this order from there, but if it is not available anywhere
690
    #then we should fulfil this order from its default warehouse.
691
 
692
    elif (not item.isWarehousePreferenceSticky and item.preferredWarehouse is not None) :
785 rajveer 693
        try:
4406 anupam.sin 694
            current_inventory_snapshot = CurrentInventorySnapshot.query.filter_by(warehouse_id = item.preferredWarehouse, item_id = item_id).one()
871 chandransh 695
            availability = current_inventory_snapshot.availibility - current_inventory_snapshot.reserved
4317 varun.gupt 696
        except Exception as e:
697
            print e
785 rajveer 698
            availability = 0    
4406 anupam.sin 699
        if availability > 0:
700
            warehouse_retid = item.preferredWarehouse
4476 anupam.sin 701
            total_availability = availability
4406 anupam.sin 702
        else :
703
            [logisticsLocation, warehouse_retid, total_availability] = \
704
                        __get_warehouse_with_max_availability(warehouse_loc = warehouse_loc, \
705
                                                            warehouse_ids = warehouse_ids, item = item)
706
 
707
    else :
708
        [logisticsLocation, warehouse_retid, total_availability] = \
709
                        __get_warehouse_with_max_availability(warehouse_loc = warehouse_loc, \
710
                                                            warehouse_ids = warehouse_ids, item = item)
2341 chandransh 711
 
4406 anupam.sin 712
    if item.expectedDelay is None:
713
        print 'expectedDelay field for this item was Null. Resetting it to 0'
714
        item.expectedDelay = 0
715
 
716
    ## FIXME Assign warehouse 5 (9D2) for all the hotspot products.
717
    #if warehouse_retid in [warehouse.id for warehouse in get_warehouses_for_vendor(1)]:
4433 anupam.sin 718
    if int(warehouse_retid) in [1,2,3,4,5]:
4406 anupam.sin 719
        warehouse_retid = 5
720
        warehouse = Warehouse.get_by(id=warehouse_retid)
721
        logisticsLocation = warehouse.logisticsLocation  
722
 
723
    return [logisticsLocation, int(warehouse_retid), total_availability, int(item.expectedDelay)]
724
 
725
def __get_warehouse_with_max_availability(warehouse_loc, warehouse_ids, item):
726
 
727
    warehouse_retid = -1
728
    max_availability = 0
729
    total_availability = 0
730
 
731
    for warehouse_id in warehouse_ids:
732
            try:
733
                current_inventory_snapshot = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item.id).one()
734
                availability = current_inventory_snapshot.availibility - current_inventory_snapshot.reserved
735
            except Exception as e:
736
                print e
737
                availability = 0    
738
            if availability > max_availability:
739
                warehouse_retid = warehouse_id
740
                max_availability = availability
741
            total_availability = total_availability + availability
742
 
743
    #If no warehouse could be found, use the default warehouse for this item
643 chandransh 744
    if warehouse_retid == -1:
759 chandransh 745
        # This is the case when all warehouses have exhausted their
746
        # inventory of this item or no warehouse is available in this
747
        # location.
4406 anupam.sin 748
        warehouse_retid = int(item.defaultWarehouse)
2344 chandransh 749
        warehouse = Warehouse.get_by(id=warehouse_retid)
4406 anupam.sin 750
        warehouse_loc = warehouse.logisticsLocation
2341 chandransh 751
        try:
4406 anupam.sin 752
            current_inventory_snapshot = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_retid, item_id = item.id).one()
3503 chandransh 753
            max_availability = current_inventory_snapshot.availibility - current_inventory_snapshot.reserved
4317 varun.gupt 754
        except Exception as e:
755
            print e
3503 chandransh 756
            max_availability = 0
757
        total_availability = max_availability
643 chandransh 758
 
4406 anupam.sin 759
    return [warehouse_loc, warehouse_retid, total_availability]
760
'''    
761
def calculate_total_availability(warehouse_ids, item_id):
762
    total_availability = 0
763
    for warehouse_id in warehouse_ids:
764
            try:
765
                current_inventory_snapshot = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id).one()
766
                availability = current_inventory_snapshot.availibility - current_inventory_snapshot.reserved
767
            except Exception as e:
768
                print e
769
                availability = 0    
770
            total_availability = total_availability + availability
771
    return total_availability
772
'''
2341 chandransh 773
 
122 ashish 774
def get_warehouses_for_item(item_id):
643 chandransh 775
 
122 ashish 776
    if not item_id:
777
        raise InventoryServiceException(101, "bad item_id")
635 rajveer 778
    item = get_item(item_id)
122 ashish 779
 
780
    if not item:
781
        raise InventoryServiceException(101, "bad item")
782
 
483 rajveer 783
    warehouses = item.currentInventory.warehouse
501 rajveer 784
    return warehouses
785
 
2404 chandransh 786
def get_child_categories(category):
787
    cm = CategoryManager()
2621 varun.gupt 788
    cat = cm.getCategory(category)
789
    return cat.children_category_ids if cat else None
2404 chandransh 790
 
626 chandransh 791
def get_best_sellers(start_index, stop_index, category=-1):
2404 chandransh 792
    '''
793
    Returns the Best Sellers between the start and the stop index in the given category
794
    '''
1926 rajveer 795
    query = get_best_sellers_query(category, None)
1098 chandransh 796
    best_sellers = query.all()[start_index:stop_index]
621 chandransh 797
    return get_thrift_item_list(best_sellers)
798
 
2093 chandransh 799
def get_best_sellers_count(category=-1):
2404 chandransh 800
    '''
801
    Returns the number of best sellers in the given category
802
    '''
1926 rajveer 803
    count = get_best_sellers_query(category, None).count()
1120 rajveer 804
    if count is None:
805
        count = 0
766 rajveer 806
    return count
621 chandransh 807
 
1926 rajveer 808
def get_best_sellers_catalog_ids(start_index, stop_index, brand, category=-1):
2404 chandransh 809
    '''
810
    Returns the Best sellers for the given brand and category between the start and the stop index.
811
    Ignores the category if it's passed as -1 and the brand if it's passed as None. 
812
    '''
1926 rajveer 813
    query = get_best_sellers_query(category, brand)
1098 chandransh 814
    best_sellers = query.all()[start_index:stop_index]
621 chandransh 815
    return [item.catalog_item_id for item in best_sellers]
1970 rajveer 816
 
1926 rajveer 817
def get_best_sellers_query(category, brand):
2404 chandransh 818
    '''
819
    Returns the query to be used for getting Best Sellers.
820
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
821
    '''
1098 chandransh 822
    query = Item.query.filter_by(status=status.ACTIVE).filter(Item.bestSellingRank != None)
626 chandransh 823
    if category != -1:
1970 rajveer 824
        all_categories = [category]
825
        child_categories = get_child_categories(category)
826
        if child_categories is not None:
827
            all_categories = all_categories + child_categories 
828
        query = query.filter(Item.category.in_(all_categories))
1926 rajveer 829
    if brand is not None:
830
        query = query.filter_by(brand=brand)
1098 chandransh 831
    query = query.order_by(asc(Item.bestSellingRank))
621 chandransh 832
    return query
609 chandransh 833
 
1098 chandransh 834
def get_best_deals(category=-1):
2404 chandransh 835
    '''
836
    Returns the Best deals in the given category. Ignores the category if it's passed as -1.
837
    '''
838
    query = get_best_deals_query(Item, category, None)
1098 chandransh 839
    items = query.all()
609 chandransh 840
    return get_thrift_item_list(items)
841
 
1098 chandransh 842
def get_best_deals_count(category=-1):
2404 chandransh 843
    '''
844
    Returns the count of best deals in the given category.
845
    Ignores the category if it's -1.
846
    '''
847
    count = get_best_deals_counting_query(func.count(distinct(Item.catalog_item_id)), category, None).scalar()
1120 rajveer 848
    if count is None:
849
        count = 0
766 rajveer 850
    return count
501 rajveer 851
 
1926 rajveer 852
def get_best_deals_catalog_ids(start_index, stop_index, brand, category=-1):
2404 chandransh 853
    '''
854
    Returns the catalog_item_ids of best deal items for the given brand and category.
855
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
856
    '''
857
    query = get_best_deals_query(Item, category, brand)
1098 chandransh 858
    best_deal_items = query.all()[start_index:stop_index]
859
    return [item.catalog_item_id for item in best_deal_items]
860
 
2404 chandransh 861
def get_best_deals_counting_query(obj, category, brand):
862
    '''
863
    Returns the query to be used to select the best deals in the given brand and category.
864
    Ignores the category if it's passed as -1 and the brand if it's passed as None. 
865
    '''
866
    query = session.query(obj).filter_by(status=status.ACTIVE).filter(Item.bestDealValue != None)
626 chandransh 867
    if category != -1:
1970 rajveer 868
        all_categories = [category]
869
        child_categories = get_child_categories(category)
870
        if child_categories is not None:
871
            all_categories = all_categories + child_categories 
872
        query = query.filter(Item.category.in_(all_categories))
1926 rajveer 873
    if brand is not None:
874
        query = query.filter_by(brand=brand)
2404 chandransh 875
    return query
876
 
877
def get_best_deals_query(obj, category, brand):
878
    '''
879
    Returns the query to be used to get the best deals in the given category and brand.
880
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
881
    '''
882
    query = get_best_deals_counting_query(obj, category, brand)
1098 chandransh 883
    query = query.group_by(Item.catalog_item_id).order_by(desc(Item.bestDealValue))
884
    return query
609 chandransh 885
 
1098 chandransh 886
def get_latest_arrivals(limit, category=-1):
2404 chandransh 887
    '''
888
    Returns up to limit number of Latest Arrivals in the given category.
889
    '''
2975 chandransh 890
    categories = []
891
    if category != -1:
892
        categories = [category]
893
    query = get_latest_arrivals_query(Item, categories, None)
1098 chandransh 894
    items = query.all()[0:limit]
609 chandransh 895
    return get_thrift_item_list(items)
598 chandransh 896
 
1098 chandransh 897
def get_latest_arrivals_count(limit, category=-1):
2404 chandransh 898
    '''
899
    Returns the number of latest arrivals which will be displayed on the website.
3016 chandransh 900
    To ignore the categories, pass the list as empty. To ignore brand, pass it as null.
2404 chandransh 901
    '''
2975 chandransh 902
    categories = []
903
    if category != -1:
904
        categories = [category]
905
    count = get_latest_arrivals_counting_query(func.count(distinct(Item.catalog_item_id)), categories, None).scalar()
1120 rajveer 906
    if count is None:
907
        count = 0
908
    count = min(count, limit)
766 rajveer 909
    return count
602 chandransh 910
 
2975 chandransh 911
def get_latest_arrivals_catalog_ids(start_index, stop_index, brand, categories=[]):
2404 chandransh 912
    '''
913
    Returns the catalog_item_ids of the latest arrivals between the start and the stop index
3016 chandransh 914
    To ignore the categories, pass the list as empty. To ignore brand, pass it as null.
2404 chandransh 915
    '''
2975 chandransh 916
    query = get_latest_arrivals_query(Item, categories, brand)
1098 chandransh 917
    latest_arrivals = query.all()[start_index:stop_index]
918
    return [item.catalog_item_id for item in latest_arrivals]
919
 
2975 chandransh 920
def get_latest_arrivals_counting_query(obj, categories, brand):
2404 chandransh 921
    '''
922
    Returns the query to be used to count Latest arrivals.
3016 chandransh 923
    To ignore the categories, pass the list as empty. To ignore brand, pass it as null.
2404 chandransh 924
    '''
925
    query = session.query(obj).filter_by(status=status.ACTIVE)
2975 chandransh 926
 
927
    all_categories = []
928
    for category in categories:
929
        all_categories.append(category)
1970 rajveer 930
        child_categories = get_child_categories(category)
2975 chandransh 931
        if child_categories:
932
            all_categories = all_categories + child_categories
933
    if all_categories: 
1970 rajveer 934
        query = query.filter(Item.category.in_(all_categories))
2975 chandransh 935
 
1926 rajveer 936
    if brand is not None:
937
        query = query.filter_by(brand=brand)
2404 chandransh 938
    return query
939
 
2975 chandransh 940
def get_latest_arrivals_query(obj, categories, brand):
2404 chandransh 941
    '''
942
    Returns the query to be used to retrieve Latest Arrivals.
943
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
944
    '''
2975 chandransh 945
    query = get_latest_arrivals_counting_query(obj, categories, brand)
1098 chandransh 946
    query = query.group_by(Item.catalog_item_id).order_by(desc(Item.startDate))
947
    return query
609 chandransh 948
 
949
def get_thrift_item_list(items):
1098 chandransh 950
    return [to_t_item(item) for item in items if item != None]
635 rajveer 951
 
1155 rajveer 952
def generate_new_entity_id():
953
    generator =  EntityIDGenerator.query.one()
954
    id = generator.id + 1
955
    generator.id = id
956
    session.commit()
957
    return id
958
 
635 rajveer 959
def put_category_object(object):
960
    category = Category.get_by(id=1)
961
    if category is None:
962
        category = Category()
963
    category.object = object    
964
    session.commit()
965
    return True
966
 
967
def get_category_object():
766 rajveer 968
    object = Category.get_by(id=1).object
969
    return object
970
 
4283 anupam.sin 971
def get_item_pricing(item_id, vendorId):
1341 chandransh 972
    item = Item.query.filter_by(id=item_id).first()
973
    if item is None:
974
        raise InventoryServiceException(101, "Bad Item")
4307 anupam.sin 975
    '''
976
    if vendor id is -1 then we calculate an average transfer price to be populated
977
    at the time of order creation. This will be later updated with actual transfer price
978
    at the time of billing.
979
    '''
980
    if(vendorId == -1):
4543 anupam.sin 981
        total = 0
4307 anupam.sin 982
        try:
983
            item_pricings = VendorItemPricing.query.filter_by(item=item).all()
4315 anupam.sin 984
            if item_pricings:
4307 anupam.sin 985
                for item_pricing in item_pricings:
4543 anupam.sin 986
                    total += item_pricing.transfer_price
4315 anupam.sin 987
                avg = total / len(item_pricings)
988
                item_pricing.transfer_price = avg
4543 anupam.sin 989
            else:
990
                item_pricing = VendorItemPricing()
991
                item_pricing.transfer_price = item.sellingPrice
992
                vendor = Vendor()
993
                vendor.id = vendorId
994
                item_pricing.vendor = vendor
995
                item_pricing.item = item
996
 
997
            return item_pricing
4307 anupam.sin 998
        except:
999
            raise InventoryServiceException(101, "Item pricing not found ")
4283 anupam.sin 1000
    vendor = Vendor.get_by(id=vendorId)    
1341 chandransh 1001
    try:
1002
        item_pricing = VendorItemPricing.query.filter_by(vendor=vendor, item=item).one()
1347 chandransh 1003
        return item_pricing
3244 chandransh 1004
    except MultipleResultsFound:
1341 chandransh 1005
        raise InventoryServiceException(110, "Multiple pricing information present for Vendor: " + vendor.name + " and Item: " + str(item_id))
3244 chandransh 1006
    except NoResultFound:
1341 chandransh 1007
        raise InventoryServiceException(111, "Missing pricing information for Vendor: " + vendor.name + " and Item: " + str(item_id))
1008
 
1970 rajveer 1009
def add_category(t_category):
1010
    category = Category.get_by(id=t_category.id)
1011
    if category is None:
1012
        category = Category()
1013
    category.id = t_category.id 
1014
    category.label = t_category.label
1015
    category.description = t_category.description
1016
    category.parent_category_id = t_category.parent_category_id 
1017
    session.commit()
1018
    return True
1019
 
1020
def get_category(id):
1021
    return Category.query.filter_by(id=id).first()
1022
 
1023
def get_all_categories():
1024
    return Category.query.all()
1025
 
1991 ankur.sing 1026
 
1027
def get_all_item_pricing(item_id):
1028
    item = Item.query.filter_by(id=item_id).first()
1029
    if item is None:
1030
        raise InventoryServiceException(101, "Bad Item")
1031
    item_pricing = VendorItemPricing.query.filter_by(item=item).all()
1032
    return item_pricing
1033
 
2116 ankur.sing 1034
def get_item_mappings(item_id):
1035
    item = Item.query.filter_by(id=item_id).first()
1036
    if item is None:
1037
        raise InventoryServiceException(101, "Bad Item")
1038
    item_mappings = VendorItemMapping.query.filter_by(item=item).all()
1039
    return item_mappings
1040
 
1041
def add_vendor_pricing(vendorItemPricing):
1991 ankur.sing 1042
    if not vendorItemPricing:
1043
        raise InventoryServiceException(108, "Bad vendorItemPricing in request")
1044
    vendorId = vendorItemPricing.vendorId
1045
    itemId = vendorItemPricing.itemId
1046
 
1047
    try:
1048
        vendor = Vendor.query.filter_by(id=vendorId).one()
1049
    except:
1050
        raise InventoryServiceException(101, "Vendor not found for vendorId " + str(vendorId))
1051
 
1052
    try:
1053
        item = Item.query.filter_by(id=itemId).one()
1054
    except:
1055
        raise InventoryServiceException(101, "Item not found for vendorId " + str(itemId))
1056
 
2120 ankur.sing 1057
    validate_vendor_prices(to_t_item(item), vendorItemPricing)
2065 ankur.sing 1058
 
1991 ankur.sing 1059
    try:
1060
        ds_vendorItemPricing = VendorItemPricing.query.filter(and_(VendorItemPricing.vendor==vendor, VendorItemPricing.item==item)).one()
1061
    except:
2116 ankur.sing 1062
        ds_vendorItemPricing = VendorItemPricing()
1063
        ds_vendorItemPricing.vendor = vendor
1064
        ds_vendorItemPricing.item = item
1991 ankur.sing 1065
 
1066
    if vendorItemPricing.mop:
1067
        ds_vendorItemPricing.mop = vendorItemPricing.mop
1068
    if vendorItemPricing.dealerPrice:
1069
        ds_vendorItemPricing.dealerPrice = vendorItemPricing.dealerPrice
1070
    if vendorItemPricing.transferPrice:
2065 ankur.sing 1071
        ds_vendorItemPricing.transfer_price = vendorItemPricing.transferPrice
1991 ankur.sing 1072
 
1073
    session.commit()
1074
    return
1075
 
2358 ankur.sing 1076
def add_vendor_item_mapping(key, vendorItemMapping):
2116 ankur.sing 1077
    if not vendorItemMapping:
1078
        raise InventoryServiceException(108, "Bad vendorItemMapping in request")
1079
    vendorId = vendorItemMapping.vendorId
1080
    itemId = vendorItemMapping.itemId
1081
 
1082
    try:
1083
        vendor = Vendor.query.filter_by(id=vendorId).one()
1084
    except:
1085
        raise InventoryServiceException(101, "Vendor not found for vendorId " + str(vendorId))
1086
 
1087
    try:
1088
        item = Item.query.filter_by(id=itemId).one()
1089
    except:
1090
        raise InventoryServiceException(101, "Item not found for vendorId " + str(itemId))
1091
 
1092
    try:
2358 ankur.sing 1093
        ds_vendorItemMapping = VendorItemMapping.query.filter(and_(VendorItemMapping.vendor==vendor, VendorItemMapping.item==item, VendorItemMapping.item_key==key)).one()
2116 ankur.sing 1094
    except:
1095
        ds_vendorItemMapping = VendorItemMapping()
1096
        ds_vendorItemMapping.vendor = vendor
1097
        ds_vendorItemMapping.item = item
2497 ankur.sing 1098
    ds_vendorItemMapping.vendor_category = vendorItemMapping.vendorCategory
2116 ankur.sing 1099
    ds_vendorItemMapping.item_key = vendorItemMapping.itemKey
1100
 
1101
    session.commit()
1102
    return
1103
 
2120 ankur.sing 1104
def validate_item_prices(item):
2129 ankur.sing 1105
    if item.mrp == None or item.sellingPrice == None or item.mrp == "" or item.sellingPrice == "":
1106
        return
1107
    if item.mrp < item.sellingPrice:
2120 ankur.sing 1108
        print "[BAD MRP and SP:] for {0} {1} {2} {3}. MRP={4}, SP={5}".format(item.productGroup, item.brand, item.modelNumber, item.color, str(item.mrp), str(item.sellingPrice))
1109
        raise InventoryServiceException(101, "[BAD MRP and SP:] for {0} {1} {2} {3}. MRP={4}, SP={5}".format(item.productGroup, item.brand, item.modelNumber, item.color, str(item.mrp), str(item.sellingPrice)))
2065 ankur.sing 1110
    return
2120 ankur.sing 1111
 
1112
def validate_vendor_prices(item, vendorPrices):
2129 ankur.sing 1113
    if item.mrp != None and item.mrp != "" and vendorPrices.mop != "" and item.mrp <  vendorPrices.mop:
2120 ankur.sing 1114
        print "[BAD MRP and MOP:] for {0} {1} {2} {3}. MRP={4}. MOP={5}, Vendor={6}".format(item.productGroup, item.brand, item.modelNumber, item.color, str(item.mrp), str(vendorPrices.mop), str(vendorPrices.vendorId))
1115
        raise InventoryServiceException(101, "[BAD MRP and MOP:] for {0} {1} {2} {3}. MRP={4}. MOP={5}, Vendor={6}".format(item.productGroup, item.brand, item.modelNumber, item.color, str(item.mrp), str(vendorPrices.mop), str(vendorPrices.vendorId)))
1116
    if vendorPrices.mop != "" and vendorPrices.transferPrice != "" and vendorPrices.transferPrice > vendorPrices.mop:
1117
        print "[BAD MOP and TP:] for {0} {1} {2} {3}. TP={4}. MOP={5}, Vendor={6}".format(item.productGroup, item.brand, item.modelNumber, item.color, str(vendorPrices.transferPrice), str(vendorPrices.mop), str(vendorPrices.vendorId))
1118
        raise InventoryServiceException(101, "[BAD MOP and TP:] for {0} {1} {2} {3}. TP={4}. MOP={5}, Vendor={6}".format(item.productGroup, item.brand, item.modelNumber, item.color, str(vendorPrices.transferPrice), str(vendorPrices.mop), str(vendorPrices.vendorId)))
1119
    return
2065 ankur.sing 1120
 
1121
def get_all_vendors():
1122
    return Vendor.query.all()
1123
 
4725 phani.kuma 1124
def check_color_valid(color):
1125
    if color is not None:
1126
        color = color.strip().lower()
1127
        if color != '' and color != 'na' and color != 'blank' and color != '(blank)':
1128
            return True
1129
    return False
1130
 
1131
def check_similar_item(brand, model_number, model_name, color):
2129 ankur.sing 1132
    query = Item.query
2428 ankur.sing 1133
    query = query.filter_by(brand=brand)
1134
    query = query.filter_by(model_number=model_number)
4725 phani.kuma 1135
    query = query.filter_by(model_name=model_name)
1136
    similar_items = query.all()
1137
    item = None
1138
    # Check if a similar item already exists in our database
1139
    for old_item in similar_items:
1140
        if old_item.color != None and old_item.color.strip().lower() == color.strip().lower():
1141
            item = old_item
1142
            break
1143
 
1144
    # Check if a similar item already exists in our database with out valid color if similar item with same color is not found
2116 ankur.sing 1145
    if item is None:
4725 phani.kuma 1146
        for old_item in similar_items:
1147
            if not check_color_valid(old_item.color):
1148
                item = old_item
1149
                break
1150
    i = 0
1151
    color_of_similar_item = None
1152
    # Check if a similar item already exists in our database to be used to get catalog_item_id
1153
    for old_item in similar_items:
1154
        # get a similar item already existing in our database with valid color
1155
        if check_color_valid(old_item.color):
1156
            similar_item = old_item
1157
            color_of_similar_item = similar_item.color
1158
            break
1159
        i = i + 1
1160
        # get a similar item already existing in our database if similar item with valid color is not found
1161
        if i == len(similar_items):
1162
            similar_item = old_item
1163
            color_of_similar_item = similar_item.color
1164
 
1165
    # Check if a similar item that is obtained above is having a valid color
1166
    if check_color_valid(color_of_similar_item):
1167
        # if a similar item that is obtained above is having a valid color and new item is about to be created with out valid color it is not done.
1168
        # since for example if their is a item with red color in our database and we are creating a new item with no color for the same product which is wrong.
1169
        if item is None and not check_color_valid(color):
1170
            return similar_item.id
1171
 
1172
    if item is None:
2116 ankur.sing 1173
        return 0
1174
    else:
1175
        return item.id
2286 ankur.sing 1176
 
1177
def change_risky_flag(item_id, risky):
1178
    item = get_item(item_id)
1179
    if not item:
1180
        raise InventoryServiceException(101, "Item missing in our database")
1181
    try:
1182
        log_risky_flag(item_id, risky)
1183
    except:
1184
        print "Not able to log risky flag change"
1185
    item.risky = risky
4295 varun.gupt 1186
    if not risky and item.status == status.PAUSED_BY_RISK:
2368 ankur.sing 1187
        change_item_status(item.id, status.ACTIVE)
2286 ankur.sing 1188
    session.commit()
1189
 
2358 ankur.sing 1190
def get_items_by_vendor_category(vendor_category):
1191
    if not vendor_category:
1192
        raise InventoryServiceException(101, "Invalid vendor category in request")
1193
    query = Item.query.filter(and_(Item.hotspotCategory==vendor_category, Item.status != status.PHASED_OUT))
1194
    items = query.all()
1195
    return items
2116 ankur.sing 1196
 
2358 ankur.sing 1197
def get_risky_items():
1198
    items = Item.query.filter_by(risky=True).all()
1199
    return items
3008 rajveer 1200
 
2809 rajveer 1201
def get_similar_items_catalog_ids(start_index, stop_index, itemId):
3008 rajveer 1202
    query = SimilarItems.query.filter_by(item_id=itemId).limit(stop_index-start_index)
1203
    similar_items = query.all()
3289 rajveer 1204
    return_list = []
1205
    for similar_item in similar_items:
1206
        isActive = False
1207
        try:
1208
            all_items = Item.query.filter_by(catalog_item_id=similar_item.catalog_item_id).all()
1209
        except:
1210
            continue
1211
        for item in all_items:
1212
            isActive = isActive or item.status == status.ACTIVE
1213
        if isActive:
1214
            return_list.append(similar_item.catalog_item_id)
1215
    return return_list
4423 phani.kuma 1216
 
1217
def get_all_similar_items_catalog_ids(itemId):
1218
    query = SimilarItems.query.filter_by(item_id=itemId)
1219
    similar_items = query.all()
1220
    return_list = []
1221
    for similar_item in similar_items:
1222
        item_query = Item.query.filter_by(catalog_item_id=similar_item.catalog_item_id).limit(1)
1223
        item = item_query.one()
1224
        return_list.append(item)
2809 rajveer 1225
 
4423 phani.kuma 1226
    return get_thrift_item_list(return_list)
1227
 
1228
def add_similar_item_catalog_id(itemId, catalog_item_id):
1229
    if not itemId or not catalog_item_id:
1230
        raise InventoryServiceException(101, "Bad itemId or catalogItemId in request")
1231
    items_for_entity = get_items_by_catalog_id(catalog_item_id)
1232
    if not len(items_for_entity):
1233
        raise InventoryServiceException(101, "catalogItemId does not exists in database")
1234
 
1235
    s_items = SimilarItems.query.filter_by(item_id=itemId, catalog_item_id=catalog_item_id).all()
1236
    if not len(s_items):
1237
        s_item = SimilarItems()
1238
        s_item.item_id=itemId
1239
        s_item.catalog_item_id=catalog_item_id
1240
        session.commit()
1241
        return items_for_entity[0]
1242
    else:
1243
        raise InventoryServiceException(101, "Already exists")
1244
 
1245
def delete_similar_item_catalog_id(itemId, catalog_item_id):
1246
    if not itemId or not catalog_item_id:
1247
        raise InventoryServiceException(101, "Bad itemId or catalogItemId in request")
1248
 
1249
    similar_item = SimilarItems.query.filter_by(item_id=itemId, catalog_item_id=catalog_item_id).all()
1250
    if len(similar_item):
1251
        similar_item[0].delete()
1252
    session.commit()
1253
    return True
1254
 
3079 rajveer 1255
def add_product_notification(itemId, email):
1256
    try:
3470 rajveer 1257
        try:
1258
            product_notification = ProductNotification.query.filter_by(item_id=itemId, email=email).one()
1259
        except:
1260
            product_notification = ProductNotification()
1261
            product_notification.email = email
1262
            product_notification.item_id = itemId
3079 rajveer 1263
        product_notification.addedOn = datetime.datetime.now()
1264
        session.commit()
1265
        return True
1266
    except:
1267
        return False
3086 rajveer 1268
 
1269
 
1270
def send_product_notifications():
1271
    product_notifications = ProductNotification.query.all()
1272
    for product_notification in product_notifications:
1273
        item = product_notification.item
4406 anupam.sin 1274
        availability = __get_item_availability(item, None)
3309 rajveer 1275
        if availability > 0 and item.status == status.ACTIVE:
3086 rajveer 1276
            __enque_product_notification_email(product_notification.email, __get_product_name(item) , product_notification.addedOn, __get_product_url(item), item.id)
1277
            product_notification.delete()
1278
    session.commit()
1279
    return True
1280
 
1281
def __get_product_name(item):
1282
    product_name = item.brand + " " + item.model_name + " " + item.model_number
1283
    color = item.color
1284
    if color is not None and color != 'NA':
1285
        product_name = product_name + " (" + color + ")"
3201 rajveer 1286
    product_name = product_name.replace("  "," ")
3086 rajveer 1287
    return product_name
1288
 
1289
 
1290
def __get_product_url(item):
1291
    product_url = "http://www.saholic.com/mobile-phones/" + item.brand + "-" + item.model_name + "-" + item.model_number + "-" + str(item.catalog_item_id)
1292
    product_url = product_url.replace("--","-")
1293
    product_url = product_url.replace(" ","")
1294
    return product_url
1295
 
3348 varun.gupt 1296
def get_all_brands_by_category(category_id):
1297
    catm = CategoryManager()
1298
    child_categories = catm.getCategory(category_id).children_category_ids
1299
    brands = session.query(distinct(Item.brand)).filter(Item.category.in_(child_categories)).all()
1300
 
1301
    return [brand[0] for brand in brands]
3086 rajveer 1302
 
1303
def __enque_product_notification_email(email, product, date, url, itemId):
1304
 
1305
    html = """
1306
        <html>
1307
        <body>
1308
        <div>
1309
        <p>
1310
            Hi,<br /><br />
1311
            The product requested by you on $date is now available on saholic.com.
1312
        </p>
1313
 
1314
        <p>    
1315
        <strong>Product: $product </strong>
1316
        </p>
1317
 
1318
        <p>
1319
        Click the link below to visit the product: 
1320
        <br/>
1321
        $url
1322
        </p>
1323
        <p>
1324
        Regards,<br/>
1325
        Saholic Customer Support Team<br/>
1326
        www.saholic.com<br/>
1327
        Email: help@saholic.com<br/>
1328
        </p>
1329
        </div>
1330
        </body>
1331
        </html>
1332
        """
1333
 
1334
    html = Template(html).substitute(dict(product=product,date=date,url=url))
3079 rajveer 1335
 
3086 rajveer 1336
    try:
1337
        helper_client = HelperClient().get_client()
1338
        helper_client.saveUserEmailForSending(email, "", "Product requested by you is available now.", html, str(itemId), "ProductNotification")
1339
    except Exception as e:
1340
        print e
1341
 
3557 rajveer 1342
def get_all_sources():
1343
    sources = Source.query.all()
1344
    return [to_t_source(source) for source in sources]
3086 rajveer 1345
 
3557 rajveer 1346
def get_item_pricing_by_source(itemId, sourceId):
1347
    item = Item.query.filter_by(id=itemId).first()
1348
    if item is None:
1349
        raise InventoryServiceException(101, "Bad Item")
1350
 
1351
    source = Source.query.filter_by(id=sourceId).first()
1352
    if source is None:
1353
        raise InventoryServiceException(101, "Source not found for sourceId " + str(sourceId))
1354
 
1355
    item_pricing = SourceItemPricing.query.filter_by(source=source, item=item).first()
1356
    if item_pricing is None:
1357
        raise InventoryServiceException(101, "Pricing information not found for sourceId " + str(sourceId))
1358
    return item_pricing
1359
 
1360
def add_source_item_pricing(sourceItemPricing):
1361
    if not sourceItemPricing:
1362
        raise InventoryServiceException(108, "Bad sourceItemPricing in request")
1363
 
1364
    if not sourceItemPricing.sellingPrice:
4326 mandeep.dh 1365
        raise InventoryServiceException(101, "Selling Price is not defined for sourceId " + str(sourceItemPricing.sourceId))
3557 rajveer 1366
 
1367
    sourceId = sourceItemPricing.sourceId
1368
    itemId = sourceItemPricing.itemId
1369
 
1370
    item = Item.query.filter_by(id=itemId).first()
1371
    if item is None:
1372
        raise InventoryServiceException(101, "Bad Item")
1373
 
1374
    source = Source.query.filter_by(id=sourceId).first()
1375
    if source is None:
1376
        raise InventoryServiceException(101, "Source not found for sourceId " + str(sourceId))
1377
 
3564 rajveer 1378
    ds_sourceItemPricing = SourceItemPricing.get_by(source=source, item=item)
3557 rajveer 1379
    if ds_sourceItemPricing is None:
1380
        ds_sourceItemPricing = SourceItemPricing()
1381
        ds_sourceItemPricing.source = source
1382
        ds_sourceItemPricing.item = item
1383
 
1384
    if sourceItemPricing.mrp:
1385
        ds_sourceItemPricing.mrp = sourceItemPricing.mrp
1386
    ds_sourceItemPricing.sellingPrice = sourceItemPricing.sellingPrice
1387
 
1388
    session.commit()
1389
    return
1390
 
1391
def get_all_source_pricing(itemId):
1392
    item = Item.query.filter_by(id=itemId).first()
1393
    if item is None:
1394
        raise InventoryServiceException(101, "Bad Item")
1395
    source_pricing = SourceItemPricing.query.filter_by(item=item).all()
1396
    return source_pricing
1397
 
1398
 
1399
def get_item_for_source(item_id, sourceId):
1400
    item = get_item(item_id)
1401
    if sourceId == -1:
1402
        return item
1403
    try:
1404
        sip = get_item_pricing_by_source(item_id, sourceId)
1405
        item.sellingPrice = sip.sellingPrice
1406
        if sip.mrp:
1407
            item.mrp = sip.mrp
1408
    except:
1409
        print "No source pricing"
1410
    return item
1411
 
3872 chandransh 1412
def search_items(search_terms, offset, limit):
1413
    query = Item.query
1414
 
1415
    query_clause = []
1416
 
1417
    search_terms = ['%' + search_term + '%' for search_term in search_terms]
1418
 
1419
    for search_term in search_terms:
1420
        query_clause.append(Item.brand.like(search_term))
1421
        query_clause.append(Item.model_number.like(search_term))
1422
        query_clause.append(Item.model_name.like(search_term))
1423
 
1424
    query = query.filter(or_(*query_clause))
1425
 
1426
    query = query.order_by(Item.product_group, Item.brand, Item.model_number, Item.model_name).offset(offset)
1427
    if limit:
1428
        query = query.limit(limit)
1429
    items = query.all()
1430
    return items
1431
 
1432
def get_search_result_count(search_terms):
1433
    query = Item.query
1434
 
1435
    query_clause = []
1436
 
1437
    search_terms = ['%' + search_term + '%' for search_term in search_terms]
1438
 
1439
    for search_term in search_terms:
1440
        query_clause.append(Item.brand.like(search_term))
1441
        query_clause.append(Item.model_number.like(search_term))
1442
        query_clause.append(Item.model_name.like(search_term))
1443
 
1444
    query = query.filter(or_(*query_clause))
1445
 
1446
    return query.count()
1447
 
3924 rajveer 1448
def __clear_homepage_cache():
1449
    try:
1450
        # create a password manager
1451
        password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
1452
        # Add the username and password.
1453
        configclient = ConfigClient()
4310 rajveer 1454
        ips = configclient.get_property("production_servers_private_ips");
3924 rajveer 1455
        ips = ips.split(" ")
1456
 
1457
        for ip in ips:
4310 rajveer 1458
            try:
1459
                top_level_url = "http://" + ip + ":8080/"
1460
                password_mgr.add_password(None, top_level_url, "saholic", "shop2020")
1461
                handler = urllib2.HTTPBasicAuthHandler(password_mgr)
3924 rajveer 1462
 
4310 rajveer 1463
                opener = urllib2.build_opener(handler)
3924 rajveer 1464
 
4310 rajveer 1465
                # use the opener to fetch a URL
1466
                res = opener.open(top_level_url + "cache-admin/HomePageSnippets?_method=delete")
1467
                print "Successfully cleared home page cache" + res.read()
1468
            except:
1469
                print "Unable to clear home page cache" + res.read()
3924 rajveer 1470
    except:
1471
        print "Unable to clear cache, still should continue with other operations"
4024 chandransh 1472
 
4062 chandransh 1473
def get_pending_orders_inventory(vendor_id=1):
4024 chandransh 1474
    """
1475
    Returns a list of inventory stock for items for which there are pending orders.
1476
    """
4341 rajveer 1477
 
4368 rajveer 1478
    warehouse_ids = [warehouse.id for warehouse in get_warehouses_for_vendor(vendor_id)]
4064 chandransh 1479
    pending_items_inventory = []
1480
    if warehouse_ids:
1481
        pending_items_inventory = session.query(CurrentInventorySnapshot.item_id, func.sum(CurrentInventorySnapshot.availibility), func.sum(CurrentInventorySnapshot.reserved)).filter(CurrentInventorySnapshot.warehouse_id.in_(warehouse_ids)).group_by(CurrentInventorySnapshot.item_id).having(func.sum(CurrentInventorySnapshot.reserved) > 0).all()
4024 chandransh 1482
    return pending_items_inventory
4295 varun.gupt 1483
 
1484
def get_product_notifications(start_datetime):
1485
    '''
1486
    Returns a list of Product Notification objects each representing user requests for notification
1487
    '''
1488
    query = ProductNotification.query
3924 rajveer 1489
 
4295 varun.gupt 1490
    if start_datetime:
1491
        query = query.filter(ProductNotification.addedOn > start_datetime)
1492
 
1493
    notifications = query.order_by(desc('addedOn')).all()
1494
    return notifications
1495
 
1496
def get_product_notification_request_count(start_datetime):
1497
    '''
1498
    Returns list of items and the counts of product notification requests
1499
    '''
1500
    print start_datetime
1501
    query = session.query(ProductNotification, func.count(ProductNotification.email).label('count'))
1502
 
1503
    if start_datetime:
1504
        query = query.filter(ProductNotification.addedOn > start_datetime)
1505
 
1506
    counts = query.group_by(ProductNotification.item_id).order_by(desc('count')).all()
1507
    return counts
1508
 
766 rajveer 1509
def close_session():
1510
    if session.is_active:
1511
        print "session is active. closing it."
1399 rajveer 1512
        session.close()
3376 rajveer 1513
 
1514
def is_alive():
1515
    try:
1516
        session.query(Item.id).limit(1).one()
1517
        return True
1518
    except:
1519
        return False
4332 anupam.sin 1520
 
1521
def add_vendor(vendor):
1522
    if not vendor:
1523
        raise InventoryServiceException(108, "Bad vendor")
1524
    if get_Vendor(vendor.id):
1525
        #vendor is already present.
1526
        raise InventoryServiceException(101, "Vendor already present")
1527
 
1528
    ds_vendor = Vendor()
1529
    ds_vendor.id = vendor.id
1530
    ds_vendor.name = vendor.name
1531
    session.commit()
1532
    return ds_vendor.id
1533
 
1534
def add_warehouse_vendor_mapping(warehouse_id, VendorId):
1535
    return True
1536
 
1537
def get_vendors_for_warehouse(warehouse_id):
1538
    try:
1539
        warehouse = Warehouse.get_by(id=warehouse_id)
1540
        return warehouse.vendors
1541
    except:
1542
        raise InventoryServiceException(108, "Bad Warehouse Id")
1543
 
1544
def get_warehouses_for_vendor(vendorId):
1545
    try:
1546
        vendor = get_Vendor(vendorId)
1547
        return vendor.warehouses
1548
    except:
4649 phani.kuma 1549
        raise InventoryServiceException(108, "Bad Vendor Id")
1550
 
1551
def add_authorization_log_for_item(itemId, username, reason):
1552
    if not itemId or not username:
1553
        raise InventoryServiceException(101, "Bad itemId or Invalid username in request")
1554
    authorize_log = AuthorizationLog()
1555
    authorize_log.item_id = itemId
1556
    authorize_log.username = username
1557
    authorize_log.reason = reason
1558
    session.commit()
1559
    return True