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
|
|
|
|
331
|
|
|
event.Event.get_cash_events = __get_cash_events |
332
|
|
|
|
333
|
|
|
# This is in a stupid place due to circular input problems |
334
|
|
|
@classmethod |
335
|
|
|
@limitable_all |
336
|
|
|
def __get_restock_events(cls): |
337
|
|
|
return DBSession.query(event.Event)\ |
338
|
|
|
.join(Transaction)\ |
339
|
|
|
.filter(Transaction.type == 'restock')\ |
340
|
|
|
.filter(event.Event.deleted==False)\ |
341
|
|
|
.order_by(desc(event.Event.timestamp)) |
342
|
|
|
|
343
|
|
|
event.Event.get_restock_events = __get_restock_events |
344
|
|
|
|
345
|
|
|
# This is in a stupid place due to circular input problems |
346
|
|
|
@classmethod |
347
|
|
|
@limitable_all |
348
|
|
|
def __get_emptycash_events(cls): |
349
|
|
|
return DBSession.query(event.Event)\ |
350
|
|
|
.join(Transaction)\ |
351
|
|
|
.filter(or_( |
352
|
|
|
Transaction.type == 'emptycashbox', |
353
|
|
|
Transaction.type == 'emptybitcoin'))\ |
354
|
|
|
.filter(event.Event.deleted==False)\ |
355
|
|
|
.order_by(desc(event.Event.timestamp)) |
356
|
|
|
|
357
|
|
|
event.Event.get_emptycash_events = __get_emptycash_events |
358
|
|
|
|
359
|
|
|
# This is in a stupid place due to circular input problems |
360
|
|
|
@classmethod |
361
|
|
|
@limitable_all |
362
|
|
|
def __get_deposit_events(cls): |
363
|
|
|
return DBSession.query(event.Event)\ |
364
|
|
|
.join(Transaction)\ |
365
|
|
|
.filter(or_( |
366
|
|
|
Transaction.type == 'cashdeposit', |
367
|
|
|
Transaction.type == 'ccdeposit', |
368
|
|
|
Transaction.type == 'btcdeposit'))\ |
369
|
|
|
.filter(event.Event.deleted==False)\ |
370
|
|
|
.order_by(desc(event.Event.timestamp)) |
371
|
|
|
|
372
|
|
|
event.Event.get_deposit_events = __get_deposit_events |
373
|
|
|
|
374
|
|
|
# This is in a stupid place due to circular input problems |
375
|
|
|
@property |
376
|
|
|
def __days_since_last_purchase(self): |
377
|
|
|
last_purchase = object_session(self).query(event.Event)\ |
378
|
|
|
.join(Transaction)\ |
379
|
|
|
.filter(Transaction.fr_account_virt_id == self.id)\ |
380
|
|
|
.filter(event.Event.type == 'purchase')\ |
381
|
|
|
.filter(event.Event.deleted==False)\ |
382
|
|
|
.order_by(desc(event.Event.timestamp)).first() |
383
|
|
|
|
384
|
|
|
if last_purchase: |
385
|
|
|
diff = arrow.now() - last_purchase.timestamp |
386
|
|
|
return diff.days |
387
|
|
|
else: |
388
|
|
|
return None |
389
|
|
|
|
390
|
|
|
user.User.days_since_last_purchase = __days_since_last_purchase |
391
|
|
|
|
392
|
|
|
|
393
|
|
|
|
394
|
|
|
class Purchase(Transaction): |
395
|
|
|
__mapper_args__ = {'polymorphic_identity': 'purchase'} |
396
|
|
|
discount = Column(Numeric) |
397
|
|
|
|
398
|
|
|
def __init__(self, event, user, discount=None): |
399
|
|
|
chezbetty_v = account.get_virt_account("chezbetty") |
400
|
|
|
Transaction.__init__(self, event, user, chezbetty_v, None, None, Decimal(0.0)) |
401
|
|
|
self.discount = discount |
402
|
|
|
|
403
|
|
|
|
404
|
|
|
class Deposit(Transaction): |
405
|
|
|
__mapper_args__ = {'polymorphic_identity': 'deposit'} |
406
|
|
|
|
407
|
|
|
@classmethod |
408
|
|
|
def deposits_by_period(cls, period, start=None, end=None): |
409
|
|
|
r = DBSession.query(cls.amount.label('summable'), event.Event.timestamp)\ |
410
|
|
|
.join(event.Event)\ |
411
|
|
|
.order_by(event.Event.timestamp)\ |
412
|
|
|
.filter(event.Event.deleted==False) |
413
|
|
|
if start: |
414
|
|
|
r = r.filter(event.Event.timestamp>=start.replace(tzinfo=None)) |
415
|
|
|
if end: |
416
|
|
|
r = r.filter(event.Event.timestamp<end.replace(tzinfo=None)) |
417
|
|
|
|
418
|
|
|
return utility.group(r.all(), period) |
419
|
|
|
|
420
|
|
|
|
421
|
|
|
class CashDeposit(Deposit): |
422
|
|
|
__mapper_args__ = {'polymorphic_identity': 'cashdeposit'} |
423
|
|
|
|
424
|
|
|
CONTENTS_THRESHOLD = 1000 |
425
|
|
|
REPEAT_THRESHOLD = 100 |
426
|
|
|
|
427
|
|
|
def __init__(self, event, user, amount): |
428
|
|
|
cashbox_c = account.get_cash_account("cashbox") |
429
|
|
|
prev = cashbox_c.balance |
430
|
|
|
Transaction.__init__(self, event, None, user, None, cashbox_c, amount) |
431
|
|
|
new = cashbox_c.balance |
432
|
|
|
|
433
|
|
|
# It feels like the model should not have all of this application |
434
|
|
|
# specific logic in it. What does sending an email have to do with |
435
|
|
|
# representing a transaction. I think this should be moved to |
436
|
|
|
# datalayer.py which does handle application logic. |
437
|
|
|
try: |
438
|
|
|
if prev < CashDeposit.CONTENTS_THRESHOLD and new > CashDeposit.CONTENTS_THRESHOLD: |
439
|
|
|
self.send_alert_email(new) |
440
|
|
|
elif prev > CashDeposit.CONTENTS_THRESHOLD: |
441
|
|
|
pr = int((prev - CashDeposit.CONTENTS_THRESHOLD) / CashDeposit.REPEAT_THRESHOLD) |
442
|
|
|
nr = int((new - CashDeposit.CONTENTS_THRESHOLD) / CashDeposit.REPEAT_THRESHOLD) |
443
|
|
|
if pr != nr: |
444
|
|
|
self.send_alert_email(new, nr) |
445
|
|
|
except: |
446
|
|
|
# Some error sending email. Let's not prevent the deposit from |
447
|
|
|
# going through. |
448
|
|
|
pass |
449
|
|
|
|
450
|
|
|
def send_alert_email(self, amount, repeat=0): |
451
|
|
|
settings = get_current_registry().settings |
452
|
|
|
|
453
|
|
|
SUBJECT = 'Time to empty Betty. Cash box has ${}.'.format(amount) |
454
|
|
|
TO = '[email protected]' |
455
|
|
|
|
456
|
|
|
body = """ |
457
|
|
|
<p>Betty's cash box is getting full. Time to go to the bank.</p> |
458
|
|
|
<p>The cash box currently contains ${}.</p> |
459
|
|
|
""".format(amount) |
460
|
|
|
if repeat > 8: |
461
|
|
|
body = """ |
462
|
|
|
<p><strong>Yo! Get your shit together! That's a lot of cash lying |
463
|
|
|
around!</strong></p>""" + body |
464
|
|
|
elif repeat > 4: |
465
|
|
|
body = body + """ |
466
|
|
|
<p><strong>But seriously, you should probably go empty the cashbox |
467
|
|
|
like, right meow.</strong></p>""" |
468
|
|
|
|
469
|
|
|
if 'debugging' in settings and bool(int(settings['debugging'])): |
470
|
|
|
SUBJECT = '[ DEBUG_MODE ] ' + SUBJECT |
471
|
|
|
body = """ |
472
|
|
|
<p><em>This message was sent from a debugging session and may be |
473
|
|
|
safely ignored.</em></p>""" + body |
474
|
|
|
|
475
|
|
|
utility.send_email(TO=TO, SUBJECT=SUBJECT, body=body) |
476
|
|
|
|
477
|
|
|
|
478
|
|
|
class CCDeposit(Deposit): |
479
|
|
|
__mapper_args__ = {'polymorphic_identity': 'ccdeposit'} |
480
|
|
|
|
481
|
|
|
stripe_id = Column(Text) |
482
|
|
|
cc_last4 = Column(Text) |
483
|
|
|
|
484
|
|
|
def __init__(self, event, user, amount, stripe_id, last4): |
485
|
|
|
chezbetty_c = account.get_cash_account("chezbetty") |
486
|
|
|
Transaction.__init__(self, event, None, user, None, chezbetty_c, amount) |
487
|
|
|
self.stripe_id = stripe_id |
488
|
|
|
self.cc_last4 = last4 |
489
|
|
|
|
490
|
|
|
|
491
|
|
|
class BTCDeposit(Deposit): |
492
|
|
|
__mapper_args__ = {'polymorphic_identity': 'btcdeposit'} |
493
|
|
|
|
494
|
|
|
btctransaction = Column(String(64)) |
495
|
|
|
address = Column(String(64)) |
496
|
|
|
amount_btc = Column(Numeric, nullable=True) |
497
|
|
|
|
498
|
|
|
def __init__(self, event, user, amount, btctransaction, address, amount_btc): |
499
|
|
|
btcbox_c = account.get_cash_account("btcbox") |
500
|
|
|
Transaction.__init__(self, event, None, user, None, btcbox_c, amount) |
501
|
|
|
self.btctransaction = btctransaction |
502
|
|
|
self.address = address |
503
|
|
|
self.amount_btc = amount_btc |
504
|
|
|
|
505
|
|
|
def __getattr__(self, name): |
506
|
|
|
if name == 'img': |
507
|
|
|
return utility.string_to_qrcode(self.btctransaction) |
508
|
|
|
else: |
509
|
|
|
raise AttributeError |
510
|
|
|
|
511
|
|
|
@classmethod |
512
|
|
|
def from_address(cls, address): |
513
|
|
|
return DBSession.query(cls).join(event.Event)\ |
514
|
|
|
.filter(cls.address == address)\ |
515
|
|
|
.filter(event.Event.deleted == False).one() |
516
|
|
|
|
517
|
|
|
|
518
|
|
|
class Adjustment(Transaction): |
519
|
|
|
__mapper_args__ = {'polymorphic_identity': 'adjustment'} |
520
|
|
|
|
521
|
|
|
def __init__(self, event, user, amount): |
522
|
|
|
chezbetty_v = account.get_virt_account("chezbetty") |
523
|
|
|
Transaction.__init__(self, event, chezbetty_v, user, None, None, amount) |
524
|
|
|
|
525
|
|
|
|
526
|
|
|
class Restock(Transaction): |
527
|
|
|
__mapper_args__ = {'polymorphic_identity': 'restock'} |
528
|
|
|
|
529
|
|
|
# Additional cost that should get distributed over the entire restock |
530
|
|
|
amount_restock_cost = Column(Numeric, nullable=True) |
531
|
|
|
|
532
|
|
|
def __init__(self, event, global_cost): |
533
|
|
|
chezbetty_v = account.get_virt_account("chezbetty") |
534
|
|
|
chezbetty_c = account.get_cash_account("chezbetty") |
535
|
|
|
Transaction.__init__(self, event, chezbetty_v, None, chezbetty_c, None, Decimal(0.0)) |
536
|
|
|
self.amount_restock_cost = global_cost |
537
|
|
|
|
538
|
|
|
|
539
|
|
|
class Inventory(Transaction): |
540
|
|
|
__mapper_args__ = {'polymorphic_identity': 'inventory'} |
541
|
|
|
def __init__(self, event): |
542
|
|
|
chezbetty_v = account.get_virt_account("chezbetty") |
543
|
|
|
Transaction.__init__(self, event, chezbetty_v, None, None, None, Decimal(0.0)) |
544
|
|
|
|
545
|
|
|
|
546
|
|
|
class EmptyCashBox(Transaction): |
547
|
|
|
__mapper_args__ = {'polymorphic_identity': 'emptycashbox'} |
548
|
|
|
def __init__(self, event, amount): |
549
|
|
|
cashbox_c = account.get_cash_account("cashbox") |
550
|
|
|
chezbetty_c = account.get_cash_account("chezbetty") |
551
|
|
|
Transaction.__init__(self, event, None, None, cashbox_c, chezbetty_c, amount) |
552
|
|
|
|
553
|
|
|
|
554
|
|
|
class EmptyBitcoin(Transaction): |
555
|
|
|
__mapper_args__ = {'polymorphic_identity': 'emptybitcoin'} |
556
|
|
|
def __init__(self, event, amount): |
557
|
|
|
btnbox_c = account.get_cash_account("btcbox") |
558
|
|
|
chezbetty_c = account.get_cash_account("chezbetty") |
559
|
|
|
Transaction.__init__(self, event, None, None, btnbox_c, chezbetty_c, amount) |
560
|
|
|
|
561
|
|
|
|
562
|
|
|
class Lost(Transaction): |
563
|
|
|
__mapper_args__ = {'polymorphic_identity': 'lost'} |
564
|
|
|
def __init__(self, event, source_acct, amount): |
565
|
|
|
Transaction.__init__(self, event, None, None, source_acct, None, amount) |
566
|
|
|
|
567
|
|
|
|
568
|
|
|
class Found(Transaction): |
569
|
|
|
__mapper_args__ = {'polymorphic_identity': 'found'} |
570
|
|
|
def __init__(self, event, dest_acct, amount): |
571
|
|
|
Transaction.__init__(self, event, None, None, None, dest_acct, amount) |
572
|
|
|
|
573
|
|
|
|
574
|
|
|
class Donation(Transaction): |
575
|
|
|
__mapper_args__ = {'polymorphic_identity': 'donation'} |
576
|
|
|
def __init__(self, event, amount): |
577
|
|
|
chezbetty_v = account.get_virt_account("chezbetty") |
578
|
|
|
chezbetty_c = account.get_cash_account("chezbetty") |
579
|
|
|
Transaction.__init__(self, event, None, chezbetty_v, None, chezbetty_c, amount) |
580
|
|
|
|
581
|
|
|
|
582
|
|
|
class Withdrawal(Transaction): |
583
|
|
|
__mapper_args__ = {'polymorphic_identity': 'withdrawal'} |
584
|
|
|
def __init__(self, event, amount): |
585
|
|
|
chezbetty_v = account.get_virt_account("chezbetty") |
586
|
|
|
chezbetty_c = account.get_cash_account("chezbetty") |
587
|
|
|
Transaction.__init__(self, event, chezbetty_v, None, chezbetty_c, None, amount) |
588
|
|
|
|
589
|
|
|
|
590
|
|
|
################################################################################ |
591
|
|
|
## SUB TRANSACTIONS |
592
|
|
|
################################################################################ |
593
|
|
|
|
594
|
|
|
class SubTransaction(Base): |
595
|
|
|
__tablename__ = "subtransactions" |
596
|
|
|
|
597
|
|
|
id = Column(Integer, primary_key=True, nullable=False) |
598
|
|
|
transaction_id = Column(Integer, ForeignKey("transactions.id"), nullable=False) |
599
|
|
|
amount = Column(Numeric, nullable=False) |
600
|
|
|
type = Column(Enum("purchaselineitem", "restocklineitem", |
601
|
|
|
"restocklinebox", "inventorylineitem", |
602
|
|
|
name="subtransaction_type"), nullable=False) |
603
|
|
|
item_id = Column(Integer, ForeignKey("items.id"), nullable=True) |
604
|
|
|
quantity = Column(Integer, nullable=False) |
605
|
|
|
wholesale = Column(Numeric, nullable=False) |
606
|
|
|
|
607
|
|
|
# For restocks |
608
|
|
|
coupon_amount = Column(Numeric, nullable=True) # Amount of discount on the item |
609
|
|
|
sales_tax = Column(Boolean, nullable=True) # Whether sales tax was charged |
610
|
|
|
bottle_deposit = Column(Boolean, nullable=True) # Whether there was a bottle deposit |
611
|
|
|
|
612
|
|
|
transaction = relationship(Transaction, backref="subtransactions", cascade="all") |
613
|
|
|
item = relationship(item.Item, backref="subtransactions") |
614
|
|
|
|
615
|
|
|
__mapper_args__ = {'polymorphic_on': type} |
616
|
|
|
|
617
|
|
|
def __init__(self, transaction, amount, item_id, quantity, wholesale): |
618
|
|
|
self.transaction_id = transaction.id |
619
|
|
|
self.amount = amount |
620
|
|
|
self.item_id = item_id |
621
|
|
|
self.quantity = quantity |
622
|
|
|
self.wholesale = wholesale |
623
|
|
|
|
624
|
|
|
def __getattr__(self, name): |
625
|
|
|
if name == 'deleted': |
626
|
|
|
return self.transaction.event.deleted |
627
|
|
|
else: |
628
|
|
|
raise AttributeError |
629
|
|
|
|
630
|
|
|
@classmethod |
631
|
|
|
@limitable_all |
632
|
|
|
def all_item(cls, id): |
633
|
|
|
return DBSession.query(cls)\ |
634
|
|
|
.join(Transaction)\ |
635
|
|
|
.join(event.Event)\ |
636
|
|
|
.filter(cls.item_id == id)\ |
637
|
|
|
.filter(event.Event.deleted==False)\ |
638
|
|
|
.order_by(desc(event.Event.timestamp)) |
639
|
|
|
|
640
|
|
|
@classmethod |
641
|
|
|
@limitable_all |
642
|
|
|
def all_item_purchases(cls, id): |
643
|
|
|
return DBSession.query(cls)\ |
644
|
|
|
.join(Transaction)\ |
645
|
|
|
.join(event.Event)\ |
646
|
|
|
.filter(cls.item_id == id)\ |
647
|
|
|
.filter(event.Event.deleted==False)\ |
648
|
|
|
.filter(event.Event.type=="purchase")\ |
649
|
|
|
.order_by(desc(event.Event.timestamp)) |
650
|
|
|
|
651
|
|
|
@classmethod |
652
|
|
|
@limitable_all |
653
|
|
|
def all_item_events(cls, id): |
654
|
|
|
return DBSession.query(cls)\ |
655
|
|
|
.join(Transaction)\ |
656
|
|
|
.join(event.Event)\ |
657
|
|
|
.filter(cls.item_id == id)\ |
658
|
|
|
.filter(event.Event.deleted==False)\ |
659
|
|
|
.filter(or_(event.Event.type=="inventory", event.Event.type =="restock"))\ |
660
|
|
|
.order_by(desc(event.Event.timestamp)) |
661
|
|
|
|
662
|
|
|
@classmethod |
663
|
|
|
@limitable_all |
664
|
|
|
def all(cls, trans_type=None): |
665
|
|
|
if not trans_type: |
666
|
|
|
return DBSession.query(cls)\ |
667
|
|
|
.join(Transaction)\ |
668
|
|
|
.join(event.Event)\ |
669
|
|
|
.filter(event.Event.deleted==False)\ |
670
|
|
|
.order_by(desc(event.Event.timestamp)) |
671
|
|
|
else: |
672
|
|
|
return DBSession.query(cls)\ |
673
|
|
|
.join(Transaction)\ |
674
|
|
|
.join(event.Event)\ |
675
|
|
|
.filter(cls.type==trans_type)\ |
676
|
|
|
.filter(event.Event.deleted==False)\ |
677
|
|
|
.order_by(desc(event.Event.timestamp)) |
678
|
|
|
|
679
|
|
|
class PurchaseLineItem(SubTransaction): |
680
|
|
|
__mapper_args__ = {'polymorphic_identity': 'purchaselineitem'} |
681
|
|
|
price = Column(Numeric) |
682
|
|
|
def __init__(self, transaction, amount, item, quantity, price, wholesale): |
683
|
|
|
SubTransaction.__init__(self, transaction, amount, item.id, quantity, wholesale) |
684
|
|
|
self.price = price |
685
|
|
|
|
686
|
|
|
@classmethod |
687
|
|
|
def quantity_by_period(cls, period, start=None, end=None): |
688
|
|
|
r = DBSession.query(cls.quantity.label('summable'), event.Event.timestamp)\ |
689
|
|
|
.join(Transaction)\ |
690
|
|
|
.join(event.Event)\ |
691
|
|
|
.filter(event.Event.deleted==False)\ |
692
|
|
|
.order_by(event.Event.timestamp) |
693
|
|
|
if start: |
694
|
|
|
r = r.filter(event.Event.timestamp>=start.replace(tzinfo=None)) |
695
|
|
|
if end: |
696
|
|
|
r = r.filter(event.Event.timestamp<end.replace(tzinfo=None)) |
697
|
|
|
return utility.group(r.all(), period) |
698
|
|
|
|
699
|
|
|
@classmethod |
700
|
|
|
def virtual_revenue_by_period(cls, period, start=None, end=None): |
701
|
|
|
r = DBSession.query(cls.amount.label('summable'), event.Event.timestamp)\ |
702
|
|
|
.join(Transaction)\ |
703
|
|
|
.join(event.Event)\ |
704
|
|
|
.filter(event.Event.deleted==False)\ |
705
|
|
|
.order_by(event.Event.timestamp) |
706
|
|
|
if start: |
707
|
|
|
r = r.filter(event.Event.timestamp>=start.replace(tzinfo=None)) |
708
|
|
|
if end: |
709
|
|
|
r = r.filter(event.Event.timestamp<end.replace(tzinfo=None)) |
710
|
|
|
return utility.group(r.all(), period) |
711
|
|
|
|
712
|
|
|
@classmethod |
713
|
|
|
def profit_on_sales(cls, start=None, end=None): |
714
|
|
|
r = DBSession.query(func.sum(cls.amount-(cls.wholesale*cls.quantity)).label('p'))\ |
715
|
|
|
.join(Transaction)\ |
716
|
|
|
.join(event.Event)\ |
717
|
|
|
.filter(event.Event.deleted==False) |
718
|
|
|
if start: |
719
|
|
|
r = r.filter(event.Event.timestamp>=start.replace(tzinfo=None)) |
720
|
|
|
if end: |
721
|
|
|
r = r.filter(event.Event.timestamp<end.replace(tzinfo=None)) |
722
|
|
|
|
723
|
|
|
return r.one().p or Decimal(0.0) |
724
|
|
|
|
725
|
|
|
@classmethod |
726
|
|
|
def item_sale_quantities(cls, item_id): |
727
|
|
|
return DBSession.query(cls, event.Event)\ |
728
|
|
|
.join(Transaction)\ |
729
|
|
|
.join(event.Event)\ |
730
|
|
|
.filter(event.Event.deleted==False)\ |
731
|
|
|
.filter(cls.item_id==int(item_id))\ |
732
|
|
|
.order_by(event.Event.timestamp).all() |
733
|
|
|
|
734
|
|
|
|
735
|
|
|
# This is slowww: |
736
|
|
|
# @property |
737
|
|
|
# def __number_sold(self): |
738
|
|
|
# return object_session(self).query(func.sum(PurchaseLineItem.quantity).label('c'))\ |
739
|
|
|
# .join(Transaction)\ |
740
|
|
|
# .join(event.Event)\ |
741
|
|
|
# .filter(PurchaseLineItem.item_id==self.id)\ |
742
|
|
|
# .filter(event.Event.deleted==False).one().c |
743
|
|
|
# item.Item.number_sold = __number_sold |
744
|
|
|
|
745
|
|
|
|
746
|
|
|
class RestockLineItem(SubTransaction): |
747
|
|
|
__mapper_args__ = {'polymorphic_identity': 'restocklineitem'} |
748
|
|
|
def __init__(self, |
749
|
|
|
transaction, |
750
|
|
|
amount, |
751
|
|
|
item, |
752
|
|
|
quantity, |
753
|
|
|
wholesale, |
754
|
|
|
coupon, |
755
|
|
|
sales_tax, |
756
|
|
|
bottle_deposit): |
757
|
|
|
SubTransaction.__init__(self, transaction, amount, item.id, quantity, wholesale) |
758
|
|
|
self.coupon_amount = coupon |
759
|
|
|
self.sales_tax = sales_tax |
760
|
|
|
self.bottle_deposit = bottle_deposit |
761
|
|
|
|
762
|
|
|
|
763
|
|
|
class RestockLineBox(SubTransaction): |
764
|
|
|
__mapper_args__ = {'polymorphic_identity': 'restocklinebox'} |
765
|
|
|
box_id = Column(Integer, ForeignKey("boxes.id"), nullable=True) |
766
|
|
|
|
767
|
|
|
box = relationship(box.Box, backref="subtransactions") |
768
|
|
|
|
769
|
|
|
def __init__(self, |
770
|
|
|
transaction, |
771
|
|
|
amount, |
772
|
|
|
box, |
773
|
|
|
quantity, |
774
|
|
|
wholesale, |
775
|
|
|
coupon, |
776
|
|
|
sales_tax, |
777
|
|
|
bottle_deposit): |
778
|
|
|
self.transaction_id = transaction.id |
779
|
|
|
self.amount = amount |
780
|
|
|
self.box_id = box.id |
781
|
|
|
self.quantity = quantity |
782
|
|
|
self.wholesale = wholesale |
783
|
|
|
self.coupon_amount = coupon |
784
|
|
|
self.sales_tax = sales_tax |
785
|
|
|
self.bottle_deposit = bottle_deposit |
786
|
|
|
|
787
|
|
|
|
788
|
|
|
class InventoryLineItem(SubTransaction): |
789
|
|
|
__mapper_args__ = {'polymorphic_identity': 'inventorylineitem'} |
790
|
|
|
quantity_predicted = synonym(SubTransaction.quantity) |
791
|
|
|
quantity_counted = Column(Integer) |
792
|
|
|
|
793
|
|
|
def __init__(self, transaction, amount, item, quantity_predicted, quantity_counted, wholesale): |
794
|
|
|
SubTransaction.__init__(self, transaction, amount, item.id, quantity_predicted, wholesale) |
795
|
|
|
self.quantity_counted = quantity_counted |
796
|
|
|
|
797
|
|
|
|
798
|
|
|
|
799
|
|
|
################################################################################ |
800
|
|
|
## SUBSUB TRANSACTIONS |
801
|
|
|
################################################################################ |
802
|
|
|
|
803
|
|
|
# This is for tracking which items were in which boxes when we restocked |
804
|
|
|
|
805
|
|
|
class SubSubTransaction(Base): |
806
|
|
|
__tablename__ = "subsubtransactions" |
807
|
|
|
|
808
|
|
|
id = Column(Integer, primary_key=True, nullable=False) |
809
|
|
|
subtransaction_id = Column(Integer, ForeignKey("subtransactions.id"), nullable=False) |
810
|
|
|
type = Column(Enum("restocklineboxitem", |
811
|
|
|
name="subsubtransaction_type"), nullable=False) |
812
|
|
|
item_id = Column(Integer, ForeignKey("items.id"), nullable=True) |
813
|
|
|
quantity = Column(Integer, nullable=False) |
814
|
|
|
|
815
|
|
|
subtransaction = relationship(SubTransaction, backref="subsubtransactions", cascade="all") |
816
|
|
|
item = relationship(item.Item, backref="subsubtransactions") |
817
|
|
|
|
818
|
|
|
__mapper_args__ = {'polymorphic_on': type} |
819
|
|
|
|
820
|
|
|
def __init__(self, subtransaction, item_id, quantity): |
821
|
|
|
self.subtransaction_id = subtransaction.id |
822
|
|
|
self.item_id = item_id |
823
|
|
|
self.quantity = quantity |
824
|
|
|
|
825
|
|
|
def __getattr__(self, name): |
826
|
|
|
if name == 'deleted': |
827
|
|
|
return self.subtransaction.transaction.event.deleted |
828
|
|
|
else: |
829
|
|
|
raise AttributeError |
830
|
|
|
|
831
|
|
|
@classmethod |
832
|
|
|
@limitable_all |
833
|
|
|
def all_item(cls, item_id): |
834
|
|
|
return DBSession.query(cls)\ |
835
|
|
|
.join(SubTransaction)\ |
836
|
|
|
.join(Transaction)\ |
837
|
|
|
.join(event.Event)\ |
838
|
|
|
.filter(cls.item_id == item_id)\ |
839
|
|
|
.filter(event.Event.deleted==False)\ |
840
|
|
|
.order_by(cls.id) |
841
|
|
|
|
842
|
|
|
|
843
|
|
|
class RestockLineBoxItem(SubSubTransaction): |
844
|
|
|
__mapper_args__ = {'polymorphic_identity': 'restocklineboxitem'} |
845
|
|
|
def __init__(self, subtransaction, item, quantity): |
846
|
|
|
SubSubTransaction.__init__(self, subtransaction, item.id, quantity) |
847
|
|
|
|
848
|
|
|
|
849
|
|
|
|
850
|
|
|
|