From 8265212c114c9ac0684acb608a3208637db99c1c Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Thu, 26 Sep 2019 16:41:39 +0200 Subject: [PATCH] added importing from csv --- 3rdparty/CMakeLists.txt | 1 + 3rdparty/csv-parser | 2 +- src/core/CMakeLists.txt | 9 ++++-- src/core/csvreader.cpp | 66 +++++++++++++++++++++++++++++++++++++++++ src/core/csvreader.h | 18 +++++++++++ src/core/utils.cpp | 10 +++++++ src/core/utils.h | 1 + src/gui/mainwindow.cpp | 11 +++++-- 8 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 src/core/csvreader.cpp create mode 100644 src/core/csvreader.h diff --git a/3rdparty/CMakeLists.txt b/3rdparty/CMakeLists.txt index ad35ca6..94fa640 100644 --- a/3rdparty/CMakeLists.txt +++ b/3rdparty/CMakeLists.txt @@ -2,3 +2,4 @@ if(NOT KIMA2_USE_EXTERNAL_JSON) set(JSON_BuildTests OFF CACHE INTERNAL "") add_subdirectory(nlohmann_json EXCLUDE_FROM_ALL) endif() +add_subdirectory(csv-parser) diff --git a/3rdparty/csv-parser b/3rdparty/csv-parser index dab0416..e4a899d 160000 --- a/3rdparty/csv-parser +++ b/3rdparty/csv-parser @@ -1 +1 @@ -Subproject commit dab0416d838b560ac7cd7090a32081d0a1e1c919 +Subproject commit e4a899dcafcfa14e448348e9b3c8c06d7697dbf8 diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index cb0d4c1..3d9fa92 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -18,17 +18,20 @@ set(CORE_SOURCES sale.cpp marketplace.cpp excelreader.cpp + csvreader.cpp jsonutil.cpp utils.cpp ) -add_library(core STATIC ${CORE_SOURCES}) +add_library(core STATIC ${CORE_SOURCES}) +target_include_directories(core PRIVATE ${PROJECT_SOURCE_DIR}/3rdparty/csv-parser/include) if (WIN32) - target_link_libraries(core PRIVATE printer Boost::boost Boost::date_time sqlite3 nlohmann_json::nlohmann_json ${XLNT_LIBRARY}) + target_link_libraries(core PRIVATE printer Boost::boost Boost::date_time sqlite3 nlohmann_json::nlohmann_json ${XLNT_LIBRARY} csv) target_link_libraries(core PRIVATE bcrypt) else() - target_link_libraries(core PRIVATE printer Boost::boost Boost::date_time sqlite3 nlohmann_json::nlohmann_json ${XLNT_LIBRARIES}) + target_link_libraries(core PRIVATE printer Boost::boost Boost::date_time sqlite3 nlohmann_json::nlohmann_json ${XLNT_LIBRARIES} csv) endif() +#target_include_directories(core PRIVATE ${PROJECT_SOURCE_DIR}/3rdparty/csv-parser) target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/core/csvreader.cpp b/src/core/csvreader.cpp new file mode 100644 index 0000000..285caa5 --- /dev/null +++ b/src/core/csvreader.cpp @@ -0,0 +1,66 @@ +#include "csvreader.h" +#include "utils.h" + +#include +#include + +namespace fs = std::filesystem; + +std::size_t CsvReader::readSellersFromFile(const fs::path& filePath, Marketplace* market) +{ + csv::CSVFormat format; + format.delimiter(';'); + + csv::CSVReader csvReader(filePath.string(), format); + + 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().empty() && row[3].get().empty()) { + ++rowCount; + continue; + } + + auto seller = std::make_unique(); + seller->createUuid(); + seller->setSellerNo(row[0].get()); + if (row[1].is_int()) { + seller->setNumArticlesOffered(row[1].get()); + } else { + seller->setNumArticlesOffered(0); + } + std::string firstName = row[2].get(); + seller->setFirstName(trim(firstName)); + std::string lastName = row[3].get(); + 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->findSellerWithUuid("11111111-1111-1111-1111-111111111111"); + if (!specialSeller) { + auto seller = std::make_unique(); + seller->setUuidFromString("11111111-1111-1111-1111-111111111111"); + 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 +} diff --git a/src/core/csvreader.h b/src/core/csvreader.h new file mode 100644 index 0000000..4c88551 --- /dev/null +++ b/src/core/csvreader.h @@ -0,0 +1,18 @@ +#ifndef CSV_READER_H +#define CSV_READER_H + +#include "marketplace.h" +#include "seller.h" + +#include +#include +#include +#include + +class CsvReader +{ + public: + static std::size_t readSellersFromFile(const std::filesystem::path& filePath, Marketplace* market); +}; + +#endif \ No newline at end of file diff --git a/src/core/utils.cpp b/src/core/utils.cpp index ccb0d34..498e34f 100644 --- a/src/core/utils.cpp +++ b/src/core/utils.cpp @@ -2,6 +2,7 @@ #include #include +#include std::string formatCentAsEuroString(const int cent, int width) { @@ -60,4 +61,13 @@ std::string& rtrim(std::string& str, const std::string& chars) std::string& trim(std::string& str, const std::string& chars) { return ltrim(rtrim(str, chars), chars); +} + +bool case_insensitive_match(std::string s1, std::string s2) { + //convert s1 and s2 into lower case strings + transform(s1.begin(), s1.end(), s1.begin(), ::tolower); + transform(s2.begin(), s2.end(), s2.begin(), ::tolower); + if(s1.compare(s2) == 0) + return true; //The strings are same + return false; //not matched } \ No newline at end of file diff --git a/src/core/utils.h b/src/core/utils.h index f227da4..7ea6499 100644 --- a/src/core/utils.h +++ b/src/core/utils.h @@ -13,5 +13,6 @@ std::optional convertToPosPrinterDevice(const std::string& vendor std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r "); std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r "); std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r "); +bool case_insensitive_match(std::string s1, std::string s2); #endif diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 1e63ea8..c0fb637 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -445,14 +446,20 @@ void MainWindow::onImportSellerExcelActionTriggered() .exec(); auto filename = QFileDialog::getOpenFileName(this, "Verkäufer importieren", QString(), - "Excel Dateien (*.xlsx *.xls)"); + "Excel Dateien (*.xlsx);CSV Dateien (*.csv)"); if (filename.isEmpty()) return; fs::path filePath(filename.toStdWString()); - std::size_t numImported = ExcelReader::readSellersFromFile(filePath, marketplace_.get()); + std::size_t numImported{}; + if (case_insensitive_match(filePath.extension().string(), std::string("xslx"))) { + numImported = ExcelReader::readSellersFromFile(filePath, marketplace_.get()); + } else { + numImported = CsvReader::readSellersFromFile(filePath, marketplace_.get()); + } + updateStatLabel(); using namespace std::string_literals;