fecr
Version:
Modulo de comprobantes electrónicos para el API del Ministerio de Hacienda de Costa Rica versión 4.3
60 lines (50 loc) • 1.64 kB
JavaScript
"use strict";
var Voucher = require("./voucher.js");
var utils = require("./utils.js");
class Purchase extends Voucher {
constructor(purchase) {
super(purchase);
this.rootTag = "FacturaElectronicaCompra";
this.namespaces = {
"xmlns": "https://cdn.comprobanteselectronicos.go.cr/xml-schemas/v4.3/facturaElectronicaCompra",
"xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
};
this.voucherType = 8;
if (purchase.sequence) {
this.sequence = purchase.sequence;
} else {
this.generateSequence();
}
if (purchase.key) {
this.key = purchase.key;
} else {
this.generateKey();
}
}
generateKey() {
var country = "506";
var day = utils.zfill(this.date.date(), 2);
var month = utils.zfill(this.date.month() + 1, 2);
var year = utils.zfill(this.date.year() - 2000, 2);
var idNumber = utils.zfill(this.receiver.id, 12);
var type = utils.zfill(this.situation, 1);
var securityCode = utils.zfill(this.securityCode, 8); // eslint-disable-next-line max-len
this.key = country + day + month + year + idNumber + this.sequence + type + securityCode;
}
send() {
return new Promise((resolve, reject) => {
this.signedXmlBase64 = Buffer.from(this.buildXml()).toString("base64");
this.sign().then(() => this.api.send(this.getPayload())).then(() => {
resolve({
data: this.data,
xml: this.signedXmlBase64
});
}).catch(error => {
reject(error);
});
/* */
});
}
}
module.exports = Purchase;