Subversion Repositories SmartDukaan

Rev

Rev 2404 | Rev 2497 | 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,\
1991 ankur.sing 11
    VendorItemMapping, Vendor
122 ashish 12
from shop2020.thriftpy.model.v1.catalog.ttypes import \
13
    InventoryServiceException, status
2065 ankur.sing 14
from shop2020.model.v1.catalog.impl.Convertors import to_t_item,\
15
    to_t_vendor_item_pricing
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
1970 rajveer 20
from shop2020.model.v1.catalog.impl.CategoryManager import CategoryManager
2404 chandransh 21
from sqlalchemy.sql.expression import and_, distinct, func
94 ashish 22
 
23
def initialize():
24
    DataService.initialize()
25
 
26
 
103 ashish 27
def get_all_items(is_active):
28
    if is_active:
766 rajveer 29
        items = Item.query.filter_by(status= status.ACTIVE).all()
103 ashish 30
    else:
766 rajveer 31
        items = Item.query.all()
32
    return items
33
 
103 ashish 34
def get_all_items_by_status(status):
2174 ankur.sing 35
    if status == None:
103 ashish 36
        #return all items
766 rajveer 37
        items = get_all_items(False)
103 ashish 38
    else:
766 rajveer 39
        items = Item.query.filter_by(status=status).all()
40
    return items
103 ashish 41
 
635 rajveer 42
def get_item(item_id):
766 rajveer 43
    item = Item.get_by(id=item_id)
44
    return item
94 ashish 45
 
635 rajveer 46
def get_items_by_catalog_id(catalog_id):
447 rajveer 47
    query = Item.query.filter_by(catalog_item_id=catalog_id)
437 rajveer 48
    try:
635 rajveer 49
        items = query.all()
50
        return get_thrift_item_list(items)
1399 rajveer 51
    except Exception as ex:
52
        print ex
437 rajveer 53
        raise InventoryServiceException(109, "Item not found")
54
 
576 chandransh 55
def is_active(item_id):
56
    try:
635 rajveer 57
        item = get_item(item_id)
576 chandransh 58
        return item.status == status.ACTIVE
59
    except InventoryServiceException:
60
        return False
2035 rajveer 61
 
62
def get_item_status_description(itemId):
63
    item = get_item(itemId)
64
    return item.status_description
563 chandransh 65
 
94 ashish 66
def get_Warehouse(warehouse_id):
67
    return Warehouse.get_by(id=warehouse_id)
68
 
122 ashish 69
def get_all_warehouses_by_status(status):
70
    if not status:
766 rajveer 71
        warehouses = Warehouse.query.all()
122 ashish 72
    else:
851 chandransh 73
        warehouses = Warehouse.query.filter_by(status=status).all()
74
    return warehouses
122 ashish 75
 
94 ashish 76
def get_all_warehouses_for_item(item_id):
635 rajveer 77
    item = get_item(item_id)
94 ashish 78
    if not item:
122 ashish 79
        raise InventoryServiceException(108, "Some unforeseen error while obtaining item")
80
    return item.get_all_warehouses
94 ashish 81
 
122 ashish 82
def get_all_items_for_warehouse(warehouse_id):
83
    warehouse = get_Warehouse(warehouse_id)
84
    if not warehouse:
85
        raise InventoryServiceException(108, "bad warehouse")
86
    return warehouse.all_items
87
 
94 ashish 88
def add_warehouse(warehouse):
103 ashish 89
    if not warehouse:
122 ashish 90
        raise InventoryServiceException(108, "Bad warehouse")
103 ashish 91
    if get_Warehouse(warehouse.id):
92
        #warehouse is already present.
122 ashish 93
        raise InventoryServiceException(101, "Warehouse already present")
103 ashish 94
 
94 ashish 95
    ds_warehouse = Warehouse()
96
    ds_warehouse.id = warehouse.id
97
    ds_warehouse.location = warehouse.location
122 ashish 98
    ds_warehouse.status = status.ACTIVE
103 ashish 99
    ds_warehouse.addedOn = datetime.datetime.now()
483 rajveer 100
    ds_warehouse.lastCheckedOn = datetime.datetime.now()
101
    ds_warehouse.tinNumber = warehouse.tinNumber
102
    ds_warehouse.pincode = warehouse.pincode
103
    if warehouse.vendorString:
104
        ds_warehouse.vendorString = warehouse.vendorString
94 ashish 105
    session.commit()
103 ashish 106
    return ds_warehouse.id
94 ashish 107
 
122 ashish 108
def update_item(item):
109
    if not item:
110
        raise InventoryServiceException(108, "Bad item in request")
111
 
112
    if not item.id:
609 chandransh 113
        raise InventoryServiceException(101, "Missing id for update")
122 ashish 114
 
2120 ankur.sing 115
    validate_item_prices(item)
2065 ankur.sing 116
 
635 rajveer 117
    ds_item = get_item(item.id)
122 ashish 118
 
119
    if not ds_item:
609 chandransh 120
        raise InventoryServiceException(101, "Item missing in our database")
122 ashish 121
 
963 chandransh 122
    if item.productGroup:
123
        ds_item.product_group = item.productGroup 
124
    if item.brand:
125
        ds_item.brand = item.brand
