import export nearly finished

This commit is contained in:
Martin Brodbeck 2018-08-05 14:52:34 +02:00
parent de91a804a0
commit 3ba230d9b9
5 changed files with 72 additions and 5 deletions

View file

@ -491,8 +491,6 @@ unsigned int Database::storeSales(std::vector<std::unique_ptr<Sale>>& sales)
++count;
sqlite3_finalize(stmt);
}
// Maybe not necessary (because of DELETE CASCADE): Delete here individual articles
}
endTransaction();

View file

@ -66,6 +66,9 @@ void JsonUtil::exportSales(const std::string& filename, Marketplace* market, int
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();
@ -100,5 +103,33 @@ void JsonUtil::importSales(const std::string& filename, Marketplace* market, int
"hier bereits verwendet.");
}
// TODO: Import sales
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();
}

View file

@ -84,6 +84,15 @@ Seller* Marketplace::findSellerWithSellerNo(int sellerNo)
return (*iter).get();
}
Seller* Marketplace::findSellerWithUuid(const std::string& uuid)
{
auto iter = std::find_if(sellers_.begin(), sellers_.end(),
[uuid](const auto& a) { return a->getUuidAsString() == uuid; });
if (iter == sellers_.end())
return nullptr;
return (*iter).get();
}
void Marketplace::addArticleToBasket(std::unique_ptr<Article> article)
{
basket_.push_back(std::move(article));
@ -129,6 +138,18 @@ void Marketplace::removeSale(boost::uuids::uuid uuid)
sales_.end());
}
void Marketplace::setSalesToDelete(int cashPointNo)
{
std::for_each(sales_.begin(), sales_.end(), [cashPointNo](auto& sale) {
if (sale->getSourceNo() == cashPointNo) {
sale->setState(Sale::State::DELETE);
for (auto& article : sale->getArticles()) {
article->setState(Article::State::DELETE);
}
}
});
}
void Marketplace::exportReportToCSV(const std::string& filename, int feeInPercent, int maxFeeInEuro)
{
const char delimiter = ',';

View file

@ -35,10 +35,12 @@ class Marketplace
void sortSellers();
Seller* findSellerWithSellerNo(int sellerNo);
Seller* findSellerWithUuid(const std::string& uuid);
void addArticleToBasket(std::unique_ptr<Article> article);
size_t basketSize();
void finishCurrentSale(std::unique_ptr<Sale> sale);
void removeSale(boost::uuids::uuid uuid);
void setSalesToDelete(int cashPointNo);
int getOverallSumInCent();
std::string getOverallSumAsString();

View file

@ -11,8 +11,8 @@
#include <excelreader.h>
#include <regex>
#include <exception>
#include <regex>
#include <QFileDialog>
#include <QMessageBox>
@ -278,6 +278,9 @@ void MainWindow::onImportSellerJsonActionTriggered()
auto filename = QFileDialog::getOpenFileName(this, "Verkäufer importieren", QString(),
"JSON Dateien (*.json)");
if (filename.isEmpty())
return;
JsonUtil::importSellers(filename.toStdString(), marketplace_.get());
}
@ -286,6 +289,9 @@ void MainWindow::onExportSellerJsonActionTriggered()
auto filename = QFileDialog::getSaveFileName(this, "Verkäufer exportieren", QString(),
"JSON Dateien (*.json)");
if (filename.isEmpty())
return;
JsonUtil::exportSellers(filename.toStdString(), marketplace_.get());
}
@ -295,6 +301,10 @@ void MainWindow::onExportSalesJsonActionTriggered()
auto filename = QFileDialog::getSaveFileName(this, "Umsätze/Transaktionen exportieren",
QString(), "JSON Dateien (*.json)");
if (filename.isEmpty())
return;
JsonUtil::exportSales(filename.toStdString(), marketplace_.get(),
settings.value("global/cashPointNo").toInt());
}
@ -314,12 +324,17 @@ void MainWindow::onImportSalesJsonActionTriggered()
auto filename = QFileDialog::getOpenFileName(this, "Umsätze/Transaktionen importieren",
QString(), "JSON Dateien (*.json)");
if (filename.isEmpty())
return;
delete ui_.salesView->model();
try {
JsonUtil::importSales(filename.toStdString(), marketplace_.get(),
settings.value("global/cashPointNo").toInt());
settings.value("global/cashPointNo").toInt());
} catch (std::runtime_error& err) {
QMessageBox(QMessageBox::Icon::Warning, "Import nicht möglich", err.what(), QMessageBox::Ok,
this)
.exec();
}
setSaleModel();
}