Subversion Repositories SmartDukaan

Rev

Rev 1347 | Rev 1399 | 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,\
11
    VendorItemMapping
122 ashish 12
from shop2020.thriftpy.model.v1.catalog.ttypes import \
13
    InventoryServiceException, status
602 chandransh 14
from shop2020.model.v1.catalog.impl.Convertors import to_t_item
94 ashish 15
import datetime
871 chandransh 16
import sys
602 chandransh 17
from shop2020.utils.Utils import log_entry, to_py_date
621 chandransh 18
from sqlalchemy import desc, asc
94 ashish 19
 
20
 
21
def initialize():
22
    DataService.initialize()
23
 
24
 
103 ashish 25
def get_all_items(is_active):
26
    if is_active:
766 rajveer 27
        items = Item.query.filter_by(status= status.ACTIVE).all()
103 ashish 28
    else:
766 rajveer 29
        items = Item.query.all()
30
    return items
31
 
103 ashish 32
def get_all_items_by_status(status):
33
    if not status:
34
        #return all items
766 rajveer 35
        items = get_all_items(False)
103 ashish 36
    else:
766 rajveer 37
        items = Item.query.filter_by(status=status).all()
38
    return items
103 ashish 39
 
635 rajveer 40
def get_item(item_id):
766 rajveer 41
    item = Item.get_by(id=item_id)
42
    return item
94 ashish 43
 
635 rajveer 44
def get_items_by_catalog_id(catalog_id):
447 rajveer 45
    query = Item.query.filter_by(catalog_item_id=catalog_id)
437 rajveer 46
    try:
635 rajveer 47
        items = query.all()
48
        return get_thrift_item_list(items)
437 rajveer 49
    except:
50
        raise InventoryServiceException(109, "Item not found")
51
 
576 chandransh 52
def is_active(item_id):
53
    try:
635 rajveer 54
        item = get_item(item_id)
576 chandransh 55
        return item.status == status.ACTIVE
56
    except InventoryServiceException:
57
        return False
563 chandransh 58
 
94 ashish 59
def get_Warehouse(warehouse_id):
60
    return Warehouse.get_by(id=warehouse_id)
61
 
122 ashish 62
def get_all_warehouses_by_status(status):
63
    if not status:
766 rajveer 64
        warehouses = Warehouse.query.all()
122 ashish 65
    else:
851 chandransh 66
        warehouses = Warehouse.query.filter_by(status=status).all()
67
    return warehouses
122 ashish 68
 
94 ashish 69
def get_all_warehouses_for_item(item_id):
635 rajveer 70
    item = get_item(item_id)
94 ashish 71
    if not item:
122 ashish 72
        raise InventoryServiceException(108, "Some unforeseen error while obtaining item")
73
    return item.get_all_warehouses
94 ashish 74
 
122 ashish 75
def get_all_items_for_warehouse(warehouse_id):
76
    warehouse = get_Warehouse(warehouse_id)
77
    if not warehouse:
78
        raise InventoryServiceException(108, "bad warehouse")
79
    return warehouse.all_items
80
 
94 ashish 81
def add_warehouse(warehouse):
103 ashish 82
    if not warehouse:
122 ashish 83
        raise InventoryServiceException(108, "Bad warehouse")
103 ashish 84
    if get_Warehouse(warehouse.id):
85
        #warehouse is already present.
122 ashish 86
        raise InventoryServiceException(101, "Warehouse already present")
103 ashish 87
 
94 ashish 88
    ds_warehouse = Warehouse()
89
    ds_warehouse.id = warehouse.id
90
    ds_warehouse.location = warehouse.location
122 ashish 91
    ds_warehouse.status = status.ACTIVE
103 ashish 92
    ds_warehouse.addedOn = datetime.datetime.now()
483 rajveer 93
    ds_warehouse.lastCheckedOn = datetime.datetime.now()
94
    ds_warehouse.tinNumber = warehouse.tinNumber
95
    ds_warehouse.pincode = warehouse.pincode
96
    if warehouse.vendorString:
97
        ds_warehouse.vendorString = warehouse.vendorString
94 ashish 98
    session.commit()
103 ashish 99
    return ds_warehouse.id
