Subversion Repositories SmartDukaan

Rev

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