kima2/src/core/sale.cpp

46 lines
1.3 KiB
C++

#include "sale.h"
#include "utils.h"
#include <numeric>
void Sale::addArticle(Article* articlePtr)
{
articlePtr->setSale(this);
articles_.push_back(articlePtr);
}
ArticlesVec& Sale::getArticles() { return articles_; }
void Sale::removeArticle(const Article* articlePtr)
{
/* 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);
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, const Article* b) { return a + b->getPrice(); });
return sum;
}
std::string Sale::sumAsString() { return formatCentAsEuroString(sumInCents()); }
std::string Sale::getTimestamp() const { return timestamp_; }
void Sale::setTimestamp(const std::string& timestamp) { timestamp_ = timestamp; }
std::string Sale::getTimestampFormatted() const
{
boost::posix_time::ptime time = boost::posix_time::from_iso_extended_string(timestamp_);
return boost::posix_time::to_simple_string(time);
}