511 rajveer 126
    if item.modelName:
127
        ds_item.model_name = item.modelName
128
    if item.modelNumber:
129
        ds_item.model_number = item.modelNumber
609 chandransh 130
    if item.color:
131
        ds_item.color = item.color
483 rajveer 132
    if item.category:
133
        ds_item.category = item.category
134
    if item.comments:
135
        ds_item.comments = item.comments
511 rajveer 136
 
122 ashish 137
    if item.catalogItemId:
138
        ds_item.catalog_item_id = item.catalogItemId
483 rajveer 139
 
2129 ankur.sing 140
    ds_item.mrp = item.mrp
141
    ds_item.sellingPrice = item.sellingPrice
142
 
483 rajveer 143
    if item.mop:
144
        ds_item.mop = item.mop
609 chandransh 145
    if item.dealerPrice:
146
        ds_item.dealerPrice = item.dealerPrice
2065 ankur.sing 147
    if item.transferPrice:
148
        ds_item.transfer_price = item.transferPrice
2174 ankur.sing 149
    ds_item.weight = item.weight
2129 ankur.sing 150
 
2358 ankur.sing 151
    if ds_item.status != item.itemStatus:
2402 rajveer 152
        add_status_change_log(ds_item, item.itemStatus)
2358 ankur.sing 153
        ds_item.status = item.itemStatus
2035 rajveer 154
    if item.status_description:
155
        ds_item.status_description = item.status_description
122 ashish 156
 
511 rajveer 157
    if item.startDate:
2116 ankur.sing 158
        ds_item.startDate = to_py_date(item.startDate)
511 rajveer 159
    if item.retireDate:
2116 ankur.sing 160
        ds_item.retireDate = to_py_date(item.retireDate)
511 rajveer 161
 
122 ashish 162
    if item.featureId:
163
        ds_item.feature_id = item.featureId
164
    if item.featureDescription:
165
        ds_item.feature_description = item.featureDescription
166
 
2129 ankur.sing 167
    ds_item.bestDealText = item.bestDealText
168
    ds_item.bestDealValue = item.bestDealValue
2065 ankur.sing 169
    ds_item.bestSellingRank = item.bestSellingRank
170
    ds_item.defaultForEntity = item.defaultForEntity
2251 ankur.sing 171
    ds_item.risky = item.risky
2347 ankur.sing 172
    ds_item.updatedOn = datetime.datetime.now()
2065 ankur.sing 173
 
122 ashish 174
    session.commit();
175
    return ds_item.id
94 ashish 176
 
2116 ankur.sing 177
def check_vendor_item_mapping(product_group, brand, model_number, color, vendor_id, vendor_category):
178
    key = product_group.strip().lower() + '|' + brand.strip().lower() + '|' + model_number.strip().lower() + '|' + color.strip().lower()
179
    try:
180
        vim = VendorItemMapping.query.filter_by(vendor_id=vendor_id, item_key=key, vendor_category=vendor_category).one()
181
        return True
182
    except:
183
        return False
184
 
103 ashish 185
def add_item(item):
186
    if not item:
122 ashish 187
        raise InventoryServiceException(108, "Bad item in request")
635 rajveer 188
    if get_item(item.id):
122 ashish 189
        raise InventoryServiceException(101, "Item already exists")
2120 ankur.sing 190
 
191
    validate_item_prices(item)
192
 
103 ashish 193
    ds_item = Item()
963 chandransh 194
    if item.productGroup:
195
        ds_item.product_group = item.productGroup
196
    if item.brand:
197
        ds_item.brand = item.brand
515 rajveer 198
    if item.modelName:
199
        ds_item.model_name = item.modelName
200
    if item.modelNumber:
201
        ds_item.model_number = item.modelNumber
609 chandransh 202
    if item.color:
203
        ds_item.color = item.color
2116 ankur.sing 204
    if item.hotspotCategory:
205
        ds_item.hotspotCategory = item.hotspotCategory
206
        if item.hotspotCategory == 'Handsets':
2403 ankur.sing 207
            ds_item.preferredWarehouse = 1
2116 ankur.sing 208
        else:
2403 ankur.sing 209
            ds_item.preferredWarehouse = 2
2116 ankur.sing 210
 
483 rajveer 211
    if item.category:
212
        ds_item.category = item.category
213
    if item.comments:
214
        ds_item.comments = item.comments
215
 
103 ashish 216
    ds_item.addedOn = datetime.datetime.now()
609 chandransh 217
    ds_item.updatedOn = datetime.datetime.now()
2116 ankur.sing 218
    if item.startDate:
219
        ds_item.startDate = to_py_date(item.startDate)
220
    if item.retireDate:
221
        ds_item.retireDate = to_py_date(item.retireDate)
609 chandransh 222
 
483 rajveer 223
    if item.mrp:
224
        ds_item.mrp = item.mrp
225
    if item.mop:
226
        ds_item.mop = item.mop
227
    if item.sellingPrice:
228
        ds_item.sellingPrice = item.sellingPrice
609 chandransh 229
    if item.dealerPrice:
230
        ds_item.dealerPrice = item.dealerPrice
2116 ankur.sing 231
    if item.transferPrice:
232
        ds_item.transfer_price = item.transferPrice
