kima2/src/core/sale.cpp

42 lines
1.1 KiB
C++

#include "sale.h"
#include <numeric>
void Sale::addArticle(std::shared_ptr<Article> articlePtr)
{
articlePtr->setSale(this);
articles_.push_back(articlePtr);
}
std::vector<Article*> Sale::getArticles()
{
std::vector<Article*> articles(articles_.size());
for (const auto& article : articles_) {
articles.push_back(article.get());
}
return articles;
}
void Sale::removeArticle(const Article* articlePtr)
{
auto it = std::find_if(articles_.begin(), articles_.end(),
[&articlePtr](auto art) { return art.get() == articlePtr; });
if (it != articles_.end()) {
(*it)->setSale(nullptr);
(*it)->setState(
Article::State::DELETE); // since we only have ad-hoc articles, that have all been sold
articles_.erase(it);
}
}
int Sale::sumInCents()
{
int sum = std::accumulate(articles_.begin(), articles_.end(), 0,
[](int a, std::shared_ptr<Article> b) { return a + b->getPrice(); });
return sum;
}
std::string Sale::getTimestamp() { return timestamp_; }
void Sale::setTimestamp(const std::string& timestamp) { timestamp_ = timestamp; }