import * as React from 'react';
import { useTheme } from 'styled-components/native';
import Box from '@wcpos/components/src/box';
import ErrorBoundary from '@wcpos/components/src/error-boundary';
import Text from '@wcpos/components/src/text';
import { CustomerNote } from './totals/customer-note';
import { Taxes } from './totals/taxes';
import { useT } from '../../../../contexts/translations';
import { useTaxDisplay } from '../../hooks/taxes/use-tax-display';
import { useCurrentOrderCurrencyFormat } from '../../hooks/use-current-order-currency-format';
import { useOrderTotals } from '../hooks/use-order-totals';
/**
*
*/
const Totals = () => {
const theme = useTheme();
const t = useT();
const { format } = useCurrentOrderCurrencyFormat();
const { inclOrExcl } = useTaxDisplay({ context: 'cart' });
const {
subtotal,
subtotal_tax,
fee_total,
fee_tax,
tax_lines,
total_tax,
discount_tax,
discount_total,
shipping_tax,
shipping_total,
} = useOrderTotals();
/**
* Helpers
*/
const hasSubtotal = parseFloat(subtotal) !== 0;
const hasDiscount = parseFloat(discount_total) !== 0;
const hasShipping = parseFloat(shipping_total) !== 0;
const hasFee = parseFloat(fee_total) !== 0;
const hasTax = parseFloat(total_tax) !== 0;
const hasTotals = hasSubtotal || hasDiscount || hasShipping || hasFee || hasTax;
/**
*
*/
const displaySubtotal =
inclOrExcl === 'incl' ? parseFloat(subtotal) + parseFloat(subtotal_tax) : subtotal;
const displayDiscountTotal =
inclOrExcl === 'incl' ? parseFloat(discount_total) + parseFloat(discount_tax) : discount_total;
const displayFeeTotal =
inclOrExcl === 'incl' ? parseFloat(fee_total) + parseFloat(fee_tax) : fee_total;
const displayShippingTotal =
inclOrExcl === 'incl' ? parseFloat(shipping_total) + parseFloat(shipping_tax) : shipping_total;
return (
<>
{hasTotals ? (
{t('Subtotal', { _tags: 'core' })}:
{format(displaySubtotal)}
{
// Discounts
hasDiscount && (
{t('Discount', { _tags: 'core' })}:
{format(`-${displayDiscountTotal}`)}
)
}
{
// Fees
hasFee && (
{t('Fees', { _tags: 'core' })}:
{format(displayFeeTotal)}
)
}
{
// Shipping
hasShipping && (
{t('Shipping', { _tags: 'core' })}:
{format(displayShippingTotal)}
)
}
{hasTax ? (
) : null}
) : null}
>
);
};
export default Totals;