Subversion Repositories SmartDukaan

Rev

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