122 ashish 233
    if item.weight:
234
        ds_item.weight = item.weight
235
 
236
    if item.featureId:
237
        ds_item.feature_id = item.featureId
238
    if item.featureDescription:
239
        ds_item.feature_description = item.featureDescription
240
 
103 ashish 241
    if item.otherInfo:
242
        for k,v in item.otherInfo.iteritems():
243
            info = ItemInfo()
244
            info.key = k
245
            info.value = v
246
            ds_item.iteminfo.append(info)
2116 ankur.sing 247
 
103 ashish 248
    #check if categories present. If yes, add them to system
122 ashish 249
 
609 chandransh 250
    if item.bestDealValue:
251
        ds_item.bestDealValue = item.bestDealValue
252
    if item.bestDealText:
253
        ds_item.bestDealText = item.bestDealText
2116 ankur.sing 254
    if item.bestSellingRank:
255
        ds_item.bestSellingRank = item.bestSellingRank
256
    ds_item.defaultForEntity = item.defaultForEntity
2251 ankur.sing 257
    ds_item.risky = item.risky
609 chandransh 258
 
2116 ankur.sing 259
    # Check if a similar item already exists in our database
260
    similar_item = Item.query.filter_by(product_group=item.productGroup, brand = item.brand, model_number=item.modelNumber, hotspotCategory=item.hotspotCategory).first()
261
    print "[SIMILAR ITEM FOUND:] FOR {0} {1} {2} {3} {4}".format(item.productGroup, item.brand, item.modelNumber, item.hotspotCategory, item.color)
262
 
263
    if similar_item is None or similar_item.catalog_item_id is None:
264
        # If there is no similar item in the database from before,
265
        # use the entity_id_generator
266
        entity_id = EntityIDGenerator.query.first()
267
        ds_item.catalog_item_id = entity_id.id + 1
268
        ds_item.status = status.IN_PROCESS
269
        ds_item.status_description = "This item is in process."
270
        entity_id.id = entity_id.id  + 1
271
    else:
272
        #If a similar item already exists for a product group, brand and model_number, set it as same.
273
        ds_item.catalog_item_id = similar_item.catalog_item_id
274
        ds_item.category = similar_item.category
275
        ds_item.status = similar_item.status
276
        ds_item.status_description = similar_item.status_description
277
 
103 ashish 278
    session.commit();
609 chandransh 279
    return ds_item.id       
103 ashish 280
 
483 rajveer 281
def update_inventory(warehouse_id, timestamp, availability):
282
    warehouse = get_Warehouse(warehouse_id)
283
    if not warehouse:
284
        raise InventoryServiceException(107, "Warehouse? Where?")
285
 
286
    time = datetime.datetime.now()
287
    warehouse.lastCheckedOn = time
288
    warehouse.vendorString = timestamp
1368 chandransh 289
    vendor = warehouse.vendor
2368 ankur.sing 290
    session.commit()
1368 chandransh 291
    for item_key, quantity in availability.iteritems():
483 rajveer 292
        try:
1368 chandransh 293
            vendor_item_mapping = VendorItemMapping.query.filter_by(vendor=vendor, item_key=item_key).one();
294
            item = vendor_item_mapping.item
494 rajveer 295
        except:
296
            continue  
297
        try:
483 rajveer 298
            item_inventory_history = ItemInventoryHistory()
299
            item_inventory_history.warehouse = warehouse
300
            item_inventory_history.item = item
301
            item_inventory_history.timestamp = time
302
            item_inventory_history.availibility = quantity
303
 
304
            current_inventory_snapshot = CurrentInventorySnapshot.get_by(item=item, warehouse=warehouse)
305
            if not current_inventory_snapshot:
306
                current_inventory_snapshot = CurrentInventorySnapshot()
307
                current_inventory_snapshot.item = item
308
                current_inventory_snapshot.warehouse = warehouse
309
                current_inventory_snapshot.availibility = 0
871 chandransh 310
                current_inventory_snapshot.reserved = 0
483 rajveer 311
            # added the difference in the current inventory    
312
            current_inventory_snapshot.availibility = current_inventory_snapshot.availibility + quantity
313
        except:
314
            raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
2368 ankur.sing 315
        session.commit() 
316
        check_risky_item(item)
483 rajveer 317
 
94 ashish 318
def get_item_inventoy(item_id):
319
 
320
    inventory = Item.get_by(id=item_id).currentInventory
321
    if not inventory:
122 ashish 322
        raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
94 ashish 323
    return inventory
324
 
635 rajveer 325
def get_item_inventory_by_item_id(item_id):
326
    inventory = Item.get_by(id=item_id).currentInventory
379 ashish 327
    if not inventory:
328
        raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
329
    return inventory
330
 
103 ashish 331
 
332
def retire_warehouse(warehouse_id):
333
    if not warehouse_id:
122 ashish 334
        raise InventoryServiceException(101, "Bad warehouse id")
103 ashish 335
    warehouse = get_Warehouse(warehouse_id)
336
    if not warehouse:
122 ashish 337
        raise InventoryServiceException(108, "warehouse id not present")
338
    warehouse.status = status.DELETED;
103 ashish 339
    session.commit()
340
 
