131 lines
4.7 KiB
C++
131 lines
4.7 KiB
C++
#include "outputdialog.h"
|
|
#include "check/check.h"
|
|
#include "output/output_options.h"
|
|
#include "ui_outputdialog.h"
|
|
#include <QFileDialog>
|
|
#include <QLineEdit>
|
|
#include <QMainWindow>
|
|
#include <QStandardItemModel>
|
|
#include <fstream>
|
|
#include <outputcolumn.h>
|
|
#include <outputcolumnmodel.h>
|
|
#include "settings/settings.h"
|
|
#include "utils/utils.h"
|
|
#include <map>
|
|
|
|
OutputDialog::OutputDialog(QWidget *parent, std::vector<Check> *checks)
|
|
: QDialog(parent), ui(new Ui::OutputDialog), checks(checks),
|
|
options(OutputOptions()) {
|
|
settings = new Settings(get_path_relative_to_home(".local/share/checks_parser/settings.json"));
|
|
|
|
ui->setupUi(this);
|
|
|
|
columns = new std::vector<OutputColumn>;
|
|
|
|
OutputColumnModel *model = new OutputColumnModel(&(*columns), this);
|
|
|
|
ui->listView->setModel(model);
|
|
|
|
for (unsigned short i = 0; i < 6; i ++)
|
|
columns->push_back(OutputColumn(tr("Кто здесь?"), ColumnType::date));
|
|
|
|
for (auto &column : column_names) {
|
|
std::string name = settings->get_all_settings()["output_order"][column.first]["name"];
|
|
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));
|
|
}
|
|
|
|
ui->printHeaderCheckBox->setChecked(settings->get_all_settings()["print_header"]);
|
|
ui->printTotalCheckBox->setChecked(settings->get_all_settings()["print_total"]);
|
|
}
|
|
|
|
OutputDialog::~OutputDialog() {
|
|
delete settings;
|
|
delete ui;
|
|
}
|
|
|
|
void OutputDialog::on_buttonBox_accepted() {
|
|
std::ofstream output_file(this->options.get_path());
|
|
|
|
print_header(&output_file);
|
|
|
|
for (Check &check : *checks) {
|
|
int row_number = 0;
|
|
for (auto it = check.get_goods().begin(); it != check.get_goods().end(); it++, row_number++) {
|
|
for (int i = 0; i < columns->size(); i ++) {
|
|
OutputColumn &column = columns->at(i);
|
|
switch (column.get_column_type()) {
|
|
case ColumnType::date:
|
|
if (row_number == 0) output_file << check.get_date();
|
|
break;
|
|
case ColumnType::goods_name:
|
|
output_file << it->get_name();
|
|
break;
|
|
case ColumnType::goods_price_per_unit:
|
|
output_file << std::fixed << std::setprecision(2) << it->get_price_per_unit();
|
|
break;
|
|
case ColumnType::goods_quantity:
|
|
output_file << std::fixed << std::setprecision(2) << it->get_quantity();
|
|
break;
|
|
case ColumnType::goods_net_weight:
|
|
output_file << it->get_net_weight();
|
|
break;
|
|
case ColumnType::goods_total:
|
|
output_file << std::fixed << std::setprecision(2) << it->calculate_total_price();
|
|
break;
|
|
}
|
|
|
|
if (i < columns->size() - 1) {
|
|
output_file << ",";
|
|
} else {
|
|
output_file << "\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
if (this->options.get_print_total()) {
|
|
output_file << "Total: " << std::fixed << std::setprecision(2) << check.calculae_total_price() << std::endl;
|
|
}
|
|
}
|
|
output_file.close();
|
|
save_settings();
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
void OutputDialog::print_header(std::ofstream *output_file) {
|
|
if (options.get_print_header()) {
|
|
for (unsigned int i = 0; i < columns->size(); i ++) {
|
|
OutputColumn column = columns->at(i);
|
|
(*output_file) << column.get_text().toStdString()
|
|
<< (i == columns->size() - 1
|
|
? ""
|
|
: ",");
|
|
}
|
|
*output_file << std::endl;
|
|
}
|
|
}
|
|
|
|
void OutputDialog::save_settings() {
|
|
for (int i = 0; i < columns->size(); i ++) {
|
|
OutputColumn &column = columns->at(i);
|
|
std::string key = find_key_by_value(column_names, column.get_column_type());
|
|
settings->get_all_settings()["output_order"][key]["name"] = column.get_text().toStdString();
|
|
settings->get_all_settings()["output_order"][key]["position"] = i + 1;
|
|
}
|
|
settings->flush();
|
|
}
|