Subversion Repositories SmartDukaan

Rev

Rev 94 | Rev 122 | 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
8
from shop2020.model.v1.catalog.impl.DataService import Category, Item, Vendor,\
103 ashish 9
    Warehouse, ItemInventoryHistory, CurrentInventorySnapshot, ItemInfo,\
10
    ItemChangeLog
94 ashish 11
from shop2020.thriftpy.model.v1.catalog.ttypes import Category as TCategory,\
12
    CatalogServiceException
13
from shop2020.thriftpy.model.v1.catalog.ttypes import Item as TItem
14
from shop2020.thriftpy.model.v1.catalog.ttypes import Vendor as TVendor
15
from shop2020.thriftpy.model.v1.catalog.ttypes import Warehouse as TWarehouse
16
import datetime
103 ashish 17
from shop2020.utils.Utils import status_active, status_added, status_retired
94 ashish 18
 
19
 
20
def initialize():
21
    DataService.initialize()
22
 
23
def is_category_present(id_key):
24
    category = Category.get_by(id=id_key)
25
    if category:
26
        return True    
27
    return False
28
 
29
def get_category(id_key):
30
    category = Category.get_by(id=id_key)
31
    return category
32
 
33
def get_all_categories():
34
    return Category.query.all()
35
 
36
def get_all_items_for_category(category_id):
37
    category = Category.get_by(id=category_id)
38
    if not category:
39
        raise CatalogServiceException(108, "Some unforeseen error while obtaining")
40
    return category.items
41
 
103 ashish 42
def get_all_items(is_active):
43
    if is_active:
44
        return Item.get_by(status=status_active)
45
    else:
46
        return Item.query.all()
47
 
48
def get_all_items_by_status(status):
49
    if not status:
50
        #return all items
51
        return get_all_items(False)
52
    else:
53
        return Item.get_by(status=status).all()
54
 
94 ashish 55
def get_Item(item_id):
56
    return Item.get_by(id=item_id)
57
 
103 ashish 58
def get_all_vendors():
59
    return Vendor.query.all()
60
 
61
def get_all_active_vendors():
62
    return Vendor.get_by(status=status_active).all()
63
 
94 ashish 64
def get_Vendor(vendor_id):
65
    return Vendor.get_by(id=vendor_id)
66
 
67
def get_Warehouse(warehouse_id):
68
    return Warehouse.get_by(id=warehouse_id)
69
 
70
def get_all_warehouses_for_item(item_id):
71
    item = get_Item(item_id)
72
    if not item:
73
        raise CatalogServiceException(108, "Some unforeseen error while obtaining item")
103 ashish 74
    return item.get_all_warehouses()
94 ashish 75
 
76
def add_warehouse(warehouse):
103 ashish 77
    if not warehouse:
78
        raise CatalogServiceException(108, "Bad warehouse")
79
    if get_Warehouse(warehouse.id):
80
        #warehouse is already present.
81
        raise CatalogServiceException(101, "Warehouse already present")
82
 
94 ashish 83
    ds_warehouse = Warehouse()
84
    ds_warehouse.id = warehouse.id
85
    ds_warehouse.location = warehouse.location
103 ashish 86
    ds_warehouse.status = status_active
87
    ds_warehouse.addedOn = datetime.datetime.now()
94 ashish 88
    session.commit()
103 ashish 89
    return ds_warehouse.id
94 ashish 90
 
91
def add_vendor(vendor):
103 ashish 92
    if get_Vendor(vendor.id):
93
        raise CatalogServiceException(101, "Vendor already present")
94 ashish 94
    ds_vendor = Vendor()
95
    if not vendor:
96
        raise CatalogServiceException(108, "Bad vendor in request")
97
    ds_vendor.id = vendor.id
98
    ds_vendor.name = vendor.name
99
    session.commit()
103 ashish 100
    return ds_vendor.id
94 ashish 101
 