341
def retire_item(item_id):
342
    if not item_id:
122 ashish 343
        raise InventoryServiceException(101, "bad item id")
635 rajveer 344
    item = get_item(item_id)
103 ashish 345
    if not item:
122 ashish 346
        raise InventoryServiceException(108, "item id not present")
347
    item.status = status.PHASED_OUT
348
    item.retireDate = datetime.datetime.now()
103 ashish 349
    session.commit()
122 ashish 350
 
351
#need to implement threads based solution here
103 ashish 352
def start_item_on(item_id, timestamp):
353
    if not item_id:
122 ashish 354
        raise InventoryServiceException(101, "bad item id")
635 rajveer 355
    item = get_item(item_id)
103 ashish 356
    if not item:
122 ashish 357
        raise InventoryServiceException(108, "item id not present")
103 ashish 358
 
122 ashish 359
    item.status = status.ACTIVE
360
    item.startDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
361
    add_status_change_log(item, status.ACTIVE)
103 ashish 362
    session.commit()
363
 
122 ashish 364
#need to implement threads here
103 ashish 365
def retire_item_on(item_id, timestamp):
366
    if not item_id:
122 ashish 367
        raise InventoryServiceException(101, "bad item id")
635 rajveer 368
    item = get_item(item_id)
103 ashish 369
    if not item:
122 ashish 370
        raise InventoryServiceException(108, "item id not present")
103 ashish 371
 
122 ashish 372
    item.status = status.PHASED_OUT
373
    item.retireDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
374
    add_status_change_log(item, status.PHASED_OUT)
103 ashish 375
    session.commit()
376
 
377
def add_status_change_log(item, new_status):
378
    item_change_log = ItemChangeLog()
379
    item_change_log.new_status = new_status
380
    item_change_log.old_status = item.status
381
    item_change_log.timestamp = datetime.datetime.now()
382
    item_change_log.item = item
383
    session.commit()
384
 
385
def change_item_status(item_id, new_status):
386
    if not item_id:
122 ashish 387
        raise InventoryServiceException(101, "bad item id")
635 rajveer 388
    item = get_item(item_id)
103 ashish 389
    if not item:
122 ashish 390
        raise InventoryServiceException(108, "item id not present")
2116 ankur.sing 391
    add_status_change_log(item, new_status)
122 ashish 392
    item.status = new_status
2251 ankur.sing 393
    if item.status == status.PHASED_OUT:
394
        item.status_description = "This item has been phased out"
395
    elif item.status == status.DELETED:
396
        item.status_description = "This item has been deleted"
397
    elif item.status == status.PAUSED:
398
        item.status_description = "This item is currently out of stock"        
399
    elif item.status == status.ACTIVE:
400
        item.status_description = "This item is active"
401
    elif item.status == status.IN_PROCESS:
402
        item.status_description = "This item is in process"
403
    elif item.status == status.CONTENT_COMPLETE:
404
        item.status_description = "This item is in process"
103 ashish 405
    session.commit()
406
 
407
def get_item_availability_for_warehouse(warehouse_id, item_id):
408
    if not warehouse_id:
122 ashish 409
        raise InventoryServiceException(101, "bad warehouse_id")
103 ashish 410
    if not item_id:
122 ashish 411
        raise InventoryServiceException(101, "bad item_id")
103 ashish 412
 
413
    warehouse = get_Warehouse(warehouse_id)
414
    if not warehouse:
122 ashish 415
        raise InventoryServiceException(108, "warehouse does not exist")
635 rajveer 416
    item = get_item(item_id)
504 rajveer 417
 
103 ashish 418
    if not item:
766 rajveer 419
        raise InventoryServiceException(108, "item does not exist")
122 ashish 420
 
494 rajveer 421
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id)
504 rajveer 422
    query = query.filter_by(item_id = item.id)
494 rajveer 423
    try:
424
        current_inventory_snapshot = query.one()
871 chandransh 425
        return current_inventory_snapshot.availibility - current_inventory_snapshot.reserved
494 rajveer 426
    except:
427
        return 0
428
    """
429
    current_inventory_snapshot = CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse_id == warehouse_id, CurrentInventorySnapshot.item_id == item_id).one()
103 ashish 430
    if not current_inventory_snapshot:
431
        return 0
432
    else:
433
        return current_inventory_snapshot.availibility
494 rajveer 434
    """
643 chandransh 435
 
2368 ankur.sing 436
def check_risky_item(item):
2251 ankur.sing 437
    if not item.risky:
438
        return
2368 ankur.sing 439
    all_inventory = CurrentInventorySnapshot.query.filter_by(item = item).all()
440
    availibility = 0
441
    reserved = 0
442
    for currInv in all_inventory:
443
        availibility = availibility + currInv.availibility
444
        reserved = reserved + currInv.reserved 
445
    if availibility - reserved <= 0:
2251 ankur.sing 446
        if item.status == status.ACTIVE:
447
            change_item_status(item.id, status.PAUSED)
448
    else:
449
        if item.status == status.PAUSED:
450
            change_item_status(item.id, status.ACTIVE)
2368 ankur.sing 451
    session.commit()
2251 ankur.sing 452
 
