-
Notifications
You must be signed in to change notification settings - Fork 5
/
borrowersearch.cpp
155 lines (119 loc) · 5.2 KB
/
borrowersearch.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
/*
* Copyright 2010 Kyle M Hall <kyle.m.hall@gmail.com>
*
* This file is part of Koha Offline Circulation.
*
* Koha Offline Circulation is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Koha Offline Circulation is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Koha Offline Circulation. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtGui>
#include <QtSql>
#include <QDebug>
#include "borrowersearch.h"
BorrowerSearch::BorrowerSearch(QWidget *parent) : QDialog(parent) {
setupUi(this);
setupActions();
}
BorrowerSearch::~BorrowerSearch() {
}
void BorrowerSearch::setupActions() {
connect(searchBorrowersButton, SIGNAL(clicked()),
this, SLOT(searchBorrowers()));
connect(nameLast, SIGNAL(returnPressed()),
this, SLOT(searchBorrowers()));
connect(nameFirst, SIGNAL(returnPressed()),
this, SLOT(searchBorrowers()));
connect(pushButtonOK, SIGNAL(clicked()),
this, SLOT(acceptBorrower()));
connect(resultsTable, SIGNAL(itemDoubleClicked(QTableWidgetItem *)),
this, SLOT(acceptBorrower()));
connect(pushButtonCancel, SIGNAL(clicked()),
this, SLOT(cancelSearch()));
}
void BorrowerSearch::searchBorrowers() {
QSettings settings;
QString borrowersDbFilePath = settings.value("borrowersDbFilePath").toString();
// If borrowersDbFilePath is not set,
// Default to APP_DIR/borrowers.db so the program does not crash.
if ( borrowersDbFilePath.isEmpty() ) {
borrowersDbFilePath = "borrowers.db";
}
clearResults();
QString lastnameSearch = nameLast->text();
QString firstnameSearch = nameFirst->text();
QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
db.setDatabaseName( borrowersDbFilePath );
if ( db.open() ) {
QString borrowersQuery = "SELECT * FROM borrowers WHERE firstname LIKE '"
+ firstnameSearch + "%' AND surname LIKE '" + lastnameSearch
+ "%' ORDER BY firstname ASC";
QSqlQuery query( borrowersQuery );
qDebug() << "Borrower Search SQL: " + borrowersQuery;
QSqlRecord record = query.record();
while (query.next()) {
QString lastname = query.value(record.indexOf("surname")).toString();
QString firstname = query.value(record.indexOf("firstname")).toString();
QString dateofbirth = query.value(record.indexOf("dateofbirth")).toString();
QString streetaddress = query.value(record.indexOf("address")).toString();
QString city = query.value(record.indexOf("city")).toString();
QString state = query.value(record.indexOf("state")).toString();
QString zipcode = query.value(record.indexOf("zipcode")).toString();
QString cardnumber = query.value(record.indexOf("cardnumber")).toString();
QString address = streetaddress + "\n" + city + ", " + state + "\n" + zipcode;
qDebug() << query.at() << ":" << lastname << "," << firstname;
addSearchResult( lastname, firstname, dateofbirth, address, cardnumber );
}
qDebug() << "Search Complete";
} else {
qDebug() << db.lastError();
qFatal( "Failed to connect." );
QMessageBox::warning(this, tr("Cannot Borrowers File"),
tr("Unable to open the file borrowers.db.\n"
"This file is needed in order to search for borrowers."
"Please create the borrowers.db file and place it in"
"the same directory as this program.\n\n"),
QMessageBox::Ok);
}
}
void BorrowerSearch::acceptBorrower() {
QList<QTableWidgetItem *> itemList = resultsTable->selectedItems();
if ( itemList.size() > 0 ) {
QTableWidgetItem *item = itemList.takeFirst();
QString cardnumber = item->text();
emit useBorrower( cardnumber );
}
this->hide();
}
void BorrowerSearch::cancelSearch() {
this->hide();
}
void BorrowerSearch::addSearchResult( const QString & lastname, const QString & firstname, const QString & dateofbirth, const QString & address, const QString & cardnumber ) {
QTableWidgetItem *cardnumberItem = new QTableWidgetItem( cardnumber );
QTableWidgetItem *lastnameItem = new QTableWidgetItem( lastname );
QTableWidgetItem *firstnameItem = new QTableWidgetItem( firstname );
QTableWidgetItem *dateofbirthItem = new QTableWidgetItem( dateofbirth );
QTableWidgetItem *addressItem = new QTableWidgetItem( address );
int row = resultsTable->rowCount();
resultsTable->insertRow(row);
resultsTable->setItem(row, COLUMN_CARDNUMBER, cardnumberItem);
resultsTable->setItem(row, COLUMN_LASTNAME, lastnameItem);
resultsTable->setItem(row, COLUMN_FIRSTNAME, firstnameItem);
resultsTable->setItem(row, COLUMN_DOB, dateofbirthItem);
resultsTable->setItem(row, COLUMN_ADDRESS, addressItem);
}
void BorrowerSearch::clearResults() {
int rowCount = resultsTable->rowCount();
for ( int row = 0; row < rowCount; row++ ) {
resultsTable->removeRow( 0 );
}
}