-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabases.cpp
More file actions
346 lines (294 loc) · 11.7 KB
/
Databases.cpp
File metadata and controls
346 lines (294 loc) · 11.7 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
/*
* Databases.cpp
*
* Created on: 14 Feb 2012
* Author: Max Foster
*/
#include <iostream>
#include <fstream>
using namespace std;
#include "Databases.h"
#include "Database.h"
#include "Setting.h"
#include "Customer.h"
#include "Job.h"
#include "Part.h"
#include "Task.h"
#include "Expense.h"
#include "VatRate.h"
#include "Globals.h"
#include "ExpenseController.h"
#include "CustomerController.h"
#include "JobController.h"
Database<Setting> *settingDatabasePtr = NULL; // Not static so it can be accessed by Database with extern
static Database<Customer> *customerDatabasePtr = NULL;
static Database<Job> *jobDatabasePtr = NULL;
static Database<Part> *partDatabasePtr = NULL;
static Database<Task> *taskDatabasePtr = NULL;
static Database<Expense> *expenseDatabasePtr = NULL;
static Database<VatRate> *vatRateDatabasePtr = NULL;
static bool initialised = false;
#ifdef COMPILE_TESTS
static bool testing;
void Databases::init(bool testing)
{
// If the instances of Database have not already been created, create them
if (initialised) return;
settingDatabasePtr = new Database<Setting>(testing);
customerDatabasePtr = new Database<Customer>(testing);
jobDatabasePtr = new Database<Job>(testing);
partDatabasePtr = new Database<Part>(testing);
taskDatabasePtr = new Database<Task>(testing);
expenseDatabasePtr = new Database<Expense>(testing);
vatRateDatabasePtr = new Database<VatRate>(testing);
initialised = true;
::testing = testing;
}
#else
void Databases::init()
{
// If the instances of Database have not already been created, create them
if (initialised) return;
settingDatabasePtr = new Database<Setting>;
customerDatabasePtr = new Database<Customer>;
jobDatabasePtr = new Database<Job>;
partDatabasePtr = new Database<Part>;
taskDatabasePtr = new Database<Task>;
expenseDatabasePtr = new Database<Expense>;
vatRateDatabasePtr = new Database<VatRate>;
initialised = true;
}
#endif
void Databases::finalise()
{
// If the databases have been initialised, delete the instances of the databases
if (!initialised) return;
delete customerDatabasePtr;
delete jobDatabasePtr;
delete partDatabasePtr;
delete taskDatabasePtr;
delete expenseDatabasePtr;
delete vatRateDatabasePtr;
delete settingDatabasePtr;
customerDatabasePtr = NULL;
jobDatabasePtr = NULL;
partDatabasePtr = NULL;
taskDatabasePtr = NULL;
expenseDatabasePtr = NULL;
vatRateDatabasePtr = NULL;
settingDatabasePtr = NULL;
initialised = false;
}
Database<Setting> & Databases::settings()
{
// If the database pointer is null, then throw an exception, otherwise return a reference to the database
if (settingDatabasePtr == NULL) throw(std::runtime_error("Setting database not initialised"));
return *settingDatabasePtr;
}
Database<Customer> & Databases::customers()
{
if (customerDatabasePtr == NULL) throw(std::runtime_error("Customer database not initialised"));
return *customerDatabasePtr;
}
Database<Job> & Databases::jobs()
{
if (jobDatabasePtr == NULL) throw(std::runtime_error("Job database not initialised"));
return *jobDatabasePtr;
}
Database<Part> & Databases::parts()
{
if (partDatabasePtr == NULL) throw(std::runtime_error("Part database not initialised"));
return *partDatabasePtr;
}
Database<Task> & Databases::tasks()
{
if (taskDatabasePtr == NULL) throw(std::runtime_error("Task database not initialised"));
return *taskDatabasePtr;
}
Database<Expense> & Databases::expenses()
{
if (expenseDatabasePtr == NULL) throw(std::runtime_error("Expense database not initialised"));
return *expenseDatabasePtr;
}
Database<VatRate> & Databases::vatRates()
{
if (vatRateDatabasePtr == NULL) throw(std::runtime_error("VAT rate database not initialised"));
return *vatRateDatabasePtr;
}
void Databases::reloadDatabaseFilenames()
{
// If the databases haven't been initialised, throw an exception, otherwise force the databases to reload the
// database file names and move the files if necessary
if (!initialised) throw(std::runtime_error("Databases not initialised"));
#ifdef COMPILE_TESTS
settingDatabasePtr->reloadFilename(true, testing);
customerDatabasePtr->reloadFilename(true, testing);
jobDatabasePtr->reloadFilename(true, testing);
partDatabasePtr->reloadFilename(true, testing);
taskDatabasePtr->reloadFilename(true, testing);
expenseDatabasePtr->reloadFilename(true, testing);
vatRateDatabasePtr->reloadFilename(true, testing);
#else
settingDatabasePtr->reloadFilename(true);
customerDatabasePtr->reloadFilename(true);
jobDatabasePtr->reloadFilename(true);
partDatabasePtr->reloadFilename(true);
taskDatabasePtr->reloadFilename(true);
expenseDatabasePtr->reloadFilename(true);
vatRateDatabasePtr->reloadFilename(true);
#endif
}
string sanitize(const string &str)
{
string returnStr = str;
size_t index = -1;
while ((index = returnStr.find(L'£', index + 1)) != string::npos) returnStr.replace(index, 1, "£");
index = -1;
while ((index = returnStr.find('&', index + 1)) != string::npos) returnStr.replace(index, 1, "&");
return returnStr;
}
void exportExpenseXml(ofstream &file)
{
Database<Expense>::recordListPtr expenses = ExpenseController::getAllExpenses();
string output = "<expenses>\n";
file.write(output.c_str(), output.size());
for (unsigned i = 0; i < expenses->size(); ++i)
{
Expense &expense = expenses->at(i);
Date date(expense.getDate());
output =
"<expense>\n"
"<day>" + toString(date.day) + "</day>\n"
"<month>" + toString(date.month) + "</month>\n"
"<year>" + toString(date.year) + "</year>\n"
"<description>" + sanitize(expense.getDescription()) + "</description>\n"
"<price>" + to2Dp(toString(expense.getPrice()).c_str()).toStdString() + "</price>\n"
"<vat>" + to2Dp(toString(expense.getVat()).c_str()).toStdString() + "</vat>\n"
"<category>" + lowerCase(expense.getTypeString()) + "</category>\n"
"</expense>\n";
file.write(output.c_str(), output.size());
}
output = "</expenses>\n";
file.write(output.c_str(), output.size());
}
void exportPartsXml(ofstream &file, Job &job)
{
Database<Part>::recordListPtr parts = JobController::getJobParts(job.getId());
string output = "<materials>\n";
file.write(output.c_str(), output.size());
for (unsigned i = 0; i < parts->size(); ++i)
{
Part &part = parts->at(i);
output =
"<material>\n"
"<name>" + sanitize(part.getName()) + "</name>\n"
"<code>" + sanitize(part.getNumber()) + "</code>\n"
"<price>" + to2Dp(toString(part.getPrice()).c_str()).toStdString() + "</price>\n"
"<quantity>" + toString(part.getQuantity()) + "</quantity>\n"
"</material>\n";
file.write(output.c_str(), output.size());
}
output = "</materials>\n";
file.write(output.c_str(), output.size());
}
void exportJobXml(ofstream &file, Customer &customer)
{
Database<Job>::recordListPtr jobs = CustomerController::getCustomerJobs(customer.getId());
string output = "<jobs>\n";
file.write(output.c_str(), output.size());
for (unsigned i = 0; i < jobs->size(); ++i)
{
Job &job = jobs->at(i);
Date date(job.getDate());
string completedString, invoiceIdCodeString;
if ((job.getCompletionState() == Job::DONE_PAID) || (job.getCompletionState() == Job::DONE_UNPAID))
completedString = "true";
else completedString = "false";
invoiceIdCodeString = toString(job.getId()) + toString(date.day) + toString(date.month) + toString(date.year);
output =
"<job>\n"
"<day>" + toString(date.day) + "</day>\n"
"<month>" + toString(date.month) + "</month>\n"
"<year>" + toString(date.year) + "</year>\n"
"<hour>" + toString(date.hour) + "</hour>\n"
"<minute>" + toString(date.minute - (date.minute % 5)) + "</minute>\n"
"<description>" + sanitize(job.getDescription()) + "</description>\n"
"<labour_charge>" + to2Dp(toString(job.getLabourCharge()).c_str()).toStdString() + "</labour_charge>\n"
"<completed>" + completedString + "</completed>\n"
"<vat_rate>" + toString(Globals::vatRate(date)) + "</vat_rate>\n"
"<invoice_id_code>" + invoiceIdCodeString + "</invoice_id_code>\n";
file.write(output.c_str(), output.size());
exportPartsXml(file, job);
if (job.getCompletionState() == Job::DONE_PAID)
{
Date date(job.getPaymentDate());
output =
"<payment>\n"
"<payment_method>" + sanitize(lowerCase(job.getPaymentMethodString())) + "</payment_method>\n"
"<day>" + toString(date.day) + "</day>\n"
"<month>" + toString(date.month) + "</month>\n"
"<year>" + toString(date.year) + "</year>\n"
"</payment>\n";
file.write(output.c_str(), output.size());
}
output = "</job>\n";
file.write(output.c_str(), output.size());
}
output = "</jobs>\n";
file.write(output.c_str(), output.size());
}
void exportCustomerXml(ofstream &file)
{
Database<Customer>::recordListPtr customers = CustomerController::getAllCustomers();
string output = "<customers>\n";
file.write(output.c_str(), output.size());
for (unsigned i = 0; i < customers->size(); ++i)
{
Customer &customer = customers->at(i);
output =
"<customer>\n"
"<forename>" + sanitize(customer.getForename()) + "</forename>\n"
"<surname>" + sanitize(customer.getSurname()) + "</surname>\n"
"<address_line_1>" + sanitize(customer.getAddressLine1()) + "</address_line_1>\n"
"<address_line_2>" + sanitize(customer.getAddressLine2()) + "</address_line_2>\n"
"<town>" + sanitize(customer.getTown()) + "</town>\n"
"<postcode>" + sanitize(customer.getPostcode()) + "</postcode>\n"
"<home_phone_number>" + sanitize(customer.getHomePhoneNumber()) + "</home_phone_number>\n"
"<mobile_phone_number>" + sanitize(customer.getMobilePhoneNumber()) + "</mobile_phone_number>\n"
"<email_address>" + sanitize(customer.getEmailAddress()) + "</email_address>\n";
file.write(output.c_str(), output.size());
exportJobXml(file, customer);
output = "</customer>\n";
file.write(output.c_str(), output.size());
}
output = "</customers>\n";
file.write(output.c_str(), output.size());
}
void exportBusinessXml(ofstream &file)
{
string output =
"<business>\n"
"<name>Ian Foster Services</name>\n"
"<currency>&pound;</currency>\n"
"<country>United Kingdom</country>\n"
"<business_details></business_details>\n";
file.write(output.c_str(), output.size());
exportCustomerXml(file);
exportExpenseXml(file);
output = "</business>";
file.write(output.c_str(), output.size());
}
void Databases::exportXml(const char *filename)
{
if (!initialised) throw(std::runtime_error("Databases not initialised"));
ofstream file;
file.open(filename);
if (!file.is_open()) {
cout << "File 'database.xml' could not be created" << endl;
return;
}
string output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
file.write(output.c_str(), output.size());
exportBusinessXml(file);
file.close();
}