Subversion Repositories SmartDukaan

Rev

Rev 501 | Rev 511 | 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,\
501 rajveer 10
    ItemChangeLog, Price, LatestArrivals, BestDeals, BestSellers
122 ashish 11
from shop2020.thriftpy.model.v1.catalog.ttypes import \
12
    InventoryServiceException, status
94 ashish 13
from shop2020.thriftpy.model.v1.catalog.ttypes import Item as TItem
122 ashish 14
 
94 ashish 15
from shop2020.thriftpy.model.v1.catalog.ttypes import Warehouse as TWarehouse
16
import datetime
122 ashish 17
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
501 rajveer 18
from sqlalchemy import desc, asc
94 ashish 19
 
20
 
21
def initialize():
22
    DataService.initialize()
23
 
24
 
103 ashish 25
def get_all_items(is_active):
26
    if is_active:
122 ashish 27
        return Item.query.filter_by(status= status.ACTIVE).all()
103 ashish 28
    else:
29
        return Item.query.all()
30
 
31
def get_all_items_by_status(status):
32
    if not status:
33
        #return all items
34
        return get_all_items(False)
35
    else:
122 ashish 36
        return Item.query.filter_by(status=status).all()
103 ashish 37
 
94 ashish 38
def get_Item(item_id):
39
    return Item.get_by(id=item_id)
40
 
379 ashish 41
def get_item_by_catalog_id(catalog_id):
447 rajveer 42
    query = Item.query.filter_by(catalog_item_id=catalog_id)
437 rajveer 43
    try:
44
        return query.one()
45
    except:
46
        raise InventoryServiceException(109, "Item not found")
47
 
483 rajveer 48
def get_item_by_vendor_id(vendor_id):
49
    query = Item.query.filter_by(vendor_item_id=vendor_id)
494 rajveer 50
    #query = Item.query.filter_by(vendor_item_id='6417182037054')
483 rajveer 51
    try:
52
        return query.one()
53
    except:
54
        raise InventoryServiceException(109, "Item not found")
55
 
94 ashish 56
def get_Warehouse(warehouse_id):
57
    return Warehouse.get_by(id=warehouse_id)
58
 
122 ashish 59
def get_all_warehouses_by_status(status):
60
    if not status:
61
        return Warehouse.query.all()
62
    else:
63
        return Warehouse.query.filter_by(status=status)
64
 
94 ashish 65
def get_all_warehouses_for_item(item_id):
66
    item = get_Item(item_id)
67
    if not item:
122 ashish 68
        raise InventoryServiceException(108, "Some unforeseen error while obtaining item")
69
    return item.get_all_warehouses
94 ashish 70
 
122 ashish 71
def get_all_items_for_warehouse(warehouse_id):
72
    warehouse = get_Warehouse(warehouse_id)
73
    if not warehouse:
74
        raise InventoryServiceException(108, "bad warehouse")
75
    return warehouse.all_items
76
 
94 ashish 77
def add_warehouse(warehouse):
103 ashish 78
    if not warehouse:
122 ashish 79
        raise InventoryServiceException(108, "Bad warehouse")
103 ashish 80
    if get_Warehouse(warehouse.id):
81
        #warehouse is already present.
122 ashish 82
        raise InventoryServiceException(101, "Warehouse already present")
103 ashish 83
 
94 ashish 84
    ds_warehouse = Warehouse()
85
    ds_warehouse.id = warehouse.id
86
    ds_warehouse.location = warehouse.location
122 ashish 87
    ds_warehouse.status = status.ACTIVE
103 ashish 88
    ds_warehouse.addedOn = datetime.datetime.now()
483 rajveer 89
    ds_warehouse.lastCheckedOn = datetime.datetime.now()
90
    ds_warehouse.tinNumber = warehouse.tinNumber
91
    ds_warehouse.pincode = warehouse.pincode
92
    if warehouse.vendorString:
93
        ds_warehouse.vendorString = warehouse.vendorString
94 ashish 94
    session.commit()
103 ashish 95
    return ds_warehouse.id
94 ashish 96
 
122 ashish 97
def update_item(item):
98
    if not item:
99
        raise InventoryServiceException(108, "Bad item in request")
100
 
101
    if not item.id:
102
        raise InventoryServiceException(101, "non existent item")
103
 
104
    ds_item = get_Item(item.id)
105
 
106
    if not ds_item:
107
        raise InventoryServiceException(101, "Item already exists")
108
 
483 rajveer 109
    if item.manufacturer_name:
110
        ds_item.manufacturer_name = item.manufacturer_name
111
    if item.model_name:
112
        ds_item.model_name = item.model_name
113
    if item.model_number:
114
        ds_item.model_number = item.model_number
115
    if item.category:
116
        ds_item.category = item.category
117
    if item.comments:
118
        ds_item.comments = item.comments
119
 
122 ashish 120
    if item.catalogItemId:
121
        ds_item.catalog_item_id = item.catalogItemId
122
    if item.vendorItemId:
123
        ds_item.vendor_item_id = item.vendorItemId
124
    else:
125
        raise InventoryServiceException(101, "vendor item id cannot be missing")
483 rajveer 126
 
127
    if item.mrp:
128
        ds_item.mrp = item.mrp
129
    if item.mop:
130
        ds_item.mop = item.mop
131
    if item.sellingPrice:
132
        ds_item.sellingPrice = item.sellingPrice
122 ashish 133
    if item.weight:
134
        ds_item.weight = item.weight
483 rajveer 135
    if item.weight:
136
        ds_item.weight = item.weight
122 ashish 137
 
138
    if item.featureId:
139
        ds_item.feature_id = item.featureId
140
    if item.featureDescription:
141
        ds_item.feature_description = item.featureDescription
142
 
143
    session.commit();
144
    return ds_item.id
94 ashish 145
 
103 ashish 146
def add_item(item):
147
    if not item:
122 ashish 148
        raise InventoryServiceException(108, "Bad item in request")
103 ashish 149
    if get_Item(item.id):
122 ashish 150
        raise InventoryServiceException(101, "Item already exists")
483 rajveer 151
 
103 ashish 152
    ds_item = Item()
122 ashish 153
    if item.id:
154
        ds_item.id = item.id
483 rajveer 155
    if item.manufacturer_name:
156
        ds_item.manufacturer_name = item.manufacturer_name
157
    if item.model_name:
158
        ds_item.model_name = item.model_name
159
    if item.model_number:
160
        ds_item.model_number = item.model_number
161
    if item.category:
162
        ds_item.category = item.category
163
    if item.comments:
164
        ds_item.comments = item.comments
165
 
122 ashish 166
    if item.catalogItemId:
167
        ds_item.catalog_item_id = item.catalogItemId
168
    if item.vendorItemId:
169
        ds_item.vendor_item_id = item.vendorItemId
170
    else:
171
        raise InventoryServiceException(101, "vendor item id cannot be missing")
172
    if item.itemStatus:
173
        ds_item.status = item.itemStatus
174
    else: #this is for update calls
175
        ds_item.status = status.IN_PROCESS
103 ashish 176
    ds_item.addedOn = datetime.datetime.now()
483 rajveer 177
    if item.mrp:
178
        ds_item.mrp = item.mrp
179
    if item.mop:
180
        ds_item.mop = item.mop
181
    if item.sellingPrice:
182
        ds_item.sellingPrice = item.sellingPrice
122 ashish 183
    if item.weight:
184
        ds_item.weight = item.weight
185
 
186
    if item.featureId:
187
        ds_item.feature_id = item.featureId
188
    if item.featureDescription:
189
        ds_item.feature_description = item.featureDescription
190
 
103 ashish 191
    if item.otherInfo:
192
        for k,v in item.otherInfo.iteritems():
193
            info = ItemInfo()
194
            info.key = k
195
            info.value = v
196
            ds_item.iteminfo.append(info)
197
    #check if categories present. If yes, add them to system
122 ashish 198
 
103 ashish 199
    session.commit();
200
    return ds_item.id
201
 
483 rajveer 202
#this is no more used
122 ashish 203
def update_price(item, ds_item):
204
    log_entry("update_price", "updating price for item")
205
    for k,v in item.price.iteritems():
206
        price = Price()
207
        price.warehouse_id = k
208
        price.price = v
386 ashish 209
        price.item = ds_item
210
        session.commit() 
122 ashish 211
 
483 rajveer 212
#this is no more used
122 ashish 213
def update_price_for_warehouse(item_id, warehouse_id, price):
214
    log_entry("update_price", "updating price for item and warehouse")
215
    price = Price.query.filter_by(warehouse_id=warehouse_id, item_id=item_id).one()
216
    if not price:
217
        price = Price()
218
        item = get_Item(item_id)
219
        price.warehouse_id = warehouse_id
220
        price.item = item
221
    price.price = price    
222
    session.commit()
94 ashish 223
 
224
 
483 rajveer 225
def update_inventory(warehouse_id, timestamp, availability):
226
    warehouse = get_Warehouse(warehouse_id)
227
    if not warehouse:
228
        raise InventoryServiceException(107, "Warehouse? Where?")
229
 
230
    time = datetime.datetime.now()
231
    warehouse.lastCheckedOn = time
232
    warehouse.vendorString = timestamp
233
 
234
    for k,v in availability.iteritems():
235
        vendor_id = k
236
        quantity = v
237
        try:
494 rajveer 238
            item = Item.query.filter_by(vendor_item_id=vendor_id).one()
239
        except:
240
            continue  
241
        try:
483 rajveer 242
            item_inventory_history = ItemInventoryHistory()
243
            item_inventory_history.warehouse = warehouse
244
            item_inventory_history.item = item
245
            item_inventory_history.timestamp = time
246
            item_inventory_history.availibility = quantity
247
 
248
            current_inventory_snapshot = CurrentInventorySnapshot.get_by(item=item, warehouse=warehouse)
249
            if not current_inventory_snapshot:
250
                current_inventory_snapshot = CurrentInventorySnapshot()
251
                current_inventory_snapshot.item = item
252
                current_inventory_snapshot.warehouse = warehouse
253
                current_inventory_snapshot.availibility = 0
254
            # added the difference in the current inventory    
255
            current_inventory_snapshot.availibility = current_inventory_snapshot.availibility + quantity
256
        except:
257
            raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
258
    session.commit() 
259
 
260
 
261
 
262
#this is no more used
263
def update_inventory_old(item_id, warehouse_id, quantity, timestamp):
94 ashish 264
    item = get_Item(item_id)
265
    warehouse = get_Warehouse(warehouse_id) 
122 ashish 266
    time = to_py_date(timestamp)
94 ashish 267
 
268
    if not item:
122 ashish 269
        raise InventoryServiceException(106, "Item not found, how did you get this?")
94 ashish 270
    if not warehouse:
122 ashish 271
        raise InventoryServiceException(107, "Warehouse? Where?")
94 ashish 272
    try:
273
        item_inventory_history = ItemInventoryHistory()
274
        item_inventory_history.warehouse = warehouse
275
        item_inventory_history.item = item
276
        item_inventory_history.timestamp = time
122 ashish 277
        item_inventory_history.availibility = quantity
94 ashish 278
 
279
        current_inventory_snapshot = CurrentInventorySnapshot.get_by(item=item, warehouse=warehouse)
122 ashish 280
        if not current_inventory_snapshot:
281
            current_inventory_snapshot = CurrentInventorySnapshot()
282
            current_inventory_snapshot.item = item
283
            current_inventory_snapshot.warehouse = warehouse
284
 
94 ashish 285
        current_inventory_snapshot.availibility = quantity
286
        current_inventory_snapshot.checkedOn = time
287
        session.commit()
288
    except:
122 ashish 289
        raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
94 ashish 290
 
291
def get_item_inventoy(item_id):
292
 
293
    inventory = Item.get_by(id=item_id).currentInventory
294
    if not inventory:
122 ashish 295
        raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
94 ashish 296
    return inventory
297
 
383 ashish 298
def get_item_inventory_by_catalog_id(item_id):
379 ashish 299
    inventory = Item.get_by(catalog_item_id=item_id).currentInventory
300
    if not inventory:
301
        raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
302
    return inventory
303
 
103 ashish 304
 
305
def retire_warehouse(warehouse_id):
306
    if not warehouse_id:
122 ashish 307
        raise InventoryServiceException(101, "Bad warehouse id")
103 ashish 308
    warehouse = get_Warehouse(warehouse_id)
309
    if not warehouse:
122 ashish 310
        raise InventoryServiceException(108, "warehouse id not present")
311
    warehouse.status = status.DELETED;
103 ashish 312
    session.commit()
313
 
314
def retire_item(item_id):
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")
320
    item.status = status.PHASED_OUT
321
    item.retireDate = datetime.datetime.now()
103 ashish 322
    session.commit()
122 ashish 323
 
