|
1
|
|
|
from .model import * |
|
2
|
|
|
from . import account |
|
3
|
|
|
from . import event |
|
4
|
|
|
from . import item |
|
5
|
|
|
from . import box |
|
6
|
|
|
from . import user |
|
7
|
|
|
from chezbetty import utility |
|
8
|
|
|
|
|
9
|
|
|
import arrow |
|
10
|
|
|
from pyramid.threadlocal import get_current_registry |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class Transaction(Base): |
|
14
|
|
|
__tablename__ = 'transactions' |
|
15
|
|
|
|
|
16
|
|
|
id = Column(Integer, primary_key=True, nullable=False) |
|
17
|
|
|
|
|
18
|
|
|
event_id = Column(Integer, ForeignKey("events.id")) |
|
19
|
|
|
|
|
20
|
|
|
to_account_virt_id = Column(Integer, ForeignKey("accounts.id")) |
|
21
|
|
|
fr_account_virt_id = Column(Integer, ForeignKey("accounts.id")) |
|
22
|
|
|
to_account_cash_id = Column(Integer, ForeignKey("accounts.id")) |
|
23
|
|
|
fr_account_cash_id = Column(Integer, ForeignKey("accounts.id")) |
|
24
|
|
|
amount = Column(Numeric, nullable=False) |
|
25
|
|
|
|
|
26
|
|
|
# Virtual Transaction Meaning # Cash Transaction Meaning # Notes required? |
|
27
|
|
|
type = Column(Enum("purchase", # user_account -> chezbetty. None |
|
28
|
|
|
"deposit", |
|
29
|
|
|
"cashdeposit", # null -> user_account. null -> cashbox. |
|
30
|
|
|
"ccdeposit", # null -> user_account. null -> chezbetty |
|
31
|
|
|
"btcdeposit", # null -> user_account null -> btcbox |
|
32
|
|
|
"adjustment", # chezbetty <-> user None Yes |
|
33
|
|
|
"restock", # chezbetty -> null chezbetty -> null |
|
34
|
|
|
"inventory", # chezbetty <-> null None |
|
35
|
|
|
"emptycashbox", # None cashbox -> chezbetty |
|
36
|
|
|
"emptybitcoin", # None btcbox -> chezbetty |
|
37
|
|
|
"lost", # None chezbetty/cashbox/btcbox -> null Yes |
|
38
|
|
|
"found", # None null -> chezbetty/cashbox/btcbox Yes |
|
39
|
|
|
"donation", # null -> chezbetty null -> chezbetty Yes |
|
40
|
|
|
"withdrawal", # chezbetty -> null chezbetty -> null Yes |
|
41
|
|
|
name="transaction_type"), nullable=False) |
|
42
|
|
|
__mapper_args__ = {'polymorphic_on': type} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
to_account_virt = relationship(account.Account, |
|
46
|
|
|
foreign_keys=[to_account_virt_id,], |
|
47
|
|
|
backref="transactions_to_virt" |
|
48
|
|
|
) |
|
49
|
|
|
fr_account_virt = relationship(account.Account, |
|
50
|
|
|
foreign_keys=[fr_account_virt_id,], |
|
51
|
|
|
backref="transactions_from_virt" |
|
52
|
|
|
) |
|
53
|
|
|
|
|
54
|
|
|
to_account_cash = relationship(account.Account, |
|
55
|
|
|
foreign_keys=[to_account_cash_id,], |
|
56
|
|
|
backref="transactions_to_cash" |
|
57
|
|
|
) |
|
58
|
|
|
fr_account_cash = relationship(account.Account, |
|
59
|
|
|
foreign_keys=[fr_account_cash_id,], |
|
60
|
|
|
backref="transactions_from_cash" |
|
61
|
|
|
) |
|
62
|
|
|
event = relationship(event.Event, |
|
63
|
|
|
foreign_keys=[event_id,], |
|
64
|
|
|
backref="transactions" |
|
65
|
|
|
) |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
def __init__(self, event, fr_acct_virt, to_acct_virt, fr_acct_cash, to_acct_cash, amount): |
|
69
|
|
|
self.to_account_virt_id = to_acct_virt.id if to_acct_virt else None |
|
70
|
|
|
self.fr_account_virt_id = fr_acct_virt.id if fr_acct_virt else None |
|
71
|
|
|
self.to_account_cash_id = to_acct_cash.id if to_acct_cash else None |
|
72
|
|
|
self.fr_account_cash_id = fr_acct_cash.id if fr_acct_cash else None |
|
73
|
|
|
|
|
74
|
|
|
self.to_acct_virt = to_acct_virt |
|
75
|
|
|
self.fr_acct_virt = fr_acct_virt |
|
76
|
|
|
self.to_acct_cash = to_acct_cash |
|
77
|
|
|
self.fr_acct_cash = fr_acct_cash |
|
78
|
|
|
|
|
79
|
|
|
self.event_id = event.id |
|
80
|
|
|
self.amount = amount |
|
81
|
|
|
|
|
82
|
|
|
# Update the balances of the accounts we are moving money between |
|
83
|
|
|
if to_acct_virt: |
|
84
|
|
|
to_acct_virt.balance += self.amount |
|
85
|
|
|
if fr_acct_virt: |
|
86
|
|
|
fr_acct_virt.balance -= self.amount |
|
87
|
|
|
|
|
88
|
|
|
if to_acct_cash: |
|
89
|
|
|
to_acct_cash.balance += self.amount |
|
90
|
|
|
if fr_acct_cash: |
|
91
|
|
|
fr_acct_cash.balance -= self.amount |
|
92
|
|
|
|
|
93
|
|
|
def update_amount(self, amount): |
|
94
|
|
|
# Remove the balance we added before (upon init or last update_amount) |
|
95
|
|
|
if self.to_acct_virt: |
|
96
|
|
|
self.to_acct_virt.balance -= self.amount |
|
97
|
|
|
if self.fr_acct_virt: |
|
98
|
|
|
self.fr_acct_virt.balance += self.amount |
|
99
|
|
|
if self.to_acct_cash: |
|
100
|
|
|
self.to_acct_cash.balance -= self.amount |
|
101
|
|
|
if self.fr_acct_cash: |
|
102
|
|
|
self.fr_acct_cash.balance += self.amount |
|
103
|
|
|
|
|
104
|
|
|
# Save the amount so we can subtract it later if needed |
|
105
|
|
|
self.amount = amount |
|
106
|
|
|
|
|
107
|
|
|
# Apply the new amount |
|
108
|
|
|
if self.to_acct_virt: |
|
109
|
|
|
self.to_acct_virt.balance += self.amount |
|
110
|
|
|
if self.fr_acct_virt: |
|
111
|
|
|
self.fr_acct_virt.balance -= self.amount |
|
112
|
|
|
if self.to_acct_cash: |
|
113
|
|
|
self.to_acct_cash.balance += self.amount |
|
114
|
|
|
if self.fr_acct_cash: |
|
115
|
|
|
self.fr_acct_cash.balance -= self.amount |
|
116
|
|
|
|
|
117
|
|
|
@classmethod |
|
118
|
|
|
def from_id(cls, id): |
|
119
|
|
|
return DBSession.query(cls)\ |
|
120
|
|
|
.filter(cls.id == id).one() |
|
121
|
|
|
|
|
122
|
|
|
@classmethod |
|
123
|
|
|
def get_balance(cls, trans_type, account_obj): |
|
124
|
|
|
return DBSession.query(coalesce(func.sum(cls.amount), 0).label("balance"))\ |
|
125
|
|
|
.join(event.Event)\ |
|
126
|
|
|
.filter(or_(cls.fr_account_cash_id==account_obj.id, |
|
127
|
|
|
cls.to_account_cash_id==account_obj.id, |
|
128
|
|
|
cls.fr_account_virt_id==account_obj.id, |
|
129
|
|
|
cls.to_account_virt_id==account_obj.id))\ |
|
130
|
|
|
.filter(cls.type==trans_type)\ |
|
131
|
|
|
.filter(event.Event.deleted==False).one() |
|
132
|
|
|
|
|
133
|
|
|
@classmethod |
|
134
|
|
|
def count(cls, trans_type=None): |
|
135
|
|
|
if not trans_type: |
|
136
|
|
|
return DBSession.query(func.count(cls.id).label('c'))\ |
|
137
|
|
|
.join(event.Event)\ |
|
138
|
|
|
.filter(event.Event.deleted==False).one().c |
|
139
|
|
|
else: |
|
140
|
|
|
return DBSession.query(func.count(cls.id).label('c'))\ |
|
141
|
|
|
.join(event.Event)\ |
|
142
|
|
|
.filter(cls.type==trans_type)\ |
|
143
|
|
|
.filter(event.Event.deleted==False).one().c |
|
144
|
|
|
|
|
145
|
|
|
@classmethod |
|
146
|
|
|
def total(cls, start=None, end=None): |
|
147
|
|
|
r = DBSession.query(func.sum(cls.amount).label('a'))\ |
|
148
|
|
|
.join(event.Event)\ |
|
149
|
|
|
.filter(event.Event.deleted==False) |
|
150
|
|
|
|
|
151
|
|
|
if start: |
|
152
|
|
|
r = r.filter(event.Event.timestamp>=start) |
|
153
|
|
|
if end: |
|
154
|
|
|
r = r.filter(event.Event.timestamp<end) |
|
155
|
|
|
|
|
156
|
|
|
return r.one().a or Decimal(0.0) |
|
157
|
|
|
|
|
158
|
|
|
# Get the total amount of discounts people have received for keeping |
|
159
|
|
|
# money in their account |
|
160
|
|
|
@classmethod |
|
161
|
|
|
def discounts(cls, start=None, end=None): |
|
162
|
|
|
r = DBSession.query(func.sum(cls.discount).label('d'))\ |
|
163
|
|
|
.join(event.Event)\ |
|
164
|
|
|
.filter(cls.discount > 0)\ |
|
165
|
|
|
.filter(event.Event.deleted==False) |
|
166
|
|
|
|
|
167
|
|
|
if start: |
|
168
|
|
|
r = r.filter(event.Event.timestamp>=start) |
|
169
|
|
|
if end: |
|
170
|
|
|
r = r.filter(event.Event.timestamp<end) |
|
171
|
|
|
|
|
172
|
|
|
return r.one().d or Decimal(0.0) |
|
173
|
|
|
|
|
174
|
|
|
# Get the total amount of fees people have paid for being in debt |
|
175
|
|
|
@classmethod |
|
176
|
|
|
def fees(cls, start=None, end=None): |
|
177
|
|
|
r = DBSession.query(func.sum(cls.discount).label('f'))\ |
|
178
|
|
|
.join(event.Event)\ |
|
179
|
|
|
.filter(cls.discount < 0)\ |
|
180
|
|
|
.filter(event.Event.deleted==False) |
|
181
|
|
|
|
|
182
|
|
|
if start: |
|
183
|
|
|
r = r.filter(event.Event.timestamp>=start) |
|
184
|
|
|
if end: |
|
185
|
|
|
r = r.filter(event.Event.timestamp<end) |
|
186
|
|
|
|
|
187
|
|
|
return r.one().f or Decimal(0.0) |
|
188
|
|
|
|
|
189
|
|
|
# Returns an array of tuples where the first item is a millisecond timestamp, |
|
190
|
|
|
# the next is the total amount of debt, and the next is the total amount |
|
191
|
|
|
# of stored money for users. |
|
192
|
|
|
@classmethod |
|
193
|
|
|
def get_balance_total_daily(cls): |
|
194
|
|
|
rows = DBSession.query(cls.amount, |
|
195
|
|
|
cls.type, |
|
196
|
|
|
cls.to_account_virt_id, |
|
197
|
|
|
cls.fr_account_virt_id, |
|
198
|
|
|
event.Event.timestamp)\ |
|
199
|
|
|
.join(event.Event)\ |
|
200
|
|
|
.filter(or_( |
|
201
|
|
|
cls.type=='purchase', |
|
202
|
|
|
cls.type=='cashdeposit', |
|
203
|
|
|
cls.type=='ccdeposit', |
|
204
|
|
|
cls.type=='btcdeposit', |
|
205
|
|
|
cls.type=='adjustment' |
|
206
|
|
|
))\ |
|
207
|
|
|
.order_by(event.Event.timestamp)\ |
|
208
|
|
|
.all() |
|
209
|
|
|
return utility.timeseries_balance_total_daily(rows) |
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
@classmethod |
|
213
|
|
|
def get_balances_over_time_for_user(cls, user): |
|
214
|
|
|
rows = DBSession.query(cls.amount, |
|
215
|
|
|
cls.type, |
|
216
|
|
|
cls.to_account_virt_id, |
|
217
|
|
|
cls.fr_account_virt_id, |
|
218
|
|
|
event.Event.timestamp)\ |
|
219
|
|
|
.join(event.Event)\ |
|
220
|
|
|
.filter(or_( |
|
221
|
|
|
cls.type=='purchase', |
|
222
|
|
|
cls.type=='cashdeposit', |
|
223
|
|
|
cls.type=='ccdeposit', |
|
224
|
|
|
cls.type=='btcdeposit', |
|
225
|
|
|
cls.type=='adjustment' |
|
226
|
|
|
))\ |
|
227
|
|
|
.filter(or_( |
|
228
|
|
|
cls.to_account_virt_id == user.id, |
|
229
|
|
|
cls.fr_account_virt_id == user.id, |
|
230
|
|
|
))\ |
|
231
|
|
|
.order_by(event.Event.timestamp)\ |
|
232
|
|
|
.all() |
|
233
|
|
|
# We can re-use the global balance calculation code because the query |
|
234
|
|
|
# filtered it down to only this user, only now the "global" total |
|
235
|
|
|
# positive values (r[2]) and total debt (r[1]) are just this user's |
|
236
|
|
|
# balance, so we pull out the right column at each point in time. |
|
237
|
|
|
rows = utility.timeseries_balance_total_daily(rows) |
|
238
|
|
|
rows = [(r[0],r[2]/100 if r[1]==0 else -r[1]/100) for r in rows] |
|
239
|
|
|
return rows |
|
240
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
def __get_transactions_query(self): |
|
243
|
|
|
return object_session(self).query(Transaction)\ |
|
244
|
|
|
.join(event.Event)\ |
|
245
|
|
|
.filter(or_( |
|
246
|
|
|
or_( |
|
247
|
|
|
Transaction.to_account_virt_id == self.id, |
|
248
|
|
|
Transaction.fr_account_virt_id == self.id, |
|
249
|
|
|
Transaction.to_account_cash_id == self.id, |
|
250
|
|
|
Transaction.fr_account_cash_id == self.id), |
|
251
|
|
|
and_( |
|
252
|
|
|
or_(event.Event.type == "purchase", |
|
253
|
|
|
event.Event.type == "deposit"), |
|
254
|
|
|
event.Event.user_id == self.id)))\ |
|
255
|
|
|
.filter(event.Event.deleted==False)\ |
|
256
|
|
|
.order_by(desc(event.Event.timestamp))\ |
|
257
|
|
|
|
|
258
|
|
|
@limitable_all |
|
259
|
|
|
def __get_transactions(self): |
|
260
|
|
|
return __get_transactions_query(self) |
|
261
|
|
|
|
|
262
|
|
|
@property |
|
263
|
|
|
def __transactions(self): |
|
264
|
|
|
return __get_transactions(self) |
|
265
|
|
|
|
|
266
|
|
|
account.Account.get_transactions_query = __get_transactions_query |
|
267
|
|
|
account.Account.get_transactions = __get_transactions |
|
268
|
|
|
account.Account.transactions = __transactions |
|
269
|
|
|
|
|
270
|
|
|
# This is in a stupid place due to circular input problems |
|
271
|
|
|
@limitable_all |
|
272
|
|
|
def __get_events(self): |
|
273
|
|
|
return object_session(self).query(event.Event)\ |
|
274
|
|
|
.join(Transaction)\ |
|
275
|
|
|
.filter(or_( |
|
276
|
|
|
or_( |
|
277
|
|
|
Transaction.to_account_virt_id == self.id, |
|
278
|
|
|
Transaction.fr_account_virt_id == self.id, |
|
279
|
|
|
Transaction.to_account_cash_id == self.id, |
|
280
|
|
|
Transaction.fr_account_cash_id == self.id), |
|
281
|
|
|
and_( |
|
282
|
|
|
or_(event.Event.type == "purchase", |
|
283
|
|
|
event.Event.type == "deposit"), |
|
284
|
|
|
event.Event.user_id == self.id)))\ |
|
285
|
|
|
.filter(event.Event.deleted==False)\ |
|
286
|
|
|
.order_by(desc(event.Event.timestamp)) |
|
287
|
|
|
|
|
288
|
|
|
@property |
|
289
|
|
|
def __events(self): |
|
290
|
|
|
return __get_events(self).all() |
|
291
|
|
|
|
|
292
|
|
|
account.Account.get_events = __get_events |
|
293
|
|
|
account.Account.events = __events |
|
294
|
|
|
|
|
295
|
|
|
# This is in a stupid place due to circular input problems |
|
296
|
|
|
@property |
|
297
|
|
|
def __total_deposit_amount(self): |
|
298
|
|
|
return object_session(self).query(func.sum(Transaction.amount).label("total"))\ |
|
299
|
|
|
.join(event.Event)\ |
|
300
|
|
|
.filter(and_( |
|
301
|
|
|
Transaction.to_account_virt_id == self.id, |
|
302
|
|
|
or_(Transaction.type == 'cashdeposit', |
|
303
|
|
|
Transaction.type == 'ccdeposit', |
|
304
|
|
|
Transaction.type == 'btcdeposit')))\ |
|
305
|
|
|
.filter(event.Event.deleted==False).one().total or Decimal(0.0) |
|
306
|
|
|
account.Account.total_deposits = __total_deposit_amount |
|
307
|
|
|
|
|
308
|
|
|
# This is in a stupid place due to circular input problems |
|
309
|
|
|
@property |
|
310
|
|
|
def __total_purchase_amount(self): |
|
311
|
|
|
return object_session(self).query(func.sum(Transaction.amount).label("total"))\ |
|
312
|
|
|
.join(event.Event)\ |
|
313
|
|
|
.filter(and_( |
|
314
|
|
|
Transaction.fr_account_virt_id == self.id, |
|
315
|
|
|
Transaction.type == 'purchase'))\ |
|
316
|
|
|
.filter(event.Event.deleted==False).one().total or Decimal(0.0) |
|
317
|
|
|
account.Account.total_purchases = __total_purchase_amount |
|
318
|
|
|
|
|
319
|
|
|
# This is in a stupid place due to circular input problems |
|
320
|
|
|
@classmethod |
|
321
|
|
|
@limitable_all |
|
322
|
|
|
def __get_cash_events(cls): |
|
323
|
|
|
return DBSession.query(event.Event)\ |
|
324
|
|
|
.join(Transaction)\ |
|
325
|
|
|
.filter(or_( |
|
326
|
|
|
Transaction.to_account_cash_id == account.get_cash_account("chezbetty").id, |
|
327
|
|
|
Transaction.fr_account_cash_id == account.get_cash_account("chezbetty").id))\ |
|
328
|
|
|
.filter(event.Event.deleted==False)\ |
|
329
|
|
|
.order_by(desc(event.Event.timestamp)) |
|
330
|
|
|
event.Event.get_cash_events = __get_cash_events |
|
331
|
|
|
|
|
332
|
|
|
# This is in a stupid place due to circular input problems |
|
333
|
|
|
@classmethod |
|
334
|
|
|
@limitable_all |
|
335
|
|
|
def __get_restock_events(cls): |
|
336
|
|
|
return DBSession.query(event.Event)\ |
|
337
|
|
|
.join(Transaction)\ |
|
338
|
|
|
.filter(Transaction.type == 'restock')\ |
|
339
|
|
|
.filter(event.Event.deleted==False)\ |
|
340
|
|
|
.order_by(desc(event.Event.timestamp)) |
|
341
|
|
|
event.Event.get_restock_events = __get_restock_events |
|
342
|
|
|
|
|
343
|
|
|
# This is in a stupid place due to circular input problems |
|
344
|
|
|
@classmethod |
|
345
|
|
|
@limitable_all |
|
346
|
|
|
def __get_emptycash_events(cls): |
|
347
|
|
|
return DBSession.query(event.Event)\ |
|
348
|
|
|
.join(Transaction)\ |
|
349
|
|
|
.filter(or_( |
|
350
|
|
|
Transaction.type == 'emptycashbox', |
|
351
|
|
|
Transaction.type == 'emptybitcoin'))\ |
|
352
|
|
|
.filter(event.Event.deleted==False)\ |
|
353
|
|
|
.order_by(desc(event.Event.timestamp)) |
|
354
|
|
|
event.Event.get_emptycash_events = __get_emptycash_events |
|
355
|
|
|
|
|
356
|
|
|
# This is in a stupid place due to circular input problems |
|
357
|
|
|
@classmethod |
|
358
|
|
|
@limitable_all |
|
359
|
|
|
def __get_deposit_events(cls): |
|
360
|
|
|
return DBSession.query(event.Event)\ |
|
361
|
|
|
.join(Transaction)\ |
|
362
|
|
|
.filter(or_( |
|
363
|
|
|
Transaction.type == 'cashdeposit', |
|
364
|
|
|
Transaction.type == 'ccdeposit', |
|
365
|
|
|
Transaction.type == 'btcdeposit'))\ |
|
366
|
|
|
.filter(event.Event.deleted==False)\ |
|
367
|
|
|
.order_by(desc(event.Event.timestamp)) |
|
368
|
|
|
event.Event.get_deposit_events = __get_deposit_events |
|
369
|
|
|
|
|
370
|
|
|
# This is in a stupid place due to circular input problems |
|
371
|
|
|
@property |
|
372
|
|
|
def __days_since_last_purchase(self): |
|
373
|
|
|
last_purchase = object_session(self).query(event.Event)\ |
|
374
|
|
|
.join(Transaction)\ |
|
375
|
|
|
.filter(Transaction.fr_account_virt_id == self.id)\ |
|
376
|
|
|
.filter(event.Event.type == 'purchase')\ |
|
377
|
|
|
.filter(event.Event.deleted==False)\ |
|
378
|
|
|
.order_by(desc(event.Event.timestamp)).first() |
|
379
|
|
|
|
|
380
|
|
|
if last_purchase: |
|
381
|
|
|
diff = arrow.now() - last_purchase.timestamp |
|
382
|
|
|
return diff.days |
|
383
|
|
|
else: |
|
384
|
|
|
return None |
|
385
|
|
|
user.User.days_since_last_purchase = __days_since_last_purchase |
|
386
|
|
|
|
|
387
|
|
|
# This is in a stupid place due to circular input problems |
|
388
|
|
|
@property |
|
389
|
|
|
def __lifetime_fees(self): |
|
390
|
|
|
return object_session(self).query(func.sum(Purchase.discount).label("f"))\ |
|
391
|
|
|
.join(event.Event)\ |
|
392
|
|
|
.filter(Transaction.fr_account_virt_id == self.id)\ |
|
393
|
|
|
.filter(Purchase.discount < 0)\ |
|
394
|
|
|
.filter(event.Event.type == 'purchase')\ |
|
395
|
|
|
.filter(event.Event.deleted==False).one().f or Decimal(0.0) |
|
396
|
|
|
user.User.lifetime_fees = __lifetime_fees |
|
397
|
|
|
|
|
398
|
|
|
# This is in a stupid place due to circular input problems |
|
399
|
|
|
@property |
|
400
|
|
|
def __lifetime_discounts(self): |
|
401
|
|
|
return object_session(self).query(func.sum(Purchase.discount).label("f"))\ |
|
402
|
|
|
.join(event.Event)\ |
|
403
|
|
|
.filter(Transaction.fr_account_virt_id == self.id)\ |
|
404
|
|
|
.filter(Purchase.discount > 0)\ |
|
405
|
|
|
.filter(event.Event.type == 'purchase')\ |
|
406
|
|
|
.filter(event.Event.deleted==False).one().f or Decimal(0.0) |
|
407
|
|
|
user.User.lifetime_discounts = __lifetime_discounts |
|
408
|
|
|
|
|
409
|
|
|
|
|
410
|
|
|
class Purchase(Transaction): |
|
411
|
|
|
__mapper_args__ = {'polymorphic_identity': 'purchase'} |
|
412
|
|
|
discount = Column(Numeric) |
|
413
|
|
|
|
|
414
|
|
|
def __init__(self, event, user, discount=None): |
|
415
|
|
|
chezbetty_v = account.get_virt_account("chezbetty") |
|
416
|
|
|
Transaction.__init__(self, event, user, chezbetty_v, None, None, Decimal(0.0)) |
|
417
|
|
|
self.discount = discount |
|
418
|
|
|
|
|
419
|
|
|
|
|
420
|
|
|
class Deposit(Transaction): |
|
421
|
|
|
__mapper_args__ = {'polymorphic_identity': 'deposit'} |
|
422
|
|
|
|
|
423
|
|
|
@classmethod |
|
424
|
|
|
def deposits_by_period(cls, period, start=None, end=None): |
|
425
|
|
|
r = DBSession.query(cls.amount.label('summable'), event.Event.timestamp)\ |
|
426
|
|
|
.join(event.Event)\ |
|
427
|
|
|
.order_by(event.Event.timestamp)\ |
|
428
|
|
|
.filter(event.Event.deleted==False) |
|
429
|
|
|
if start: |
|
430
|
|
|
r = r.filter(event.Event.timestamp>=start.replace(tzinfo=None)) |
|
431
|
|
|
if end: |
|
432
|
|
|
r = r.filter(event.Event.timestamp<end.replace(tzinfo=None)) |
|
433
|
|
|
|
|
434
|
|
|
return utility.group(r.all(), period) |
|
435
|
|
|
|
|
436
|
|
|
|
|
437
|
|
|
class CashDeposit(Deposit): |
|
438
|
|
|
__mapper_args__ = {'polymorphic_identity': 'cashdeposit'} |
|
439
|
|
|
|
|
440
|
|
|
CONTENTS_THRESHOLD = 1000 |
|
441
|
|
|
REPEAT_THRESHOLD = 100 |
|
442
|
|
|
|
|
443
|
|
|
def __init__(self, event, user, amount): |
|
444
|
|
|
cashbox_c = account.get_cash_account("cashbox") |
|
445
|
|
|
prev = cashbox_c.balance |
|
446
|
|
|
Transaction.__init__(self, event, None, user, None, cashbox_c, amount) |
|
447
|
|
|
new = cashbox_c.balance |
|
448
|
|
|
|
|
449
|
|
|
# It feels like the model should not have all of this application |
|
450
|
|
|
# specific logic in it. What does sending an email have to do with |
|
451
|
|
|
# representing a transaction. I think this should be moved to |
|
452
|
|
|
# datalayer.py which does handle application logic. |
|
453
|
|
|
try: |
|
454
|
|
|
if prev < CashDeposit.CONTENTS_THRESHOLD and new > CashDeposit.CONTENTS_THRESHOLD: |
|
455
|
|
|
self.send_alert_email(new) |
|
456
|
|
|
elif prev > CashDeposit.CONTENTS_THRESHOLD: |
|
457
|
|
|
pr = int((prev - CashDeposit.CONTENTS_THRESHOLD) / CashDeposit.REPEAT_THRESHOLD) |
|
458
|
|
|
nr = int((new - CashDeposit.CONTENTS_THRESHOLD) / CashDeposit.REPEAT_THRESHOLD) |
|
459
|
|
|
if pr != nr: |
|
460
|
|
|
self.send_alert_email(new, nr) |
|
461
|
|
|
except: |
|
462
|
|
|
# Some error sending email. Let's not prevent the deposit from |
|
463
|
|
|
# going through. |
|
464
|
|
|
pass |
|
465
|
|
|
|
|
466
|
|
|
def send_alert_email(self, amount, repeat=0): |
|
467
|
|
|
settings = get_current_registry().settings |
|
468
|
|
|
|
|
469
|
|
|
SUBJECT = 'Time to empty Betty. Cash box has ${}.'.format(amount) |
|
470
|
|
|
TO = '[email protected]' |
|
471
|
|
|
|
|
472
|
|
|
body = """ |
|
473
|
|
|
<p>Betty's cash box is getting full. Time to go to the bank.</p> |
|
474
|
|
|
<p>The cash box currently contains ${}.</p> |
|
475
|
|
|
""".format(amount) |
|
476
|
|
|
if repeat > 8: |
|
477
|
|
|
body = """ |
|
478
|
|
|
<p><strong>Yo! Get your shit together! That's a lot of cash lying |
|
479
|
|
|
around!</strong></p>""" + body |
|
480
|
|
|
elif repeat > 4: |
|
481
|
|
|
body = body + """ |
|
482
|
|
|
<p><strong>But seriously, you should probably go empty the cashbox |
|
483
|
|
|
like, right meow.</strong></p>""" |
|
484
|
|
|
|
|
485
|
|
|
if 'debugging' in settings and bool(int(settings['debugging'])): |
|
486
|
|
|
SUBJECT = '[ DEBUG_MODE ] ' + SUBJECT |
|
487
|
|
|
body = """ |
|
488
|
|
|
<p><em>This message was sent from a debugging session and may be |
|
489
|
|
|
safely ignored.</em></p>""" + body |
|
490
|
|
|
|
|
491
|
|
|
utility.send_email(TO=TO, SUBJECT=SUBJECT, body=body) |
|
492
|
|
|
|
|
493
|
|
|
|
|
494
|
|
|
class CCDeposit(Deposit): |
|
495
|
|
|
__mapper_args__ = {'polymorphic_identity': 'ccdeposit'} |
|
496
|
|
|
|
|
497
|
|
|
stripe_id = Column(Text) |
|
498
|
|
|
cc_last4 = Column(Text) |
|
499
|
|
|
|
|
500
|
|
|
def __init__(self, event, user, amount, stripe_id, last4): |
|
501
|
|
|
chezbetty_c = account.get_cash_account("chezbetty") |
|
502
|
|
|
Transaction.__init__(self, event, None, user, None, chezbetty_c, amount) |
|
503
|
|
|
self.stripe_id = stripe_id |
|
504
|
|
|
self.cc_last4 = last4 |
|
505
|
|
|
|
|
506
|
|
|
|
|
507
|
|
|
class BTCDeposit(Deposit): |
|
508
|
|
|
__mapper_args__ = {'polymorphic_identity': 'btcdeposit'} |
|
509
|
|
|
|
|
510
|
|
|
btctransaction = Column(String(64)) |
|
511
|
|
|
address = Column(String(64)) |
|
512
|
|
|
amount_btc = Column(Numeric, nullable=True) |
|
513
|
|
|
|
|
514
|
|
|
def __init__(self, event, user, amount, btctransaction, address, amount_btc): |
|
515
|
|
|
btcbox_c = account.get_cash_account("btcbox") |
|
516
|
|
|
Transaction.__init__(self, event, None, user, None, btcbox_c, amount) |
|
517
|
|
|
self.btctransaction = btctransaction |
|
518
|
|
|
self.address = address |
|
519
|
|
|
self.amount_btc = amount_btc |
|
520
|
|
|
|
|
521
|
|
|
def __getattr__(self, name): |
|
522
|
|
|
if name == 'img': |
|
523
|
|
|
return utility.string_to_qrcode(self.btctransaction) |
|
524
|
|
|
else: |
|
525
|
|
|
raise AttributeError |
|
526
|
|
|
|
|
527
|
|
|
@classmethod |
|
528
|
|
|
def from_address(cls, address): |
|
529
|
|
|
return DBSession.query(cls).join(event.Event)\ |
|
530
|
|
|
.filter(cls.address == address)\ |
|
531
|
|
|
.filter(event.Event.deleted == False).one() |
|
532
|
|
|
|
|
533
|
|
|
|
|
534
|
|
|
class Adjustment(Transaction): |
|
535
|
|
|
__mapper_args__ = {'polymorphic_identity': 'adjustment'} |
|
536
|
|
|
|
|
537
|
|
|
def __init__(self, event, user, amount): |
|
538
|
|
|
chezbetty_v = account.get_virt_account("chezbetty") |
|
539
|
|
|
Transaction.__init__(self, event, chezbetty_v, user, None, None, amount) |
|
540
|
|
|
|
|
541
|
|
|
|
|
542
|
|
|
class Restock(Transaction): |
|
543
|
|
|
__mapper_args__ = {'polymorphic_identity': 'restock'} |
|
544
|
|
|
|
|
545
|
|
|
# Additional cost that should get distributed over the entire restock |
|
546
|
|
|
amount_restock_cost = Column(Numeric, nullable=True) |
|
547
|
|
|
|
|
548
|
|
|
def __init__(self, event, global_cost): |
|
549
|
|
|
chezbetty_v = account.get_virt_account("chezbetty") |
|
550
|
|
|
chezbetty_c = account.get_cash_account("chezbetty") |
|
551
|
|
|
Transaction.__init__(self, event, chezbetty_v, None, chezbetty_c, None, Decimal(0.0)) |
|
552
|
|
|
self.amount_restock_cost = global_cost |
|
553
|
|
|
|
|
554
|
|
|
|
|
555
|
|
|
class Inventory(Transaction): |
|
556
|
|
|
__mapper_args__ = {'polymorphic_identity': 'inventory'} |
|
557
|
|
|
def __init__(self, event): |
|
558
|
|
|
chezbetty_v = account.get_virt_account("chezbetty") |
|
559
|
|
|
Transaction.__init__(self, event, chezbetty_v, None, None, None, Decimal(0.0)) |
|
560
|
|
|
|
|
561
|
|
|
|
|
562
|
|
|
class EmptyCashBox(Transaction): |
|
563
|
|
|
__mapper_args__ = {'polymorphic_identity': 'emptycashbox'} |
|
564
|
|
|
def __init__(self, event, amount): |
|
565
|
|
|
cashbox_c = account.get_cash_account("cashbox") |
|
566
|
|
|
chezbetty_c = account.get_cash_account("chezbetty") |
|
567
|
|
|
Transaction.__init__(self, event, None, None, cashbox_c, chezbetty_c, amount) |
|
568
|
|
|
|
|
569
|
|
|
|
|
570
|
|
|
class EmptyBitcoin(Transaction): |
|
571
|
|
|
__mapper_args__ = {'polymorphic_identity': 'emptybitcoin'} |
|
572
|
|
|
def __init__(self, event, amount): |
|
573
|
|
|
btnbox_c = account.get_cash_account("btcbox") |
|
574
|
|
|
chezbetty_c = account.get_cash_account("chezbetty") |
|
575
|
|
|
Transaction.__init__(self, event, None, None, btnbox_c, chezbetty_c, amount) |
|
576
|
|
|
|
|
577
|
|
|
|
|
578
|
|
|
class Lost(Transaction): |
|
579
|
|
|
__mapper_args__ = {'polymorphic_identity': 'lost'} |
|
580
|
|
|
def __init__(self, event, source_acct, amount): |
|
581
|
|
|
Transaction.__init__(self, event, None, None, source_acct, None, amount) |
|
582
|
|
|
|
|
583
|
|
|
|
|
584
|
|
|
class Found(Transaction): |
|
585
|
|
|
__mapper_args__ = {'polymorphic_identity': 'found'} |
|
586
|
|
|
def __init__(self, event, dest_acct, amount): |
|
587
|
|
|
Transaction.__init__(self, event, None, None, None, dest_acct, amount) |
|
588
|
|
|
|
|
589
|
|
|
|
|
590
|
|
|
class Donation(Transaction): |
|
591
|
|
|
__mapper_args__ = {'polymorphic_identity': 'donation'} |
|
592
|
|
|
def __init__(self, event, amount): |
|
593
|
|
|
chezbetty_v = account.get_virt_account("chezbetty") |
|
594
|
|
|
chezbetty_c = account.get_cash_account("chezbetty") |
|
595
|
|
|
Transaction.__init__(self, event, None, chezbetty_v, None, chezbetty_c, amount) |
|
596
|
|
|
|
|
597
|
|
|
|
|
598
|
|
|
class Withdrawal(Transaction): |
|
599
|
|
|
__mapper_args__ = {'polymorphic_identity': 'withdrawal'} |
|
600
|
|
|
def __init__(self, event, amount): |
|
601
|
|
|
chezbetty_v = account.get_virt_account("chezbetty") |
|
602
|
|
|
chezbetty_c = account.get_cash_account("chezbetty") |
|
603
|
|
|
Transaction.__init__(self, event, chezbetty_v, None, chezbetty_c, None, amount) |
|
604
|
|
|
|
|
605
|
|
|
|
|
606
|
|
|
################################################################################ |
|
607
|
|
|
## SUB TRANSACTIONS |
|
608
|
|
|
################################################################################ |
|
609
|
|
|
|
|
610
|
|
|
class SubTransaction(Base): |
|
611
|
|
|
__tablename__ = "subtransactions" |
|
612
|
|
|
|
|
613
|
|
|
id = Column(Integer, primary_key=True, nullable=False) |
|
614
|
|
|
transaction_id = Column(Integer, ForeignKey("transactions.id"), nullable=False) |
|
615
|
|
|
amount = Column(Numeric, nullable=False) |
|
616
|
|
|
type = Column(Enum("purchaselineitem", "restocklineitem", |
|
617
|
|
|
"restocklinebox", "inventorylineitem", |
|
618
|
|
|
name="subtransaction_type"), nullable=False) |
|
619
|
|
|
item_id = Column(Integer, ForeignKey("items.id"), nullable=True) |
|
620
|
|
|
quantity = Column(Integer, nullable=False) |
|
621
|
|
|
wholesale = Column(Numeric, nullable=False) |
|
622
|
|
|
|
|
623
|
|
|
# For restocks |
|
624
|
|
|
coupon_amount = Column(Numeric, nullable=True) # Amount of discount on the item |
|
625
|
|
|
sales_tax = Column(Boolean, nullable=True) # Whether sales tax was charged |
|
626
|
|
|
bottle_deposit = Column(Boolean, nullable=True) # Whether there was a bottle deposit |
|
627
|
|
|
|
|
628
|
|
|
transaction = relationship(Transaction, backref="subtransactions", cascade="all") |
|
629
|
|
|
item = relationship(item.Item, backref="subtransactions") |
|
630
|
|
|
|
|
631
|
|
|
__mapper_args__ = {'polymorphic_on': type} |
|
632
|
|
|
|
|
633
|
|
|
def __init__(self, transaction, amount, item_id, quantity, wholesale): |
|
634
|
|
|
self.transaction_id = transaction.id |
|
635
|
|
|
self.amount = amount |
|
636
|
|
|
self.item_id = item_id |
|
637
|
|
|
self.quantity = quantity |
|
638
|
|
|
self.wholesale = wholesale |
|
639
|
|
|
|
|
640
|
|
|
def __getattr__(self, name): |
|
641
|
|
|
if name == 'deleted': |
|
642
|
|
|
return self.transaction.event.deleted |
|
643
|
|
|
else: |
|
644
|
|
|
raise AttributeError |
|
645
|
|
|
|
|
646
|
|
|
@classmethod |
|
647
|
|
|
@limitable_all |
|
648
|
|
|
def all_item(cls, id): |
|
649
|
|
|
return DBSession.query(cls)\ |
|
650
|
|
|
.join(Transaction)\ |
|
651
|
|
|
.join(event.Event)\ |
|
652
|
|
|
.filter(cls.item_id == id)\ |
|
653
|
|
|
.filter(event.Event.deleted==False)\ |
|
654
|
|
|
.order_by(desc(event.Event.timestamp)) |
|
655
|
|
|
|
|
656
|
|
|
@classmethod |
|
657
|
|
|
@limitable_all |
|
658
|
|
|
def all_item_purchases(cls, id): |
|
659
|
|
|
return DBSession.query(cls)\ |
|
660
|
|
|
.join(Transaction)\ |
|
661
|
|
|
.join(event.Event)\ |
|
662
|
|
|
.filter(cls.item_id == id)\ |
|
663
|
|
|
.filter(event.Event.deleted==False)\ |
|
664
|
|
|
.filter(event.Event.type=="purchase")\ |
|
665
|
|
|
.order_by(desc(event.Event.timestamp)) |
|
666
|
|
|
|
|
667
|
|
|
@classmethod |
|
668
|
|
|
@limitable_all |
|
669
|
|
|
def all_item_events(cls, id): |
|
670
|
|
|
return DBSession.query(cls)\ |
|
671
|
|
|
.join(Transaction)\ |
|
672
|
|
|
.join(event.Event)\ |
|
673
|
|
|
.filter(cls.item_id == id)\ |
|
674
|
|
|
.filter(event.Event.deleted==False)\ |
|
675
|
|
|
.filter(or_(event.Event.type=="inventory", event.Event.type =="restock"))\ |
|
676
|
|
|
.order_by(desc(event.Event.timestamp)) |
|
677
|
|
|
|
|
678
|
|
|
@classmethod |
|
679
|
|
|
@limitable_all |
|
680
|
|
|
def all(cls, trans_type=None): |
|
681
|
|
|
if not trans_type: |
|
682
|
|
|
return DBSession.query(cls)\ |
|
683
|
|
|
.join(Transaction)\ |
|
684
|
|
|
.join(event.Event)\ |
|
685
|
|
|
.filter(event.Event.deleted==False)\ |
|
686
|
|
|
.order_by(desc(event.Event.timestamp)) |
|
687
|
|
|
else: |
|
688
|
|
|
return DBSession.query(cls)\ |
|
689
|
|
|
.join(Transaction)\ |
|
690
|
|
|
.join(event.Event)\ |
|
691
|
|
|
.filter(cls.type==trans_type)\ |
|
692
|
|
|
.filter(event.Event.deleted==False)\ |
|
693
|
|
|
.order_by(desc(event.Event.timestamp)) |
|
694
|
|
|
|
|
695
|
|
|
class PurchaseLineItem(SubTransaction): |
|
696
|
|
|
__mapper_args__ = {'polymorphic_identity': 'purchaselineitem'} |
|
697
|
|
|
price = Column(Numeric) |
|
698
|
|
|
def __init__(self, transaction, amount, item, quantity, price, wholesale): |
|
699
|
|
|
SubTransaction.__init__(self, transaction, amount, item.id, quantity, wholesale) |
|
700
|
|
|
self.price = price |
|
701
|
|
|
|
|
702
|
|
|
@classmethod |
|
703
|
|
|
def quantity_by_period(cls, period, start=None, end=None): |
|
704
|
|
|
r = DBSession.query(cls.quantity.label('summable'), event.Event.timestamp)\ |
|
705
|
|
|
.join(Transaction)\ |
|
706
|
|
|
.join(event.Event)\ |
|
707
|
|
|
.filter(event.Event.deleted==False)\ |
|
708
|
|
|
.order_by(event.Event.timestamp) |
|
709
|
|
|
if start: |
|
710
|
|
|
r = r.filter(event.Event.timestamp>=start.replace(tzinfo=None)) |
|
711
|
|
|
if end: |
|
712
|
|
|
r = r.filter(event.Event.timestamp<end.replace(tzinfo=None)) |
|
713
|
|
|
return utility.group(r.all(), period) |
|
714
|
|
|
|
|
715
|
|
|
@classmethod |
|
716
|
|
|
def virtual_revenue_by_period(cls, period, start=None, end=None): |
|
717
|
|
|
r = DBSession.query(cls.amount.label('summable'), event.Event.timestamp)\ |
|
718
|
|
|
.join(Transaction)\ |
|
719
|
|
|
.join(event.Event)\ |
|
720
|
|
|
.filter(event.Event.deleted==False)\ |
|
721
|
|
|
.order_by(event.Event.timestamp) |
|
722
|
|
|
if start: |
|
723
|
|
|
r = r.filter(event.Event.timestamp>=start.replace(tzinfo=None)) |
|
724
|
|
|
if end: |
|
725
|
|
|
r = r.filter(event.Event.timestamp<end.replace(tzinfo=None)) |
|
726
|
|
|
return utility.group(r.all(), period) |
|
727
|
|
|
|
|
728
|
|
|
@classmethod |
|
729
|
|
|
def profit_on_sales(cls, start=None, end=None): |
|
730
|
|
|
r = DBSession.query(func.sum(cls.amount-(cls.wholesale*cls.quantity)).label('p'))\ |
|
731
|
|
|
.join(Transaction)\ |
|
732
|
|
|
.join(event.Event)\ |
|
733
|
|
|
.filter(event.Event.deleted==False) |
|
734
|
|
|
if start: |
|
735
|
|
|
r = r.filter(event.Event.timestamp>=start.replace(tzinfo=None)) |
|
736
|
|
|
if end: |
|
737
|
|
|
r = r.filter(event.Event.timestamp<end.replace(tzinfo=None)) |
|
738
|
|
|
|
|
739
|
|
|
return r.one().p or Decimal(0.0) |
|
740
|
|
|
|
|
741
|
|
|
@classmethod |
|
742
|
|
|
def item_sale_quantities(cls, item_id): |
|
743
|
|
|
return DBSession.query(cls, event.Event)\ |
|
744
|
|
|
.join(Transaction)\ |
|
745
|
|
|
.join(event.Event)\ |
|
746
|
|
|
.filter(event.Event.deleted==False)\ |
|
747
|
|
|
.filter(cls.item_id==int(item_id))\ |
|
748
|
|
|
.order_by(event.Event.timestamp).all() |
|
749
|
|
|
|
|
750
|
|
|
|
|
751
|
|
|
# This is slowww: |
|
752
|
|
|
# @property |
|
753
|
|
|
# def __number_sold(self): |
|
754
|
|
|
# return object_session(self).query(func.sum(PurchaseLineItem.quantity).label('c'))\ |
|
755
|
|
|
# .join(Transaction)\ |
|
756
|
|
|
# .join(event.Event)\ |
|
757
|
|
|
# .filter(PurchaseLineItem.item_id==self.id)\ |
|
758
|
|
|
# .filter(event.Event.deleted==False).one().c |
|
759
|
|
|
# item.Item.number_sold = __number_sold |
|
760
|
|
|
|
|
761
|
|
|
|
|
762
|
|
|
class RestockLineItem(SubTransaction): |
|
763
|
|
|
__mapper_args__ = {'polymorphic_identity': 'restocklineitem'} |
|
764
|
|
|
def __init__(self, |
|
765
|
|
|
transaction, |
|
766
|
|
|
amount, |
|
767
|
|
|
item, |
|
768
|
|
|
quantity, |
|
769
|
|
|
wholesale, |
|
770
|
|
|
coupon, |
|
771
|
|
|
sales_tax, |
|
772
|
|
|
bottle_deposit): |
|
773
|
|
|
SubTransaction.__init__(self, transaction, amount, item.id, quantity, wholesale) |
|
774
|
|
|
self.coupon_amount = coupon |
|
775
|
|
|
self.sales_tax = sales_tax |
|
776
|
|
|
self.bottle_deposit = bottle_deposit |
|
777
|
|
|
|
|
778
|
|
|
|
|
779
|
|
|
class RestockLineBox(SubTransaction): |
|
780
|
|
|
__mapper_args__ = {'polymorphic_identity': 'restocklinebox'} |
|
781
|
|
|
box_id = Column(Integer, ForeignKey("boxes.id"), nullable=True) |
|
782
|
|
|
|
|
783
|
|
|
box = relationship(box.Box, backref="subtransactions") |
|
784
|
|
|
|
|
785
|
|
|
def __init__(self, |
|
786
|
|
|
transaction, |
|
787
|
|
|
amount, |
|
788
|
|
|
box, |
|
789
|
|
|
quantity, |
|
790
|
|
|
wholesale, |
|
791
|
|
|
coupon, |
|
792
|
|
|
sales_tax, |
|
793
|
|
|
bottle_deposit): |
|
794
|
|
|
self.transaction_id = transaction.id |
|
795
|
|
|
self.amount = amount |
|
796
|
|
|
self.box_id = box.id |
|
797
|
|
|
self.quantity = quantity |
|
798
|
|
|
self.wholesale = wholesale |
|
799
|
|
|
self.coupon_amount = coupon |
|
800
|
|
|
self.sales_tax = sales_tax |
|
801
|
|
|
self.bottle_deposit = bottle_deposit |
|
802
|
|
|
|
|
803
|
|
|
|
|
804
|
|
|
class InventoryLineItem(SubTransaction): |
|
805
|
|
|
__mapper_args__ = {'polymorphic_identity': 'inventorylineitem'} |
|
806
|
|
|
quantity_predicted = synonym(SubTransaction.quantity) |
|
807
|
|
|
quantity_counted = Column(Integer) |
|
808
|
|
|
|
|
809
|
|
|
def __init__(self, transaction, amount, item, quantity_predicted, quantity_counted, wholesale): |
|
810
|
|
|
SubTransaction.__init__(self, transaction, amount, item.id, quantity_predicted, wholesale) |
|
811
|
|
|
self.quantity_counted = quantity_counted |
|
812
|
|
|
|
|
813
|
|
|
|
|
814
|
|
|
|
|
815
|
|
|
################################################################################ |
|
816
|
|
|
## SUBSUB TRANSACTIONS |
|
817
|
|
|
################################################################################ |
|
818
|
|
|
|
|
819
|
|
|
# This is for tracking which items were in which boxes when we restocked |
|
820
|
|
|
|
|
821
|
|
|
class SubSubTransaction(Base): |
|
822
|
|
|
__tablename__ = "subsubtransactions" |
|
823
|
|
|
|
|
824
|
|
|
id = Column(Integer, primary_key=True, nullable=False) |
|
825
|
|
|
subtransaction_id = Column(Integer, ForeignKey("subtransactions.id"), nullable=False) |
|
826
|
|
|
type = Column(Enum("restocklineboxitem", |
|
827
|
|
|
name="subsubtransaction_type"), nullable=False) |
|
828
|
|
|
item_id = Column(Integer, ForeignKey("items.id"), nullable=True) |
|
829
|
|
|
quantity = Column(Integer, nullable=False) |
|
830
|
|
|
|
|
831
|
|
|
subtransaction = relationship(SubTransaction, backref="subsubtransactions", cascade="all") |
|
832
|
|
|
item = relationship(item.Item, backref="subsubtransactions") |
|
833
|
|
|
|
|
834
|
|
|
__mapper_args__ = {'polymorphic_on': type} |
|
835
|
|
|
|
|
836
|
|
|
def __init__(self, subtransaction, item_id, quantity): |
|
837
|
|
|
self.subtransaction_id = subtransaction.id |
|
838
|
|
|
self.item_id = item_id |
|
839
|
|
|
self.quantity = quantity |
|
840
|
|
|
|
|
841
|
|
|
def __getattr__(self, name): |
|
842
|
|
|
if name == 'deleted': |
|
843
|
|
|
return self.subtransaction.transaction.event.deleted |
|
844
|
|
|
else: |
|
845
|
|
|
raise AttributeError |
|
846
|
|
|
|
|
847
|
|
|
@classmethod |
|
848
|
|
|
@limitable_all |
|
849
|
|
|
def all_item(cls, item_id): |
|
850
|
|
|
return DBSession.query(cls)\ |
|
851
|
|
|
.join(SubTransaction)\ |
|
852
|
|
|
.join(Transaction)\ |
|
853
|
|
|
.join(event.Event)\ |
|
854
|
|
|
.filter(cls.item_id == item_id)\ |
|
855
|
|
|
.filter(event.Event.deleted==False)\ |
|
856
|
|
|
.order_by(cls.id) |
|
857
|
|
|
|
|
858
|
|
|
|
|
859
|
|
|
class RestockLineBoxItem(SubSubTransaction): |
|
860
|
|
|
__mapper_args__ = {'polymorphic_identity': 'restocklineboxitem'} |
|
861
|
|
|
def __init__(self, subtransaction, item, quantity): |
|
862
|
|
|
SubSubTransaction.__init__(self, subtransaction, item.id, quantity) |
|
863
|
|
|
|
|
864
|
|
|
|
|
865
|
|
|
|
|
866
|
|
|
|