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.

247 lines
7.5 KiB

#include "marketplace.h"
#include "database.h"
4 years ago
#include <algorithm>
#include <fstream>
4 years ago
#include <iomanip>
4 years ago
#include <numeric>
#include <sstream>
4 years ago
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
}
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
}
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) {
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();
// double sumInEuro = sumInCent / 100.0L;
// std::stringstream sumStream;
// sumStream << std::fixed << std::setprecision(2) << sumInEuro << " €";
// return sumStream.str();
4 years ago
std::stringstream sumStream;
4 years ago
// sumStream.imbue(std::locale("de_DE.utf8"));
sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sumInCent, false);
4 years ago
return sumStream.str();
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::exportReportToCSV(const std::string& filename, int feeInPercent, int maxFeeInEuro)
{
4 years ago
const char delimiter = ',';
std::ofstream file(filename);
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();
std::stringstream sumStream;
sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sum, false);
return sumStream.str();
}
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);
std::stringstream sumStream;
sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sum, false);
return sumStream.str();
}
std::string Marketplace::getOverallRevenueAsString(int percent, int maxFee)
{
int sum = getOverallSumInCent();
int pay = getOverallPaymentInCent(percent, maxFee);
std::stringstream sumStream;
sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sum - pay, false);
return sumStream.str();
}
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)
{
std::stringstream feeStream;
feeStream << std::right << std::setw(10) << std::showbase
<< std::put_money(marketFee(sum, percent, maxFee), false);
return feeStream.str();
}
std::string paymentAsString(int sumInCent, int percent, int maxFeeInCent)
{
std::stringstream feeStream;
feeStream << std::right << std::setw(10) << std::showbase
<< std::put_money(sumInCent - marketFee(sumInCent, percent, maxFeeInCent), false);
return feeStream.str();
}
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();
4 years ago
}