Constructor
new Invoice(taxpayer, customer)
- Source:
Example
invoice = new Invoice(taxpayer, customer)
invoice.setIssueDate(new Date("13-Sep-2026 UTC"))
invoice.setCurrencyId("PEN")
invoice.setTypeCode(1)
invoice.setSerie("F000")
invoice.setNumeration(19931105)
invoice.setOrderReference("kar01")
invoice.setOrderReferenceText("Exchanging money")
invoice.addItem(product)
invoice.addDocumentReference(additionalDocumentReference)
invoice.toXml()
await invoice.finalize(subtle)
Parameters:
| Name | Type | Description |
|---|---|---|
taxpayer |
Taxpayer | Taxpayer with all their details ready. |
customer |
Person | Generic person. |
Extends
Methods
addDocumentReference(documentReference)
- Description:
Adds a document reference to the invoice.
If the document reference is a prepaid payment reference, it will also add a charge to the invoice.
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
documentReference |
module:receipt~DocumentReference |
addItem(item, withoutCalculationopt) → {void}
- Description:
Adds an item to the collection or cart.
- Source:
- Overrides:
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
item |
module:receipt~Item | The item to be added. |
||
withoutCalculation |
boolean |
<optional> |
false
|
If true, skips recalculating totals after adding the item. |
Returns:
This function does not return a value.
- Type
- void
clearItems()
- Description:
Clear all items setting the array of items to empty.
- Source:
- Overrides:
clearOrderReference()
- Description:
Empty reference identity and its description.
- Source:
createXmlWrapper()
- Description:
Create new XML document.
- Source:
- Overrides:
(async) createZip(type, xmlStringopt) → {Promise.<any>}
- Description:
Create a ZIP file containing XML. Default type is base64.
- Source:
- Overrides:
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
type |
"base64" | "string" | "text" | "binarystring" | "array" | "uint8array" | "arraybuffer" | "blob" | "nodebuffer" |
base64
|
according JSZip API. |
|
xmlString |
string |
<optional> |
that is raw XML. |
Returns:
A ZIP file containing XML.
- Type
- Promise.<any>
(async) declare(zipStream) → {Promise.<string>}
- Description:
Declare the document sending it to server.
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
zipStream |
string | The zip file. |
Returns:
The proof of the document.
- Type
- Promise.<string>
(async) finalize(cryptoSubtle, canonMethod)
- Description:
Finalize the document signing it.
- Source:
- Overrides:
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
cryptoSubtle |
SubtleCrypto | The crypto subtle to use for signing. |
|
canonMethod |
string |
c14n
|
The canonicalization method to use for signing. |
fromXml()
- Source:
- Overrides:
Returns:
xmlDoc parsed
getCurrencyId() → {string}
- Source:
- Overrides:
Returns:
- Type
- string
getCustomer() → {module:person~Person}
- Source:
- Overrides:
Returns:
The customer set in constructor.
- Type
- module:person~Person
getCustomizationId() → {string}
- Source:
- Overrides:
Returns:
The customization ID.
- Type
- string
getHash() → {string}
- Source:
- Overrides:
Returns:
The cryptographic hash of the document.
- Type
- string
getId(withType, compacted) → {string}
- Description:
Format serie and number: F000-00000001
- Source:
- Overrides:
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
withType |
boolean |
false
|
Include type code. |
compacted |
boolean |
false
|
Compact format. |
Returns:
- Type
- string
getIssueDate() → {Date}
- Source:
- Overrides:
Returns:
The issue date of the document.
- Type
- Date
getNumeration() → {number}
- Source:
- Overrides:
Returns:
The numeration of the document.
- Type
- number
getOperationAmount(index) → {number}
- Description:
Access to operation amounts array stored in 4 cells.
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
index |
number | from 0 to 3 |
Returns:
- Value of operation amount.
- Type
- number
getQrData() → {string}
- Description:
Gets the QR data that is header of this document separated by pipes.
RUC|Serie|Numero|Igv|Total|Fecha|TipoDoc|NumDoc
- Source:
- Overrides:
Returns:
- Type
- string
getSerie() → {string}
- Source:
- Overrides:
Returns:
The serie of the document.
- Type
- string
getShareableAmount()
- Description:
Performs substraction payableAmount with detractionAmount.
- Source:
getTaxpayer() → {module:person~Taxpayer}
- Source:
- Overrides:
Returns:
The taxpayer set in constructor.
getTypeCode(withFormat) → {number|string}
- Source:
- Overrides:
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
withFormat |
boolean |
false
|
Format the type code. |
Returns:
The type code of the document.
- Type
- number | string
getUblVersion() → {string}
- Source:
- Overrides:
Returns:
The UBL version.
- Type
- string
(async) handleProof(zipStream, isBase64, compacted) → {Promise.<Array.<(number|string)>>}
- Description:
Handle answer that is in a ZIP file.
- Source:
- Overrides:
Example
Handling CDR
const [cdrCode, cdrDescription] = await invoice.handleProof(serverZipStream)
if (cdrCode === 0) {
console.log("OK", cdrDescription)
}
else {
console.log(cdrDescription)
}
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
zipStream |
string | The zip file. |
|
isBase64 |
boolean |
true
|
Whether the zip file is base64. |
compacted |
boolean |
false
|
Whether the document is compacted. |
Returns:
Code and description in the proof of the document.
- Type
- Promise.<Array.<(number|string)>>
hasDetraction() → {boolean}
- Description:
Checks if detraction is applicable
- Source:
Returns:
True if detraction is applicable, false otherwise.
- Type
- boolean
recalcMounts()
- Description:
Use it if maybe you have edited an item or removed.
- Source:
- Overrides:
recalcWithDiscountFactor()
- Description:
After setting discount exists factor so recalc setting all amounts.
NOTE: This currently only considers the main discount (code 02).
- Source:
removeItem(index)
- Description:
Recreate items array without an item.
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
index |
number | The index of the item to remove. |
removeShare(index)
- Description:
Recreate shares array without a share.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
index |
in array. |
setCurrencyId(cid)
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
cid |
string | ISO 4217 currency code. |
setDetraction()
- Description:
Create detraction object with given percentage.
- Source:
setDueDate(date)
- Description:
Empty hook to be overridden by subclasses. Invoice overrides it.
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date | The due date. |
setHash(hash)
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
hash |
string | The cryptographic hash of the document. |
setId(serie, numeration)
- Description:
Set serie and number at once.
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
serie |
string | The serie of the document. |
numeration |
number | The numeration of the document. |
setIssueDate(date)
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date | The issue date of the document. |
setName(name)
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string | The name of the document. |
setNumeration(number)
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
number |
number | The numeration of the document from 1 to 99999999. |
setOperationAmount(index, amount)
- Description:
Store an operation amount in cell of the array.
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
index |
number | from 0 to 3 |
amount |
number | Value of operation amount. |
setOrderReference(reference)
- Description:
Set order reference identity.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
reference |
string | Order reference identity up to 20 characters without spaces. |
setSerie(serie)
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
serie |
string | The serie of the document with 4 characters. |
setTaxpayer(taxpayer)
- Description:
Replace the taxpayer.
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
taxpayer |
module:person~Taxpayer | The new taxpayer. |
setTypeCode(code)
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
code |
number | The type code of the document. |
setUblVersion(ublVersion)
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
ublVersion |
string |
(async) sign(cryptoSubtle, canonMethod)
- Source:
- Overrides:
- Deprecated:
- Use finalize() instead for better performance and architecture.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
cryptoSubtle |
SubtleCrypto | The crypto subtle to use for signing. |
|
canonMethod |
string |
c14n
|
The canonicalization method to use for signing. |
toString() → {string}
- Description:
Convert the document to XML string.
It includes signature node if the document was finalized.
- Source:
- Overrides:
Returns:
The XML string.
- Type
- string
unsetId()
- Description:
Unset serie and number to set an anonymous document.
- Source:
- Overrides:
validate()
- Description:
Check if everything can be processed.
It does some calculations
- Source:
(async) validateXmlWithXsd(mainXsdContent, importedXsdContents) → {Promise.<boolean>}
- Description:
Validate own XML against XSD.
- Source:
- Overrides:
Parameters:
| Name | Type | Description |
|---|---|---|
mainXsdContent |
string | Main XSD content. |
importedXsdContents |
Array.<string> | Array of imported XSD contents. |
Returns:
- false if there are errors.
- Type
- Promise.<boolean>