def get(self): user = users.get_current_user() if user is None: self.redirect(users.create_login_url(self.request.uri)) return dtnow = datetime.utcnow() try: month = int(self.request.get('mois')) year = int(self.request.get('annee')) except: month = dtnow.month year = dtnow.year users_expenses = UserExpenses.get_user_expenses(month, year) #months = date_helper.get_all_months() months = range(1, 13) # for the moment the status is only computed when there are only 2 users status = {'maxuser': user, 'minuser': users_helper.get_other_nickname(), 'debt': 0} if len(users_expenses) == 2: status = UserExpenses.compute_balance(users_expenses, month, year) logging.info(status) #month_name = months[month-1]['Name'] template_values = { 'logouturl': users.create_logout_url("/"), 'currentuser': users.get_current_user().nickname(), 'selectedmonth': month, #'selectedmonthname': month_name, 'selectedyear': year, 'month_range': months, 'year_range': range(2009, dtnow.year + 1), 'usersexpenses': users_expenses, 'status_maxuser': status['maxuser'], 'status_minuser': status['minuser'], 'status_debt': '%.2f' % status['debt'], 'nextmonth': date_helper.get_next_month(month), 'nextyear': date_helper.get_year_of_next_month(month, year), 'previousmonth': date_helper.get_previous_month(month), 'previousyear': date_helper.get_year_of_previous_month(month, year) } #session["monthname"] = months[month-1]['Name'] path = os.path.join(os.path.dirname(__file__), '../templates/index.html') cookie_value = "%d#%d"%(month, year) logging.info("_balanceSession = "+ cookie_value) self.response.headers.add_header('Set-Cookie', '_balanceSession=' + cookie_value + ';path=/;max-age=' + str(7*24*3600) + ';') self.response.out.write(template.render(path, template_values))
def compute_balance(cls, users_expenses, month, year): if len(users_expenses) == 0: # nothing to do return user_a = users_expenses[0] if len(users_expenses) == 2: user_b = users_expenses[1] else: user_b = UserExpenses(users_helper.get_other_nickname()) debt = (user_a.totalcommon - user_b.totalcommon) / 2 + user_a.totaladvance - user_b.totaladvance if user_a.totaltax != 0.0 or user_b.totaltax != 0.0: debt += cls.compute_tax_balance(user_a, user_b, year) if debt > 0: min_user = user_b max_user = user_a else: debt = -debt min_user = user_a max_user = user_b # update the balance in DataStore query = Balance.query(Balance.month == month, Balance.year == year) balance = query.get() if balance is None: balance = Balance() balance.year = year balance.month = month balance.debtor = min_user.user balance.incredit = max_user.user balance.amount = debt balance.put() return {'year': year, 'month': month, 'maxuser': max_user.user, 'minuser': min_user.user, 'debt': debt}
def update_tax_ratio(self, ratio, year, user): query = TaxRatio.query(TaxRatio.year == year) update = False for taxratio in query.fetch(2): if taxratio.user == user: taxratio.ratio = ratio else: taxratio.ratio = 1 - ratio logging.debug("saving ratio %s for user %s" % (str(taxratio), user)) taxratio.put() update = True if not update: taxratioa = TaxRatio(user=user, ratio=ratio, year=year) logging.debug("saving %s" % str(taxratioa)) taxratioa.put() taxratiob = TaxRatio(user=users_helper.get_other_nickname(), ratio=1 - ratio, year=year) logging.debug("saving %s" % str(taxratiob)) taxratiob.put() else: # update balance with this new tax ratio for month in range(1, 12): user_expenses = UserExpenses.get_user_expenses(month, year) UserExpenses.compute_balance(user_expenses, month, year)