falcotura-atv-sdk
Version:
Librería (SDK) de Javascript/NodeJS para acceder al API de Administración Tributaria Virtual (ATV) del Ministerio de Hacienda.
149 lines • 6.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Document = void 0;
class Document {
constructor(props) {
this.props = props;
}
get clave() {
return this.props.clave.value;
}
get fullConsecutive() {
return this.props.fullConsecutive.value;
}
get providerId() {
return this.props.providerId;
}
get activityCode() {
return this.props.emitter.activityCode;
}
get issueDate() {
return this.props.issueDate;
}
get emitter() {
return this.props.emitter;
}
get receiver() {
return this.props.receiver;
}
get orderLines() {
return this.props.orderLines;
}
get conditionSale() {
return this.props.conditionSale;
}
get deadlineCredit() {
return this.props.deadlineCredit;
}
get paymentMethod() {
return this.props.paymentMethod;
}
get others() {
return this.props.others;
}
get summaryInvoice() {
var _a, _b;
if (this.props.summaryInvoice) {
return this.props.summaryInvoice;
}
const servicesLinesSum = this.sumServicesLines();
const merchandiseLinesSum = this.sumMerchandiseLines();
const allLinesSum = this.sumOrderLines();
// Lógica para calcular los totales del resumen
const totalEncumberedServices = servicesLinesSum.totalAmount - servicesLinesSum.totalExempt - servicesLinesSum.totalExonerated - servicesLinesSum.totalNonTaxable;
const totalEncumberedMerchandise = merchandiseLinesSum.totalAmount - merchandiseLinesSum.totalExempt - merchandiseLinesSum.totalExonerated - merchandiseLinesSum.totalNonTaxable;
const totalEncumbered = totalEncumberedServices + totalEncumberedMerchandise;
const totalExempt = allLinesSum.totalExempt;
const totalExonerated = allLinesSum.totalExonerated;
const totalNonTaxable = allLinesSum.totalNonTaxable;
const totalSale = totalEncumbered + totalExempt + totalExonerated + totalNonTaxable;
const totalDiscounts = 0; // Asumimos 0 si no se calcula
const totalNetSale = totalSale - totalDiscounts;
const totalTaxes = allLinesSum.totalTaxes;
const totalVoucher = totalNetSale + totalTaxes;
return {
currency: {
code: (_a = this.props.currencyCode) !== null && _a !== void 0 ? _a : 'CRC',
exchangeRate: (_b = this.props.exchangeRate) !== null && _b !== void 0 ? _b : '1'
},
totalExemptServices: servicesLinesSum.totalExempt,
totalEncumberedServices,
totalExonerated,
totalTaxedServices: servicesLinesSum.totalTaxes,
totalTaxes,
totalDiscounts: 0,
totalEncumberedMerchandise,
totalTaxed: allLinesSum.totalTaxes,
totalExemptMerchandise: merchandiseLinesSum.totalExempt,
totalExempt,
totalNetSale,
totalEncumbered,
totalTaxBreakdown: [], // La lógica del desglose de impuestos está en `billDocToAtv.ts`
totalSale,
totalVoucher,
// Se añaden los campos para 'No Sujeto' que estaban causando errores
totalNonTaxable,
totalNonTaxableServices: servicesLinesSum.totalNonTaxable,
totalNonTaxableMerchandise: merchandiseLinesSum.totalNonTaxable
};
}
get referenceInformation() {
return this.props.referenceInformation;
}
isAService(orderLine) {
const servicesMeasurementUnits = ['Sp', 'St', 'Spe'];
return servicesMeasurementUnits.includes(orderLine.measureUnit);
}
isTaxable(orderLine) {
if (!orderLine.tax || orderLine.tax.amount === undefined || orderLine.tax.amount === null) {
return false;
}
return orderLine.tax.code === '01' && orderLine.tax.amount > 0;
}
isExempt(orderLine) {
var _a;
// 32 El código 10, Tarifa Exenta Ley 9635, Articulo 8
return ((_a = orderLine.tax) === null || _a === void 0 ? void 0 : _a.rateCode) === '10';
}
isExonerated(orderLine) {
var _a;
return ((_a = orderLine.tax) === null || _a === void 0 ? void 0 : _a.rateCode) === '01';
}
isNonTaxable(orderLine) {
var _a;
// Un ítem es "No Sujeto" si su tax.code NO es '01' o si el monto del impuesto es 0.
// Esto cubrirá el caso donde un ítem con tax.code '01' y tarifa '0' sea considerado "No Sujeto".
return ((_a = orderLine.tax) === null || _a === void 0 ? void 0 : _a.rateCode) === '01';
}
sumLines(lines) {
return lines.reduce((previousValue, currentValue) => {
var _a, _b;
const totalAmount = previousValue.totalAmount + currentValue.totalAmount;
const totalTaxes = previousValue.totalTaxes + (this.isTaxable(currentValue) ? ((_b = (_a = currentValue.tax) === null || _a === void 0 ? void 0 : _a.amount) !== null && _b !== void 0 ? _b : 0) : 0);
const totalExempt = previousValue.totalExempt + (this.isExempt(currentValue) ? currentValue.totalAmount : 0);
const totalExonerated = previousValue.totalExonerated + (this.isExonerated(currentValue) ? currentValue.totalAmount : 0);
const totalNonTaxable = previousValue.totalNonTaxable + (this.isNonTaxable(currentValue) ? currentValue.totalAmount : 0);
return {
totalAmount,
totalTaxes,
totalExempt,
totalExonerated,
totalNonTaxable
};
}, { totalAmount: 0, totalTaxes: 0, totalExempt: 0, totalExonerated: 0, totalNonTaxable: 0 });
}
sumServicesLines() {
return this.sumLines(this.orderLines.filter(this.isAService));
}
sumMerchandiseLines() {
return this.sumLines(this.orderLines.filter((ol) => !this.isAService(ol)));
}
sumOrderLines() {
return this.sumLines(this.orderLines);
}
static create(props) {
return new Document(props);
}
}
exports.Document = Document;
//# sourceMappingURL=Document.js.map