94 ashish 100
 
122 ashish 101
def update_item(item):
102
    if not item:
103
        raise InventoryServiceException(108, "Bad item in request")
104
 
105
    if not item.id:
609 chandransh 106
        raise InventoryServiceException(101, "Missing id for update")
122 ashish 107
 
635 rajveer 108
    ds_item = get_item(item.id)
122 ashish 109
 
110
    if not ds_item:
609 chandransh 111
        raise InventoryServiceException(101, "Item missing in our database")
122 ashish 112
 
963 chandransh 113
    if item.productGroup:
114
        ds_item.product_group = item.productGroup 
115
    if item.brand:
116
        ds_item.brand = item.brand
511 rajveer 117
    if item.modelName:
118
        ds_item.model_name = item.modelName
119
    if item.modelNumber:
120
        ds_item.model_number = item.modelNumber
609 chandransh 121
    if item.color:
122
        ds_item.color = item.color
483 rajveer 123
    if item.category:
124
        ds_item.category = item.category
125
    if item.comments:
126
        ds_item.comments = item.comments
511 rajveer 127
 
122 ashish 128
    if item.catalogItemId:
129
        ds_item.catalog_item_id = item.catalogItemId
483 rajveer 130
 
131
    if item.mrp:
132
        ds_item.mrp = item.mrp
133
    if item.mop:
134
        ds_item.mop = item.mop
135
    if item.sellingPrice:
136
        ds_item.sellingPrice = item.sellingPrice
609 chandransh 137
    if item.dealerPrice:
138
        ds_item.dealerPrice = item.dealerPrice
122 ashish 139
    if item.weight:
140
        ds_item.weight = item.weight
511 rajveer 141
    if item.itemStatus:
142
        ds_item.status = item.itemStatus
122 ashish 143
 
511 rajveer 144
    if item.startDate:
145
        ds_item.startDate = item.startDate
146
    if item.retireDate:
147
        ds_item.retireDate = item.retireDate
148
 
122 ashish 149
    if item.featureId:
150
        ds_item.feature_id = item.featureId
151
    if item.featureDescription:
152
        ds_item.feature_description = item.featureDescription
153
 
609 chandransh 154
    if item.bestDealText:
155
        ds_item.bestDealText = item.bestDealText
156
    if item.bestDealValue:
157
        ds_item.bestDealValue = item.bestDealValue
158
 
159
    ds_item.updated_on = datetime.datetime.now()
122 ashish 160
    session.commit();
161
    return ds_item.id
94 ashish 162
 
103 ashish 163
def add_item(item):
164
    if not item:
122 ashish 165
        raise InventoryServiceException(108, "Bad item in request")
635 rajveer 166
    if get_item(item.id):
122 ashish 167
        raise InventoryServiceException(101, "Item already exists")
483 rajveer 168
 
103 ashish 169
    ds_item = Item()
122 ashish 170
    if item.id:
171
        ds_item.id = item.id
963 chandransh 172
    if item.productGroup:
173
        ds_item.product_group = item.productGroup
174
    if item.brand:
175
        ds_item.brand = item.brand
515 rajveer 176
    if item.modelName:
177
        ds_item.model_name = item.modelName
178
    if item.modelNumber:
179
        ds_item.model_number = item.modelNumber
609 chandransh 180
    if item.color:
181
        ds_item.color = item.color
483 rajveer 182
    if item.category:
183
        ds_item.category = item.category
184
    if item.comments:
185
        ds_item.comments = item.comments
186
 
122 ashish 187
    if item.catalogItemId:
188
        ds_item.catalog_item_id = item.catalogItemId
189
    if item.itemStatus:
190
        ds_item.status = item.itemStatus
191
    else: #this is for update calls
192
        ds_item.status = status.IN_PROCESS
103 ashish 193
    ds_item.addedOn = datetime.datetime.now()
609 chandransh 194
    ds_item.updatedOn = datetime.datetime.now()
195
 
483 rajveer 196
    if item.mrp:
197
        ds_item.mrp = item.mrp
198
    if item.mop:
199
        ds_item.mop = item.mop
200
    if item.sellingPrice:
201
        ds_item.sellingPrice = item.sellingPrice
