Subversion Repositories SmartDukaan

Rev

Rev 94 | Rev 103 | 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 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
 
14
class Category(Entity):
15
    '''
16
    This class represents the catalog. 
17
    '''
100 ashish 18
    id = Field(Integer, primary_key=True)
94 ashish 19
    name = Field(String(30))
20
    isRoot = Field(Integer)
21
    addedOn = Field(DateTime)
22
    items = OneToMany('Item')
23
    children = OneToMany('Category', inverse='parent')
24
    parent = ManyToOne('Category', inverse='children')
25
 
26
    def __repr__(self):
27
        return "<id>%id </id>"%(self.id)
28
 
29
 
30
class Vendor(Entity):
100 ashish 31
    id = Field(Integer, primary_key=True)
94 ashish 32
    name = Field(String(30))
33
    warehouses = ManyToMany('Warehouse')
34
    def __repr__(self):
35
        return "<Vendor>%s</Vendor>" %(self.name)
36
 
37
 
38
class Warehouse(Entity):
100 ashish 39
    id = Field(Integer, primary_key=True)
94 ashish 40
    location = Field(String(20))
41
    vendors = ManyToMany('Vendor')
42
    inventory = OneToMany('CurrentInventorySnapshot')
43
    inventoryHistory = OneToMany('ItemInventoryHistory')
44
 
45
    def __repr__(self):
46
        return "<warehouse>%s</warehouse>" %(self.location)
47
 
48
 
49
class Item(Entity):
100 ashish 50
    id = Field(Integer, primary_key=True)
94 ashish 51
    name = Field(String(20))
52
    currentInventory = OneToMany('CurrentInventorySnapshot')
53
    inventoryHistory = OneToMany('ItemInventoryHistory')
54
    category = ManyToOne("Category")
55
    def __repr__(self):
56
        return "<Item>%s</item>" % (self.name)
57
 
58
    def get_total_inventory(self):
59
        if self.currentInventory:
60
            i = 0
61
            for entry in self.currentInventory:
62
                i += entry.availability
63
            return i
64
 
65
        else:
66
            return 0;
67
 
68
    def get_all_warehouses(self):
69
        warehouses = []
70
        for warehouse in self.currentInventory.warehouse:
71
            warehouses.append(warehouse)
72
        return warehouses
73
 
74
 
75
class CurrentInventorySnapshot(Entity):
100 ashish 76
    id = Field(Integer, primary_key=True)
94 ashish 77
    checkedOn = Field(DateTime, default=datetime.datetime)
78
    availibility = Field(Integer)
79
    item = ManyToOne("Item")
80
    warehouse = ManyToOne("Warehouse")
81
 
82
    def __repr__(self):
83
        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)
84
 
85
#===============================================================================
86
# This is just a placeholder, will be using naveen's code for this.
87
#===============================================================================
88
 
89
class ItemAttribs(Entity):
100 ashish 90
    id = Field(Integer, primary_key=True)
94 ashish 91
    def __repr__(self):
92
        pass
93
 
94
 
95
class ItemInventoryHistory(Entity):
100 ashish 96
    id = Field(Integer, primary_key=True)    
94 ashish 97
    timestamp = Field(DateTime, default=datetime.datetime.now())
98
    availibility = Field(Integer)
99
    item = ManyToOne("Item")
100
    warehouse = ManyToOne("Warehouse")
101
 
102
    def __repr__(self):
103
        pass
104
 
105
 
106
def initialize():
100 ashish 107
    metadata.bind = "sqlite:///catalog.sqlite"
94 ashish 108
    metadata.bind.echo = True
109
 
110
    setup_all(True)