kima2/src/core/marketplace.cpp

137 lines
3.9 KiB
C++
Raw Normal View History

2018-07-13 13:04:19 +02:00
#include "marketplace.h"
#include "database.h"
2018-07-16 18:04:25 +02:00
#include <algorithm>
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
2018-07-16 12:00:17 +02:00
Marketplace::Marketplace()
{
2018-07-16 15:30:24 +02:00
auto seller = std::make_unique<Seller>("Max", "Mustermann");
2018-07-16 18:04:25 +02:00
seller->createUuid();
2018-07-16 15:30:24 +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
}
2018-07-17 11:09:35 +02:00
void Marketplace::loadFromDb()
{
Database db;
db.loadSellers(sellers_);
2018-07-25 09:31:17 +02:00
db.loadSales(sales_, sellers_);
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
}
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)
{
basket_.push_back(std::move(article));
2018-07-21 21:18:22 +02:00
}
size_t Marketplace::basketSize() { return basket_.size(); }
void Marketplace::finishCurrentSale()
{
if (basket_.size() == 0)
return;
auto sale = std::make_unique<Sale>();
2018-07-25 09:31:17 +02:00
sale->createUuid();
2018-07-21 21:18:22 +02:00
for (auto iter = basket_.begin(); iter != basket_.end(); ++iter) {
sale->addArticle((*iter).get());
(*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();
2018-07-24 07:50:51 +02:00
// double sumInEuro = sumInCent / 100.0L;
// std::stringstream sumStream;
// sumStream << std::fixed << std::setprecision(2) << sumInEuro << " €";
// return sumStream.str();
2018-07-23 14:18:24 +02:00
std::stringstream sumStream;
2018-07-28 11:52:43 +02:00
// sumStream.imbue(std::locale("de_DE.utf8"));
2018-07-24 07:50:51 +02:00
sumStream << std::right << std::setw(12) << std::showbase << std::put_money(sumInCent, false);
2018-07-23 14:18:24 +02:00
return sumStream.str();
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-23 14:18:24 +02:00
}