Subversion Repositories SmartDukaan

Rev

Rev 621 | Rev 635 | 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,\
609 chandransh 10
    ItemChangeLog, BestSellers
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
 
94 ashish 36
def get_Item(item_id):
37
    return Item.get_by(id=item_id)
38
 
379 ashish 39
def get_item_by_catalog_id(catalog_id):
447 rajveer 40
    query = Item.query.filter_by(catalog_item_id=catalog_id)
437 rajveer 41
    try:
42
        return query.one()
43
    except:
44
        raise InventoryServiceException(109, "Item not found")
45
 
483 rajveer 46
def get_item_by_vendor_id(vendor_id):
47
    query = Item.query.filter_by(vendor_item_id=vendor_id)
494 rajveer 48
    #query = Item.query.filter_by(vendor_item_id='6417182037054')
483 rajveer 49
    try:
50
        return query.one()
51
    except:
52
        raise InventoryServiceException(109, "Item not found")
53
 
576 chandransh 54
def is_active(item_id):
55
    try:
56
        item = get_item_by_catalog_id(item_id)
57
        return item.status == status.ACTIVE
58
    except InventoryServiceException:
59
        return False
563 chandransh 60
 
94 ashish 61
def get_Warehouse(warehouse_id):
62
    return Warehouse.get_by(id=warehouse_id)
63
 
122 ashish 64
def get_all_warehouses_by_status(status):
65
    if not status:
66
        return Warehouse.query.all()
67
    else:
68
        return Warehouse.query.filter_by(status=status)
69
 
94 ashish 70
def get_all_warehouses_for_item(item_id):
71
    item = get_Item(item_id)
72
    if not item:
122 ashish 73
        raise InventoryServiceException(108, "Some unforeseen error while obtaining item")
74
    return item.get_all_warehouses
94 ashish 75
 
122 ashish 76
def get_all_items_for_warehouse(warehouse_id):
77
    warehouse = get_Warehouse(warehouse_id)
78
    if not warehouse:
79
        raise InventoryServiceException(108, "bad warehouse")
80
    return warehouse.all_items
81
 
94 ashish 82
def add_warehouse(warehouse):
103 ashish 83
    if not warehouse:
122 ashish 84
        raise InventoryServiceException(108, "Bad warehouse")
103 ashish 85
    if get_Warehouse(warehouse.id):
86
        #warehouse is already present.
122 ashish 87
        raise InventoryServiceException(101, "Warehouse already present")
103 ashish 88
 
94 ashish 89
    ds_warehouse = Warehouse()
90
    ds_warehouse.id = warehouse.id
91
    ds_warehouse.location = warehouse.location
122 ashish 92
    ds_warehouse.status = status.ACTIVE
103 ashish 93
    ds_warehouse.addedOn = datetime.datetime.now()
483 rajveer 94
    ds_warehouse.lastCheckedOn = datetime.datetime.now()
95
    ds_warehouse.tinNumber = warehouse.tinNumber
96
    ds_warehouse.pincode = warehouse.pincode
97
    if warehouse.vendorString:
98
        ds_warehouse.vendorString = warehouse.vendorString
94 ashish 99
    session.commit()
103 ashish 100
    return ds_warehouse.id
94 ashish 101
 
122 ashish 102
def update_item(item):
103
    if not item:
104
        raise InventoryServiceException(108, "Bad item in request")
105
 
106
    if not item.id:
609 chandransh 107
        raise InventoryServiceException(101, "Missing id for update")
122 ashish 108
 
109
    ds_item = get_Item(item.id)
110
 
111
    if not ds_item:
609 chandransh 112
        raise InventoryServiceException(101, "Item missing in our database")
122 ashish 113
 
511 rajveer 114
    if item.manufacturerName:
115
        ds_item.manufacturer_name = item.manufacturerName
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
129
    if item.vendorItemId:
130
        ds_item.vendor_item_id = item.vendorItemId
131
    else:
609 chandransh 132
        raise InventoryServiceException(101, "Vendor item id cannot be missing")
483 rajveer 133
 
134
    if item.mrp:
135
        ds_item.mrp = item.mrp
136
    if item.mop:
137
        ds_item.mop = item.mop
138
    if item.sellingPrice:
139
        ds_item.sellingPrice = item.sellingPrice
609 chandransh 140
    if item.dealerPrice:
141
        ds_item.dealerPrice = item.dealerPrice
122 ashish 142
    if item.weight:
