-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
551 lines (466 loc) · 18 KB
/
mainwindow.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "loginwindow.h"
#include "utilities.h"
#include "salesmanagementwindow.h"
#include "editproductfromsalewindow.h"
#include "inventorymanagementwindow.h"
#include "collaboratorsmanagementwindow.h"
#include "databaseconnection.h"
#include <QMessageBox>
// Globals variables
int MainWindow::id_collaborator;
QString MainWindow::name_collaborator;
int MainWindow::access_collaborator;
bool MainWindow::userLogged;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Configure window
QIcon iconWindow;
iconWindow.addFile(":/images/sale.png");
this->setWindowIcon(iconWindow);
this->setWindowTitle("Sale");
this->setFixedSize(699, 415);
// Init window setup
userLogged = false;
ui->label_name->setText("No user logged in");
lockedPadlock.addFile(":/images/lock.png");
unlockedPadlock.addFile(":/images/unlock.png");
ui->pushButton_Block->setText("");
ui->pushButton_Block->setIcon(lockedPadlock);
ui->statusbar->addWidget(ui->pushButton_Block);
ui->statusbar->addWidget(ui->label_name);
// Init window configurations
InitFieldsWindow();
ui->pushButton_editProduct->setAutoDefault(false);
ui->pushButton_finalizeSale->setAutoDefault(false);
ui->pushButton_removeProduct->setAutoDefault(false);
ui->pushButton_searchProduct->setAutoDefault(false);
ui->lineEdit_total->setEnabled(false);
// Configure sales table
Utilities utilities;
QStringList headerLabels = {"Id", "Description", "Unit value", "Quantity", "Total"};
utilities.TableWidgetBasicConfigurations(ui->tableWidget_listProducts, headerLabels);
// Configure regex to int fields
utilities.ConfigureRegexLineEdit(ui->lineEdit_idProduct, 2);
utilities.ConfigureRegexLineEdit(ui->lineEdit_quantity, 2);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_Block_clicked()
{
// Check if user is logged
if(!userLogged)
{
LoginWindow loginWindow;
loginWindow.exec();
// Check user logged
if(userLogged)
{
ui->pushButton_Block->setIcon(unlockedPadlock);
ui->label_name->setText(name_collaborator);
}
}
else
{
userLogged = false;
ui->label_name->setText("No user logged in");
ui->pushButton_Block->setIcon(lockedPadlock);
}
}
void MainWindow::on_actionInventory_triggered()
{
// Check the access
if(userLogged && (access_collaborator == 1 || access_collaborator == 2))
{
InventoryManagementWindow inventoryManagementWindow;
inventoryManagementWindow.exec();
}
else
{
QMessageBox::information(this, "Information", "Unauthorized access");
}
}
void MainWindow::on_actionCollaborators_triggered()
{
// Check the access
if(userLogged && (access_collaborator == 1 || access_collaborator == 2))
{
CollaboratosManagementWindow collaboratorsManagementWindow;
collaboratorsManagementWindow.exec();
}
else
{
QMessageBox::information(this, "Information", "Unauthorized access");
}
}
void MainWindow::on_actionSales_triggered()
{
// Check the access
if(userLogged && (access_collaborator == 1 || access_collaborator == 2))
{
SalesManagementWindow salesManagementWindow;
salesManagementWindow.exec();
}
else
{
QMessageBox::information(this, "Information", "Unauthorized access");
}
}
void MainWindow::InsertProductIntoTableWidget()
{
DatabaseConnection dbConnection;
// Get product informations
if(dbConnection.open())
{
QSqlQuery query;
query.prepare("SELECT id, quantity, description, sale_price FROM tb_inventory WHERE id = " +
ui->lineEdit_idProduct->text());
if(query.exec())
{
query.first();
if(query.value(0).toString() != "")
{
// Check if the quantity is valid
int quantity_storage = query.value(1).toInt();
if (quantity_storage >= ui->lineEdit_quantity->text().toInt())
{
float total;
int line = 0;
// Insert into table widget
ui->tableWidget_listProducts->insertRow(line);
ui->tableWidget_listProducts->setItem(line, 0, new QTableWidgetItem(query.value(0).toString()));
ui->tableWidget_listProducts->setItem(line, 1, new QTableWidgetItem(query.value(2).toString()));
ui->tableWidget_listProducts->setItem(line, 2, new QTableWidgetItem(query.value(3).toString()));
ui->tableWidget_listProducts->setItem(line, 3, new QTableWidgetItem(ui->lineEdit_quantity->text()));
total = ui->lineEdit_quantity->text().toInt() * query.value(3).toString().toFloat();
ui->tableWidget_listProducts->setItem(line, 4, new QTableWidgetItem(QString::number(total)));
ui->tableWidget_listProducts->setRowHeight(line, 20);
// Update total sale
total = CalculateTotalSale(ui->tableWidget_listProducts, 4);
ui->lineEdit_total->setText(QString::number(total));
// Update product quantity at database
UpdateProductQuantiy(query.value(0).toInt(), -(ui->lineEdit_quantity->text().toInt()));
}
else
{
QString message = (quantity_storage == 0) ? "Product unavailable. Quantity in stock is zero." :
"Invalid quantity. Max quantity available: " + query.value(1).toString();
QMessageBox::warning(this, "Error", message);
}
}
else
{
QMessageBox::warning(this, "Error", "Product not found");
}
}
else
{
QMessageBox::warning(this, "Error", "Erro to add product to sale");
}
dbConnection.close();
}
else
{
QMessageBox::warning(this, "Error", "Unable to connect database to read product informations");
}
// Restore window configurations
InitFieldsWindow();
}
void MainWindow::UpdateProductQuantiy(int id_product, int quantity)
{
DatabaseConnection dbConnection;
if(dbConnection.open())
{
// Get current quantity
QSqlQuery query;
query.prepare("SELECT quantity FROM tb_inventory WHERE id = " + QString::number(id_product));
if(query.exec())
{
query.first();
// Calculate new quantity
int new_quantity = query.value(0).toInt() + quantity;
// Update quantity at database
query.prepare("UPDATE tb_inventory SET quantity = " + QString::number(new_quantity) +
" WHERE id = " + QString::number(id_product));
// Check query status
if(!query.exec())
{
QMessageBox::warning(this, "Error", "Erro to update product quantity");
}
}
else
{
QMessageBox::warning(this, "Error", "Product not found");
}
dbConnection.close();
}
else
{
QMessageBox::warning(this, "Error", "Unable to connect database to read product quantity");
}
}
void MainWindow::InitFieldsWindow()
{
// Init window configurations
ui->lineEdit_quantity->setText("1");
ui->lineEdit_idProduct->clear();
ui->lineEdit_idProduct->setFocus();
}
float MainWindow::CalculateTotalSale(QTableWidget *tableWidget, int column)
{
// Init total
float total = 0;
// Add the value from each row
for(int i = 0; i < tableWidget->rowCount(); i++)
{
// Update total
total += tableWidget->item(i, column)->text().toFloat();
}
return total;
}
void MainWindow::on_lineEdit_idProduct_returnPressed()
{
// Check if user is logged
if(!userLogged)
{
QMessageBox::information(this, "Error", "Log in first");
return;
}
// Insert new product to the table widget
InsertProductIntoTableWidget();
}
void MainWindow::on_pushButton_finalizeSale_clicked()
{
// Check if user is logged
if(!userLogged)
{
QMessageBox::information(this, "Error", "Log in first");
return;
}
if(ui->tableWidget_listProducts->rowCount() != 0)
{
DatabaseConnection dbConnection;
// Finalizing sale
if(dbConnection.open())
{
// Get fields
int id_collaborator = MainWindow::id_collaborator;
float total = CalculateTotalSale(ui->tableWidget_listProducts, 4);
QString date = QDate::currentDate().toString("yyyy-MM-dd") +
QTime::currentTime().toString(":hh:mm:ss");
// Update tb_sales table
QSqlQuery query;
query.prepare("INSERT INTO tb_sales (date, id_collaborator, total_value) VALUES ('" + date
+ "', " + QString::number(id_collaborator) + ", " + QString::number(total) + ")");
// Check query return
if(query.exec())
{
// Build query
query.prepare("SELECT id FROM tb_sales ORDER BY id DESC LIMIT 1");
// Execute query
if(query.exec())
{
// Get sale id
query.first();
int id_sale = query.value(0).toInt();
// Get products
for(int i = 0; i < ui->tableWidget_listProducts->rowCount(); i++)
{
// Get fields
int id_product = ui->tableWidget_listProducts->item(i, 0)->text().toInt();
float sale_price = ui->tableWidget_listProducts->item(i, 2)->text().toFloat();
int quantity = ui->tableWidget_listProducts->item(i, 3)->text().toInt();
// Update tb_products_sales table
query.prepare("INSERT INTO tb_products_sales (id_sale, id_product, quantity, sale_price) VALUES "
"(" + QString::number(id_sale) + ", " + QString::number(id_product) + ", " +
QString::number(quantity) + ", " + QString::number(sale_price) + ")");
// Check query return
if(!query.exec())
{
QMessageBox::warning(this, "Error", "Error to insert products sale information into database");
return;
}
}
QMessageBox::information(this, "Success", "Sale: '" +
QString::number(id_sale) +
"' inserted with success");
}
else
{
QMessageBox::warning(this, "Error", "Error to get sale id from database");
}
// Restore initial window configurations
InitFieldsWindow();
Utilities utilities;
utilities.CleanTableWidget(ui->tableWidget_listProducts);
ui->lineEdit_total->setText("0");
}
else
{
QMessageBox::warning(this, "Error", "Error to insert new sale into database");
}
dbConnection.close();
}
else
{
QMessageBox::information(this, "Error", "Unable to connect database to finalize sale");
}
}
else
{
QMessageBox::information(this, "Error", "Add a product first");
}
}
void MainWindow::on_pushButton_searchProduct_clicked()
{
// Check if user is logged
if(!userLogged)
{
QMessageBox::information(this, "Error", "Log in first");
return;
}
// Check if the product id field is empty
if(ui->lineEdit_idProduct->text().isEmpty())
{
QMessageBox::information(this, "Error", "Insert the product id");
return;
}
// Check if the quantity is valid
if(ui->lineEdit_quantity->text().toInt() <= 0)
{
QMessageBox::information(this, "Error", "The quantity should be greater than 0");
return;
}
// Insert product
InsertProductIntoTableWidget();
}
void MainWindow::on_pushButton_editProduct_clicked()
{
// Check if user is logged
if(!userLogged)
{
QMessageBox::information(this, "Error", "Log in first");
return;
}
// Check if is a valid product
if(ui->tableWidget_listProducts->currentRow() != -1)
{
// Get product fields
int id_product = ui->tableWidget_listProducts->item(ui->tableWidget_listProducts->currentRow(), 0)->text().toInt();
QString description = ui->tableWidget_listProducts->item(ui->tableWidget_listProducts->currentRow(), 1)->text();
float salePrice = ui->tableWidget_listProducts->item(ui->tableWidget_listProducts->currentRow(), 2)->text().toFloat();
int quantity = ui->tableWidget_listProducts->item(ui->tableWidget_listProducts->currentRow(), 3)->text().toInt();
// Show edit product from sale window
EditProductFromSaleWindow *editProductFromSaleWindow;
editProductFromSaleWindow = new EditProductFromSaleWindow(id_product, description, salePrice, quantity);
editProductFromSaleWindow->exec();
// Create database connection
DatabaseConnection dbConnection;
if(dbConnection.open())
{
// Get current quantity
QSqlQuery query;
query.prepare("SELECT quantity FROM tb_inventory WHERE id = " + QString::number(id_product));
if(query.exec())
{
query.first();
// Check if the new quantity is valid
if((editProductFromSaleWindow->quantity > (quantity + query.value(0).toInt())) &&
(quantity < editProductFromSaleWindow->quantity))
{
QMessageBox::information(this, "Error", "Invalid quantity. Maximum quantity available: " +
QString::number(quantity + query.value(0).toInt()));
dbConnection.close();
return;
}
else
{
// Update product quantity at database
UpdateProductQuantiy(id_product, quantity - editProductFromSaleWindow->quantity);
}
}
else
{
QMessageBox::warning(this, "Error", "Product not found");
}
dbConnection.close();
}
else
{
QMessageBox::information(this, "Error", "Unable to connect database to update product sale");
return;
}
// Update table widget
ui->tableWidget_listProducts->setItem(ui->tableWidget_listProducts->currentRow(), 2,
new QTableWidgetItem(QString::number(editProductFromSaleWindow->salePrice)));
ui->tableWidget_listProducts->setItem(ui->tableWidget_listProducts->currentRow(), 3,
new QTableWidgetItem(QString::number(editProductFromSaleWindow->quantity)));
// Update product total
float total = editProductFromSaleWindow->quantity * editProductFromSaleWindow->salePrice;
ui->tableWidget_listProducts->setItem(ui->tableWidget_listProducts->currentRow(), 4,
new QTableWidgetItem(QString::number(total)));
// Update sale total
total = CalculateTotalSale(ui->tableWidget_listProducts, 4);
ui->lineEdit_total->setText(QString::number(total));
ui->tableWidget_listProducts->setCurrentCell(-1, -1);
}
else
{
QMessageBox::information(this, "Error", "Select a product first");
}
}
void MainWindow::on_pushButton_removeProduct_clicked()
{
// Check if user is logged
if(!userLogged)
{
QMessageBox::information(this, "Error", "Log in first");
return;
}
// Check if is a valid product
if(ui->tableWidget_listProducts->currentColumn() != -1)
{
// Create message box
QMessageBox::StandardButton option;
option = QMessageBox::question(this, "Remove", "Do you want to remove this product?",
QMessageBox::Yes | QMessageBox::No);
// Check option selected
if(option == QMessageBox::Yes)
{
// Update product quantity
int id_product = ui->tableWidget_listProducts->item(ui->tableWidget_listProducts->currentRow(), 0)->text().toInt();
int quantity = ui->tableWidget_listProducts->item(ui->tableWidget_listProducts->currentRow(), 3)->text().toInt();
UpdateProductQuantiy(id_product, quantity);
// Removing row
ui->tableWidget_listProducts->removeRow(ui->tableWidget_listProducts->currentRow());
ui->tableWidget_listProducts->setCurrentCell(-1, -1);
// Update sale total
ui->lineEdit_total->setText(QString::number(CalculateTotalSale(ui->tableWidget_listProducts, 4)));
}
}
else
{
QMessageBox::information(this, "Error", "Select a product first");
}
}
void MainWindow::on_actionAbout_triggered()
{
// Build message
QString message = "This is free licensed software developed by Thiago Sérvulo Guimarães.\n"
"The version you are currently using is 1.0.0.\n"
"For further instructions on how to use the system, please refer "
"to the documentation in the README.md file.";
// Show message box
QMessageBox::information(this, "About", message);
}
void MainWindow::on_actionExit_triggered()
{
// Close application
this->close();
}