Account pay functional

This commit is contained in:
Quentin Snow 2023-01-27 20:47:33 -06:00
parent 6413749d76
commit b02994d041
3 changed files with 51 additions and 1 deletions

View File

@ -179,3 +179,25 @@ void Database::earn(const std::string &account, long double &value, std::string
if (rc != SQLITE_DONE)
throw std::runtime_error("Failed to create earning");
}
void Database::pay(const std::string &account, long double &value, std::string &description, std::string &receipt,
long long date, sqlite3 *db) {
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, "INSERT INTO payment (value, description, receipt, accountId, date) VALUES "
"(?, ?, ?, (SELECT id FROM account WHERE name = ?), ?)", -1, &stmt, nullptr);
if (rc != SQLITE_OK)
throw std::runtime_error("Failed preparing pay statement");
sqlite3_bind_double(stmt, 1, value);
sqlite3_bind_text(stmt, 2, (description.empty() ? nullptr : description.c_str()), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 3, (receipt.empty() ? nullptr : receipt.c_str()), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 4, (account.empty() ? nullptr : account.c_str()), -1, SQLITE_TRANSIENT);
sqlite3_bind_int64(stmt, 5, date);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE)
throw std::runtime_error("Failed to create payment");
}

View File

@ -101,6 +101,8 @@ public:
* @param date The date of the earning in UNIX time
* @param db The sqlite3 database connection
*
* @throws std::runtime_error If the earning could not be created in the database
*
* This function records an earning in the database by inserting a new row in the 'earning' table with the provided information.
* The accountId is retrieved from the 'account' table using the provided account name.
* If the query fails to prepare or execute, the function throws a runtime_error.
@ -109,6 +111,27 @@ public:
earn(const std::string &account, long double &value, std::string &description, std::string &receipt,
long long int date,
sqlite3 *db);
/**
* @brief The function records an payment in the database
*
* @param account The name of the account to record the payment in
* @param value The value of the payment
* @param description A description of the payment, can be an empty string
* @param receipt A receipt of the payment, can be an empty string
* @param date The date of the payment in UNIX time
* @param db The sqlite3 database connection
*
* @throws std::runtime_error If the payment could not be created in the database
*
* This function records an payment in the database by inserting a new row in the 'payment' table with the provided information.
* The accountId is retrieved from the 'account' table using the provided account name.
* If the query fails to prepare or execute, the function throws a runtime_error.
*/
static void
pay(const std::string &account, long double &value, std::string &description, std::string &receipt,
long long int date,
sqlite3 *db);
};

View File

@ -3,11 +3,16 @@
//
#include "PaymentOperation.h"
#include "../database.h"
#include "../exceptions/badValue.h"
using namespace Budget::OptHandlers;
void PaymentOperation::commit() {
//TODO This function will be called when the action needs to be done
if (!Database::doesAccountExist(account, db))
throw Budget::Exceptions::BadValue("Account " + account + " doesn't exist");
Database::pay(account, flags.value, flags.description, flags.receipt, flags.date, db);
}
PaymentOperation::PaymentOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {}