src/EditSupplier.cpp

Tue, 15 Feb 2022 21:21:12 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 15 Feb 2022 21:21:12 +0100
changeset 11
c9cdc15d3caf
parent 10
8aa2bd9ba9e8
child 17
f0bcdbd3d36f
permissions
-rw-r--r--

The Supplier editor saves changes and inserts new suppliers. It sends a signal to InventorySuppliers when done (always for now). Refresh the table still doesn't work. Added a missing iconn in the Inventory menus dropdown.

/**
 * 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 "bmsapp.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;

    if (id < 0) {
	setWindowTitle(QString("BMSapp - Add new supplier"));
    } else {
	setWindowTitle(QString("BMSapp - Edit supplier %1").arg(id));
	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->notesEdit, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
}


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


void EditSupplier::onOKButtonClicked()
{
    QSqlQuery query;
    bool modified, valid;

    /* The notes field uses a signal textChanged() */
    modified = (ui->nameEdit->isModified() || ui->addressEdit->isModified() || ui->cityEdit->isModified() ||
		ui->zipEdit->isModified() || ui->countryEdit->isModified() || ui->webEdit->isModified() ||
		ui->emailEdit->isModified() || ui->phoneEdit->isModified() || this->textIsChanged);

    valid = true;
    /* If there are errors in the form, show a message and do "return;" */
    if (ui->nameEdit->text().length() < 2) {
//	QMessageBox msgBox;
//	msgBox.setText("Name empty or too short.");
//	msgBox.exec();
	return;
    }

    if (modified && valid) {
	qDebug() << "EditSupplier do SQL";
    	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() << query.lastError();
	}

    	this->close();
    	this->setResult(1);
    } else {
	/* Not saving */
	this->close();
	this->setResult(0);
    }
}


void EditSupplier::onTextChanged()
{
    this->textIsChanged = true;
}


void EditSupplier::onCancelButtonClicked()
{
    this->close();
    this->setResult(0);
}

mercurial