#include "excelreader.h" #include "utils.h" #include #include namespace fs = std::filesystem; std::size_t ExcelReader::readSellersFromFile(const fs::path& filePath, Marketplace* market) { xlnt::workbook wb; std::ifstream mystream(filePath, std::ios::binary); if (!mystream.is_open()) { throw std::runtime_error("Could not open Excel file"); } wb.load(mystream); for (auto& seller : market->getSellers()) { seller->setState(Seller::State::DELETE); } market->storeToDb(true); auto ws = wb.sheet_by_index(0); const int START_ROW = 5; const int END_ROW = 504; int rowCount{}; for (const auto& row : ws.rows(false)) { if (rowCount < START_ROW) { ++rowCount; continue; } else if (rowCount > END_ROW) { break; } if (row[2].value().empty() && row[3].value().empty()) { ++rowCount; continue; } auto seller = std::make_unique(); seller->setSellerNo(row[0].value()); seller->setNumArticlesOffered(row[1].value()); std::string firstName = row[2].value(); seller->setFirstName(trim(firstName)); std::string lastName = row[3].value(); 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->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 }