-
Notifications
You must be signed in to change notification settings - Fork 1
/
createDeliveryNote.js
445 lines (361 loc) · 11.5 KB
/
createDeliveryNote.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
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
const fs = require("fs");
const PDFDocument = require("pdfkit");
const columns = new Map([
["code", { "header": "Code", "width": 45 }],
["designation", { "header": "Description", "width": 170, "textWidth": 156 }],
["isCE", { "header": "", "width": 0, "textWidth": 14 }],
["weight", { "header": "Net weight", "width": 40 }],
["tare", { "header": "Tare", "width": 40 }],
["quantity", { "header": "Quantity", "width": 45 }],
["grossPrice", { "header": "Gross price", "width": 55 }],
["discount", { "header": "R.", "width": 20 }],
["netPrice", { "header": "Net price", "width": 55 }],
["grossTotal", { "header": "Gross total", "width": 60 }],
["VAT", { "header": "VAT", "width": 20 }],
]);
// Fill textWidth property when not explicitly set
// Set x as the relative coordinate of the column
var x = 0;
for (const column of columns.values()) {
column.x = x;
if (!column.hasOwnProperty('textWidth')) {
column.textWidth = column.width;
}
x += column.textWidth;
};
function generateHeaderBox(doc, deliveryNote, x, y) {
var width = 260;
var bottomVerticalSeparatorX = 55;
// Draw figure
doc
.roundedRect(x, y, width, 150, 5)
.stroke();
// Draw the bottom table
// Horizontal lines
var bottomTableY = 60;
var tableRowHeight = 18;
for (i=0; i < 5; i++) {
doc
.moveTo(x, y + bottomTableY + tableRowHeight * i)
.lineTo(x + width, y + bottomTableY + tableRowHeight * i)
.stroke();
}
// Vertical separator of the bottom part
doc
.moveTo(x + bottomVerticalSeparatorX, y + 60)
.lineTo(x + bottomVerticalSeparatorX, y + 150)
.stroke();
// Fill the top section
var companyName = "Nagarro ES France";
var companyInfoLines = [
"8a Rue Icare - 67960 Entzheim",
"Tel. +33 69 24 20 00",
"Reg nr: 82515283800017"
];
doc
.font('Courier-Bold')
.fontSize(18)
.text(companyName, x + 2, y + 2, { width: width, align: "center" })
.fontSize(6)
.text("SASU - 10.000€", x + 2, y + 17, { width: width, align: "center" })
.font('Courier')
doc.text("", x + 2, y + 30);
companyInfoLines.forEach(companyInfoLine => {
doc
.fontSize(7)
.text(companyInfoLine, { width: width, align: "left" })
.moveDown(0);
});
// Fill the bottom table, line by line
// first the left part that is fixed, the right part which is variable.
// font size and style for the right part of the bottom table
var xPadding = 5;
var rightX = x + bottomVerticalSeparatorX + xPadding;
var leftFontSize = 8;
var rightFontSize = 9;
var leftStyle = {
width: bottomVerticalSeparatorX - 2,
height: leftFontSize,
align: "center"
}
var rightStyle = {
width: width - bottomVerticalSeparatorX - (xPadding * 2),
height: rightFontSize,
align: "left"
};
var offsetY = y + bottomTableY + 6;
// Date
doc
.fontSize(leftFontSize)
.text("Date", x, offsetY, leftStyle);
var dateText = deliveryNote.date + " at " + deliveryNote.time + ", Site code : " + deliveryNote.site
doc
.fontSize(rightFontSize)
.text(dateText, rightX, offsetY, rightStyle);
offsetY += tableRowHeight;
// Camion
doc
.fontSize(leftFontSize)
.text("Truck", x, offsetY, leftStyle);
doc
.fontSize(rightFontSize)
.text(deliveryNote.truck, rightX, offsetY, rightStyle);
offsetY += tableRowHeight;
// Client
doc
.fontSize(leftFontSize)
.text("Customer ID", x, offsetY, leftStyle);
doc
.fontSize(rightFontSize)
.text(deliveryNote.customerId, rightX, offsetY, rightStyle);
offsetY += tableRowHeight;
// Contact
doc
.fontSize(leftFontSize)
.text("Contact", x, offsetY, leftStyle);
offsetY += tableRowHeight;
// Manage contact text concatenation depending on the available info
if (deliveryNote.customerPhoneNumber) {
// remove all spaces from phone number because white-spacing is too
// big with the Courier font. It overflows the table cell.
var tel = "Phone: " + deliveryNote.customerPhoneNumber.replace(/ /g,"");
} else {
var tel = "";
}
if (deliveryNote.customerFaxNumber) {
// remove all spaces from fax number because white-spacing is too
// big with the Courier font. It overflows the table cell.
var fax = "Fax: " + deliveryNote.customerFaxNumber.replace(/ /g,"");
} else {
var fax = "";
}
if (deliveryNote.customerPhoneNumber && deliveryNote.customerFaxNumber) {
var separator = " - ";
} else {
var separator = "";
}
doc
.fontSize(rightFontSize)
.text(tel + separator + fax, rightX, y + 120, rightStyle);
offsetY += tableRowHeight;
// City
doc
.fontSize(leftFontSize)
.text("City", x, y + 139, leftStyle);
doc
.fontSize(rightFontSize)
.text(deliveryNote.customerCity, rightX, y + 138, rightStyle);
}
function generateDeliveryNoteText(doc, deliveryNoteId, x, y) {
doc
.font('Courier-Bold')
.fontSize(15)
.text("Delivery note " + deliveryNoteId, x, y, { width: 270 })
.font('Courier');
}
function generateAddressesBox(doc, deliveryNote, x, y) {
var width = 260;
var height = 130;
// Draw complete lines
doc
.moveTo(x, y)
.lineTo(x, y + height)
.lineTo(x + width, y + height)
.lineTo(x + width, y)
.moveTo(x, y)
.stroke();
// Draw broken line for shipping address
var centralGap = 120;
doc
.moveTo(x, y)
.lineTo(x + width / 2 - centralGap / 2, y)
.moveTo(x + width / 2 + centralGap / 2, y)
.lineTo(x + width, y)
.stroke();
doc
.fontSize(8)
.text("Shipping address", x, y - 2 , { width: width, align: "center" })
// Draw broken line for delivery address
doc
.moveTo(x, y + height / 2)
.lineTo(x + width / 2 - centralGap / 2, y + height / 2)
.moveTo(x + width / 2 + centralGap / 2, y + height / 2)
.lineTo(x + width, y + height / 2)
.stroke();
doc
.fontSize(8)
.text("Billing address", x, y + height / 2 - 2, { width: width, align: "center" })
// Hack as moveTo doesn't work as expected, see issue #1092 on Github
doc.text("", x + 10, y + 8);
deliveryNote.shippingAddressLines.forEach(line => {
doc
.fontSize(10)
.text(line, { width: width - 10, height: 10, align: "left" })
.moveDown(0);
});
// Hack as moveTo doesn't work as expected, see issue #1092 on Github
doc.text("", x + 10, y + height / 2 + 8);
deliveryNote.billingAddressLines.forEach(line => {
doc
.fontSize(10)
.text(line, { width: width - 10, height: 10, align: "left" })
.moveDown(0);
});
}
function createTable(doc, footNote, x, y, height) {
var textPadding = 3;
var cumulX = x;
var tableWidth = 0;
// Create a rectangle for each visible column
// Add a header
for (const column of columns.values()) {
tableWidth += column.width;
if (column.width > 0) {
createColumn(doc, column.header, textPadding, x + column.x, y, column.width, height);
};
cumulX += column.textWidth;
};
// Draw line under header
doc
.moveTo(x, y + 10)
.lineTo(x + tableWidth, y + 10)
.stroke();
// Foot note
doc
.fontSize(8)
.fillColor("red")
.text(footNote, 20, y + height + 2);
doc.fillColor("black");
}
function createColumn(doc, header, textPadding, x, y, width, height) {
// Draw the rectangle
doc
.rect(x, y, width, height)
.stroke();
// Write the header of the column
doc
.fontSize(6)
.text(header, x, y + textPadding, { width: width, align: "center" });
};
function fillRows(doc, rows, x, y) {
var textPadding = 2;
var fontSize = 8;
var lineHeightIncrement = fontSize + textPadding;
// loop on the line of the delivery note
rows.forEach(row => {
rowHeight = lineHeightIncrement;
// loop on the keys of the object to fill the corresponding column
Object.keys(row).forEach(function(key) {
column = columns.get(key);
maxTextWidth = column.textWidth - textPadding;
// deal with the isCE logo which generates an image and not a text
if (key === "isCE") {
if (row['isCE']) {
style = {
width: maxTextWidth,
height: fontSize,
align: "center"
};
doc.image("CE_logo.png", x + column.x, y + textPadding, style);
}
} else {
// fix the width of the text to avoid overflow
style = {
width: maxTextWidth,
height: lineHeightIncrement,
align: "left"
};
// transform all text values to arrays to loop on them
if (!Array.isArray(row[key])) {
textLines = [row[key]];
} else {
textLines = row[key];
};
// the total height of the row increases if there are more text lines than before
rowHeight = Math.max(rowHeight, lineHeightIncrement * textLines.length);
// display the text lines
// offset the height of the text by the number of lines preceeding in the list
textLines.forEach((textLine, i) => {
doc
.fontSize(fontSize)
.text(textLine, x + column.x + textPadding, y + i * lineHeightIncrement + textPadding, style);
});
}
});
// increment the next row height
y += rowHeight;
});
}
function generateFooter(doc, y) {
var legalTextLines = [
"Pomerium vile aestimant urbis diversitate coluntur esse extra nascitur obsequiorum sine obsequiorum aestimant orbos orbos.",
"TCoegit qui cum cum capitis reconciliat universis conscripti undique et et gratiam de amor et me animus pristinus in olim.",
"Memoriam odium memoriam caedes dictionem libidines sine puteos morte memoriam.",
"Coegit qui cum cum capitis reconciliat universis conscripti undique et et gratiam de amor et me animus pristinus in olim.",
"Memoriam odium memoriam caedes dictionem libidines sine puteos morte memoriam."
];
// Hack as moveTo doesn't work as expected, see issue #1092 on Github
doc.text("", 20, y)
legalTextLines.forEach(line => {
doc
.fontSize(5.5)
.text(line, { width: 400, align: "left" })
.moveDown(0);
});
// Signature box
var boxX = 425;
// keep a right margin of 20 points
var boxWidth = doc.page.width - 20 - boxX;
doc
.rect(boxX, y, boxWidth, 50)
.stroke();
doc
.fontSize(8)
.text("Delivery received", boxX, y + 5, { width: boxWidth, align: "center" })
.moveDown(0.2)
.text(" Signature :");
}
function createDeliveryNote(deliveryNote) {
let doc = new PDFDocument({
size: "A4",
margin: 0,
font: "Courier"
});
doc.lineWidth(0.5);
// repeat the document twice with only small variations
const duplicate_doc = [
{
topY: 0,
headerNumber: "1",
tableFootNote: "Customer copy",
},
{
topY: doc.page.height / 2 - 10,
headerNumber: "2",
tableFootNote: "Copy",
}
];
duplicate_doc.forEach(duplicata => {
// top-left box
generateHeaderBox(doc, deliveryNote, 20, duplicata.topY + 20);
// top-right title
generateDeliveryNoteText(doc, deliveryNote.deliveryNoteId, 300, duplicata.topY + 20);
// top-right box
generateAddressesBox(doc, deliveryNote, 310, duplicata.topY + 40);
// index of page between headers
doc
.fontSize(10)
.text(duplicata.headerNumber, 292, duplicata.topY + 160);
// create the table column by column, with headers and the foot note
createTable(doc, duplicata.tableFootNote, 20, duplicata.topY + 175, 170);
// fill the table with the info from the deliveryNote
fillRows(doc, deliveryNote.rows, 20, duplicata.topY + 185);
// legal footer with signature box
generateFooter(doc, duplicata.topY + 360);
})
doc.end();
return doc
}
module.exports = {
createDeliveryNote
};