myinvois-sdk
Version:
TypeScript SDK for interacting with the Malaysia e-invoicing system (MyInvois) API
293 lines (292 loc) • 10.6 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Invoice = void 0;
const base_model_1 = require("./base-model");
const party_1 = require("./party");
const tax_total_1 = require("./tax-total");
const invoice_period_1 = require("./invoice-period");
const moment_timezone_1 = __importDefault(require("moment-timezone"));
/**
* Represents an invoice document
*/
class Invoice extends base_model_1.BaseModel {
constructor() {
super(...arguments);
// Document identification
this._id = '';
this._issueDate = new Date();
this._invoiceTypeCode = '';
this._documentCurrencyCode = 'MYR';
this._taxCurrencyCode = 'MYR';
// Parties
this._supplier = new party_1.Party();
this._customer = new party_1.Party();
// Document details
this._invoicePeriod = new invoice_period_1.InvoicePeriod();
this._additionalDocumentReferences = [];
// Monetary totals
this._lineExtensionAmount = 0;
this._taxExclusiveAmount = 0;
this._taxInclusiveAmount = 0;
this._allowanceTotalAmount = 0;
this._chargeTotalAmount = 0;
this._payableRoundingAmount = 0;
this._payableAmount = 0;
// Tax information
this._taxTotal = new tax_total_1.TaxTotal();
// Line items
this._invoiceLines = [];
}
// Getter and Setter for ID
get id() {
return this._id;
}
set id(value) {
this._id = value;
}
// Getter and Setter for issueDate
get issueDate() {
return this._issueDate;
}
set issueDate(value) {
this._issueDate = value;
}
// Getter and Setter for invoiceTypeCode
get invoiceTypeCode() {
return this._invoiceTypeCode;
}
set invoiceTypeCode(value) {
this._invoiceTypeCode = value;
}
// Getter and Setter for documentCurrencyCode
get documentCurrencyCode() {
return this._documentCurrencyCode;
}
set documentCurrencyCode(value) {
this._documentCurrencyCode = value;
}
// Getter and Setter for taxCurrencyCode
get taxCurrencyCode() {
return this._taxCurrencyCode;
}
set taxCurrencyCode(value) {
this._taxCurrencyCode = value;
}
// Getter and Setter for supplier
get supplier() {
return this._supplier;
}
set supplier(value) {
this._supplier = value;
}
// Getter and Setter for customer
get customer() {
return this._customer;
}
set customer(value) {
this._customer = value;
}
// Getter and Setter for invoicePeriod
get invoicePeriod() {
return this._invoicePeriod;
}
set invoicePeriod(value) {
this._invoicePeriod = value;
}
// Getter and Setter for additionalDocumentReferences
get additionalDocumentReferences() {
return this._additionalDocumentReferences;
}
set additionalDocumentReferences(value) {
this._additionalDocumentReferences = value;
}
// Method to add a document reference
addDocumentReference(reference) {
this._additionalDocumentReferences.push(reference);
}
// Getters and Setters for monetary totals
get lineExtensionAmount() {
return this._lineExtensionAmount;
}
set lineExtensionAmount(value) {
this._lineExtensionAmount = value;
}
get taxExclusiveAmount() {
return this._taxExclusiveAmount;
}
set taxExclusiveAmount(value) {
this._taxExclusiveAmount = value;
}
get taxInclusiveAmount() {
return this._taxInclusiveAmount;
}
set taxInclusiveAmount(value) {
this._taxInclusiveAmount = value;
}
get allowanceTotalAmount() {
return this._allowanceTotalAmount;
}
set allowanceTotalAmount(value) {
this._allowanceTotalAmount = value;
}
get chargeTotalAmount() {
return this._chargeTotalAmount;
}
set chargeTotalAmount(value) {
this._chargeTotalAmount = value;
}
get payableRoundingAmount() {
return this._payableRoundingAmount;
}
set payableRoundingAmount(value) {
this._payableRoundingAmount = value;
}
get payableAmount() {
return this._payableAmount;
}
set payableAmount(value) {
this._payableAmount = value;
}
// Getter and Setter for taxTotal
get taxTotal() {
return this._taxTotal;
}
set taxTotal(value) {
this._taxTotal = value;
}
// Getter and Setter for invoiceLines
get invoiceLines() {
return this._invoiceLines;
}
set invoiceLines(value) {
this._invoiceLines = value;
}
// Method to add an invoice line
addInvoiceLine(line) {
this._invoiceLines.push(line);
}
/**
* Calculate totals based on invoice lines
*/
calculateTotals() {
// Calculate line extension amount (sum of line amounts)
this._lineExtensionAmount = this._invoiceLines.reduce((sum, line) => sum + line.lineExtensionAmount, 0);
// Calculate tax totals
this._taxTotal.calculateTotals(this._invoiceLines);
// Set other amounts
this._taxExclusiveAmount = this._lineExtensionAmount;
this._taxInclusiveAmount = this._taxExclusiveAmount + this._taxTotal.taxAmount;
this._payableAmount = this._taxInclusiveAmount + this._payableRoundingAmount;
}
/**
* Converts the invoice to a JSON representation
*/
toJSON() {
return {
"_D": "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2",
"_A": "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2",
"_B": "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2",
"Invoice": [
{
"ID": [
{
"_": this._id
}
],
"IssueDate": [
{
"_": (0, moment_timezone_1.default)(this._issueDate).format('YYYY-MM-DD')
}
],
"IssueTime": [
{
"_": (0, moment_timezone_1.default)(this._issueDate).format('HH:mm:ss') + 'Z'
}
],
"InvoiceTypeCode": [
{
"_": this._invoiceTypeCode,
"listVersionID": "1.1"
}
],
"DocumentCurrencyCode": [
{
"_": this._documentCurrencyCode
}
],
"TaxCurrencyCode": [
{
"_": this._taxCurrencyCode
}
],
"InvoicePeriod": this._invoicePeriod && typeof this._invoicePeriod.toJSON === 'function' ? this._invoicePeriod.toJSON() : [],
"AdditionalDocumentReference": this._additionalDocumentReferences.map(ref => ref.toJSON()),
"AccountingSupplierParty": [
{
"Party": [this._supplier && typeof this._supplier.toJSON === 'function' ? this._supplier.toJSON() : {}]
}
],
"AccountingCustomerParty": [
{
"Party": [this._customer && typeof this._customer.toJSON === 'function' ? this._customer.toJSON() : {}]
}
],
"TaxTotal": [this._taxTotal && typeof this._taxTotal.toJSON === 'function' ? this._taxTotal.toJSON() : {}],
"LegalMonetaryTotal": [
{
"LineExtensionAmount": [
{
"_": this._lineExtensionAmount,
"currencyID": this._documentCurrencyCode
}
],
"TaxExclusiveAmount": [
{
"_": this._taxExclusiveAmount,
"currencyID": this._documentCurrencyCode
}
],
"TaxInclusiveAmount": [
{
"_": this._taxInclusiveAmount,
"currencyID": this._documentCurrencyCode
}
],
"AllowanceTotalAmount": [
{
"_": this._allowanceTotalAmount,
"currencyID": this._documentCurrencyCode
}
],
"ChargeTotalAmount": [
{
"_": this._chargeTotalAmount,
"currencyID": this._documentCurrencyCode
}
],
"PayableRoundingAmount": [
{
"_": this._payableRoundingAmount,
"currencyID": this._documentCurrencyCode
}
],
"PayableAmount": [
{
"_": this._payableAmount,
"currencyID": this._documentCurrencyCode
}
]
}
],
"InvoiceLine": this._invoiceLines && Array.isArray(this._invoiceLines) ?
this._invoiceLines.map(line => line && typeof line.toJSON === 'function' ? line.toJSON() : {}) :
[]
}
]
};
}
}
exports.Invoice = Invoice;