Receipt

receipt~ Receipt

The Receipt class serves as the abstract foundation for all document types in the library.
It implements the core lifecycle of an electronic tax document (CPE), including identity management, XML structure initialization, digital signing, ZIP packaging and transmission to SUNAT endpoints.

Constructor

new Receipt(taxpayer, customer, name)

Source:
Parameters:
Name Type Description
taxpayer module:person~Taxpayer

The taxpayer.

customer module:person~Person

The customer.

name string

The name of the document.

Members

additionalDocumentReferences

Source:

contractDocumentReferences

Source:

despatchDocumentReferences

Source:

items

Source:

prepaidPaymentReferences

Source:

Methods

addDocumentReference(documentReference)

Description:
  • Add document reference according internal type code.
    Stores documents in its own array by its reference type.

Source:
Parameters:
Name Type Description
documentReference module:receipt~DocumentReference

addItem(item)

Source:
Parameters:
Name Type Description
item module:receipt~Item

The item to add.

clearItems()

Description:
  • Clear all items setting the array of items to empty.

Source:

createXmlWrapper()

Description:
  • Create new XML document.

Source:

(async) createZip(type, xmlStringopt) → {Promise.<any>}

Description:
  • Create a ZIP file containing XML. Default type is base64.

Source:
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:
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:
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(xmlContent)

Description:
  • Parse receipt header.

Source:
Parameters:
Name Type Description
xmlContent string

The XML content of the document.

Returns:

xmlDoc parsed.

getCustomer() → {module:person~Person}

Source:
Returns:

The customer set in constructor.

Type
module:person~Person

getCustomizationId() → {string}

Source:
Returns:

The customization ID.

Type
string

getHash() → {string}

Source:
Returns:

The cryptographic hash of the document.

Type
string

getId(withType, compacted) → {string}

Description:
  • Format serie and number: F000-00000001

Source:
Parameters:
Name Type Default Description
withType boolean false

Include type code.

compacted boolean false

Compact format.

Returns:
Type
string

getIssueDate() → {Date}

Source:
Returns:

The issue date of the document.

Type
Date

getNumeration() → {number}

Source:
Returns:

The numeration of the document.

Type
number

getSerie() → {string}

Source:
Returns:

The serie of the document.

Type
string

getTaxpayer() → {module:person~Taxpayer}

Source:
Returns:

The taxpayer set in constructor.

Type
module:person~Taxpayer

getTypeCode(withFormat) → {number|string}

Source:
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:
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:
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)>>

removeItem(index)

Description:
  • Recreate items array without an item.

Source:
Parameters:
Name Type Description
index number

The index of the item to remove.

setHash(hash)

Source:
Parameters:
Name Type Description
hash string

The cryptographic hash of the document.

setId(serie, numeration)

Description:
  • Set serie and number at once.

Source:
Parameters:
Name Type Description
serie string

The serie of the document.

numeration number

The numeration of the document.

setIssueDate(date)

Source:
Parameters:
Name Type Description
date Date

The issue date of the document.

setName(name)

Source:
Parameters:
Name Type Description
name string

The name of the document.

setNumeration(number)

Source:
Parameters:
Name Type Description
number number

The numeration of the document from 1 to 99999999.

setSerie(serie)

Source:
Parameters:
Name Type Description
serie string

The serie of the document with 4 characters.

setTaxpayer(taxpayer)

Description:
  • Replace the taxpayer.

Source:
Parameters:
Name Type Description
taxpayer module:person~Taxpayer

The new taxpayer.

setTypeCode(code)

Source:
Parameters:
Name Type Description
code number

The type code of the document.

setUblVersion(ublVersion)

Source:
Parameters:
Name Type Description
ublVersion string

(async) sign(cryptoSubtle, canonMethod)

Source:
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:
Returns:

The XML string.

Type
string

unsetId()

Description:
  • Unset serie and number to set an anonymous document.

Source:

(async) validateXmlWithXsd(mainXsdContent, importedXsdContents) → {Promise.<boolean>}

Description:
  • Validate own XML against XSD.

Source:
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>

(static) amountToWords(amount, junctor, tail, decimals) → {string}

Description:
  • Helper to convert an amount to words.

Source:
Parameters:
Name Type Default Description
amount number

The amount to convert to words.

junctor string

The junctor to use.

tail string

The tail to use.

decimals number 2

The number of decimals to use.

Returns:

The amount in words.

Type
string

(static) displayDate()

Description:
  • Helper to print a date.
    https://stackoverflow.com/a/41480350

Source:
Returns:

date as string in format yyyy-mm-dd.

(static) displayTime(date)

Description:
  • Helper to print a time of a date.

Source:
Parameters:
Name Type Description
date Date

The date to handle time.

Returns:

time as string in format HH:MM:SS

(static) removeCdataTag(cdata) → {string}

Description:
  • Helper to remove CDATA tags from a string.

Source:
Parameters:
Name Type Description
cdata string

The string to remove CDATA tags from.

Returns:

The string without CDATA tags.

Type
string