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);