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.

287 lines
8.4 KiB

#include "marketplace.h"
#include "database.h"
#include "utils.h"
4 years ago
#include <algorithm>
#include <fstream>
4 years ago
#include <iomanip>
4 years ago
#include <numeric>
#include <sstream>
4 years ago
namespace fs = std::filesystem;
Marketplace::Marketplace()
{
4 years ago
auto seller = std::make_unique<Seller>("Max", "Mustermann");
4 years ago
seller->createUuid();
4 years ago
sellers_.push_back(std::move(seller));
}
void Marketplace::storeToDb(bool onlyDelete)
{
Database db;
db.storeSellers(sellers_, onlyDelete);
4 years ago
db.storeSales(sales_);
}
4 years ago
void Marketplace::loadFromDb()
{
Database db;
db.loadSellers(sellers_);
4 years ago
db.loadSales(sales_, sellers_);
4 years ago
}
4 years ago
4 years ago
SellersVec& Marketplace::getSellers() { return sellers_; }
4 years ago
SalesVec& Marketplace::getSales() { return sales_; }
4 years ago
int Marketplace::getNextSellerNo()
{
auto iter = std::max_element(
sellers_.begin(), sellers_.end(),
4 years ago
[](const auto& a, const auto& b) -> bool { return a->getSellerNo() < b->getSellerNo(); });
4 years ago
if (iter == sellers_.end())
return 1;
4 years ago
return (*iter)->getSellerNo() + 1;
}
4 years ago
int Marketplace::getNextArticleNo()
{
int maxArtNoInDb{0};
int maxArtNoInBasket{0};
auto iter = std::max_element(sellers_.begin(), sellers_.end(),
[](const auto& a, const auto& b) -> bool {
return a->getMaxArticleNo() < b->getMaxArticleNo();
});
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;
4 years ago
}
int Marketplace::getNumSellersDelete()
{
4 years ago
int count = std::count_if(sellers_.begin(), sellers_.end(),
[](const auto& a) { return a->getState() == Seller::State::DELETE; });
return count;
4 years ago
}
int Marketplace::getNumArticlesSold()
{
int sum = std::accumulate(sellers_.begin(), sellers_.end(), 0, [](int a, const auto& seller) {
return a + seller->numArticlesSold();
});
return sum;
}
int Marketplace::getNumArticlesOffered()
{
int sum = std::accumulate(sellers_.begin(), sellers_.end(), 0, [](int a, const auto& seller) {
return a + seller->numArticlesOffered();
});
return sum;
}
void Marketplace::sortSellers() { std::sort(sellers_.begin(), sellers_.end()); }
Seller* Marketplace::findSellerWithSellerNo(int sellerNo)
{
auto iter = std::find_if(sellers_.begin(), sellers_.end(),
[sellerNo](const auto& a) { return a->getSellerNo() == sellerNo; });
if (iter == sellers_.end())
return nullptr;
return (*iter).get();
4 years ago
}
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();
}
4 years ago
void Marketplace::addArticleToBasket(std::unique_ptr<Article> article)
{
basket_.push_back(std::move(article));
}
size_t Marketplace::basketSize() { return basket_.size(); }
void Marketplace::finishCurrentSale(std::unique_ptr<Sale> sale)
{
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());
(*iter)->getSeller()->addArticle(std::move(*iter));
}
sales_.push_back(std::move(sale));
4 years ago
basket_.clear();
storeToDb();
4 years ago
}
4 years ago
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);
4 years ago
}
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());
}
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)
{
4 years ago
const char delimiter = ',';
std::ofstream file(filePath);
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
<< escapeCsvValue(paymentAsString(seller->sumInCents(), feeInPercent, maxFeeInEuro),
delimiter)
<< "\n";
}
}
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);
}
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);
}
std::string Marketplace::getOverallRevenueAsString(int percent, int maxFee)
{
int sum = getOverallSumInCent();
int pay = getOverallPaymentInCent(percent, maxFee);
return formatCentAsEuroString(sum - pay);
}
double marketFee(int sum, int percent, int maxFee)
{
int fee = (sum * percent) / 100.0L;
if (maxFee <= 0) {
return fee;
}
return fee > maxFee ? maxFee : fee;
}
std::string marketFeeAsString(int sum, int percent, int maxFee)
{
return formatCentAsEuroString(marketFee(sum, percent, maxFee));
}
std::string paymentAsString(int sumInCent, int percent, int maxFeeInCent)
{
return formatCentAsEuroString(sumInCent - marketFee(sumInCent, percent, maxFeeInCent));
}
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();
}
void Marketplace::clear()
{
std::for_each(sellers_.begin(), sellers_.end(), [](auto& seller) {
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();
4 years ago
}