KIMA2 ist ein kleines Kassenprogramm für Kindersachenmärkte. https://www.rustysoft.de/?01_kima2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

72 lines
2.1 KiB

#include "csvreader.h"
#include "utils.h"
#include <fstream>
#include <csv.hpp>
namespace fs = std::filesystem;
std::size_t CsvReader::readSellersFromFile(const fs::path& filePath, Marketplace* market)
{
csv::CSVFormat format;
format.delimiter(';');
#if defined(_WIN64) || defined(_WIN32)
// Windows: Somhow this is necessary in order to open file names with umlauts
auto wide = filePath.wstring();
std::string fileName(wide.begin(), wide.end());
csv::CSVReader csvReader(fileName, format);
#else
csv::CSVReader csvReader(filePath.string(), format);
#endif
for (auto& seller : market->getSellers()) {
seller->setState(Seller::State::DELETE);
}
market->storeToDb(true);
int rowCount{};
for (csv::CSVRow &row : csvReader) {
if (!row[0].is_int()) {
++rowCount;
continue;
}
if (row[2].get<std::string>().empty() && row[3].get<std::string>().empty()) {
++rowCount;
continue;
}
auto seller = std::make_unique<Seller>();
seller->setSellerNo(row[0].get<int>());
if (row[1].is_int()) {
seller->setNumArticlesOffered(row[1].get<int>());
} else {
seller->setNumArticlesOffered(0);
}
std::string firstName = row[2].get<std::string>();
seller->setFirstName(trim(firstName));
std::string lastName = row[3].get<std::string>();
seller->setLastName(trim(lastName));
market->getSellers().push_back(std::move(seller));
rowCount++;
}
// If there was no special seller "Sonderkonto" in import data, then create one
auto specialSeller = market->findSellerWithSellerNo(0);
if (!specialSeller) {
auto seller = std::make_unique<Seller>();
seller->setSellerNo(0);
seller->setLastName("Sonderkonto");
seller->setFirstName("Sonderkonto");
seller->setNumArticlesOffered(0);
market->getSellers().push_back(std::move(seller));
}
market->sortSellers();
market->storeToDb();
return market->getSellers().size() - 1; // minus 1 because we don't count the "special" seller
}