324
#need to implement threads based solution here
103 ashish 325
def start_item_on(item_id, timestamp):
326
    if not item_id:
122 ashish 327
        raise InventoryServiceException(101, "bad item id")
103 ashish 328
    item = get_Item(item_id)
329
    if not item:
122 ashish 330
        raise InventoryServiceException(108, "item id not present")
103 ashish 331
 
122 ashish 332
    item.status = status.ACTIVE
333
    item.startDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
334
    add_status_change_log(item, status.ACTIVE)
103 ashish 335
    session.commit()
336
 
122 ashish 337
#need to implement threads here
103 ashish 338
def retire_item_on(item_id, timestamp):
339
    if not item_id:
122 ashish 340
        raise InventoryServiceException(101, "bad item id")
103 ashish 341
    item = get_Item(item_id)
342
    if not item:
122 ashish 343
        raise InventoryServiceException(108, "item id not present")
103 ashish 344
 
122 ashish 345
    item.status = status.PHASED_OUT
346
    item.retireDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
347
    add_status_change_log(item, status.PHASED_OUT)
103 ashish 348
    session.commit()
349
 
350
def add_status_change_log(item, new_status):
351
    item_change_log = ItemChangeLog()
352
    item_change_log.new_status = new_status
353
    item_change_log.old_status = item.status
354
    item_change_log.timestamp = datetime.datetime.now()
355
    item_change_log.item = item
356
    session.commit()
357
 
358
def change_item_status(item_id, new_status):
359
    if not item_id:
122 ashish 360
        raise InventoryServiceException(101, "bad item id")
103 ashish 361
    item = get_Item(item_id)
362
    if not item:
122 ashish 363
        raise InventoryServiceException(108, "item id not present")
103 ashish 364
 
122 ashish 365
    item.status = new_status
366
    add_status_change_log(item, new_status)
103 ashish 367
    session.commit()
368
 
369
def get_item_availability_for_warehouse(warehouse_id, item_id):
370
    if not warehouse_id:
122 ashish 371
        raise InventoryServiceException(101, "bad warehouse_id")
103 ashish 372
    if not item_id:
122 ashish 373
        raise InventoryServiceException(101, "bad item_id")
103 ashish 374
 
375
    warehouse = get_Warehouse(warehouse_id)
376
    if not warehouse:
122 ashish 377
        raise InventoryServiceException(108, "warehouse does not exist")
504 rajveer 378
    item = get_item_by_catalog_id(item_id)
379
 
103 ashish 380
    if not item:
122 ashish 381
            raise InventoryServiceException(108, "item does not exist")
382
 
494 rajveer 383
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id)
504 rajveer 384
    query = query.filter_by(item_id = item.id)
494 rajveer 385
    try:
386
        current_inventory_snapshot = query.one()
387
        return current_inventory_snapshot.availibility
388
    except:
389
        return 0
390
    """
391
    current_inventory_snapshot = CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse_id == warehouse_id, CurrentInventorySnapshot.item_id == item_id).one()
103 ashish 392
    if not current_inventory_snapshot:
393
        return 0
394
    else:
395
        return current_inventory_snapshot.availibility
494 rajveer 396
    """
122 ashish 397
def get_warehouses_for_item(item_id):
398
    if not item_id:
399
        raise InventoryServiceException(101, "bad item_id")
400
    item = get_Item(item_id)
401
 
402
    if not item:
403
        raise InventoryServiceException(101, "bad item")
404
 
483 rajveer 405
    warehouses = item.currentInventory.warehouse
501 rajveer 406
    return warehouses
407
 
408
def get_best_sellers():
409
    sellers = BestSellers.query.all()
410
 
411
    t_sellers = []
412
    for seller in sellers:
413
        t_sellers.append(seller.item_id)
414
    return t_sellers   
415
 
416
def get_best_deals():
417
    deals = BestDeals.query.all()
418
 
419
    #query = BestDeals.query.all()
420
    #deals = query.order_by(asc(BestDeals.priority)).all()
421
 
422
 
423
    t_deals = []
424
    for deal in deals:
425
        t_deals.append(deal.item_id)
426
    return t_deals   
427
 
428
def get_latest_arrivals():
429
    arrivals = LatestArrivals.query.all()
430
 
431
    t_arrivals = []
432
    for arrival in arrivals:
433
        t_arrivals.append(arrival.item_id)
434
    return t_arrivals