Refactor cost and price calculations across multiple models to return 0 instead of undefined when no cost or price is provided. This change ensures consistent handling of missing values in Filament, FilamentSku, Invoice, OrderItem, Part, PartSku, and ProductSku models.

This commit is contained in:
Tom Butcher 2026-06-20 01:41:00 +01:00
parent f50949a192
commit 247bcc0ee5
7 changed files with 37 additions and 48 deletions

View File

@ -186,14 +186,13 @@ export const Filament = {
columnWidth: 150,
value: (objectData) => {
const cost = objectData?.cost
if (!cost) return undefined
if (!cost) return 0
if (objectData?.costTaxRate?.rateType == 'percentage') {
return (
(cost * (1 + objectData?.costTaxRate?.rate / 100)).toFixed(2) ||
undefined
(cost * (1 + objectData?.costTaxRate?.rate / 100)).toFixed(2) || 0
)
} else if (objectData?.costTaxRate?.rateType == 'amount') {
return (cost + objectData?.costTaxRate?.rate).toFixed(2) || undefined
return (cost + objectData?.costTaxRate?.rate).toFixed(2) || 0
}
return cost
}

View File

@ -201,11 +201,11 @@ export const FilamentSku = {
if (!objectData?.overrideCost) return undefined
const cost = objectData?.cost
const taxRate = objectData?.costTaxRate
if (!cost) return undefined
if (!cost) return 0
if (taxRate?.rateType == 'percentage') {
return (cost * (1 + taxRate?.rate / 100)).toFixed(2) || undefined
return (cost * (1 + taxRate?.rate / 100)).toFixed(2) || 0
} else if (taxRate?.rateType == 'amount') {
return (cost + taxRate?.rate).toFixed(2) || undefined
return (cost + taxRate?.rate).toFixed(2) || 0
}
return cost
},

View File

@ -395,20 +395,19 @@ export const Invoice = {
columnWidth: 200,
value: (objectData) => {
const invoiceAmount = objectData?.invoiceAmount || 0
if (!invoiceAmount) return 0
if (objectData?.taxRate?.rateType == 'percentage') {
return (
(invoiceAmount * (1 + objectData?.taxRate?.rate / 100)).toFixed(
2
) || undefined
) || 0
)
} else if (objectData?.taxRate?.rateType == 'amount') {
return (
(invoiceAmount + objectData?.taxRate?.rate).toFixed(2) ||
undefined
(invoiceAmount + objectData?.taxRate?.rate).toFixed(2) || 0
)
} else {
return invoiceAmount || 0
}
return invoiceAmount
}
}
],
@ -505,20 +504,19 @@ export const Invoice = {
columnWidth: 200,
value: (objectData) => {
const invoiceAmount = objectData?.invoiceAmount || 0
if (!invoiceAmount) return 0
if (objectData?.taxRate?.rateType == 'percentage') {
return (
(invoiceAmount * (1 + objectData?.taxRate?.rate / 100)).toFixed(
2
) || undefined
) || 0
)
} else if (objectData?.taxRate?.rateType == 'amount') {
return (
(invoiceAmount + objectData?.taxRate?.rate).toFixed(2) ||
undefined
(invoiceAmount + objectData?.taxRate?.rate).toFixed(2) || 0
)
} else {
return invoiceAmount || 0
}
return invoiceAmount
}
}
],

View File

