kima2/src/core/jsonutil.cpp

138 lines
4.7 KiB
C++

#include "jsonutil.h"
#include "database.h"
#include <nlohmann/json.hpp>
#include <fstream>
namespace fs = std::filesystem;
using json = nlohmann::json;
void JsonUtil::exportSellers(const std::filesystem::path& filePath, Marketplace* market)
{
json root;
std::ofstream file(filePath);
for (const auto& seller : market->getSellers()) {
json newEntry;
newEntry["seller_no"] = seller->getSellerNo();
newEntry["last_name"] = seller->getLastName();
newEntry["first_name"] = seller->getFirstName();
newEntry["num_offered_articles"] = seller->numArticlesOffered();
root["sellers"].push_back(newEntry);
}
file << root.dump(4) << std::endl;
}
std::size_t JsonUtil::importSellers(const std::filesystem::path& filePath, Marketplace* market)
{
for (auto& seller : market->getSellers()) {
seller->setState(Seller::State::DELETE);
}
market->storeToDb(true);
std::ifstream file(filePath);
json jsonValues = json::parse(file);
for (auto val : jsonValues["sellers"]) {
auto seller = std::make_unique<Seller>();
seller->setSellerNo(val["seller_no"]);
seller->setLastName(val["last_name"]);
seller->setFirstName(val["first_name"]);
seller->setNumArticlesOffered(val["num_offered_articles"]);
market->getSellers().push_back(std::move(seller));
}
// If there was no special seller "Sonderkonto" in import data, then create one
auto specialSeller = market->findSellerWithSellerNo(0);
if (!specialSeller) {
auto seller = std::make_unique<Seller>();
seller->setSellerNo(0);
seller->setLastName("Sonderkonto");
seller->setFirstName("Sonderkonto");
seller->setNumArticlesOffered(0);
market->getSellers().push_back(std::move(seller));
}
market->sortSellers();
market->storeToDb();
return market->getSellers().size() - 1; // minus 1 because we don't count the "special" seller
}
void JsonUtil::exportSales(const std::filesystem::path& filePath, Marketplace* market, int cashPointNo)
{
json root;
std::ofstream file(filePath);
root["source_no"] = cashPointNo;
for (const auto& sale : market->getSales()) {
if (sale->getSourceNo() != cashPointNo)
continue;
json newSale;
newSale["uuid"] = sale->getUuidAsString();
newSale["timestamp"] = sale->getTimestamp();
for (const auto& article : sale->getArticles()) {
json newArticle;
newArticle["uuid"] = article->getUuidAsString();
newArticle["seller_no"] = article->getSeller()->getSellerNo();
newArticle["desc"] = article->getDescription();
newArticle["price"] = article->getPrice();
// newArticle["source_no"] = article->getSourceNo();
newArticle["article_no"] = article->getArticleNo();
newSale["articles"].push_back(newArticle);
}
root["sales"].push_back(newSale);
}
file << root.dump(4) << std::endl;
}
void JsonUtil::importSales(const std::filesystem::path& filePath, Marketplace* market, int cashPointNo)
{
std::ifstream file(filePath);
json jsonValues = json::parse(file);
int source_no = jsonValues["source_no"];
if (source_no == cashPointNo) {
throw std::runtime_error("Die Kassen-Nr. der zu imporierenden Daten wird von dieser Kasse "
"hier bereits verwendet.");
}
market->setSalesToDelete(jsonValues["source_no"]);
market->storeToDb();
for (const auto& valSale : jsonValues["sales"]) {
auto sale = std::make_unique<Sale>();
sale->setUuidFromString(valSale["uuid"]);
sale->setSourceNo(jsonValues["source_no"]);
sale->setTimestamp(valSale["timestamp"]);
for (const auto& valArticle : valSale["articles"]) {
auto article = std::make_unique<Article>();
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->findSellerWithSellerNo(valArticle["seller_no"]);
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();
}