-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalesmanagementwindow.cpp
370 lines (307 loc) · 11.9 KB
/
salesmanagementwindow.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
#include "salesmanagementwindow.h"
#include "ui_salesmanagementwindow.h"
#include "databaseconnection.h"
#include "utilities.h"
#include <iostream>
#include <fstream>
#include <QDateEdit>
#include <QtSql>
#include <QMessageBox>
#include <QDir>
SalesManagementWindow::SalesManagementWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::SalesManagementWindow)
{
ui->setupUi(this);
// Window layout
QIcon iconWindow;
iconWindow.addFile(":/images/sale_management.png");
this->setWindowIcon(iconWindow);
this->setWindowTitle("Sales Management");
this->setFixedSize(789, 522);
// Configure data format
ui->dateEdit_initial->setDisplayFormat("yyyy-MM-dd");
ui->dateEdit_final->setDisplayFormat("yyyy-MM-dd");
// Init with a date
QDate today = QDate::currentDate();
ui->dateEdit_initial->setDate(today);
ui->dateEdit_final->setDate(today);
// Configure products sales table
Utilities utilities;
QStringList headerLabels = {"Product Id", "Quantity", "Unitary Price", "Total Price"};
utilities.TableWidgetBasicConfigurations(ui->tableWidget_productsSales, headerLabels);
utilities.CleanTableWidget(ui->tableWidget_productsSales);
// Configure sales table
headerLabels = {"Id", "Date", "Collaborator", "Total"};
utilities.TableWidgetBasicConfigurations(ui->tableWidget_sales, headerLabels);
ShowAllSalesIntoTableWidget();
}
void SalesManagementWindow::ShowAllSalesIntoTableWidget()
{
// Prepare tables
Utilities utilities;
ui->tableWidget_sales->clearSelection();
utilities.CleanTableWidget(ui->tableWidget_productsSales);
// Show all sales
DatabaseConnection dbConnection;
if(dbConnection.open())
{
// Build query
QSqlQuery query;
query.prepare("SELECT s.id, s.date, COALESCE(c.name, 'not found'), "
"printf('%.2f', s.total_value) "
"FROM tb_sales s LEFT JOIN tb_collaborators c "
"ON s.id_collaborator = c.id ORDER BY s.id");
// Update table widget
if(!utilities.QueryToUpdateTableWidget(&query, ui->tableWidget_sales))
{
QMessageBox::warning(this, "Error", "Unable to read sales from database");
}
dbConnection.close();
}
else
{
QMessageBox::warning(this, "Error", "Unable to connect database to read all sales");
}
}
SalesManagementWindow::~SalesManagementWindow()
{
delete ui;
}
void SalesManagementWindow::on_tableWidget_sales_itemSelectionChanged()
{
// Get current row
int currentRow = ui->tableWidget_sales->currentRow();
// Check whether is a valid row
if(currentRow < 0)
{
return;
}
// Show sales products
DatabaseConnection dbConnection;
if(dbConnection.open())
{
Utilities utilities;
QSqlQuery query;
// Get sale id
int idSale = ui->tableWidget_sales->item(currentRow, 0)->text().toInt();
// Build query
query.prepare("SELECT i.description, ps.quantity, printf('%.2f', ps.sale_price), "
"printf('%.2f', ps.quantity * ps.sale_price) "
"FROM tb_products_sales ps JOIN tb_inventory i ON ps.id_product = i.id "
"WHERE ps.id_sale = " + QString::number(idSale) + " ORDER BY ps.id");
// Update table widget
if(!utilities.QueryToUpdateTableWidget(&query, ui->tableWidget_productsSales))
{
QMessageBox::warning(this, "Error", "Unable to read prducts of sales from database");
}
dbConnection.close();
}
else
{
QMessageBox::warning(this, "Error", "Unable to connect database to read products of sales");
}
}
void SalesManagementWindow::on_pushButton_filter_clicked()
{
// Check dates
if(ui->dateEdit_final->date() < ui->dateEdit_initial->date())
{
QMessageBox::information(this, "Information", "The final date should be later than the initial");
return;
}
// Filter sales
DatabaseConnection dbConnection;
if(dbConnection.open())
{
// Block signals to ignore items selections changed
ui->tableWidget_sales->blockSignals(true);
Utilities utilities;
QSqlQuery query;
// Build query
query.prepare("SELECT s.id, s.date, c.name, printf('%.2f', s.total_value)"
" FROM tb_sales s JOIN tb_collaborators c "
"ON s.id_collaborator = c.id WHERE s.date BETWEEN '" +
ui->dateEdit_initial->text() + ":00:00:00' AND '" +
ui->dateEdit_final->text() + ":23:59:59' ORDER BY s.id");
// Update table widget
if(!utilities.QueryToUpdateTableWidget(&query, ui->tableWidget_sales))
{
QMessageBox::warning(this, "Error", "Unable to read sales from database");
}
// Clean product sales table
utilities.CleanTableWidget(ui->tableWidget_productsSales);
ui->tableWidget_sales->clearSelection();
ui->tableWidget_sales->setCurrentCell(-1, -1);
dbConnection.close();
// Restore signals
ui->tableWidget_sales->blockSignals(false);
}
else
{
QMessageBox::warning(this, "Error", "Unable to connect database to filter sales");
}
}
void SalesManagementWindow::on_pushButton_allSales_clicked()
{
ShowAllSalesIntoTableWidget();
}
void SalesManagementWindow::on_pushButton_export_clicked()
{
DatabaseConnection dbConnection;
if(dbConnection.open())
{
// Directory to save reports
QString directoryPath = "reports/";
// Create directory if it doesn't exist
if (!QDir(directoryPath).exists())
{
QDir().mkdir(directoryPath);
}
// Get current date
QDateTime currentDateTime = QDateTime::currentDateTime();
// Convert to string
QString currentDateTimeString = currentDateTime.toString("yyyy-MM-dd_hh_mm_ss");
// Build report file
std::string filePath = directoryPath.toStdString() + "report_" +
currentDateTimeString.toStdString() + ".txt";
std::ofstream reportFile(filePath, std::ios::out);
// Init sales quantity
int quantSales = 0;
// Open file
if (reportFile.is_open())
{
// Build query
QSqlQuery query1;
query1.prepare("SELECT s.id, s.date, c.name, printf('%.2f', s.total_value)"
" FROM tb_sales s JOIN tb_collaborators c"
" ON s.id_collaborator = c.id ORDER BY s.id");
// Execute query
if(query1.exec())
{
// Write elements
while(query1.next())
{
// Check sales quantity
if(!quantSales)
{
// Write header
reportFile << "Report sales\n\n";
}
// Increment sales quantity
++quantSales;
// Get sale information
int idSale = query1.value(0).toInt();
QString date = query1.value(1).toString();
QString collaborator = query1.value(2).toString();
double totalValue = query1.value(3).toDouble();
// Write elements
reportFile << ("Sale id: " + std::to_string(idSale) + "\n");
reportFile << ("Date: " + date.toStdString() + "\n");
reportFile << ("Collaborator: " + collaborator.toStdString() + "\n");
reportFile << ("Total sale: " + std::to_string(totalValue) + "\n");
// Build query
QSqlQuery query2;
query2.prepare("SELECT i.description, ps.quantity, ps.sale_price "
"FROM tb_products_sales ps JOIN tb_inventory i ON ps.id_product = i.id "
"WHERE ps.id_sale = " + QString::number(idSale) + " ORDER BY ps.id");
// Execute query
if(query2.exec())
{
// Write header
reportFile << "Products: \n";
while(query2.next())
{
// Get products information
QString product = query2.value(0).toString();
int quantity = query2.value(1).toInt();
double salePrice = query2.value(2).toDouble();
// Write products
reportFile << ("\tProduct: " + product.toStdString() + "\n");
reportFile << ("\tQuantity: " + std::to_string(quantity) + "\n");
reportFile << ("\tSale price: " + std::to_string(salePrice) + "\n\n");
}
}
reportFile << "\n";
}
}
dbConnection.close();
// Check sales quantity
if(!quantSales)
{
reportFile << "There are no sales";
}
// Close file
reportFile.close();
// Show message box
QMessageBox::information(this, "Information", "The report was created");
}
else
{
QMessageBox::warning(this, "Error", "Error to create file");
}
}
else
{
QMessageBox::warning(this, "Error", "Unable to connect database to export information");
}
}
void SalesManagementWindow::on_pushButton_removeSale_clicked()
{
// Get current line
int currentRow = ui->tableWidget_sales->currentRow();
// Check whether the sale is invalid
if(currentRow < 0)
{
QMessageBox::warning(this, "Error", "Select a sale first");
return;
}
// Remove sale
DatabaseConnection dbConnection;
if(dbConnection.open())
{
QMessageBox::StandardButton button = QMessageBox::question(this, "Remove",
"Do you want to remove this sale?",
QMessageBox::Yes | QMessageBox::No);
if(button == QMessageBox::Yes)
{
// Block signals to ignore items selections changed
ui->tableWidget_sales->blockSignals(true);
// Get sale id
int idSale = ui->tableWidget_sales->item(currentRow, 0)->text().toInt();
// Build query
QSqlQuery query;
query.prepare("DELETE FROM tb_products_sales WHERE id_sale = " + QString::number(idSale));
// Execute query
if(query.exec())
{
// Build query
query.prepare("DELETE FROM tb_sales WHERE id = " + QString::number(idSale));
// Execute query
if(query.exec())
{
// Update table widget
ShowAllSalesIntoTableWidget();
QMessageBox::information(this, "Success", "Sale '" +
QString::number(idSale) +
"' removed with success");
}
else
{
QMessageBox::warning(this, "Error", "Unable to remove sales from database");
}
}
else
{
QMessageBox::warning(this, "Error", "Unable to remove product sales from database");
}
}
dbConnection.close();
// Restore signals
ui->tableWidget_sales->blockSignals(false);
}
else
{
QMessageBox::warning(this, "Error", "Unable to connect database to remove sale");
}
}