609 chandransh 202
    if item.dealerPrice:
203
        ds_item.dealerPrice = item.dealerPrice
122 ashish 204
    if item.weight:
205
        ds_item.weight = item.weight
206
 
207
    if item.featureId:
208
        ds_item.feature_id = item.featureId
209
    if item.featureDescription:
210
        ds_item.feature_description = item.featureDescription
211
 
103 ashish 212
    if item.otherInfo:
213
        for k,v in item.otherInfo.iteritems():
214
            info = ItemInfo()
215
            info.key = k
216
            info.value = v
217
            ds_item.iteminfo.append(info)
218
    #check if categories present. If yes, add them to system
122 ashish 219
 
609 chandransh 220
    if item.bestDealValue:
221
        ds_item.bestDealValue = item.bestDealValue
222
    if item.bestDealText:
223
        ds_item.bestDealText = item.bestDealText
224
 
103 ashish 225
    session.commit();
609 chandransh 226
    return ds_item.id       
103 ashish 227
 
483 rajveer 228
def update_inventory(warehouse_id, timestamp, availability):
229
    warehouse = get_Warehouse(warehouse_id)
230
    if not warehouse:
231
        raise InventoryServiceException(107, "Warehouse? Where?")
232
 
233
    time = datetime.datetime.now()
234
    warehouse.lastCheckedOn = time
235
    warehouse.vendorString = timestamp
1368 chandransh 236
    vendor = warehouse.vendor
483 rajveer 237
 
1368 chandransh 238
    for item_key, quantity in availability.iteritems():
483 rajveer 239
        try:
1368 chandransh 240
            vendor_item_mapping = VendorItemMapping.query.filter_by(vendor=vendor, item_key=item_key).one();
241
            item = vendor_item_mapping.item
494 rajveer 242
        except:
243
            continue  
244
        try:
483 rajveer 245
            item_inventory_history = ItemInventoryHistory()
246
            item_inventory_history.warehouse = warehouse
247
            item_inventory_history.item = item
248
            item_inventory_history.timestamp = time
249
            item_inventory_history.availibility = quantity
250
 
251
            current_inventory_snapshot = CurrentInventorySnapshot.get_by(item=item, warehouse=warehouse)
252
            if not current_inventory_snapshot:
253
                current_inventory_snapshot = CurrentInventorySnapshot()
254
                current_inventory_snapshot.item = item
255
                current_inventory_snapshot.warehouse = warehouse
256
                current_inventory_snapshot.availibility = 0
871 chandransh 257
                current_inventory_snapshot.reserved = 0
483 rajveer 258
            # added the difference in the current inventory    
259
            current_inventory_snapshot.availibility = current_inventory_snapshot.availibility + quantity
260
        except:
261
            raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
262
    session.commit() 
263
 
94 ashish 264
def get_item_inventoy(item_id):
265
 
266
    inventory = Item.get_by(id=item_id).currentInventory
267
    if not inventory:
122 ashish 268
        raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
94 ashish 269
    return inventory
270
 
635 rajveer 271
def get_item_inventory_by_item_id(item_id):
272
    inventory = Item.get_by(id=item_id).currentInventory
379 ashish 273
    if not inventory:
274
        raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
275
    return inventory
276
 
103 ashish 277
 
278
def retire_warehouse(warehouse_id):
279
    if not warehouse_id:
122 ashish 280
        raise InventoryServiceException(101, "Bad warehouse id")
103 ashish 281
    warehouse = get_Warehouse(warehouse_id)
282
    if not warehouse:
122 ashish 283
        raise InventoryServiceException(108, "warehouse id not present")
284
    warehouse.status = status.DELETED;
103 ashish 285
    session.commit()
286
 
287
def retire_item(item_id):
288
    if not item_id:
122 ashish 289
        raise InventoryServiceException(101, "bad item id")
635 rajveer 290
    item = get_item(item_id)
103 ashish 291
    if not item:
122 ashish 292
        raise InventoryServiceException(108, "item id not present")
293
    item.status = status.PHASED_OUT
294
    item.retireDate = datetime.datetime.now()
103 ashish 295
    session.commit()
122 ashish 296
 
