@teaploy/megaprint
Version:
Megaprint npm integration
66 lines (65 loc) • 2.23 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Item = void 0;
const tax_1 = require("./tax");
const lodash_1 = require("lodash");
var TypeService;
(function (TypeService) {
TypeService["goods"] = "B";
TypeService["service"] = "S";
})(TypeService || (TypeService = {}));
class Item {
constructor(quantity, metric, description, type, unitPrice, discount, calculateTaxes = ['IVA']) {
if (type !== 'B' && type !== 'S') {
throw new Error('Invalid item type');
}
this.quantity = quantity;
this.metric = metric;
this.type = type;
this.description = description;
this.unitPrice = unitPrice;
this.discount = discount;
this.price = this.quantity * this.unitPrice;
this.total = this.price - this.discount;
this.taxes = calculateTaxes.map((taxName, index) => {
switch (taxName) {
case 'IVA': {
const amountTaxable = this.price / 1.12;
const tax = amountTaxable * 0.12;
return new tax_1.Tax(taxName, (index += 1), parseFloat(amountTaxable.toFixed(2)), parseFloat(tax.toFixed(2)));
}
default: {
throw new Error('Invalid Tax');
}
}
});
}
getTaxes() {
return lodash_1.chain(this.taxes)
.groupBy('shortName')
.map((value, key) => ({
tax: key,
total: value.reduce((total, tax) => total + tax.amountTax, 0),
}))
.value();
}
toDte(line) {
return {
$: {
BienOServicio: this.type,
NumeroLinea: line,
},
'dte:Cantidad': this.quantity,
'dte:UnidadMedida': this.metric,
'dte:Descripcion': this.description,
'dte:PrecioUnitario': this.unitPrice,
'dte:Precio': this.price,
'dte:Descuento': this.discount,
'dte:Impuestos': this.taxes.map((tax) => ({
'dte:Impuesto': tax.toDte(),
})),
'dte:Total': this.total,
};
}
}
exports.Item = Item;