added importing from csv
This commit is contained in:
parent
780e37f565
commit
8265212c11
8 changed files with 112 additions and 6 deletions
1
3rdparty/CMakeLists.txt
vendored
1
3rdparty/CMakeLists.txt
vendored
|
@ -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)
|
||||
|
|
2
3rdparty/csv-parser
vendored
2
3rdparty/csv-parser
vendored
|
@ -1 +1 @@
|
|||
Subproject commit dab0416d838b560ac7cd7090a32081d0a1e1c919
|
||||
Subproject commit e4a899dcafcfa14e448348e9b3c8c06d7697dbf8
|
|
@ -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})
|
||||
|
|
66
src/core/csvreader.cpp
Normal file
66
src/core/csvreader.cpp
Normal 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
18
src/core/csvreader.h
Normal 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
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <iomanip>
|
||||
#include <numeric>
|
||||
#include <algorithm>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
}
|
|
@ -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& 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
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include <utils.h>
|
||||
|
||||
#include <excelreader.h>
|
||||
#include <csvreader.h>
|
||||
#include <posprinter.h>
|
||||
|
||||
#include <exception>
|
||||
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue