Skip to content

Commit

Permalink
fix: quote show price with tax
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianJiang2021 authored and CarlLiu2023 committed Apr 10, 2023
1 parent 6fa829a commit d27d0d0
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 35 deletions.
60 changes: 59 additions & 1 deletion apps/storefront/src/pages/quote/QuoteDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
getBcQuoteDetail,
exportB2BQuotePdf,
exportBcQuotePdf,
searchB2BProducts,
searchBcProducts,
} from '@/shared/service/b2b'

import {
Expand All @@ -39,6 +41,7 @@ import {
import {
snackbar,
getSearchVal,
getDefaultCurrencyInfo,
} from '@/utils'

import QuoteDetailHeader from './components/QuoteDetailHeader'
Expand All @@ -57,6 +60,13 @@ import {

import Message from './components/Message'

import {
ProductInfoProps,
} from './shared/config'
import {
conversionProductsList,
} from '../shoppingListDetails/shared/config'

const QuoteDetail = () => {
const {
id = '',
Expand All @@ -65,6 +75,9 @@ const QuoteDetail = () => {

const {
state: {
companyInfo: {
id: companyInfoId,
},
role,
customer,
isB2BUser,
Expand All @@ -86,9 +99,53 @@ const QuoteDetail = () => {
grandTotal: 0,
})
const [isRequestLoading, setIsRequestLoading] = useState(false)
const {
currency_code: currencyCode,
} = getDefaultCurrencyInfo()

const location = useLocation()

const handleGetProductsById = async (listProducts: ProductInfoProps[]) => {
if (listProducts.length > 0) {
const productIds: number[] = []

listProducts.forEach((item) => {
if (!productIds.includes(item.productId)) {
productIds.push(item.productId)
}
})
const getProducts = isB2BUser ? searchB2BProducts : searchBcProducts

try {
const {
productsSearch,
} = await getProducts({
productIds,
currencyCode,
companyId: companyInfoId,
})

const newProductsSearch = conversionProductsList(productsSearch)

listProducts.forEach((item) => {
const productInfo = newProductsSearch.find((search: CustomFieldItems) => {
const {
id: productId,
} = search

return +item.productId === +productId
})

item.productsSearch = productInfo || {}
})

return listProducts
} catch (err: any) {
snackbar.error(err)
}
}
}

const getQuoteDetail = async () => {
setIsRequestLoading(true)

Expand All @@ -108,6 +165,7 @@ const QuoteDetail = () => {
const {
quote,
} = await fn(data)
const productsWithMoreInfo = await handleGetProductsById(quote.productsList)

setQuoteDetail(quote)
setQuoteSummary({
Expand All @@ -118,7 +176,7 @@ const QuoteDetail = () => {
grandTotal: quote.totalAmount,
})
setCurrency(quote.currency)
setProductList(quote.productsList)
setProductList(productsWithMoreInfo)

const {
backendAttachFiles = [],
Expand Down
55 changes: 55 additions & 0 deletions apps/storefront/src/pages/quote/QuoteDraft.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
getB2BCustomerAddresses,
createQuote,
createBCQuote,
searchB2BProducts,
searchBcProducts,
} from '@/shared/service/b2b'

import {
Expand Down Expand Up @@ -90,6 +92,8 @@ import {

import {
getProductOptionsFields,
conversionProductsList,
ListItemProps,
} from '../shoppingListDetails/shared/config'

import {
Expand Down Expand Up @@ -334,8 +338,57 @@ const QuoteDraft = ({

const {
token: currencyToken,
currency_code: currencyCode,
} = getDefaultCurrencyInfo()

const handleGetProductsById = async (listProducts: ListItemProps[]) => {
if (listProducts.length > 0) {
const productIds: number[] = []
listProducts.forEach((item) => {
const {
node,
} = item
if (!productIds.includes(node.productId)) {
productIds.push(node.productId)
}
})

const getProducts = isB2BUser ? searchB2BProducts : searchBcProducts

try {
const {
productsSearch,
} = await getProducts({
productIds,
currencyCode,
companyId: companyB2BId,
})

const newProductsSearch = conversionProductsList(productsSearch)

listProducts.forEach((item) => {
const {
node,
} = item

const productInfo = newProductsSearch.find((search: CustomFieldItems) => {
const {
id: productId,
} = search

return +node.productId === +productId
})

node.productsSearch = productInfo || {}
})

return listProducts
} catch (err: any) {
snackbar.error(err)
}
}
}

const getQuoteTableDetails = async (params: CustomFieldItems) => {
const quoteDraftAllList = B3LStorage.get('b2bQuoteDraftList') || []

Expand All @@ -351,6 +404,8 @@ const QuoteDraft = ({
}
const list = quoteDraftAllList.slice(startIndex, endIndex)

await handleGetProductsById(list)

list.forEach((item: any) => {
let additionalCalculatedPriceTax = 0
let additionalCalculatedPrice = 0
Expand Down
14 changes: 11 additions & 3 deletions apps/storefront/src/pages/quote/QuotesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ import {
B3LStorage,
} from '@/utils'

import {
addPrice,
} from './shared/config'

interface SortByListProps {
[key: string]: number | string
}
Expand Down Expand Up @@ -264,7 +268,9 @@ const QuotesList = () => {

const quoteDraftAllList = B3LStorage.get('b2bQuoteDraftList') || []
if (params.offset === 0 && quoteDraftAllList.length) {
const price = quoteDraftAllList.reduce((pre: number, cur: CustomFieldItems) => pre + (cur.node.basePrice * cur.node.quantity), 0)
const summaryPrice = addPrice()

// const price = quoteDraftAllList.reduce((pre: number, cur: CustomFieldItems) => pre + (cur.node.basePrice * cur.node.quantity), 0)
const quoteDraft = {
node: {
quoteNumber: '—',
Expand All @@ -277,8 +283,9 @@ const QuotesList = () => {
currency: {
token: currencyToken,
},
totalAmount: price,
totalAmount: summaryPrice?.subtotal,
status: 0,
taxTotal: summaryPrice?.tax,
},
}

Expand Down Expand Up @@ -350,9 +357,10 @@ const QuotesList = () => {
token,
},
totalAmount,
taxTotal,
} = item

return (`${token}${(+totalAmount).toFixed(2)}`)
return (`${token}${(+totalAmount + +taxTotal).toFixed(2)}`)
},
style: {
textAlign: 'right',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const QuoteDetailSummary = (props: QuoteDetailSummaryProps) => {
}}
>
<Typography>Original subtotal</Typography>
<Typography>{priceFormat(+originalSubtotal)}</Typography>
<Typography>{priceFormat(+originalSubtotal + +tax)}</Typography>
</Grid>
<Grid
container
Expand Down Expand Up @@ -89,7 +89,7 @@ export const QuoteDetailSummary = (props: QuoteDetailSummaryProps) => {
color: '#212121',
}}
>
{priceFormat(+quotedSubtotal)}
{priceFormat(+quotedSubtotal + +tax)}
</Typography>
</Grid>

Expand Down Expand Up @@ -134,7 +134,7 @@ export const QuoteDetailSummary = (props: QuoteDetailSummaryProps) => {
color: '#212121',
}}
>
{priceFormat(+grandTotal)}
{priceFormat(+grandTotal + +tax)}
</Typography>
</Grid>
</Box>
Expand Down
29 changes: 24 additions & 5 deletions apps/storefront/src/pages/quote/components/QuoteDetailTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ interface ProductInfoProps {
variantId: number,
variantSku: string,
productsSearch: CustomFieldItems,
offeredPrice: number | string,
}

interface ListItemProps {
Expand Down Expand Up @@ -210,13 +211,22 @@ const QuoteDetailTable = (props: ShoppingDetailTableProps, ref: Ref<unknown>) =>
{
key: 'Price',
title: 'Price',
render: (row) => {
render: (row: CustomFieldItems) => {
const {
basePrice,
offeredPrice,
productsSearch: {
variants,
},
variantId,
} = row
const currentVariantInfo = variants.find((item: CustomFieldItems) => +item.variant_id === +variantId) || {}
const bcCalculatedPrice: {
tax_inclusive: number | string,
} = currentVariantInfo.bc_calculated_price

const price = +basePrice
const withTaxPrice = +bcCalculatedPrice.tax_inclusive
const discountPrice = +offeredPrice
const isDiscount = price - discountPrice > 0

Expand All @@ -230,7 +240,7 @@ const QuoteDetailTable = (props: ShoppingDetailTableProps, ref: Ref<unknown>) =>
textDecoration: 'line-through',
}}
>
{`${currencyToken}${price.toFixed(2)}`}
{`${currencyToken}${withTaxPrice.toFixed(2)}`}
</Typography>
)
}
Expand All @@ -241,7 +251,7 @@ const QuoteDetailTable = (props: ShoppingDetailTableProps, ref: Ref<unknown>) =>
color: isDiscount ? '#2E7D32' : '#212121',
}}
>
{`${currencyToken}${discountPrice.toFixed(2)}`}
{`${currencyToken}${(+withTaxPrice - +isDiscount).toFixed(2)}`}
</Typography>

</>
Expand Down Expand Up @@ -277,13 +287,22 @@ const QuoteDetailTable = (props: ShoppingDetailTableProps, ref: Ref<unknown>) =>
basePrice,
quantity,
offeredPrice,
productsSearch: {
variants,
},
variantId,
} = row
const currentVariantInfo = variants.find((item: CustomFieldItems) => +item.variant_id === +variantId) || {}
const bcCalculatedPrice: {
tax_inclusive: number | string,
} = currentVariantInfo.bc_calculated_price
const price = +basePrice
const withTaxPrice = +bcCalculatedPrice.tax_inclusive
const discountPrice = +offeredPrice
const isDiscount = price - discountPrice > 0

const total = price * +quantity
const totalWithDiscount = discountPrice * +quantity
const total = withTaxPrice * +quantity
const totalWithDiscount = (+withTaxPrice - +isDiscount) * +quantity

return (
<Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,23 @@ const QuoteDetailTableCard = (props: QuoteTableCardProps) => {
sku,
notes,
offeredPrice,
productsSearch: {
variants,
},
variantId,
} = quoteTableItem
const currentVariantInfo = variants.find((item: CustomFieldItems) => +item.variant_id === +variantId) || {}
const bcCalculatedPrice: {
tax_inclusive: number | string,
} = currentVariantInfo.bc_calculated_price

const price = +basePrice
const withTaxPrice = +bcCalculatedPrice.tax_inclusive
const discountPrice = +offeredPrice
const isDiscount = price - discountPrice > 0

const total = price * +quantity
const totalWithDiscount = discountPrice * +quantity
const total = withTaxPrice * +quantity
const totalWithDiscount = (+withTaxPrice - +isDiscount) * +quantity

return (
<Box
Expand Down Expand Up @@ -132,7 +141,7 @@ const QuoteDetailTableCard = (props: QuoteTableCardProps) => {
textDecoration: 'line-through',
}}
>
{`${currencyToken}${price.toFixed(2)}`}
{`${currencyToken}${withTaxPrice.toFixed(2)}`}
</span>
)
}
Expand All @@ -141,7 +150,7 @@ const QuoteDetailTableCard = (props: QuoteTableCardProps) => {
color: isDiscount ? '#2E7D32' : '#212121',
}}
>
{`${currencyToken}${discountPrice.toFixed(2)}`}
{`${currencyToken}${(+withTaxPrice - +isDiscount).toFixed(2)}`}
</span>
</Typography>

Expand Down
Loading

0 comments on commit d27d0d0

Please sign in to comment.