import / export sellers completed

This commit is contained in:
Martin Brodbeck 2018-08-02 12:34:56 +02:00
parent 045bc8dd20
commit 61da9adce5
5 changed files with 66 additions and 10 deletions

View File

@ -1,4 +1,5 @@
#include "jsonutil.h"
#include "database.h"
#include <json/json.h>
@ -15,17 +16,39 @@ void JsonUtil::exportSellers(const std::string& filename, Marketplace* market)
std::unique_ptr<Json::StreamWriter> 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>();
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();
}

View File

@ -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

View File

@ -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(),

View File

@ -33,6 +33,7 @@ class MainWindow : public QMainWindow
void onSellerNoEditCheckSellerNo();
void onPaidButtonTriggered();
void onImportSellerExcelActionTriggered();
void onImportSellerJsonActionTriggered();
void onExportSellerJsonActionTriggered();
void setSaleModel();

View File

@ -403,8 +403,15 @@ drucken</string>
<property name="title">
<string>&amp;Verkäufer</string>
</property>
<widget class="QMenu" name="importSellerMenu">
<property name="title">
<string>Importieren</string>
</property>
<addaction name="importSellerExcelAction"/>
<addaction name="importSellerJsonAction"/>
</widget>
<addaction name="actionEditSeller"/>
<addaction name="importExcelAction"/>
<addaction name="importSellerMenu"/>
<addaction name="exportSellerJsonAction"/>
</widget>
<widget class="QMenu" name="menuHilfe">
@ -460,16 +467,21 @@ drucken</string>
<string>Auswertung</string>
</property>
</action>
<action name="importExcelAction">
<property name="text">
<string>Importieren (Excel)</string>
</property>
</action>
<action name="exportSellerJsonAction">
<property name="text">
<string>Exportieren (JSON)</string>
</property>
</action>
<action name="importSellerExcelAction">
<property name="text">
<string>Aus Excel-Datei</string>
</property>
</action>
<action name="importSellerJsonAction">
<property name="text">
<string>Aus JSON-Datei</string>
</property>
</action>
</widget>
<resources/>
<connections/>