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( query = db.session.query(
db.func.sum( db.func.sum(
db.case( db.case(
[(db.func.sign(Operation.value) == -1, [(db.func.sign(Operation.value) == -1, Operation.value)],
Operation.value)],
else_=0 else_=0
) )
).label("expenses"), ).label("expenses"),
db.func.sum( db.func.sum(
db.case( db.case(
[(db.func.sign(Operation.value) == 1, [(db.func.sign(Operation.value) == 1, Operation.value)],
Operation.value)],
else_=0 else_=0
) )
).label("revenues"), ).label("revenues"),

View File

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