code formatting

This commit is contained in:
Martin Brodbeck 2022-07-07 15:21:46 +02:00
parent d677dfd628
commit acc3095e60
23 changed files with 215 additions and 215 deletions

View file

@ -11,18 +11,18 @@ void Article::setArticleNo(int articleNo) { m_articleNo = articleNo; }
void Article::setPrice(int price) { m_price = price; } void Article::setPrice(int price) { m_price = price; }
void Article::setDescription(const std::string& description) { m_description = description; } void Article::setDescription(const std::string &description) { m_description = description; }
void Article::setSale(Sale* salePtr) { m_salePtr = salePtr; } void Article::setSale(Sale *salePtr) { m_salePtr = salePtr; }
void Article::setSeller(Seller* sellerPtr) { m_sellerPtr = sellerPtr; } void Article::setSeller(Seller *sellerPtr) { m_sellerPtr = sellerPtr; }
bool Article::isSold() { return m_salePtr ? true : false; } bool Article::isSold() { return m_salePtr ? true : false; }
std::string Article::getDescription() { return m_description; } std::string Article::getDescription() { return m_description; }
Seller* Article::getSeller() { return m_sellerPtr; } Seller *Article::getSeller() { return m_sellerPtr; }
Sale* Article::getSale() { return m_salePtr; } Sale *Article::getSale() { return m_salePtr; }
int Article::getPrice() const { return m_price; } int Article::getPrice() const { return m_price; }

View file

