Subversion Repositories SmartDukaan

Rev

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