kima2/src/core/marketplace.cpp
2018-07-28 11:52:43 +02:00

137 lines
No EOL
3.9 KiB
C++

#include "marketplace.h"
#include "database.h"
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <sstream>
Marketplace::Marketplace()
{
auto seller = std::make_unique<Seller>("Max", "Mustermann");
seller->createUuid();
sellers_.push_back(std::move(seller));
}
void Marketplace::storeToDb(bool onlyDelete)
{
Database db;
db.storeSellers(sellers_, onlyDelete);
db.storeSales(sales_);
}
void Marketplace::loadFromDb()
{
Database db;
db.loadSellers(sellers_);
db.loadSales(sales_, sellers_);
}
SellersVec& Marketplace::getSellers() { return sellers_; }
SalesVec& Marketplace::getSales() { return sales_; }
int Marketplace::getNextSellerNo()
{
auto iter = std::max_element(
sellers_.begin(), sellers_.end(),
[](const auto& a, const auto& b) -> bool { return a->getSellerNo() < b->getSellerNo(); });
if (iter == sellers_.end())
return 1;
return (*iter)->getSellerNo() + 1;
}
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;
}
int Marketplace::getNumSellersDelete()
{
int count = std::count_if(sellers_.begin(), sellers_.end(),
[](const auto& a) { return a->getState() == Seller::State::DELETE; });
return count;
}
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();
}
void Marketplace::addArticleToBasket(std::unique_ptr<Article> article)
{
basket_.push_back(std::move(article));
}
size_t Marketplace::basketSize() { return basket_.size(); }
void Marketplace::finishCurrentSale()
{
if (basket_.size() == 0)
return;
auto sale = std::make_unique<Sale>();
sale->createUuid();
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));
basket_.clear();
storeToDb();
}
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();
std::stringstream sumStream;
// sumStream.imbue(std::locale("de_DE.utf8"));
sumStream << std::right << std::setw(12) << std::showbase << std::put_money(sumInCent, false);
return sumStream.str();
}
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());
}