KIMA2 ist ein kleines Kassenprogramm für Kindersachenmärkte. https://www.rustysoft.de/?01_kima2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

135 lines
4.7 KiB

#include "jsonutil.h"
#include "database.h"
#include <json/json.h>
#include <fstream>
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<Json::StreamWriter> writer(builder.newStreamWriter());
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->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();
}
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<Json::StreamWriter> writer(builder.newStreamWriter());
root["source_no"] = cashPointNo;
for (const auto& sale : market->getSales()) {
if (sale->getSourceNo() != cashPointNo)
continue;
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.");
}
market->setSalesToDelete(jsonValues["source_no"].asInt());
market->storeToDb();
for (const auto& valSale : jsonValues["sales"]) {
auto sale = std::make_unique<Sale>();
sale->setUuidFromString(valSale["uuid"].asString());
sale->setSourceNo(jsonValues["source_no"].asInt());
sale->setTimestamp(valSale["timestamp"].asString());
for (const auto& valArticle : valSale["articles"]) {
auto article = std::make_unique<Article>();
article->setUuidFromString(valArticle["uuid"].asString());
article->setSourceNo(jsonValues["source_no"].asInt());
article->setArticleNo(valArticle["article_no"].asInt());
article->setDescription(valArticle["desc"].asString());
article->setPrice(valArticle["price"].asInt());
auto seller = market->findSellerWithUuid(valArticle["seller_uuid"].asString());
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();
}