UNPKG

lbx-invoice

Version:

Provides functionality around generating invoices.

111 lines 5.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InvoiceCalcUtilities = void 0; const big_number_utilities_1 = require("./big-number.utilities"); /** * Provides functionality around calculating invoices/prices. */ class InvoiceCalcUtilities { /** * Gets the total price of an item before tax. * @param item - The invoice item to get the price from. * @returns The amount of the item times its price. */ static getItemTotalPriceBeforeTax(item) { return big_number_utilities_1.BigNumberUtilities.multiply(item.amount, item.price); } /** * Gets the total price of an item after tax. * @param item - The invoice item to get the price from. * @returns The total price before tax times 1 + tax/100. */ static getItemTotalPriceAfterTax(item) { const itemTotalBeforeTax = InvoiceCalcUtilities.getItemTotalPriceBeforeTax(item); const taxMultiplier = big_number_utilities_1.BigNumberUtilities.add(1, big_number_utilities_1.BigNumberUtilities.divide(item.vat.rate, 100)); return big_number_utilities_1.BigNumberUtilities.multiply(itemTotalBeforeTax, taxMultiplier); } /** * Gets the total tax for the given item. * @param item - The item to get the total tax from. * @returns The total price before tax times tax/100. */ static getItemTotalTax(item) { const itemTotalBeforeTax = InvoiceCalcUtilities.getItemTotalPriceBeforeTax(item); return big_number_utilities_1.BigNumberUtilities.multiply(itemTotalBeforeTax, big_number_utilities_1.BigNumberUtilities.divide(item.vat.rate, 100)); } /** * Gets the total price of an invoice before tax. * @param invoice - The invoice to get the price from. * @returns The addition of the total price before tax of all items in the invoice. If this would be negative this returns 0. */ static getTotalBeforeTax(invoice) { let res = big_number_utilities_1.BigNumberUtilities.new(0); for (const item of invoice.items) { res = big_number_utilities_1.BigNumberUtilities.add(res, InvoiceCalcUtilities.getItemTotalPriceBeforeTax(item)); } return res.lt(0) ? big_number_utilities_1.BigNumberUtilities.new(0) : res; } /** * Gets the total price of an invoice after tax. * @param invoice - The invoice to get the price from. * @returns The addition of the total price before tax and the total tax of the invoice. */ static getTotalAfterTax(invoice) { const totalBeforeTax = InvoiceCalcUtilities.getTotalBeforeTax(invoice); const totalTax = InvoiceCalcUtilities.getTotalTax(invoice); return big_number_utilities_1.BigNumberUtilities.add(totalBeforeTax, totalTax); } /** * Gets the total tax of the invoice. * @param invoice - The invoice to get the total tax from. * @returns The addition of the tax of all items of the invoice. */ static getTotalTax(invoice) { let res = big_number_utilities_1.BigNumberUtilities.new(0); for (const item of invoice.items) { res = big_number_utilities_1.BigNumberUtilities.add(res, InvoiceCalcUtilities.getItemTotalTax(item)); } return res; } /** * Gets the total tax for a specific tax group (eg. 19%). * @param invoice - The invoice to get the total tax from. * @param group - The tax group for which the total tax should be calculated. * @param groupOnlyByRate - Whether or not VAT groups are only determined by rate and not by category code. * @returns The addition of the tax of all items that are in the provided tax group. */ static getTotalTaxForTaxGroup(invoice, group, groupOnlyByRate) { const itemsInTaxGroup = this.getItemsForTaxGroup(invoice, group, groupOnlyByRate); let res = big_number_utilities_1.BigNumberUtilities.new(0); for (const item of itemsInTaxGroup) { res = big_number_utilities_1.BigNumberUtilities.add(res, InvoiceCalcUtilities.getItemTotalTax(item)); } return res; } /** * Gets the total price of an invoice before tax. * @param invoice - The invoice to get the price from. * @param group - The tax group to get the total before from. * @param groupOnlyByRate - Whether or not VAT groups are only determined by rate and not by category code. * @returns The addition of the total price before tax of all items in the invoice. If this would be negative this returns 0. */ static getTotalBeforeTaxForTaxGroup(invoice, group, groupOnlyByRate) { const itemsInTaxGroup = this.getItemsForTaxGroup(invoice, group, groupOnlyByRate); let res = big_number_utilities_1.BigNumberUtilities.new(0); for (const item of itemsInTaxGroup) { res = big_number_utilities_1.BigNumberUtilities.add(res, InvoiceCalcUtilities.getItemTotalPriceBeforeTax(item)); } return res.lt(0) ? big_number_utilities_1.BigNumberUtilities.new(0) : res; } static getItemsForTaxGroup(invoice, group, groupOnlyByRate) { const itemsInTaxGroup = invoice.items.filter(item => { if (groupOnlyByRate) { return item.vat.rate === group.rate; } return item.vat.categoryCode === group.categoryCode && item.vat.rate === group.rate; }); return itemsInTaxGroup; } } exports.InvoiceCalcUtilities = InvoiceCalcUtilities; //# sourceMappingURL=invoice-calc.utilities.js.map