|
1
|
|
|
from .model import * |
|
2
|
|
|
|
|
3
|
|
|
class ItemImage(Base): |
|
4
|
|
|
__tablename__ = 'items_images' |
|
5
|
|
|
|
|
6
|
|
|
id = Column(Integer, primary_key=True, nullable=False) |
|
7
|
|
|
item_id = Column(Integer, ForeignKey("items.id"), nullable=False) |
|
8
|
|
|
img = Column(LargeBinary, nullable=False) |
|
9
|
|
|
|
|
10
|
|
|
def __init__(self, item_id, img): |
|
11
|
|
|
self.item_id = item_id |
|
12
|
|
|
self.img = img |
|
13
|
|
|
|
|
14
|
|
|
def __str__(self): |
|
15
|
|
|
return "<ItemImage (%s)>" % self.item.name |
|
16
|
|
|
|
|
17
|
|
|
__repr__ = __str__ |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
class Item(Versioned, Base): |
|
21
|
|
|
__tablename__ = 'items' |
|
22
|
|
|
|
|
23
|
|
|
id = Column(Integer, primary_key=True, nullable=False) |
|
24
|
|
|
name = Column(String(255), nullable=False, unique=True) |
|
25
|
|
|
barcode = Column(String(255), nullable=True, unique=True) |
|
26
|
|
|
price = Column(Numeric, nullable=False) |
|
27
|
|
|
wholesale = Column(Numeric, nullable=False) |
|
28
|
|
|
bottle_dep = Column(Boolean, nullable=False, default=False) |
|
29
|
|
|
sales_tax = Column(Boolean, nullable=False, default=False) |
|
30
|
|
|
in_stock = Column(Integer, nullable=False, default=0) |
|
31
|
|
|
img = relationship(ItemImage, uselist=False, backref="item") |
|
32
|
|
|
|
|
33
|
|
|
enabled = Column(Boolean, default=True, nullable=False) |
|
34
|
|
|
|
|
35
|
|
|
def __init__(self, name, barcode, price, wholesale, bottle_dep, sales_tax, |
|
36
|
|
|
in_stock, enabled): |
|
37
|
|
|
self.name = name |
|
38
|
|
|
self.barcode = barcode |
|
39
|
|
|
self.price = price |
|
40
|
|
|
self.wholesale = wholesale |
|
41
|
|
|
self.bottle_dep = bottle_dep |
|
42
|
|
|
self.sales_tax = sales_tax |
|
43
|
|
|
self.in_stock = in_stock |
|
44
|
|
|
self.enabled = enabled |
|
45
|
|
|
|
|
46
|
|
|
@classmethod |
|
47
|
|
|
def from_id(cls, id): |
|
48
|
|
|
return DBSession.query(cls).filter(cls.id == id).one() |
|
49
|
|
|
|
|
50
|
|
|
@classmethod |
|
51
|
|
|
def from_barcode(cls, barcode): |
|
52
|
|
|
return DBSession.query(cls).filter(cls.barcode == barcode).one() |
|
53
|
|
|
|
|
54
|
|
|
@classmethod |
|
55
|
|
|
def from_fuzzy(cls, search_str): |
|
56
|
|
|
return DBSession.query(cls)\ |
|
57
|
|
|
.filter(or_( |
|
58
|
|
|
cls.barcode.ilike('%{}%'.format(search_str)), |
|
59
|
|
|
cls.name.ilike('%{}%'.format(search_str)) |
|
60
|
|
|
)).all() |
|
61
|
|
|
|
|
62
|
|
|
@classmethod |
|
63
|
|
|
def from_name(cls, name): |
|
64
|
|
|
return DBSession.query(cls).filter(cls.name == name).one() |
|
65
|
|
|
|
|
66
|
|
|
@classmethod |
|
67
|
|
|
def all(cls): |
|
68
|
|
|
return DBSession.query(cls)\ |
|
69
|
|
|
.filter(cls.enabled)\ |
|
70
|
|
|
.order_by(cls.name).all() |
|
71
|
|
|
|
|
72
|
|
|
@classmethod |
|
73
|
|
|
def all_force(cls): |
|
74
|
|
|
return DBSession.query(cls)\ |
|
75
|
|
|
.order_by(cls.name).all() |
|
76
|
|
|
|
|
77
|
|
|
@classmethod |
|
78
|
|
|
def count(cls): |
|
79
|
|
|
return DBSession.query(func.count(cls.id).label('c'))\ |
|
80
|
|
|
.filter(cls.enabled).one().c |
|
81
|
|
|
|
|
82
|
|
|
@classmethod |
|
83
|
|
|
def exists_name(cls, name): |
|
84
|
|
|
return DBSession.query(func.count(cls.id).label('c'))\ |
|
85
|
|
|
.filter(cls.name == name).one().c > 0 |
|
86
|
|
|
|
|
87
|
|
|
@classmethod |
|
88
|
|
|
def exists_barcode(cls, barcode): |
|
89
|
|
|
return DBSession.query(func.count(cls.id).label('c'))\ |
|
90
|
|
|
.filter(cls.barcode == barcode).one().c > 0 |
|
91
|
|
|
|
|
92
|
|
|
@classmethod |
|
93
|
|
|
def total_inventory_wholesale(cls): |
|
94
|
|
|
return DBSession.query(func.sum(cls.wholesale * cls.in_stock).label('c'))\ |
|
95
|
|
|
.one().c or 0 |
|
96
|
|
|
|
|
97
|
|
|
def __str__(self): |
|
98
|
|
|
return "<Item (%s)>" % self.name |
|
99
|
|
|
|
|
100
|
|
|
__repr__ = __str__ |
|
101
|
|
|
|
|
102
|
|
|
|