103 ashish 102
def add_item(item):
103
    if not item:
104
        raise CatalogServiceException(108, "Bad item in request")
105
    if get_Item(item.id):
106
        raise CatalogServiceException(101, "Item already exists")
107
 
108
    ds_item = Item()
109
    ds_item.id = item.id
110
    ds_item.status = status_added
111
    ds_item.addedOn = datetime.datetime.now()
112
    ds_item.price = item.price
113
    if item.otherInfo:
114
        for k,v in item.otherInfo.iteritems():
115
            info = ItemInfo()
116
            info.key = k
117
            info.value = v
118
            ds_item.iteminfo.append(info)
119
    #check if categories present. If yes, add them to system
120
    if item.categories:
121
        for category_id in item.categories:
122
            category = get_category(category_id)
123
            if category:
124
                ds_item.category.append(category)
125
            else:
126
                raise CatalogServiceException(108, "Category not found ".category_id)
127
    session.commit();
128
    return ds_item.id
129
 
94 ashish 130
def add_category(category):
131
    try: 
132
        ds_category = Category()
133
 
134
        ds_category.id = category.id
135
        ds_category.name = category.name
136
        ds_category.addedOn = datetime.datetime.now()
137
        ds_category.isRoot = category.isTopLevel
138
        if category.parent_id:
139
            ds_category.parent= Category.get_by(id=category.parent_id)
140
 
141
        if category.childCategories:
142
            for child in category.childCategories:
143
                ds_category.children.append(get_category(child))
103 ashish 144
        ds_category.status = status_active
94 ashish 145
        #save the category now
146
        session.commit()
103 ashish 147
        return ds_category.id
94 ashish 148
    except:
103 ashish 149
        return 0
94 ashish 150
 
151
def add_item_to_category(id, item):
152
    category = get_category(id)
153
    if not category:
154
        raise CatalogServiceException(101, "no such category exists")
155
    if not item:
156
        raise CatalogServiceException(104, "bad item")
157
    try:
103 ashish 158
        item_id = add_item(item)
159
        added_item = get_Item(item_id)
160
        added_item.categories.append(category)
94 ashish 161
        session.commit()
103 ashish 162
        return item_id
94 ashish 163
    except:
103 ashish 164
        raise CatalogServiceException(101, "intenral error")
94 ashish 165
 
103 ashish 166
def add_item_to_category_by_id(category_id, item_id):
167
    category = get_category(category_id)
168
    item = get_Item(item_id)
169
    if not category:
170
        raise CatalogServiceException(101, "no such category exists")
171
    if not item:
172
        raise CatalogServiceException(104, "bad item")
173
    item.categories.append(category)
174
    session.commit()
175
 
94 ashish 176
def update_inventory(item_id, warehouse_id, quantity, timestamp):
177
    item = get_Item(item_id)
178
    warehouse = get_Warehouse(warehouse_id) 
179
    time = datetime.datetime.fromtimestamp(timestamp)
180
 
181
    if not item:
182
        raise CatalogServiceException(106, "Item not found, how did you get this?")
183
    if not warehouse:
184
        raise CatalogServiceException(107, "Warehouse? Where?")
185
    try:
186
        item_inventory_history = ItemInventoryHistory()
187
        item_inventory_history.warehouse = warehouse
188
        item_inventory_history.item = item
189
        item_inventory_history.timestamp = time
190
        item_inventory_history.availability = quantity
191
 
192
        current_inventory_snapshot = CurrentInventorySnapshot.get_by(item=item, warehouse=warehouse)
193
        current_inventory_snapshot.availibility = quantity
194
        current_inventory_snapshot.checkedOn = time
195
        session.commit()
196
    except:
197
        raise CatalogServiceException(108, "Some unforeseen error while updating inventory")
198
 
199
def get_item_inventoy(item_id):
200
 
201
    inventory = Item.get_by(id=item_id).currentInventory
202
    if not inventory:
