From 61da9adce568ac526b3935db859b4d2970e26508 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Thu, 2 Aug 2018 12:34:56 +0200 Subject: [PATCH] import / export sellers completed --- src/core/jsonutil.cpp | 29 ++++++++++++++++++++++++++--- src/core/jsonutil.h | 1 + src/gui/mainwindow.cpp | 21 ++++++++++++++++++++- src/gui/mainwindow.h | 1 + src/gui/mainwindow.ui | 24 ++++++++++++++++++------ 5 files changed, 66 insertions(+), 10 deletions(-) diff --git a/src/core/jsonutil.cpp b/src/core/jsonutil.cpp index f163ba9..6dc855c 100644 --- a/src/core/jsonutil.cpp +++ b/src/core/jsonutil.cpp @@ -1,4 +1,5 @@ #include "jsonutil.h" +#include "database.h" #include @@ -15,17 +16,39 @@ void JsonUtil::exportSellers(const std::string& filename, Marketplace* market) std::unique_ptr writer(builder.newStreamWriter()); - root["encoding"] = "UTF-8"; - 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->getFirstName(); + newEntry["num_offered_articles"] = seller->numArticlesOffered(); root["sellers"].append(newEntry); } writer->write(root, &file); +} + +void JsonUtil::importSellers(const std::string& filename, Marketplace* market) +{ + for (auto& seller : market->getSellers()) + { + seller->setState(Seller::State::DELETE); + } + market->storeToDb(true); + + Json::Value jsonValues; + std::ifstream file(filename); + file >> jsonValues; + + for(auto val : jsonValues["sellers"]) { + auto seller = std::make_unique(); + seller->setUuidFromString(val["uuid"].asString()); + seller->setSellerNo(val["seller_no"].asInt()); + seller->setLastName(val["last_name"].asString()); + seller->setFirstName(val["first_name"].asString()); + seller->setNumArticlesOffered(val["num_offered_articles"].asInt()); + market->getSellers().push_back(std::move(seller)); + } + market->storeToDb(); } \ No newline at end of file diff --git a/src/core/jsonutil.h b/src/core/jsonutil.h index e71b59d..0e3a03d 100644 --- a/src/core/jsonutil.h +++ b/src/core/jsonutil.h @@ -9,6 +9,7 @@ class JsonUtil { public: static void exportSellers(const std::string& filename, Marketplace* market); + static void importSellers(const std::string& filename, Marketplace* market); }; #endif \ No newline at end of file diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 972981c..5d12161 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -62,8 +62,10 @@ MainWindow::MainWindow() this->setWindowTitle("KIMA2 - Kasse Nr. " + QSettings().value("global/cashPointNo").toString()); }); - connect(ui_.importExcelAction, &QAction::triggered, this, + connect(ui_.importSellerExcelAction, &QAction::triggered, this, &MainWindow::onImportSellerExcelActionTriggered); + connect(ui_.importSellerJsonAction, &QAction::triggered, this, + &MainWindow::onImportSellerJsonActionTriggered); connect(ui_.exportSellerJsonAction, &QAction::triggered, this, &MainWindow::onExportSellerJsonActionTriggered); } @@ -258,6 +260,23 @@ void MainWindow::onImportSellerExcelActionTriggered() ExcelReader::readSellersFromFile(filename.toStdString(), marketplace_.get()); } +void MainWindow::onImportSellerJsonActionTriggered() +{ + if (!marketplace_->getSales().empty()) { + QMessageBox(QMessageBox::Icon::Information, "Import nicht möglich", + "Der Import ist nicht möglich, da schon Verkäufe getätigt wurden.", + QMessageBox::StandardButton::Ok, this) + .exec(); + return; + } + + auto filename = QFileDialog::getOpenFileName(this, "Verkäufer importieren", QString(), + "JSON Dateien (*.json)"); + + JsonUtil::importSellers(filename.toStdString(), marketplace_.get()); +} + + void MainWindow::onExportSellerJsonActionTriggered() { auto filename = QFileDialog::getSaveFileName(this, "Verkäufer exportieren", QString(), diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index cf1b12d..77f59c6 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -33,6 +33,7 @@ class MainWindow : public QMainWindow void onSellerNoEditCheckSellerNo(); void onPaidButtonTriggered(); void onImportSellerExcelActionTriggered(); + void onImportSellerJsonActionTriggered(); void onExportSellerJsonActionTriggered(); void setSaleModel(); diff --git a/src/gui/mainwindow.ui b/src/gui/mainwindow.ui index 58e86c3..f0f54a6 100644 --- a/src/gui/mainwindow.ui +++ b/src/gui/mainwindow.ui @@ -403,8 +403,15 @@ drucken &Verkäufer + + + Importieren + + + + - + @@ -460,16 +467,21 @@ drucken Auswertung - - - Importieren (Excel) - - Exportieren (JSON) + + + Aus Excel-Datei + + + + + Aus JSON-Datei + +