DigitalConsumer.ISS/src/helpers/price-calculator.js
2024-05-07 12:12:08 -05:00

31 lines
1.2 KiB
JavaScript

/**
* Returns the price of a single line item. Will include sales tax if includeTax is true
* @param {object} lineItem line item to price
* @returns {number} price of the line item
*/
export function getPriceOfLineItem(lineItem) {
let price = (lineItem.kitPrice ?? 0) + (lineItem.laborAmount ?? 0) + (lineItem.sellingPrice ?? 0);
if (lineItem.childParts && lineItem.childParts.length !== 0) {
// eslint-disable-next-line no-use-before-define
price += getPriceOfLineItems(lineItem.childParts);
}
return price;
}
/**
* Returns the price for the given array of line items. Will include sales tax if includeTax is true
* @param {Array} lineItems array of line items to be priced
* @returns {number} price of the lines items
*/
export function getPriceOfLineItems(lineItems) {
return lineItems?.reduce((accumulator, lineItem) => accumulator + getPriceOfLineItem(lineItem), 0) ?? 0;
}
/**
* Returns the sales tax for the given array of line items
* @param {Array} lineItems array of line items to get the sales tax for
* @returns {number} sales tax of the line items
*/
export function getTaxOfLineItems(lineItems) {
return lineItems?.reduce((accumulator, lineItem) => accumulator + (lineItem.salesTax ?? 0), 0) ?? 0;
}