added store autodetect in email scene

This commit is contained in:
leca 2025-03-30 20:48:53 +03:00
parent 5600e03ce1
commit 6b815dbe0a
10 changed files with 65 additions and 29 deletions

View File

@ -1,5 +1,6 @@
#include "emailtextscene.h"
#include "ui_emailtextscene.h"
#include <QMessageBox>
#include <iostream>
#include <outputdialog.h>
@ -10,7 +11,7 @@ EmailTextScene::EmailTextScene(QWidget *parent)
, ui(new Ui::EmailTextScene) {
ui->setupUi(this);
auto modules = parser.get_modules_names();
modules = parser.get_modules_names();
for (auto &module : modules) {
ui->store_combo_box->addItem(QString::fromStdString(module));
@ -43,3 +44,14 @@ void EmailTextScene::on_parse_button_clicked() {
d.exec();
}
void EmailTextScene::on_check_content_textChanged() {
std::string store = parser.try_autodetect_store(ui->check_content->toPlainText().toStdString());
if (store == "") {
std::cerr << tr("Could not autodetect store. If you beleive that this is an error, please, report to the developer.").toStdString() << std::endl;
return;
}
unsigned int index = ui->store_combo_box->findText(QString::fromStdString(store));
ui->store_combo_box->setCurrentIndex(index);
}

View File

@ -19,9 +19,12 @@ public:
private slots:
void on_parse_button_clicked();
void on_check_content_textChanged();
private:
Ui::EmailTextScene *ui;
Parser parser;
std::vector<std::string> modules;
};
#endif // EMAILTEXTSCENE_H

View File

@ -1,5 +1,6 @@
{
"name":"Магнит",
"autodetect_regex":"",
"goods_name_regex": "([\\(\\)\\%\\*a-zA-Z0-9\u0401\u0451\u0410-\u044f \\.\\-\/]{17,100})",
"goods_price_regex": "[0-9]{0,4}[^%]\\.[0-9]{2} ",
"goods_quantity_regex": "([0-9]{0,4}[^%]\\.[0-9]{3} )|(\t\\d )",

View File

@ -1,8 +1,8 @@
{
"name":"Пятёрочка",
"goods_name_regex": "^\\d+\\s\\t\\*?[\\(\\)\\%\\*a-zA-Z0-9ёЁА-я\\.\\-\\/ ]+",
"autodetect_regex":"ООО\s*\"Агроторг\"",
"goods_name_regex": "([\\(\\)\\%\\*a-zA-Z0-9\u0401\u0451\u0410-\u044f \\.\\-\/]{17,100})",
"goods_price_regex": "[0-9]{0,4}[^%]\\.[0-9]{2} ",
"goods_net_weight_regex": "((\\d+(\\.|,)?\\d{0,}((м|)л|(к|м|)г|т|ц|шт|(pc|)s|(m|k|)g|(m|)l|t))(\\s|\\t){0,})+",
"goods_quantity_regex": "([0-9]{0,4}[^%]\\.[0-9]{3} )|(\t\\d )",
"check_start_regex": "КАССОВЫЙ ЧЕК\nприход",
"check_end_regex": "Итог\\:.{0,3}[0-9]{0,6}\\.[0-9]{2}"

View File

@ -16,7 +16,7 @@ std::vector<std::string> StoreModule::parse(std::wstring str, std::wstring regex
std::cout << "Handling: " << to_utf8(str) << std::endl;
for (boost::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
it++) {
std::wcout << "Parsed: " << it->str() << std::endl;
std::cout << "Parsed: " << to_utf8(it->str()) << std::endl;
result.push_back(to_utf8(it->str()));
}
@ -28,6 +28,7 @@ StoreModule::StoreModule(std::string path) {
nlohmann::json settings = nlohmann::json::parse(settings_file);
this->name = from_utf8(settings["name"]);
this->autodetect_regex = from_utf8(settings["autodetect_regex"]);
this->goods_name_regex = from_utf8(settings["goods_name_regex"]);
this->goods_price_regex = from_utf8(settings["goods_price_regex"]);
this->goods_net_weight_regex = from_utf8(settings["goods_net_weight_regex"]);
@ -89,3 +90,8 @@ std::wstring StoreModule::trim_check(std::wstring& check) {
}
std::wstring StoreModule::get_name() { return this->name; }
bool StoreModule::search_autodetect_regex(std::wstring str) {
std::vector<std::string> parsed = parse(str, this->autodetect_regex, boost::regex_constants::collate);
return parsed.size() > 0;
}

View File

@ -8,6 +8,7 @@
class StoreModule {
std::string path;
std::wstring name;
std::wstring autodetect_regex;
std::wstring goods_name_regex;
std::wstring goods_price_regex;
std::wstring goods_net_weight_regex;
@ -15,9 +16,9 @@ class StoreModule {
std::wstring check_start_regex;
std::wstring check_end_regex;
std::vector<std::string> parse(std::wstring, std::wstring, boost::regex_constants::flag_type_);
std::vector<std::string> parse(std::wstring str, std::wstring regexp, boost::regex_constants::flag_type_ flag);
public:
StoreModule(std::string);
StoreModule(std::string path);
StoreModule();
std::vector<std::string> parse_name(std::wstring);
@ -27,8 +28,9 @@ public:
std::wstring trim_check(std::wstring&);
std::wstring get_name();
bool search_autodetect_regex(std::wstring str);
};
#endif // STORE_MODULE_H

View File

@ -12,6 +12,7 @@
using namespace std::experimental;
using namespace std::experimental::filesystem;
#else
#include <QObject>
# include <filesystem>
using namespace std::filesystem;
#endif
@ -25,9 +26,7 @@ std::vector<std::string> Parser::search_modules() {
if (!exists(modules_dir)) {
create_directories(path);
std::cout << "No modules directory found. Created one at " << path
<< std::endl;
std::cout << "Please, download modules to that directory from my git."
std::cout << QObject::tr("No modules directory found. Created one at ").toStdString() << path
<< std::endl;
}
@ -86,33 +85,33 @@ std::vector<Goods> Parser::parse(std::wstring check_plaintext) {
return result;
}
std::string Parser::try_autodetect_store(std::string str) {
std::vector<std::string> stored_modules_paths = search_modules();
for (auto &module_path : stored_modules_paths) {
StoreModule module(module_path);
if (module.search_autodetect_regex(from_utf8(str))) return to_utf8(module.get_name());
}
return "";
}
std::vector<std::string> Parser::check_updates() {
std::cout << "Checking updates for stores modules" << std::endl;
std::cout << QObject::tr("Checking updates for stores modules").toStdString() << std::endl;
Settings s(get_path_relative_to_home(".local/share/checks_parser/settings.json"));
std::string path = get_path_relative_to_home(s.get_setting("stores_modules_dir"));
std::vector<std::string> to_download;
std::vector<std::string> stored_modules;
directory_entry modules_dir(path);
if (!exists(modules_dir)) {
create_directories(path);
}
for (const auto& file : directory_iterator(path)) {
if (!is_regular_file(file)) continue;
stored_modules.push_back(file.path().filename());
std::cout << file.path().filename() << " detected local store module" << std::endl;
}
std::vector<std::string> stored_modules = search_modules();
Net n;
std::cerr << "Downloading modules list from: " << s.get_setting("stores_modules_url") << std::endl;
std::cerr << QObject::tr("Downloading modules list from: ").toStdString() << s.get_setting("stores_modules_url") << std::endl;
std::vector<std::string> remote_modules = n.get_all_modules(s.get_setting("stores_modules_url"));
if (stored_modules.empty()) {
std::cout << "I need to download everything" << std::endl;
to_download = remote_modules;
} else {
for (const std::string& module : remote_modules) {
if (!vector_contains_element(stored_modules, module)) {
std::cout << "I need to download store module " << module << std::endl;
std::cout << QObject::tr("Queueing download of ").toStdString() << module << std::endl;
to_download.push_back(module);
}
}

View File

@ -22,6 +22,9 @@ public:
void set_module(std::string);
std::vector<Goods> parse(std::wstring);
std::string try_autodetect_store(std::string);
};
#endif // PARSER_H

View File

@ -56,15 +56,20 @@
<translation>Back</translation>
</message>
<message>
<location filename="../emailtextscene.cpp" line="31"/>
<location filename="../emailtextscene.cpp" line="32"/>
<source>An error has occured. Check was matched incorrectly. Vector sizes are different. Please, contact the developer.</source>
<translation>An error has occured. Check was matched incorrectly. Vector sizes are different. Please, contact the developer.</translation>
</message>
<message>
<location filename="../emailtextscene.cpp" line="33"/>
<location filename="../emailtextscene.cpp" line="34"/>
<source>Error in parsing</source>
<translation>Error in parsing</translation>
</message>
<message>
<location filename="../emailtextscene.cpp" line="51"/>
<source>Could not autodetect store. If you beleive that this is an error, please, report to the developer.</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>MainWindow</name>

View File

@ -56,15 +56,20 @@
<translation>Назад</translation>
</message>
<message>
<location filename="../emailtextscene.cpp" line="31"/>
<location filename="../emailtextscene.cpp" line="32"/>
<source>An error has occured. Check was matched incorrectly. Vector sizes are different. Please, contact the developer.</source>
<translation>Произошла ошибка. Чек был прочитан неверно. Размеры векторов различаются. Пожалуйста, сообщите об этом разработчику.</translation>
</message>
<message>
<location filename="../emailtextscene.cpp" line="33"/>
<location filename="../emailtextscene.cpp" line="34"/>
<source>Error in parsing</source>
<translation>Ошибка в парсинге</translation>
</message>
<message>
<location filename="../emailtextscene.cpp" line="51"/>
<source>Could not autodetect store. If you beleive that this is an error, please, report to the developer.</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>MainWindow</name>