2020-08-16 09:40:14 +02:00
|
|
|
|
/*
|
2023-01-01 17:05:57 +01:00
|
|
|
|
Copyright 2006-2023 The QElectroTech Team
|
2020-08-17 16:42:07 +02:00
|
|
|
|
This file is part of QElectroTech.
|
2019-11-03 13:27:46 +01:00
|
|
|
|
|
2020-08-17 16:42:07 +02:00
|
|
|
|
QElectroTech is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
(at your option) any later version.
|
2019-11-03 13:27:46 +01:00
|
|
|
|
|
2020-08-17 16:42:07 +02:00
|
|
|
|
QElectroTech is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
2019-11-03 13:27:46 +01:00
|
|
|
|
|
2020-08-17 16:42:07 +02:00
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
2019-11-03 13:27:46 +01:00
|
|
|
|
*/
|
|
|
|
|
#include "bomexportdialog.h"
|
2020-12-08 19:57:35 +01:00
|
|
|
|
|
|
|
|
|
#include "../dataBase/ui/elementquerywidget.h"
|
|
|
|
|
#include "../qetapp.h"
|
|
|
|
|
#include "../qetinformation.h"
|
|
|
|
|
#include "../qetproject.h"
|
2019-11-03 13:27:46 +01:00
|
|
|
|
#include "ui_bomexportdialog.h"
|
|
|
|
|
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QSqlError>
|
2019-11-09 11:38:30 +01:00
|
|
|
|
#include <QSqlRecord>
|
2019-11-03 13:27:46 +01:00
|
|
|
|
|
|
|
|
|
/**
|
2020-08-18 21:28:52 +02:00
|
|
|
|
@brief BOMExportDialog::BOMExportDialog
|
|
|
|
|
@param project
|
|
|
|
|
@param parent
|
|
|
|
|
*/
|
2019-11-03 13:27:46 +01:00
|
|
|
|
BOMExportDialog::BOMExportDialog(QETProject *project, QWidget *parent) :
|
2020-08-18 21:29:12 +02:00
|
|
|
|
QDialog(parent),
|
2020-08-16 14:23:59 +02:00
|
|
|
|
ui(new Ui::BOMExportDialog),
|
|
|
|
|
m_project(project)
|
2019-11-03 13:27:46 +01:00
|
|
|
|
{
|
2020-08-18 21:29:12 +02:00
|
|
|
|
ui->setupUi(this);
|
2019-11-09 11:38:30 +01:00
|
|
|
|
|
2020-08-17 16:42:07 +02:00
|
|
|
|
m_query_widget = new ElementQueryWidget(this);
|
|
|
|
|
ui->m_main_layout->insertWidget(0, m_query_widget);
|
|
|
|
|
//By default format as bom is clicked
|
|
|
|
|
on_m_format_as_bom_clicked(true);
|
2019-11-03 13:27:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-08-18 21:28:52 +02:00
|
|
|
|
@brief BOMExportDialog::~BOMExportDialog
|
|
|
|
|
*/
|
2019-11-03 13:27:46 +01:00
|
|
|
|
BOMExportDialog::~BOMExportDialog()
|
|
|
|
|
{
|
2020-07-15 20:20:07 +02:00
|
|
|
|
delete ui;
|
2019-11-03 13:27:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-08-18 21:28:52 +02:00
|
|
|
|
@brief BOMExportDialog::exec
|
|
|
|
|
@return
|
|
|
|
|
*/
|
2019-11-03 13:27:46 +01:00
|
|
|
|
int BOMExportDialog::exec()
|
|
|
|
|
{
|
2020-08-17 16:42:07 +02:00
|
|
|
|
auto r = QDialog::exec();
|
2020-07-15 20:20:07 +02:00
|
|
|
|
if (r == QDialog::Accepted)
|
|
|
|
|
{
|
2020-08-17 16:42:07 +02:00
|
|
|
|
//save in csv file
|
2020-07-15 20:20:07 +02:00
|
|
|
|
QString file_name = tr("nomenclature_") + QString(m_project ->title() + ".csv");
|
|
|
|
|
QString file_path = QFileDialog::getSaveFileName(this, tr("Enregister sous... "), file_name, tr("Fichiers csv (*.csv)"));
|
|
|
|
|
QFile file(file_path);
|
|
|
|
|
if (!file_path.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
if (QFile::exists(file_path ))
|
|
|
|
|
{
|
|
|
|
|
// if file already exist -> delete it
|
|
|
|
|
if (!QFile::remove(file_path) )
|
|
|
|
|
{
|
|
|
|
|
QMessageBox::critical(this, tr("Erreur"),
|
|
|
|
|
tr("Impossible de remplacer le fichier!\n\n")+
|
|
|
|
|
"Destination : "+file_path+"\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
|
|
|
|
|
{
|
|
|
|
|
QTextStream stream(&file);
|
2020-06-09 23:07:07 +02:00
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) // ### Qt 6: remove
|
2020-07-15 20:20:07 +02:00
|
|
|
|
stream << getBom() << endl;
|
2020-06-09 23:07:07 +02:00
|
|
|
|
#else
|
2020-09-24 17:01:33 +02:00
|
|
|
|
#if TODO_LIST
|
2020-07-19 22:06:42 +02:00
|
|
|
|
#pragma message("@TODO remove code for QT 5.15 or later")
|
2020-09-24 17:01:33 +02:00
|
|
|
|
#endif
|
2020-07-15 20:20:07 +02:00
|
|
|
|
stream << getBom() << &Qt::endl(stream);
|
2020-06-09 23:07:07 +02:00
|
|
|
|
#endif
|
2020-07-15 20:20:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-09 11:38:30 +01:00
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-03 13:27:46 +01:00
|
|
|
|
QString BOMExportDialog::getBom()
|
|
|
|
|
{
|
2020-08-17 16:42:07 +02:00
|
|
|
|
m_project->dataBase()->updateDB();
|
|
|
|
|
auto query_ = m_project->dataBase()->newQuery(m_query_widget->queryStr());
|
|
|
|
|
QString return_string;
|
2019-11-03 13:27:46 +01:00
|
|
|
|
|
2020-08-17 16:42:07 +02:00
|
|
|
|
if (!query_.exec()) {
|
|
|
|
|
qDebug() << "BOMExportDialog::getBom : query errir : " << query_.lastError();
|
2020-07-15 20:20:07 +02:00
|
|
|
|
}
|
2020-08-17 16:42:07 +02:00
|
|
|
|
else
|
2020-07-15 20:20:07 +02:00
|
|
|
|
{
|
2020-08-17 16:42:07 +02:00
|
|
|
|
//HEADERS
|
|
|
|
|
if (ui->m_include_headers)
|
2020-07-15 20:20:07 +02:00
|
|
|
|
{
|
2020-08-17 16:42:07 +02:00
|
|
|
|
auto record_ = query_.record();
|
|
|
|
|
QStringList header_name;
|
|
|
|
|
for (auto i=0 ; i<record_.count() ; ++i)
|
2020-07-15 20:20:07 +02:00
|
|
|
|
{
|
2020-08-17 16:42:07 +02:00
|
|
|
|
auto field_name = record_.fieldName(i);
|
|
|
|
|
|
|
|
|
|
qDebug() << "field name = " << field_name;
|
|
|
|
|
if (field_name == "position") {
|
|
|
|
|
header_name << tr("Position");
|
|
|
|
|
} else if (field_name == "diagram_position") {
|
|
|
|
|
header_name << tr("Position du folio");
|
|
|
|
|
} else if (field_name == "designation_qty") {
|
|
|
|
|
header_name << tr("Quantité numéro d'article", "Special field with name : designation quantity");
|
|
|
|
|
} else {
|
2020-11-08 19:16:02 +01:00
|
|
|
|
header_name << QETInformation::translatedInfoKey(field_name);
|
2020-08-17 16:42:07 +02:00
|
|
|
|
if (header_name.isEmpty()) {
|
|
|
|
|
header_name << field_name;
|
2020-07-15 20:20:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-03 13:27:46 +01:00
|
|
|
|
|
2020-08-17 16:42:07 +02:00
|
|
|
|
}
|
|
|
|
|
return_string = header_name.join(";") + "\n";
|
2019-11-09 11:38:30 +01:00
|
|
|
|
}
|
2019-11-03 13:27:46 +01:00
|
|
|
|
|
2020-08-17 16:42:07 +02:00
|
|
|
|
//ROWS
|
|
|
|
|
while (query_.next())
|
2020-07-15 20:20:07 +02:00
|
|
|
|
{
|
2020-08-17 16:42:07 +02:00
|
|
|
|
auto i=0;
|
|
|
|
|
QStringList values;
|
|
|
|
|
while (query_.value(i).isValid())
|
2020-07-15 20:20:07 +02:00
|
|
|
|
{
|
2020-08-17 16:42:07 +02:00
|
|
|
|
auto date = query_.value(i).toDate();
|
|
|
|
|
if (!date.isNull()) {
|
|
|
|
|
values << QLocale::system().toString(query_.value(i).toDate(), QLocale::ShortFormat);
|
|
|
|
|
} else {
|
|
|
|
|
values << query_.value(i).toString();
|
|
|
|
|
}
|
|
|
|
|
++i;
|
2020-07-15 20:20:07 +02:00
|
|
|
|
}
|
2019-11-03 13:27:46 +01:00
|
|
|
|
|
2020-08-17 16:42:07 +02:00
|
|
|
|
return_string += values.join(";") + "\n";
|
|
|
|
|
values.clear();
|
2019-11-09 14:28:13 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-03 13:27:46 +01:00
|
|
|
|
|
2020-08-17 16:42:07 +02:00
|
|
|
|
qDebug() << return_string;
|
|
|
|
|
return return_string;
|
2019-11-03 13:27:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 21:28:52 +02:00
|
|
|
|
/**
|
|
|
|
|
@brief BOMExportDialog::on_m_format_as_bom_clicked
|
|
|
|
|
@param checked
|
|
|
|
|
*/
|
2020-08-17 16:42:07 +02:00
|
|
|
|
void BOMExportDialog::on_m_format_as_bom_clicked(bool checked) {
|
|
|
|
|
m_query_widget->setGroupBy("designation", checked);
|
|
|
|
|
m_query_widget->setCount("COUNT(*) AS designation_qty", checked);
|
2019-11-03 13:27:46 +01:00
|
|
|
|
}
|