15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 */ |
16 */ |
17 #include "EditSupplier.h" |
17 #include "EditSupplier.h" |
18 #include "../ui/ui_EditSupplier.h" |
18 #include "../ui/ui_EditSupplier.h" |
19 #include "bmsapp.h" |
19 #include "bmsapp.h" |
20 #include <QDebug> |
|
21 #include <QtSql> |
|
22 |
20 |
23 |
21 |
24 EditSupplier::EditSupplier(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditSupplier) |
22 EditSupplier::EditSupplier(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditSupplier) |
25 { |
23 { |
26 QSqlQuery query; |
24 QSqlQuery query; |
27 |
25 |
28 qDebug() << Q_FUNC_INFO << id; |
26 qDebug() << "EditSupplier record:" << id; |
29 ui->setupUi(this); |
27 ui->setupUi(this); |
|
28 this->recno = id; |
30 |
29 |
31 if (id < 0) { |
30 if (id < 0) { |
32 setWindowTitle( QString("BMSapp - Add new supplier")); |
31 setWindowTitle(QString("BMSapp - Add new supplier")); |
33 } else { |
32 } else { |
34 setWindowTitle( QString("BMSapp - Edit supplier %1").arg(id)); |
33 setWindowTitle(QString("BMSapp - Edit supplier %1").arg(id)); |
35 query.prepare("SELECT * FROM inventory_suppliers WHERE record = :recno"); |
34 query.prepare("SELECT * FROM inventory_suppliers WHERE record = :recno"); |
36 query.bindValue(":recno", id); |
35 query.bindValue(":recno", id); |
37 query.exec(); |
36 query.exec(); |
38 query.next(); |
37 query.next(); |
39 |
38 |
43 ui->zipEdit->setText(query.value(4).toString()); |
42 ui->zipEdit->setText(query.value(4).toString()); |
44 ui->countryEdit->setText(query.value(5).toString()); |
43 ui->countryEdit->setText(query.value(5).toString()); |
45 ui->webEdit->setText(query.value(6).toString()); |
44 ui->webEdit->setText(query.value(6).toString()); |
46 ui->emailEdit->setText(query.value(7).toString()); |
45 ui->emailEdit->setText(query.value(7).toString()); |
47 ui->phoneEdit->setText(query.value(8).toString()); |
46 ui->phoneEdit->setText(query.value(8).toString()); |
48 ui->notesEdit->setText(query.value(9).toString()); |
47 ui->notesEdit->setPlainText(query.value(9).toString()); |
49 } |
48 } |
|
49 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(onTextChanged())); |
50 } |
50 } |
51 |
51 |
52 |
52 |
53 EditSupplier::~EditSupplier() |
53 EditSupplier::~EditSupplier() |
54 { |
54 { |
55 qDebug() << Q_FUNC_INFO; |
55 qDebug() << "EditSupplier closed"; |
56 delete ui; |
56 delete ui; |
|
57 emit entry_changed(); |
57 } |
58 } |
58 |
59 |
59 |
60 |
60 void EditSupplier::onOKButtonClicked() |
61 void EditSupplier::onOKButtonClicked() |
61 { |
62 { |
62 qDebug() << Q_FUNC_INFO << "Ok, check for valid data"; |
63 QSqlQuery query; |
|
64 bool modified, valid; |
63 |
65 |
|
66 /* The notes field uses a signal textChanged() */ |
|
67 modified = (ui->nameEdit->isModified() || ui->addressEdit->isModified() || ui->cityEdit->isModified() || |
|
68 ui->zipEdit->isModified() || ui->countryEdit->isModified() || ui->webEdit->isModified() || |
|
69 ui->emailEdit->isModified() || ui->phoneEdit->isModified() || this->textIsChanged); |
|
70 |
|
71 valid = true; |
64 /* If there are errors in the form, show a message and do "return;" */ |
72 /* If there are errors in the form, show a message and do "return;" */ |
|
73 if (ui->nameEdit->text().length() < 2) { |
|
74 // QMessageBox msgBox; |
|
75 // msgBox.setText("Name empty or too short."); |
|
76 // msgBox.exec(); |
|
77 return; |
|
78 } |
65 |
79 |
66 /* Save or insert the data */ |
80 if (modified && valid) { |
|
81 qDebug() << "EditSupplier do SQL"; |
|
82 if (this->recno == -1) { |
|
83 query.prepare("INSERT INTO inventory_suppliers SET name = :name, address = :address, city = :city, zip = :zip, " |
|
84 "country = :country, website = :web, email = :email, phone = :phone, notes = :notes, uuid = :uuid"); |
|
85 } else { |
|
86 query.prepare("UPDATE inventory_suppliers SET name = :name, address = :address, city = :city, zip = :zip, " |
|
87 "country = :country, website = :web, email = :email, phone = :phone, notes = :notes WHERE record = :recno"); |
|
88 } |
|
89 query.bindValue(":name", ui->nameEdit->text()); |
|
90 query.bindValue(":address", ui->addressEdit->text()); |
|
91 query.bindValue(":city", ui->cityEdit->text()); |
|
92 query.bindValue(":zip", ui->zipEdit->text()); |
|
93 query.bindValue(":country", ui->countryEdit->text()); |
|
94 query.bindValue(":web", ui->webEdit->text()); |
|
95 query.bindValue(":email", ui->emailEdit->text()); |
|
96 query.bindValue(":phone", ui->phoneEdit->text()); |
|
97 query.bindValue(":notes", ui->notesEdit->toPlainText()); |
|
98 if (this->recno == -1) { |
|
99 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); |
|
100 } else { |
|
101 query.bindValue(":recno", this->recno); |
|
102 } |
|
103 query.exec(); |
|
104 if (query.lastError().isValid()) { |
|
105 qDebug() << query.lastError(); |
|
106 } |
67 |
107 |
68 this->close(); |
108 this->close(); |
69 this->setResult(1); |
109 this->setResult(1); |
|
110 } else { |
|
111 /* Not saving */ |
|
112 this->close(); |
|
113 this->setResult(0); |
|
114 } |
|
115 } |
|
116 |
|
117 |
|
118 void EditSupplier::onTextChanged() |
|
119 { |
|
120 this->textIsChanged = true; |
70 } |
121 } |
71 |
122 |
72 |
123 |
73 void EditSupplier::onCancelButtonClicked() |
124 void EditSupplier::onCancelButtonClicked() |
74 { |
125 { |
75 qDebug() << Q_FUNC_INFO; |
|
76 this->close(); |
126 this->close(); |
77 this->setResult(0); |
127 this->setResult(0); |
78 } |
128 } |