kima2/src/core/sale.cpp

43 lines
1.2 KiB
C++
Raw Normal View History

2018-07-12 13:34:17 +02:00
#include "sale.h"
#include <numeric>
2018-07-20 11:52:26 +02:00
void Sale::addArticle(Article* articlePtr)
2018-07-12 13:42:22 +02:00
{
2018-07-12 14:39:08 +02:00
articlePtr->setSale(this);
2018-07-12 13:42:22 +02:00
articles_.push_back(articlePtr);
}
2018-07-28 11:52:43 +02:00
ArticlesVec& Sale::getArticles() { return articles_; }
2018-07-13 10:51:07 +02:00
2018-07-13 10:12:16 +02:00
void Sale::removeArticle(const Article* articlePtr)
{
2018-07-20 11:52:26 +02:00
/* auto it = std::find_if(articles_.begin(), articles_.end(),
[&articlePtr](auto art) { return art.get() == articlePtr; }); */
auto it = std::find(articles_.begin(), articles_.end(), articlePtr);
2018-07-13 10:12:16 +02:00
if (it != articles_.end()) {
(*it)->setSale(nullptr);
(*it)->setState(
Article::State::DELETE); // since we only have ad-hoc articles, that have all been sold
2018-07-13 10:12:16 +02:00
articles_.erase(it);
}
}
2018-07-12 13:34:17 +02:00
int Sale::sumInCents()
{
2018-07-12 13:42:22 +02:00
int sum = std::accumulate(articles_.begin(), articles_.end(), 0,
2018-07-20 11:52:26 +02:00
[](int a, const Article* b) { return a + b->getPrice(); });
2018-07-12 13:42:22 +02:00
return sum;
2018-07-13 10:51:07 +02:00
}
2018-07-25 16:04:45 +02:00
std::string Sale::sumAsString()
{
std::stringstream sumStream;
sumStream << std::right << std::setw(12) << std::showbase << std::put_money(sumInCents(), false);
return sumStream.str();
}
2018-07-13 10:51:07 +02:00
std::string Sale::getTimestamp() { return timestamp_; }
void Sale::setTimestamp(const std::string& timestamp) { timestamp_ = timestamp; }