kima2/src/core/marketplace.cpp

287 lines
8.3 KiB
C++
Raw Normal View History

2018-07-13 13:04:19 +02:00
#include "marketplace.h"
#include "database.h"
#include "utils.h"
2018-07-13 13:04:19 +02:00
2018-07-16 18:04:25 +02:00
#include <algorithm>
2019-10-07 14:08:01 +02:00
#include <filesystem>
2018-07-31 10:28:07 +02:00
#include <fstream>
2018-07-23 14:18:54 +02:00
#include <iomanip>
2018-07-23 14:18:24 +02:00
#include <numeric>
#include <sstream>
2018-07-16 18:04:25 +02:00
namespace fs = std::filesystem;
2018-07-16 12:00:17 +02:00
Marketplace::Marketplace()
{
2019-10-04 15:15:43 +02:00
/*auto seller = std::make_unique<Seller>("Max", "Mustermann");
2018-07-16 18:04:25 +02:00
seller->createUuid();
2019-10-04 15:15:43 +02:00
sellers_.push_back(std::move(seller)); */
2018-07-16 12:00:17 +02:00
}
void Marketplace::storeToDb(bool onlyDelete)
2018-07-16 12:00:17 +02:00
{
2018-07-17 10:19:41 +02:00
Database db;
db.storeSellers(sellers_, onlyDelete);
2018-07-25 09:31:17 +02:00
db.storeSales(sales_);
2018-07-16 12:00:17 +02:00
}
Database::InitResult Marketplace::loadFromDb()
2018-07-17 11:09:35 +02:00
{
Database db;
db.loadSellers(sellers_);
2018-07-25 09:31:17 +02:00
db.loadSales(sales_, sellers_);
return db.getInitResult();
2018-07-17 11:09:35 +02:00
}
2018-07-16 18:04:25 +02:00
2018-07-20 11:52:26 +02:00
SellersVec& Marketplace::getSellers() { return sellers_; }
2018-07-16 18:04:25 +02:00
2018-07-25 16:04:45 +02:00
SalesVec& Marketplace::getSales() { return sales_; }
2018-07-16 18:04:25 +02:00
int Marketplace::getNextSellerNo()
{
auto iter = std::max_element(
sellers_.begin(), sellers_.end(),
2018-07-20 11:52:26 +02:00
[](const auto& a, const auto& b) -> bool { return a->getSellerNo() < b->getSellerNo(); });
2018-07-17 20:17:46 +02:00
if (iter == sellers_.end())
return 1;
2018-07-16 18:04:25 +02:00
return (*iter)->getSellerNo() + 1;
2018-07-17 15:32:16 +02:00
}
2018-07-22 20:10:22 +02:00
int Marketplace::getNextArticleNo()
{
2018-07-23 13:39:49 +02:00
int maxArtNoInDb{0};
int maxArtNoInBasket{0};
2018-07-23 08:57:35 +02:00
auto iter = std::max_element(sellers_.begin(), sellers_.end(),
[](const auto& a, const auto& b) -> bool {
return a->getMaxArticleNo() < b->getMaxArticleNo();
});
2018-07-23 13:39:49 +02:00
if (iter != sellers_.end())
maxArtNoInDb = (*iter)->getMaxArticleNo();
auto iter2 =
std::max_element(basket_.begin(), basket_.end(), [](const auto& a, const auto& b) -> bool {
return a->getArticleNo() < b->getArticleNo();
});
if (iter2 != basket_.end())
maxArtNoInBasket = (*iter2)->getArticleNo();
return maxArtNoInBasket > maxArtNoInDb ? maxArtNoInBasket + 1 : maxArtNoInDb + 1;
2018-07-22 20:10:22 +02:00
}
2018-07-17 15:32:16 +02:00
int Marketplace::getNumSellersDelete()
{
2018-07-17 20:17:46 +02:00
int count = std::count_if(sellers_.begin(), sellers_.end(),
[](const auto& a) { return a->getState() == Seller::State::DELETE; });
2018-07-17 15:32:16 +02:00
return count;
2018-07-18 09:00:46 +02:00
}
int Marketplace::getNumArticlesSold()
{
int sum = std::accumulate(sellers_.begin(), sellers_.end(), 0, [](int a, const auto& seller) {
return a + seller->numArticlesSold();
});
return sum;
}
2018-08-15 11:25:45 +02:00
int Marketplace::getNumArticlesOffered()
{
int sum = std::accumulate(sellers_.begin(), sellers_.end(), 0, [](int a, const auto& seller) {
return a + seller->numArticlesOffered();
});
return sum;
}
2018-07-20 22:07:42 +02:00
void Marketplace::sortSellers() { std::sort(sellers_.begin(), sellers_.end()); }
Seller* Marketplace::findSellerWithSellerNo(int sellerNo)
{
2018-07-21 21:18:22 +02:00
auto iter = std::find_if(sellers_.begin(), sellers_.end(),
[sellerNo](const auto& a) { return a->getSellerNo() == sellerNo; });
2018-07-20 22:07:42 +02:00
if (iter == sellers_.end())
return nullptr;
return (*iter).get();
2018-07-21 19:24:56 +02:00
}
void Marketplace::addArticleToBasket(std::unique_ptr<Article> article)
{
2019-10-07 14:08:01 +02:00
basket_.insert(basket_.begin(),
std::move(article)); // article to the beginning of the basket vector
2018-07-21 21:18:22 +02:00
}
size_t Marketplace::basketSize() { return basket_.size(); }
2018-07-30 14:43:02 +02:00
void Marketplace::finishCurrentSale(std::unique_ptr<Sale> sale)
2018-07-21 21:18:22 +02:00
{
if (basket_.size() == 0)
return;
for (auto iter = basket_.begin(); iter != basket_.end(); ++iter) {
iter->get()->setSourceNo(sale->getSourceNo()); // update source_no for article in the case
// the user has changed it in the meanwhile
sale->addArticle(iter->get());
2018-07-21 21:18:22 +02:00
(*iter)->getSeller()->addArticle(std::move(*iter));
}
2018-07-23 08:57:35 +02:00
2018-07-21 21:18:22 +02:00
sales_.push_back(std::move(sale));
2018-07-22 20:10:22 +02:00
basket_.clear();
2018-07-23 08:57:35 +02:00
storeToDb();
2018-07-22 20:10:22 +02:00
}
2018-07-23 14:18:24 +02:00
BasketVec& Marketplace::getBasket() { return basket_; }
int Marketplace::getBasketSumInCent()
{
int sum = std::accumulate(basket_.begin(), basket_.end(), 0,
[](int a, const auto& b) { return a + b->getPrice(); });
return sum;
}
std::string Marketplace::getBasketSumAsString()
{
int sumInCent = getBasketSumInCent();
return formatCentAsEuroString(sumInCent);
2018-07-28 11:52:43 +02:00
}
void Marketplace::removeSale(boost::uuids::uuid uuid)
{
sales_.erase(std::remove_if(sales_.begin(), sales_.end(),
[&uuid](const auto& a) { return a->getUuid() == uuid; }),
sales_.end());
2018-07-30 09:50:54 +02:00
}
2018-08-05 14:52:34 +02:00
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 fs::path& filePath, int feeInPercent, int maxFeeInEuro)
2018-07-31 10:28:07 +02:00
{
2019-09-27 08:56:24 +02:00
const char delimiter = ';';
std::ofstream file(filePath);
2018-07-31 10:28:07 +02:00
file << "Verk.Nr." << delimiter << "Nachname" << delimiter << "Vorname" << delimiter
<< "Anz. gemeldet" << delimiter << "Anz. verkauft" << delimiter << "Umsatz" << delimiter
<< "Auszahlung\n";
for (const auto& seller : sellers_) {
file << seller->getSellerNo() << delimiter
<< escapeCsvValue(seller->getLastName(), delimiter) << delimiter
<< escapeCsvValue(seller->getFirstName(), delimiter) << delimiter
<< seller->numArticlesOffered() << delimiter << seller->numArticlesSold() << delimiter
<< escapeCsvValue(seller->sumAsString(), delimiter) << delimiter
2019-10-07 14:08:01 +02:00
<< escapeCsvValue(
paymentAsString(seller->sumInCents(), feeInPercent, maxFeeInEuro * 100),
delimiter)
2018-07-31 10:28:07 +02:00
<< "\n";
}
}
2018-07-31 16:15:48 +02:00
int Marketplace::getOverallSumInCent()
{
int sum = std::accumulate(sellers_.begin(), sellers_.end(), 0,
[](int a, const auto& b) { return a + b->sumInCents(); });
return sum;
}
std::string Marketplace::getOverallSumAsString()
{
int sum = getOverallSumInCent();
return formatCentAsEuroString(sum);
2018-07-31 16:15:48 +02:00
}
int Marketplace::getOverallPaymentInCent(int percent, int maxFee)
{
int sum = std::accumulate(
sellers_.begin(), sellers_.end(), 0, [percent, maxFee](int a, const auto& b) {
return a + b->sumInCents() - marketFee(b->sumInCents(), percent, maxFee);
});
return sum;
}
std::string Marketplace::getOverallPaymentAsString(int percent, int maxFee)
{
int sum = getOverallPaymentInCent(percent, maxFee);
return formatCentAsEuroString(sum);
2018-07-31 16:15:48 +02:00
}
std::string Marketplace::getOverallRevenueAsString(int percent, int maxFee)
{
int sum = getOverallSumInCent();
int pay = getOverallPaymentInCent(percent, maxFee);
return formatCentAsEuroString(sum - pay);
2018-07-31 16:15:48 +02:00
}
2018-07-30 09:50:54 +02:00
double marketFee(int sum, int percent, int maxFee)
{
int fee = (sum * percent) / 100.0L;
2018-07-30 13:40:58 +02:00
if (maxFee <= 0) {
return fee;
}
2018-07-30 09:50:54 +02:00
return fee > maxFee ? maxFee : fee;
}
std::string marketFeeAsString(int sum, int percent, int maxFee)
{
return formatCentAsEuroString(marketFee(sum, percent, maxFee));
2018-07-30 09:50:54 +02:00
}
2018-07-30 15:19:29 +02:00
std::string paymentAsString(int sumInCent, int percent, int maxFeeInCent)
2018-07-30 09:50:54 +02:00
{
return formatCentAsEuroString(sumInCent - marketFee(sumInCent, percent, maxFeeInCent));
2018-07-31 10:28:07 +02:00
}
std::string escapeCsvValue(const std::string& value, const char delimiter)
{
std::stringstream output;
bool containsDelim{false};
if (value.find(delimiter) != std::string::npos || value.find('"') != std::string::npos) {
containsDelim = true;
output << '"';
}
for (auto& symbol : value) {
if (symbol == '"') {
output << '"' << symbol;
} else {
output << symbol;
}
}
if (containsDelim) {
output << '"';
}
return output.str();
2018-08-08 10:22:38 +02:00
}
void Marketplace::clear()
{
/* std::for_each(sellers_.begin(), sellers_.end(), [](auto& seller) {
2018-08-08 10:22:38 +02:00
if (seller->getUuidAsString() == "11111111-1111-1111-1111-111111111111") {
for (auto& article : seller->getArticles()) {
article->setState(Article::State::DELETE);
}
} else {
seller->setState(Seller::State::DELETE);
}
});
std::for_each(sales_.begin(), sales_.end(),
[](auto& sale) { sale->setState(Sale::State::DELETE); });
storeToDb(); */
Database db;
db.newDb();
loadFromDb();
2019-10-04 15:15:43 +02:00
}