2018-08-02 11:15:15 +02:00
|
|
|
#include "jsonutil.h"
|
2018-08-02 12:34:56 +02:00
|
|
|
#include "database.h"
|
2018-08-02 11:15:15 +02:00
|
|
|
|
2018-10-18 12:06:36 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2018-08-02 11:15:15 +02:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
2018-08-09 16:37:56 +02:00
|
|
|
namespace fs = std::filesystem;
|
2018-10-18 12:06:36 +02:00
|
|
|
using json = nlohmann::json;
|
2018-08-09 16:37:56 +02:00
|
|
|
|
2018-10-18 12:06:36 +02:00
|
|
|
void JsonUtil::exportSellers(const std::filesystem::path& filePath, Marketplace* market)
|
2018-08-02 11:15:15 +02:00
|
|
|
{
|
2018-10-18 12:06:36 +02:00
|
|
|
json root;
|
2018-08-09 13:16:00 +02:00
|
|
|
std::ofstream file(filePath);
|
2018-08-02 11:15:15 +02:00
|
|
|
|
|
|
|
for (const auto& seller : market->getSellers()) {
|
2018-10-18 12:06:36 +02:00
|
|
|
json newEntry;
|
2018-08-02 11:15:15 +02:00
|
|
|
newEntry["uuid"] = seller->getUuidAsString();
|
|
|
|
newEntry["seller_no"] = seller->getSellerNo();
|
|
|
|
newEntry["last_name"] = seller->getLastName();
|
|
|
|
newEntry["first_name"] = seller->getFirstName();
|
2018-08-02 12:34:56 +02:00
|
|
|
newEntry["num_offered_articles"] = seller->numArticlesOffered();
|
2018-10-18 12:06:36 +02:00
|
|
|
root["sellers"].push_back(newEntry);
|
2018-08-02 11:15:15 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 12:06:36 +02:00
|
|
|
file << root.dump(4) << std::endl;
|
2018-08-02 12:34:56 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 12:06:36 +02:00
|
|
|
void JsonUtil::importSellers(const std::filesystem::path& filePath, Marketplace* market)
|
2018-08-02 12:34:56 +02:00
|
|
|
{
|
2018-08-02 15:06:35 +02:00
|
|
|
for (auto& seller : market->getSellers()) {
|
2018-08-02 12:34:56 +02:00
|
|
|
seller->setState(Seller::State::DELETE);
|
|
|
|
}
|
|
|
|
market->storeToDb(true);
|
2018-08-02 15:06:35 +02:00
|
|
|
|
2018-08-09 12:58:11 +02:00
|
|
|
std::ifstream file(filePath);
|
2018-10-18 12:06:36 +02:00
|
|
|
json jsonValues = json::parse(file);
|
2018-08-02 12:34:56 +02:00
|
|
|
|
2018-08-02 15:06:35 +02:00
|
|
|
for (auto val : jsonValues["sellers"]) {
|
2018-08-02 12:34:56 +02:00
|
|
|
auto seller = std::make_unique<Seller>();
|
2018-10-18 12:06:36 +02:00
|
|
|
seller->setUuidFromString(val["uuid"]);
|
|
|
|
seller->setSellerNo(val["seller_no"]);
|
|
|
|
seller->setLastName(val["last_name"]);
|
|
|
|
seller->setFirstName(val["first_name"]);
|
|
|
|
seller->setNumArticlesOffered(val["num_offered_articles"]);
|
2018-08-02 12:34:56 +02:00
|
|
|
market->getSellers().push_back(std::move(seller));
|
|
|
|
}
|
2018-08-07 08:31:43 +02:00
|
|
|
|
|
|
|
// If there was no special seller "Sonderkonto" in import data, then create one
|
|
|
|
auto specialSeller = market->findSellerWithUuid("11111111-1111-1111-1111-111111111111");
|
|
|
|
if (!specialSeller) {
|
|
|
|
auto seller = std::make_unique<Seller>();
|
|
|
|
seller->setUuidFromString("11111111-1111-1111-1111-111111111111");
|
|
|
|
seller->setSellerNo(0);
|
|
|
|
seller->setLastName("Sonderkonto");
|
|
|
|
seller->setFirstName("Sonderkonto");
|
|
|
|
seller->setNumArticlesOffered(0);
|
|
|
|
market->getSellers().push_back(std::move(seller));
|
|
|
|
}
|
|
|
|
|
|
|
|
market->sortSellers();
|
|
|
|
|
2018-08-02 12:34:56 +02:00
|
|
|
market->storeToDb();
|
2018-08-02 15:06:35 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 12:06:36 +02:00
|
|
|
void JsonUtil::exportSales(const std::filesystem::path& filePath, Marketplace* market, int cashPointNo)
|
2018-08-02 15:06:35 +02:00
|
|
|
{
|
2018-10-18 12:06:36 +02:00
|
|
|
json root;
|
2018-08-09 13:11:23 +02:00
|
|
|
std::ofstream file(filePath);
|
2018-08-02 15:06:35 +02:00
|
|
|
|
|
|
|
root["source_no"] = cashPointNo;
|
|
|
|
|
|
|
|
for (const auto& sale : market->getSales()) {
|
2018-08-05 14:52:34 +02:00
|
|
|
if (sale->getSourceNo() != cashPointNo)
|
|
|
|
continue;
|
|
|
|
|
2018-10-18 12:06:36 +02:00
|
|
|
json newSale;
|
2018-08-02 15:06:35 +02:00
|
|
|
newSale["uuid"] = sale->getUuidAsString();
|
|
|
|
newSale["timestamp"] = sale->getTimestamp();
|
|
|
|
|
|
|
|
for (const auto& article : sale->getArticles()) {
|
2018-10-18 12:06:36 +02:00
|
|
|
json newArticle;
|
2018-08-02 15:06:35 +02:00
|
|
|
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();
|
|
|
|
|
2018-10-18 12:06:36 +02:00
|
|
|
newSale["articles"].push_back(newArticle);
|
2018-08-02 15:06:35 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 12:06:36 +02:00
|
|
|
root["sales"].push_back(newSale);
|
2018-08-02 15:06:35 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 12:06:36 +02:00
|
|
|
file << root.dump(4) << std::endl;
|
2018-08-02 15:28:35 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 12:06:36 +02:00
|
|
|
void JsonUtil::importSales(const std::filesystem::path& filePath, Marketplace* market, int cashPointNo)
|
2018-08-02 15:28:35 +02:00
|
|
|
{
|
2018-08-09 13:00:51 +02:00
|
|
|
std::ifstream file(filePath);
|
2018-10-18 12:06:36 +02:00
|
|
|
json jsonValues = json::parse(file);
|
2018-08-02 15:28:35 +02:00
|
|
|
|
2018-10-18 12:06:36 +02:00
|
|
|
int source_no = jsonValues["source_no"];
|
2018-08-02 15:28:35 +02:00
|
|
|
if (source_no == cashPointNo) {
|
|
|
|
throw std::runtime_error("Die Kassen-Nr. der zu imporierenden Daten wird von dieser Kasse "
|
|
|
|
"hier bereits verwendet.");
|
|
|
|
}
|
|
|
|
|
2018-10-18 12:06:36 +02:00
|
|
|
market->setSalesToDelete(jsonValues["source_no"]);
|
2018-08-05 14:52:34 +02:00
|
|
|
market->storeToDb();
|
|
|
|
|
|
|
|
for (const auto& valSale : jsonValues["sales"]) {
|
|
|
|
auto sale = std::make_unique<Sale>();
|
2018-10-18 12:06:36 +02:00
|
|
|
sale->setUuidFromString(valSale["uuid"]);
|
|
|
|
sale->setSourceNo(jsonValues["source_no"]);
|
|
|
|
sale->setTimestamp(valSale["timestamp"]);
|
2018-08-05 14:52:34 +02:00
|
|
|
for (const auto& valArticle : valSale["articles"]) {
|
|
|
|
auto article = std::make_unique<Article>();
|
2018-10-18 12:06:36 +02:00
|
|
|
article->setUuidFromString(valArticle["uuid"]);
|
|
|
|
article->setSourceNo(jsonValues["source_no"]);
|
|
|
|
article->setArticleNo(valArticle["article_no"]);
|
|
|
|
article->setDescription(valArticle["desc"]);
|
|
|
|
article->setPrice(valArticle["price"]);
|
|
|
|
auto seller = market->findSellerWithUuid(valArticle["seller_uuid"]);
|
2018-08-05 14:52:34 +02:00
|
|
|
if (seller == nullptr) {
|
|
|
|
throw std::runtime_error(
|
|
|
|
"Die zu importierenden Daten verweisen auf einen nicht vorhandenen Verkäufer. "
|
|
|
|
"Die Daten konnten nicht importiert werden.");
|
|
|
|
// continue;
|
|
|
|
}
|
|
|
|
sale->addArticle(article.get());
|
|
|
|
seller->addArticle(std::move(article));
|
|
|
|
}
|
|
|
|
market->getSales().push_back(std::move(sale));
|
|
|
|
}
|
|
|
|
|
|
|
|
market->storeToDb();
|
2018-08-02 11:15:15 +02:00
|
|
|
}
|