-
Notifications
You must be signed in to change notification settings - Fork 0
/
formoperator.cpp
290 lines (235 loc) · 8.76 KB
/
formoperator.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
#include "formoperator.h"
#include "ui_formoperator.h"
#include <QFormLayout>
#include <QLabel>
#include <QPushButton>
#include <QMessageBox>
#include <helper.h>
#include <matchrow.h>
#include <mainwindow.h>
#include <jalali.h>
#include <musers.h>
#include <mmatches.h>
#include <mobjects.h>
FormOperator::FormOperator(QWidget *parent) :
QWidget(parent),
ui(new Ui::FormOperator)
{
ui->setupUi(this);
viewer = new ViewerForm((MainWindow*) parent);
connect(ui->eUser, SIGNAL(select()), this, SLOT(selectUser()));
connect(ui->eUser, SIGNAL(cancel()), this, SLOT(cancelUser()));
if (! options()["Match"].toBool())
itemsQuery = MObjects::getObjectsQuery();
else
itemsQuery = "select cid, clabel, substr(ctitle, 0, 80) as ctitle from ("+ MObjects::getObjectsQuery() +" union "
"select 'x'||matches.id as cid, categories.title as clabel, matches.title as ctitle from matches inner join categories on matches.category_id = categories.id) as _t";
connect(ui->eObject, SIGNAL(select()), this, SLOT(selectObject()));
connect(ui->eObject, SIGNAL(cancel()), this, SLOT(cancelObject()));
cancelUser();
}
FormOperator::~FormOperator()
{
delete ui;
}
void FormOperator::cancelUser()
{
ui->eUser->setQuery(MUsers::getUsersQuery());
ui->gDeliver->setEnabled(false);
ui->gReceive->setEnabled(false);
cancelObject();
ui->eObject->setText("");
ui->lFine->setText("0");
ui->lDebt->setText("0");
ui->sPayment->setValue(0);
ui->sDiscount->setValue(0);
// clean lObjects
QLayoutItem *child;
while ((child = ui->lObjects->layout()->takeAt(0)) != 0)
delete child->widget();
}
void FormOperator::cancelObject()
{
ui->eObject->setQuery(itemsQuery);
ui->bDeliver->setEnabled(false);
ui->bPreview->setEnabled(false);
ui->gMatchOpt->setVisible(false);
ui->lMatchError->setText("");
}
void FormOperator::selectUser()
{
if (! ui->eUser->value().isEmpty())
{
bool receive = false;
QStringList matchObjects;
QSqlQuery qry;
MatchRow* row;
int fine;
// show delivered matches
qry.exec(QString("select match_id, matches.title, matches.object_id, belongs.label, answers.delivered_at from answers inner join matches on answers.match_id = matches.id left join belongs on matches.object_id = belongs.object_id where user_id = %1 and received_at is null and answers.delivered_at > (select started_at from library)").arg(ui->eUser->value()));
while (qry.next()) {
fine = qry.value(2).toString().isEmpty() ? 0 : MObjects::getFine(ui->eUser->value(), qry.value(2).toString());
row = new MatchRow(qry.value(1).toString(), qry.value(3).toString(), QStringList() << "" << tr("Received"), qry.value(0).toString(), qry.value(2).toString(), fine, qry.value(4).toDate(), QDate(), ui->gObjects);
ui->lObjects->layout()->addWidget(row);
matchObjects << qry.value(2).toString();
connect(row, SIGNAL(changed()), this, SLOT(refreshFine()));
receive = true;
}
// show delivered objects
qry.exec(QString("select borrows.object_id, objects.title, belongs.label, borrows.delivered_at, borrows.renewed_at from borrows inner join objects on borrows.object_id = objects.id inner join belongs on borrows.object_id = belongs.object_id where user_id = %1 and received_at is null").arg(ui->eUser->value()));
while(qry.next()) {
if (matchObjects.contains(qry.value(0).toString())) continue;
fine = MObjects::getFine(ui->eUser->value(), qry.value(0).toString());
row = new MatchRow(qry.value(1).toString(), qry.value(2).toString(), QStringList() << "" << tr("Received") << tr("Renewed"), "", qry.value(0).toString(), fine, qry.value(3).toDate(), qry.value(4).toDate(), ui->gObjects);
ui->lObjects->layout()->addWidget(row);
connect(row, SIGNAL(changed()), this, SLOT(refreshFine()));
receive = true;
}
// space filler
ui->lObjects->layout()->addWidget(new QWidget);
// numbers
int debt = MObjects::getDebt(ui->eUser->value());
ui->lDebt->setText(QString("%1").arg(debt));
refreshFine();
ui->gDeliver->setEnabled(true);
if (receive)
ui->gReceive->setEnabled(true);
ui->sDiscount->setEnabled(App::hasAccess("manager"));
ui->eObject->setFocus();
}
}
void FormOperator::refreshFine()
{
int fine = 0;
MatchRow* row;
for (int i = 0; i < ui->lObjects->layout()->count() - 1; i++)
{
row = (MatchRow*)(ui->lObjects->layout()->itemAt(i)->widget());
if (! row->getState().isEmpty())
fine += row->fine;
}
ui->lFine->setText(QString("%1").arg(fine));
}
void FormOperator::selectObject()
{
if (! ui->eObject->value().isEmpty()) {
ui->bDeliver->setEnabled(true);
if (options()["Match"].toBool()) {
if (ui->eObject->value().startsWith("x")) {
// pure matches
matchId = ui->eObject->value().mid(1);
} else {
// matches that linked with objects
QSqlQuery qry;
qry.exec(QString("select id from matches where object_id = %1").arg(ui->eObject->value()));
if (qry.next())
matchId = qry.value(0).toString();
}
}
if (! matchId.isEmpty()) {
ui->gMatchOpt->setVisible(true);
ui->lMatchError->setText(MMatches::isDeliverable(ui->eUser->value(), matchId));
ui->lMatchError->setVisible(!ui->lMatchError->text().isEmpty());
ui->bPreview->setEnabled(true);
prepareViewer();
}
ui->bDeliver->setFocus();
}
}
void FormOperator::on_bDeliver_clicked()
{
if (! ui->eUser->value().isEmpty() && ! ui->eObject->value().isEmpty())
{
QString msg;
bool match = (!matchId.isEmpty()) && (ui->cDeliverMatch->isChecked());
if (match)
msg = MMatches::isDeliverable(ui->eUser->value(), matchId);
if (msg.isEmpty() && !ui->eObject->value().startsWith("x"))
msg = MObjects::deliver(ui->eUser->value(), ui->eObject->value());
if (match && msg.isEmpty()) {
msg = MMatches::deliver(ui->eUser->value(), matchId);
if (msg.isEmpty() && ui->cPrint->isChecked())
viewer->on_bPrint_clicked();
}
if (! msg.isEmpty()) {
QMessageBox::critical(0, QObject::tr("Ketabkhaane"), msg);
return;
}
cancelUser();
selectUser();
}
}
void FormOperator::on_bPreview_clicked()
{
viewer->exec();
}
void FormOperator::prepareViewer()
{
// don't perpare match again
static QString prepared;
if (prepared == matchId)
return;
StrMap match;
QList<StrPair> questions, asks;
MMatches::get(matchId, match, questions);
match["user"] = ui->eUser->value();
QSqlQuery qry;
// select random questions
int qs = 4;
qry.exec("select questions from ageclasses where id = "+ match["ageclass"].toString());
if (qry.next())
qs = qry.value(0).toInt();
if (questions.count() < qs)
qs = questions.count();
int* selected = new int[qs];
for (int i = 0; i < qs;)
{
int n = rand() % questions.count();
bool exists = false;
for (int j = 0; j < i; j++)
if (selected[j] == n) { exists = true; break; }
if (! exists)
{
asks.append(qMakePair(questions[n].first, QString("")));
selected[i] = n; i++;
}
}
viewer->showMatch(match, asks);
prepared = ui->eObject->value();
}
void FormOperator::focus()
{
ui->eUser->setFocus();
}
void FormOperator::on_bReceive_clicked()
{
QString msg;
// payment
msg = MObjects::charge(ui->eUser->value(), ui->lFine->text().toInt(), ui->sDiscount->value(), ui->sPayment->value());
if (! msg.isEmpty())
{
QMessageBox::critical(0, QObject::tr("Ketabkhaane"), msg);
return;
}
// last widget is space filler
MatchRow* row;
for (int i = 0; i < ui->lObjects->layout()->count() - 1; i++)
{
row = (MatchRow*)(ui->lObjects->layout()->itemAt(i)->widget());
if (row->getState() == tr("Received"))
{
if (! row->objectId.isEmpty())
MObjects::receive(ui->eUser->value(), row->objectId);
if (! row->matchId.isEmpty())
MMatches::receive(ui->eUser->value(), row->matchId);
}
else if (row->getState() == tr("Renewed"))
{
if (! row->objectId.isEmpty())
MObjects::renew(ui->eUser->value(), row->objectId);
}
}
cancelUser();
ui->eUser->setText("");
ui->eUser->setFocus();
}