871 chandransh 453
def reserve_item_in_warehouse(item_id, warehouse_id, quantity):    
454
    if not warehouse_id:
455
        raise InventoryServiceException(101, "bad warehouse_id")
2251 ankur.sing 456
    item = get_item(item_id)
457
    if not item:
871 chandransh 458
        raise InventoryServiceException(101, "bad item_id")
459
 
460
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id)
461
    try:
462
        current_inventory_snapshot = query.one()
463
        current_inventory_snapshot.reserved = current_inventory_snapshot.reserved + quantity
464
        session.commit()
2368 ankur.sing 465
        check_risky_item(item)
871 chandransh 466
        return True
467
    except:
468
        print "Unexpected error:", sys.exc_info()[0]
469
        return False
470
 
471
def reduce_reservation_count(item_id, warehouse_id, quantity):
472
    if not warehouse_id:
473
        raise InventoryServiceException(101, "bad warehouse_id")
2251 ankur.sing 474
    item = get_item(item_id)
475
    if not item:
871 chandransh 476
        raise InventoryServiceException(101, "bad item_id")
477
 
478
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id)
479
    try:
480
        current_inventory_snapshot = query.one()
481
        current_inventory_snapshot.reserved = current_inventory_snapshot.reserved - quantity
482
        session.commit()
2368 ankur.sing 483
        check_risky_item(item)
871 chandransh 484
        return True
485
    except:
486
        print "Unexpected error:", sys.exc_info()[0]
487
        return False
488
 
2075 rajveer 489
def mark_item_as_content_complete(entity_id, category, brand, modelName, modelNumber):
723 chandransh 490
    content_complete_status = status.CONTENT_COMPLETE
1294 chandransh 491
    items = Item.query.filter_by(catalog_item_id=entity_id, status=status.IN_PROCESS).all()
723 chandransh 492
    current_timestamp = datetime.datetime.now()
493
    for item in items:
494
        item_change_log = ItemChangeLog()
495
        item_change_log.old_status = item.status
496
        item_change_log.new_status = content_complete_status
497
        item_change_log.timestamp = current_timestamp
498
        item_change_log.item = item
499
 
500
        item.status = content_complete_status
2075 rajveer 501
        item.category = category
502
        item.brand = brand
2081 rajveer 503
        item.model_name = modelName
504
        item.model_number = modelNumber
723 chandransh 505
        item.updatedOn = current_timestamp
506
    session.commit()
507
    return True
1294 chandransh 508
 
643 chandransh 509
def get_item_availability_for_location(warehouse_loc, item_id):
510
    if warehouse_loc is None:
511
        raise InventoryServiceException(101, "Bad Warehouse Location")
512
    if not item_id:
1416 chandransh 513
        raise InventoryServiceException(101, "Bad Item id")
643 chandransh 514
 
1416 chandransh 515
    item = Item.get_by(id=item_id)
643 chandransh 516
    warehouses = Warehouse.query.filter_by(logisticsLocation=warehouse_loc).all()
517
    warehouse_ids = [warehouse.id for warehouse in warehouses]
518
    warehouse_retid = -1
519
    global_availability = 0
520
    for warehouse_id in warehouse_ids:
785 rajveer 521
        try:
871 chandransh 522
            current_inventory_snapshot = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id).one()
523
            availability = current_inventory_snapshot.availibility - current_inventory_snapshot.reserved
785 rajveer 524
        except:
525
            availability = 0    
643 chandransh 526
        if availability > global_availability:
527
            warehouse_retid = warehouse_id
528
            global_availability = availability
2341 chandransh 529
 
643 chandransh 530
    if warehouse_retid == -1:
759 chandransh 531
        # This is the case when all warehouses have exhausted their
532
        # inventory of this item or no warehouse is available in this
533
        # location.
1416 chandransh 534
        warehouse_retid = int(item.preferredWarehouse)
2344 chandransh 535
        warehouse = Warehouse.get_by(id=warehouse_retid)
2341 chandransh 536
        try:
537
            current_inventory_snapshot = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_retid, item_id = item_id).one()
538
            global_availability = current_inventory_snapshot.availibility - current_inventory_snapshot.reserved
539
        except:
540
            global_availability = 0
643 chandransh 541
 
2341 chandransh 542
    return [warehouse.logisticsLocation, warehouse_retid, global_availability]
543
 
122 ashish 544
def get_warehouses_for_item(item_id):
643 chandransh 545
 
122 ashish 546
    if not item_id:
547
        raise InventoryServiceException(101, "bad item_id")
635 rajveer 548
    item = get_item(item_id)
122 ashish 549
 
550
    if not item:
551
        raise InventoryServiceException(101, "bad item")
552
 
483 rajveer 553
    warehouses = item.currentInventory.warehouse
501 rajveer 554
    return warehouses
555
 
2404 chandransh 556
def get_child_categories(category):
557
    cm = CategoryManager()
558
    return cm.getCategory(category).children_category_ids
559
 
626 chandransh 560
def get_best_sellers(start_index, stop_index, category=-1):
2404 chandransh 561
    '''
562
    Returns the Best Sellers between the start and the stop index in the given category
563
    '''
1926 rajveer 564
    query = get_best_sellers_query(category, None)
1098 chandransh 565
    best_sellers = query.all()[start_index:stop_index]