297
#need to implement threads based solution here
103 ashish 298
def start_item_on(item_id, timestamp):
299
    if not item_id:
122 ashish 300
        raise InventoryServiceException(101, "bad item id")
635 rajveer 301
    item = get_item(item_id)
103 ashish 302
    if not item:
122 ashish 303
        raise InventoryServiceException(108, "item id not present")
103 ashish 304
 
122 ashish 305
    item.status = status.ACTIVE
306
    item.startDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
307
    add_status_change_log(item, status.ACTIVE)
103 ashish 308
    session.commit()
309
 
122 ashish 310
#need to implement threads here
103 ashish 311
def retire_item_on(item_id, timestamp):
312
    if not item_id:
122 ashish 313
        raise InventoryServiceException(101, "bad item id")
635 rajveer 314
    item = get_item(item_id)
103 ashish 315
    if not item:
122 ashish 316
        raise InventoryServiceException(108, "item id not present")
103 ashish 317
 
122 ashish 318
    item.status = status.PHASED_OUT
319
    item.retireDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
320
    add_status_change_log(item, status.PHASED_OUT)
103 ashish 321
    session.commit()
322
 
323
def add_status_change_log(item, new_status):
324
    item_change_log = ItemChangeLog()
325
    item_change_log.new_status = new_status
326
    item_change_log.old_status = item.status
327
    item_change_log.timestamp = datetime.datetime.now()
328
    item_change_log.item = item
329
    session.commit()
330
 
331
def change_item_status(item_id, new_status):
332
    if not item_id:
122 ashish 333
        raise InventoryServiceException(101, "bad item id")
635 rajveer 334
    item = get_item(item_id)
103 ashish 335
    if not item:
122 ashish 336
        raise InventoryServiceException(108, "item id not present")
103 ashish 337
 
122 ashish 338
    item.status = new_status
339
    add_status_change_log(item, new_status)
103 ashish 340
    session.commit()
341
 
342
def get_item_availability_for_warehouse(warehouse_id, item_id):
343
    if not warehouse_id:
122 ashish 344
        raise InventoryServiceException(101, "bad warehouse_id")
103 ashish 345
    if not item_id:
122 ashish 346
        raise InventoryServiceException(101, "bad item_id")
103 ashish 347
 
348
    warehouse = get_Warehouse(warehouse_id)
349
    if not warehouse:
122 ashish 350
        raise InventoryServiceException(108, "warehouse does not exist")
635 rajveer 351
    item = get_item(item_id)
504 rajveer 352
 
103 ashish 353
    if not item:
766 rajveer 354
        raise InventoryServiceException(108, "item does not exist")
122 ashish 355
 
494 rajveer 356
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id)
504 rajveer 357
    query = query.filter_by(item_id = item.id)
494 rajveer 358
    try:
359
        current_inventory_snapshot = query.one()
871 chandransh 360
        return current_inventory_snapshot.availibility - current_inventory_snapshot.reserved
494 rajveer 361
    except:
362
        return 0
363
    """
364
    current_inventory_snapshot = CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse_id == warehouse_id, CurrentInventorySnapshot.item_id == item_id).one()
103 ashish 365
    if not current_inventory_snapshot:
366
        return 0
367
    else:
368
        return current_inventory_snapshot.availibility
494 rajveer 369
    """
643 chandransh 370
 
871 chandransh 371
def reserve_item_in_warehouse(item_id, warehouse_id, quantity):    
372
    if not warehouse_id:
373
        raise InventoryServiceException(101, "bad warehouse_id")
374
    if not item_id:
375
        raise InventoryServiceException(101, "bad item_id")
376
 
377
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id)
378
    try:
379
        current_inventory_snapshot = query.one()
380
        current_inventory_snapshot.reserved = current_inventory_snapshot.reserved + quantity
381
        session.commit()
382
        return True
383
    except:
384
        print "Unexpected error:", sys.exc_info()[0]
385
        return False
386
 
387
def reduce_reservation_count(item_id, warehouse_id, quantity):
388
    if not warehouse_id:
389
        raise InventoryServiceException(101, "bad warehouse_id")
390
    if not item_id:
391
        raise InventoryServiceException(101, "bad item_id")
392
 
