Account pay functional
This commit is contained in:
parent
6413749d76
commit
b02994d041
@ -179,3 +179,25 @@ void Database::earn(const std::string &account, long double &value, std::string
|
|||||||
if (rc != SQLITE_DONE)
|
if (rc != SQLITE_DONE)
|
||||||
throw std::runtime_error("Failed to create earning");
|
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");
|
||||||
|
}
|
@ -101,6 +101,8 @@ public:
|
|||||||
* @param date The date of the earning in UNIX time
|
* @param date The date of the earning in UNIX time
|
||||||
* @param db The sqlite3 database connection
|
* @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.
|
* 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.
|
* 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.
|
* 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,
|
earn(const std::string &account, long double &value, std::string &description, std::string &receipt,
|
||||||
long long int date,
|
long long int date,
|
||||||
sqlite3 *db);
|
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,11 +3,16 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include "PaymentOperation.h"
|
#include "PaymentOperation.h"
|
||||||
|
#include "../database.h"
|
||||||
|
#include "../exceptions/badValue.h"
|
||||||
|
|
||||||
using namespace Budget::OptHandlers;
|
using namespace Budget::OptHandlers;
|
||||||
|
|
||||||
void PaymentOperation::commit() {
|
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)) {}
|
PaymentOperation::PaymentOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {}
|
Loading…
x
Reference in New Issue
Block a user