import React, { useState } from 'react'; import { ActivityIndicator, TouchableOpacity, View } from 'react-native' import { Cart, useOrder, useLanguage, useUtils, useConfig, useValidationFields, } from 'ordering-components-external/native'; import { OSContainer, OSProductList, OSBill, OSTable, OSRow, Divider } from './styles'; import { ProductItemAccordion } from '../ProductItemAccordion'; import { CouponControl } from '../CouponControl'; import { OAlert, OInput, OModal, OText } from '../shared'; import { verifyDecimals } from '../../utils'; import { useTheme } from 'styled-components/native'; import { TaxInformation } from '../TaxInformation'; import AntIcon from 'react-native-vector-icons/AntDesign' const OrderSummaryUI = (props: any) => { const { cart, changeQuantity, getProductMax, offsetDisabled, removeProduct, isCartPending, isFromCheckout, commentState, handleChangeComment, onNavigationRedirect, handleRemoveOfferClick } = props; const theme = useTheme(); const [, t] = useLanguage(); const [{ configs }] = useConfig(); const [orderState] = useOrder(); const [{ parsePrice, parseNumber }] = useUtils(); const [validationFields] = useValidationFields(); const [openTaxModal, setOpenTaxModal] = useState({ open: false, data: null }) const [confirm, setConfirm] = useState({ open: false, content: null, handleOnAccept: null, id: null, title: null }) const isCouponEnabled = validationFields?.fields?.checkout?.coupon?.enabled; const handleDeleteClick = (product: any) => { removeProduct(product, cart) } const handleEditProduct = (product: any) => { onNavigationRedirect && onNavigationRedirect('ProductDetails', { isCartProduct: true, productCart: product, businessSlug: cart?.business?.slug, businessId: cart?.business_id, categoryId: product?.category_id, productId: product?.id, isFromCheckout: isFromCheckout, }) } const getIncludedTaxes = () => { if (cart?.taxes === null) { return cart.business.tax_type === 1 ? cart?.tax : 0 } else { return cart?.taxes.reduce((taxIncluded: number, tax: any) => { return taxIncluded + (tax.type === 1 ? tax.summary?.tax : 0) }, 0) } } const getIncludedTaxesDiscounts = () => { return cart?.taxes?.filter((tax: any) => tax?.type === 1)?.reduce((carry: number, tax: any) => carry + (tax?.summary?.tax_after_discount ?? tax?.summary?.tax), 0) } const onRemoveOffer = (id: number) => { setConfirm({ open: true, content: [t('QUESTION_DELETE_OFFER', 'Are you sure that you want to delete the offer?')], title: t('OFFER', 'Offer'), handleOnAccept: () => { setConfirm({ ...confirm, open: false }) handleRemoveOfferClick(id) } }) } 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' })}> onRemoveOffer(offer?.id)}> - {parsePrice(offer?.summary?.discount)} )) } {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).map((tax: any) => ( {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) => ( {fee.name || t('INHERIT_FROM_BUSINESS', 'Inherit from business')}{' '} ({parsePrice(fee?.fixed)} + {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' })}> onRemoveOffer(offer?.id)}> - {parsePrice(offer?.summary?.discount)} )) } {orderState?.options?.type === 1 && cart?.delivery_price > 0 && ( {t('DELIVERY_FEE', 'Delivery Fee')} {parsePrice(cart?.delivery_price)} )} { 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' })}> onRemoveOffer(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)} )} {isCouponEnabled && !isCartPending && ( )} {t('TOTAL', 'Total')} {parsePrice(cart?.total >= 0 ? cart?.total : 0)} {cart?.status !== 2 && ( {t('COMMENTS', 'Comments')} handleChangeComment(value)} style={{ alignItems: 'flex-start', width: '100%', height: 100, borderColor: theme.colors.textSecondary, paddingRight: 50, marginTop: 10 }} multiline /> {commentState?.loading && ( )} )} )} setOpenTaxModal({ open: false, data: null })} 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}%`})` : ''} `} > setConfirm({ ...confirm, open: false, title: null })} onClose={() => setConfirm({ ...confirm, open: false, title: null })} /> )} ) } export const OrderSummary = (props: any) => { const orderSummaryProps = { ...props, UIComponent: OrderSummaryUI } return ( ) }