-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdfGenerator.cpp
More file actions
402 lines (346 loc) · 16.2 KB
/
PdfGenerator.cpp
File metadata and controls
402 lines (346 loc) · 16.2 KB
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
/*
* PdfGenerator.cpp
*
* Created on: 29 Feb 2012
* Author: Max Foster
*/
#include <map>
using namespace std;
#include <QWebView>
#include <QUrl>
#include <QPrinter>
#include <QFile>
#include "PdfGenerator.h"
#include "Globals.h"
#include "Utils.h"
#include "Databases.h"
#include "Job.h"
#include "Part.h"
#include "Customer.h"
#include "Expense.h"
#include "JobController.h"
#include "CustomerController.h"
#include "ExpenseController.h"
// Attributes is a map/dictionary that has string keys and string values. A key represents a place in the HTML to
// replace with the value. E.g. The string '<$ forename $>' in the HTML code will be replaced by the value indexed by
// the string 'price' in the map, so the resulting HTML will look something like 'John'
typedef map<QString, QString> Attributes;
QString toValidFileName(const char *fileName_)
{
// If the file name does not end with '.pdf' append '.pdf' to it
QString fileName(fileName_);
if (!fileName.endsWith(".pdf", Qt::CaseInsensitive)) fileName += ".pdf";
return fileName;
}
QString getHtml(const QString &resourceName)
{
// Read the data from the HTML resource and return it as a string
QString returnString;
QFile file(":html/" + resourceName);
if (file.exists())
{
file.open(QFile::ReadOnly);
if (file.isOpen())
{
returnString = file.readAll();
file.close();
}
}
return returnString;
}
void parseHtml(QString &html, Attributes &attributes)
{
static const QString startTag = "<$", endTag = "$>";
int startIndex = 0;
QString key;
// While there are instances of the start tag string in the HTML
while ((startIndex = html.indexOf(startTag, startIndex)) >= 0)
{
// Find the end of the tag
int substringStart = startIndex + startTag.length();
int endIndex = html.indexOf(endTag, substringStart);
if (endIndex < 0) break;
int substringEnd = endIndex - endTag.length();
while (html[substringStart] == ' ') ++substringStart;
while (html[substringEnd] == ' ') --substringEnd;
// Get the keyword in between the tags
key = html.mid(substringStart, substringEnd - (substringStart - 1));
// Replace the whole tag with a value in the attributes dictionary
html.replace(startIndex, endIndex + endTag.length() - startIndex, attributes[key]);
}
}
// A macro that creates a webview and attemps to get the HTML data from a HTML resource
#define GET_WEBVIEW_AND_HTML() \
QWebView view;\
QString html = getHtml(templateResourceName);\
if (html.isEmpty())\
{\
showErrorDialog(("Could not open file " + templateResourceName).toStdString().c_str());\
return false;\
}
// A macro that prints the rendered HTML page into a PDF file
#define PRINT_TO_PDF() \
QPrinter printer;\
printer.setOutputFormat(QPrinter::PdfFormat);\
printer.setOutputFileName(fileName);\
view.print(&printer)
void getInvoiceReceiptAttributes(Attributes &attributes, const Job &job)
{
// Get the customer and parts associated with the job
Customer customer = CustomerController::getCustomer(job.getCustomerId());
Database<Part>::recordListPtr parts = JobController::getJobParts(job.getId());
Date jobDate(job.getDate());
attributes["invoice-number"] =
QString((toString(job.getId()) + toString(jobDate.day) + toString(jobDate.month) + toString(jobDate.year))
.c_str());
// Fill in the date attribute and customer details attributes
attributes["date"] = jobDate.toQStringWithoutTime();
attributes["customer-name"] = createFullName(customer.getForename(), customer.getSurname());
attributes["customer-addressline1"] = customer.getAddressLine1();
if (strlen(customer.getAddressLine2()) > 0) // If there is an address line 2, then fill it in as normal
{
attributes["customer-addressline2"] = customer.getAddressLine2();
attributes["customer-town"] = customer.getTown();
}
else // Otherwise set the town as the address line 2 to make the address look normal
{
attributes["customer-addressline2"] = customer.getTown();
attributes["customer-town"] = "";
}
attributes["customer-postcode"] = customer.getPostcode();
QString customerTelephone = customer.getHomePhoneNumber();
if (strlen(customer.getMobilePhoneNumber()) > 0)
{
customerTelephone += " / ";
customerTelephone += customer.getMobilePhoneNumber();
}
attributes["customer-telephone"] = customerTelephone;
attributes["customer-emailaddress"] = customer.getEmailAddress();
attributes["job-description"] = job.getDescription();
// Loop through the parts to calculate total prices and also to generate HTML for the rows of the parts table in
// the PDF
double totalPartPriceExclVat = 0.0, totalPartVat = 0.0;
QString partHtml = "";
for (unsigned i = 0; i < parts->size(); ++i)
{
Part &part = parts->at(i);
const double partPriceExclVat = part.getPrice() * part.getQuantity(),
partVat = doubleTo2Dp(partPriceExclVat * (part.getVatRate() / 100.0));
partHtml += "<tr><td class='text-mid'>";
partHtml += toString(part.getQuantity()).c_str();
partHtml += "</td><td>";
partHtml += part.getName();
partHtml += "</td><td class='text-mid'>";
partHtml += L'£';
partHtml += to2Dp(toString(partPriceExclVat).c_str());
partHtml += "</td><td class='text-mid'>";
partHtml += L'£';
partHtml += to2Dp(toString(partPriceExclVat + partVat).c_str());
partHtml += "</td></tr>";
totalPartPriceExclVat += partPriceExclVat;
totalPartVat += partVat;
}
attributes["parts-rows"] = partHtml;
// Set the price attributes
attributes["parts-totalpriceexclvat"] = to2Dp(toString(totalPartPriceExclVat).c_str());
attributes["parts-vat"] = to2Dp(toString(totalPartVat).c_str());
attributes["parts-totalpriceinclvat"] = to2Dp(toString(totalPartPriceExclVat + totalPartVat).c_str());
attributes["job-labourcharge"] = to2Dp(toString(job.getLabourCharge()).c_str());
attributes["vatrate"] = toString(Globals::vatRate(job.getDate())).c_str();
attributes["job-vat"] = to2Dp(toString(job.getVat()).c_str());
attributes["job-total"] = to2Dp(toString(job.getLabourCharge() + job.getVat()).c_str());
attributes["totalprice-exclvat"] = to2Dp(toString(job.getLabourCharge() + totalPartPriceExclVat).c_str());
attributes["totalprice-inclvat"]
= to2Dp(toString(job.getLabourCharge() + job.getVat() + totalPartPriceExclVat + totalPartVat).c_str());
}
bool PdfGenerator::generateInvoice(const char *fileName_, const Job &job)
{
QString fileName = toValidFileName(fileName_), templateResourceName = "invoice_receipt_template.html";
Attributes attributes;
getInvoiceReceiptAttributes(attributes, job);
attributes["title"] = "INVOICE";
attributes["extracontent"] =
"<p>Cheque Payments to 'Ian Foster Services' address above. For Credit/Debit Card payments please contact "
"Ian on number above.<br/>For Bacs payments; Sort Code 089250. Account No 70772614, Reference, your name."
"<br/>We thank you for your custom.</p>";
GET_WEBVIEW_AND_HTML();
parseHtml(html, attributes);
view.setHtml(html);
PRINT_TO_PDF();
return true;
}
bool PdfGenerator::generateReceipt(const char *fileName_, const Job &job)
{
QString fileName = toValidFileName(fileName_), templateResourceName = "invoice_receipt_template.html";
Attributes attributes;
getInvoiceReceiptAttributes(attributes, job);
attributes["title"] = "RECEIPT";
// Need to update the date to the current date instead of the job date
attributes["date"] = Date(time(NULL)).toQStringWithoutTime();
// An extra row in the totals table in the invoice needs to be added to display the payment method
QString extraTotalRow = "<td class='tbl-l'><strong>Payment Method:</strong></td><td class='tbl-r'>";
extraTotalRow += job.getPaymentMethodString().c_str();
extraTotalRow += "</td>";
attributes["extratotalrows"] = extraTotalRow;
GET_WEBVIEW_AND_HTML();
parseHtml(html, attributes);
view.setHtml(html);
PRINT_TO_PDF();
return true;
}
// Functions to be used in sorting/searching functions in PdfGenerator::generateReport
namespace DateFunctions
{
static time_t dateLowerBound, dateUpperBound;
template<typename T>
static bool isRecordDateWithinBounds(const T &record, void *)
{
return (record.getDate() >= dateLowerBound) && (record.getDate() <= dateUpperBound);
}
template<typename T>
static bool isRecordPaymentDateWithinBounds(const T &record, void *)
{
return (record.getPaymentDate() >= dateLowerBound) && (record.getPaymentDate() <= dateUpperBound);
}
template<typename T>
static int compareRecordDates(const T &record1, const T &record2)
{
const time_t date1 = record1.getDate(), date2 = record2.getDate();
return date1 < date2 ? -1 : (date1 > date2 ? 1 : 0);
}
}
bool PdfGenerator::generateReport(const char *fileName, const int month, const int year)
{
// Convert the month and year into a start date and end date, and overload PdfGenerator::generateReport to keep the
// code DRY
return generateReport(fileName, Date(0, 0, 1, month, year), Date(time_t(Date(0, 0, 1, month + 1, year)) - 1));
}
bool PdfGenerator::generateReport(const char *fileName_, const Date &startDate, const Date &endDate)
{
QString fileName = toValidFileName(fileName_), templateResourceName = "report_template.html";
// Set the date bounds, get all of the jobs and expenses and remove any that are outside the date bounds.
// Then sort them by date ascending
DateFunctions::dateLowerBound = startDate;
DateFunctions::dateUpperBound = endDate;
Database<Job>::recordListPtr jobs = JobController::getAllJobs();
Databases::jobs().keepRecords(*jobs, DateFunctions::isRecordPaymentDateWithinBounds, NULL);
Databases::jobs().sortRecords(*jobs, 0, jobs->size() - 1, DateFunctions::compareRecordDates);
Database<Expense>::recordListPtr expenses = ExpenseController::getAllExpenses();
Databases::expenses().keepRecords(*expenses, DateFunctions::isRecordDateWithinBounds, NULL);
Databases::expenses().sortRecords(*expenses, 0, expenses->size() - 1, DateFunctions::compareRecordDates);
// Loop through the jobs to calculate the total income excluding VAT and total VAT to be added, as well as
// generating HTML to populate the jobs table in the report
double incomeExclVat = 0.0, incomeVat = 0.0;
QString jobHtml = "";
for (unsigned i = 0; i < jobs->size(); ++i)
{
Job &job = jobs->at(i);
if (job.getCompletionState() == Job::DONE_PAID)
{
double jobChargeExclVat = job.getLabourCharge(), jobVat = job.getVat();
// Add part prices onto job totals
Database<Part>::recordListPtr parts = JobController::getJobParts(job.getId());
for (unsigned j = 0; j < parts->size(); ++j)
{
Part &part = parts->at(j);
jobChargeExclVat += part.getPrice() * part.getQuantity();
jobVat += doubleTo2Dp((part.getPrice() * part.getQuantity()) * (part.getVatRate() / 100.0));
}
incomeExclVat += jobChargeExclVat;
incomeVat += jobVat;
// We need the customer name associated with the job
Customer customer = CustomerController::getCustomer(job.getCustomerId());
jobHtml += "<tr><td class='text-mid'>";
jobHtml += Date(job.getPaymentDate()).toQStringWithoutTime();
jobHtml += "</td><td class='text-mid'>";
jobHtml += createFullName(customer.getForename(), customer.getSurname());
jobHtml += "</td><td class='text-mid'>";
jobHtml += job.getPaymentMethodString().c_str();
jobHtml += "</td><td class='text-mid'>";
jobHtml += L'£';
jobHtml += to2Dp(toString(jobChargeExclVat).c_str());
jobHtml += "</td><td class='text-mid'>";
jobHtml += L'£';
jobHtml += to2Dp(toString(jobVat).c_str());
jobHtml += "</td><td class='text-mid'>";
jobHtml += L'£';
jobHtml += to2Dp(toString(jobChargeExclVat + jobVat).c_str());
jobHtml += "</td></tr>";
}
}
const double incomeTotal = incomeExclVat + incomeVat;
// Loop throught the expenses to calculate totals and generate HTML
double expensesExclVat = 0.0, expensesVat = 0.0;
QString expenseHtml = "";
for (unsigned i = 0; i < expenses->size(); ++i)
{
Expense &expense = expenses->at(i);
expensesExclVat += expense.getPrice();
expensesVat += expense.getVat();
expenseHtml += "<tr><td class='text-mid'>";
expenseHtml += Date(expense.getDate()).toQStringWithoutTime();
expenseHtml += "</td><td class='text-mid'>";
expenseHtml += expense.getTypeString().c_str();
expenseHtml += "</td><td class='text-mid'>";
expenseHtml += L'£';
expenseHtml += to2Dp(toString(expense.getPrice()).c_str());
expenseHtml += "</td><td class='text-mid'>";
expenseHtml += L'£';
expenseHtml += to2Dp(toString(expense.getVat()).c_str());
expenseHtml += "</td><td class='text-mid'>";
expenseHtml += L'£';
expenseHtml += to2Dp(toString(expense.getTotalPrice()).c_str());
expenseHtml += "</td></tr>";
}
const double expenseTotal = expensesExclVat + expensesVat,
vatOwed = incomeVat - expensesVat;
Attributes attributes;
// Create a title that looks natural whatever start and end date is picked
attributes["title"] = "Report for ";
// If the start date is at the beginning of a month and the end date at the end of a month...
if ((startDate.day == 1) && (endDate.day == (unsigned)QDate(endDate).daysInMonth()))
{
// If the start and end date are in the same month, use a '<month name> <year>' format
// E.g. 'Report for March 2012'
if ((startDate.month == endDate.month) && (startDate.year == endDate.year))
{
attributes["title"]
+= QDate::longMonthName(startDate.month) + ' ' + toString(startDate.year).c_str();
}
// Otherwise if the years are the same, use a '<month name> - <month name> <year>' format
// E.g. 'Report for March - June 2012'
else if (startDate.year == endDate.year)
{
attributes["title"]
+= QDate::longMonthName(startDate.month) + " - " + QDate::longMonthName(endDate.month) + ' '
+ toString(startDate.year).c_str();
}
// Otherwise use a '<month name> <year> - <month name> <year>' format
// E.g. 'Report for March 2012 - June 2013'
else
{
attributes["title"]
+= QDate::longMonthName(startDate.month) + ' ' + toString(startDate.year).c_str() + " - "
+ QDate::longMonthName(endDate.month) + ' ' + toString(endDate.year).c_str();
}
}
// Otherwise use a 'DD/MM/YYYY to DD/MM/YYYY' format, e.g. 'Report for 2/3/2012 to 17/11/2012'
else attributes["title"] += startDate.toQStringWithoutTime() + " to " + endDate.toQStringWithoutTime();
// Set the remaining attributes and generate the PDF
attributes["income-exclvat"] = to2Dp(toString(incomeExclVat).c_str());
attributes["income-vat"] = to2Dp(toString(incomeVat).c_str());
attributes["income-total"] = to2Dp(toString(incomeTotal).c_str());
attributes["expenses-exclvat"] = to2Dp(toString(expensesExclVat).c_str());
attributes["expenses-vat"] = to2Dp(toString(expensesVat).c_str());
attributes["expenses-total"] = to2Dp(toString(expenseTotal).c_str());
attributes["profit-exclvat"] = to2Dp(toString(incomeExclVat - expensesExclVat).c_str());
attributes["profit-inclvat"] = to2Dp(toString(incomeTotal - expenseTotal).c_str());
attributes["vatowed"] = to2Dp(toString(vatOwed).c_str());
attributes["jobs-rows"] = jobHtml;
attributes["expenses-rows"] = expenseHtml;
GET_WEBVIEW_AND_HTML();
parseHtml(html, attributes);
view.setHtml(html);
PRINT_TO_PDF();
return true;
}