-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmanager.cpp
422 lines (342 loc) · 15.5 KB
/
pmanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include "pmanager.h"
#include "ui_pmanager.h"
#include <QMessageBox>
PManager::PManager(QWidget *parent) :
QTabWidget(parent),
ui(new Ui::PManager)
{
ui->setupUi(this);
ui->del_prod_type->setEnabled(false);
selected_row = -1;
type_items = new QList<QTreeWidgetItem *>();
items = new QList<QTreeWidgetItem *>();
db = EDBconnection::getInstance();
//
ui->prod_treeWidget->hideColumn(4);
ui->suppliers_types_table->hideColumn(3);
ui->prod_types_table->setColumnWidth(1, 300);
ui->suppliers_types_table->setColumnWidth(2, 180);
updateSuppliersList();
}
PManager::~PManager()
{
delete ui;
}
void PManager::startP(void)
{
updateProdTypesList();
this->show();
}
// PRODUCT TYPES
void PManager::on_add_prod_type_clicked()
{
QString name = ui->prod_type_name_ed->text();
QString description = ui->prod_type_dscr_ed->toPlainText();
if (!name.isEmpty() && !description.isEmpty()) {
QString query = QString("INSERT INTO product_types (name, description) VALUES('%1', '%2')").arg(name).arg(description);
EDBconnection::getInstance()->insert(query);
qDebug()<<"execute: "<<query;
updateProdTypesList();
}
}
void PManager::updateProdTypesList(void)
{
QTableWidgetItem *ptwi = 0;
QList<QStringList> List = EDBconnection::getInstance()->get("SELECT name, description FROM product_types");
if (!List.isEmpty()) {
ui->prod_types_table->setRowCount(List.length());
for (int i = 0; i < List.length(); ++i)
for (int j = 0; j < List.at(0).length(); ++j) {
ptwi = new QTableWidgetItem(List[i].at(j));
ui->prod_types_table->setItem(i, j, ptwi);
}
} else {
qDebug("List is empty");
}
}
void PManager::updateProdList(void)
{
QString query = QString("SELECT pt.name as type_name, " \
" p.title as product_title, p.description as prod_dscr," \
" p.extra_charge as extra_charge, p.rating as rating, p.id as id" \
" FROM products AS p INNER JOIN product_types AS pt" \
" ON p.product_type_id=pt.id ORDER BY type_name");
QList<QStringList> List = db->get(query);
if (type_items->length() > 0) type_items->clear();
if (items->length() > 0) items->clear();
ui->prod_treeWidget->clear();
ui->prod_type_cb->clear();
type_items->append(new QTreeWidgetItem(ui->prod_treeWidget, QStringList(QObject::trUtf8("Add new"))));
type_items->at(0)->setForeground(0, QBrush(QColor(0xFF, 0xAA, 0x00)));
if (!List.isEmpty()) {
for (int i=0; i < List.length(); ++i) {
if (i == 0 || QString::compare(List.at(i).at(0), List.at(i-1).at(0), Qt::CaseSensitive) != 0) {
type_items->append(new QTreeWidgetItem(ui->prod_treeWidget, QStringList(List[i][0])));
type_items->at(type_items->length()-1)->setForeground(0, QBrush(QColor(0x00, 0x2E, 0xB8)));
type_items->at(type_items->length()-1)->setBackgroundColor(0, QColor(0xCC, 0xD9, 0xD3, 150));
type_items->at(type_items->length()-1)->setBackgroundColor(1, QColor(0xCC, 0xD9, 0xD3, 150));
type_items->at(type_items->length()-1)->setBackgroundColor(2, QColor(0xCC, 0xD9, 0xD3, 150));
type_items->at(type_items->length()-1)->setBackgroundColor(3, QColor(0xCC, 0xD9, 0xD3, 150));
}
QStringList tmpList;
tmpList<<List[i][1]<<List[i][2]<<List[i][3]<<List[i][4]<<List[i][5];
QTreeWidgetItem *temp_item = new QTreeWidgetItem(type_items->at(type_items->length() - 1), tmpList);
items->append(temp_item);
// public slots inherited from QTreeView -- void QTreeView::expandAll () [slot]
// Warning: if the model contains a large number of items, this function will take some time to execute.
if (List.length() < 20) ui->prod_treeWidget->expandAll();
}
} else {
qDebug("List is empty");
return;
}
// select, click & focus 'add new' item
ui->prod_treeWidget->setCurrentItem(ui->prod_treeWidget->topLevelItem(0));
on_prod_treeWidget_itemClicked(ui->prod_treeWidget->currentItem(), 0);
ui->prod_treeWidget->setFocus();
}
void PManager::on_prod_types_table_cellActivated(int row, int column)
{
Q_UNUSED(column);
selected_row = row;
ui->prod_type_name_ed->setText(ui->prod_types_table->item(row, 0)->text());
ui->prod_type_dscr_ed->setText(ui->prod_types_table->item(row, 1)->text());
// ui->add_prod_type->setEnabled(false);
ui->save_prod_type->setEnabled(true);
ui->del_prod_type->setEnabled(true);
}
void PManager::on_save_prod_type_clicked()
{
qDebug("save");
QString new_name = db->escape(ui->prod_type_name_ed->text());
QString new_description = db->escape(ui->prod_type_dscr_ed->toPlainText());
QString name = ui->prod_types_table->item(selected_row, 0)->text();
QString query = QString("UPDATE product_types SET name='%1', description='%2' WHERE name = '%3'")
.arg(new_name).arg(new_description).arg(name);
qDebug()<<query;
EDBconnection::getInstance()->query(query);
updateProdTypesList();
}
void PManager::on_del_prod_type_clicked()
{
qDebug("delete");
QString name = db->escape(ui->prod_types_table->item(selected_row, 0)->text());
QString query = QString("DELETE FROM product_types WHERE name = '%1'").arg(name);
EDBconnection::getInstance()->query(query);
ui->prod_type_name_ed->clear();
ui->prod_type_dscr_ed->clear();
updateProdTypesList();
}
void PManager::on_tabWidget_currentChanged(int index)
{
switch (index) {
case 0 : updateProdTypesList(); break;
case 1 : updateProdList(); break;
case 2 : updateSuppliersList(); break;
}
}
// PRODUCTS
void PManager::on_prod_treeWidget_itemClicked(QTreeWidgetItem* item, int column)
{
Q_UNUSED(column);
ui->add_update_btn->setEnabled(true);
ui->prod_type_cb->clear();
if (item->parent() != 0) {
// resource selected
ui->prod_type_cb->addItem(item->parent()->text(0));
ui->prod_title_le->setText(item->text(0));
ui->prod_dscr_te->setText(item->text(1));
ui->extra_charge_dsb->setValue(item->text(2).toDouble());
ui->rating_dsb->setValue(item->text(3).toDouble());
ui->add_update_btn->setText(trUtf8("Изменить"));
ui->delete_btn->setEnabled(true);
ui->prod_title_le->setReadOnly(true);
} else {
// resource type or 'Add New' selected
ui->prod_title_le->clear();
ui->prod_dscr_te->clear();
ui->extra_charge_dsb->setValue(0.00);
ui->rating_dsb->setValue(0.0);
ui->delete_btn->setEnabled(false);
ui->prod_title_le->setReadOnly(false);
if (ui->prod_treeWidget->indexOfTopLevelItem(item) == 0) {
// 'Add New' selected
ui->add_update_btn->setText(trUtf8("Добавить"));
QList<QStringList> List = db->get("SELECT name FROM product_types ORDER BY name");
for (int i = 0; i < List.length(); ++i) ui->prod_type_cb->addItem(List[i].at(0));
} else {
// resource type selected
ui->add_update_btn->setEnabled(false);
}
}
}
void PManager::on_add_update_btn_clicked()
{
// Add / Update resources info
QString prod_type = EDBconnection::escape(ui->prod_type_cb->currentText());
QString prod_title = EDBconnection::escape(ui->prod_title_le->text());
QString prod_dscr = db->escape(ui->prod_dscr_te->toPlainText());
double extra_chrge = ui->extra_charge_dsb->value();
double rating = ui->rating_dsb->value();
if (prod_type.isEmpty() || prod_title.isEmpty()) return;
if (ui->prod_treeWidget->currentItem()->parent() != 0) {
qDebug("Update");
// Update suppliers_resources
QString query = QString("UPDATE products SET title = '%1', description = '%2', extra_charge = '%3', rating = '%4' "\
"WHERE id = '%5'").arg(prod_title).arg(prod_dscr).arg(extra_chrge).arg(rating)
.arg(ui->prod_treeWidget->currentItem()->text(4));
db->query(query);
} else if (ui->prod_treeWidget->indexOfTopLevelItem(ui->prod_treeWidget->currentItem()) == 0) {
qDebug("Add new");
// this resource type already exists
QString query = QString("SELECT id FROM `product_types` WHERE name = '%1'").arg(prod_type);
int prod_type_id = db->get(query)[0].at(0).toInt();
qDebug()<<"prod_type_id :"<<prod_type_id;
if (prod_type_id == -1) {
qDebug("ERROR : (prod_type_id == -1)");
return;
}
// Insert resource (UNIQUE)
query = QString("INSERT INTO products(product_type_id, title, description, extra_charge, rating) " \
"VALUES('%1', '%2', '%3', '%4', '%5') ")
.arg(prod_type_id).arg(prod_title).arg(prod_dscr).arg(extra_chrge).arg(rating);
int res_id = db->insert(query);
if (res_id == -1) {
// ERROR message
QMessageBox::warning(0, "Warning", trUtf8("Ошибка при добавлении товара. \n" \
"Возможно товар с таким названием уже существует."));
return;
}
}
updateProdList();
}
void PManager::on_delete_btn_clicked()
{
// Delete resources
qDebug("delete");
int result = QMessageBox::question( 0, trUtf8("Внимание"),
trUtf8("Вы действительно хотите удалить этот товар?"),
QMessageBox::Yes, QMessageBox::No );
if (result == QMessageBox::Yes) {
QString query = QString("DELETE FROM products WHERE id='%1'").arg(ui->prod_treeWidget->currentItem()->text(4));
db->query(query);
updateProdList();
}
}
// SUPPLIERS
void PManager::updateSuppliersList(void)
{
QList<QStringList> List = EDBconnection::getInstance()->get("SELECT name, distance, contacts, id FROM suppliers");
if (!List.isEmpty()) {
ui->suppliers_types_table->setRowCount(List.length());
for (int i = 0; i < List.length(); ++i)
for (int j = 0; j < List.at(0).length(); ++j)
ui->suppliers_types_table->setItem(i, j, new QTableWidgetItem(List[i].at(j)));
} else {
qDebug("List is empty");
}
}
void PManager::on_add_supplier_btn_clicked()
{
QString name = db->escape(ui->supplier_name_le->text());
QString contacts = db->escape(ui->supplier_contacts_te->toPlainText());
double distance = ui->supplier_distance_dsb->value();
if (!name.isEmpty() && !contacts.isEmpty() && distance != 0.0) {
QString query = QString("INSERT INTO suppliers (name, distance, contacts, regdate) VALUES('%1', '%2', '%3', '%4')")
.arg(name).arg(distance).arg(contacts)
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH-mm-ss"));
EDBconnection::getInstance()->insert(query);
updateSuppliersList();
}
}
void PManager::on_suppliers_types_table_cellClicked(int row, int column)
{
Q_UNUSED(column);
ui->supplier_name_le->setText(ui->suppliers_types_table->item(row, 0)->text());
ui->supplier_contacts_te->setText(ui->suppliers_types_table->item(row, 2)->text());
ui->supplier_distance_dsb->setValue(ui->suppliers_types_table->item(row, 1)->text().toDouble());
ui->save_supplier_btn->setEnabled(true);
ui->del_supplier_btn->setEnabled(true);
}
void PManager::on_save_supplier_btn_clicked()
{
qDebug("save");
QString name = db->escape(ui->supplier_name_le->text());
QString contacts = db->escape(ui->supplier_contacts_te->toPlainText());
double distance = ui->supplier_distance_dsb->value();
int current_supplier_id = ui->suppliers_types_table->item(ui->suppliers_types_table->currentRow(), 3)->text().toInt();
QString query = QString("UPDATE suppliers SET name='%1', distance='%2', contacts='%3' WHERE id = '%4'")
.arg(name).arg(distance).arg(contacts).arg(current_supplier_id);
EDBconnection::getInstance()->query(query);
updateSuppliersList();
}
void PManager::on_del_supplier_btn_clicked()
{
// Delete supplier
qDebug("delete");
int result = QMessageBox::question( 0, trUtf8("Внимание"),
trUtf8("Вы действительно хотите удалить этого поставщика?"),
QMessageBox::Yes, QMessageBox::No );
if (result == QMessageBox::Yes) {
int current_supplier = ui->suppliers_types_table->item(ui->suppliers_types_table->currentRow(), 3)->text().toInt();
QString query = QString("DELETE FROM suppliers WHERE id='%1'").arg(current_supplier);
db->query(query);
updateSuppliersList();
}
}
// PRICE LIST
void PManager::updatePriceList(void)
{
/*
SELECT CONCAT(pt.name, " ", p.title) as product, s.name as supplier, prc.price, prc.number
FROM price AS prc
INNER JOIN suppliers AS s ON s.id=prc.supplier_id
INNER JOIN products AS p ON p.id=prc.product_id
INNER JOIN product_types AS pt ON p.product_type_id=pt.id
*/
QString query = "SELECT CONCAT(pt.name, " ", p.title) as product FROM products AS p " \
"INNER JOIN product_types AS pt ON p.product_type_id=pt.id";
QList<QStringList> products_List = db->get(query);
if (!products_List.isEmpty()) {
// ui->suppliers_types_table->setRowCount(products_List.length());
for (int i = 0; i < products_List.length(); ++i)
for (int j = 0; j < products_List.at(0).length(); ++j)
ui->suppliers_types_table->setItem(i, j, new QTableWidgetItem(products_List[i].at(j)));
} else {
qDebug("List is empty");
}
QList<QStringList> suppliers_List = db->get("SELECT name FROM suppliers");
if (!suppliers_List.isEmpty()) {
ui->suppliers_types_table->setRowCount(suppliers_List.length());
for (int i = 0; i < suppliers_List.length(); ++i)
for (int j = 0; j < suppliers_List.at(0).length(); ++j)
ui->suppliers_types_table->setItem(i, j, new QTableWidgetItem(suppliers_List[i].at(j)));
} else {
qDebug("List is empty");
}
}
void PManager::on_new_price_rec_btn_clicked()
{
ui->suppliers_types_table->clear();
ui->suppliers_types_table->setRowCount(1);
QString query = "SELECT CONCAT(pt.name, " ", p.title) as product FROM products AS p " \
"INNER JOIN product_types AS pt ON p.product_type_id=pt.id";
QList<QStringList> products_List = db->get(query);
if (!products_List.isEmpty()) {
// ui->suppliers_types_table->setRowCount(products_List.length());
for (int i = 0; i < products_List.length(); ++i)
for (int j = 0; j < products_List.at(0).length(); ++j)
ui->suppliers_types_table->setItem(i, j, new QTableWidgetItem(products_List[i].at(j)));
} else {
qDebug("List is empty");
}
QList<QStringList> suppliers_List = db->get("SELECT name FROM suppliers");
if (!suppliers_List.isEmpty()) {
ui->suppliers_types_table->setRowCount(suppliers_List.length());
for (int i = 0; i < suppliers_List.length(); ++i)
for (int j = 0; j < suppliers_List.at(0).length(); ++j)
ui->suppliers_types_table->setItem(i, j, new QTableWidgetItem(suppliers_List[i].at(j)));
} else {
qDebug("List is empty");
}
}