This commit is contained in:
Alexis Lahouze 2017-05-04 14:45:18 +02:00
parent 36af37d611
commit 85468abe58
2 changed files with 13 additions and 17 deletions

View File

@ -69,15 +69,13 @@ class Account(db.Model):
query = db.session.query(
db.func.sum(
db.case(
[(db.func.sign(Operation.value) == -1,
Operation.value)],
[(db.func.sign(Operation.value) == -1, Operation.value)],
else_=0
)
).label("expenses"),
db.func.sum(
db.case(
[(db.func.sign(Operation.value) == 1,
Operation.value)],
[(db.func.sign(Operation.value) == 1, Operation.value)],
else_=0
)
).label("revenues"),

View File

@ -99,29 +99,27 @@ class ScheduledOperation(db.Model):
stop_date = arrow.get(self.stop_date)
# Iterate over the range.
for c_date in arrow.Arrow.range(
"month", start_date, stop_date
)[::self.frequency]:
date_range = arrow.Arrow.range("month", start_date, stop_date)
dates = date_range[::self.frequency]
for date in dates:
# If a date day is below the first date day, next dates will not
# have the right day.
day = min(self.day, monthrange(
c_date.year, c_date.month)[1])
date.year, date.month)[1])
c_date = c_date.replace(day=day)
date = date.replace(day=day)
# Search if a close operation exists.
if not db.session.query(
Operation
).filter(
if not db.session.query(Operation).filter(
Operation.account_id == self.account_id,
Operation.scheduled_operation_id == self.id,
Operation.operation_date >= c_date.replace(weeks=-2).date(),
Operation.operation_date <= c_date.replace(weeks=+2).date()
Operation.operation_date >= date.replace(weeks=-2).date(),
Operation.operation_date <= date.replace(weeks=+2).date()
).count():
# Create not confirmed operation if not found.
operation = Operation(
operation_date=c_date.date(),
operation_date=date.date(),
label=self.label,
value=self.value,
category=self.category,