Skip to content

Commit

Permalink
Fix message not displayed (#340)
Browse files Browse the repository at this point in the history
* Fix #339, missing rendering of message

* [Tests] Add tests for message and additional information
  • Loading branch information
schoero authored Aug 25, 2021
1 parent fbd12ed commit ec124a3
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 4 deletions.
50 changes: 46 additions & 4 deletions src/swissqrbill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,9 @@ export class PDF extends ExtendedPDF.PDF {
}


//-- Additional information
//-- Message / Additional information

if(this._data.additionalInformation !== undefined){
if(this._data.message !== undefined || this._data.additionalInformation !== undefined){

this.fontSize(8);
this.font("Helvetica-Bold");
Expand All @@ -552,9 +552,44 @@ export class PDF extends ExtendedPDF.PDF {

this.fontSize(10);
this.font("Helvetica");
this.text(this._data.additionalInformation, {

const options = {
width: utils.mmToPoints(87)
});
};

const singleLineHeight = this.heightOfString("A", options);
const maxLines = this._referenceType === "QRR" || this._referenceType === "SCOR" ? 3 : 4;
const linesOfMessage = this._data.message !== undefined ? this.heightOfString(this._data.message, options) / singleLineHeight : 0;
const linesOfAdditionalInformation = this._data.additionalInformation !== undefined ? this.heightOfString(this._data.additionalInformation, options) / singleLineHeight : 0;

if(this._data.additionalInformation !== undefined){

if(this._referenceType === "QRR" || this._referenceType === "SCOR"){

// QRR and SCOR have 1 line for the message and 2 lines for the additional information

if(this._data.message !== undefined){
this.text(this._data.message, { ...options, lineBreak: false, ellipsis: true, height: singleLineHeight });
}

} else {

// Non QRR and SCOR have 4 lines total available and the message should be shortened if necessary

if(this._data.message !== undefined){
if(linesOfMessage + linesOfAdditionalInformation > maxLines){
const maxLinesOfMessage = maxLines - linesOfAdditionalInformation;
this.text(this._data.message, { ...options, height: singleLineHeight * maxLinesOfMessage, lineBreak: true, ellipsis: true });
}
}

}

this.text(this._data.additionalInformation, options);

} else if(this._data.message !== undefined){
this.text(this._data.message, { ...options, height: singleLineHeight * maxLines, lineBreak: true, ellipsis: true });
}

this.moveDown();

Expand Down Expand Up @@ -785,6 +820,13 @@ export class PDF extends ExtendedPDF.PDF {
}


//-- Message + Additional information

if(this._data.message !== undefined && this._data.additionalInformation !== undefined){
if(this._data.additionalInformation.length + this._data.message.length > 140){ throw new Error("Message and additionalInfromation combined must be a maximum of 140 characters."); }
}


//-- AV1

if(this._data.av1 !== undefined){
Expand Down
25 changes: 25 additions & 0 deletions tests/additional-information.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const SwissQRBill = require("../");

const data = {
currency: "CHF",
amount: 1199.95,
reference: "210000000003139471430009017",
additionalInformation: "//S1/10/10201409/11/170309/20/14000000/ 30/106017086",
creditor: {
name: "Robert Schneider AG",
address: "Rue du Lac 1268",
zip: 2501,
city: "Biel",
account: "CH4431999123000889012",
country: "CH"
},
debtor: {
name: "Pia-Maria Rutschmann-Schnyder",
address: "Grosse Marktgasse 28",
zip: 9400,
city: "Rorschach",
country: "CH"
}
};

const pdf = new SwissQRBill.PDF(data, "./output/additional-information.pdf", { "size": "A6/5" });
26 changes: 26 additions & 0 deletions tests/message-additional-information.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const SwissQRBill = require("../");

const data = {
currency: "CHF",
amount: 1199.95,
reference: "210000000003139471430009017",
message: "Invoice number 12345 from order number 12345 on may 3rd 2021.",
additionalInformation: "//S1/10/10201409/11/170309/20/14000000/ 30/106017086",
creditor: {
name: "Robert Schneider AG",
address: "Rue du Lac 1268",
zip: 2501,
city: "Biel",
account: "CH4431999123000889012",
country: "CH"
},
debtor: {
name: "Pia-Maria Rutschmann-Schnyder",
address: "Grosse Marktgasse 28",
zip: 9400,
city: "Rorschach",
country: "CH"
}
};

const pdf = new SwissQRBill.PDF(data, "./output/message-additional-information.pdf", { "size": "A6/5" });
25 changes: 25 additions & 0 deletions tests/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const SwissQRBill = require("../");

const data = {
currency: "CHF",
amount: 1199.95,
reference: "210000000003139471430009017",
message: "Invoice number 12345 from order number 12345 on may 3rd 2021.",
creditor: {
name: "Robert Schneider AG",
address: "Rue du Lac 1268",
zip: 2501,
city: "Biel",
account: "CH4431999123000889012",
country: "CH"
},
debtor: {
name: "Pia-Maria Rutschmann-Schnyder",
address: "Grosse Marktgasse 28",
zip: 9400,
city: "Rorschach",
country: "CH"
}
};

const pdf = new SwissQRBill.PDF(data, "./output/message.pdf", { "size": "A6/5" });
24 changes: 24 additions & 0 deletions tests/no-reference-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const SwissQRBill = require("../");

const data = {
currency: "CHF",
amount: 1199.95,
message: "Invoice number 12345 from order number 12345 on may 3rd 2021.",
creditor: {
name: "Robert Schneider AG",
address: "Rue du Lac 1268",
zip: 2501,
city: "Biel",
account: "CH5800791123000889012",
country: "CH"
},
debtor: {
name: "Pia-Maria Rutschmann-Schnyder",
address: "Grosse Marktgasse 28",
zip: 9400,
city: "Rorschach",
country: "CH"
}
};

const pdf = new SwissQRBill.PDF(data, "./output/no-reference-message.pdf", { "size": "A6/5" });
8 changes: 8 additions & 0 deletions tests/run-windows.bat
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ echo "french"
call node french
echo "italian"
call node italian
echo "message"
call message
echo "no-reference-message"
call no-reference-message
echo "message-additional-information"
call message-additional-information
echo "additional-information"
call additional-information
echo "multipage"
call node multipage
echo "no-amount"
Expand Down
8 changes: 8 additions & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ echo "french"
node french
echo "italian"
node italian
echo "message"
node message
echo "no-reference-message"
node no-reference-message
echo "message-additional-information"
node message-additional-information
echo "additional-information"
node additional-information
echo "multipage"
node multipage
echo "no-amount"
Expand Down

0 comments on commit ec124a3

Please sign in to comment.