143
        ds_item.weight = item.weight
511 rajveer 144
    if item.itemStatus:
145
        ds_item.status = item.itemStatus
122 ashish 146
 
511 rajveer 147
    if item.startDate:
148
        ds_item.startDate = item.startDate
149
    if item.retireDate:
150
        ds_item.retireDate = item.retireDate
151
 
122 ashish 152
    if item.featureId:
153
        ds_item.feature_id = item.featureId
154
    if item.featureDescription:
155
        ds_item.feature_description = item.featureDescription
156
 
609 chandransh 157
    if item.bestDealText:
158
        ds_item.bestDealText = item.bestDealText
159
    if item.bestDealValue:
160
        ds_item.bestDealValue = item.bestDealValue
161
 
162
    ds_item.updated_on = datetime.datetime.now()
122 ashish 163
    session.commit();
164
    return ds_item.id
94 ashish 165
 
103 ashish 166
def add_item(item):
167
    if not item:
122 ashish 168
        raise InventoryServiceException(108, "Bad item in request")
103 ashish 169
    if get_Item(item.id):
122 ashish 170
        raise InventoryServiceException(101, "Item already exists")
483 rajveer 171
 
103 ashish 172
    ds_item = Item()
122 ashish 173
    if item.id:
174
        ds_item.id = item.id
515 rajveer 175
    if item.manufacturerName:
176
        ds_item.manufacturer_name = item.manufacturerName
177
    if item.modelName:
178
        ds_item.model_name = item.modelName
179
    if item.modelNumber:
180
        ds_item.model_number = item.modelNumber
609 chandransh 181
    if item.color:
182
        ds_item.color = item.color
483 rajveer 183
    if item.category:
184
        ds_item.category = item.category
185
    if item.comments:
186
        ds_item.comments = item.comments
187
 
122 ashish 188
    if item.catalogItemId:
189
        ds_item.catalog_item_id = item.catalogItemId
190
    if item.vendorItemId:
191
        ds_item.vendor_item_id = item.vendorItemId
192
    else:
193
        raise InventoryServiceException(101, "vendor item id cannot be missing")
194
    if item.itemStatus:
195
        ds_item.status = item.itemStatus
196
    else: #this is for update calls
197
        ds_item.status = status.IN_PROCESS
103 ashish 198
    ds_item.addedOn = datetime.datetime.now()
609 chandransh 199
    ds_item.updatedOn = datetime.datetime.now()
200
 
483 rajveer 201
    if item.mrp:
202
        ds_item.mrp = item.mrp
203
    if item.mop:
204
        ds_item.mop = item.mop
205
    if item.sellingPrice:
206
        ds_item.sellingPrice = item.sellingPrice
609 chandransh 207
    if item.dealerPrice:
208
        ds_item.dealerPrice = item.dealerPrice
122 ashish 209
    if item.weight:
210
        ds_item.weight = item.weight
211
 
212
    if item.featureId:
213
        ds_item.feature_id = item.featureId
214
    if item.featureDescription:
215
        ds_item.feature_description = item.featureDescription
216
 
103 ashish 217
    if item.otherInfo:
218
        for k,v in item.otherInfo.iteritems():
219
            info = ItemInfo()
220
            info.key = k
221
            info.value = v
222
            ds_item.iteminfo.append(info)
223
    #check if categories present. If yes, add them to system
122 ashish 224
 
609 chandransh 225
    if item.bestDealValue:
226
        ds_item.bestDealValue = item.bestDealValue
227
    if item.bestDealText:
228
        ds_item.bestDealText = item.bestDealText
229
 
103 ashish 230
    session.commit();
609 chandransh 231
    return ds_item.id       
103 ashish 232
 
483 rajveer 233
def update_inventory(warehouse_id, timestamp, availability):
234
    warehouse = get_Warehouse(warehouse_id)
235
    if not warehouse:
236
        raise InventoryServiceException(107, "Warehouse? Where?")
237
 
238
    time = datetime.datetime.now()
239
    warehouse.lastCheckedOn = time
240
    warehouse.vendorString = timestamp
241
 
612 chandransh 242
    for item_pk, quantity in availability.iteritems():
483 rajveer 243
        try:
612 chandransh 244
            brand, model, color = item_pk.split(';')
245
            item = Item.query.filter_by(manufacturer_name=brand, model_number=model, color=color).one()
494 rajveer 246
        except:
247
            continue  
248
        try:
