44 lines
1.2 KiB
C++
44 lines
1.2 KiB
C++
#include "sale.h"
|
|
#include "utils.h"
|
|
|
|
#include <numeric>
|
|
|
|
void Sale::addArticle(Article *articlePtr)
|
|
{
|
|
articlePtr->setSale(this);
|
|
m_articles.push_back(articlePtr);
|
|
}
|
|
|
|
ArticlesVec &Sale::getArticles() { return m_articles; }
|
|
|
|
void Sale::removeArticle(const Article *articlePtr)
|
|
{
|
|
auto it = std::find(m_articles.begin(), m_articles.end(), articlePtr);
|
|
|
|
if (it != m_articles.end()) {
|
|
(*it)->setSale(nullptr);
|
|
(*it)->setState(
|
|
Article::State::DELETE); // since we only have ad-hoc articles, that have all been sold
|
|
m_articles.erase(it);
|
|
}
|
|
}
|
|
|
|
int Sale::sumInCents()
|
|
{
|
|
int sum = std::accumulate(m_articles.begin(), m_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 m_timestamp; }
|
|
|
|
void Sale::setTimestamp(const std::string ×tamp) { m_timestamp = timestamp; }
|
|
|
|
std::string Sale::getTimestampFormatted() const
|
|
{
|
|
boost::posix_time::ptime time = boost::posix_time::from_iso_extended_string(m_timestamp);
|
|
return boost::posix_time::to_simple_string(time);
|
|
}
|