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

74 lines
2.6 KiB
TypeScript
Raw Normal View History

2025-09-29 18:22:59 +00:00
import { Description, FieldGroup, Fieldset, Legend } from "@repo/rdx-ui/components";
import { Badge } from "@repo/shadcn-ui/components";
import { ReceiptIcon } from "lucide-react";
2025-10-12 10:43:06 +00:00
import { ComponentProps } from 'react';
2025-09-29 18:22:59 +00:00
import { useFormContext, useWatch } from "react-hook-form";
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 InvoiceTaxSummary = (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-09-29 18:22:59 +00:00
const taxes = useWatch({
control,
name: "taxes",
defaultValue: [],
});
2025-10-12 10:43:06 +00:00
console.log(getValues());
2025-09-29 18:22:59 +00:00
2025-10-12 10:43:06 +00:00
const formatCurrency = (amount: number) => {
2025-09-29 18:22:59 +00:00
return new Intl.NumberFormat("es-ES", {
style: "currency",
2025-10-12 10:43:06 +00:00
currency: "EUR",
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}).format(amount);
2025-09-29 18:22:59 +00:00
};
2025-10-12 10:43:06 +00:00
const displayTaxes = taxes || [];
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
<ReceiptIcon className='size-5' /> {t("form_groups.tax_resume.title")}
2025-09-29 18:22:59 +00:00
</Legend>
<Description>{t("form_groups.tax_resume.description")}</Description>
<FieldGroup className='grid grid-cols-1'>
<div className='space-y-3'>
{displayTaxes.map((tax, index) => (
2025-10-12 10:43:06 +00:00
<div key={`${tax.tax_code}-${index}`} className='border rounded-lg p-3 space-y-2'>
<div className='flex items-center justify-between mb-2 '>
2025-09-29 18:22:59 +00:00
<Badge variant='secondary' className='text-xs'>
2025-10-12 10:43:06 +00:00
{tax.tax_label}
2025-09-29 18:22:59 +00:00
</Badge>
</div>
2025-10-12 10:43:06 +00:00
<div className='space-y-2 text-sm'>
2025-09-29 18:22:59 +00:00
<div className='flex justify-between'>
2025-10-12 10:43:06 +00:00
<span className='text-muted-foreground'>Base para el impuesto:</span>
<span className='font-medium tabular-nums'>{formatCurrency(tax.taxable_amount)}</span>
2025-09-29 18:22:59 +00:00
</div>
<div className='flex justify-between'>
2025-10-12 10:43:06 +00:00
<span className='text-muted-foreground'>Importe de impuesto:</span>
<span className='font-medium text-primary tabular-nums'>
2025-09-29 18:22:59 +00:00
{formatCurrency(tax.taxes_amount)}
</span>
</div>
</div>
</div>
))}
{displayTaxes.length === 0 && (
<div className='text-center py-6 text-muted-foreground'>
<ReceiptIcon className='h-8 w-8 mx-auto mb-2 opacity-50' />
<p className='text-sm'>No hay impuestos aplicados</p>
</div>
)}
</div>
</FieldGroup>
</Fieldset>
);
};