483 rajveer 249
            item_inventory_history = ItemInventoryHistory()
250
            item_inventory_history.warehouse = warehouse
251
            item_inventory_history.item = item
252
            item_inventory_history.timestamp = time
253
            item_inventory_history.availibility = quantity
254
 
255
            current_inventory_snapshot = CurrentInventorySnapshot.get_by(item=item, warehouse=warehouse)
256
            if not current_inventory_snapshot:
257
                current_inventory_snapshot = CurrentInventorySnapshot()
258
                current_inventory_snapshot.item = item
259
                current_inventory_snapshot.warehouse = warehouse
260
                current_inventory_snapshot.availibility = 0
261
            # added the difference in the current inventory    
262
            current_inventory_snapshot.availibility = current_inventory_snapshot.availibility + quantity
263
        except:
264
            raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
265
    session.commit() 
266
 
94 ashish 267
def get_item_inventoy(item_id):
268
 
269
    inventory = Item.get_by(id=item_id).currentInventory
270
    if not inventory:
122 ashish 271
        raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
94 ashish 272
    return inventory
273
 
383 ashish 274
def get_item_inventory_by_catalog_id(item_id):
379 ashish 275
    inventory = Item.get_by(catalog_item_id=item_id).currentInventory
276
    if not inventory:
277
        raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
278
    return inventory
279
 
103 ashish 280
 
281
def retire_warehouse(warehouse_id):
282
    if not warehouse_id:
122 ashish 283
        raise InventoryServiceException(101, "Bad warehouse id")
103 ashish 284
    warehouse = get_Warehouse(warehouse_id)
285
    if not warehouse:
122 ashish 286
        raise InventoryServiceException(108, "warehouse id not present")
287
    warehouse.status = status.DELETED;
103 ashish 288
    session.commit()
289
 
290
def retire_item(item_id):
291
    if not item_id:
122 ashish 292
        raise InventoryServiceException(101, "bad item id")
103 ashish 293
    item = get_Item(item_id)
294
    if not item:
122 ashish 295
        raise InventoryServiceException(108, "item id not present")
296
    item.status = status.PHASED_OUT
297
    item.retireDate = datetime.datetime.now()
103 ashish 298
    session.commit()
122 ashish 299
 
300
#need to implement threads based solution here
103 ashish 301
def start_item_on(item_id, timestamp):
302
    if not item_id:
122 ashish 303
        raise InventoryServiceException(101, "bad item id")
103 ashish 304
    item = get_Item(item_id)
305
    if not item:
122 ashish 306
        raise InventoryServiceException(108, "item id not present")
103 ashish 307
 
122 ashish 308
    item.status = status.ACTIVE
309
    item.startDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
310
    add_status_change_log(item, status.ACTIVE)
103 ashish 311
    session.commit()
312
 
122 ashish 313
#need to implement threads here
103 ashish 314
def retire_item_on(item_id, timestamp):
315
    if not item_id:
122 ashish 316
        raise InventoryServiceException(101, "bad item id")
103 ashish 317
    item = get_Item(item_id)
318
    if not item:
122 ashish 319
        raise InventoryServiceException(108, "item id not present")
103 ashish 320
 
122 ashish 321
    item.status = status.PHASED_OUT
322
    item.retireDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
323
    add_status_change_log(item, status.PHASED_OUT)
103 ashish 324
    session.commit()
325
 
326
def add_status_change_log(item, new_status):
327
    item_change_log = ItemChangeLog()
328
    item_change_log.new_status = new_status
329
    item_change_log.old_status = item.status
330
    item_change_log.timestamp = datetime.datetime.now()
331
    item_change_log.item = item
332
    session.commit()
333
 
334
def change_item_status(item_id, new_status):
335
    if not item_id:
122 ashish 336
        raise InventoryServiceException(101, "bad item id")
103 ashish 337
    item = get_Item(item_id)
338
    if not item:
122 ashish 339
        raise InventoryServiceException(108, "item id not present")
103 ashish 340
 
122 ashish 341
    item.status = new_status
342
    add_status_change_log(item, new_status)
103 ashish 343
    session.commit()
344
 
345
def get_item_availability_for_warehouse(warehouse_id, item_id):
346
    if not warehouse_id:
122 ashish 347
        raise InventoryServiceException(101, "bad warehouse_id")
103 ashish 348
    if not item_id:
122 ashish 349
        raise InventoryServiceException(101, "bad item_id")
