def route_process_check(payment_id, date_posted, action): """Records a check as deposited.""" # Server side validation unposted_ids = [str(x["payment_id"]) for x in helpers.get_unposted_payments()] errs = helpers.test_predicates(( (payment_id in unposted_ids, True, "Not a valid payment ID!"), (date_posted != "", action == "Post", "No date entered!" ) )) # If any of the validation failed if errs: return flask.redirect(flask.url_for("budget.route_checks")) # Decide what to do if action == "Post": helpers.post_payment(payment_id, date_posted) flask.flash("Payment successfully posted!") elif action == "Void": helpers.void_payment(payment_id) flask.flash("Payment successfully voided!") else: flask.flash("Not a legitimate action!") return flask.redirect(flask.url_for("budget.route_checks"))
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))
def route_submit_expense(budget_id, date_incurred, amount, description, payee_id, new_payee, payment_type, account_id, check_no, defer_payment): """Sends the expense to the database.""" # Checkboxes aren't sent as bools... :( defer_payment = defer_payment is not None # Server side validation valid_expense = helpers.validate_expense(budget_id, date_incurred, amount, description) valid_payment = helpers.validate_payment(payment_type, account_id, check_no) valid_payee = helpers.validate_payee(payee_id, new_payee) errs = helpers.test_predicates(( (valid_expense, True, "Invalid expense."), (valid_payment, not defer_payment, "Invalid payment."), (valid_payee, defer_payment, "Invalid payee." ) )) if errs: return flask.redirect(flask.url_for("budget.route_add_expense")) transaction = flask.g.db.begin() try: # This next part depends on whether we are deferring payment or not. # If so, then we leave the payment ID null, cause it the corresponding # payment hasn't been made yet. However, we require the payee. If it's a new # payee, we have to insert it into the database first. # If not, then we need to make a new payment, and use its ID. Payee ID is # not needed. if defer_payment: payee_id = payee_id or helpers.record_new_payee(new_payee) payment_id = None else: date_written = date_incurred # non-deferred payments are instant date_posted = None if payment_type == PaymentType.CHECK.value else date_written payee_id = None payment_id = helpers.record_payment( account_id, payment_type, amount, date_written, date_posted, payee_id, check_no) # Either way, record the expense helpers.record_expense( budget_id, date_incurred, description, amount, payment_id, payee_id) transaction.commit() flask.flash("Expense recorded successfully!") except Exception: transaction.rollback() flask.flash("An unexpected error occurred. Please find an IMSS rep.") return flask.redirect(flask.url_for("budget.route_add_expense"))
def route_submit_expense(budget_id, date_incurred, amount, description, payee_id, new_payee, payment_type, account_id): """Sends the expense to the database.""" defer_payment = False # deprecated feature check_no = "0" # deprecated feature # Server side validation valid_expense = helpers.validate_expense(budget_id, date_incurred, amount, description) valid_payment = helpers.validate_payment(payment_type, account_id, check_no) valid_payee = helpers.validate_payee(payee_id, new_payee) errs = helpers.test_predicates( ((valid_expense, True, "Invalid expense."), (valid_payment, not defer_payment, "Invalid payment."), (valid_payee, defer_payment, "Invalid payee."))) if errs: return flask.redirect(flask.url_for("budget.route_add_expense")) transaction = flask.g.db.begin() try: date_written = date_incurred date_posted = None if payment_type == PaymentType.CHECK.value else date_written payee_id = None payment_id = helpers.record_payment(account_id, payment_type, amount, date_written, date_posted, payee_id, check_no) # Record the expense helpers.record_expense(budget_id, date_incurred, description, amount, payment_id, payee_id) transaction.commit() flask.flash("Expense recorded successfully!") except Exception: transaction.rollback() flask.flash("An unexpected error occurred. Please find an IMSS rep.") return flask.redirect(flask.url_for("budget.route_add_expense"))
def route_submit_unpaid(payee_id, payment_type, account_id, check_no, date_written): """Sends the payment to the database.""" # The date posted is the same as the date written unless we're using a check date_posted = None if ( payment_type != PaymentType.CHECK.value) else date_written # Server side validation total = helpers.get_unpaid_amount(payee_id) valid_payment = helpers.validate_payment(payment_type, account_id, check_no) has_expenses = (total is not None) errs = helpers.test_predicates(( (valid_payment, True, "Invalid payment."), (has_expenses, True, "This payee has no expenses to reimburse."), )) if errs: return flask.redirect(flask.url_for("budget.route_unpaid")) # We use a transaction to make sure we don't submit halfway. transaction = flask.g.db.begin() try: payment_id = helpers.record_payment(account_id, payment_type, total, date_written, date_posted, payee_id, check_no) helpers.mark_as_paid(payee_id, payment_id) transaction.commit() flask.flash("Payment recorded successfully!") except Exception: transaction.rollback() flask.flash("An unexpected error occurred. Please find an IMSS rep.") return flask.redirect(flask.url_for("budget.route_unpaid"))
def route_submit_unpaid(payee_id, payment_type, account_id, check_no, date_written): """Sends the payment to the database.""" # The date posted is the same as the date written unless we're using a check date_posted = None if (payment_type != PaymentType.CHECK.value) else date_written # Server side validation total = helpers.get_unpaid_amount(payee_id) valid_payment = helpers.validate_payment(payment_type, account_id, check_no) has_expenses = (total is not None) errs = helpers.test_predicates(( (valid_payment, True, "Invalid payment."), (has_expenses, True, "This payee has no expenses to reimburse."), )) if errs: return flask.redirect(flask.url_for("budget.route_unpaid")) # We use a transaction to make sure we don't submit halfway. transaction = flask.g.db.begin() try: payment_id = helpers.record_payment( account_id, payment_type, total, date_written, date_posted, payee_id, check_no) helpers.mark_as_paid(payee_id, payment_id) transaction.commit() flask.flash("Payment recorded successfully!") except Exception: transaction.rollback() flask.flash("An unexpected error occurred. Please find an IMSS rep.") return flask.redirect(flask.url_for("budget.route_unpaid"))