393
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id)
394
    try:
395
        current_inventory_snapshot = query.one()
396
        current_inventory_snapshot.reserved = current_inventory_snapshot.reserved - quantity
397
        session.commit()
398
        return True
399
    except:
400
        print "Unexpected error:", sys.exc_info()[0]
401
        return False
402
 
723 chandransh 403
def mark_item_as_content_complete(entity_id):
404
    content_complete_status = status.CONTENT_COMPLETE
1294 chandransh 405
    items = Item.query.filter_by(catalog_item_id=entity_id, status=status.IN_PROCESS).all()
723 chandransh 406
    current_timestamp = datetime.datetime.now()
407
    for item in items:
408
        item_change_log = ItemChangeLog()
409
        item_change_log.old_status = item.status
410
        item_change_log.new_status = content_complete_status
411
        item_change_log.timestamp = current_timestamp
412
        item_change_log.item = item
413
 
414
        item.status = content_complete_status
415
        item.updatedOn = current_timestamp
416
    session.commit()
417
    return True
1294 chandransh 418
 
643 chandransh 419
def get_item_availability_for_location(warehouse_loc, item_id):
420
    if warehouse_loc is None:
421
        raise InventoryServiceException(101, "Bad Warehouse Location")
422
    if not item_id:
423
        raise InventoryServiceException(101, "Bas Item id")
424
 
425
    warehouses = Warehouse.query.filter_by(logisticsLocation=warehouse_loc).all()
426
    warehouse_ids = [warehouse.id for warehouse in warehouses]
427
    warehouse_retid = -1
428
    global_availability = 0
429
    for warehouse_id in warehouse_ids:
785 rajveer 430
        try:
871 chandransh 431
            current_inventory_snapshot = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id).one()
432
            availability = current_inventory_snapshot.availibility - current_inventory_snapshot.reserved
785 rajveer 433
        except:
434
            availability = 0    
643 chandransh 435
        if availability > global_availability:
436
            warehouse_retid = warehouse_id
437
            global_availability = availability
438
    if warehouse_retid == -1:
759 chandransh 439
        # This is the case when all warehouses have exhausted their
440
        # inventory of this item or no warehouse is available in this
441
        # location.
643 chandransh 442
        #TODO: Change 1 to be a constant read from the Config client.
443
        warehouse_retid = 1
444
    return [warehouse_retid, global_availability]
445
 
122 ashish 446
def get_warehouses_for_item(item_id):
643 chandransh 447
 
122 ashish 448
    if not item_id:
449
        raise InventoryServiceException(101, "bad item_id")
635 rajveer 450
    item = get_item(item_id)
122 ashish 451
 
452
    if not item:
453
        raise InventoryServiceException(101, "bad item")
454
 
483 rajveer 455
    warehouses = item.currentInventory.warehouse
501 rajveer 456
    return warehouses
457
 
626 chandransh 458
def get_best_sellers(start_index, stop_index, category=-1):
621 chandransh 459
    query = get_best_sellers_query(category)
1098 chandransh 460
    best_sellers = query.all()[start_index:stop_index]
621 chandransh 461
    return get_thrift_item_list(best_sellers)
462
 
463
def get_best_sellers_count(category):
1120 rajveer 464
    count = get_best_sellers_query(category).count()
465
    if count is None:
466
        count = 0
766 rajveer 467
    return count
621 chandransh 468
 
626 chandransh 469
def get_best_sellers_catalog_ids(start_index, stop_index, category=-1):
621 chandransh 470
    query = get_best_sellers_query(category)
1098 chandransh 471
    best_sellers = query.all()[start_index:stop_index]
621 chandransh 472
    return [item.catalog_item_id for item in best_sellers]
501 rajveer 473
 
621 chandransh 474
def get_best_sellers_query(category):
1098 chandransh 475
    query = Item.query.filter_by(status=status.ACTIVE).filter(Item.bestSellingRank != None)
626 chandransh 476
    if category != -1:
621 chandransh 477
        query = query.filter_by(category=category)
1098 chandransh 478
    query = query.order_by(asc(Item.bestSellingRank))
621 chandransh 479
    return query
609 chandransh 480
 