103 ashish 350
 
351
    warehouse = get_Warehouse(warehouse_id)
352
    if not warehouse:
122 ashish 353
        raise InventoryServiceException(108, "warehouse does not exist")
504 rajveer 354
    item = get_item_by_catalog_id(item_id)
355
 
103 ashish 356
    if not item:
122 ashish 357
            raise InventoryServiceException(108, "item does not exist")
358
 
494 rajveer 359
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id)
504 rajveer 360
    query = query.filter_by(item_id = item.id)
494 rajveer 361
    try:
362
        current_inventory_snapshot = query.one()
363
        return current_inventory_snapshot.availibility
364
    except:
365
        return 0
366
    """
367
    current_inventory_snapshot = CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse_id == warehouse_id, CurrentInventorySnapshot.item_id == item_id).one()
103 ashish 368
    if not current_inventory_snapshot:
369
        return 0
370
    else:
371
        return current_inventory_snapshot.availibility
494 rajveer 372
    """
122 ashish 373
def get_warehouses_for_item(item_id):
374
    if not item_id:
375
        raise InventoryServiceException(101, "bad item_id")
376
    item = get_Item(item_id)
377
 
378
    if not item:
379
        raise InventoryServiceException(101, "bad item")
380
 
483 rajveer 381
    warehouses = item.currentInventory.warehouse
501 rajveer 382
    return warehouses
383
 
626 chandransh 384
def get_best_sellers(start_index, stop_index, category=-1):
621 chandransh 385
    query = get_best_sellers_query(category)
386
    best_sellers = query.order_by(asc(Item.bestSellingRank)).all()[start_index:stop_index]
387
    return get_thrift_item_list(best_sellers)
388
 
389
def get_best_sellers_count(category):
390
    query = get_best_sellers_query(category)
391
    return query.count()
392
 
626 chandransh 393
def get_best_sellers_catalog_ids(start_index, stop_index, category=-1):
621 chandransh 394
    query = get_best_sellers_query(category)
395
    best_sellers = query.order_by(asc(Item.bestSellingRank)).all()[start_index:stop_index]
396
    return [item.catalog_item_id for item in best_sellers]
501 rajveer 397
 
621 chandransh 398
def get_best_sellers_query(category):
399
    query = Item.query.filter(Item.bestSellingRank != None)
626 chandransh 400
    if category != -1:
621 chandransh 401
        query = query.filter_by(category=category)
402
    return query
609 chandransh 403
 
501 rajveer 404
def get_best_deals():
621 chandransh 405
    items = Item.query.filter(Item.bestDealValue != None).order_by(desc(Item.bestDealValue)).all()
609 chandransh 406
    return get_thrift_item_list(items)
407
 
408
def get_best_deals_count():
409
    return Item.query.filter(Item.bestDealValue != None).count()
501 rajveer 410
 
626 chandransh 411
def get_best_deals_catalog_ids(start_index, stop_index, category=-1):
621 chandransh 412
    query = Item.query.filter(Item.bestDealValue != None)
626 chandransh 413
    if category != -1:
621 chandransh 414
        query = query.filter_by(category=category)
415
    best_deal_items = query.order_by(desc(Item.bestDealValue)).all()[start_index:stop_index]
416
    return [item.catalog_item_id for item in best_deal_items]
609 chandransh 417
 
599 chandransh 418
def get_latest_arrivals(limit):
419
    items = Item.query.order_by(desc(Item.startDate)).all()[0:limit]
609 chandransh 420
    return get_thrift_item_list(items)
598 chandransh 421
 
602 chandransh 422
def get_latest_arrivals_count(limit):
609 chandransh 423
    #FIXME: This method has not been functionally tested. Have to talk to Rajveer to check where is it used.
602 chandransh 424
    return min(Item.query.count(), limit)
425
 
626 chandransh 426
def get_latest_arrivals_catalog_ids(start_index, stop_index, category=-1):
621 chandransh 427
    query = Item.query
626 chandransh 428
    if category != -1:
621 chandransh 429
        query = query.filter_by(category=category)
430
    latest_arrivals = query.order_by(desc(Item.startDate)).all()[start_index:stop_index]
431
    return [item.catalog_item_id for item in latest_arrivals] 
609 chandransh 432
 
433
def get_thrift_item_list(items):
434
    ret_items = []
435
    for item in items:
436
        if item:
437
            ret_items.append(to_t_item(item))    
438
    return ret_items