@ -370,24 +370,20 @@ export const OrderItem = {
columnWidth: 175,
value: (objectData) => {
const totalAmount = objectData?.itemAmount * objectData?.quantity || 0
if (!totalAmount) return 0
if (objectData?.taxRate?.rateType == 'percentage') {
if (objectData?.quantity == undefined || objectData?.quantity == 0) {
return undefined
}
return (
(
(totalAmount || 0) *
totalAmount *
(1 + objectData?.taxRate?.rate / 100)
).toFixed(2) || undefined
).toFixed(2) || 0
)
} else if (objectData?.taxRate?.rateType == 'amount') {
return (
((totalAmount || 0) + objectData?.taxRate?.rate).toFixed(2) ||
undefined
(totalAmount + objectData?.taxRate?.rate).toFixed(2) || 0
)
} else {
return totalAmount || 0
}
return totalAmount
}
},
{

View File

@ -155,14 +155,13 @@ export const Part = {
columnWidth: 150,
value: (objectData) => {
const cost = objectData?.cost
if (!cost) return undefined
if (!cost) return 0
if (objectData?.costTaxRate?.rateType == 'percentage') {
return (
(cost * (1 + objectData?.costTaxRate?.rate / 100)).toFixed(2) ||
undefined
(cost * (1 + objectData?.costTaxRate?.rate / 100)).toFixed(2) || 0
)
} else if (objectData?.costTaxRate?.rateType == 'amount') {
return (cost + objectData?.costTaxRate?.rate).toFixed(2) || undefined
return (cost + objectData?.costTaxRate?.rate).toFixed(2) || 0
}
return cost
}
@ -241,16 +240,13 @@ export const Part = {
} else {
price = objectData?.price
}
if (!price) return undefined
if (!price) return 0
if (objectData?.priceTaxRate?.rateType == 'percentage') {
return (
(price * (1 + objectData?.priceTaxRate?.rate / 100)).toFixed(2) ||
undefined
(price * (1 + objectData?.priceTaxRate?.rate / 100)).toFixed(2) || 0
)
} else if (objectData?.priceTaxRate?.rateType == 'amount') {
return (
(price + objectData?.priceTaxRate?.rate).toFixed(2) || undefined
)
return (price + objectData?.priceTaxRate?.rate).toFixed(2) || 0
}
return price
}

View File

@ -201,11 +201,11 @@ export const PartSku = {
if (!objectData?.overrideCost) return undefined
const cost = objectData?.cost
const taxRate = objectData?.costTaxRate
if (!cost) return undefined
if (!cost) return 0
if (taxRate?.rateType == 'percentage') {
return (cost * (1 + taxRate?.rate / 100)).toFixed(2) || undefined
return (cost * (1 + taxRate?.rate / 100)).toFixed(2) || 0
} else if (taxRate?.rateType == 'amount') {
return (cost + taxRate?.rate).toFixed(2) || undefined
return (cost + taxRate?.rate).toFixed(2) || 0
}
return cost
},
@ -276,13 +276,13 @@ export const PartSku = {
} else {
price = objectData?.price
}
if (price == null) return undefined
if (!price) return 0
const taxRate =
objectData?.priceTaxRate ?? objectData?.part?.priceTaxRate
if (taxRate?.rateType == 'percentage') {
return (price * (1 + taxRate?.rate / 100)).toFixed(2) || undefined
return (price * (1 + taxRate?.rate / 100)).toFixed(2) || 0
} else if (taxRate?.rateType == 'amount') {
return (price + taxRate?.rate).toFixed(2) || undefined
return (price + taxRate?.rate).toFixed(2) || 0
}
return price
},

View File

@ -212,11 +212,11 @@ export const ProductSku = {
if (!objectData?.overrideCost) return undefined
const cost = objectData?.cost
const taxRate = objectData?.costTaxRate
if (!cost) return undefined
if (!cost) return 0
if (taxRate?.rateType == 'percentage') {
return (cost * (1 + taxRate?.rate / 100)).toFixed(2) || undefined
return (cost * (1 + taxRate?.rate / 100)).toFixed(2) || 0
} else if (taxRate?.rateType == 'amount') {
return (cost + taxRate?.rate).toFixed(2) || undefined
return (cost + taxRate?.rate).toFixed(2) || 0
}
return cost
},
@ -289,13 +289,13 @@ export const ProductSku = {
} else {
price = objectData?.price
}
if (price == null) return undefined
if (!price) return 0
const taxRate =
objectData?.priceTaxRate ?? objectData?.product?.priceTaxRate
if (taxRate?.rateType == 'percentage') {
return (price * (1 + taxRate?.rate / 100)).toFixed(2) || undefined
return (price * (1 + taxRate?.rate / 100)).toFixed(2) || 0
} else if (taxRate?.rateType == 'amount') {
return (price + taxRate?.rate).toFixed(2) || undefined
return (price + taxRate?.rate).toFixed(2) || 0
}
return price
},