Subversion Repositories SmartDukaan

Rev

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