import React, { useState, useRef } from 'react'; import { ActivityIndicator, View } from 'react-native' import { Cart, useOrder, useLanguage, useUtils, useConfig, useValidationFields, } from 'ordering-components/native'; import { useTheme } from 'styled-components/native'; import { OSContainer, OSProductList, OSBill, OSTable, OSRow, Divider } from './styles'; import { ProductItemAccordion } from '../ProductItemAccordion'; import { CouponControl } from '../CouponControl'; import { OInput, OModal, OText, OAlert } from '../shared'; import { verifyDecimals } from '../../utils'; import AntIcon from 'react-native-vector-icons/AntDesign' import { TaxInformation } from '../TaxInformation'; import { TouchableOpacity } from 'react-native'; import { MomentOption } from '../MomentOption'; const OrderSummaryUI = (props: any) => { const { changeQuantity, getProductMax, offsetDisabled, removeProduct, isCartPending, isFromCheckout, commentState, handleChangeComment, onNavigationRedirect, handleRemoveOfferClick, preorderSlotInterval, preorderLeadTime, preorderTimeRange, preorderMaximumDays, preorderMinimumDays, cateringTypes, hideDeliveryFee, loyaltyRewardRate, maxDate, hideCommentsByValidationCheckout, hideCouponByValidationCheckout } = props; const theme = useTheme() const [, t] = useLanguage(); const [{ configs }] = useConfig(); const [orderState] = useOrder(); const [{ parsePrice, parseNumber }] = useUtils(); const commentRef = useRef() const [openTaxModal, setOpenTaxModal] = useState({ open: false, data: null, type: '' }) const [comment] = useState(orderState?.carts?.[`businessId:${props.cart.business_id}`]?.comment ?? '') const hideCartDiscount = hideCouponByValidationCheckout const hideCartComments = hideCommentsByValidationCheckout const cart = orderState?.carts?.[`businessId:${props.cart.business_id}`] const walletName: any = { cash: { name: t('PAY_WITH_CASH_WALLET', 'Pay with Cash Wallet'), }, credit_point: { name: t('PAY_WITH_CREDITS_POINTS_WALLET', 'Pay with Credit Points Wallet'), } } const getIncludedTaxes = (isDeliveryFee?: boolean) => { if (cart?.taxes === null || !cart?.taxes) { return cart?.business?.tax_type === 1 ? cart?.tax : 0 } else { return cart?.taxes.reduce((taxIncluded: number, tax: any) => { return taxIncluded + (((!isDeliveryFee && tax.type === 1 && tax.target === 'product') || (isDeliveryFee && tax.type === 1 && tax.target === 'delivery_fee')) ? tax.summary?.tax : 0) }, 0) } } const clearAmount = (value: any) => parseFloat((Math.trunc(value * 100) / 100).toFixed(configs.format_number_decimal_length?.value ?? 2)) const loyaltyRewardValue = clearAmount((cart?.subtotal + getIncludedTaxes()) * loyaltyRewardRate) const handleDeleteClick = (product: any) => { removeProduct(product, cart) } const handleEditProduct = (product: any) => { onNavigationRedirect('ProductDetails', { isCartProduct: true, productCart: product, businessSlug: cart?.business?.slug, businessId: cart?.business_id, categoryId: product?.category_id, productId: product?.id, isFromCheckout: isFromCheckout, }) } const getIncludedTaxesDiscounts = () => { return cart?.taxes?.filter((tax: any) => (tax?.type === 1 && tax?.target === 'product'))?.reduce((carry: number, tax: any) => carry + (tax?.summary?.tax_after_discount ?? tax?.summary?.tax), 0) } const OfferAlert = ({ offerId }: any) => { return ( handleRemoveOfferClick(offerId)} > ) } return ( {cart?.products?.length > 0 && ( <> {cart?.products?.map((product: any) => ( ))} {cart?.valid && ( {t('SUBTOTAL', 'Subtotal')} {parsePrice(cart?.subtotal + getIncludedTaxes())} {cart?.discount > 0 && cart?.total >= 0 && cart?.offers?.length === 0 && ( {cart?.discount_type === 1 ? ( {t('DISCOUNT', 'Discount')} {`(${verifyDecimals(cart?.discount_rate, parsePrice)}%)`} ) : ( {t('DISCOUNT', 'Discount')} )} - {parsePrice(cart?.discount || 0)} )} { cart?.offers?.length > 0 && cart?.offers?.filter((offer: any) => offer?.target === 1)?.map((offer: any) => ( {offer.name} {offer.rate_type === 1 && ( {`(${verifyDecimals(offer?.rate, parsePrice)}%)`} )} setOpenTaxModal({ open: true, data: offer, type: 'offer_target_1' })}> {!!offer?.id && ( )} - {parsePrice(offer?.summary?.discount)} )) } {cart?.business_id && ( )} {cart?.subtotal_with_discount > 0 && cart?.discount > 0 && cart?.total >= 0 && ( {t('SUBTOTAL_WITH_DISCOUNT', 'Subtotal with discount')} {cart?.business?.tax_type === 1 ? ( {parsePrice(cart?.subtotal_with_discount + getIncludedTaxesDiscounts() ?? 0)} ) : ( {parsePrice(cart?.subtotal_with_discount ?? 0)} )} )} { cart.taxes?.length > 0 && cart.taxes.filter((tax: any) => tax.type === 2 && tax?.rate !== 0 && tax?.target === 'product').map((tax: any, i: number) => ( {tax.name || t('INHERIT_FROM_BUSINESS', 'Inherit from business')}{' '} {`(${verifyDecimals(tax?.rate, parseNumber)}%)`}{' '} setOpenTaxModal({ open: true, data: tax, type: 'tax' })} > {parsePrice(tax?.summary?.tax_after_discount ?? tax?.summary?.tax ?? 0)} )) } { cart?.fees?.length > 0 && cart?.fees?.filter((fee: any) => !(fee.fixed === 0 && fee.percentage === 0)).map((fee: any, i: number) => ( {t(fee.name?.toUpperCase()?.replace(/ /g, '_'), fee.name) || t('INHERIT_FROM_BUSINESS', 'Inherit from business')}{' '} ({fee?.fixed > 0 && `${parsePrice(fee?.fixed)}${fee.percentage > 0 ? ' + ' : ''}`}{fee.percentage > 0 && `${fee.percentage}%`}){' '} setOpenTaxModal({ open: true, data: fee, type: 'fee' })} > {parsePrice(fee?.summary?.fixed + (fee?.summary?.percentage_after_discount ?? fee?.summary?.percentage) ?? 0)} )) } { cart?.offers?.length > 0 && cart?.offers?.filter((offer: any) => offer?.target === 3)?.map((offer: any) => ( {offer.name} {offer.rate_type === 1 && ( {`(${verifyDecimals(offer?.rate, parsePrice)}%)`} )} setOpenTaxModal({ open: true, data: offer, type: 'offer_target_3' })}> {!!offer?.id && ( )} - {parsePrice(offer?.summary?.discount)} )) } {orderState?.options?.type === 1 && !hideDeliveryFee && ( {t('DELIVERY_FEE', 'Delivery Fee')} {parsePrice(cart?.delivery_price_with_discount + getIncludedTaxes(true))} )} { cart?.taxes?.length > 0 && cart?.taxes?.filter((tax: any) => tax?.type === 2 && tax?.rate !== 0 && tax?.target === 'delivery_fee').map((tax: any, i: number) => ( {tax.name || t('INHERIT_FROM_BUSINESS', 'Inherit from business')} {`(${verifyDecimals(tax?.rate, parseNumber)}%)`} setOpenTaxModal({ open: true, data: tax, type: 'tax' })}> {parsePrice(tax?.summary?.tax_after_discount ?? tax?.summary?.tax ?? 0)} )) } { cart?.offers?.length > 0 && cart?.offers?.filter((offer: any) => offer?.target === 2)?.map((offer: any) => ( {offer.name} {offer.rate_type === 1 && ( {`(${verifyDecimals(offer?.rate, parsePrice)}%)`} )} setOpenTaxModal({ open: true, data: offer, type: 'offer_target_2' })}> {!!offer?.id && ( )} - {parsePrice(offer?.summary?.discount)} )) } {cart?.driver_tip > 0 && ( {t('DRIVER_TIP', 'Driver tip')} {cart?.driver_tip_rate > 0 && parseInt(configs?.driver_tip_type?.value, 10) === 2 && !parseInt(configs?.driver_tip_use_custom?.value, 10) && ( `(${verifyDecimals(cart?.driver_tip_rate, parseNumber)}%)` )} {parsePrice(cart?.driver_tip)} )} {cart?.payment_events?.length > 0 && cart?.payment_events?.map((event: any) => ( {walletName[cart?.wallets?.find((wallet: any) => wallet.id === event.wallet_id)?.type]?.name} -{parsePrice(event.amount, { isTruncable: true })} ))} {!hideCartDiscount && !isCartPending && cart?.business_id && ( )} {cart?.total >= 1 && ( {t('TOTAL', 'Total')} {parsePrice(cart?.balance >= 0 ? cart?.balance : 0)} {!!loyaltyRewardValue && ( {t('REWARD_LOYALTY_POINT', 'Reward :amount: on loyalty points').replace(':amount:', loyaltyRewardValue)} )} )} {cart?.business_id && cart?.status !== 2 && !hideCartComments && ( {t('COMMENTS', 'Comments')} handleChangeComment(value)} style={{ alignItems: 'flex-start', width: '100%', height: 100, borderColor: theme.colors.border, paddingRight: 50, marginTop: 10, borderRadius: 8 }} forwardRef={commentRef} multiline /> {commentState?.loading && ( )} )} )} {cateringTypes.includes(orderState?.options?.type) && maxDate && cart?.business && ( )} setOpenTaxModal({ open: false, data: null, type: '' })} entireModal title={`${openTaxModal.data?.name || t('INHERIT_FROM_BUSINESS', 'Inherit from business')} ${openTaxModal.data?.rate_type !== 2 ? `(${typeof openTaxModal.data?.rate === 'number' ? `${openTaxModal.data?.rate}%` : `${parsePrice(openTaxModal.data?.fixed ?? 0)} + ${openTaxModal.data?.percentage}%`})` : ''} `} > )} ) } export const OrderSummary = (props: any) => { const orderSummaryProps = { ...props, disablePreviousComment: true, UIComponent: OrderSummaryUI } return ( ) }