export report as csv file implemented
This commit is contained in:
parent
e13339a432
commit
46aab9a836
5 changed files with 75 additions and 2 deletions
|
@ -2,6 +2,7 @@
|
|||
#include "database.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <numeric>
|
||||
#include <sstream>
|
||||
|
@ -133,6 +134,27 @@ void Marketplace::removeSale(boost::uuids::uuid uuid)
|
|||
sales_.end());
|
||||
}
|
||||
|
||||
void Marketplace::exportReportToCSV(const std::string& filename, int feeInPercent, int maxFeeInEuro)
|
||||
{
|
||||
const unsigned char delimiter = ',';
|
||||
std::ofstream file(filename);
|
||||
|
||||
file << "Verk.Nr." << delimiter << "Nachname" << delimiter << "Vorname" << delimiter
|
||||
<< "Anz. gemeldet" << delimiter << "Anz. verkauft" << delimiter << "Umsatz" << delimiter
|
||||
<< "Auszahlung\n";
|
||||
|
||||
for (const auto& seller : sellers_) {
|
||||
file << seller->getSellerNo() << delimiter
|
||||
<< escapeCsvValue(seller->getLastName(), delimiter) << delimiter
|
||||
<< escapeCsvValue(seller->getFirstName(), delimiter) << delimiter
|
||||
<< seller->numArticlesOffered() << delimiter << seller->numArticlesSold() << delimiter
|
||||
<< escapeCsvValue(seller->sumAsString(), delimiter) << delimiter
|
||||
<< escapeCsvValue(paymentAsString(seller->sumInCents(), feeInPercent, maxFeeInEuro),
|
||||
delimiter)
|
||||
<< "\n";
|
||||
}
|
||||
}
|
||||
|
||||
double marketFee(int sum, int percent, int maxFee)
|
||||
{
|
||||
int fee = (sum * percent) / 100.0L;
|
||||
|
@ -156,4 +178,29 @@ std::string paymentAsString(int sumInCent, int percent, int maxFeeInCent)
|
|||
feeStream << std::right << std::setw(10) << std::showbase
|
||||
<< std::put_money(sumInCent - marketFee(sumInCent, percent, maxFeeInCent), false);
|
||||
return feeStream.str();
|
||||
}
|
||||
|
||||
std::string escapeCsvValue(const std::string& value, const char delimiter)
|
||||
{
|
||||
std::stringstream output;
|
||||
bool containsDelim{false};
|
||||
|
||||
if (value.find(delimiter) != std::string::npos || value.find('"') != std::string::npos) {
|
||||
containsDelim = true;
|
||||
output << '"';
|
||||
}
|
||||
|
||||
for (auto& symbol : value) {
|
||||
if (symbol == '"') {
|
||||
output << '"' << symbol;
|
||||
} else {
|
||||
output << symbol;
|
||||
}
|
||||
}
|
||||
|
||||
if (containsDelim) {
|
||||
output << '"';
|
||||
}
|
||||
|
||||
return output.str();
|
||||
}
|
|
@ -41,6 +41,8 @@ class Marketplace
|
|||
void finishCurrentSale(std::unique_ptr<Sale> sale);
|
||||
void removeSale(boost::uuids::uuid uuid);
|
||||
|
||||
void exportReportToCSV(const std::string& filename, int feeInPercent, int maxFeeInEuro);
|
||||
|
||||
private:
|
||||
SellersVec sellers_;
|
||||
SalesVec sales_;
|
||||
|
@ -50,5 +52,6 @@ class Marketplace
|
|||
double marketFee(int sumInCent, int percent, int maxFeeInCent);
|
||||
std::string marketFeeAsString(int sumInCent, int percent, int maxFeeInCent);
|
||||
std::string paymentAsString(int sumInCent, int percent, int maxFeeInCent);
|
||||
std::string escapeCsvValue(const std::string& value, const char delimiter);
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue