Subversion Repositories SmartDukaan

Rev

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