myinvois-sdk
Version:
TypeScript SDK for interacting with the Malaysia e-invoicing system (MyInvois) API
183 lines (182 loc) • 5.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaxTotal = void 0;
const base_model_1 = require("./base-model");
const tax_subtotal_1 = require("./tax-subtotal");
/**
* Represents tax total information
*/
class TaxTotal extends base_model_1.BaseModel {
constructor() {
super(...arguments);
this._taxAmount = 0;
this._taxableAmount = 0;
this._taxType = '';
this._taxRate = 0;
this._taxExemptionReason = '';
this._taxSubtotals = [];
}
// Getter and Setter for taxAmount
get taxAmount() {
return this._taxAmount;
}
set taxAmount(value) {
this._taxAmount = value;
}
// Getter and Setter for taxableAmount
get taxableAmount() {
return this._taxableAmount;
}
set taxableAmount(value) {
this._taxableAmount = value;
}
// Getter and Setter for taxType
get taxType() {
return this._taxType;
}
set taxType(value) {
this._taxType = value;
}
// Getter and Setter for taxRate
get taxRate() {
return this._taxRate;
}
set taxRate(value) {
this._taxRate = value;
}
// Getter and Setter for taxExemptionReason
get taxExemptionReason() {
return this._taxExemptionReason;
}
set taxExemptionReason(value) {
this._taxExemptionReason = value;
}
// Getter for taxSubtotals
get taxSubtotals() {
return this._taxSubtotals;
}
/**
* Add a tax subtotal
* @param subtotal The tax subtotal to add
*/
addTaxSubtotal(subtotal) {
this._taxSubtotals.push(subtotal);
}
/**
* Calculate tax totals based on invoice lines
* @param invoiceLines The invoice lines to calculate tax totals from
*/
calculateTotals(invoiceLines) {
// Group lines by tax type and rate
const taxGroups = new Map();
// Calculate tax for each line and group by tax type/rate
invoiceLines.forEach(line => {
const taxTotal = line.taxTotal;
const key = `${taxTotal.taxType}_${taxTotal.taxRate}`;
if (!taxGroups.has(key)) {
taxGroups.set(key, {
taxType: taxTotal.taxType,
taxRate: taxTotal.taxRate,
taxableAmount: 0,
taxAmount: 0,
exemptionReason: taxTotal.taxExemptionReason
});
}
const group = taxGroups.get(key);
group.taxableAmount += taxTotal.taxableAmount;
group.taxAmount += taxTotal.taxAmount;
});
// Clear existing subtotals
this._taxSubtotals = [];
// Create tax subtotals for each group
let totalTaxAmount = 0;
taxGroups.forEach((group) => {
const subtotal = new tax_subtotal_1.TaxSubtotal();
subtotal.taxableAmount = group.taxableAmount;
subtotal.taxAmount = group.taxAmount;
subtotal.taxType = group.taxType;
subtotal.taxRate = group.taxRate;
if (group.exemptionReason) {
subtotal.taxExemptionReason = group.exemptionReason;
}
this._taxSubtotals.push(subtotal);
totalTaxAmount += group.taxAmount;
});
// Set total tax amount
this._taxAmount = totalTaxAmount;
}
/**
* Converts the tax total to a JSON representation for the invoice level
*/
toJSON() {
return {
"TaxAmount": [
{
"_": this._taxAmount,
"currencyID": "MYR"
}
],
"TaxSubtotal": this._taxSubtotals.map(subtotal => subtotal.toJSON())
};
}
/**
* Converts the tax total to a JSON representation for the line level
*/
toLineJSON() {
return {
"TaxAmount": [
{
"_": this._taxAmount,
"currencyID": "MYR"
}
],
"TaxSubtotal": [
{
"TaxableAmount": [
{
"_": this._taxableAmount,
"currencyID": "MYR"
}
],
"TaxAmount": [
{
"_": this._taxAmount,
"currencyID": "MYR"
}
],
"Percent": [
{
"_": this._taxRate
}
],
"TaxCategory": [
{
"ID": [
{
"_": this._taxType
}
],
"TaxExemptionReason": this._taxType === 'E' && this._taxExemptionReason ? [
{
"_": this._taxExemptionReason
}
] : null,
"TaxScheme": [
{
"ID": [
{
"_": "OTH",
"schemeID": "UN/ECE 5153",
"schemeAgencyID": "6"
}
]
}
]
}
]
}
]
};
}
}
exports.TaxTotal = TaxTotal;