import React, { useState, useCallback } from 'react' import { Platform, StyleSheet, View, TouchableOpacity, ScrollView } from 'react-native'; import AntDesignIcon from 'react-native-vector-icons/AntDesign' import { OButton, OText, OLink, OModal } from '../shared' import { OrderContent, OrderBusiness, OrderCustomer, OrderProducts, Table, OrderBill, Total, OSRow, OrderVehicle, OrderSpot, } from './styles'; import { ProductItemAccordion } from '../ProductItemAccordion'; import { verifyDecimals, calculateDistance, transformDistance, getCurrenySymbol } from '../../utils'; import { useLanguage, useUtils, useConfig, useSession, useApi } from 'ordering-components/native'; import { useTheme } from 'styled-components/native'; import { ReviewCustomer } from '../ReviewCustomer' import MaterialIcon from 'react-native-vector-icons/MaterialCommunityIcons' import { DeviceOrientationMethods } from '../../../../../src/hooks/DeviceOrientation' const { useDeviceOrientation } = DeviceOrientationMethods interface OrderContent { order: any, logisticOrderStatus?: Array, isOrderGroup?: boolean, lastOrder?: boolean } export const OrderContentComponent = (props: OrderContent) => { const [, t] = useLanguage(); const theme = useTheme() const [ordering] = useApi() const [{ user }] = useSession() const { order, logisticOrderStatus, isOrderGroup, lastOrder } = props; const [{ parsePrice, parseNumber }] = useUtils(); const [{ configs }] = useConfig(); const [orientationState] = useDeviceOrientation(); const distanceUnit = configs?.distance_unit?.value const WIDTH_SCREEN = orientationState?.dimensions?.width const [openReviewModal, setOpenReviewModal] = useState(false) const [showCustomFields, setShowCustomFields] = useState(false); const [isReadMore, setIsReadMore] = useState({ customerAddress: false, businessAddressNotes: false }) const [lengthMore, setLengthMore] = useState({ customerAddress: false, businessAddressNotes: false }) const pastOrderStatuses = [1, 2, 5, 6, 10, 11, 12, 16, 17] const deliveryTypes = [1, 7] const commentDivide = ordering?.project?.includes('delosi') ? order?.comment?.split('Total') : [order?.comment] 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 styles = StyleSheet.create({ linkWithIcons: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', marginBottom: 5, flex: 1, }, textBold: { fontWeight: '600', }, textLink: { color: '#365CC7' }, btnReview: { borderWidth: 0, backgroundColor: theme.colors.primary, borderRadius: 8, } }) const getIncludedTaxes = (isDeliveryFee?: boolean) => { if (!order?.taxes) return 0 if (order?.taxes?.length === 0) { return order.tax_type === 1 ? order?.summary?.tax ?? 0 : 0 } else { return order?.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 getIncludedTaxesDiscounts = () => { return order?.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 containsOnlyNumbers = (str: string) => { str = str?.replace('+', ''); return /^\d+$/.test(str); } const onTextLayout = useCallback((e: any, item: string) => { if (item === 'customerAddress') { const customerAddressMore = (e.nativeEvent.lines.length == 2 && e.nativeEvent.lines[1].width > WIDTH_SCREEN * .76) || e.nativeEvent.lines.length > 2 setLengthMore(prev => ({ ...prev, customerAddress: customerAddressMore })) } if (item === 'businessAddressNotes') { const businessAddressNotesMore = (e.nativeEvent.lines.length == 3 && e.nativeEvent.lines[2].width > WIDTH_SCREEN * .76) || e.nativeEvent.lines.length > 3 setLengthMore(prev => ({ ...prev, businessAddressNotes: businessAddressNotesMore })) } }, []); return ( {isOrderGroup && ( {t('ORDER', 'Order')} #{order?.id} )} {order?.metafields?.length > 0 && ( setShowCustomFields((prev: boolean) => !prev)} style={{ flexDirection: 'row', width: '100%', justifyContent: 'space-between', alignItems: 'flex-start' }}> {t('CUSTOM_FIELDS', 'Custom fields')} {showCustomFields && ( <> {order.metafields.map((field: any) => ( {field.key} {field.value} ))} )} )} {t('BUSINESS_DETAILS', 'Business details')} {order?.business?.name} {(!order?.isLogistic || !logisticOrderStatus?.includes(order?.status)) && ( <> {!!order?.business?.email && ( )} {!!order?.business?.cellphone && ( )} {!!order?.business?.phone && ( )} )} {!!order?.business?.address && ( )} {!!order?.business?.location && order?.customer?.location && ( {t('DISTANCE_TO_THE_BUSINESS', 'Distance to the business')}: {transformDistance(calculateDistance(order?.business?.location, { latitude: order?.customer?.location?.lat, longitude: order?.customer?.location?.lng }), distanceUnit)} {t(distanceUnit?.toUpperCase?.(), distanceUnit)} )} {!!order?.business?.address_notes && ( <> onTextLayout(e, 'businessAddressNotes')} numberOfLines={isReadMore.businessAddressNotes ? 20 : 3} /> {lengthMore.businessAddressNotes && ( setIsReadMore({ ...isReadMore, businessAddressNotes: !isReadMore.businessAddressNotes })} > {isReadMore.businessAddressNotes ? t('SHOW_LESS', 'Show less') : t('READ_MORE', 'Read more')} )} )} {t('CUSTOMER_DETAILS', 'Customer details')} { (!!order?.customer?.name || !!order?.customer?.lastname) && ( {!!order?.customer?.name && ( {order?.customer?.name} )} {!!order?.customer?.middle_name && ( {order?.customer?.middle_name} )} {!!order?.customer?.lastname && ( {order?.customer?.lastname} )} { !!order?.customer?.second_lastname && ( {order?.customer?.second_lastname} ) } {!!order?.user_review?.qualification && ( {order?.user_review?.qualification} )} ) } {!!order?.customer?.email && ( )} {!!order?.customer?.cellphone && ( )} {!!order?.customer?.phone && ( )} {!!order?.customer?.address && ( <> onTextLayout(e, 'customerAddress')} numberOfLines={isReadMore.customerAddress ? 20 : 2} shorcut={order?.customer?.address} TextStyle={styles.textLink} /> {lengthMore.customerAddress && ( setIsReadMore({ ...isReadMore, customerAddress: !isReadMore.customerAddress })} > {isReadMore.customerAddress ? t('SHOW_LESS', 'Show less') : t('READ_MORE', 'Read more')} )} )} {!!order?.customer?.internal_number && ( {order?.customer?.internal_number} )} {!!order?.customer?.address_notes && ( {order?.customer?.address_notes} )} {!!order?.customer?.zipcode && ( {order?.customer?.zipcode} )} {!!order?.on_behalf_of && ( {t('ON_BEHALF_OF', 'On behalf of')}{': '} {order?.on_behalf_of} )} {(order?.delivery_option !== undefined && deliveryTypes.includes(order?.delivery_type)) && ( {order?.delivery_option !== undefined && deliveryTypes.includes(order?.delivery_type) && ( {t(order?.delivery_option?.name?.toUpperCase?.()?.replace(/ /g, '_'), order?.delivery_option?.name)} )} )} {!order?.user_review && pastOrderStatuses.includes(order?.status) && order?.customer_id && ( setOpenReviewModal(true)} /> )} {t('ORDER_DETAILS', 'Order Details')} {!!order?.comment && ( {`${t('ORDER_COMMENT', 'Order Comment')}: `} {commentDivide?.map((fragment: string, i: number) => ( {ordering?.project?.includes('delosi') && i === 1 ? t('TOTAL', 'Total') : ''}{fragment} ))} )} {order?.products?.length > 0 && order?.products.map((product: any, i: number) => ( ))} {t('SUBTOTAL', 'Subtotal')} {parsePrice(((order?.summary?.subtotal ?? order?.subtotal) + getIncludedTaxes()), { currency: getCurrenySymbol(order?.currency) })}
{(order?.summary?.discount > 0 ?? order?.discount > 0) && order?.offers?.length === 0 && ( {order?.offer_type === 1 ? ( {t('DISCOUNT', theme?.defaultLanguages?.DISCOUNT || 'Discount')}{' '} {`(${verifyDecimals(order?.offer_rate, parsePrice)}%)`} ) : ( {t('DISCOUNT', theme?.defaultLanguages?.DISCOUNT || 'Discount')} )} - {parsePrice(order?.summary?.discount ?? order?.discount, { currency: getCurrenySymbol(order?.currency) })}
)} { order?.offers?.length > 0 && order?.offers?.filter((offer: any) => offer?.target === 1)?.map((offer: any) => ( {t(offer.name?.toUpperCase?.()?.replace(/ /g, '_'), offer.name)} {offer.rate_type === 1 && ( {`(${verifyDecimals(offer?.rate, parsePrice)}%)`} )} - {parsePrice(offer?.summary?.discount, { currency: getCurrenySymbol(order?.currency) })}
)) } {order?.summary?.subtotal_with_discount > 0 && order?.summary?.discount > 0 && order?.summary?.total >= 0 && ( {t('SUBTOTAL_WITH_DISCOUNT', 'Subtotal with discount')} {order?.tax_type === 1 ? ( {parsePrice((order?.summary?.subtotal_with_discount + getIncludedTaxesDiscounts() ?? 0), { currency: getCurrenySymbol(order?.currency) })} ) : ( {parsePrice(order?.summary?.subtotal_with_discount ?? 0, { currency: getCurrenySymbol(order?.currency) })} )}
)} {order?.taxes?.length === 0 && order?.tax_type === 2 && order?.summary?.tax > 0 && ( {t('TAX', 'Tax')} {`(${verifyDecimals(order?.tax, parseNumber)}%)`} {parsePrice(order?.summary?.tax ?? 0, { currency: getCurrenySymbol(order?.currency) })}
)} { order?.fees?.length === 0 && order?.summary?.service_fee > 0 && ( {t('SERVICE_FEE', 'Service fee')} {`(${verifyDecimals(order?.service_fee, parseNumber)}%)`} {parsePrice(order?.summary?.service_fee ?? 0, { currency: getCurrenySymbol(order?.currency) })}
) } { order?.taxes?.length > 0 && order?.taxes?.filter((tax: any) => tax?.type === 2 && tax?.rate !== 0 && tax?.target === 'product').map((tax: any) => ( {t(tax?.name?.toUpperCase?.()?.replace(/ /g, '_'), tax?.name) || t('INHERIT_FROM_BUSINESS', 'Inherit from business')} {`(${verifyDecimals(tax?.rate, parseNumber)}%)`}{' '} {parsePrice(tax?.summary?.tax_after_discount ?? tax?.summary?.tax ?? 0, { currency: getCurrenySymbol(order?.currency) })}
)) } { order?.fees?.length > 0 && order?.fees?.filter((fee: any) => !(fee.fixed === 0 && fee.percentage === 0))?.map((fee: any) => ( {t(fee?.name?.toUpperCase?.()?.replace(/ /g, '_'), fee?.name) || t('INHERIT_FROM_BUSINESS', 'Inherit from business')} ({fee?.fixed > 0 && `${parsePrice(fee?.fixed, { currency: getCurrenySymbol(order?.currency) })} + `}{fee.percentage}%){' '} {parsePrice(fee?.summary?.fixed + (fee?.summary?.percentage_after_discount ?? fee?.summary?.percentage) ?? 0, { currency: getCurrenySymbol(order?.currency) })}
)) } { order?.offers?.length > 0 && order?.offers?.filter((offer: any) => offer?.target === 3)?.map((offer: any) => ( {t(offer.name?.toUpperCase?.()?.replace(/ /g, '_'), offer.name)} {offer.rate_type === 1 && ( {`(${verifyDecimals(offer?.rate, parsePrice)}%)`} )} - {parsePrice(offer?.summary?.discount, { currency: getCurrenySymbol(order?.currency) })}
)) } { typeof order?.summary?.delivery_price === 'number' && order.delivery_type !== 2 && ( {t('DELIVERY_FEE', 'Delivery Fee')} {parsePrice(order?.summary?.delivery_price + getIncludedTaxes(true), { currency: getCurrenySymbol(order?.currency) })}
) } { order?.taxes?.length > 0 && order?.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)}%)`} {parsePrice(tax?.summary?.tax_after_discount ?? tax?.summary?.tax ?? 0, { currency: getCurrenySymbol(order?.currency) })}
)) } { order?.offers?.length > 0 && order?.offers?.filter((offer: any) => offer?.target === 2)?.map((offer: any) => ( {t(offer.name?.toUpperCase?.()?.replace(/ /g, '_'), offer.name)} {offer.rate_type === 1 && ( {`(${verifyDecimals(offer?.rate, parsePrice, { currency: getCurrenySymbol(order?.currency) })}%)`} )} - {parsePrice(offer?.summary?.discount, { currency: getCurrenySymbol(order?.currency) })}
)) } {(order?.summary?.driver_tip > 0 || order?.driver_tip > 0) && order.delivery_type !== 2 && ( {t('DRIVER_TIP', 'Driver tip')} {order?.driver_tip > 0 && parseInt(configs?.driver_tip_type?.value, 10) === 2 && !parseInt(configs?.driver_tip_use_custom?.value, 10) && ( `(${verifyDecimals(order?.driver_tip, parseNumber)}%)` )} {parsePrice(order?.summary?.driver_tip ?? order?.totalDriverTip, { currency: getCurrenySymbol(order?.currency) })}
)} {t('TOTAL', 'Total')} {parsePrice(order?.summary?.total ?? order?.total, { currency: getCurrenySymbol(order?.currency) })}
{order?.payment_events?.length > 0 && ( {t('PAYMENTS', 'Payments')} {order?.payment_events?.map((event: any) => ( {event?.wallet_event ? walletName[event?.wallet_event?.wallet?.type]?.name : event?.paymethod?.gateway ? t(event?.paymethod?.gateway?.toUpperCase?.(), event?.paymethod?.name) : order?.paymethod?.id === event?.paymethod_id ? t(order?.paymethod?.gateway?.toUpperCase?.(), order?.paymethod?.name) : ''} {(event?.paymethod?.gateway === 'cash' && order?.cash) ? parsePrice(order?.cash, { currency: getCurrenySymbol(order?.currency) }) : `-${parsePrice(event?.amount, { currency: getCurrenySymbol(order?.currency) })}`} ))} )}
{!!order?.spot_number && ( {t('SPOT_NUMBER', 'Spot number')} {order?.spot_number}
)} {!!order?.vehicle && ( {t('VEHICLE', 'Vehicle')} {t('CAR_REGISTRATION', 'Car registration')} {order?.vehicle?.car_registration}
{t('COLOR', 'Color')} {order?.vehicle?.color}
{t('MODEL', 'Model')} {order?.vehicle?.model}
{t('TYPE', 'Type')} {order?.vehicle?.type}
)} setOpenReviewModal(false)} entireModal customClose > setOpenReviewModal(false)} onClose={() => setOpenReviewModal(false)} />
) }