kima2/src/core/sale.cpp

44 lines
1.2 KiB
C++
Raw Normal View History

2018-07-12 13:34:17 +02:00
#include "sale.h"
#include "utils.h"
2018-07-12 13:34:17 +02:00
#include <numeric>
2022-07-07 15:21:46 +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);
2022-07-07 15:21:46 +02:00
m_articles.push_back(articlePtr);
2018-07-12 13:42:22 +02:00
}
2022-07-07 15:21:46 +02:00
ArticlesVec &Sale::getArticles() { return m_articles; }
2018-07-13 10:51:07 +02:00
2022-07-07 15:21:46 +02:00
void Sale::removeArticle(const Article *articlePtr)
2018-07-13 10:12:16 +02:00
{
2022-07-07 15:21:46 +02:00
auto it = std::find(m_articles.begin(), m_articles.end(), articlePtr);
2018-07-20 11:52:26 +02:00
2022-07-07 15:21:46 +02:00
if (it != m_articles.end()) {
2018-07-13 10:12:16 +02:00
(*it)->setSale(nullptr);
(*it)->setState(
Article::State::DELETE); // since we only have ad-hoc articles, that have all been sold
2022-07-07 15:21:46 +02:00
m_articles.erase(it);
2018-07-13 10:12:16 +02:00
}
}
2018-07-12 13:34:17 +02:00
int Sale::sumInCents()
{
2022-07-07 15:21:46 +02:00
int sum = std::accumulate(m_articles.begin(), m_articles.end(), 0,
[](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
}
std::string Sale::sumAsString() { return formatCentAsEuroString(sumInCents()); }
2018-07-25 16:04:45 +02:00
2022-07-07 15:21:46 +02:00
std::string Sale::getTimestamp() const { return m_timestamp; }
2018-07-13 10:51:07 +02:00
2022-07-07 15:21:46 +02:00
void Sale::setTimestamp(const std::string &timestamp) { m_timestamp = timestamp; }
2018-08-06 21:31:40 +02:00
std::string Sale::getTimestampFormatted() const
{
2022-07-07 15:21:46 +02:00
boost::posix_time::ptime time = boost::posix_time::from_iso_extended_string(m_timestamp);
2018-08-06 21:31:40 +02:00
return boost::posix_time::to_simple_string(time);
}