commit14
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Head, router, useForm, usePage } from '@inertiajs/react';
|
||||
import { Head, Link, router, useForm } from '@inertiajs/react';
|
||||
import { useState } from 'react';
|
||||
import {
|
||||
FormErrors,
|
||||
@@ -32,6 +32,7 @@ type Invoice = {
|
||||
issued_on: string;
|
||||
status: string;
|
||||
created_by: number;
|
||||
canValidate: boolean;
|
||||
subtotal_minor: number;
|
||||
tax_minor: number;
|
||||
total_minor: number;
|
||||
@@ -51,10 +52,14 @@ export default function Invoices({
|
||||
payablesAvailable = false,
|
||||
stockReturns = [],
|
||||
stockReversals = [],
|
||||
focusInvoiceId = null,
|
||||
focusOrderId = null,
|
||||
}: {
|
||||
invoices: Page<Invoice>;
|
||||
orders: { id: number; number: string; supplier: string; items: Source[] }[];
|
||||
canManage: boolean;
|
||||
focusInvoiceId?: number | null;
|
||||
focusOrderId?: number | null;
|
||||
payablesAvailable?: boolean;
|
||||
stockReturns?: {
|
||||
reference: string;
|
||||
@@ -72,8 +77,9 @@ export default function Invoices({
|
||||
event_on: string;
|
||||
}[];
|
||||
}) {
|
||||
const { auth } = usePage().props;
|
||||
const [orderId, setOrderId] = useState('');
|
||||
const [orderId, setOrderId] = useState(
|
||||
focusOrderId === null ? '' : String(focusOrderId),
|
||||
);
|
||||
const [validationError, setValidationError] = useState('');
|
||||
const [validating, setValidating] = useState<number | null>(null);
|
||||
const form = useForm({
|
||||
@@ -82,7 +88,15 @@ export default function Invoices({
|
||||
subtotal: '',
|
||||
tax: '',
|
||||
total: '',
|
||||
items: [] as Line[],
|
||||
items: (orders
|
||||
.find((source) => source.id === focusOrderId)
|
||||
?.items.map((line) => ({
|
||||
purchase_order_item_id: line.id,
|
||||
quantity: '',
|
||||
unit: line.unit,
|
||||
unit_price: (line.unit_price_minor / 100).toFixed(2),
|
||||
tax_rate: String(line.tax_rate),
|
||||
})) ?? []) as Line[],
|
||||
});
|
||||
const order = orders.find((o) => String(o.id) === orderId);
|
||||
return (
|
||||
@@ -148,10 +162,15 @@ export default function Invoices({
|
||||
</ul>
|
||||
</Panel>
|
||||
)}
|
||||
{canManage && (
|
||||
{(focusInvoiceId !== null || focusOrderId !== null) && (
|
||||
<Link href="/facturas" className="underline">
|
||||
Todas as facturas / seleccionar outra encomenda
|
||||
</Link>
|
||||
)}
|
||||
{canManage && focusInvoiceId === null && (
|
||||
<Panel
|
||||
title="Registar factura"
|
||||
description="Transcreva os valores do documento do fornecedor. A conferência exige coincidência exacta; o registo não valida automaticamente a factura."
|
||||
description="Reutilize a encomenda e indique os dados novos do documento do fornecedor. Unidade, preço e IVA vêm da encomenda; os totais do documento continuam a exigir coincidência exacta e conferência independente."
|
||||
>
|
||||
<form
|
||||
className="grid gap-4"
|
||||
@@ -159,9 +178,17 @@ export default function Invoices({
|
||||
e.preventDefault();
|
||||
form.transform((d) => ({
|
||||
...d,
|
||||
items: d.items.filter(
|
||||
(l) => l.quantity !== '',
|
||||
),
|
||||
items: d.items
|
||||
.filter((l) => l.quantity !== '')
|
||||
.map(
|
||||
({
|
||||
purchase_order_item_id,
|
||||
quantity,
|
||||
}) => ({
|
||||
purchase_order_item_id,
|
||||
quantity,
|
||||
}),
|
||||
),
|
||||
}));
|
||||
form.post(`/ordens/${orderId}/facturas`, {
|
||||
onSuccess: () => {
|
||||
@@ -268,25 +295,31 @@ export default function Invoices({
|
||||
tax_rate: 'IVA (%)',
|
||||
}[key]
|
||||
}
|
||||
<Input
|
||||
value={line[key]}
|
||||
onChange={(e) =>
|
||||
form.setData(
|
||||
'items',
|
||||
form.data.items.map(
|
||||
(l, i) =>
|
||||
i === index
|
||||
? {
|
||||
...l,
|
||||
[key]: e
|
||||
.target
|
||||
.value,
|
||||
}
|
||||
: l,
|
||||
),
|
||||
)
|
||||
}
|
||||
/>
|
||||
{key === 'quantity' ? (
|
||||
<Input
|
||||
value={line[key]}
|
||||
onChange={(e) =>
|
||||
form.setData(
|
||||
'items',
|
||||
form.data.items.map(
|
||||
(l, i) =>
|
||||
i === index
|
||||
? {
|
||||
...l,
|
||||
[key]: e
|
||||
.target
|
||||
.value,
|
||||
}
|
||||
: l,
|
||||
),
|
||||
)
|
||||
}
|
||||
/>
|
||||
) : (
|
||||
<p className="rounded border bg-muted/30 px-3 py-2">
|
||||
{line[key]}
|
||||
</p>
|
||||
)}
|
||||
</label>
|
||||
))}
|
||||
</fieldset>
|
||||
@@ -334,45 +367,55 @@ export default function Invoices({
|
||||
</p>
|
||||
)}
|
||||
{invoices.data.map((invoice) => (
|
||||
<Panel
|
||||
key={invoice.id}
|
||||
title={`${invoice.number} · ${invoice.snapshot.supplier.name}`}
|
||||
>
|
||||
<p>
|
||||
{invoice.snapshot.order_number} ·{' '}
|
||||
{invoice.issued_on} ·{' '}
|
||||
{invoice.status === 'validated'
|
||||
? 'Validada'
|
||||
: 'Pendente de validação'}
|
||||
</p>
|
||||
<p>
|
||||
Subtotal {money(invoice.subtotal_minor)} · IVA{' '}
|
||||
{money(invoice.tax_minor)} · Total{' '}
|
||||
{money(invoice.total_minor)}
|
||||
</p>
|
||||
{payablesAvailable && (
|
||||
<p className="my-3">
|
||||
<a
|
||||
className="underline"
|
||||
href={`/obrigacoes-fornecedores?invoice=${invoice.id}`}
|
||||
>
|
||||
Consultar ou preparar obrigação desta
|
||||
factura
|
||||
</a>
|
||||
<div key={invoice.id} id={`invoice-${invoice.id}`}>
|
||||
<Panel
|
||||
title={`${invoice.number} · ${invoice.snapshot.supplier.name}`}
|
||||
>
|
||||
<p>
|
||||
{invoice.snapshot.order_number} ·{' '}
|
||||
{invoice.issued_on} ·{' '}
|
||||
{invoice.status === 'validated'
|
||||
? 'Validada'
|
||||
: 'Pendente de validação'}
|
||||
</p>
|
||||
)}
|
||||
<ul className="my-3 space-y-2">
|
||||
{invoice.items.map((l) => (
|
||||
<li key={l.id}>
|
||||
{l.snapshot.description} ·{' '}
|
||||
{l.quantity_milli / 1000} {l.snapshot.unit}{' '}
|
||||
· {money(l.subtotal_minor + l.tax_minor)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
{canManage &&
|
||||
invoice.status === 'pending' &&
|
||||
invoice.created_by !== auth.user?.id && (
|
||||
<p>
|
||||
Subtotal {money(invoice.subtotal_minor)} · IVA{' '}
|
||||
{money(invoice.tax_minor)} · Total{' '}
|
||||
{money(invoice.total_minor)}
|
||||
</p>
|
||||
{payablesAvailable && (
|
||||
<p className="my-3">
|
||||
<a
|
||||
className="underline"
|
||||
href={`/obrigacoes-fornecedores?invoice=${invoice.id}`}
|
||||
>
|
||||
Consultar ou preparar obrigação desta
|
||||
factura
|
||||
</a>
|
||||
</p>
|
||||
)}
|
||||
{invoice.status === 'validated' && (
|
||||
<p className="my-3">
|
||||
<Link
|
||||
className="underline"
|
||||
href={`/pagamentos?invoice_id=${invoice.id}`}
|
||||
>
|
||||
Consultar pagamentos externos desta
|
||||
factura
|
||||
</Link>
|
||||
</p>
|
||||
)}
|
||||
<ul className="my-3 space-y-2">
|
||||
{invoice.items.map((l) => (
|
||||
<li key={l.id}>
|
||||
{l.snapshot.description} ·{' '}
|
||||
{l.quantity_milli / 1000}{' '}
|
||||
{l.snapshot.unit} ·{' '}
|
||||
{money(l.subtotal_minor + l.tax_minor)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
{invoice.canValidate && (
|
||||
<Button
|
||||
disabled={validating !== null}
|
||||
onClick={() => {
|
||||
@@ -397,7 +440,8 @@ export default function Invoices({
|
||||
Conferir e validar
|
||||
</Button>
|
||||
)}
|
||||
</Panel>
|
||||
</Panel>
|
||||
</div>
|
||||
))}
|
||||
{invoices.data.length === 0 && (
|
||||
<p>Sem facturas registadas nesta empresa.</p>
|
||||
|
||||
Reference in New Issue
Block a user