diff --git a/src/layouts/service-location/shop-question/shop-question.spec.js b/src/layouts/service-location/shop-question/shop-question.spec.js index b93728110..7118ab985 100644 --- a/src/layouts/service-location/shop-question/shop-question.spec.js +++ b/src/layouts/service-location/shop-question/shop-question.spec.js @@ -452,7 +452,7 @@ describe("shop-question.vue", () => { // Assert expect(wrapper.vm.answers.length).toBe(6); - console.log(wrapper.vm.answers); + expect(wrapper.vm.answers).toEqual([ { buttonBodyCopy: "4403 Executive Pkwy, Westerville, OH 43081", diff --git a/src/store/index.js b/src/store/index.js index 0c5fc252f..c096fa8fd 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -2605,12 +2605,69 @@ export function mapTaxedAvailableLineItemsToStoreFormat(availableLineItems, stor for (let [category, lineItemsInCategory] of Object.entries(lineItems)) { lineItemsInCategory = lineItemsInCategory ?? []; - for (let lineItemIndex = 0; lineItemIndex < lineItemsInCategory.length; lineItemIndex++) { - const availableLineItem = availableLineItems.find( - (ali) => ali.partNumber == lineItemsInCategory[lineItemIndex].partNumber + if (category == "supportingItems") { + // if the category is supporting items we need to filter out the items that aren't repair chips + const nonRepairChipSupportItemsLineItems = lineItemsInCategory.filter( + (lineItem) => lineItem.partNumber != "WSREPAIR" ); - if (availableLineItem) { - lineItemsInCategory[lineItemIndex].salesTax = availableLineItem.salesTax; + + /* + Because repair chips all have the same part number but different prices based on the quantity, + we have to sort the store line items and available line items by descending labor amount in order to + map the tax correctly to each repair chip + */ + // get the supporting items that ARE repair chips and sort them by descending labor amount + let repairChipLineItems = lineItemsInCategory.filter( + (lineItem) => lineItem.partNumber == "WSREPAIR" + ); + repairChipLineItems = repairChipLineItems.sort( + (a, b) => parseFloat(b.laborAmount) - parseFloat(a.laborAmount) + ); + + // get the supporting items from the available line items (taxed) that ARE repair chips and sort them by descending labor amount + let availableRepairChipLineItems = availableLineItems.filter( + (lineItem) => lineItem.partNumber == "WSREPAIR" + ); + availableRepairChipLineItems = availableRepairChipLineItems.sort( + (a, b) => parseFloat(b.laborAmount) - parseFloat(a.laborAmount) + ); + + // go through each one of those mapping the taxes to the correct chip + for (let i = 0; i < availableRepairChipLineItems.length; i++) { + repairChipLineItems[i].salesTax = availableRepairChipLineItems[i].salesTax; + } + + // then we map the non-repair chip items based on part number + for ( + let lineItemIndex = 0; + lineItemIndex < nonRepairChipSupportItemsLineItems.length; + lineItemIndex++ + ) { + const availableLineItem = availableLineItems.find( + (ali) => + ali.partNumber == + nonRepairChipSupportItemsLineItems[lineItemIndex].partNumber + ); + if (availableLineItem) { + nonRepairChipSupportItemsLineItems[lineItemIndex].salesTax = + availableLineItem.salesTax; + } + } + + // finally we splice the two arrays back into one + lineItemsInCategory = nonRepairChipSupportItemsLineItems.concat(repairChipLineItems); + } else { + for ( + let lineItemIndex = 0; + lineItemIndex < lineItemsInCategory.length; + lineItemIndex++ + ) { + const availableLineItem = availableLineItems.find( + (ali) => ali.partNumber == lineItemsInCategory[lineItemIndex].partNumber + ); + if (availableLineItem) { + lineItemsInCategory[lineItemIndex].salesTax = availableLineItem.salesTax; + } } } }