| 94 |
ashish |
1 |
'''
|
|
|
2 |
Created on 22-Mar-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
|
|
6 |
from elixir import *
|
|
|
7 |
from elixir.entity import Entity
|
|
|
8 |
from elixir.fields import Field
|
|
|
9 |
from sqlalchemy.types import Integer
|
|
|
10 |
from elixir.relationships import OneToMany, ManyToOne
|
|
|
11 |
import datetime
|
|
|
12 |
|
|
|
13 |
|
| 103 |
ashish |
14 |
|
| 94 |
ashish |
15 |
class Category(Entity):
|
|
|
16 |
'''
|
|
|
17 |
This class represents the catalog.
|
|
|
18 |
'''
|
| 100 |
ashish |
19 |
id = Field(Integer, primary_key=True)
|
| 94 |
ashish |
20 |
name = Field(String(30))
|
|
|
21 |
isRoot = Field(Integer)
|
|
|
22 |
addedOn = Field(DateTime)
|
| 103 |
ashish |
23 |
status = Field(Integer)
|
|
|
24 |
items = ManyToMany('Item')
|
| 94 |
ashish |
25 |
children = OneToMany('Category', inverse='parent')
|
|
|
26 |
parent = ManyToOne('Category', inverse='children')
|
| 103 |
ashish |
27 |
|
| 94 |
ashish |
28 |
|
|
|
29 |
def __repr__(self):
|
|
|
30 |
return "<id>%id </id>"%(self.id)
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
class Vendor(Entity):
|
| 100 |
ashish |
34 |
id = Field(Integer, primary_key=True)
|
| 94 |
ashish |
35 |
name = Field(String(30))
|
| 103 |
ashish |
36 |
status = Field(Integer)
|
|
|
37 |
addedOn = Field(datetime)
|
| 94 |
ashish |
38 |
warehouses = ManyToMany('Warehouse')
|
|
|
39 |
def __repr__(self):
|
|
|
40 |
return "<Vendor>%s</Vendor>" %(self.name)
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
class Warehouse(Entity):
|
| 100 |
ashish |
44 |
id = Field(Integer, primary_key=True)
|
| 94 |
ashish |
45 |
location = Field(String(20))
|
| 103 |
ashish |
46 |
status = Field(Integer)
|
|
|
47 |
addedOn = Field(datetime)
|
| 94 |
ashish |
48 |
vendors = ManyToMany('Vendor')
|
|
|
49 |
inventory = OneToMany('CurrentInventorySnapshot')
|
|
|
50 |
inventoryHistory = OneToMany('ItemInventoryHistory')
|
|
|
51 |
|
|
|
52 |
def __repr__(self):
|
|
|
53 |
return "<warehouse>%s</warehouse>" %(self.location)
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
class Item(Entity):
|
| 100 |
ashish |
57 |
id = Field(Integer, primary_key=True)
|
| 94 |
ashish |
58 |
name = Field(String(20))
|
| 103 |
ashish |
59 |
price = Field(Float)
|
|
|
60 |
addedOn = Field(datetime)
|
|
|
61 |
status = Field(Integer)
|
|
|
62 |
startDate = Field(datetime)
|
|
|
63 |
retireDate = Field(datetime)
|
| 94 |
ashish |
64 |
currentInventory = OneToMany('CurrentInventorySnapshot')
|
|
|
65 |
inventoryHistory = OneToMany('ItemInventoryHistory')
|
| 103 |
ashish |
66 |
category = ManyToMany("Category")
|
|
|
67 |
iteminfo = OneToMany("ItemInfo")
|
|
|
68 |
statusChangeLog = OneToMany("ItemChangeLog")
|
|
|
69 |
|
| 94 |
ashish |
70 |
def __repr__(self):
|
|
|
71 |
return "<Item>%s</item>" % (self.name)
|
|
|
72 |
|
| 103 |
ashish |
73 |
@property
|
| 94 |
ashish |
74 |
def get_total_inventory(self):
|
|
|
75 |
if self.currentInventory:
|
|
|
76 |
i = 0
|
|
|
77 |
for entry in self.currentInventory:
|
|
|
78 |
i += entry.availability
|
|
|
79 |
return i
|
|
|
80 |
|
|
|
81 |
else:
|
|
|
82 |
return 0;
|
| 103 |
ashish |
83 |
|
|
|
84 |
@property
|
| 94 |
ashish |
85 |
def get_all_warehouses(self):
|
|
|
86 |
warehouses = []
|
|
|
87 |
for warehouse in self.currentInventory.warehouse:
|
|
|
88 |
warehouses.append(warehouse)
|
|
|
89 |
return warehouses
|
|
|
90 |
|
| 103 |
ashish |
91 |
class ItemInfo(Entity):
|
|
|
92 |
key = Field(String(30))
|
|
|
93 |
value = Field(String(100))
|
|
|
94 |
item = ManyToOne("Item")
|
|
|
95 |
|
|
|
96 |
def __repr__(self):
|
|
|
97 |
return "<ItemInfo><key>%s</key><value>%s</value></ItemInfo>" % (self.key, self.value)
|
| 94 |
ashish |
98 |
|
|
|
99 |
class CurrentInventorySnapshot(Entity):
|
| 100 |
ashish |
100 |
id = Field(Integer, primary_key=True)
|
| 94 |
ashish |
101 |
checkedOn = Field(DateTime, default=datetime.datetime)
|
|
|
102 |
availibility = Field(Integer)
|
|
|
103 |
item = ManyToOne("Item")
|
|
|
104 |
warehouse = ManyToOne("Warehouse")
|
|
|
105 |
|
|
|
106 |
def __repr__(self):
|
|
|
107 |
return "<current inventory><availability> %s </availability><time>%s</time><item>%s</item><warehouse>%s</warehouse></current inventory>" %(self.availibility, self.checkedOn, self.item, self.warehouse)
|
|
|
108 |
|
|
|
109 |
#===============================================================================
|
|
|
110 |
# This is just a placeholder, will be using naveen's code for this.
|
|
|
111 |
#===============================================================================
|
|
|
112 |
|
|
|
113 |
class ItemInventoryHistory(Entity):
|
| 100 |
ashish |
114 |
id = Field(Integer, primary_key=True)
|
| 94 |
ashish |
115 |
timestamp = Field(DateTime, default=datetime.datetime.now())
|
|
|
116 |
availibility = Field(Integer)
|
|
|
117 |
item = ManyToOne("Item")
|
|
|
118 |
warehouse = ManyToOne("Warehouse")
|
|
|
119 |
|
|
|
120 |
def __repr__(self):
|
|
|
121 |
pass
|
| 103 |
ashish |
122 |
|
|
|
123 |
class ItemChangeLog(Entity):
|
|
|
124 |
id = Field(Integer, primary_key=True)
|
|
|
125 |
old_status = Field(Integer)
|
|
|
126 |
new_status = Field(Integer)
|
|
|
127 |
timestamp = Field(datetime)
|
|
|
128 |
item = ManyToOne("Item")
|
| 94 |
ashish |
129 |
|
| 103 |
ashish |
130 |
def __repr__(self):
|
|
|
131 |
return "<Log><id>%d</id><item>%d</item><old_status>%d</old_status><new_status>%d</new_status></Log>" %(self.id,self.item.id, self.old_status, self.new_status)
|
| 94 |
ashish |
132 |
|
| 103 |
ashish |
133 |
|
| 94 |
ashish |
134 |
def initialize():
|
| 100 |
ashish |
135 |
metadata.bind = "sqlite:///catalog.sqlite"
|
| 94 |
ashish |
136 |
metadata.bind.echo = True
|
|
|
137 |
setup_all(True)
|