From 045bc8dd20c87507b72d48c8b4022049ee100894 Mon Sep 17 00:00:00 2001
From: Martin Brodbeck
Date: Thu, 2 Aug 2018 11:15:15 +0200
Subject: [PATCH 1/3] export sellers as json file
---
src/core/CMakeLists.txt | 6 ++++--
src/core/excelreader.cpp | 12 ++++++++----
src/core/jsonutil.cpp | 31 +++++++++++++++++++++++++++++++
src/core/jsonutil.h | 14 ++++++++++++++
src/gui/kima2.cpp | 4 ++--
src/gui/mainwindow.cpp | 15 +++++++++++++--
src/gui/mainwindow.h | 3 ++-
src/gui/mainwindow.ui | 6 ++++++
8 files changed, 80 insertions(+), 11 deletions(-)
create mode 100644 src/core/jsonutil.cpp
create mode 100644 src/core/jsonutil.h
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 7df4118..64a1c6b 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -3,7 +3,8 @@ set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.62 COMPONENTS date_time REQUIRED)
find_package(SQLite3 REQUIRED)
find_package(PkgConfig REQUIRED)
-pkg_check_modules(XLNT REQUIRED xlnt)
+pkg_check_modules(XLNT REQUIRED xlnt>=1.3)
+pkg_check_modules(JSONCPP REQUIRED jsoncpp)
set(CORE_SOURCES
database.cpp
@@ -13,10 +14,11 @@ set(CORE_SOURCES
sale.cpp
marketplace.cpp
excelreader.cpp
+ jsonutil
)
add_library(core STATIC ${CORE_SOURCES})
-target_link_libraries(core Boost::boost Boost::date_time sqlite3 ${XLNT_LIBRARIES})
+target_link_libraries(core Boost::boost Boost::date_time sqlite3 ${XLNT_LIBRARIES} ${JSONCPP_LIBRARIES})
if (WIN32)
target_link_libraries(core bcrypt)
endif()
diff --git a/src/core/excelreader.cpp b/src/core/excelreader.cpp
index eee03ef..f136a34 100644
--- a/src/core/excelreader.cpp
+++ b/src/core/excelreader.cpp
@@ -8,15 +8,19 @@ void ExcelReader::readSellersFromFile(const std::string& filename, Marketplace*
wb.load(filename);
auto ws = wb.sheet_by_index(0);
- const int START_ROW = 6;
- const int END_ROW = 349;
+ const int START_ROW = 5;
+ const int END_ROW = 350;
int rowCount{};
for (const auto& row : ws.rows(false)) {
// auto test = row[0].value();
- if (rowCount < START_ROW)
+ if (rowCount < START_ROW) {
+ rowCount++;
continue;
- std::cout << row[0].to_string() << "\n";
+ } else if (rowCount > END_ROW) {
+ break;
+ }
+ std::cout << row[0].value() << "\n";
rowCount++;
}
}
diff --git a/src/core/jsonutil.cpp b/src/core/jsonutil.cpp
new file mode 100644
index 0000000..f163ba9
--- /dev/null
+++ b/src/core/jsonutil.cpp
@@ -0,0 +1,31 @@
+#include "jsonutil.h"
+
+#include
+
+#include
+
+void JsonUtil::exportSellers(const std::string& filename, Marketplace* market)
+{
+ Json::Value root;
+ std::ofstream file(filename);
+
+ Json::StreamWriterBuilder builder;
+ builder["commentStyle"] = "None";
+ builder["indentation"] = " ";
+
+ 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();
+ root["sellers"].append(newEntry);
+ }
+
+ writer->write(root, &file);
+}
\ No newline at end of file
diff --git a/src/core/jsonutil.h b/src/core/jsonutil.h
new file mode 100644
index 0000000..e71b59d
--- /dev/null
+++ b/src/core/jsonutil.h
@@ -0,0 +1,14 @@
+#ifndef JSON_H
+#define JSON_H
+
+#include "marketplace.h"
+
+#include
+
+class JsonUtil
+{
+ public:
+ static void exportSellers(const std::string& filename, Marketplace* market);
+};
+
+#endif
\ No newline at end of file
diff --git a/src/gui/kima2.cpp b/src/gui/kima2.cpp
index baf9cb7..b8886cf 100644
--- a/src/gui/kima2.cpp
+++ b/src/gui/kima2.cpp
@@ -14,8 +14,8 @@ int main(int argc, char* argv[])
// Set the locale to german, so that currency is correct
// std::locale german("de_DE.utf-8");
- std::locale myLocale("");
- std::locale::global(myLocale);
+ //std::locale myLocale("");
+ //std::locale::global(myLocale);
QApplication kimaApp{argc, argv};
diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp
index de48a3b..972981c 100644
--- a/src/gui/mainwindow.cpp
+++ b/src/gui/mainwindow.cpp
@@ -2,6 +2,7 @@
#include "basketmodel.h"
#include "config.h"
+#include "jsonutil.h"
#include "pricedialog.h"
#include "reportdialog.h"
#include "salemodel.h"
@@ -62,7 +63,9 @@ MainWindow::MainWindow()
QSettings().value("global/cashPointNo").toString());
});
connect(ui_.importExcelAction, &QAction::triggered, this,
- &MainWindow::onImportExcelActionTriggered);
+ &MainWindow::onImportSellerExcelActionTriggered);
+ connect(ui_.exportSellerJsonAction, &QAction::triggered, this,
+ &MainWindow::onExportSellerJsonActionTriggered);
}
void MainWindow::onActionEditSellerTriggered()
@@ -239,7 +242,7 @@ void MainWindow::onAbout()
">info@rustysoft.de>
");
}
-void MainWindow::onImportExcelActionTriggered()
+void MainWindow::onImportSellerExcelActionTriggered()
{
if (!marketplace_->getSales().empty()) {
QMessageBox(QMessageBox::Icon::Information, "Import nicht möglich",
@@ -253,4 +256,12 @@ void MainWindow::onImportExcelActionTriggered()
"Excel Dateien (*.xlsx *.xls)");
ExcelReader::readSellersFromFile(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
diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h
index 8c87f91..cf1b12d 100644
--- a/src/gui/mainwindow.h
+++ b/src/gui/mainwindow.h
@@ -32,7 +32,8 @@ class MainWindow : public QMainWindow
void onActionEditSellerTriggered();
void onSellerNoEditCheckSellerNo();
void onPaidButtonTriggered();
- void onImportExcelActionTriggered();
+ void onImportSellerExcelActionTriggered();
+ void onExportSellerJsonActionTriggered();
void setSaleModel();
Ui::MainWindow ui_;
diff --git a/src/gui/mainwindow.ui b/src/gui/mainwindow.ui
index 56c493c..58e86c3 100644
--- a/src/gui/mainwindow.ui
+++ b/src/gui/mainwindow.ui
@@ -405,6 +405,7 @@ drucken
+
From 61da9adce568ac526b3935db859b4d2970e26508 Mon Sep 17 00:00:00 2001
From: Martin Brodbeck
Date: Thu, 2 Aug 2018 12:34:56 +0200
Subject: [PATCH 2/3] 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
+
-
+
From da4e223b16edffd6cd1cf5054fb5984e562e0b91 Mon Sep 17 00:00:00 2001
From: Martin Brodbeck
Date: Thu, 2 Aug 2018 12:36:37 +0200
Subject: [PATCH 3/3] code cleanup
---
src/core/marketplace.h | 3 ---
1 file changed, 3 deletions(-)
diff --git a/src/core/marketplace.h b/src/core/marketplace.h
index 0751b04..9bf6542 100644
--- a/src/core/marketplace.h
+++ b/src/core/marketplace.h
@@ -16,9 +16,6 @@ using SalesVec = std::vector>;
using BasketVec = std::vector>;
} // namespace
-struct Basket {
-};
-
class Marketplace
{
public: