Uecko_ERP/modules/customer-invoices/src/web/components/editor/invoice-totals.tsx

83 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-10-13 17:41:21 +00:00
import { formatCurrency } from '@erp/core';
2025-09-29 18:22:59 +00:00
import { Description, FieldGroup, Fieldset, Legend } from "@repo/rdx-ui/components";
import { Input, Label, Separator } from "@repo/shadcn-ui/components";
import { CalculatorIcon } from "lucide-react";
2025-10-12 10:43:06 +00:00
import { ComponentProps } from 'react';
import { Controller, useFormContext } from "react-hook-form";
2025-10-13 17:41:21 +00:00
import { useInvoiceContext } from '../../context';
2025-09-29 18:22:59 +00:00
import { useTranslation } from "../../i18n";
2025-10-12 10:43:06 +00:00
import { InvoiceFormData } from "../../schemas";
2025-09-29 18:22:59 +00:00
2025-10-12 10:43:06 +00:00
export const InvoiceTotals = (props: ComponentProps<"fieldset">) => {
2025-09-29 18:22:59 +00:00
const { t } = useTranslation();
2025-10-12 10:43:06 +00:00
const { control, getValues } = useFormContext<InvoiceFormData>();
2025-10-13 17:41:21 +00:00
const { currency_code, language_code } = useInvoiceContext();
2025-09-29 18:22:59 +00:00
return (
2025-10-12 10:43:06 +00:00
<Fieldset {...props}>
2025-09-29 18:22:59 +00:00
<Legend className='flex items-center gap-2 text-foreground'>
2025-10-12 10:43:06 +00:00
<CalculatorIcon className='size-5' /> {t("form_groups.totals.title")}
2025-09-29 18:22:59 +00:00
</Legend>
<Description>{t("form_groups.totals.description")}</Description>
<FieldGroup className='grid grid-cols-1'>
<div className='space-y-3'>
<div className='flex justify-between items-center'>
2025-10-13 17:41:21 +00:00
<Label className='text-sm'>Subtotal</Label>
<span className='font-medium tabular-nums'>{formatCurrency(getValues('subtotal_amount'), 2, currency_code, language_code)}</span>
2025-09-29 18:22:59 +00:00
</div>
<div className='flex justify-between items-center gap-4'>
2025-10-13 17:41:21 +00:00
<Label className='text-sm'>Descuento global (%)</Label>
2025-09-29 18:22:59 +00:00
<div className='flex items-center gap-2'>
2025-10-12 10:43:06 +00:00
<Controller
control={control}
name={"discount_percentage"}
render={({
field, fieldState
2025-10-13 17:41:21 +00:00
}) => (
<Input
readOnly={false}
value={field.value}
onChange={field.onChange}
disabled={fieldState.isValidating}
onBlur={field.onBlur}
className='w-20 text-right'
/>)}
2025-09-29 18:22:59 +00:00
/>
</div>
</div>
<div className='flex justify-between items-center'>
2025-10-13 17:41:21 +00:00
<Label className='text-sm'>Importe del descuento</Label>
2025-10-12 10:43:06 +00:00
<span className='font-medium text-destructive tabular-nums'>
2025-10-13 17:41:21 +00:00
-{formatCurrency(getValues("discount_amount"), 2, currency_code, language_code)}
2025-09-29 18:22:59 +00:00
</span>
</div>
2025-10-13 17:41:21 +00:00
<Separator className='bg-muted-foreground' />
2025-09-29 18:22:59 +00:00
<div className='flex justify-between items-center'>
2025-10-13 17:41:21 +00:00
<Label className='text-sm'>Base imponible</Label>
<span className='font-medium tabular-nums'>{formatCurrency(getValues('taxable_amount'), 2, currency_code, language_code)}</span>
2025-09-29 18:22:59 +00:00
</div>
<div className='flex justify-between items-center'>
2025-10-13 17:41:21 +00:00
<Label className='text-sm'>Total de impuestos</Label>
<span className='font-medium tabular-nums'>{formatCurrency(getValues('taxes_amount'), 2, currency_code, language_code)}</span>
2025-09-29 18:22:59 +00:00
</div>
2025-10-13 17:41:21 +00:00
<Separator className='bg-muted-foreground h-0.5' />
2025-09-29 18:22:59 +00:00
<div className='flex justify-between items-center'>
2025-10-13 17:41:21 +00:00
<Label className='text-xl font-semibold'>Total de la factura</Label>
2025-10-12 10:43:06 +00:00
<span className='text-xl font-bold text-primary tabular-nums'>
2025-10-13 17:41:21 +00:00
{formatCurrency(getValues('total_amount'), 2, currency_code, language_code)}
2025-09-29 18:22:59 +00:00
</span>
</div>
</div>
</FieldGroup>
</Fieldset>
);
};