/
customers.cpp
202 lines (169 loc) · 5.88 KB
/
customers.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
#include "customers.h"
#include "ui_customers.h"
#include <QString>
#include <QStringList>
#include "dbmysql.h"
#include <QMessageBox>
#include "add_new_customer.h"
Customers::Customers(QWidget *parent) :
QWidget(parent),
ui(new Ui::Customers)
{
ui->setupUi(this);
}
Customers::~Customers()
{
delete ui;
}
void Customers::getAllCustomerData()
{
DbMysql* d = DbMysql::getInstance();
/*
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("testQt");
db.setUserName("root");
db.setPort(3306);
db.setPassword("");
*/
if (!d->getConnection().open()){
qDebug() << "Failed to connect to root mysql admin";
qDebug() << d->getConnection().lastError().text();
}
else {
/*
DbMysql* db1 = DbMysql::getInstance();
if(!db1->getDb().open()){
return;
}
else {
*/
QSqlQuery query( "SELECT * FROM customers order by customer_id desc;",d->getConnection() );
if( !query.isActive() )
{
QMessageBox msgBox;
msgBox.critical(this,"Error","Failed to execute query.");
qDebug()<<"Failed to execute query. select customers";
qDebug()<<query.lastError().text();
return;
}
else{
int row = 0;
ui->customersDetailsTable->setRowCount(query.size());
while( query.next() )
{
//QStringList ss = line.split('\t');
//if(ui->customersDetailsTable->rowCount() < row + 1)
//if(ui->tableWidget->columnCount() < ss.size())
// ui->tableWidget->setColumnCount( ss.size() );
int columnSize = ui->customersDetailsTable->columnCount();
//qDebug()<<"Column Size:"<<ui->customersDetailsTable->columnCount();
for( int column = 0; column < columnSize; column++)
{
QTableWidgetItem *newItem = new QTableWidgetItem();
newItem->setText(query.value(column).toString());
//qDebug()<<"Data:"<<query.value(column).toString();
ui->customersDetailsTable->setItem(row, column, newItem);
}
row++;
//int fff = query.value( 0 ).toInt();
}
//ui->customersDetailsTable->resizeColumnsToContents();
ui->customersDetailsTable->horizontalHeader()->setHighlightSections(false);
//ui->customersDetailsTable->horizontalHeader()->setStyleSheet("QHeaderView::section { background-color:#0B2161;color:#fff; }");
}
d->getConnection().close();
//}
}
}
/*
void Customers::on_toolButton_triggered(QAction *arg1)
{
}
*/
void Customers::on_addCustomer_clicked()
{
qDebug()<<"button clicked";
add_new_customer* addc = new add_new_customer();
addc->setCustomerParent(this);
addc->show();
}
void Customers::on_editCustomer_clicked()
{
if(ui->customersDetailsTable->selectedItems().size() > 0)
{
QString customer_id = ui->customersDetailsTable->selectedItems().at(0)->text();
add_new_customer* adc = new add_new_customer();
adc->editCustomer(customer_id);
adc->show();
}
else
{
QMessageBox msgBox;
msgBox.information(this,"Edit Customer Details","Please select a customer before edit.");
}
}
void Customers::on_deleteCustomer_clicked()
{
QMessageBox::StandardButton reply;
QMessageBox msgBox;
reply = QMessageBox::question(this, "Delete Customer", "Are you want to delete customer?",QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
qDebug() << "Yes was clicked";
DbMysql* d = DbMysql::getInstance();
QString customer_id = ui->customersDetailsTable->selectedItems().at(0)->text();
if (!d->getConnection().open()) {
msgBox.critical(this,"Error","Failed to connect database.");
}
else {
QSqlQuery query("delete from customers where customer_id="+customer_id+";",d->getConnection());
if( !query.exec() )
{
msgBox.critical(this,"Error","Failed to execute query.");
qDebug()<<query.lastError().text();
return;
}
else{
msgBox.information(this,"Information","Customer Deleted.");
this->getAllCustomerData();
}
}
}
}
void Customers::on_searchBtn_clicked()
{
DbMysql* d = DbMysql::getInstance();
if (!d->getConnection().open()){
qDebug() << "Failed to connect to root mysql admin";
qDebug() << d->getConnection().lastError().text();
}
else {
QSqlQuery query( "SELECT * FROM customers where customer_name like '%"+ui->customerName->text()+"%';",d->getConnection() );
if( !query.isActive() )
{
QMessageBox msgBox;
msgBox.critical(this,"Error","Failed to execute query.");
qDebug()<<"Failed to execute query. select customers";
qDebug()<<query.lastError().text();
return;
}
else{
int row = 0;
ui->customersDetailsTable->setRowCount(query.size());
while( query.next() )
{
int columnSize = ui->customersDetailsTable->columnCount();
for( int column = 0; column < columnSize; column++)
{
QTableWidgetItem *newItem = new QTableWidgetItem();
newItem->setText(query.value(column).toString());
ui->customersDetailsTable->setItem(row, column, newItem);
}
row++;
}
ui->customersDetailsTable->resizeColumnsToContents();
ui->customersDetailsTable->horizontalHeader()->setHighlightSections(false);
}
d->getConnection().close();
}
}