myinvois-sdk
Version:
TypeScript SDK for interacting with the Malaysia e-invoicing system (MyInvois) API
179 lines (178 loc) • 5.36 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InvoiceLine = void 0;
const base_model_1 = require("./base-model");
const item_1 = require("./item");
const price_1 = require("./price");
const tax_total_1 = require("./tax-total");
/**
* Represents an invoice line
*/
class InvoiceLine extends base_model_1.BaseModel {
constructor() {
super(...arguments);
this._id = 0;
this._quantity = 0;
this._unitCode = 'C62'; // Default unit code
this._lineExtensionAmount = 0;
this._discountRate = 0;
this._discountAmount = 0;
this._item = new item_1.Item();
this._price = new price_1.Price();
this._taxTotal = new tax_total_1.TaxTotal();
}
// Getter and Setter for id
get id() {
return this._id;
}
set id(value) {
this._id = value;
}
// Getter and Setter for quantity
get quantity() {
return this._quantity;
}
set quantity(value) {
this._quantity = value;
this.updateCalculations();
}
// Getter and Setter for unitCode
get unitCode() {
return this._unitCode;
}
set unitCode(value) {
this._unitCode = value;
}
// Getter and Setter for lineExtensionAmount
get lineExtensionAmount() {
return this._lineExtensionAmount;
}
// Getter and Setter for discountRate
get discountRate() {
return this._discountRate;
}
set discountRate(value) {
this._discountRate = value;
this.updateCalculations();
}
// Getter and Setter for discountAmount
get discountAmount() {
return this._discountAmount;
}
// Getter and Setter for item
get item() {
return this._item;
}
set item(value) {
this._item = value;
}
// Getter and Setter for price
get price() {
return this._price;
}
set price(value) {
this._price = value;
this.updateCalculations();
}
// Getter and Setter for taxTotal
get taxTotal() {
return this._taxTotal;
}
set taxTotal(value) {
this._taxTotal = value;
}
/**
* Updates the calculations for the invoice line
*/
updateCalculations() {
// Calculate line amount before discount
const grossAmount = this._quantity * this._price.priceAmount;
// Calculate discount amount
this._discountAmount = grossAmount * this._discountRate;
// Calculate line amount after discount
this._lineExtensionAmount = grossAmount - this._discountAmount;
// Update tax calculation based on tax rate
const taxRate = this._taxTotal.taxRate;
const taxAmount = this._lineExtensionAmount * (taxRate / 100);
// Update tax total
this._taxTotal.taxableAmount = this._lineExtensionAmount;
this._taxTotal.taxAmount = taxAmount;
}
/**
* Sets the tax rate and updates calculations
* @param taxRate The tax rate to set
* @param taxType The tax type (e.g., 'S', 'Z', 'E', etc.)
* @param taxExemptionReason The reason for tax exemption (if applicable)
*/
setTax(taxRate, taxType, taxExemptionReason) {
this._taxTotal.taxRate = taxRate;
this._taxTotal.taxType = taxType;
if (taxExemptionReason) {
this._taxTotal.taxExemptionReason = taxExemptionReason;
}
this.updateCalculations();
}
/**
* Converts the invoice line to a JSON representation
*/
toJSON() {
return {
"ID": [
{
"_": this._id.toString()
}
],
"InvoicedQuantity": [
{
"_": this._quantity,
"unitCode": this._unitCode
}
],
"LineExtensionAmount": [
{
"_": this._lineExtensionAmount,
"currencyID": "MYR"
}
],
"AllowanceCharge": [
{
"ChargeIndicator": [
{
"_": false
}
],
"AllowanceChargeReason": [
{
"_": "Discount"
}
],
"MultiplierFactorNumeric": this._discountRate > 0 ? [
{
"_": this._discountRate
}
] : null,
"Amount": [
{
"_": this._discountAmount,
"currencyID": "MYR"
}
]
}
],
"TaxTotal": [this._taxTotal.toLineJSON()],
"Item": [this._item.toJSON()],
"Price": [this._price.toJSON()],
"ItemPriceExtension": [
{
"Amount": [
{
"_": this._lineExtensionAmount,
"currencyID": "MYR"
}
]
}
]
};
}
}
exports.InvoiceLine = InvoiceLine;