Skip to content

Commit

Permalink
fix: tax price
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-liu-smile authored and CarlLiu2023 committed May 10, 2023
1 parent 5a03f05 commit afd8756
Show file tree
Hide file tree
Showing 28 changed files with 754 additions and 288 deletions.
129 changes: 68 additions & 61 deletions apps/storefront/src/hooks/dom/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { searchB2BProducts, searchBcProducts } from '@/shared/service/b2b'
import { getCartInfoWithOptions } from '@/shared/service/bc'
import {
addQuoteDraftProduce,
getModifiersPrice,
getProductExtraPrice,
calculateProductListPrice,
getCalculatedProductPrice,
globalSnackbar,
isAllRequiredOptionFilled,
} from '@/utils'
Expand Down Expand Up @@ -152,16 +152,37 @@ const addQuoteToCart = (setOpenPage: DispatchProps) => {

const getOptionsList = (options: ProductOptionsProps[] | []) => {
if (options?.length === 0) return []

const option = options.map(({ nameId, valueId, value }) => {
let optionValue: number | string = valueId.toString()
if (typeof valueId === 'number' && valueId.toString().length === 10) {
optionValue = value
}

return {
optionId: `attribute[${nameId}]`,
optionValue,
const option: CustomFieldItems = []
options.forEach(({ nameId, valueId, value }) => {
let optionValue: number | string = valueId
? `${valueId}`.toString()
: value
if (
typeof valueId === 'number' &&
`${valueId}`.toString().length === 10
) {
optionValue = valueId
const date = new Date(+valueId * 1000)
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
option.push({
optionId: `attribute[${nameId}][year]`,
optionValue: year,
})
option.push({
optionId: `attribute[${nameId}][month]`,
optionValue: month,
})
option.push({
optionId: `attribute[${nameId}][day]`,
optionValue: day,
})
} else {
option.push({
optionId: `attribute[${nameId}]`,
optionValue,
})
}
})

Expand Down Expand Up @@ -203,6 +224,8 @@ const addQuoteToCart = (setOpenPage: DispatchProps) => {
productIds,
})

const newProduct: CustomFieldItems[] = []

const newProductInfo: CustomFieldItems =
conversionProductsList(productsSearch)
productsWithSKU.forEach((product) => {
Expand Down Expand Up @@ -236,11 +259,21 @@ const addQuoteToCart = (setOpenPage: DispatchProps) => {
optionList: JSON.stringify(optionsList),
productId,
basePrice: listPrice,
tax: salePrice - listPrice,
taxPrice: salePrice - listPrice,
},
}

addQuoteDraftProduce(quoteListitem, quantity, optionsList || [])
newProduct.push(quoteListitem)

// addQuoteDraftProduce(quoteListitem, quantity, optionsList || [])
})

await calculateProductListPrice(newProduct, '2')

newProduct.forEach((product: CustomFieldItems) => {
const { optionList, quantity } = product.node

addQuoteDraftProduce(product, quantity, JSON.parse(optionList) || [])
})

globalSnackbar.success('Product was added to your quote.', {
Expand Down Expand Up @@ -286,31 +319,12 @@ const addQuoteToProduct = (setOpenPage: DispatchProps) => {

const newProductInfo: CustomFieldItems =
conversionProductsList(productsSearch)
const { allOptions, variants } = newProductInfo[0]

const variantItem = variants.find(
(item: CustomFieldItems) => item.sku === sku
)
const { allOptions } = newProductInfo[0]

const optionMap = serialize(form)

const optionList = getProductOptionList(optionMap)

const modifiersPrice = getModifiersPrice(
productsSearch[0]?.modifiers || [],
optionList
)

const productExtraPrice = await getProductExtraPrice(
productsSearch[0]?.modifiers || [],
optionList,
+role
)

const additionalCalculatedPrices = [
...modifiersPrice,
...productExtraPrice,
]
const { isValid, message } = isAllRequiredOptionFilled(
allOptions,
optionList
Expand All @@ -322,34 +336,27 @@ const addQuoteToProduct = (setOpenPage: DispatchProps) => {
return
}

const quoteListitem = {
node: {
id: uuid(),
variantSku: variantItem.sku,
variantId: variantItem.variantId,
productsSearch: newProductInfo[0],
primaryImage: variantItem.image_url,
productName: newProductInfo[0].name,
quantity: +qty,
optionList: JSON.stringify(optionList),
productId,
additionalCalculatedPrices,
basePrice: variantItem.bc_calculated_price.as_entered,
tax:
variantItem.bc_calculated_price.tax_inclusive -
variantItem.bc_calculated_price.tax_exclusive,
},
}

addQuoteDraftProduce(quoteListitem, qty, optionList || [])

globalSnackbar.success('Product was added to your quote.', {
jsx: () =>
B3AddToQuoteTip({
gotoQuoteDraft: () => gotoQuoteDraft(setOpenPage),
}),
isClose: true,
const quoteListitem = await getCalculatedProductPrice({
optionList,
productsSearch: newProductInfo[0],
sku,
qty,
})

if (quoteListitem) {
addQuoteDraftProduce(quoteListitem, qty, optionList || [])
globalSnackbar.success('Product was added to your quote.', {
jsx: () =>
B3AddToQuoteTip({
gotoQuoteDraft: () => gotoQuoteDraft(setOpenPage),
}),
isClose: true,
})
} else {
globalSnackbar.error('Price error', {
isClose: true,
})
}
} catch (e) {
console.log(e)
} finally {
Expand Down
3 changes: 3 additions & 0 deletions apps/storefront/src/pages/pdp/PDP.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
searchB2BProducts,
searchBcProducts,
} from '@/shared/service/b2b'
// import {
// OptionListProduct,
// } from '@/types/products'
import {
B3SStorage,
getDefaultCurrencyInfo,
Expand Down
4 changes: 2 additions & 2 deletions apps/storefront/src/pages/quote/QuoteDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function QuoteDetail() {
discount: 0,
tax: 0,
shipping: 0,
grandTotal: 0,
totalAmount: 0,
})
const [isRequestLoading, setIsRequestLoading] = useState(false)
const [isShowFooter, setIsShowFooter] = useState(false)
Expand Down Expand Up @@ -126,7 +126,7 @@ function QuoteDetail() {
discount: quote.discount,
tax: quote.taxTotal,
shipping: quote.shippingTotal,
grandTotal: quote.totalAmount,
totalAmount: quote.totalAmount,
})
setProductList(productsWithMoreInfo)

Expand Down
23 changes: 12 additions & 11 deletions apps/storefront/src/pages/quote/QuoteDraft.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -455,22 +455,23 @@ function QuoteDraft({ setOpenPage }: QuoteDraftProps) {
(item: CustomFieldItems) => item.sku === node.variantSku
)

let prices = 0
let tax = 0
node.additionalCalculatedPrices?.forEach((item: CustomFieldItems) => {
prices += item.additionalCalculatedPrice
tax += item.additionalCalculatedPriceTax
})
allPrice += (+node.basePrice + prices) * node.quantity
// let prices = 0
// let tax = 0
// node.additionalCalculatedPrices?.forEach((item: CustomFieldItems) => {
// prices += item.additionalCalculatedPrice
// tax += item.additionalCalculatedPriceTax
// })
allPrice += +node.basePrice * node.quantity

allTaxPrice += (+node.tax + tax) * node.quantity
allTaxPrice += +node.taxPrice * node.quantity

const items = {
productId: node.productsSearch.id,
sku: node.variantSku,
basePrice: (+node.basePrice + prices).toFixed(2),
basePrice: (+node.basePrice).toFixed(2),
taxPrice: (+node.taxPrice).toFixed(2),
discount: '0.00',
offeredPrice: (+node.basePrice + prices).toFixed(2),
offeredPrice: (+node.basePrice).toFixed(2),
quantity: node.quantity,
variantId: varantsItem.variant_id,
imageUrl: node.primaryImage,
Expand All @@ -489,7 +490,7 @@ function QuoteDraft({ setOpenPage }: QuoteDraftProps) {
// notes: note,
message: newNote,
legalTerms: '',
totalAmount: allPrice.toFixed(2),
totalAmount: (allPrice + allTaxPrice).toFixed(2),
grandTotal: allPrice.toFixed(2),
subtotal: allPrice.toFixed(2),
companyId: isB2BUser ? companyB2BId || salesRepCompanyId : '',
Expand Down
56 changes: 28 additions & 28 deletions apps/storefront/src/pages/quote/components/AddToQuote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { PRODUCT_DEFAULT_IMAGE } from '@/constants'
import { searchB2BProducts, searchBcProducts } from '@/shared/service/b2b'
import {
addQuoteDraftProduce,
getModifiersPrice,
getQuickAddProductExtraPrice,
calculateProductListPrice,
snackbar,
} from '@/utils'
import { conversionProductsList } from '@/utils/b3Product/shared/config'
Expand Down Expand Up @@ -38,38 +37,19 @@ export default function AddToQuote(props: AddToListProps) {
name: productName,
quantity,
variants = [],
allOptions,
additionalProducts,
basePrice,
taxPrice,
calculatedValue,
} = product

const modifiersPrice = getModifiersPrice(
allOptions || [],
newSelectOptionList
)

const productExtraPrice = getQuickAddProductExtraPrice(
allOptions || [],
newSelectOptionList,
additionalProducts
)

const additionalCalculatedPrices = [
...modifiersPrice,
...productExtraPrice,
]

const variantInfo =
variants.length === 1
? variants[0]
: variants.find(
(item: CustomFieldItems) => item.variant_id === variantId
)

const {
image_url: primaryImage = '',
calculated_price: basePrice,
sku: variantSku,
} = variantInfo
const { image_url: primaryImage = '', sku: variantSku } = variantInfo

let selectOptions
try {
Expand All @@ -78,15 +58,23 @@ export default function AddToQuote(props: AddToListProps) {
selectOptions = '[]'
}

const taxExclusive = variantInfo.bc_calculated_price.tax_exclusive
const taxInclusive = variantInfo.bc_calculated_price.tax_inclusive

const basePriceExclusiveTax = basePrice || taxExclusive

const tax = taxPrice || +taxInclusive - +taxInclusive

return {
node: {
basePrice: basePrice.toFixed(2),
additionalCalculatedPrices,
basePrice: basePriceExclusiveTax,
taxPrice: tax,
optionList: selectOptions,
id: uuid(),
primaryImage,
productId,
productName,
calculatedValue,
productsSearch: {
...product,
selectOptions,
Expand Down Expand Up @@ -133,6 +121,8 @@ export default function AddToQuote(props: AddToListProps) {
}
})

await calculateProductListPrice(productList)

const newProducts = getNewQuoteProduct(productList)

addToQuote(newProducts)
Expand Down Expand Up @@ -187,6 +177,8 @@ export default function AddToQuote(props: AddToListProps) {
conversionProductsList(productsSearch)

let isSuccess = false

const newProducts: CustomFieldItems[] = []
validProduct.forEach((product: CustomFieldItems) => {
const {
products: { option, variantSku, productId, productName, variantId },
Expand Down Expand Up @@ -221,12 +213,20 @@ export default function AddToQuote(props: AddToListProps) {
},
}

addQuoteDraftProduce(quoteListitem, +qty, optionsList || [])
newProducts.push(quoteListitem)

isSuccess = true
})

if (isSuccess) {
await calculateProductListPrice(newProducts, '2')
newProducts.forEach((item: CustomFieldItems) => {
addQuoteDraftProduce(
item,
+item.node.qty,
JSON.parse(item.node.optionList) || []
)
})
snackbar.success('Products were added to your quote.', {
isClose: true,
})
Expand Down
Loading

0 comments on commit afd8756

Please sign in to comment.