203
        raise CatalogServiceException(108, "Some unforeseen error while updating inventory")
204
    return inventory
205
 
103 ashish 206
def retire_category(category_id):
207
 
208
    if not category_id:
209
        raise CatalogServiceException(101, "Bad category id")
210
    category = get_category(category_id)
211
 
212
    if not category:
213
        raise CatalogServiceException(108, "Category not present")
214
 
215
    category.status = status_retired
216
 
217
    session.commit()
218
 
219
def retire_warehouse(warehouse_id):
220
    if not warehouse_id:
221
        raise CatalogServiceException(101, "Bad warehouse id")
222
    warehouse = get_Warehouse(warehouse_id)
223
    if not warehouse:
224
        raise CatalogServiceException(108, "warehouse id not present")
225
    warehouse.status = status_retired;
226
    session.commit()
227
 
228
def retire_vendor(vendor_id):
229
    if not vendor_id:
230
        raise CatalogServiceException(101, "bad vendor id")
231
    vendor = get_Vendor(vendor_id)
232
 
233
    if not vendor:
234
        raise CatalogServiceException(108, "vendor id not present")
235
 
236
    vendor.status = status_retired
237
    session.commit()
238
 
239
def retire_item(item_id):
240
    if not item_id:
241
        raise CatalogServiceException(101, "bad item id")
242
    item = get_Item(item_id)
243
    if not item:
244
        raise CatalogServiceException(108, "item id not present")
245
    item.status = status_retired
246
    session.commit()
94 ashish 247
 
103 ashish 248
def start_item_on(item_id, timestamp):
249
    if not item_id:
250
        raise CatalogServiceException(101, "bad item id")
251
    item = get_Item(item_id)
252
    if not item:
253
        raise CatalogServiceException(108, "item id not present")
254
 
255
    item.status = status_active
256
    item.startDate = datetime.datetime.fromtimestamp(timestamp)
257
    add_status_change_log(item, status_active)
258
    session.commit()
259
 
260
def retire_item_on(item_id, timestamp):
261
    if not item_id:
262
        raise CatalogServiceException(101, "bad item id")
263
    item = get_Item(item_id)
264
    if not item:
265
        raise CatalogServiceException(108, "item id not present")
266
 
267
    item.status = status_retired
268
    item.retireDate = datetime.datetime.fromtimestamp(timestamp)
269
    add_status_change_log(item, status_retired)
270
    session.commit()
271
 
272
def add_status_change_log(item, new_status):
273
    item_change_log = ItemChangeLog()
274
    item_change_log.new_status = new_status
275
    item_change_log.old_status = item.status
276
    item_change_log.timestamp = datetime.datetime.now()
277
    item_change_log.item = item
278
    session.commit()
279
 
280
def change_item_status(item_id, new_status):
281
    if not item_id:
282
        raise CatalogServiceException(101, "bad item id")
283
    item = get_Item(item_id)
284
    if not item:
285
        raise CatalogServiceException(108, "item id not present")
286
 
287
    item.status = status_retired
288
    add_status_change_log(item, status_retired)
289
    session.commit()
290
 
291
def get_item_availability_for_warehouse(warehouse_id, item_id):
292
    if not warehouse_id:
293
        raise CatalogServiceException(101, "bad warehouse_id")
294
    if not item_id:
295
        raise CatalogServiceException(101, "bad item_id")
296
 
297
    warehouse = get_Warehouse(warehouse_id)
298
    if not warehouse:
299
        raise CatalogServiceException(108, "warehouse does not exist")
300
    item = get_Item(item_id)
301
 
302
    if not item:
303
            raise CatalogServiceException(108, "item does not exist")
304
    current_inventory_snapshot = CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse.id == warehouse_id, CurrentInventorySnapshot.item.id == item_id).one()
305
    if not current_inventory_snapshot:
306
        return 0
307
    else:
308
        return current_inventory_snapshot.availibility
309