2024-08-24 00:45:47 +03:00
|
|
|
#include "outputdialog.h"
|
2024-08-30 05:03:32 +03:00
|
|
|
#include "check/check.h"
|
|
|
|
#include "output/output_options.h"
|
2024-08-24 00:45:47 +03:00
|
|
|
#include "ui_outputdialog.h"
|
2024-08-30 05:03:32 +03:00
|
|
|
#include <QFileDialog>
|
2024-08-24 00:45:47 +03:00
|
|
|
#include <QMainWindow>
|
2024-08-30 05:03:32 +03:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2024-10-05 13:52:44 +03:00
|
|
|
#include "settings/settings.h"
|
|
|
|
#include "utils/utils.h"
|
2024-08-24 00:45:47 +03:00
|
|
|
|
2024-08-30 05:03:32 +03:00
|
|
|
OutputDialog::OutputDialog(QWidget *parent, Check &check)
|
|
|
|
: QDialog(parent), ui(new Ui::OutputDialog), check(check),
|
2024-10-05 13:52:44 +03:00
|
|
|
options(OutputOptions()) {
|
|
|
|
|
|
|
|
Settings settings(get_path_relative_to_home(".local/share/checks_parser/settings.json"));
|
|
|
|
|
2024-08-24 00:45:47 +03:00
|
|
|
ui->setupUi(this);
|
2024-10-05 13:52:44 +03:00
|
|
|
|
2024-11-22 23:19:04 +03:00
|
|
|
ui->tableWidget->item(0, 1)->setText(QString::fromStdString(settings.get_all_settings()["output_order"]["goods_name"]["name"]));
|
|
|
|
ui->tableWidget->item(0, 0)->setText(QString::number((int)settings.get_all_settings()["output_order"]["goods_name"]["position"]));
|
2024-10-05 13:52:44 +03:00
|
|
|
|
2024-11-22 23:19:04 +03:00
|
|
|
ui->tableWidget->item(1, 1)->setText(QString::fromStdString(settings.get_all_settings()["output_order"]["goods_price_per_unit"]["name"]));
|
|
|
|
ui->tableWidget->item(1, 0)->setText(QString::number((int)settings.get_all_settings()["output_order"]["goods_price_per_unit"]["position"]));
|
2024-10-05 13:52:44 +03:00
|
|
|
|
2024-11-22 23:19:04 +03:00
|
|
|
ui->tableWidget->item(2, 1)->setText(QString::fromStdString(settings.get_all_settings()["output_order"]["goods_quantity"]["name"]));
|
|
|
|
ui->tableWidget->item(2, 0)->setText(QString::number((int)settings.get_all_settings()["output_order"]["goods_quantity"]["position"]));
|
2024-10-05 13:52:44 +03:00
|
|
|
|
2024-11-22 23:19:04 +03:00
|
|
|
ui->tableWidget->item(3, 1)->setText(QString::fromStdString(settings.get_all_settings()["output_order"]["goods_net_weight"]["name"]));
|
|
|
|
ui->tableWidget->item(3, 0)->setText(QString::number((int)settings.get_all_settings()["output_order"]["goods_net_weight"]["position"]));
|
2024-10-05 13:52:44 +03:00
|
|
|
|
2024-11-22 23:19:04 +03:00
|
|
|
ui->tableWidget->item(4, 1)->setText(QString::fromStdString(settings.get_all_settings()["output_order"]["goods_total"]["name"]));
|
|
|
|
ui->tableWidget->item(4, 0)->setText(QString::number((int)settings.get_all_settings()["output_order"]["goods_total"]["position"]));
|
2024-10-05 13:52:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
ui->printHeaderCheckBox->setChecked(settings.get_all_settings()["print_header"]);
|
|
|
|
ui->printTotalCheckBox->setChecked(settings.get_all_settings()["print_total"]);
|
2024-08-24 00:45:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
OutputDialog::~OutputDialog() { delete ui; }
|
2024-08-30 05:03:32 +03:00
|
|
|
|
|
|
|
bool compare_position(Column &c1, Column &c2) {
|
|
|
|
return c1.position < c2.position;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputDialog::on_buttonBox_accepted() {
|
|
|
|
std::ofstream output_file(this->options.get_path());
|
|
|
|
|
2024-08-30 22:12:14 +03:00
|
|
|
for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
|
2024-08-30 05:03:32 +03:00
|
|
|
int position = ui->tableWidget->item(i, 0)->text().toInt();
|
|
|
|
std::string name = ui->tableWidget->item(i, 1)->text().toStdString();
|
|
|
|
|
2024-11-22 23:19:04 +03:00
|
|
|
std::cout << position << " " << name << std::endl;
|
|
|
|
|
2024-08-30 05:03:32 +03:00
|
|
|
Column c;
|
|
|
|
c.type = static_cast<ColumnType>(i);
|
|
|
|
c.position = position;
|
|
|
|
c.name = name;
|
|
|
|
|
|
|
|
this->options.add_or_update_column(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(this->options.get_columns().begin(),
|
|
|
|
this->options.get_columns().end(), compare_position);
|
|
|
|
|
|
|
|
if (options.get_print_header()) {
|
2024-08-30 22:12:14 +03:00
|
|
|
for (auto &column : this->options.get_columns()) {
|
2024-08-30 05:03:32 +03:00
|
|
|
output_file << column.name
|
|
|
|
<< (column.position == this->options.get_columns().size()
|
|
|
|
? ""
|
|
|
|
: ",");
|
|
|
|
}
|
|
|
|
output_file << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto goods : this->check.get_goods()) {
|
2024-08-30 22:12:14 +03:00
|
|
|
for (auto &column : this->options.get_columns()) {
|
2024-08-30 05:03:32 +03:00
|
|
|
std::string output_str;
|
|
|
|
|
|
|
|
switch (column.type) {
|
|
|
|
case ColumnType::goods_name:
|
|
|
|
output_str = goods.get_name();
|
|
|
|
break;
|
|
|
|
case ColumnType::goods_price_per_unit:
|
|
|
|
output_str = std::to_string(goods.get_price_per_unit());
|
|
|
|
break;
|
|
|
|
case ColumnType::goods_quantity:
|
|
|
|
output_str = std::to_string(goods.get_quantity());
|
|
|
|
break;
|
|
|
|
case ColumnType::goods_net_weight:
|
|
|
|
output_str = "TODO";
|
|
|
|
// TODO
|
|
|
|
break;
|
|
|
|
case ColumnType::goods_total:
|
|
|
|
output_str = std::to_string(goods.calculate_total_price());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (column.position != this->options.get_columns().size()) {
|
|
|
|
output_str += ",";
|
|
|
|
} else {
|
|
|
|
output_str += "\n";
|
|
|
|
}
|
|
|
|
output_file << output_str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->options.get_print_total()) {
|
|
|
|
output_file << "Total: " << std::to_string(check.calculae_total_price());
|
|
|
|
}
|
|
|
|
|
|
|
|
output_file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_settings(OutputOptions &options, ColumnType t, std::string name,
|
|
|
|
int value) {
|
|
|
|
Column column;
|
|
|
|
column.type = t;
|
|
|
|
column.name = name;
|
|
|
|
column.position = value;
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
options.add_or_update_column(column);
|
|
|
|
} else {
|
|
|
|
options.remove_column(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputDialog::on_chooseFileButton_clicked() {
|
|
|
|
QString filename = QFileDialog::getSaveFileName();
|
|
|
|
this->options.set_path(filename.toStdString());
|
|
|
|
ui->pathLabel->setText("Path to export: " + filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputDialog::on_printHeaderCheckBox_stateChanged(int value) {
|
|
|
|
this->options.set_print_header(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputDialog::on_printTotalCheckBox_stateChanged(int value) {
|
|
|
|
this->options.set_print_total(value);
|
|
|
|
}
|