621 chandransh 566
    return get_thrift_item_list(best_sellers)
567
 
2093 chandransh 568
def get_best_sellers_count(category=-1):
2404 chandransh 569
    '''
570
    Returns the number of best sellers in the given category
571
    '''
1926 rajveer 572
    count = get_best_sellers_query(category, None).count()
1120 rajveer 573
    if count is None:
574
        count = 0
766 rajveer 575
    return count
621 chandransh 576
 
1926 rajveer 577
def get_best_sellers_catalog_ids(start_index, stop_index, brand, category=-1):
2404 chandransh 578
    '''
579
    Returns the Best sellers for the given brand and category between the start and the stop index.
580
    Ignores the category if it's passed as -1 and the brand if it's passed as None. 
581
    '''
1926 rajveer 582
    query = get_best_sellers_query(category, brand)
1098 chandransh 583
    best_sellers = query.all()[start_index:stop_index]
621 chandransh 584
    return [item.catalog_item_id for item in best_sellers]
1970 rajveer 585
 
1926 rajveer 586
def get_best_sellers_query(category, brand):
2404 chandransh 587
    '''
588
    Returns the query to be used for getting Best Sellers.
589
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
590
    '''
1098 chandransh 591
    query = Item.query.filter_by(status=status.ACTIVE).filter(Item.bestSellingRank != None)
626 chandransh 592
    if category != -1:
1970 rajveer 593
        all_categories = [category]
594
        child_categories = get_child_categories(category)
595
        if child_categories is not None:
596
            all_categories = all_categories + child_categories 
597
        query = query.filter(Item.category.in_(all_categories))
1926 rajveer 598
    if brand is not None:
599
        query = query.filter_by(brand=brand)
1098 chandransh 600
    query = query.order_by(asc(Item.bestSellingRank))
621 chandransh 601
    return query
609 chandransh 602
 
1098 chandransh 603
def get_best_deals(category=-1):
2404 chandransh 604
    '''
605
    Returns the Best deals in the given category. Ignores the category if it's passed as -1.
606
    '''
607
    query = get_best_deals_query(Item, category, None)
1098 chandransh 608
    items = query.all()
609 chandransh 609
    return get_thrift_item_list(items)
610
 
1098 chandransh 611
def get_best_deals_count(category=-1):
2404 chandransh 612
    '''
613
    Returns the count of best deals in the given category.
614
    Ignores the category if it's -1.
615
    '''
616
    count = get_best_deals_counting_query(func.count(distinct(Item.catalog_item_id)), category, None).scalar()
1120 rajveer 617
    if count is None:
618
        count = 0
766 rajveer 619
    return count
501 rajveer 620
 
1926 rajveer 621
def get_best_deals_catalog_ids(start_index, stop_index, brand, category=-1):
2404 chandransh 622
    '''
623
    Returns the catalog_item_ids of best deal items for the given brand and category.
624
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
625
    '''
626
    query = get_best_deals_query(Item, category, brand)
1098 chandransh 627
    best_deal_items = query.all()[start_index:stop_index]
628
    return [item.catalog_item_id for item in best_deal_items]
629
 
2404 chandransh 630
def get_best_deals_counting_query(obj, category, brand):
631
    '''
632
    Returns the query to be used to select the best deals in the given brand and category.
633
    Ignores the category if it's passed as -1 and the brand if it's passed as None. 
634
    '''
635
    query = session.query(obj).filter_by(status=status.ACTIVE).filter(Item.bestDealValue != None)
626 chandransh 636
    if category != -1:
1970 rajveer 637
        all_categories = [category]
638
        child_categories = get_child_categories(category)
639
        if child_categories is not None:
640
            all_categories = all_categories + child_categories 
641
        query = query.filter(Item.category.in_(all_categories))
1926 rajveer 642
    if brand is not None:
643
        query = query.filter_by(brand=brand)
2404 chandransh 644
    return query
645
 
646
def get_best_deals_query(obj, category, brand):
647
    '''
648
    Returns the query to be used to get the best deals in the given category and brand.
649
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
650
    '''
651
    query = get_best_deals_counting_query(obj, category, brand)
1098 chandransh 652
    query = query.group_by(Item.catalog_item_id).order_by(desc(Item.bestDealValue))
653
    return query
609 chandransh 654
 
1098 chandransh 655
def get_latest_arrivals(limit, category=-1):
2404 chandransh 656
    '''
657
    Returns up to limit number of Latest Arrivals in the given category.
658
    '''
659
    query = get_latest_arrivals_query(Item, category, None)
1098 chandransh 660
    items = query.all()[0:limit]
609 chandransh 661
    return get_thrift_item_list(items)
598 chandransh 662
 
1098 chandransh 663
def get_latest_arrivals_count(limit, category=-1):
2404 chandransh 664
    '''
665
    Returns the number of latest arrivals which will be displayed on the website.
666
    Ignores the category if it's passed as -1.
667
    '''
668
    count = get_latest_arrivals_counting_query(func.count(distinct(Item.catalog_item_id)), category, None).scalar()
1120 rajveer 669
    if count is None:
670
        count = 0
671
    count = min(count, limit)
766 rajveer 672
    return count
602 chandransh 673
 
1926 rajveer 674
def get_latest_arrivals_catalog_ids(start_index, stop_index, brand, category=-1):
2404 chandransh 675
    '''
676
    Returns the catalog_item_ids of the latest arrivals between the start and the stop index
677
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
678
    '''
679
    query = get_latest_arrivals_query(Item, category, brand)
1098 chandransh 680
    latest_arrivals = query.all()[start_index:stop_index]
681
    return [item.catalog_item_id for item in latest_arrivals]
682
 
2404 chandransh 683
def get_latest_arrivals_counting_query(obj, category, brand):
684
    '''
685
    Returns the query to be used to count Latest arrivals.
686
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
687
    '''
688
    query = session.query(obj).filter_by(status=status.ACTIVE)
626 chandransh 689
    if category != -1:
1970 rajveer 690
        all_categories = [category]
691
        child_categories = get_child_categories(category)
692
        if child_categories is not None:
693
            all_categories = all_categories + child_categories 
694
        query = query.filter(Item.category.in_(all_categories))
1926 rajveer 695
    if brand is not None:
696
        query = query.filter_by(brand=brand)
2404 chandransh 697
    return query
698
 
699
def get_latest_arrivals_query(obj, category, brand):
700
    '''
701
    Returns the query to be used to retrieve Latest Arrivals.
702
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
703
    '''
704
    query = get_latest_arrivals_counting_query(obj, category, brand)
1098 chandransh 705
    query = query.group_by(Item.catalog_item_id).order_by(desc(Item.startDate))
706
    return query
609 chandransh 707
 
708
def get_thrift_item_list(items):
1098 chandransh 709
    return [to_t_item(item) for item in items if item != None]
635 rajveer 710
 
1155 rajveer 711
def generate_new_entity_id():
712
    generator =  EntityIDGenerator.query.one()
713
    id = generator.id + 1
714
    generator.id = id
715
    session.commit()
716
    return id
717
 
635 rajveer 718
def put_category_object(object):
719
    category = Category.get_by(id=1)
720
    if category is None:
721
        category = Category()
722
    category.object = object    
723
    session.commit()
724
    return True
725
 
726
def get_category_object():
766 rajveer 727
    object = Category.get_by(id=1).object
728
    return object
729
 
1341 chandransh 730
def get_item_pricing(item_id, warehouse_id):
731
    item = Item.query.filter_by(id=item_id).first()
732
    if item is None:
733
        raise InventoryServiceException(101, "Bad Item")
734
 
735
    warehouse = get_Warehouse(warehouse_id)
736
    if not warehouse:
737
        raise InventoryServiceException(108, "Bad Warehouse")
738
    vendor = warehouse.vendor
739
 
740
    try:
741
        item_pricing = VendorItemPricing.query.filter_by(vendor=vendor, item=item).one()
1347 chandransh 742
        return item_pricing
1341 chandransh 743
    except sqlalchemy.orm.exc.MultipleResultsFound:
744
        raise InventoryServiceException(110, "Multiple pricing information present for Vendor: " + vendor.name + " and Item: " + str(item_id))
745
    except sqlalchemy.orm.exc.NoResultFound:
746
        raise InventoryServiceException(111, "Missing pricing information for Vendor: " + vendor.name + " and Item: " + str(item_id))
747
 
1970 rajveer 748
def add_category(t_category):
749
    category = Category.get_by(id=t_category.id)
750
    if category is None:
751
        category = Category()
752
    category.id = t_category.id 
753
    category.label = t_category.label
754
    category.description = t_category.description
755
    category.parent_category_id = t_category.parent_category_id 
756
    session.commit()
757
    return True
758
 
759
def get_category(id):
760
    return Category.query.filter_by(id=id).first()
761
 
762
def get_all_categories():
763
    return Category.query.all()
764
 
1991 ankur.sing 765
 
766
def get_all_item_pricing(item_id):
767
    item = Item.query.filter_by(id=item_id).first()
768
    if item is None:
769
        raise InventoryServiceException(101, "Bad Item")
770
    item_pricing = VendorItemPricing.query.filter_by(item=item).all()
771
    return item_pricing
772
 
2116 ankur.sing 773
def get_item_mappings(item_id):
774
    item = Item.query.filter_by(id=item_id).first()
775
    if item is None:
776
        raise InventoryServiceException(101, "Bad Item")
777
    item_mappings = VendorItemMapping.query.filter_by(item=item).all()
778
    return item_mappings
779
 
780
def add_vendor_pricing(vendorItemPricing):
1991 ankur.sing 781
    if not vendorItemPricing:
782
        raise InventoryServiceException(108, "Bad vendorItemPricing in request")
783
    vendorId = vendorItemPricing.vendorId
784
    itemId = vendorItemPricing.itemId
785
 
786
    try:
787
        vendor = Vendor.query.filter_by(id=vendorId).one()
788
    except:
789
        raise InventoryServiceException(101, "Vendor not found for vendorId " + str(vendorId))
790
 
791
    try:
792
        item = Item.query.filter_by(id=itemId).one()
793
    except:
794
        raise InventoryServiceException(101, "Item not found for vendorId " + str(itemId))
795
 
2120 ankur.sing 796
    validate_vendor_prices(to_t_item(item), vendorItemPricing)
