Compare commits

...

2 commits

Author SHA1 Message Date
8265212c11 added importing from csv 2019-09-26 16:41:39 +02:00
780e37f565 added importing from csv 2019-09-26 16:41:28 +02:00
9 changed files with 115 additions and 5 deletions

3
.gitmodules vendored
View file

@ -4,3 +4,6 @@
[submodule "3rdparty/singleapplication"] [submodule "3rdparty/singleapplication"]
path = 3rdparty/singleapplication path = 3rdparty/singleapplication
url = https://github.com/itay-grudev/SingleApplication.git url = https://github.com/itay-grudev/SingleApplication.git
[submodule "3rdparty/csv-parser"]
path = 3rdparty/csv-parser
url = https://github.com/vincentlaucsb/csv-parser.git

View file

@ -2,3 +2,4 @@ if(NOT KIMA2_USE_EXTERNAL_JSON)
set(JSON_BuildTests OFF CACHE INTERNAL "") set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(nlohmann_json EXCLUDE_FROM_ALL) add_subdirectory(nlohmann_json EXCLUDE_FROM_ALL)
endif() endif()
add_subdirectory(csv-parser)

1
3rdparty/csv-parser vendored Submodule

@ -0,0 +1 @@
Subproject commit e4a899dcafcfa14e448348e9b3c8c06d7697dbf8

View file

@ -18,17 +18,20 @@ set(CORE_SOURCES
sale.cpp sale.cpp
marketplace.cpp marketplace.cpp
excelreader.cpp excelreader.cpp
csvreader.cpp
jsonutil.cpp jsonutil.cpp
utils.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) 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) target_link_libraries(core PRIVATE bcrypt)
else() 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() endif()
#target_include_directories(core PRIVATE ${PROJECT_SOURCE_DIR}/3rdparty/csv-parser)
target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

66
src/core/csvreader.cpp Normal file
View file

@ -0,0 +1,66 @@
#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(';');
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<std::string>().empty() && row[3].get<std::string>().empty()) {
++rowCount;
continue;
}
auto seller = std::make_unique<Seller>();
seller->createUuid();
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->findSellerWithUuid("11111111-1111-1111-1111-111111111111");
if (!specialSeller) {
auto seller = std::make_unique<Seller>();
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
}

18
src/core/csvreader.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef CSV_READER_H
#define CSV_READER_H
#include "marketplace.h"
#include "seller.h"
#include <filesystem>
#include <memory>
#include <string>
#include <vector>
class CsvReader
{
public:
static std::size_t readSellersFromFile(const std::filesystem::path& filePath, Marketplace* market);
};
#endif

View file

@ -2,6 +2,7 @@
#include <iomanip> #include <iomanip>
#include <numeric> #include <numeric>
#include <algorithm>
std::string formatCentAsEuroString(const int cent, int width) std::string formatCentAsEuroString(const int cent, int width)
{ {
@ -61,3 +62,12 @@ std::string& trim(std::string& str, const std::string& chars)
{ {
return ltrim(rtrim(str, chars), 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
}

View file

@ -13,5 +13,6 @@ std::optional<PrinterDevice> convertToPosPrinterDevice(const std::string& vendor
std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r "); 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& 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 "); 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 #endif

View file

@ -12,6 +12,7 @@
#include <utils.h> #include <utils.h>
#include <excelreader.h> #include <excelreader.h>
#include <csvreader.h>
#include <posprinter.h> #include <posprinter.h>
#include <exception> #include <exception>
@ -445,14 +446,20 @@ void MainWindow::onImportSellerExcelActionTriggered()
.exec(); .exec();
auto filename = QFileDialog::getOpenFileName(this, "Verkäufer importieren", QString(), auto filename = QFileDialog::getOpenFileName(this, "Verkäufer importieren", QString(),
"Excel Dateien (*.xlsx *.xls)"); "Excel Dateien (*.xlsx);CSV Dateien (*.csv)");
if (filename.isEmpty()) if (filename.isEmpty())
return; return;
fs::path filePath(filename.toStdWString()); 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(); updateStatLabel();
using namespace std::string_literals; using namespace std::string_literals;