@ -11,30 +11,30 @@ class Sale;
class Article : public EntityUuid class Article : public EntityUuid
{ {
public: public:
Article() = default; Article() = default;
Article(int price); Article(int price);
Article(const Article&) = delete; Article(const Article &) = delete;
virtual ~Article() = default; virtual ~Article() = default;
void setArticleNo(int articleNo); void setArticleNo(int articleNo);
void setPrice(int price); void setPrice(int price);
void setDescription(const std::string& description); void setDescription(const std::string &description);
bool isSold(); bool isSold();
void setSale(Sale* salePtr); void setSale(Sale *salePtr);
void setSeller(Seller* sellerPtr); void setSeller(Seller *sellerPtr);
int getArticleNo() const; int getArticleNo() const;
std::string getCompleteArticleNo() const; std::string getCompleteArticleNo() const;
std::string getDescription(); std::string getDescription();
Seller* getSeller(); Seller *getSeller();
Sale* getSale(); Sale *getSale();
int getPrice() const; int getPrice() const;
std::string getPriceAsString() const; std::string getPriceAsString() const;
private: private:
Seller* m_sellerPtr{}; Seller *m_sellerPtr{};
Sale* m_salePtr{}; Sale *m_salePtr{};
int m_articleNo{}; int m_articleNo{};
int m_price{}; int m_price{};
std::string m_description{}; std::string m_description{};

View file

@ -11,7 +11,7 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
std::size_t CsvReader::readSellersFromFile(const fs::path& filePath, Marketplace* market) std::size_t CsvReader::readSellersFromFile(const fs::path &filePath, Marketplace *market)
{ {
csv::CSVFormat format; csv::CSVFormat format;
format.delimiter(';'); format.delimiter(';');
@ -26,7 +26,7 @@ std::size_t CsvReader::readSellersFromFile(const fs::path& filePath, Marketplace
csv::CSVReader csvReader(filePath.string(), format); csv::CSVReader csvReader(filePath.string(), format);
#endif #endif
for (auto& seller : market->getSellers()) { for (auto &seller : market->getSellers()) {
seller->setState(Seller::State::DELETE); seller->setState(Seller::State::DELETE);
} }

View file

@ -11,9 +11,9 @@
class CsvReader class CsvReader
{ {
public: public:
static std::size_t readSellersFromFile(const std::filesystem::path& filePath, static std::size_t readSellersFromFile(const std::filesystem::path &filePath,
Marketplace* market); Marketplace *market);
}; };
#endif #endif

View file

@ -7,7 +7,7 @@
#include "boost/date_time/posix_time/posix_time.hpp" #include "boost/date_time/posix_time/posix_time.hpp"
Database::Database(const std::string& dbname) Database::Database(const std::string &dbname)
{ {
dbname_ = dbname; dbname_ = dbname;
init(); init();
@ -28,7 +28,7 @@ Database::Database()
if (!fs::exists(dbpath)) { if (!fs::exists(dbpath)) {
try { try {
fs::create_directories(dbpath); fs::create_directories(dbpath);
} catch (fs::filesystem_error& err) { } catch (fs::filesystem_error &err) {
throw err; throw err;
} }
} }
@ -57,9 +57,9 @@ void Database::newDb()
Database::~Database() { sqlite3_close(db_); } Database::~Database() { sqlite3_close(db_); }
void Database::exec(const std::string& sql) void Database::exec(const std::string &sql)
{ {
char* errMsg; char *errMsg;
const int errCode = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &errMsg); const int errCode = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &errMsg);
if (errCode) { if (errCode) {
std::string errMsgString(errMsg); // Make a C++ string of the errMsg, so that we can call std::string errMsgString(errMsg); // Make a C++ string of the errMsg, so that we can call
@ -120,7 +120,7 @@ void Database::createNew()
sqlStrings.push_back(sqlInitialEntries); sqlStrings.push_back(sqlInitialEntries);
beginTransaction(); beginTransaction();
for (const auto& sql : sqlStrings) { for (const auto &sql : sqlStrings) {
exec(sql); exec(sql);
} }
endTransaction(); endTransaction();
@ -173,7 +173,7 @@ void Database::init()
int Database::getVersion() int Database::getVersion()
{ {
int retCode{}; int retCode{};
sqlite3_stmt* stmt; sqlite3_stmt *stmt;
// Check if there's already a kima2 table available. // Check if there's already a kima2 table available.
// If not, return version == 0. // If not, return version == 0.
@ -220,15 +220,15 @@ void Database::beginTransaction() { exec("BEGIN TRANSACTION"); }
void Database::endTransaction() { exec("END TRANSACTION"); } void Database::endTransaction() { exec("END TRANSACTION"); }
unsigned int Database::storeSellers(std::vector<std::unique_ptr<Seller>>& sellers, bool onlyDelete) unsigned int Database::storeSellers(std::vector<std::unique_ptr<Seller>> &sellers, bool onlyDelete)
{ {
int retCode{}; int retCode{};
int count{}; int count{};
sqlite3_stmt* stmt; sqlite3_stmt *stmt;
beginTransaction(); beginTransaction();
for (auto& seller : sellers) { for (auto &seller : sellers) {
if (seller->getState() == Seller::State::NEW && !onlyDelete) { if (seller->getState() == Seller::State::NEW && !onlyDelete) {
retCode = sqlite3_prepare_v2( retCode = sqlite3_prepare_v2(
db_, db_,
@ -323,12 +323,12 @@ unsigned int Database::storeSellers(std::vector<std::unique_ptr<Seller>>& seller
// Everything went fine, so we can now update our objects // Everything went fine, so we can now update our objects
sellers.erase(std::remove_if(sellers.begin(), sellers.end(), sellers.erase(std::remove_if(sellers.begin(), sellers.end(),
[](const std::unique_ptr<Seller>& seller) { [](const std::unique_ptr<Seller> &seller) {
return (seller->getState() == Seller::State::DELETE); return (seller->getState() == Seller::State::DELETE);
}), }),
sellers.end()); sellers.end());
for (auto& seller : sellers) { for (auto &seller : sellers) {
seller->cleanupArticles(); seller->cleanupArticles();
seller->setState(Seller::State::OK); seller->setState(Seller::State::OK);
} }
@ -336,13 +336,13 @@ unsigned int Database::storeSellers(std::vector<std::unique_ptr<Seller>>& seller
return count; return count;
} }
unsigned int Database::storeArticles(std::vector<Article*> articles) unsigned int Database::storeArticles(std::vector<Article *> articles)
{ {
int retCode{}; int retCode{};
int count{}; int count{};
sqlite3_stmt* stmt; sqlite3_stmt *stmt;
for (auto& article : articles) { for (auto &article : articles) {
if (article->getState() == Article::State::NEW) { if (article->getState() == Article::State::NEW) {
retCode = sqlite3_prepare_v2( retCode = sqlite3_prepare_v2(
db_, db_,
@ -441,18 +441,18 @@ unsigned int Database::storeArticles(std::vector<Article*> articles)
return count; return count;
} }
unsigned int Database::storeSales(std::vector<std::unique_ptr<Sale>>& sales) unsigned int Database::storeSales(std::vector<std::unique_ptr<Sale>> &sales)
{ {
int retCode{}; int retCode{};
int count{}; int count{};
sqlite3_stmt* stmt; sqlite3_stmt *stmt;
if (sales.size() == 0) if (sales.size() == 0)
return 0; return 0;
beginTransaction(); beginTransaction();
for (auto& sale : sales) { for (auto &sale : sales) {
if (sale->getState() == Sale::State::NEW) { if (sale->getState() == Sale::State::NEW) {
retCode = sqlite3_prepare_v2(db_, retCode = sqlite3_prepare_v2(db_,
"INSERT INTO sales" "INSERT INTO sales"
@ -482,7 +482,7 @@ unsigned int Database::storeSales(std::vector<std::unique_ptr<Sale>>& sales)
++count; ++count;
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
for (const auto& article : sale->getArticles()) { for (const auto &article : sale->getArticles()) {
retCode = sqlite3_prepare_v2(db_, retCode = sqlite3_prepare_v2(db_,
"INSERT INTO sales_items" "INSERT INTO sales_items"
" (sale_id, article_id)" " (sale_id, article_id)"
@ -535,21 +535,21 @@ unsigned int Database::storeSales(std::vector<std::unique_ptr<Sale>>& sales)
// Everything went fine, so we can now update our objects // Everything went fine, so we can now update our objects
sales.erase( sales.erase(
std::remove_if(sales.begin(), sales.end(), std::remove_if(sales.begin(), sales.end(),
[](const auto& sale) { return (sale->getState() == Sale::State::DELETE); }), [](const auto &sale) { return (sale->getState() == Sale::State::DELETE); }),
sales.end()); sales.end());
for (auto& sale : sales) { for (auto &sale : sales) {
sale->setState(Sale::State::OK); sale->setState(Sale::State::OK);
} }
return count; return count;
} }
unsigned int Database::loadSellers(std::vector<std::unique_ptr<Seller>>& sellers) unsigned int Database::loadSellers(std::vector<std::unique_ptr<Seller>> &sellers)
{ {
int retCode{}; int retCode{};
int count{}; int count{};
sqlite3_stmt* stmt; sqlite3_stmt *stmt;
retCode = sqlite3_prepare_v2(db_, retCode = sqlite3_prepare_v2(db_,
"SELECT seller_no, first_name, last_name, " "SELECT seller_no, first_name, last_name, "
@ -566,8 +566,8 @@ unsigned int Database::loadSellers(std::vector<std::unique_ptr<Seller>>& sellers
++count; ++count;
auto seller = std::make_unique<Seller>(); auto seller = std::make_unique<Seller>();
seller->setSellerNo(sqlite3_column_int(stmt, 0)); seller->setSellerNo(sqlite3_column_int(stmt, 0));
seller->setFirstName(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1))); seller->setFirstName(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)));
seller->setLastName(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2))); seller->setLastName(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 2)));
seller->setNumArticlesOffered(sqlite3_column_int(stmt, 3)); seller->setNumArticlesOffered(sqlite3_column_int(stmt, 3));
seller->setState(Seller::State::OK); seller->setState(Seller::State::OK);
sellers.push_back(std::move(seller)); sellers.push_back(std::move(seller));
@ -577,7 +577,7 @@ unsigned int Database::loadSellers(std::vector<std::unique_ptr<Seller>>& sellers
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
for (auto& seller : sellers) { for (auto &seller : sellers) {
retCode = sqlite3_prepare_v2(db_, retCode = sqlite3_prepare_v2(db_,
"SELECT id, source_no, article_no, description, price" "SELECT id, source_no, article_no, description, price"
" FROM articles" " FROM articles"
@ -594,11 +594,12 @@ unsigned int Database::loadSellers(std::vector<std::unique_ptr<Seller>>& sellers
while (retCode != SQLITE_DONE) { while (retCode != SQLITE_DONE) {
++count; ++count;
auto article = std::make_unique<Article>(); auto article = std::make_unique<Article>();
article->setUuidFromString(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0))); article->setUuidFromString(
reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)));
article->setSeller(seller.get()); article->setSeller(seller.get());
article->setSourceNo(sqlite3_column_int(stmt, 1)); article->setSourceNo(sqlite3_column_int(stmt, 1));
article->setArticleNo(sqlite3_column_int(stmt, 2)); article->setArticleNo(sqlite3_column_int(stmt, 2));
article->setDescription(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3))); article->setDescription(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 3)));
article->setPrice(sqlite3_column_int(stmt, 4)); article->setPrice(sqlite3_column_int(stmt, 4));
article->setState(Article::State::OK); article->setState(Article::State::OK);
@ -613,12 +614,12 @@ unsigned int Database::loadSellers(std::vector<std::unique_ptr<Seller>>& sellers
return count; return count;
} }
unsigned int Database::loadSales(std::vector<std::unique_ptr<Sale>>& sales, unsigned int Database::loadSales(std::vector<std::unique_ptr<Sale>> &sales,
std::vector<std::unique_ptr<Seller>>& sellers) std::vector<std::unique_ptr<Seller>> &sellers)
{ {
int retCode{}; int retCode{};
int count{}; int count{};
sqlite3_stmt* stmt; sqlite3_stmt *stmt;
retCode = sqlite3_prepare_v2(db_, retCode = sqlite3_prepare_v2(db_,
"SELECT id, source_no, sold_at" "SELECT id, source_no, sold_at"
@ -631,13 +632,13 @@ unsigned int Database::loadSales(std::vector<std::unique_ptr<Sale>>& sales,
sales.clear(); sales.clear();
std::map<std::string, Sale*> saleMap; std::map<std::string, Sale *> saleMap;
while (retCode != SQLITE_DONE) { while (retCode != SQLITE_DONE) {
++count; ++count;
auto sale = std::make_unique<Sale>(); auto sale = std::make_unique<Sale>();
sale->setUuidFromString(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0))); sale->setUuidFromString(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)));
sale->setSourceNo(sqlite3_column_int(stmt, 1)); sale->setSourceNo(sqlite3_column_int(stmt, 1));
sale->setTimestamp(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2))); sale->setTimestamp(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 2)));
sale->setState(Sale::State::OK); sale->setState(Sale::State::OK);
saleMap.insert(std::make_pair(sale->getUuidAsString(), sale.get())); saleMap.insert(std::make_pair(sale->getUuidAsString(), sale.get()));
sales.push_back(std::move(sale)); sales.push_back(std::move(sale));
@ -647,8 +648,8 @@ unsigned int Database::loadSales(std::vector<std::unique_ptr<Sale>>& sales,
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
std::map<std::string, Article*> artMap; std::map<std::string, Article *> artMap;
for (const auto& seller : sellers) { for (const auto &seller : sellers) {
for (const auto article : seller->getArticles(false)) { for (const auto article : seller->getArticles(false)) {
artMap.insert(std::make_pair(article->getUuidAsString(), article)); artMap.insert(std::make_pair(article->getUuidAsString(), article));
} }
@ -664,8 +665,8 @@ unsigned int Database::loadSales(std::vector<std::unique_ptr<Sale>>& sales,
retCode = sqlite3_step(stmt); retCode = sqlite3_step(stmt);
while (retCode != SQLITE_DONE) { while (retCode != SQLITE_DONE) {
saleMap[reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0))]->addArticle( saleMap[reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0))]->addArticle(
artMap[reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1))]); artMap[reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1))]);
retCode = sqlite3_step(stmt); retCode = sqlite3_step(stmt);
} }
@ -678,7 +679,7 @@ unsigned int Database::loadSales(std::vector<std::unique_ptr<Sale>>& sales,
void Database::updateCashPointNo(int oldCashPointNo, int newCashPointNo) void Database::updateCashPointNo(int oldCashPointNo, int newCashPointNo)
{ {
int retCode{}; int retCode{};
sqlite3_stmt* stmt; sqlite3_stmt *stmt;
// Check if the new no ist already in use // Check if the new no ist already in use
retCode = sqlite3_prepare_v2(db_, "SELECT COUNT() FROM articles WHERE source_no = :source_no", retCode = sqlite3_prepare_v2(db_, "SELECT COUNT() FROM articles WHERE source_no = :source_no",

View file

@ -10,33 +10,33 @@
class Database class Database
{ {
public: public:
enum class InitResult {OK, OUTDATED_REPLACED}; enum class InitResult { OK, OUTDATED_REPLACED };
explicit Database(const std::string& dbname); explicit Database(const std::string &dbname);
Database(); Database();
~Database(); ~Database();
Database(const Database&) = delete; Database(const Database &) = delete;
Database& operator=(const Database&) = delete; Database &operator=(const Database &) = delete;
void exec(const std::string& sql); void exec(const std::string &sql);
unsigned int storeSellers(std::vector<std::unique_ptr<Seller>>& sellers, unsigned int storeSellers(std::vector<std::unique_ptr<Seller>> &sellers,
bool onlyDelete = false); bool onlyDelete = false);
unsigned int loadSellers(std::vector<std::unique_ptr<Seller>>& sellers); unsigned int loadSellers(std::vector<std::unique_ptr<Seller>> &sellers);
unsigned int storeSales(std::vector<std::unique_ptr<Sale>>& sales); unsigned int storeSales(std::vector<std::unique_ptr<Sale>> &sales);
unsigned int loadSales(std::vector<std::unique_ptr<Sale>>& sales, unsigned int loadSales(std::vector<std::unique_ptr<Sale>> &sales,
std::vector<std::unique_ptr<Seller>>& sellers); std::vector<std::unique_ptr<Seller>> &sellers);
void updateCashPointNo(int oldCashPointNo, int newCashPointNo); void updateCashPointNo(int oldCashPointNo, int newCashPointNo);
void newDb(); void newDb();
InitResult getInitResult() {return initResult_;} InitResult getInitResult() { return initResult_; }
private: private:
sqlite3* db_{nullptr}; sqlite3 *db_{nullptr};
std::string dbname_; std::string dbname_;
void init(); void init();
void beginTransaction(); void beginTransaction();
void endTransaction(); void endTransaction();
void createNew(); void createNew();
int getVersion(); int getVersion();
unsigned int storeArticles(std::vector<Article*> articles); unsigned int storeArticles(std::vector<Article *> articles);
void updateDbToVer2(); void updateDbToVer2();
void updateDbToVer3(); void updateDbToVer3();
InitResult initResult_{InitResult::OK}; InitResult initResult_{InitResult::OK};

View file

@ -3,13 +3,13 @@
class Entity class Entity
{ {
public: public:
enum class State { NEW, UPDATE, DELETE, OK }; enum class State { NEW, UPDATE, DELETE, OK };
virtual ~Entity() = default; virtual ~Entity() = default;
void setState(State state) { m_state = state; } void setState(State state) { m_state = state; }
virtual State getState() const; virtual State getState() const;
private: private:
State m_state{State::NEW}; State m_state{State::NEW};
}; };

View file

@ -5,14 +5,14 @@
class EntityInt : public Entity class EntityInt : public Entity
{ {
public: public:
EntityInt() = default; EntityInt() = default;
virtual ~EntityInt() = default; virtual ~EntityInt() = default;
EntityInt(int id); EntityInt(int id);
void setId(int id); void setId(int id);
int getId() const { return m_id; }; int getId() const { return m_id; };
protected: protected:
int m_id{}; int m_id{};
}; };

View file

@ -11,7 +11,7 @@ void EntityUuid::createUuid()
m_uuid = generator(); m_uuid = generator();
} }
void EntityUuid::setUuidFromString(const std::string& uuidString) void EntityUuid::setUuidFromString(const std::string &uuidString)
{ {
boost::uuids::string_generator generator{}; boost::uuids::string_generator generator{};
m_uuid = generator(uuidString); m_uuid = generator(uuidString);

View file

@ -10,22 +10,22 @@
class EntityUuid : public Entity class EntityUuid : public Entity
{ {
public: public:
EntityUuid() = default; EntityUuid() = default;
virtual ~EntityUuid() = default; virtual ~EntityUuid() = default;
void createUuid(); void createUuid();
void setUuidFromString(const std::string& uuidString); void setUuidFromString(const std::string &uuidString);
void setSourceNo(int sourceNo); void setSourceNo(int sourceNo);
const boost::uuids::uuid& getUuid() const { return m_uuid; }; const boost::uuids::uuid &getUuid() const { return m_uuid; };
std::string getUuidAsString() const { return boost::uuids::to_string(m_uuid); } std::string getUuidAsString() const { return boost::uuids::to_string(m_uuid); }
virtual int getSourceNo() const; virtual int getSourceNo() const;
protected: protected:
int m_sourceNo{}; int m_sourceNo{};
private: private:
boost::uuids::uuid m_uuid{}; boost::uuids::uuid m_uuid{};
}; };

View file

@ -6,7 +6,7 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
std::size_t ExcelReader::readSellersFromFile(const fs::path& filePath, Marketplace* market) std::size_t ExcelReader::readSellersFromFile(const fs::path &filePath, Marketplace *market)
{ {
xlnt::workbook wb; xlnt::workbook wb;
std::ifstream mystream(filePath, std::ios::binary); std::ifstream mystream(filePath, std::ios::binary);
@ -15,7 +15,7 @@ std::size_t ExcelReader::readSellersFromFile(const fs::path& filePath, Marketpla
} }
wb.load(mystream); wb.load(mystream);
for (auto& seller : market->getSellers()) { for (auto &seller : market->getSellers()) {
seller->setState(Seller::State::DELETE); seller->setState(Seller::State::DELETE);
} }
@ -28,7 +28,7 @@ std::size_t ExcelReader::readSellersFromFile(const fs::path& filePath, Marketpla
continue; continue;
} }
//Skip the row if the seller has neither a first name nor a surname // Skip the row if the seller has neither a first name nor a surname
if (row[2].value<std::string>().empty() && row[3].value<std::string>().empty()) { if (row[2].value<std::string>().empty() && row[3].value<std::string>().empty()) {
continue; continue;
} }

View file

@ -11,9 +11,9 @@
class ExcelReader class ExcelReader
{ {
public: public:
static std::size_t readSellersFromFile(const std::filesystem::path& filePath, static std::size_t readSellersFromFile(const std::filesystem::path &filePath,
Marketplace* market); Marketplace *market);
}; };
#endif #endif

View file

@ -5,15 +5,14 @@
#include <fstream> #include <fstream>
namespace fs = std::filesystem;
using json = nlohmann::json; using json = nlohmann::json;
void JsonUtil::exportSellers(const std::filesystem::path& filePath, Marketplace* market) void JsonUtil::exportSellers(const std::filesystem::path &filePath, Marketplace *market)
{ {
json root; json root;
std::ofstream file(filePath); std::ofstream file(filePath);
for (const auto& seller : market->getSellers()) { for (const auto &seller : market->getSellers()) {
json newEntry; json newEntry;
newEntry["seller_no"] = seller->getSellerNo(); newEntry["seller_no"] = seller->getSellerNo();
newEntry["last_name"] = seller->getLastName(); newEntry["last_name"] = seller->getLastName();
@ -25,9 +24,9 @@ void JsonUtil::exportSellers(const std::filesystem::path& filePath, Marketplace*
file << root.dump(4) << std::endl; file << root.dump(4) << std::endl;
} }
std::size_t JsonUtil::importSellers(const std::filesystem::path& filePath, Marketplace* market) std::size_t JsonUtil::importSellers(const std::filesystem::path &filePath, Marketplace *market)
{ {
for (auto& seller : market->getSellers()) { for (auto &seller : market->getSellers()) {
seller->setState(Seller::State::DELETE); seller->setState(Seller::State::DELETE);
} }
market->storeToDb(true); market->storeToDb(true);
@ -61,7 +60,7 @@ std::size_t JsonUtil::importSellers(const std::filesystem::path& filePath, Marke
return market->getSellers().size() - 1; // minus 1 because we don't count the "special" seller return market->getSellers().size() - 1; // minus 1 because we don't count the "special" seller
} }
void JsonUtil::exportSales(const std::filesystem::path& filePath, Marketplace* market, void JsonUtil::exportSales(const std::filesystem::path &filePath, Marketplace *market,
int cashPointNo) int cashPointNo)
{ {
json root; json root;
@ -69,7 +68,7 @@ void JsonUtil::exportSales(const std::filesystem::path& filePath, Marketplace* m
root["source_no"] = cashPointNo; root["source_no"] = cashPointNo;
for (const auto& sale : market->getSales()) { for (const auto &sale : market->getSales()) {
if (sale->getSourceNo() != cashPointNo) if (sale->getSourceNo() != cashPointNo)
continue; continue;
@ -77,7 +76,7 @@ void JsonUtil::exportSales(const std::filesystem::path& filePath, Marketplace* m
newSale["uuid"] = sale->getUuidAsString(); newSale["uuid"] = sale->getUuidAsString();
newSale["timestamp"] = sale->getTimestamp(); newSale["timestamp"] = sale->getTimestamp();
for (const auto& article : sale->getArticles()) { for (const auto &article : sale->getArticles()) {
json newArticle; json newArticle;
newArticle["uuid"] = article->getUuidAsString(); newArticle["uuid"] = article->getUuidAsString();
newArticle["seller_no"] = article->getSeller()->getSellerNo(); newArticle["seller_no"] = article->getSeller()->getSellerNo();
@ -95,7 +94,7 @@ void JsonUtil::exportSales(const std::filesystem::path& filePath, Marketplace* m
file << root.dump(4) << std::endl; file << root.dump(4) << std::endl;
} }
void JsonUtil::importSales(const std::filesystem::path& filePath, Marketplace* market, void JsonUtil::importSales(const std::filesystem::path &filePath, Marketplace *market,
int cashPointNo) int cashPointNo)
{ {
std::ifstream file(filePath); std::ifstream file(filePath);
@ -110,12 +109,12 @@ void JsonUtil::importSales(const std::filesystem::path& filePath, Marketplace* m
market->setSalesToDelete(jsonValues["source_no"]); market->setSalesToDelete(jsonValues["source_no"]);
market->storeToDb(); market->storeToDb();
for (const auto& valSale : jsonValues["sales"]) { for (const auto &valSale : jsonValues["sales"]) {
auto sale = std::make_unique<Sale>(); auto sale = std::make_unique<Sale>();
sale->setUuidFromString(valSale["uuid"]); sale->setUuidFromString(valSale["uuid"]);
sale->setSourceNo(jsonValues["source_no"]); sale->setSourceNo(jsonValues["source_no"]);
sale->setTimestamp(valSale["timestamp"]); sale->setTimestamp(valSale["timestamp"]);
for (const auto& valArticle : valSale["articles"]) { for (const auto &valArticle : valSale["articles"]) {
auto article = std::make_unique<Article>(); auto article = std::make_unique<Article>();
article->setUuidFromString(valArticle["uuid"]); article->setUuidFromString(valArticle["uuid"]);
article->setSourceNo(jsonValues["source_no"]); article->setSourceNo(jsonValues["source_no"]);

View file

@ -8,12 +8,12 @@
class JsonUtil class JsonUtil
{ {
public: public:
static void exportSellers(const std::filesystem::path& filePath, Marketplace* market); static void exportSellers(const std::filesystem::path &filePath, Marketplace *market);
static std::size_t importSellers(const std::filesystem::path& filePath, Marketplace* market); static std::size_t importSellers(const std::filesystem::path &filePath, Marketplace *market);
static void exportSales(const std::filesystem::path& filePath, Marketplace* market, static void exportSales(const std::filesystem::path &filePath, Marketplace *market,
int cashPointNo); int cashPointNo);
static void importSales(const std::filesystem::path& filePath, Marketplace* market, static void importSales(const std::filesystem::path &filePath, Marketplace *market,
int cashPointNo); int cashPointNo);
}; };

View file

@ -33,15 +33,15 @@ Database::InitResult Marketplace::loadFromDb()
return db.getInitResult(); return db.getInitResult();
} }
SellersVec& Marketplace::getSellers() { return sellers_; } SellersVec &Marketplace::getSellers() { return sellers_; }
SalesVec& Marketplace::getSales() { return sales_; } SalesVec &Marketplace::getSales() { return sales_; }
int Marketplace::getNextSellerNo() int Marketplace::getNextSellerNo()
{ {
auto iter = std::max_element( auto iter = std::max_element(
sellers_.begin(), sellers_.end(), sellers_.begin(), sellers_.end(),
[](const auto& a, const auto& b) -> bool { return a->getSellerNo() < b->getSellerNo(); }); [](const auto &a, const auto &b) -> bool { return a->getSellerNo() < b->getSellerNo(); });
if (iter == sellers_.end()) if (iter == sellers_.end())
return 1; return 1;
return (*iter)->getSellerNo() + 1; return (*iter)->getSellerNo() + 1;
@ -53,14 +53,14 @@ int Marketplace::getNextArticleNo()
int maxArtNoInBasket{0}; int maxArtNoInBasket{0};
auto iter = std::max_element(sellers_.begin(), sellers_.end(), auto iter = std::max_element(sellers_.begin(), sellers_.end(),
[](const auto& a, const auto& b) -> bool { [](const auto &a, const auto &b) -> bool {
return a->getMaxArticleNo() < b->getMaxArticleNo(); return a->getMaxArticleNo() < b->getMaxArticleNo();
}); });
if (iter != sellers_.end()) if (iter != sellers_.end())
maxArtNoInDb = (*iter)->getMaxArticleNo(); maxArtNoInDb = (*iter)->getMaxArticleNo();
auto iter2 = auto iter2 =
std::max_element(basket_.begin(), basket_.end(), [](const auto& a, const auto& b) -> bool { std::max_element(basket_.begin(), basket_.end(), [](const auto &a, const auto &b) -> bool {
return a->getArticleNo() < b->getArticleNo(); return a->getArticleNo() < b->getArticleNo();
}); });
@ -73,13 +73,13 @@ int Marketplace::getNextArticleNo()
int Marketplace::getNumSellersDelete() int Marketplace::getNumSellersDelete()
{ {
int count = std::count_if(sellers_.begin(), sellers_.end(), int count = std::count_if(sellers_.begin(), sellers_.end(),
[](const auto& a) { return a->getState() == Seller::State::DELETE; }); [](const auto &a) { return a->getState() == Seller::State::DELETE; });
return count; return count;
} }
int Marketplace::getNumArticlesSold() int Marketplace::getNumArticlesSold()
{ {
int sum = std::accumulate(sellers_.begin(), sellers_.end(), 0, [](int a, const auto& seller) { int sum = std::accumulate(sellers_.begin(), sellers_.end(), 0, [](int a, const auto &seller) {
return a + seller->numArticlesSold(); return a + seller->numArticlesSold();
}); });
return sum; return sum;
@ -87,7 +87,7 @@ int Marketplace::getNumArticlesSold()
int Marketplace::getNumArticlesOffered() int Marketplace::getNumArticlesOffered()
{ {
int sum = std::accumulate(sellers_.begin(), sellers_.end(), 0, [](int a, const auto& seller) { int sum = std::accumulate(sellers_.begin(), sellers_.end(), 0, [](int a, const auto &seller) {
return a + seller->numArticlesOffered(); return a + seller->numArticlesOffered();
}); });
return sum; return sum;
@ -95,10 +95,10 @@ int Marketplace::getNumArticlesOffered()
void Marketplace::sortSellers() { std::sort(sellers_.begin(), sellers_.end()); } void Marketplace::sortSellers() { std::sort(sellers_.begin(), sellers_.end()); }
Seller* Marketplace::findSellerWithSellerNo(int sellerNo) Seller *Marketplace::findSellerWithSellerNo(int sellerNo)
{ {
auto iter = std::find_if(sellers_.begin(), sellers_.end(), auto iter = std::find_if(sellers_.begin(), sellers_.end(),
[sellerNo](const auto& a) { return a->getSellerNo() == sellerNo; }); [sellerNo](const auto &a) { return a->getSellerNo() == sellerNo; });
if (iter == sellers_.end()) if (iter == sellers_.end())
return nullptr; return nullptr;
return (*iter).get(); return (*iter).get();
@ -129,12 +129,12 @@ void Marketplace::finishCurrentSale(std::unique_ptr<Sale> sale)
storeToDb(); storeToDb();
} }
BasketVec& Marketplace::getBasket() { return basket_; } BasketVec &Marketplace::getBasket() { return basket_; }
int Marketplace::getBasketSumInCent() int Marketplace::getBasketSumInCent()
{ {
int sum = std::accumulate(basket_.begin(), basket_.end(), 0, int sum = std::accumulate(basket_.begin(), basket_.end(), 0,
[](int a, const auto& b) { return a + b->getPrice(); }); [](int a, const auto &b) { return a + b->getPrice(); });
return sum; return sum;
} }
@ -148,23 +148,23 @@ std::string Marketplace::getBasketSumAsString()
void Marketplace::removeSale(boost::uuids::uuid uuid) void Marketplace::removeSale(boost::uuids::uuid uuid)
{ {
sales_.erase(std::remove_if(sales_.begin(), sales_.end(), sales_.erase(std::remove_if(sales_.begin(), sales_.end(),
[&uuid](const auto& a) { return a->getUuid() == uuid; }), [&uuid](const auto &a) { return a->getUuid() == uuid; }),
sales_.end()); sales_.end());
} }
void Marketplace::setSalesToDelete(int cashPointNo) void Marketplace::setSalesToDelete(int cashPointNo)
{ {
std::for_each(sales_.begin(), sales_.end(), [cashPointNo](auto& sale) { std::for_each(sales_.begin(), sales_.end(), [cashPointNo](auto &sale) {
if (sale->getSourceNo() == cashPointNo) { if (sale->getSourceNo() == cashPointNo) {
sale->setState(Sale::State::DELETE); sale->setState(Sale::State::DELETE);
for (auto& article : sale->getArticles()) { for (auto &article : sale->getArticles()) {
article->setState(Article::State::DELETE); article->setState(Article::State::DELETE);
} }
} }
}); });
} }
void Marketplace::exportReportToCSV(const fs::path& filePath, int feeInPercent, int maxFeeInEuro) void Marketplace::exportReportToCSV(const fs::path &filePath, int feeInPercent, int maxFeeInEuro)
{ {
const char delimiter = ';'; const char delimiter = ';';
std::ofstream file(filePath); std::ofstream file(filePath);
@ -173,7 +173,7 @@ void Marketplace::exportReportToCSV(const fs::path& filePath, int feeInPercent,
<< "Anz. gemeldet" << delimiter << "Anz. verkauft" << delimiter << "Umsatz" << delimiter << "Anz. gemeldet" << delimiter << "Anz. verkauft" << delimiter << "Umsatz" << delimiter
<< "Auszahlung\n"; << "Auszahlung\n";
for (const auto& seller : sellers_) { for (const auto &seller : sellers_) {
file << seller->getSellerNo() << delimiter file << seller->getSellerNo() << delimiter
<< escapeCsvValue(seller->getLastName(), delimiter) << delimiter << escapeCsvValue(seller->getLastName(), delimiter) << delimiter
<< escapeCsvValue(seller->getFirstName(), delimiter) << delimiter << escapeCsvValue(seller->getFirstName(), delimiter) << delimiter
@ -189,7 +189,7 @@ void Marketplace::exportReportToCSV(const fs::path& filePath, int feeInPercent,
int Marketplace::getOverallSumInCent() int Marketplace::getOverallSumInCent()
{ {
int sum = std::accumulate(sellers_.begin(), sellers_.end(), 0, int sum = std::accumulate(sellers_.begin(), sellers_.end(), 0,
[](int a, const auto& b) { return a + b->sumInCents(); }); [](int a, const auto &b) { return a + b->sumInCents(); });
return sum; return sum;
} }
@ -202,7 +202,7 @@ std::string Marketplace::getOverallSumAsString()
int Marketplace::getOverallPaymentInCent(int percent, int maxFee) int Marketplace::getOverallPaymentInCent(int percent, int maxFee)
{ {
int sum = std::accumulate( int sum = std::accumulate(
sellers_.begin(), sellers_.end(), 0, [percent, maxFee](int a, const auto& b) { sellers_.begin(), sellers_.end(), 0, [percent, maxFee](int a, const auto &b) {
return a + b->sumInCents() - marketFee(b->sumInCents(), percent, maxFee); return a + b->sumInCents() - marketFee(b->sumInCents(), percent, maxFee);
}); });
return sum; return sum;
@ -240,7 +240,7 @@ std::string paymentAsString(int sumInCent, int percent, int maxFeeInCent)
return formatCentAsEuroString(sumInCent - marketFee(sumInCent, percent, maxFeeInCent)); return formatCentAsEuroString(sumInCent - marketFee(sumInCent, percent, maxFeeInCent));
} }
std::string escapeCsvValue(const std::string& value, const char delimiter) std::string escapeCsvValue(const std::string &value, const char delimiter)
{ {
std::stringstream output; std::stringstream output;
bool containsDelim{false}; bool containsDelim{false};
@ -250,7 +250,7 @@ std::string escapeCsvValue(const std::string& value, const char delimiter)
output << '"'; output << '"';
} }
for (auto& symbol : value) { for (auto &symbol : value) {
if (symbol == '"') { if (symbol == '"') {
output << '"' << symbol; output << '"' << symbol;
} else { } else {

View file

@ -20,26 +20,26 @@ using BasketVec = std::vector<std::unique_ptr<Article>>;
class Marketplace class Marketplace
{ {
public: public:
Marketplace(); Marketplace();
void storeToDb(bool onlyDelete = false); void storeToDb(bool onlyDelete = false);
Database::InitResult loadFromDb(); Database::InitResult loadFromDb();
SellersVec& getSellers(); SellersVec &getSellers();
SalesVec& getSales(); SalesVec &getSales();
int getNextSellerNo(); int getNextSellerNo();
int getNextArticleNo(); int getNextArticleNo();
int getNumSellersDelete(); int getNumSellersDelete();
int getNumArticlesSold(); int getNumArticlesSold();
int getNumArticlesOffered(); int getNumArticlesOffered();
BasketVec& getBasket(); BasketVec &getBasket();
int getBasketSumInCent(); int getBasketSumInCent();
std::string getBasketSumAsString(); std::string getBasketSumAsString();
void sortSellers(); void sortSellers();
Seller* findSellerWithSellerNo(int sellerNo); Seller *findSellerWithSellerNo(int sellerNo);
Seller* findSellerWithUuid(const std::string& uuid); Seller *findSellerWithUuid(const std::string &uuid);
void addArticleToBasket(std::unique_ptr<Article> article); void addArticleToBasket(std::unique_ptr<Article> article);
size_t basketSize(); size_t basketSize();
void finishCurrentSale(std::unique_ptr<Sale> sale); void finishCurrentSale(std::unique_ptr<Sale> sale);
@ -54,12 +54,12 @@ class Marketplace
void clear(); void clear();
void exportReportToCSV(const std::filesystem::path& filePath, int feeInPercent, void exportReportToCSV(const std::filesystem::path &filePath, int feeInPercent,
int maxFeeInEuro); int maxFeeInEuro);
friend class ExcelReader; friend class ExcelReader;
private: private:
SellersVec sellers_; SellersVec sellers_;
SalesVec sales_; SalesVec sales_;
BasketVec basket_; BasketVec basket_;
@ -68,6 +68,6 @@ class Marketplace
double marketFee(int sumInCent, int percent, int maxFeeInCent); double marketFee(int sumInCent, int percent, int maxFeeInCent);
std::string marketFeeAsString(int sumInCent, int percent, int maxFeeInCent); std::string marketFeeAsString(int sumInCent, int percent, int maxFeeInCent);
std::string paymentAsString(int sumInCent, int percent, int maxFeeInCent); std::string paymentAsString(int sumInCent, int percent, int maxFeeInCent);
std::string escapeCsvValue(const std::string& value, const char delimiter); std::string escapeCsvValue(const std::string &value, const char delimiter);
#endif #endif

View file

@ -3,41 +3,41 @@
#include <numeric> #include <numeric>
void Sale::addArticle(Article* articlePtr) void Sale::addArticle(Article *articlePtr)
{ {
articlePtr->setSale(this); articlePtr->setSale(this);
articles_.push_back(articlePtr); m_articles.push_back(articlePtr);
} }
ArticlesVec& Sale::getArticles() { return articles_; } ArticlesVec &Sale::getArticles() { return m_articles; }
void Sale::removeArticle(const Article* articlePtr) void Sale::removeArticle(const Article *articlePtr)
{ {
auto it = std::find(articles_.begin(), articles_.end(), articlePtr); auto it = std::find(m_articles.begin(), m_articles.end(), articlePtr);
if (it != articles_.end()) { if (it != m_articles.end()) {
(*it)->setSale(nullptr); (*it)->setSale(nullptr);
(*it)->setState( (*it)->setState(
Article::State::DELETE); // since we only have ad-hoc articles, that have all been sold Article::State::DELETE); // since we only have ad-hoc articles, that have all been sold
articles_.erase(it); m_articles.erase(it);
} }
} }
int Sale::sumInCents() int Sale::sumInCents()
{ {
int sum = std::accumulate(articles_.begin(), articles_.end(), 0, int sum = std::accumulate(m_articles.begin(), m_articles.end(), 0,
[](int a, const Article* b) { return a + b->getPrice(); }); [](int a, const Article *b) { return a + b->getPrice(); });
return sum; return sum;
} }
std::string Sale::sumAsString() { return formatCentAsEuroString(sumInCents()); } std::string Sale::sumAsString() { return formatCentAsEuroString(sumInCents()); }
std::string Sale::getTimestamp() const { return timestamp_; } std::string Sale::getTimestamp() const { return m_timestamp; }
void Sale::setTimestamp(const std::string& timestamp) { timestamp_ = timestamp; } void Sale::setTimestamp(const std::string &timestamp) { m_timestamp = timestamp; }
std::string Sale::getTimestampFormatted() const std::string Sale::getTimestampFormatted() const
{ {
boost::posix_time::ptime time = boost::posix_time::from_iso_extended_string(timestamp_); boost::posix_time::ptime time = boost::posix_time::from_iso_extended_string(m_timestamp);
return boost::posix_time::to_simple_string(time); return boost::posix_time::to_simple_string(time);
} }

View file

@ -11,30 +11,30 @@
namespace namespace
{ {
using ArticlesVec = std::vector<Article*>; using ArticlesVec = std::vector<Article *>;
} }
class Sale : public EntityUuid class Sale : public EntityUuid
{ {
public: public:
Sale() = default; Sale() = default;
Sale(const Sale&) = delete; Sale(const Sale &) = delete;
virtual ~Sale() = default; virtual ~Sale() = default;
void addArticle(Article* articlePtr); void addArticle(Article *articlePtr);
void setTimestamp(const std::string& timestamp); void setTimestamp(const std::string &timestamp);
ArticlesVec& getArticles(); ArticlesVec &getArticles();
std::string getTimestamp() const; std::string getTimestamp() const;
std::string getTimestampFormatted() const; std::string getTimestampFormatted() const;
int sumInCents(); int sumInCents();
std::string sumAsString(); std::string sumAsString();
void removeArticle(const Article* articlePtr); void removeArticle(const Article *articlePtr);
private: private:
std::string timestamp_{ std::string m_timestamp{
boost::posix_time::to_iso_extended_string(boost::posix_time::second_clock::local_time())}; boost::posix_time::to_iso_extended_string(boost::posix_time::second_clock::local_time())};
mutable ArticlesVec articles_{}; mutable ArticlesVec m_articles{};
}; };
#endif #endif

View file

@ -5,32 +5,32 @@
#include <numeric> #include <numeric>
#include <sstream> #include <sstream>
Seller::Seller(const std::string& firstName, const std::string& lastName, int sellerNo, Seller::Seller(const std::string &firstName, const std::string &lastName, int sellerNo,
int numArticlesOffered) int numArticlesOffered)
: EntityInt(sellerNo) : EntityInt(sellerNo)
{ {
firstName_ = firstName; m_firstName = firstName;
lastName_ = lastName; m_lastName = lastName;
numArticlesOffered_ = numArticlesOffered; m_numArticlesOffered = numArticlesOffered;
} }
void Seller::setSellerNo(int seller_no) { setId(seller_no); } void Seller::setSellerNo(int seller_no) { setId(seller_no); }
void Seller::setFirstName(const std::string& firstName) { firstName_ = firstName; } void Seller::setFirstName(const std::string &firstName) { m_firstName = firstName; }
void Seller::setLastName(const std::string& lastName) { lastName_ = lastName; } void Seller::setLastName(const std::string &lastName) { m_lastName = lastName; }
void Seller::setNumArticlesOffered(int number) { numArticlesOffered_ = number; } void Seller::setNumArticlesOffered(int number) { m_numArticlesOffered = number; }
void Seller::addArticle(std::unique_ptr<Article> article) void Seller::addArticle(std::unique_ptr<Article> article)
{ {
article->setSeller(this); article->setSeller(this);
articles_.push_back(std::move(article)); m_articles.push_back(std::move(article));
} }
std::string Seller::getFirstName() const { return firstName_; } std::string Seller::getFirstName() const { return m_firstName; }
std::string Seller::getLastName() const { return lastName_; } std::string Seller::getLastName() const { return m_lastName; }
int Seller::getSellerNo() const { return getId(); } int Seller::getSellerNo() const { return getId(); }
@ -44,10 +44,10 @@ std::string Seller::getSellerNoAsString() const
; ;
} }
std::vector<Article*> Seller::getArticles(bool onlySold) const std::vector<Article *> Seller::getArticles(bool onlySold) const
{ {
std::vector<Article*> articles; std::vector<Article *> articles;
for (const auto& article : articles_) { for (const auto &article : m_articles) {
if (onlySold && article->isSold()) { if (onlySold && article->isSold()) {
articles.push_back(article.get()); articles.push_back(article.get());
} else if (!onlySold) { } else if (!onlySold) {
@ -57,54 +57,54 @@ std::vector<Article*> Seller::getArticles(bool onlySold) const
return articles; return articles;
} }
Article* Seller::getArticleByUuid(const std::string& uuidString) Article *Seller::getArticleByUuid(const std::string &uuidString)
{ {
auto iter = std::find_if(articles_.begin(), articles_.end(), [&uuidString](const auto& art) { auto iter = std::find_if(m_articles.begin(), m_articles.end(), [&uuidString](const auto &art) {
return art->getUuidAsString() == uuidString; return art->getUuidAsString() == uuidString;
}); });
if (iter == articles_.end()) if (iter == m_articles.end())
return nullptr; return nullptr;
return (*iter).get(); return (*iter).get();
} }
int Seller::numArticlesSold() const { return static_cast<int>(getArticles(true).size()); } int Seller::numArticlesSold() const { return static_cast<int>(getArticles(true).size()); }
int Seller::numArticlesOffered() const { return numArticlesOffered_; } int Seller::numArticlesOffered() const { return m_numArticlesOffered; }
int Seller::getMaxArticleNo() const int Seller::getMaxArticleNo() const
{ {
auto iter = std::max_element( auto iter = std::max_element(
articles_.begin(), articles_.end(), m_articles.begin(), m_articles.end(),
[](const auto& a, const auto& b) -> bool { return a->getArticleNo() < b->getArticleNo(); }); [](const auto &a, const auto &b) -> bool { return a->getArticleNo() < b->getArticleNo(); });
if (iter == articles_.end()) if (iter == m_articles.end())
return 0; return 0;
return (*iter)->getArticleNo(); return (*iter)->getArticleNo();
} }
void Seller::cleanupArticles() void Seller::cleanupArticles()
{ {
articles_.erase(std::remove_if(articles_.begin(), articles_.end(), m_articles.erase(std::remove_if(m_articles.begin(), m_articles.end(),
[](const auto& article) { [](const auto &article) {
return article->getState() == Article::State::DELETE; return article->getState() == Article::State::DELETE;
}), }),
articles_.end()); m_articles.end());
for (auto& article : articles_) { for (auto &article : m_articles) {
article->setState(Article::State::OK); article->setState(Article::State::OK);
} }
} }
int Seller::sumInCents() int Seller::sumInCents()
{ {
int sum = std::accumulate(articles_.begin(), articles_.end(), 0, int sum = std::accumulate(m_articles.begin(), m_articles.end(), 0,
[](int a, const auto& b) { return a + b->getPrice(); }); [](int a, const auto &b) { return a + b->getPrice(); });
return sum; return sum;
} }
std::string Seller::sumAsString() { return formatCentAsEuroString(sumInCents()); } std::string Seller::sumAsString() { return formatCentAsEuroString(sumInCents()); }
bool operator<(const Seller& li, const Seller& re) { return li.m_id < re.m_id; } bool operator<(const Seller &li, const Seller &re) { return li.m_id < re.m_id; }
bool operator<(const std::unique_ptr<Seller>& li, const std::unique_ptr<Seller>& re) bool operator<(const std::unique_ptr<Seller> &li, const std::unique_ptr<Seller> &re)
{ {
return li->m_id < re->m_id; return li->m_id < re->m_id;
} }

View file

@ -12,16 +12,16 @@
class Seller : public EntityInt class Seller : public EntityInt
{ {
public: public:
Seller() = default; Seller() = default;
Seller(const Seller&) = delete; Seller(const Seller &) = delete;
virtual ~Seller() = default; virtual ~Seller() = default;
Seller(const std::string& firstName, const std::string& lastName, int sellerNo = 0, Seller(const std::string &firstName, const std::string &lastName, int sellerNo = 0,
int numArticlesOffered = 0); int numArticlesOffered = 0);
void setSellerNo(int sellerNo); void setSellerNo(int sellerNo);
void setFirstName(const std::string& firstName); void setFirstName(const std::string &firstName);
void setLastName(const std::string& lastName); void setLastName(const std::string &lastName);
void setNumArticlesOffered(int number); void setNumArticlesOffered(int number);
void addArticle(std::unique_ptr<Article> article); void addArticle(std::unique_ptr<Article> article);
void cleanupArticles(); void cleanupArticles();
@ -32,20 +32,20 @@ class Seller : public EntityInt
std::string getSellerNoAsString() const; std::string getSellerNoAsString() const;
int numArticlesOffered() const; int numArticlesOffered() const;
int numArticlesSold() const; int numArticlesSold() const;
std::vector<Article*> getArticles(bool onlySold = true) const; std::vector<Article *> getArticles(bool onlySold = true) const;
Article* getArticleByUuid(const std::string& uuidString); Article *getArticleByUuid(const std::string &uuidString);
int getMaxArticleNo() const; int getMaxArticleNo() const;
int sumInCents(); int sumInCents();
std::string sumAsString(); std::string sumAsString();
friend bool operator<(const Seller& li, const Seller& re); friend bool operator<(const Seller &li, const Seller &re);
friend bool operator<(const std::unique_ptr<Seller>& li, const std::unique_ptr<Seller>& re); friend bool operator<(const std::unique_ptr<Seller> &li, const std::unique_ptr<Seller> &re);
private: private:
int numArticlesOffered_{}; int m_numArticlesOffered{};
std::string firstName_{}; std::string m_firstName{};
std::string lastName_{}; std::string m_lastName{};
std::vector<std::unique_ptr<Article>> articles_{}; std::vector<std::unique_ptr<Article>> m_articles{};
}; };
#endif #endif

View file

@ -13,7 +13,7 @@ std::string formatCentAsEuroString(const int cent, int width)
currStream.imbue(myLocale); currStream.imbue(myLocale);
currStream << std::right << std::setw(width) << std::showbase currStream << std::right << std::setw(width) << std::showbase
<< std::put_money(cent, false); << std::put_money(cent, false);
} catch (std::runtime_error& err) { } catch (std::runtime_error &err) {
currStream << std::fixed << std::setw(width >= 4 ? width - 4 : width) currStream << std::fixed << std::setw(width >= 4 ? width - 4 : width)
<< std::setprecision(2) << cent / 100.0L << ""; << std::setprecision(2) << cent / 100.0L << "";
} }
@ -21,19 +21,19 @@ std::string formatCentAsEuroString(const int cent, int width)
return currStream.str(); return currStream.str();
} }
std::string& ltrim(std::string& str, const std::string& chars) std::string &ltrim(std::string &str, const std::string &chars)
{ {
str.erase(0, str.find_first_not_of(chars)); str.erase(0, str.find_first_not_of(chars));
return str; return str;
} }
std::string& rtrim(std::string& str, const std::string& chars) std::string &rtrim(std::string &str, const std::string &chars)
{ {
str.erase(str.find_last_not_of(chars) + 1); str.erase(str.find_last_not_of(chars) + 1);
return str; return str;
} }
std::string& trim(std::string& str, const std::string& chars) std::string &trim(std::string &str, const std::string &chars)
{ {
return ltrim(rtrim(str, chars), chars); return ltrim(rtrim(str, chars), chars);
} }

View file

@ -6,9 +6,9 @@
#include <string> #include <string>
std::string formatCentAsEuroString(const int cent, int width = 10); std::string formatCentAsEuroString(const int cent, int width = 10);
std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r "); std::string &ltrim(std::string &str, const std::string &chars = "\t\n\v\f\r ");
std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r "); std::string &rtrim(std::string &str, const std::string &chars = "\t\n\v\f\r ");
std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r "); std::string &trim(std::string &str, const std::string &chars = "\t\n\v\f\r ");
bool case_insensitive_match(std::string s1, std::string s2); bool case_insensitive_match(std::string s1, std::string s2);
#endif #endif

View file

@ -17,10 +17,10 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
ReportDialog::ReportDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent, f) ReportDialog::ReportDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f)
{ {
ui_.setupUi(this); ui_.setupUi(this);
market_ = dynamic_cast<MainWindow*>(parent)->getMarketplace(); market_ = dynamic_cast<MainWindow *>(parent)->getMarketplace();
model_ = std::make_unique<ReportModel>(market_, ui_.reportView); model_ = std::make_unique<ReportModel>(market_, ui_.reportView);
ui_.reportView->setModel(model_.get()); ui_.reportView->setModel(model_.get());
ui_.reportView->hideColumn(0); ui_.reportView->hideColumn(0);
@ -76,7 +76,7 @@ void ReportDialog::onPrintReportButtonClicked()
int height = printer.height(); int height = printer.height();
int width = printer.width(); int width = printer.width();
const double ENTRIES_PER_PAGE = 51; const double ENTRIES_PER_PAGE = 51;
const auto& sellers = market_->getSellers(); const auto &sellers = market_->getSellers();
unsigned int numPages = std::ceil(sellers.size() / ENTRIES_PER_PAGE); unsigned int numPages = std::ceil(sellers.size() / ENTRIES_PER_PAGE);
painter.begin(&printer); painter.begin(&printer);
@ -136,7 +136,7 @@ void ReportDialog::onPrintReportButtonClicked()
QString content("Einzelteile ohne Nummer\n=======================\n\n"); QString content("Einzelteile ohne Nummer\n=======================\n\n");
unsigned int lines{0}; unsigned int lines{0};
unsigned int pages{1}; unsigned int pages{1};
for (const auto& article : specialSeller->getArticles(true)) { for (const auto &article : specialSeller->getArticles(true)) {
content += QString("- %1:").arg(article->getDescription().substr(0, 45).c_str(), -45); content += QString("- %1:").arg(article->getDescription().substr(0, 45).c_str(), -45);
content += QString("%1\n").arg(article->getPriceAsString().c_str(), 11); content += QString("%1\n").arg(article->getPriceAsString().c_str(), 11);
++lines; ++lines;
@ -201,7 +201,7 @@ void ReportDialog::onPrintSellerReceiptButtonClicked()
return; return;
auto indexes = selModel->selectedRows(); auto indexes = selModel->selectedRows();
auto& seller = market_->getSellers().at(indexes[0].row()); auto &seller = market_->getSellers().at(indexes[0].row());
auto printerDevice = auto printerDevice =
convertToPosPrinterDevice(posPrinterDevice.toStdString(), posPrinterEndpoint.toStdString()); convertToPosPrinterDevice(posPrinterDevice.toStdString(), posPrinterEndpoint.toStdString());
@ -220,8 +220,8 @@ void ReportDialog::onPrintSellerReceiptButtonClicked()
settings.value("global/commune", "Dettingen").toString().toStdString()); settings.value("global/commune", "Dettingen").toString().toStdString());
} }
void ReportDialog::onReportViewSelectionChanged(const QItemSelection& selected, void ReportDialog::onReportViewSelectionChanged(const QItemSelection &selected,
[[maybe_unused]] const QItemSelection& deselected) [[maybe_unused]] const QItemSelection &deselected)
{ {
if (selected.size() > 0) { if (selected.size() > 0) {
ui_.printSellerReceiptButton->setEnabled(true); ui_.printSellerReceiptButton->setEnabled(true);