fixed only handling two fields of a check

This commit is contained in:
leca 2025-05-24 16:04:44 +03:00
parent 20b08f493d
commit 1507faef6e
10 changed files with 163 additions and 85 deletions

View File

@ -1,9 +1,19 @@
#include "check.h"
#include "../goods/goods.h"
#include <iostream>
#include <QObject>
Check::Check() {}
Check::Check(std::string date, double total, OperationType type, std::string fn, std::string fd, std::string fi, std::vector<Goods> goods) {
set_date(date);
set_total(total);
set_operation_type(type);
set_fn(fn);
set_fd(fd);
set_fi(fi);
set_goods(goods);
}
void Check::add_goods(Goods goods) {
this->goods.push_back(goods);
this->total = this->calculae_total_price();
@ -25,50 +35,24 @@ double Check::calculae_total_price() {
return total;
}
std::vector<Goods>& Check::get_goods() {
return goods;
}
std::vector<Goods>& Check::get_goods() { return goods; }
void Check::set_fn(std::string fn) {
this->fn = fn;
}
void Check::set_fn(std::string fn) { this->fn = fn; }
void Check::set_fd(std::string fd) { this->fd = fd; }
void Check::set_fi(std::string fi) { this->fi = fi; }
std::string Check::get_date() { return date; }
void Check::set_fd(std::string fd) {
this->fd = fd;
}
void Check::set_fi(std::string fi) {
this->fi = fi;
}
std::string Check::get_date() {
return date;
}
void Check::set_date(std::string date) {
this->date = date;
}
void Check::set_operation_type(OperationType t) {
this->operation_type = t;
}
void Check::set_total(double total){
this->total = total;
}
double Check::get_total() {
return total;
}
OperationType Check::get_operationType() { return operation_type; }
void Check::set_date(std::string date) { this->date = date; }
void Check::set_operation_type(OperationType t) { this->operation_type = t; }
void Check::set_total(double total) { this->total = total; }
void Check::set_goods(std::vector<Goods> goods) { this->goods = goods; }
std::string Check::get_fn() { return fn; }
std::string Check::get_fd() { return fd; }
std::string Check::get_fi() { return fi; }
double Check::get_total() { return total; }
bool Check::operator==(Check &c) {
// std::cout << "Comparing" << std::endl;
// std::cout << this->date << " <>" << c.date << std::endl;
// std::cout << this->fd << " <>" << c.fd << std::endl;
// std::cout << this->fi<< " <>" << c.fi << std::endl;
// std::cout << this->fn<< " <>" << c.fn << std::endl;
// std::cout << this->operation_type << " <>" << c.operation_type << std::endl;
// std::cout << this->total<< " <>" << c.total<< std::endl;
return
this->date == c.date &&
this->fd == c.fd &&
@ -79,13 +63,6 @@ bool Check::operator==(Check &c) {
}
bool Check::operator==(const Check &c) {
// std::cout << "Comparing" << std::endl;
// std::cout << this->date << " <>" << c.date << std::endl;
// std::cout << this->fd << " <>" << c.fd << std::endl;
// std::cout << this->fi<< " <>" << c.fi << std::endl;
// std::cout << this->fn<< " <>" << c.fn << std::endl;
// std::cout << this->operation_type << " <>" << c.operation_type << std::endl;
// std::cout << this->total<< " <>" << c.total<< std::endl;
return
this->date == c.date &&
this->fd == c.fd &&
@ -94,3 +71,5 @@ bool Check::operator==(const Check &c) {
this->operation_type == c.operation_type &&
this->total == c.total;
}
Q_DECLARE_METATYPE(Check)

View File

@ -1,6 +1,7 @@
#ifndef CHECK_H
#define CHECK_H
#include "../goods/goods.h"
#include <vector>
typedef enum OperationTypes {
@ -23,6 +24,7 @@ class Check {
public:
Check();
Check(std::string date, double total, OperationType type, std::string fn, std::string fd, std::string fi, std::vector<Goods> goods);
void add_goods(Goods);
void add_goods(std::vector<Goods> &goods);
@ -37,6 +39,8 @@ public:
void set_operation_type(OperationType);
void set_total(double);
void set_goods(std::vector<Goods>);
std::string get_fn();
std::string get_fd();
std::string get_fi();
@ -48,4 +52,5 @@ public:
bool operator==(const Check &);
};
#endif // CHECK_H

View File

@ -1,5 +1,9 @@
#include "goods.h"
#include <string>
#include <QString>
#include <QObject>
Goods::Goods() { }
Goods::Goods(std::string name, double price_per_unit, std::string net_weight, double quantity) :
name(name), price_per_unit(price_per_unit),
@ -19,10 +23,53 @@ double Goods::get_price_per_unit() { return this->price_per_unit; }
void Goods::set_name(std::string name) { this->name = name; }
void Goods::set_name(QString name) { this->name = name.toStdString(); }
void Goods::set_quantity(double quantity) { this->quantity = quantity; }
void Goods::set_net_weight(std::string net_weight) { this->net_weight = net_weight; }
void Goods::set_net_weight(QString net_weight) { this->net_weight = net_weight.toStdString(); }
void Goods::set_price_per_unit(double price_per_unit) {
this->price_per_unit = price_per_unit;
}
Q_DECLARE_METATYPE(Goods)
QDataStream &operator<<(QDataStream &in, Goods &goods) {
in << QString::fromStdString(goods.get_name()) << goods.get_quantity() << QString::fromStdString(goods.get_net_weight()) << goods.get_price_per_unit();
return in;
}
QDataStream &operator>>(QDataStream &out, Goods &goods) {
QString name, net_weight;
double quantity, price_per_unit;
out >> name >> quantity >> net_weight >> price_per_unit;
goods.set_name(name);
goods.set_quantity(quantity);
goods.set_net_weight(net_weight);
goods.set_price_per_unit(price_per_unit);
return out;
}
QDataStream &operator<<(QDataStream &stream, std::vector<Goods> &goods) {
stream << (unsigned int )goods.size();
for (Goods &g : goods) {
stream << g;
}
return stream;
}
QDataStream &operator>>(QDataStream &stream, std::vector<Goods> &goods) {
unsigned int size;
stream >> size;
for (unsigned int i = 0 ; i < size; i ++) {
Goods g = Goods();
stream >> g;
goods.push_back(g);
}
return stream;
}

View File

@ -2,6 +2,7 @@
#define GOODS_H
#include <string>
#include <QDataStream>
class Goods
{
@ -10,6 +11,7 @@ class Goods
std::string net_weight; // will contain values like "5мл" or "10г"
double price_per_unit;
public:
Goods();
Goods(std::string name, double quantity, std::string net_weight, double price_per_unit);
double calculate_total_price();
@ -19,9 +21,16 @@ public:
double get_price_per_unit();
void set_name(std::string);
void set_name(QString);
void set_quantity(double);
void set_net_weight(std::string);
void set_net_weight(QString);
void set_price_per_unit(double);
};
QDataStream &operator<<(QDataStream &, Goods &);
QDataStream &operator>>(QDataStream &, Goods &);
QDataStream &operator<<(QDataStream &, std::vector<Goods> &);
QDataStream &operator>>(QDataStream &, std::vector<Goods> &);
#endif // GOODS_H

View File

@ -32,7 +32,7 @@
int main(int argc, char *argv[]) {
curl_global_init(CURL_GLOBAL_ALL);
qRegisterMetaType<Check>("Check");
std::string program_data_path = get_path_relative_to_home(".local/share/checks_parser");
create_directories(program_data_path);

View File

@ -218,8 +218,10 @@ void MainWindow::on_add_new_check_button_clicked() {
unsigned int newRowIndex = checks.size();
model->insertRows(newRowIndex, 1);
model->setData(model->index(newRowIndex, 0), QVariant::fromValue(new_check->get_date()));
model->setData(model->index(newRowIndex, 1), QVariant::fromValue(new_check->get_total()));
checks.at(newRowIndex) = *new_check;
emit model->dataChanged(model->index(newRowIndex, 0), model->index(newRowIndex, 1));
delete new_check;
@ -260,6 +262,7 @@ Check *MainWindow::parse_new_check() {
check->set_fd(ui->fd_line_edit->text().toStdString());
check->set_fi(ui->fi_line_edit->text().toStdString());
check->set_operation_type(OperationType(ui->operation_type_combo_box->currentIndex() + 1));
return check;
} catch(OfdRequestException e) {
if (!strcmp(e.what(), "Incorrect captcha")) {

View File

@ -20,6 +20,13 @@ OutputDialog::OutputDialog(QWidget *parent, std::vector<Check> *checks)
ui->setupUi(this);
for (Check &c : *checks) {
std::cout << "Check: " << c.get_date() << " " << c.get_total() << std::endl;
for (Goods &g : c.get_goods()) {
std::cout << g.get_name() << " " << g.get_net_weight() << " " << g.get_price_per_unit() << " " << g.get_quantity() << std::endl;
}
}
columns = new std::vector<OutputColumn>;
OutputColumnModel *model = new OutputColumnModel(columns, this);
@ -34,8 +41,12 @@ OutputDialog::OutputDialog(QWidget *parent, std::vector<Check> *checks)
unsigned short position = settings->get_all_settings()["output_order"][column.first]["position"];
ColumnType type = column.second;
columns->at(position - 1) = (OutputColumn(QString::fromStdString(name), type));
}
for (unsigned short i = 0; i < 6; i ++)
emit model->dataChanged(model->index(i, 0), model->index(i, 0));
ui->printHeaderCheckBox->setChecked(settings->get_all_settings()["print_header"]);
ui->printTotalCheckBox->setChecked(settings->get_all_settings()["print_total"]);
}

View File

@ -298,32 +298,32 @@
<translation>No checks to parse</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="267"/>
<location filename="../mainwindow.cpp" line="270"/>
<source>Captcha was not solved correctly!</source>
<translation>Captcha was not solved correctly!</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="269"/>
<location filename="../mainwindow.cpp" line="272"/>
<source>Captcha is incorrect</source>
<translation>Captcha is incorrect</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="274"/>
<location filename="../mainwindow.cpp" line="277"/>
<source>Internal server error. Please, try again later.</source>
<translation>Internal server error. Please, try again later.</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="276"/>
<location filename="../mainwindow.cpp" line="279"/>
<source>Internal server error</source>
<translation>Internal server error</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="281"/>
<location filename="../mainwindow.cpp" line="284"/>
<source>Check not found. Please, ensure correctness of entered data.</source>
<translation>Check not found. Please, ensure correctness of entered data.</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="283"/>
<location filename="../mainwindow.cpp" line="286"/>
<source>Check was not found</source>
<translation>Check was not found</translation>
</message>
@ -645,7 +645,7 @@
<translation>Print total</translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="30"/>
<location filename="../outputdialog.cpp" line="37"/>
<source>Кто здесь?</source>
<translation type="unfinished"></translation>
</message>

View File

@ -286,32 +286,32 @@
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="267"/>
<location filename="../mainwindow.cpp" line="270"/>
<source>Captcha was not solved correctly!</source>
<translation>Капча была решена неверно!</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="269"/>
<location filename="../mainwindow.cpp" line="272"/>
<source>Captcha is incorrect</source>
<translation>Капча введена неверно</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="274"/>
<location filename="../mainwindow.cpp" line="277"/>
<source>Internal server error. Please, try again later.</source>
<translation>Внутренняя ошибка сервера. Пожалуйста, попробуйте снова позже.</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="276"/>
<location filename="../mainwindow.cpp" line="279"/>
<source>Internal server error</source>
<translation>Внутренняя ошибка сервера</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="281"/>
<location filename="../mainwindow.cpp" line="284"/>
<source>Check not found. Please, ensure correctness of entered data.</source>
<translation>Чек не найден. Пожалуйста, убедитесь в правильности введённых данных.</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="283"/>
<location filename="../mainwindow.cpp" line="286"/>
<source>Check was not found</source>
<translation>Чек не найден</translation>
</message>
@ -629,7 +629,7 @@
<translation>Печатать Итого</translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="30"/>
<location filename="../outputdialog.cpp" line="37"/>
<source>Кто здесь?</source>
<translation type="unfinished"></translation>
</message>

View File

@ -3,7 +3,7 @@
#include <QMimeData>
#include <QIODevice>
#include <QDateTime>
#include <QVector>
CheckQueueTableModel::CheckQueueTableModel(std::vector<Check> *checks, QObject *parent)
: checks(checks), QAbstractTableModel{parent}
{}
@ -33,13 +33,15 @@ bool CheckQueueTableModel::setData(const QModelIndex &index, const QVariant &val
if (!index.isValid() || index.row() >= checks->size())
return false;
unsigned int row = index.row();
Check &c = checks->at(row);
switch (index.column()) {
case 0: {
checks->at(row).set_date(value.value<std::string>());
case 0:
c.set_date(value.value<std::string>());
break;
} case 1:
checks->at(row).set_total(value.value<double>());
case 1:
c.set_total(value.value<double>());
break;
emit dataChanged(index, index, {role});
}
return true;
}
@ -51,10 +53,8 @@ QVariant CheckQueueTableModel::headerData(int section, Qt::Orientation orientati
switch (section) {
case 0:
return tr("Date&Time");
break;
case 1:
return tr("Total");
break;
}
} else if (role == Qt::DisplayRole && orientation == Qt::Vertical) {
return section + 1;
@ -117,9 +117,16 @@ QMimeData* CheckQueueTableModel::mimeData(const QModelIndexList &indexes) const
for (const QModelIndex &i : indexes) {
if (i.isValid() && i.column() == 0) {
QString date = data(i, Qt::DisplayRole).toString();
double total = data(index(i.row(), i.column() + 1), Qt::DisplayRole).toDouble();
stream << date << total;
Check &c = checks->at(i.row());
QString date = QString::fromStdString(c.get_date()),
fn = QString::fromStdString(c.get_fn()),
fd = QString::fromStdString(c.get_fd()),
fi = QString::fromStdString(c.get_fi());
double total = c.get_total();
OperationType type = c.get_operationType();
std::vector<Goods> goods = c.get_goods();
stream << date << total << type << fn << fd << fi << goods;
}
}
mimeData->setData(CheckQueueTableModel::MimeType, encodedData);
@ -138,23 +145,40 @@ bool CheckQueueTableModel::dropMimeData(const QMimeData *data, Qt::DropAction ac
int rows = 0;
while (!stream.atEnd()) {
QString date;
QString date, fn, fd, fi;
double total;
stream >> date >> total;
Check c;
c.set_date(date.toStdString());
c.set_total(total);
OperationType type;
std::vector<Goods> goods;
stream >> date >> total >> type >> fn >> fd >> fi >> goods;
Check c = Check(
date.toStdString(),
total,
type,
fn.toStdString(),
fd.toStdString(),
fi.toStdString(),
goods
);
newItems.push_back(c);
++rows;
}
insertRows(row, rows, QModelIndex());
for (Check &c : newItems) {
QModelIndex date_index = index(row, 0, QModelIndex());
QModelIndex total_index = index(row, 1, QModelIndex());
setData(date_index, QVariant::fromValue(c.get_date()), Qt::EditRole);
setData(total_index, QVariant::fromValue(c.get_total()), Qt::EditRole);
for (Check item : newItems) {
// (*checks)[row] = std::move(item);
Check &c = checks->at(row);
c.set_date(item.get_date());
c.set_total(item.get_total());
c.set_fn(item.get_fn());
c.set_fd(item.get_fd());
c.set_fi(item.get_fi());
c.set_operation_type(item.get_operationType());
for (Goods &g : item.get_goods()) {
c.add_goods(g);
}
emit dataChanged(index(row, 0), index(row, 1));
row++;
}