diff --git a/src/core/excelreader.cpp b/src/core/excelreader.cpp index 7225c78..f39333c 100644 --- a/src/core/excelreader.cpp +++ b/src/core/excelreader.cpp @@ -18,14 +18,13 @@ void ExcelReader::readSellersFromFile(const std::string& filename, Marketplace* int rowCount{}; for (const auto& row : ws.rows(false)) { - // auto test = row[0].value(); if (rowCount < START_ROW) { ++rowCount; continue; } else if (rowCount > END_ROW) { break; } - if(row[2].value().empty() && row[3].value().empty()) { + if (row[2].value().empty() && row[3].value().empty()) { ++rowCount; continue; } diff --git a/src/core/jsonutil.cpp b/src/core/jsonutil.cpp index 6dc855c..5acadee 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,55 @@ 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); +} + +void JsonUtil::importSales(const std::string& filename, Marketplace* market, int cashPointNo) +{ + Json::Value jsonValues; + std::ifstream file(filename); + file >> jsonValues; + + int source_no = jsonValues["source_no"].asInt(); + if (source_no == cashPointNo) { + throw std::runtime_error("Die Kassen-Nr. der zu imporierenden Daten wird von dieser Kasse " + "hier bereits verwendet."); + } + + // TODO: Import sales } \ No newline at end of file diff --git a/src/core/jsonutil.h b/src/core/jsonutil.h index 0e3a03d..1f0335d 100644 --- a/src/core/jsonutil.h +++ b/src/core/jsonutil.h @@ -10,6 +10,8 @@ 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); + static void importSales(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..8447b59 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -68,6 +69,10 @@ MainWindow::MainWindow() &MainWindow::onImportSellerJsonActionTriggered); connect(ui_.exportSellerJsonAction, &QAction::triggered, this, &MainWindow::onExportSellerJsonActionTriggered); + connect(ui_.exportSalesJsonAction, &QAction::triggered, this, + &MainWindow::onExportSalesJsonActionTriggered); + connect(ui_.importSalesJsonAction, &QAction::triggered, this, + &MainWindow::onImportSalesJsonActionTriggered); } void MainWindow::onActionEditSellerTriggered() @@ -276,11 +281,45 @@ 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()); -} \ No newline at end of file +} + +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()); +} + +void MainWindow::onImportSalesJsonActionTriggered() +{ + /* 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; + } */ + + QSettings settings; + + auto filename = QFileDialog::getOpenFileName(this, "Umsätze/Transaktionen importieren", + QString(), "JSON Dateien (*.json)"); + + try { + JsonUtil::importSales(filename.toStdString(), marketplace_.get(), + settings.value("global/cashPointNo").toInt()); + } catch (std::runtime_error& err) { + QMessageBox(QMessageBox::Icon::Warning, "Import nicht möglich", err.what(), QMessageBox::Ok, + this) + .exec(); + } +} diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index 77f59c6..f928e6b 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -35,6 +35,8 @@ class MainWindow : public QMainWindow void onImportSellerExcelActionTriggered(); void onImportSellerJsonActionTriggered(); void onExportSellerJsonActionTriggered(); + void onExportSalesJsonActionTriggered(); + void onImportSalesJsonActionTriggered(); void setSaleModel(); Ui::MainWindow ui_; diff --git a/src/gui/mainwindow.ui b/src/gui/mainwindow.ui index f0f54a6..2e7d874 100644 --- a/src/gui/mainwindow.ui +++ b/src/gui/mainwindow.ui @@ -421,15 +421,17 @@ drucken - + - &Extras + &Umsätze + + - + @@ -482,6 +484,16 @@ drucken Aus JSON-Datei + + + Exportieren (JSON) + + + + + Importieren (JSON) + +