From a10ba7d7f60f591a58fde3d2aaaf766477311ef4 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Mon, 1 Oct 2018 11:29:41 +0200 Subject: [PATCH 1/5] Allow adding sellers during sale phase --- src/core/jsonutil.cpp | 28 ++++++++++++++++++++++++---- src/core/jsonutil.h | 2 +- src/gui/sellerdialog.cpp | 4 ++-- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/core/jsonutil.cpp b/src/core/jsonutil.cpp index ab4f86c..323854d 100644 --- a/src/core/jsonutil.cpp +++ b/src/core/jsonutil.cpp @@ -31,18 +31,24 @@ void JsonUtil::exportSellers(const fs::path& filePath, Marketplace* market) writer->write(root, &file); } -void JsonUtil::importSellers(const fs::path& filePath, Marketplace* market) +void JsonUtil::importSellers(const fs::path& filePath, Marketplace* market, bool cleanup) { - for (auto& seller : market->getSellers()) { - seller->setState(Seller::State::DELETE); + if (cleanup) { + for (auto& seller : market->getSellers()) { + seller->setState(Seller::State::DELETE); + } + market->storeToDb(true); } - market->storeToDb(true); Json::Value jsonValues; std::ifstream file(filePath); file >> jsonValues; for (auto val : jsonValues["sellers"]) { + auto sellerExist = market->findSellerWithUuid(val["uuid"].asString()); + if (sellerExist) { + continue; + } auto seller = std::make_unique(); seller->setUuidFromString(val["uuid"].asString()); seller->setSellerNo(val["seller_no"].asInt()); @@ -80,6 +86,17 @@ void JsonUtil::exportSales(const fs::path& filePath, Marketplace* market, int ca std::unique_ptr writer(builder.newStreamWriter()); + // Do export sellers (maybe there are new sellers created during sale) + for (const auto& seller : market->getSellers()) { + Json::Value newEntry; + newEntry["uuid"] = seller->getUuidAsString(); + newEntry["seller_no"] = seller->getSellerNo(); + newEntry["last_name"] = seller->getLastName(); + newEntry["first_name"] = seller->getFirstName(); + newEntry["num_offered_articles"] = seller->numArticlesOffered(); + root["sellers"].append(newEntry); + } + root["source_no"] = cashPointNo; for (const auto& sale : market->getSales()) { @@ -110,6 +127,9 @@ void JsonUtil::exportSales(const fs::path& filePath, Marketplace* market, int ca void JsonUtil::importSales(const fs::path& filePath, Marketplace* market, int cashPointNo) { + // First import newly created sellers during the sale phase + importSellers(filePath, market, false); + Json::Value jsonValues; std::ifstream file(filePath); file >> jsonValues; diff --git a/src/core/jsonutil.h b/src/core/jsonutil.h index 3663dcc..43d8745 100644 --- a/src/core/jsonutil.h +++ b/src/core/jsonutil.h @@ -10,7 +10,7 @@ class JsonUtil { public: static void exportSellers(const std::filesystem::path& filePath, Marketplace* market); - static void importSellers(const std::filesystem::path& filePath, Marketplace* market); + static void importSellers(const std::filesystem::path& filePath, Marketplace* market, bool cleanup = true); static void exportSales(const std::filesystem::path& filePath, Marketplace* market, int cashPointNo); static void importSales(const std::filesystem::path& filePath, Marketplace* market, int cashPointNo); }; diff --git a/src/gui/sellerdialog.cpp b/src/gui/sellerdialog.cpp index 1c4ef97..0b375ff 100644 --- a/src/gui/sellerdialog.cpp +++ b/src/gui/sellerdialog.cpp @@ -25,14 +25,14 @@ SellerDialog::SellerDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent, void SellerDialog::on_newButton_clicked() { // Don't allow new seller if market has already started - if (market_->getSales().size() > 0) { + /* if (market_->getSales().size() > 0) { QMessageBox(QMessageBox::Icon::Warning, "Hinweis", "Da die Verkaufsphase schon begonnen hat (Artikel wurden bereits verkauft) " "können Sie keine Verkäufer mehr hinzufügen.", QMessageBox::StandardButton::Ok, this) .exec(); return; - } + } */ ui_.tableView->reset(); ui_.tableView->model()->insertRows(ui_.tableView->model()->rowCount(), 1); From 964c926fb0596a12521aa615298d8fc8b440a31c Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Mon, 1 Oct 2018 12:04:09 +0200 Subject: [PATCH 2/5] Allow adding sellers during sale phase --- src/core/database.cpp | 6 ++++-- src/core/database.h | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/core/database.cpp b/src/core/database.cpp index a3240cf..12e42fa 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -186,6 +186,8 @@ void Database::beginTransaction() { exec("BEGIN TRANSACTION"); } void Database::endTransaction() { exec("END TRANSACTION"); } +void Database::rollbackTransaction() { exec("ROLLBACK TRANSACTION"); } + unsigned int Database::storeSellers(std::vector>& sellers, bool onlyDelete) { int retCode{}; @@ -221,10 +223,10 @@ unsigned int Database::storeSellers(std::vector>& seller retCode = sqlite3_step(stmt); if (retCode != SQLITE_DONE) { - + rollbackTransaction(); std::string errMsg(sqlite3_errmsg(db_)); sqlite3_finalize(stmt); - throw std::runtime_error(errMsg); + sqlite3_ throw std::runtime_error(errMsg); } ++count; sqlite3_finalize(stmt); diff --git a/src/core/database.h b/src/core/database.h index 2bd3e0b..3a6383d 100644 --- a/src/core/database.h +++ b/src/core/database.h @@ -31,6 +31,7 @@ class Database void init(); void beginTransaction(); void endTransaction(); + void rollbackTransaction(); void createNew(); int getVersion(); unsigned int storeArticles(std::vector articles); From 2f833214847a35b093e69da9788861b45d3912f5 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Mon, 1 Oct 2018 12:05:21 +0200 Subject: [PATCH 3/5] Allow adding sellers during sale phase --- src/core/database.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/database.cpp b/src/core/database.cpp index 12e42fa..9529202 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -226,7 +226,7 @@ unsigned int Database::storeSellers(std::vector>& seller rollbackTransaction(); std::string errMsg(sqlite3_errmsg(db_)); sqlite3_finalize(stmt); - sqlite3_ throw std::runtime_error(errMsg); + throw std::runtime_error(errMsg); } ++count; sqlite3_finalize(stmt); From 91f52df55479fddd6083a5693161e55f1c5c87e9 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Mon, 1 Oct 2018 12:19:26 +0200 Subject: [PATCH 4/5] More on adding new seller during sale phase --- src/core/database.cpp | 2 +- src/gui/mainwindow.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/database.cpp b/src/core/database.cpp index 9529202..411fd10 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -223,7 +223,7 @@ unsigned int Database::storeSellers(std::vector>& seller retCode = sqlite3_step(stmt); if (retCode != SQLITE_DONE) { - rollbackTransaction(); + //rollbackTransaction(); std::string errMsg(sqlite3_errmsg(db_)); sqlite3_finalize(stmt); throw std::runtime_error(errMsg); diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 46bc41c..5c586a6 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -503,6 +503,7 @@ void MainWindow::onImportSalesJsonActionTriggered() QMessageBox(QMessageBox::Icon::Warning, "Import nicht möglich", err.what(), QMessageBox::Ok, this) .exec(); + marketplace_->loadFromDb(); } setSaleModel(); } From 86a9ed38b63444d2baa3955de2d2b905d54976b8 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Mon, 1 Oct 2018 12:36:07 +0200 Subject: [PATCH 5/5] Improve error message --- src/core/database.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/database.cpp b/src/core/database.cpp index 411fd10..6516223 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -225,6 +225,7 @@ unsigned int Database::storeSellers(std::vector>& seller if (retCode != SQLITE_DONE) { //rollbackTransaction(); std::string errMsg(sqlite3_errmsg(db_)); + errMsg += "\nSellerNo: " + std::to_string(seller->getSellerNo()); sqlite3_finalize(stmt); throw std::runtime_error(errMsg); } @@ -722,4 +723,4 @@ void Database::updateCashPointNo(int oldCashPointNo, int newCashPointNo) sqlite3_finalize(stmt); endTransaction(); -} \ No newline at end of file +}