Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adjustment invoice #67

Merged
merged 4 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ let invoice = new Invoice({
buyer: buyer, // the buyer, required
items: [ soldItem1, soldItem2 ], // the sold items, required
prepaymentInvoice: false // prepayment/deposit invoice should be issued, optional, default: false
adjustmentInvoiceNumber: 'ORIGINAL-INVOICE-NUMBER', // optional, only required for creating a correcting invoice
})
```

Expand Down
10 changes: 10 additions & 0 deletions lib/Invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class Invoice {
this._options.paid = options.paid
this._options.comment = options.comment
this._options.logoImage = options.logoImage
this._options.adjustmentInvoiceNumber = options.adjustmentInvoiceNumber
this._options.prepaymentInvoice = options.prepaymentInvoice || false
}

Expand Down Expand Up @@ -66,6 +67,13 @@ export class Invoice {
assert(Array.isArray(this._options.items),
'Valid Items array missing from invoice options')


if (this._options.adjustmentInvoiceNumber !== null && this._options.adjustmentInvoiceNumber !== undefined) {
assert(typeof this._options.adjustmentInvoiceNumber === 'string', '"adjustmentInvoiceNumber" should be a string')
assert(this._options.adjustmentInvoiceNumber.length > 0, '"adjustmentInvoiceNumber" should be minimum 1 character')
this._options.adjustmentInvoice = true
}

let o = wrapWithElement('fejlec', [
[ 'keltDatum', this._options.issueDate ],
[ 'teljesitesDatum', this._options.fulfillmentDate ],
Expand All @@ -78,6 +86,8 @@ export class Invoice {
[ 'arfolyam', this._options.exchangeRate ],
[ 'rendelesSzam', this._options.orderNumber ],
[ 'elolegszamla', this._options.prepaymentInvoice ],
[ 'helyesbitoszamla', this._options.adjustmentInvoice ],
[ 'helyesbitettSzamlaszam', this._options.adjustmentInvoiceNumber ],
[ 'dijbekero', this._options.proforma ],
[ 'logoExtra', this._options.logoImage ],
[ 'szamlaszamElotag', this._options.invoiceIdPrefix ],
Expand Down
60 changes: 57 additions & 3 deletions tests/invoice.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,60 @@ describe('Invoice', function () {
it('should have `tetelek` node', function () {
expect(obj).to.have.property('tetelek')
})
})
})
})

// START- New test suite for adjustmentInvoiceNumber property
describe('adjustmentInvoiceNumber validation', function () {
it('should not include adjustmentInvoiceNumber when it is null', function (done) {
invoice._options.adjustmentInvoiceNumber = null;
ert78gb marked this conversation as resolved.
Show resolved Hide resolved
parser.parseString('<wrapper>' + invoice._generateXML() + '</wrapper>', function (err, result) {
expect(result.wrapper).to.not.have.deep.property('fejlec.helyesbitettSzamlaszam');
done();
});
});

it('should not include adjustmentInvoiceNumber when it is undefined', function (done) {
delete invoice._options.adjustmentInvoiceNumber;
parser.parseString('<wrapper>' + invoice._generateXML() + '</wrapper>', function (err, result) {
expect(result.wrapper).to.not.have.deep.property('fejlec.helyesbitettSzamlaszam');
done();
});
});

it('should throw an error when adjustmentInvoiceNumber is an empty string', function () {
expect(() => {
invoice._options.adjustmentInvoiceNumber = '';
invoice._generateXML();
}).to.throw();
ert78gb marked this conversation as resolved.
Show resolved Hide resolved
});

it('should throw an error when adjustmentInvoiceNumber is not a string', function () {
const invalidTypes = [new Date(), 123, true];
ert78gb marked this conversation as resolved.
Show resolved Hide resolved
invalidTypes.forEach(type => {
expect(() => {
invoice._options.adjustmentInvoiceNumber = type;
invoice._generateXML();
}).to.throw();
ert78gb marked this conversation as resolved.
Show resolved Hide resolved
});
});

it('should not throw an error when adjustmentInvoiceNumber is a non-empty string', function () {
expect(() => {
invoice._options.adjustmentInvoiceNumber = '12345';
invoice._generateXML();
}).to.not.throw();
});

it('should include adjustmentInvoiceNumber when it is a non-empty string', function (done) {
invoice._options.adjustmentInvoiceNumber = '12345';
parser.parseString('<wrapper>' + invoice._generateXML() + '</wrapper>', function (err, result) {
expect(result.wrapper.fejlec[0].helyesbitettSzamlaszam[0]).to.equal('12345');
done();
});
});

});
// END - New test suite for adjustmentInvoiceNumber property

});
});
});