src/EditSupplier.cpp

Tue, 12 Apr 2022 21:03:19 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 12 Apr 2022 21:03:19 +0200
changeset 131
0115b97e8c39
parent 90
2396457a8167
permissions
-rw-r--r--

Added global variables, C++ lovers will hate that. Added global acid data. Fixed several load and save errors in the json arrays in the recipe record. Added first part of the miscs table. The first part of the water tab has values.

/**
 * EditSupplier.cpp is part of bmsapp.
 *
 * bmsapp 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 3 of the License, or
 * (at your option) any later version.
 *
 * bmsapp 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "EditSupplier.h"
#include "../ui/ui_EditSupplier.h"
#include "MainWindow.h"


EditSupplier::EditSupplier(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditSupplier)
{
    QSqlQuery query;

    qDebug() << "EditSupplier record:" << id;
    ui->setupUi(this);
    this->recno = id;

    WindowTitle();
    if (id >= 0) {
	query.prepare("SELECT * FROM inventory_suppliers WHERE record = :recno");
	query.bindValue(":recno", id);
	query.exec();
	query.next();

	ui->nameEdit->setText(query.value(1).toString());
	ui->addressEdit->setText(query.value(2).toString());
	ui->cityEdit->setText(query.value(3).toString());
	ui->zipEdit->setText(query.value(4).toString());
	ui->countryEdit->setText(query.value(5).toString());
	ui->webEdit->setText(query.value(6).toString());
	ui->emailEdit->setText(query.value(7).toString());
	ui->phoneEdit->setText(query.value(8).toString());
	ui->notesEdit->setPlainText(query.value(9).toString());
    }
    connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
    connect(ui->addressEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
    connect(ui->cityEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
    connect(ui->zipEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
    connect(ui->countryEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
    connect(ui->webEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
    connect(ui->emailEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
    connect(ui->phoneEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));

    ui->saveButton->setEnabled(false);
    ui->deleteButton->setEnabled((id >= 0) ? true:false);
}


EditSupplier::~EditSupplier()
{
    qDebug() << "EditSupplier done";
    delete ui;
    emit entry_changed();
}


/*
 * Window header, mark any change with '**'
 */
void EditSupplier::WindowTitle()
{
    QString txt;

    if (this->recno < 0) {
	txt = QString(tr("BMSapp - Add new supplier"));
    } else {
	txt = QString(tr("BMSapp - Edit supplier %1").arg(this->recno));
    }

    if (this->textIsChanged) {
	txt.append((QString(" **")));
    }
    setWindowTitle(txt);
}


void EditSupplier::on_saveButton_clicked()
{
    QSqlQuery query;

    /* If there are errors in the form, show a message and do "return;" */
    if (ui->nameEdit->text().length() < 2) {
	QMessageBox::warning(this, tr("Edit Supplier"), tr("Name empty or too short."));
	return;
    }

    if (this->textIsChanged) {
    	if (this->recno == -1) {
    	    query.prepare("INSERT INTO inventory_suppliers SET name = :name, address = :address, city = :city, zip = :zip, "
		"country = :country, website = :web, email = :email, phone = :phone, notes = :notes, uuid = :uuid");
    	} else {
	    query.prepare("UPDATE inventory_suppliers SET name = :name, address = :address, city = :city, zip = :zip, "
		"country = :country, website = :web, email = :email, phone = :phone, notes = :notes WHERE record = :recno");
    	}
	query.bindValue(":name", ui->nameEdit->text());
	query.bindValue(":address", ui->addressEdit->text());
	query.bindValue(":city", ui->cityEdit->text());
	query.bindValue(":zip", ui->zipEdit->text());
	query.bindValue(":country", ui->countryEdit->text());
	query.bindValue(":web", ui->webEdit->text());
	query.bindValue(":email", ui->emailEdit->text());
	query.bindValue(":phone", ui->phoneEdit->text());
	query.bindValue(":notes", ui->notesEdit->toPlainText());
	if (this->recno == -1) {
	    query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
	} else {
	    query.bindValue(":recno", this->recno);
	}
	query.exec();
	if (query.lastError().isValid()) {
	    qDebug() << "EditSupplier" << query.lastError();
	    QMessageBox::warning(this, tr("Database error"),
                        tr("MySQL error: %1\n%2\n%3")
                        .arg(query.lastError().nativeErrorCode())
                        .arg(query.lastError().driverText())
                        .arg(query.lastError().databaseText()));
	} else {
	    qDebug() << "EditSupplier Saved";
	}
    }

    ui->saveButton->setEnabled(false);
    this->textIsChanged = false;
    WindowTitle();
}


void EditSupplier::on_deleteButton_clicked()
{
    QSqlQuery query;

    query.prepare("DELETE FROM inventory_suppliers WHERE record = :recno");
    query.bindValue(":recno", this->recno);
    query.exec();
    if (query.lastError().isValid()) {
        qDebug() << "EditSupplier" << query.lastError();
        QMessageBox::warning(this, tr("Database error"),
                        tr("MySQL error: %1\n%2\n%3")
                        .arg(query.lastError().nativeErrorCode())
                        .arg(query.lastError().driverText())
                        .arg(query.lastError().databaseText()));
    } else {
        qDebug() << "EditSupllier Deleted" << this->recno;
    }

    this->close();
    this->setResult(1);
}


void EditSupplier::is_changed()
{
    ui->saveButton->setEnabled(true);
    this->textIsChanged = true;
    WindowTitle();
}


void EditSupplier::on_quitButton_clicked()
{
    if (this->textIsChanged) {
        int rc = QMessageBox::warning(this, tr("Supplier changed"), tr("This supplier has been modified. Save changes?"),
                                QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
        switch (rc) {
            case QMessageBox::Save:
                        on_saveButton_clicked();
                        break;  /* Saved and then Quit */
            case QMessageBox::Discard:
                        break;  /* Quit without Save */
            case QMessageBox::Cancel:
                        return; /* Return to the editor page */
        }
    }

    this->close();
    this->setResult(1);
}

mercurial