Files
bodyshop/server/integrations/partsManagement/endpoints/lib/extractPartsTaxRates.js

73 lines
1.9 KiB
JavaScript

const KNOWN_PART_RATE_TYPES = [
"PAA",
"PAC",
"PAG",
"PAL",
"PAM",
"PAN",
"PAO",
"PAP",
"PAR",
"PAS",
"PASL",
"CCC",
"CCD",
"CCF",
"CCM",
"CCDR"
];
/**
* Extracts and processes parts tax rates from profile info.
* @param {object} profile - The ProfileInfo object from XML.
* @returns {object} The parts tax rates object.
*/
//TODO: Major validation would be required on this - EMS files are inconsistent with things like 5% being passed as 5.0 or .05.
const extractPartsTaxRates = (profile = {}) => {
const rateInfos = Array.isArray(profile.RateInfo) ? profile.RateInfo : [profile.RateInfo || {}];
const partsTaxRates = {};
/**
* In this context, r.RateType._ accesses the property named _ on the RateType object.
* This pattern is common when handling data parsed from XML, where element values are stored under the _ key. So,
* _ aligns to the actual value/content of the RateType field when RateType is an object (not a string).
*/
for (const r of rateInfos) {
const rateTypeRaw =
typeof r?.RateType === "string"
? r.RateType
: typeof r?.RateType === "object" && r?.RateType._
? r.RateType._
: "";
const rateType = (rateTypeRaw || "").toUpperCase();
if (!KNOWN_PART_RATE_TYPES.includes(rateType)) continue;
const taxInfo = r.TaxInfo;
const taxTier = taxInfo?.TaxTierInfo;
let percentage = parseFloat(taxTier?.Percentage ?? "NaN");
if (isNaN(percentage)) {
const tierRate = Array.isArray(r.RateTierInfo) ? r.RateTierInfo[0]?.Rate : r.RateTierInfo?.Rate;
percentage = parseFloat(tierRate ?? "NaN");
}
if (!isNaN(percentage)) {
partsTaxRates[rateType] = {
prt_discp: 0,
prt_mktyp: false,
prt_mkupp: 0,
prt_tax_in: true,
prt_tax_rt: percentage / 100
};
}
}
return partsTaxRates;
};
module.exports = {
extractPartsTaxRates,
KNOWN_PART_RATE_TYPES
};