Пример #1
0
def route_edit_expense(expense_id, budget_id, date_incurred, amount, description, payee_id, new_payee):
  """Changes the given expense."""

  expense = helpers.get_expense(expense_id)
  if expense is None:
    flask.abort(http.client.NOT_FOUND)

  existing_payment = expense["payment_id"] is not None
  valid_expense = helpers.validate_expense(budget_id, date_incurred, amount,
      description)
  amount_unchanged = expense["cost"] == Decimal(amount)
  payee_unchanged = expense["payee_id"] == int(payee_id)

  # Can't change payment info if there's a linked payment
  # TODO soften this so debit purchaes aren't a PITA

  errs = helpers.test_predicates((
    (valid_expense, True, "Invalid expense."),
    (amount_unchanged,  existing_payment, "Can't change amount with linked payment."),
    (payee_unchanged,  existing_payment, "Can't change payee with linked payment."),
  ))

  if errs:
    return flask.redirect(flask.url_for("budget.route_show_expense", expense_id=expense_id))

  success = helpers.edit_expense(
    expense_id, budget_id, date_incurred, description, amount, payee_id
  )
  if success:
    flask.flash("Success!")
  else:
    flask.flash("Something went wrong during the edit, not sure what.")

  return flask.redirect(flask.url_for("budget.route_show_expense", expense_id=expense_id))
Пример #2
0
def route_delete_expense(expense_id, budget_id, date_incurred, amount,
                         description, payee_id, new_payee):
    """
  Deletes the given expense.
  If there is a linked payment, deletes that also.
  """

    expense = helpers.get_expense(expense_id)
    if expense is None:
        flask.abort(http.client.NOT_FOUND)

    helpers.delete_expense(expense_id)
    if expense["payment_id"] is not None:
        helpers.delete_payment(expense["payment_id"])

    flask.flash("Success!")
    return flask.redirect(flask.url_for("budget.route_expenses"))
Пример #3
0
def route_show_expense(expense_id):
    """
  Displays an expense and allows you to edit it.
  """

    expense = helpers.get_expense(expense_id)

    if expense is None:
        flask.abort(http.client.NOT_FOUND)

    budgets_list = helpers.get_budget_list(expense["fyear_id"])
    payment_types = PaymentType.get_all()
    payees = helpers.get_payees()

    return flask.render_template('edit_expense.html',
                                 expense=expense,
                                 budgets=budgets_list,
                                 payment_types=payment_types,
                                 payees=payees)
Пример #4
0
def route_delete_expense(expense_id, budget_id, date_incurred, amount, description, payee_id, new_payee):
  """Deletes the given expense."""

  expense = helpers.get_expense(expense_id)
  if expense is None:
    flask.abort(http.client.NOT_FOUND)

  existing_payment = expense["payment_id"] is not None

  # Can't delete if there's a linked payment
  # TODO soften this so debit purchaes aren't a PITA

  if existing_payment:
    flask.flash("Cannot delete expense if there is a linked payment.")
    return flask.redirect(flask.url_for("budget.route_show_expense", expense_id=expense_id))

  helpers.delete_expense(expense_id)

  flask.flash("Success!")
  return flask.redirect(flask.url_for("budget.route_expenses"))
Пример #5
0
def route_show_expense(expense_id):
  """
  Displays an expense and allows you to edit it.
  """

  expense = helpers.get_expense(expense_id)

  if expense is None:
    flask.abort(http.client.NOT_FOUND)


  budgets_list = helpers.get_budget_list(expense["fyear_id"])
  payment_types = PaymentType.get_all()
  payees = helpers.get_payees()

  return flask.render_template(
    'edit_expense.html',
    expense=expense,
    budgets=budgets_list,
    payment_types=payment_types,
    payees=payees
  )
Пример #6
0
def route_edit_expense(expense_id, budget_id, date_incurred, amount,
                       description, payee_id, new_payee, payment_type):
    """Changes the given expense."""

    expense = helpers.get_expense(expense_id)
    if expense is None:
        flask.abort(http.client.NOT_FOUND)

    existing_payment = expense["payment_id"] is not None
    valid_expense = helpers.validate_expense(budget_id, date_incurred, amount,
                                             description)

    if not valid_expense:
        return flask.redirect(
            flask.url_for("budget.route_show_expense", expense_id=expense_id))

    if payee_id == '':
        payee_id = None

    success = helpers.edit_expense(expense_id, budget_id, date_incurred,
                                   description, amount, payee_id)
    if success:
        flask.flash("Success!")
    else:
        flask.flash("Something went wrong during the edit, not sure what.")

    if payment_type == '':
        payment = helpers.get_payment(expense["payment_id"])
        payment_type = payment["payment_type"]

    if existing_payment:
        helpers.edit_payment(expense["payment_id"], Decimal(amount),
                             date_incurred, payee_id, payment_type)

    return flask.redirect(
        flask.url_for("budget.route_show_expense", expense_id=expense_id))