/
routetool.cpp
587 lines (493 loc) · 26.7 KB
/
routetool.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#include "routetool.h"
#include "ui_routetool.h"
#include <QDebug>
#include <QApplication>
#include "form.h"
#include <QMessageBox>
#define vvimDebug()\
qDebug() << "[" << Q_FUNC_INFO << "]"
#define EndOfLine "\r\n"
#include "kiesgedaneophaling.h"
RouteTool::RouteTool(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::RouteTool)
{
ui->setupUi(this);
connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(close()));
connect(ui->nieuweAanmeldingMenuButton, SIGNAL(triggered()), this, SLOT(showNieuweAanmelding()));
connect(ui->nieuwOphaalpuntMenuButton, SIGNAL(triggered()), this, SLOT(showNieuwOphaalpunt()));
connect(ui->configurationMenuButton, SIGNAL(triggered()), this, SLOT(showConfiguration()));
connect(ui->kiesOphaalpuntenMenuButton, SIGNAL(triggered()), this, SLOT(showKiesOphaalpunten()));
connect(ui->nieuweLeveringMenuButton, SIGNAL(triggered()), this, SLOT(showNieuweLevering()));
connect(ui->bellijstMenuButton, SIGNAL(triggered()), this, SLOT(showBellijst()));
connect(ui->toonOphaalpuntenMenuButton, SIGNAL(triggered()), this, SLOT(showOphaalpuntenWidget()));
connect(ui->effectiefOpgehaaldeHoeveelhedenMenuButton, SIGNAL(triggered()), this, SLOT(showEffectiefOpgehaaldeHoeveelheden()));
connect(ui->actionAnnuleer_ingegeven_ophaalronde, SIGNAL(triggered()), this, SLOT(showAnnuleerIngegevenOphaalronde()));
connect(ui->actionExporteer_Historiek, SIGNAL(triggered()), this, SLOT(showExportCollectionHistory()));
connect(ui->actionExporteer_ophaalpunten, SIGNAL(triggered()), this, SLOT(showExportGegevensOphaalpunten()));
connect(ui->actionOphaalronde_aanpassen, SIGNAL(triggered()), this, SLOT(showOphaalrondeAanpassen()));
m_pForm = new Form(this);
setCentralWidget(m_pForm);
connect(&kiesOphaalpuntenWidget, SIGNAL(aanmelding_for_route(QList<SOphaalpunt> *)), m_pForm, SLOT(add_aanmeldingen(QList<SOphaalpunt>*)));
connect(this, SIGNAL(aanmelding_for_route(QList<SOphaalpunt> *)), m_pForm, SLOT(add_aanmeldingen(QList<SOphaalpunt>*)));
connect(&leveringWidget, SIGNAL(levering_for_route(SLevering)), m_pForm, SLOT(add_levering(SLevering)));
connect(&configurationWidget, SIGNAL(configurationChanged()), m_pForm, SLOT(setTotalWeightTotalVolume()));
// when table OPHAALPUNTEN is changed, we should reload all the completers. Yes, _all_ of them...
/// 1] for the mainform
connect(&ophaalpuntenWidget, SIGNAL(contentsOfDatabaseChanged()), m_pForm, SLOT(reloadCompleter()));
connect(&contactListWidget, SIGNAL(contentsOfDatabaseChanged()), m_pForm, SLOT(reloadCompleter()));
connect(&nieuwOphaalpuntWidget, SIGNAL(infoChanged()), m_pForm, SLOT(reloadCompleter()));
/// 2] for the nieuweAanmeldingWidget
connect(&ophaalpuntenWidget, SIGNAL(contentsOfDatabaseChanged()), &nieuweAanmeldingWidget, SLOT(loadOphaalpunten()));
connect(&contactListWidget, SIGNAL(contentsOfDatabaseChanged()), &nieuweAanmeldingWidget, SLOT(loadOphaalpunten()));
connect(&nieuwOphaalpuntWidget, SIGNAL(infoChanged()), &nieuweAanmeldingWidget, SLOT(loadOphaalpunten()));
/// 3] for the ophaalpuntenWidget
connect(&ophaalpuntenWidget, SIGNAL(contentsOfDatabaseChanged()), &ophaalpuntenWidget, SLOT(loadOphaalpunten()));
connect(&contactListWidget, SIGNAL(contentsOfDatabaseChanged()), &ophaalpuntenWidget, SLOT(loadOphaalpunten()));
connect(&nieuwOphaalpuntWidget, SIGNAL(infoChanged()), &ophaalpuntenWidget, SLOT(loadOphaalpunten()));
/// 3] for the leveringWidget
connect(&ophaalpuntenWidget, SIGNAL(contentsOfDatabaseChanged()), &leveringWidget, SLOT(loadOphaalpunten()));
connect(&contactListWidget, SIGNAL(contentsOfDatabaseChanged()), &leveringWidget, SLOT(loadOphaalpunten()));
connect(&nieuwOphaalpuntWidget, SIGNAL(infoChanged()), &leveringWidget, SLOT(loadOphaalpunten()));
connect(m_pForm, SIGNAL(showOphaalpuntInfo(int)), this, SLOT(showOphaalpuntInfo(int)));
connect(&nieuwOphaalpuntWidget,SIGNAL(nieuweAanmelding(int)),&nieuweAanmeldingWidget,SLOT(aanmeldingVoorOphaalpunt(int)));
/// call cleanMarkersAndOpenOldRoute() when TransportationListWriter::print() is done and Boolean is set commit 89741718d8520aab7ddb21a1a9bc2b90b7f5ec2c https://github.com/vvim/routetool/commit/89741718d8520aab7ddb21a1a9bc2b90b7f5ec2c
connect(m_pForm, SIGNAL(signalCleanMarkersAndOpenOldRoute()), this, SLOT(cleanMarkersAndOpenOldRoute()));
}
RouteTool::~RouteTool()
{
vvimDebug() << "start to deconstruct RouteTool()";
delete m_pForm;
delete ui;
vvimDebug() << "RouteTool() deconstructed";
}
void RouteTool::showNieuweAanmelding()
{
nieuweAanmeldingWidget.resetValues();
nieuweAanmeldingWidget.show();
}
void RouteTool::showNieuwOphaalpunt()
{
nieuwOphaalpuntWidget.showAanmeldingAndHistoriekButton(false);
nieuwOphaalpuntWidget.createNewOphaalpunt();
// this functions triggers "show()" AND resets the information in the dialogbox at the same time
}
void RouteTool::showConfiguration()
{
configurationWidget.setOriginalValues();
configurationWidget.show();
// here the information stays the same, so it shouldn't matter
}
void RouteTool::showKiesOphaalpunten()
{
kiesOphaalpuntenWidget.initialise();
kiesOphaalpuntenWidget.show();
// always re-initialise the list of 'aanmeldingen' as the user might have given new input since last time
}
void RouteTool::showNieuweLevering()
{
leveringWidget.resetValues();
leveringWidget.show();
}
void RouteTool::showBellijst()
{
contactListWidget.initialise();
contactListWidget.show();
}
void RouteTool::showOphaalpuntenWidget()
{
ophaalpuntenWidget.show();
}
void RouteTool::showEffectiefOpgehaaldeHoeveelheden()
{
vvimDebug() << "user clicked on showEffectiefOpgehaaldeHoeveelheden()";
KiesGedaneOphaling *kgo = new KiesGedaneOphaling(Confirming);
switch(kgo->initialise())
{
case -1 :
QMessageBox::critical(this, tr("Databank error"), tr("We konden de onbevestigde ophaalrondes niet opzoeken in de databank."));
return;
break;
case 0 :
QMessageBox::information(this, tr("Geen onbevestigde ophaalrondes gevonden"), tr("Er werden geen onbevestigde ophaalrondes gevonden."));
return;
break;
}
kgo->show();
/**
2. nieuw venster met daarin de QSqlWidgetMapper om elke locatie apart te bevestigen, in volgorde!
=> select * from aanmelding where ophaalronde_datum = GEKOZEN_OPHAALRONDE_DATUM
3. dubbelen verwijderen? => zie select in 1.
** insert into TABLE OPHAALHISTORIEK
ging dat ok? =>
** delete from TABLE AANMELDING
??? wat als we een bevestigde ophaling toch nog willen wijzigen ???
??? wat als we een geplande ophaling willen annuleren ???
--> zie showAnnuleerIngegevenOphaalronde()
**/
}
void RouteTool::showAnnuleerIngegevenOphaalronde()
{
// inherit KiesGedaneOphaling , override "accept" met UPDATE aanmelding SET ophalings_datum = NULL, volgorde = NULL WHERE ophalings_datum = :ophalingsdatum;
vvimDebug() << "user clicked on showAnnuleerIngegevenOphaalronde()";
KiesGedaneOphaling *kgo = new KiesGedaneOphaling(Deleting);
switch(kgo->initialise())
{
case -1 :
QMessageBox::critical(this, tr("Databank error"), tr("We konden de onbevestigde ophaalrondes niet opzoeken in de databank."));
return;
break;
case 0 :
QMessageBox::information(this, tr("Geen onbevestigde ophaalrondes gevonden"), tr("Er werden geen onbevestigde ophaalrondes gevonden."));
return;
break;
}
kgo->show();
}
void RouteTool::showOphaalrondeAanpassen()
{
vvimDebug() << "user clicked on showOphaalrondeAanpassen()";
vvimDebug() << "[0] FIRST, CLEAN CURRENT ROUTE. Save it ???";
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Huidige route opslaan?"));
msgBox.setText(tr("Voor je de ingegeven route opent, wil je de huidige opslaan?"));
QAbstractButton *myYesButton = msgBox.addButton(tr("Huidige route opslaan"), QMessageBox::YesRole);
QAbstractButton *myNoButton = msgBox.addButton(tr("Huidige route wissen"), QMessageBox::NoRole);
QAbstractButton *myCancelButton = msgBox.addButton(tr("Annuleren"), QMessageBox::RejectRole);
msgBox.setIcon(QMessageBox::Question);
msgBox.exec();
if(msgBox.clickedButton() == myCancelButton)
{
// works also when the user presses ESC or simply closes the QMessageBox
vvimDebug() << "user pressed CANCEL or ESC";
return;
}
if(msgBox.clickedButton() == myYesButton)
{
// open "save route"
vvimDebug() << "user pressed YES" << "save the current route before continuing";
// EXTRA BOOLEAN VOOR FORM.CPP, zie on_pbTransportationList. AFTER_VERVOERSLIJST_CONTINUE_TO_OPEN
m_pForm->afterTransportationListCleanMarkersAndOpenRoute();
m_pForm->on_pbTransportationList_clicked();
return;
}
if(msgBox.clickedButton() == myNoButton)
{
// do not save previous work, just open old route
vvimDebug() << "user pressed NO" << "do not save the current route, just open old route";
cleanMarkersAndOpenOldRoute();
}
}
void RouteTool::cleanMarkersAndOpenOldRoute()
{
vvimDebug() << "now we erase the current route and show the KGO to choose a previously saved route";
m_pForm->removeAllMarkers();
// inherit KiesGedaneOphaling
KiesGedaneOphaling *kgo = new KiesGedaneOphaling(Editing);
switch(kgo->initialise())
{
case -1 :
QMessageBox::critical(this, tr("Databank error"), tr("We konden de onbevestigde ophaalrondes niet opzoeken in de databank."));
return;
break;
case 0 :
QMessageBox::information(this, tr("Geen onbevestigde ophaalrondes gevonden"), tr("Er werden geen onbevestigde ophaalrondes gevonden."));
return;
break;
}
kgo->show();
connect(kgo, SIGNAL(showPlannedRoute(QList<SOphaalpunt> *, QDate)), m_pForm, SLOT(showPlannedRoute(QList<SOphaalpunt>*, QDate)));
}
void RouteTool::showExportCollectionHistory()
{
ExportCollectionHistory *dialogboxToExportHistoryOfCollectedQuantities = new ExportCollectionHistory();
dialogboxToExportHistoryOfCollectedQuantities->show();
}
void RouteTool::showOphaalpuntInfo(int ophaalpunt_id)
{
/** signal gets emited from Form::askMainProgramToShowOphaalpuntInfo(int ophaalpunt_id)
reason for being: when the user clicks on "on_showOphaalpunten_clicked()", the map
displays all known locations on the map (except those who were already in the route).
The locations with a known 'aanmelding' will be colored blue, the other ones are yellow.
Instead of making the distinction Blue/Yellow in the Javascript of "on_showOphaalpunten_clicked()",
we will make the distinction here. It is easier to query OphaalpuntenWidget than to write
an extensive Javascript.
**/
if(ophaalpuntenWidget.OphaalpuntHasAanmeldingPresent(ophaalpunt_id))
{
// YES, ophaalpunt has an Aanmelding present. But which one? => query database
QString SQLquery = QString("SELECT ophaalpunten.naam, aanmelding.kg_kurk, aanmelding.kg_kaarsresten, aanmelding.zakken_kurk, aanmelding.zakken_kaarsresten,"
" aanmelding.id, ophaalpunten.id, aanmelding.opmerkingen,"
" ophaalpunten.straat, ophaalpunten.nr, ophaalpunten.bus, ophaalpunten.postcode, ophaalpunten.plaats, ophaalpunten.land, aanmelding.datum "
" FROM aanmelding, ophaalpunten"
" WHERE ophaalpunten.id = aanmelding.ophaalpunt AND ophaalpunten.id = %1 AND aanmelding.ophaalronde_datum is NULL").arg(ophaalpunt_id);
QSqlQuery query;
query.prepare("SELECT ophaalpunten.naam, aanmelding.kg_kurk, aanmelding.kg_kaarsresten, aanmelding.zakken_kurk, aanmelding.zakken_kaarsresten,"
" aanmelding.id, ophaalpunten.id, aanmelding.opmerkingen,"
" ophaalpunten.straat, ophaalpunten.nr, ophaalpunten.bus, ophaalpunten.postcode, ophaalpunten.plaats, ophaalpunten.land, aanmelding.datum "
" FROM aanmelding, ophaalpunten"
" WHERE ophaalpunten.id = aanmelding.ophaalpunt AND ophaalpunten.id = :ophaalpuntid AND aanmelding.ophaalronde_datum is NULL");
query.bindValue(":ophaalpuntid",ophaalpunt_id);
if(!query.exec())
{
if(!reConnectToDatabase(query.lastError(), SQLquery, QString("[%1]").arg(Q_FUNC_INFO)))
{
vvimDebug() << "unable to reconnect to DB, halting";
QMessageBox::information(this, tr("Fout bij verbinding met de databank in functie %1").arg(Q_FUNC_INFO), tr("De databank kon niet geraadpleegd worden, probeer opnieuw. Als deze fout zich blijft voordoen, stuur het logbestand naar Wim of neem contact op met de systeembeheerder."));
return;
}
vvimDebug() << "reconnected to DB, will try query again";
QSqlQuery query2;
query2.prepare("SELECT ophaalpunten.naam, aanmelding.kg_kurk, aanmelding.kg_kaarsresten, aanmelding.zakken_kurk, aanmelding.zakken_kaarsresten,"
" aanmelding.id, ophaalpunten.id, aanmelding.opmerkingen,"
" ophaalpunten.straat, ophaalpunten.nr, ophaalpunten.bus, ophaalpunten.postcode, ophaalpunten.plaats, ophaalpunten.land, aanmelding.datum "
" FROM aanmelding, ophaalpunten"
" WHERE ophaalpunten.id = aanmelding.ophaalpunt AND ophaalpunten.id = :ophaalpuntid AND aanmelding.ophaalronde_datum is NULL");
query = query2;
query.bindValue(":ophaalpuntid",ophaalpunt_id);
if(!query.exec())
{
vvimDebug() << "ophaalpuntid = " << ophaalpunt_id << "-" << "something went wrong with checking for an existing aanmelding" << query.lastError();
QMessageBox::information(this, tr("Fout bij verbinding met heruitvoeren query in functie %1").arg(Q_FUNC_INFO), tr("De query kon niet uitgevoerd worden na reconnectie met databank, probeer opnieuw. Als deze fout zich blijft voordoen, stuur het logbestand naar Wim of neem contact op met de systeembeheerder."));
return;
}
}
if(query.size() != 1)
vvimDebug() << "[ERROR] size of result set should be 1 but is " << query.size() << "how come? There should only be 1 aanmelding???";
if (query.next())
{
QString ophaalpunt_naam = query.value(0).toString();
double kg_kurk = query.value(1).toDouble();
double kg_kaars = query.value(2).toDouble();
double zakken_kurk = query.value(3).toDouble();
double zakken_kaars = query.value(4).toDouble();
int aanmelding_id = query.value(5).toInt();
QString message_for_messagebox = tr("Ophaalpunt %5 heeft %1 kg kurk aangemeld (%2 zakken) en %3 kg kaarsresten (%4 zakken).\n\nWilt u dit ophaalpunt toevoegen aan de huidige route of het informatiescherm van dit ophaalpunt zien?").arg(kg_kurk).arg(zakken_kurk).arg(kg_kaars).arg(zakken_kaars).arg(ophaalpunt_naam);
vvimDebug() << "aanmelding:" << message_for_messagebox;
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Dit ophaalpunt heeft een aanmelding"));
msgBox.setText(message_for_messagebox);
QAbstractButton *myYesButton = msgBox.addButton(tr("Toevoegen aan route"), QMessageBox::YesRole);
QAbstractButton *myNoButton = msgBox.addButton(tr("Toon info ophaalpunt"), QMessageBox::NoRole);
QAbstractButton *myCancelButton = msgBox.addButton(tr("Annuleren"), QMessageBox::RejectRole);
// msgBox.setStandardButtons(QMessageBox::Cancel); -> how to translate buttons to Dutch?
msgBox.setIcon(QMessageBox::Question);
msgBox.exec();
if(msgBox.clickedButton() == myYesButton)
{
vvimDebug() << "voeg ophaalpunt" << ophaalpunt_id << "toe aan route";
QString opmerkingen = query.value(7).toString(); // opmerkingen (uit table aanmelding)
QString ophaalpunt_straat = query.value(8).toString();
QString huisnr = query.value(9).toString();
QString busnr = query.value(10).toString();
QString postcode = query.value(11).toString();
QString plaats = query.value(12).toString();
QString land = query.value(13).toString();
QList<SOphaalpunt> *listOfAanmeldingen = new QList<SOphaalpunt>();
SOphaalpunt _ophaalpunt(
ophaalpunt_naam,
ophaalpunt_straat,
huisnr,
busnr,
postcode,
plaats,
land,
kg_kurk,
kg_kaars,
zakken_kurk,
zakken_kaars,
aanmelding_id,
ophaalpunt_id,
opmerkingen
);
listOfAanmeldingen->append(_ophaalpunt);
emit aanmelding_for_route(listOfAanmeldingen);
return;
}
if(msgBox.clickedButton() == myCancelButton)
{
vvimDebug() << "user canceled action";
return;
}
}
else
{
vvimDebug() << "ERROR, something went wrong with querying for the aanmelding??";
vvimDebug() << "will just show info on Ophaalpunt";
}
}
vvimDebug() << "user agreed, or there is no Messagebox (because the ophaalpunt has no aanmelding_present, or maybe a DB-error occured in the final request?";
// so we arrive at this point in 2 instances: or there is no AanmeldingPresent, or there is but the user clicked on "ShowOphaalpuntInfo" in during the messagebox
nieuwOphaalpuntWidget.showOphaalpunt(ophaalpunt_id);
nieuwOphaalpuntWidget.showAanmeldingAndHistoriekButton(true);
nieuwOphaalpuntWidget.setWindowTitle("info over ophaalpunt");
}
void RouteTool::showExportGegevensOphaalpunten()
{
vvimDebug() << "export data of all locations";
vvimDebug() << "[1] show QFileDialog";
QString filters("CSVbestanden (*.csv);;Tekstbestanden (*.txt);;Microsoft Excel (*.xls *.xlsx);;All files (*.*)");
QString defaultFilter("Microsoft Excel (*.xls *.xlsx)");
QString filename = QFileDialog::getSaveFileName(0, tr("Exporteer lijst ophaalpunten naar..."), QDir::currentPath(), filters, &defaultFilter);
if(filename.count() < 1)
{
vvimDebug() << "... no filename given, the user must have pressed 'cancel' or 'close' in the FileDialogBox. Let's return to the Export Collection History Dialog Box like nothing happened...";
// no need to show QMessageBox
return; // RETURN FALSE
}
// would be better with "QFileDialog.SetDefaultSuffix()", but I simply don't get it: http://stackoverflow.com/questions/1953631/qfiledialog-adding-extension-automatically-when-saving-file
// so, a little hack:
if((filename.right(4) == ".xls") || (filename.right(5) == ".xlsx") || (filename.right(4) == ".txt") || (filename.right(4) == ".csv") )
vvimDebug() << "...file-extension is alright:" << filename.right(4);
else
{
vvimDebug() << "... no valid extension given, so we will add '.csv'";
filename.append(".csv");
}
vvimDebug() << "...we will save it as" << filename;
/// 2. now that we finally have this filename-stuff settled, let's go down to business!
vvimDebug() << "[2] open file";
QFile f( filename );
if(!f.open(QFile::WriteOnly | QFile::Truncate)) // 'truncate' == overwrite
{
vvimDebug() << "... FAILED: We could not opened file" << filename << "show messagebox to user and return to Export Collection History Dialog Box";
QMessageBox::information(this, tr("Kan bestand niet openen"), tr("Bestand %1 kan niet geopend worden, probeer opnieuw. Als deze fout zich blijft voordoen, stuur het logbestand naar Wim of neem contact op met de systeembeheerder.").arg(filename));
return; // RETURN FALSE
}
QTextStream data( &f );
//data.setCodec("Latin-1");
data.setCodec("UTF-8");
data.setGenerateByteOrderMark(true);
QSqlQuery query;
QString SQLquery = "SELECT * FROM ophaalpunten ORDER BY postcode";
query.prepare(SQLquery);
data << tr("\"Export van de ophaalpunten uit de databank\"")+EndOfLine;
data << EndOfLine;
if(!query.exec())
{
if(!reConnectToDatabase(query.lastError(), SQLquery, QString("[%1]").arg(Q_FUNC_INFO)))
{
vvimDebug() << "... FAILED: We could not execute the query " << SQLquery;
vvimDebug() << "... show messagebox to user and cancel function";
QMessageBox::information(this, tr("Fout bij verbinding met de databank").arg(Q_FUNC_INFO), tr("De databank kon niet geraadpleegd worden, probeer opnieuw. Als deze fout zich blijft voordoen, stuur het logbestand naar Wim of neem contact op met de systeembeheerder."));
return; // RETURN FALSE
}
else
{
query.prepare(SQLquery);
if(!query.exec())
{
vvimDebug() << "FAILED: We could not execute the query " << SQLquery;
vvimDebug() << "show messagebox to user and return to Export Collection History Dialog Box";
QMessageBox::information(this, tr("Fout bij verbinding met heruitvoeren query ").arg(Q_FUNC_INFO), tr("De query kon niet uitgevoerd worden na reconnectie met databank, probeer opnieuw. Als deze fout zich blijft voordoen, stuur het logbestand naar Wim of neem contact op met de systeembeheerder."));
return; // RETURN FALSE
}
}
}
QStringList strList;
int records = 0;
int columns_in_queryresult = query.record().count();
vvimDebug() << "[3] get all possibilities for contactpreference, attestation, language, ... and put them in QMaps";
QMap <int,QString> contact_preference = getQMapFromSQLTable("SELECT id, medium FROM contacteren");
QMap <int,QString> attest_frequencies = getQMapFromSQLTable("SELECT id, frequentie FROM frequentie");
QMap <int,QString> location_type = getQMapFromSQLTable("SELECT code, soort FROM soort_ophaalpunt");
QMap <int,QString> intercommunales = getQMapFromSQLTable("SELECT id, naam_intercommunale FROM intercommunales");
QMap <int,QString> languages = getQMapFromSQLTable("SELECT id, taal FROM talen");
vvimDebug() << "[4] fill in columnheaders";
vvimDebug() << "... dit mag 'dynamischer', nu is alles hardcoded, niet flexibel";
strList << tr("\"Ophaalpunt\"");
strList << tr("\"Voor kurk\"");
strList << tr("\"Voor parafine\"");
strList << tr("\"Soort ophaalpunt\""); // "code"
strList << tr("\"Intercommunale?\""); // "code intercommunale"
strList << tr("\"Straat\"");
strList << tr("\"Nr\"");
strList << tr("\"Bus\"");
strList << tr("\"Postcode\"");
strList << tr("\"Plaats\"");
strList << tr("\"Land\"");
strList << tr("\"Openingsuren\"");
strList << tr("\"Contactpersoon\"");
strList << tr("\"Telefoon1\"");
strList << tr("\"Telefoon2\"");
strList << tr("\"Email1\"");
strList << tr("\"Email2\"");
strList << tr("\"Taalvoorkeur\""); // "taalvoorkeur"
strList << tr("\"Liefst contacteren per\""); // "preferred_contact"
strList << tr("\"Attest nodig?\"");
strList << tr("\"Attestfrequentie\"");
strList << tr("\"Extra informatie\"");
strList << tr("\"Laatste contactmoment\"");
// strList << tr("\"Contacteer tegen\""); -> we do not need to export this column, see (c == 25 ) while going through the results
strList << tr("\"Laatste ophaalmoment\"");
strList << tr("\"Voorspelde nieuwe ophaling\"");
strList << tr("\"Latitude\"");
strList << tr("\"Longitude\"");
data << strList.join( ";" )+EndOfLine;
vvimDebug() << "[5] fill in the content";
while (query.next())
{
strList.clear();
for( int c = 2; c < columns_in_queryresult; ++c ) // first two columns, namely "ophaalpunten.id" and "ophaalpunten.timestamp", do not need to be exported
{
/// MAKE THIS A SWITCH STATEMENT!!!
switch(c)
{
case 25:
// the column "contact_again_on" is obsolete, but still exist in the database
break;
// Boolean values:
case 3: // Kurk?
case 4: // Parafine?
case 21: // Attest nodig?
/** if(query.value(c).toInt() == 1)
strList << tr("\"ja\"");
else
strList << tr("\"nee\"");
break;
**/ if(query.value(c).toBool())
strList << tr("\"ja\"");
else
strList << tr("\"nee\"");
break;
case 5: // Soort ophaalpunt
strList << "\""+location_type.value(query.value(c).toInt()) +"\"";
break;
case 6: // Intercommunale?
strList << "\""+intercommunales.value(query.value(c).toInt()) +"\"";
break;
case 19: // Taalvoorkeur?
strList << "\""+languages.value(query.value(c).toInt()) +"\"";
break;
case 20: // Liefst contacteren per?
strList << "\""+contact_preference.value(query.value(c).toInt()) +"\"";
break;
case 22: // Attestfrequentie
strList << "\""+attest_frequencies.value(query.value(c).toInt()) +"\"";
break;
default:
strList << "\""+query.value(c).toString().toUtf8() +"\"";
break;
}
}
/** MS Excel from Geert treats fields that contain a \n as a new row.
This can be very confusing, therefore we add the call to "replace()"
as to change every \n by a white space.
**/
data << strList.join( ";" ).replace("\n"," ")+EndOfLine;
records++;
data.flush();
}
if(records < 1)
{
vvimDebug() << "...no results from this query";
data << tr("Geen ophaalpunten gevonden in de databank.")+";\n";
}
else
{
vvimDebug() << "..." << records << "locations found and recorded";
}
QMessageBox::information(this, tr("Informatie ophaalpunten geëxporteerd"), tr("Er werden %1 ophaalpunten gevonden en geëxporteerd naar bestand %2.").arg(records).arg(filename));
f.close();
// RETURN TRUE
}