1098 chandransh 481
def get_best_deals(category=-1):
482
    query = get_best_deals_query(category)
483
    items = query.all()
609 chandransh 484
    return get_thrift_item_list(items)
485
 
1098 chandransh 486
def get_best_deals_count(category=-1):
1120 rajveer 487
    count = get_best_deals_query(category).count()
488
    if count is None:
489
        count = 0
766 rajveer 490
    return count
501 rajveer 491
 
626 chandransh 492
def get_best_deals_catalog_ids(start_index, stop_index, category=-1):
1098 chandransh 493
    query = get_best_deals_query(category)
494
    best_deal_items = query.all()[start_index:stop_index]
495
    return [item.catalog_item_id for item in best_deal_items]
496
 
497
def get_best_deals_query(category):
498
    query = Item.query.filter_by(status=status.ACTIVE).filter(Item.bestDealValue != None)
626 chandransh 499
    if category != -1:
621 chandransh 500
        query = query.filter_by(category=category)
1098 chandransh 501
    query = query.group_by(Item.catalog_item_id).order_by(desc(Item.bestDealValue))
502
    return query
609 chandransh 503
 
1098 chandransh 504
def get_latest_arrivals(limit, category=-1):
505
    query = get_latest_arrivals_query(category)
506
    items = query.all()[0:limit]
609 chandransh 507
    return get_thrift_item_list(items)
598 chandransh 508
 
1098 chandransh 509
def get_latest_arrivals_count(limit, category=-1):
1120 rajveer 510
    count = get_latest_arrivals_query(category).count()
511
    if count is None:
512
        count = 0
513
    count = min(count, limit)
766 rajveer 514
    return count
602 chandransh 515
 
626 chandransh 516
def get_latest_arrivals_catalog_ids(start_index, stop_index, category=-1):
1098 chandransh 517
    query = get_latest_arrivals_query(category)
518
    latest_arrivals = query.all()[start_index:stop_index]
519
    return [item.catalog_item_id for item in latest_arrivals]
520
 
521
def get_latest_arrivals_query(category):
522
    query = Item.query.filter_by(status=status.ACTIVE)
626 chandransh 523
    if category != -1:
621 chandransh 524
        query = query.filter_by(category=category)
1098 chandransh 525
    query = query.group_by(Item.catalog_item_id).order_by(desc(Item.startDate))
526
    return query
609 chandransh 527
 
528
def get_thrift_item_list(items):
1098 chandransh 529
    return [to_t_item(item) for item in items if item != None]
635 rajveer 530
 
1155 rajveer 531
def generate_new_entity_id():
532
    generator =  EntityIDGenerator.query.one()
533
    id = generator.id + 1
534
    generator.id = id
535
    session.commit()
536
    return id
537
 
635 rajveer 538
def put_category_object(object):
539
    category = Category.get_by(id=1)
540
    if category is None:
541
        category = Category()
542
    category.object = object    
543
    session.commit()
544
    return True
545
 
546
def get_category_object():
766 rajveer 547
    object = Category.get_by(id=1).object
548
    return object
549
 
1341 chandransh 550
def get_item_pricing(item_id, warehouse_id):
551
    item = Item.query.filter_by(id=item_id).first()
552
    if item is None:
553
        raise InventoryServiceException(101, "Bad Item")
554
 
555
    warehouse = get_Warehouse(warehouse_id)
556
    if not warehouse:
557
        raise InventoryServiceException(108, "Bad Warehouse")
558
    vendor = warehouse.vendor
559
 
560
    try:
561
        item_pricing = VendorItemPricing.query.filter_by(vendor=vendor, item=item).one()
1347 chandransh 562
        return item_pricing
1341 chandransh 563
    except sqlalchemy.orm.exc.MultipleResultsFound:
564
        raise InventoryServiceException(110, "Multiple pricing information present for Vendor: " + vendor.name + " and Item: " + str(item_id))
565
    except sqlalchemy.orm.exc.NoResultFound:
566
        raise InventoryServiceException(111, "Missing pricing information for Vendor: " + vendor.name + " and Item: " + str(item_id))
567
 
766 rajveer 568
def close_session():
569
    if session.is_active:
570
        print "session is active. closing it."
571
        session.close()