2065 ankur.sing 797
 
1991 ankur.sing 798
    try:
799
        ds_vendorItemPricing = VendorItemPricing.query.filter(and_(VendorItemPricing.vendor==vendor, VendorItemPricing.item==item)).one()
800
    except:
2116 ankur.sing 801
        ds_vendorItemPricing = VendorItemPricing()
802
        ds_vendorItemPricing.vendor = vendor
803
        ds_vendorItemPricing.item = item
1991 ankur.sing 804
 
805
    if vendorItemPricing.mop:
806
        ds_vendorItemPricing.mop = vendorItemPricing.mop
807
    if vendorItemPricing.dealerPrice:
808
        ds_vendorItemPricing.dealerPrice = vendorItemPricing.dealerPrice
809
    if vendorItemPricing.transferPrice:
2065 ankur.sing 810
        ds_vendorItemPricing.transfer_price = vendorItemPricing.transferPrice
1991 ankur.sing 811
 
812
    session.commit()
813
    return
814
 
2358 ankur.sing 815
def add_vendor_item_mapping(key, vendorItemMapping):
2116 ankur.sing 816
    if not vendorItemMapping:
817
        raise InventoryServiceException(108, "Bad vendorItemMapping in request")
818
    vendorId = vendorItemMapping.vendorId
819
    itemId = vendorItemMapping.itemId
820
 
821
    try:
822
        vendor = Vendor.query.filter_by(id=vendorId).one()
823
    except:
824
        raise InventoryServiceException(101, "Vendor not found for vendorId " + str(vendorId))
825
 
826
    try:
827
        item = Item.query.filter_by(id=itemId).one()
828
    except:
829
        raise InventoryServiceException(101, "Item not found for vendorId " + str(itemId))
830
 
831
    try:
2358 ankur.sing 832
        ds_vendorItemMapping = VendorItemMapping.query.filter(and_(VendorItemMapping.vendor==vendor, VendorItemMapping.item==item, VendorItemMapping.item_key==key)).one()
2116 ankur.sing 833
    except:
834
        ds_vendorItemMapping = VendorItemMapping()
835
        ds_vendorItemMapping.vendor = vendor
836
        ds_vendorItemMapping.item = item
837
        ds_vendorItemMapping.vendor_category = vendorItemMapping.vendorCategory
838
    ds_vendorItemMapping.item_key = vendorItemMapping.itemKey
839
 
840
    session.commit()
841
    return
842
 
2120 ankur.sing 843
def validate_item_prices(item):
2129 ankur.sing 844
    if item.mrp == None or item.sellingPrice == None or item.mrp == "" or item.sellingPrice == "":
845
        return
846
    if item.mrp < item.sellingPrice:
2120 ankur.sing 847
        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))
848
        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 849
    return
2120 ankur.sing 850
 
851
def validate_vendor_prices(item, vendorPrices):
2129 ankur.sing 852
    if item.mrp != None and item.mrp != "" and vendorPrices.mop != "" and item.mrp <  vendorPrices.mop:
2120 ankur.sing 853
        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))
854
        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)))
855
    if vendorPrices.mop != "" and vendorPrices.transferPrice != "" and vendorPrices.transferPrice > vendorPrices.mop:
856
        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))
857
        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)))
858
    return
2065 ankur.sing 859
 
860
def get_all_vendors():
861
    return Vendor.query.all()
862
 
2116 ankur.sing 863
def check_similar_item(product_group, brand, model_number, color):
2129 ankur.sing 864
    query = Item.query
2428 ankur.sing 865
    query = query.filter_by(product_group=product_group)
866
    query = query.filter_by(brand=brand)
867
    query = query.filter_by(model_number=model_number)
2129 ankur.sing 868
    if color:
869
        query = query.filter_by(color=color)
870
    item = query.first()
2116 ankur.sing 871
    if item is None:
872
        return 0
873
    else:
874
        return item.id
2286 ankur.sing 875
 
876
def change_risky_flag(item_id, risky):
877
    item = get_item(item_id)
878
    if not item:
879
        raise InventoryServiceException(101, "Item missing in our database")
880
    try:
881
        log_risky_flag(item_id, risky)
882
    except:
883
        print "Not able to log risky flag change"
884
    item.risky = risky
2368 ankur.sing 885
    if not risky and item.status == status.PAUSED:
886
        change_item_status(item.id, status.ACTIVE)
2286 ankur.sing 887
    session.commit()
2368 ankur.sing 888
    check_risky_item(item)
2286 ankur.sing 889
 
2358 ankur.sing 890
def get_items_by_vendor_category(vendor_category):
891
    if not vendor_category:
892
        raise InventoryServiceException(101, "Invalid vendor category in request")
893
    query = Item.query.filter(and_(Item.hotspotCategory==vendor_category, Item.status != status.PHASED_OUT))
894
    items = query.all()
895
    return items
2116 ankur.sing 896
 
2358 ankur.sing 897
def get_risky_items():
898
    items = Item.query.filter_by(risky=True).all()
899
    return items
900
 
766 rajveer 901
def close_session():
902
    if session.is_active:
903
        print "session is active. closing it."
1399 rajveer 904
        session.close()