settings implemented

This commit is contained in:
Martin Brodbeck 2018-07-30 13:40:58 +02:00
parent 06c99052df
commit 9e85e2892d
9 changed files with 284 additions and 2 deletions

View file

@ -107,7 +107,7 @@ void Database::init()
if (errCode) {
throw std::runtime_error("Could not open database file.");
}
// sqlite3_db_config(db_, SQLITE_DBCONFIG_ENABLE_FKEY);
exec("PRAGMA foreign_keys = ON;");
@ -644,3 +644,73 @@ unsigned int Database::loadSales(std::vector<std::unique_ptr<Sale>>& sales,
return count;
}
void Database::updateCashPointNo(int oldCashPointNo, int newCashPointNo)
{
int retCode{};
sqlite3_stmt* stmt;
// Check if the new no ist already in use
retCode = sqlite3_prepare_v2(db_, "SELECT COUNT() FROM articles WHERE source_no = :source_no",
-1, &stmt, nullptr);
if (retCode != SQLITE_OK)
throw std::string(sqlite3_errmsg(db_));
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":source_no"), newCashPointNo);
retCode = sqlite3_step(stmt);
int count{};
if (retCode != SQLITE_ROW) {
std::string errMsg(sqlite3_errmsg(db_));
sqlite3_finalize(stmt);
throw std::runtime_error(errMsg);
}
count = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
if (count > 0) {
throw std::runtime_error("The desired cashpoint no is aleady in use.");
}
beginTransaction();
retCode = sqlite3_prepare_v2(
db_, "UPDATE articles SET source_no = :new_source_no WHERE source_no = :old_source_no", -1,
&stmt, nullptr);
if (retCode != SQLITE_OK)
throw std::string(sqlite3_errmsg(db_));
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":new_source_no"), newCashPointNo);
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":old_source_no"), oldCashPointNo);
retCode = sqlite3_step(stmt);
if (retCode != SQLITE_DONE) {
std::string errMsg(sqlite3_errmsg(db_));
sqlite3_finalize(stmt);
throw std::runtime_error(errMsg);
}
sqlite3_finalize(stmt);
retCode = sqlite3_prepare_v2(
db_, "UPDATE sales SET source_no = :new_source_no WHERE source_no = :old_source_no", -1,
&stmt, nullptr);
if (retCode != SQLITE_OK)
throw std::string(sqlite3_errmsg(db_));
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":new_source_no"), newCashPointNo);
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":old_source_no"), oldCashPointNo);
retCode = sqlite3_step(stmt);
if (retCode != SQLITE_DONE) {
std::string errMsg(sqlite3_errmsg(db_));
sqlite3_finalize(stmt);
throw std::runtime_error(errMsg);
}
sqlite3_finalize(stmt);
endTransaction();
}

View file

@ -23,6 +23,7 @@ class Database
unsigned int storeSales(std::vector<std::unique_ptr<Sale>>& sales);
unsigned int loadSales(std::vector<std::unique_ptr<Sale>>& sales,
std::vector<std::unique_ptr<Seller>>& sellers);
void updateCashPointNo(int oldCashPointNo, int newCashPointNo);
private:
sqlite3* db_{nullptr};

View file

@ -139,6 +139,9 @@ void Marketplace::removeSale(boost::uuids::uuid uuid)
double marketFee(int sum, int percent, int maxFee)
{
int fee = (sum * percent) / 100.0L;
if (maxFee <= 0) {
return fee;
}
return fee > maxFee ? maxFee : fee;
}