From c82378dfa9965290c46b26f6f34f27f631be3940 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Thu, 2 Aug 2018 15:06:35 +0200 Subject: [PATCH] export sales works now --- src/core/jsonutil.cpp | 43 ++++++++++++++++++++++++++++++++++++++---- src/core/jsonutil.h | 1 + src/gui/mainwindow.cpp | 12 +++++++++++- src/gui/mainwindow.h | 1 + src/gui/mainwindow.ui | 12 +++++++++--- 5 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/core/jsonutil.cpp b/src/core/jsonutil.cpp index 6dc855c..8935760 100644 --- a/src/core/jsonutil.cpp +++ b/src/core/jsonutil.cpp @@ -31,17 +31,16 @@ void JsonUtil::exportSellers(const std::string& filename, Marketplace* market) void JsonUtil::importSellers(const std::string& filename, Marketplace* market) { - for (auto& seller : market->getSellers()) - { + 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"]) { + for (auto val : jsonValues["sellers"]) { auto seller = std::make_unique(); seller->setUuidFromString(val["uuid"].asString()); seller->setSellerNo(val["seller_no"].asInt()); @@ -51,4 +50,40 @@ void JsonUtil::importSellers(const std::string& filename, Marketplace* market) market->getSellers().push_back(std::move(seller)); } market->storeToDb(); +} + +void JsonUtil::exportSales(const std::string& filename, Marketplace* market, int cashPointNo) +{ + Json::Value root; + std::ofstream file(filename); + + Json::StreamWriterBuilder builder; + builder["commentStyle"] = "None"; + builder["indentation"] = " "; + + std::unique_ptr writer(builder.newStreamWriter()); + + root["source_no"] = cashPointNo; + + for (const auto& sale : market->getSales()) { + Json::Value newSale; + newSale["uuid"] = sale->getUuidAsString(); + newSale["timestamp"] = sale->getTimestamp(); + + for (const auto& article : sale->getArticles()) { + Json::Value newArticle; + newArticle["uuid"] = article->getUuidAsString(); + newArticle["seller_uuid"] = article->getSeller()->getUuidAsString(); + newArticle["desc"] = article->getDescription(); + newArticle["price"] = article->getPrice(); + // newArticle["source_no"] = article->getSourceNo(); + newArticle["article_no"] = article->getArticleNo(); + + newSale["articles"].append(newArticle); + } + + root["sales"].append(newSale); + } + + writer->write(root, &file); } \ No newline at end of file diff --git a/src/core/jsonutil.h b/src/core/jsonutil.h index 0e3a03d..45403e1 100644 --- a/src/core/jsonutil.h +++ b/src/core/jsonutil.h @@ -10,6 +10,7 @@ class JsonUtil public: static void exportSellers(const std::string& filename, Marketplace* market); static void importSellers(const std::string& filename, Marketplace* market); + static void exportSales(const std::string& filename, Marketplace* market, int cashPointNo); }; #endif \ No newline at end of file diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 5d12161..52247e6 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -68,6 +68,8 @@ MainWindow::MainWindow() &MainWindow::onImportSellerJsonActionTriggered); connect(ui_.exportSellerJsonAction, &QAction::triggered, this, &MainWindow::onExportSellerJsonActionTriggered); + connect(ui_.exportSalesJsonAction, &QAction::triggered, this, + &MainWindow::onExportSalesJsonActionTriggered); } void MainWindow::onActionEditSellerTriggered() @@ -276,11 +278,19 @@ void MainWindow::onImportSellerJsonActionTriggered() JsonUtil::importSellers(filename.toStdString(), marketplace_.get()); } - void MainWindow::onExportSellerJsonActionTriggered() { auto filename = QFileDialog::getSaveFileName(this, "Verkäufer exportieren", QString(), "JSON Dateien (*.json)"); JsonUtil::exportSellers(filename.toStdString(), marketplace_.get()); +} + +void MainWindow::onExportSalesJsonActionTriggered() +{ + QSettings settings; + + auto filename = QFileDialog::getSaveFileName(this, "Umsätze/Transaktionen exportieren", + QString(), "JSON Dateien (*.json)"); + JsonUtil::exportSales(filename.toStdString(), marketplace_.get(), settings.value("global/cashPointNo").toInt()); } \ No newline at end of file diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index 77f59c6..300f039 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -35,6 +35,7 @@ class MainWindow : public QMainWindow void onImportSellerExcelActionTriggered(); void onImportSellerJsonActionTriggered(); void onExportSellerJsonActionTriggered(); + void onExportSalesJsonActionTriggered(); void setSaleModel(); Ui::MainWindow ui_; diff --git a/src/gui/mainwindow.ui b/src/gui/mainwindow.ui index f0f54a6..6af5b36 100644 --- a/src/gui/mainwindow.ui +++ b/src/gui/mainwindow.ui @@ -421,15 +421,16 @@ drucken - + - &Extras + &Umsätze + - + @@ -482,6 +483,11 @@ drucken Aus JSON-Datei + + + Exportieren (JSON) + +