-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
285 lines (259 loc) · 10.8 KB
/
index.js
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
var fs = require('fs');
var path = require('path');
var pdf = require('html-pdf');
var moment = require('moment');
require('moment-range');
var express = require('express');
var router = express.Router();
var User = require('../../../models/user');
var Church = require('../../../models/church');
function getChurchAndValidate (options, callback) {
User.getUserAndValidate(options.email, options.accessToken, function (user) {
Church
.findOne({
"_id": options.id
})
.where({
$or: [{
"_id": {
$in : user.churches
}
}, {
"createdBy": user._id,
}]
})
.exec(function(err, result) {
callback(result);
});
});
}
router.get('/:id', function (req, res) {
var email = req.session.email;
var accessToken = req.cookies.accessToken;
var id = req.params.id;
var start = req.query.start;
var end = req.query.end;
var range;
if (start && end) {
if (moment(start,"MM/DD/YYYY").isValid() === false || moment(end,"MM/DD/YYYY").isValid() === false) {
return res.status(401).json({
success: false,
message: "Start date invalid."
});
}
range = moment().range(moment(start,"MM/DD/YYYY"), moment(end,"MM/DD/YYYY"));
} else {
var now = new Date();
start = "01/01/" + now.getFullYear();
end = "12/31/" + now.getFullYear();
range = moment().range(moment(start,"MM/DD/YYYY"), moment(end,"MM/DD/YYYY"));
}
if (!email || !accessToken || !id) {
return res.status(401).json({
success: false,
message: "Authentication error."
});
}
getChurchAndValidate({
email: email,
accessToken: accessToken,
id: id,
}, function (church) {
// create html generation from returned church
var html = fs.readFileSync(path.join(__dirname,'./taxStatement.html'), 'utf8');
var options = {
"format": "Letter",
"base": "http://churchetto.com/",
"httpHeaders": {
"email": email,
"token": accessToken,
}
};
var churchAddress = "";
if (church.address && church.address.line1) {
churchAddress =
"<div style=\"font-size:14px;\">"
+ church.address.line1
+ "<br />" + church.address.city
+ ", " + church.address.state
+ " " + church.address.zip
+ "</div>"
} else {
churchAddress = "<div style=\"font-size:14px;\">No Address Data Available</div>";
}
var members = "";
church.members.sort(function (a,b) {
var nameA = "";
var nameB = "";
if (a.lastName) {
nameA = a.lastName.toLowerCase();
if (a.firstName) {
nameA = nameA + a.firstName.toLowerCase();
}
}
if (b.lastName) {
nameB = b.lastName.toLowerCase();
if (b.firstName) {
nameB = nameB + b.firstName.toLowerCase();
}
}
if (nameA < nameB) //sort string ascending
return -1;
if (nameA > nameB)
return 1;
return 0; //default return value (no sorting)
}).map(function (member, i) {
if (!member.address) { member.address = {} }
if (!member.address.line1) { member.address.line1 = "" }
if (!member.address.line2) { member.address.line2 = "" }
if (!member.address.city) { member.address.city = "" }
if (!member.address.state) { member.address.state = "" }
if (!member.address.zip) { member.address.zip = "" }
var memberHtml = fs.readFileSync(path.join(__dirname,'./member.html'), 'utf8');
var contributionTableHtml = fs.readFileSync(path.join(__dirname,'./contributionTable.html'), 'utf8');
var contributionRowHtml = fs.readFileSync(path.join(__dirname,'./contributionRow.html'), 'utf8');
var header = "";
header = header + "<h1 style=\"margin:0px;font-family:'Poiret One', cursive;\">" + church.name + "</h1>";
if (church.phone && church.phone.main) {
header = header + "<div>" + church.phone.main + "</div>";
}
header = header + "<div>" + churchAddress + "</div>";
memberHtml = memberHtml.replace('{HEADER}',header);
memberHtml = memberHtml.replace("{MEMBER_NAME}",
member.lastName + ", " + member.firstName);
memberHtml = memberHtml.replace("{MEMBER_ADDRESS}",
"<div style=\"font-size:12px;\">"
+ member.address.line1
+ "<br />" + member.address.city
+ ", " + member.address.state
+ " " + member.address.zip
+ "</div>");
memberHtml = memberHtml.replace("{DATE_RANGE}",start + " - " + end)
var nonTaxDeductibleHtml = contributionTableHtml;
var nonTaxDeductibleRowsHtml = "";
var nonTaxDeductibleTotal = 0;
var totalContributions = 0;
church.contributions.sort(function (a,b) {
return a.date < b.date;
}).map(function (contribution) {
if (contribution.memberId.toString() !== member._id.toString()) { return; }
if (!contribution.date || range.contains(moment(contribution.date)) === false) { return; }
if (contribution.amount) {
totalContributions = totalContributions + contribution.amount;
}
if (contribution.isTaxDeductible !== false) { return; }
var row = contributionRowHtml;
if (contribution.date) {
row = row.replace("{DATE}",moment(contribution.date).format("MM/DD/YYYY"));
} else {
row = row.replace("{DATE}","-");
}
if (contribution.description) {
row = row.replace("{DESCRIPTION}",contribution.description);
} else {
row = row.replace("{DESCRIPTION}","-");
}
if (contribution.amount) {
nonTaxDeductibleTotal = nonTaxDeductibleTotal + contribution.amount;
row = row.replace("{AMOUNT}","$" + parseFloat(contribution.amount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'));
} else {
row = row.replace("{AMOUNT}","-");
}
nonTaxDeductibleRowsHtml = nonTaxDeductibleRowsHtml + row;
});
nonTaxDeductibleHtml = nonTaxDeductibleHtml.replace("{TITLE}","Non Tax Deductible Contributions");
nonTaxDeductibleHtml = nonTaxDeductibleHtml.replace("{TOTAL_AMOUNT}","Total Non Tax Deductible: $" + parseFloat(nonTaxDeductibleTotal).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'));
nonTaxDeductibleHtml = nonTaxDeductibleHtml.replace("{ROWS}",nonTaxDeductibleRowsHtml);
if (nonTaxDeductibleTotal > 0) {
memberHtml = memberHtml.replace("{NON_TAX_DEDUCTIBLE_CONTRIBUTIONS}",nonTaxDeductibleHtml);
} else {
memberHtml = memberHtml.replace("{NON_TAX_DEDUCTIBLE_CONTRIBUTIONS}","");
}
var taxDeductibleHtml = contributionTableHtml;
var taxDeductibleRowsHtml = "";
var taxDeductibleTotal = 0;
church.contributions.sort(function (a,b) {
return a.date < b.date;
}).map(function (contribution) {
if (contribution.memberId.toString() !== member._id.toString()) { return; }
if (!contribution.date || range.contains(moment(contribution.date)) === false) { return; }
if (contribution.isTaxDeductible !== true) { return; }
var row = contributionRowHtml;
if (contribution.date) {
row = row.replace("{DATE}",moment(contribution.date).format("MM/DD/YYYY"));
} else {
row = row.replace("{DATE}","-");
}
if (contribution.description) {
row = row.replace("{DESCRIPTION}",contribution.description);
} else {
row = row.replace("{DESCRIPTION}","-");
}
if (contribution.amount) {
taxDeductibleTotal = taxDeductibleTotal + contribution.amount;
row = row.replace("{AMOUNT}","$" + parseFloat(contribution.amount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'));
} else {
row = row.replace("{AMOUNT}","-");
}
taxDeductibleRowsHtml = taxDeductibleRowsHtml + row;
});
taxDeductibleHtml = taxDeductibleHtml.replace("{TITLE}","Tax Deductible Contributions");
taxDeductibleHtml = taxDeductibleHtml.replace("{TOTAL_AMOUNT}","Total Tax Deductible: $" + parseFloat(taxDeductibleTotal).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'));
taxDeductibleHtml = taxDeductibleHtml.replace("{ROWS}",taxDeductibleRowsHtml);
if (taxDeductibleTotal > 0) {
memberHtml = memberHtml.replace("{TAX_DEDUCTIBLE_CONTRIBUTIONS}",taxDeductibleHtml);
} else {
memberHtml = memberHtml.replace("{TAX_DEDUCTIBLE_CONTRIBUTIONS}","");
}
var otherHtml = contributionTableHtml;
var otherRowsHtml = "";
var otherTotal = 0;
church.contributions.sort(function (a,b) {
return a.date < b.date;
}).map(function (contribution) {
if (contribution.memberId.toString() !== member._id.toString()) { return; }
if (!contribution.date || range.contains(moment(contribution.date)) === false) { return; }
if (contribution.isTaxDeductible !== undefined) { return; }
var row = contributionRowHtml;
if (contribution.date) {
row = row.replace("{DATE}",moment(contribution.date).format("MM/DD/YYYY"));
} else {
row = row.replace("{DATE}","-");
}
if (contribution.description) {
row = row.replace("{DESCRIPTION}",contribution.description);
} else {
row = row.replace("{DESCRIPTION}","-");
}
if (contribution.amount) {
otherTotal = otherTotal + contribution.amount;
row = row.replace("{AMOUNT}","$" + parseFloat(contribution.amount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'));
} else {
row = row.replace("{AMOUNT}","-");
}
otherRowsHtml = otherRowsHtml + row;
});
otherHtml = otherHtml.replace("{TITLE}","Other Contributions");
otherHtml = otherHtml.replace("{TOTAL_AMOUNT}","Total Other: $" + parseFloat(otherTotal).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'));
otherHtml = otherHtml.replace("{ROWS}",otherRowsHtml);
if (otherTotal > 0) {
memberHtml = memberHtml.replace("{OTHER_CONTRIBUTIONS}",otherHtml);
} else {
memberHtml = memberHtml.replace("{OTHER_CONTRIBUTIONS}","");
}
memberHtml = memberHtml.replace("{TOTAL_CONTRIBUTIONS}","Total Contributions: $" + parseFloat(totalContributions).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'))
memberHtml = memberHtml.replace("{FOOTER}","No goods or services were provided in return for these contributions, other than intangible religious benefits.");
if (i + 1 === church.members.length) {
memberHtml = memberHtml.replace("<div class=\"member member-margin\">","<div class=\"member-margin\">");
}
members = members + memberHtml;
});
html = html.replace('{MEMBERS}',members);
//res.send(html);
pdf.create(html, options).toStream(function(err, stream){
if (err) return console.log(err);
stream.pipe(res);
});
});
});
module.exports = router;