@@ -1,5 +1,17 @@
|
||||
import { Head, Link, router, useForm } from '@inertiajs/react';
|
||||
import { useState } from 'react';
|
||||
import {
|
||||
Head,
|
||||
Link,
|
||||
router,
|
||||
useForm,
|
||||
usePage,
|
||||
useRemember,
|
||||
} from '@inertiajs/react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import BudgetDistributionFields, {
|
||||
type BudgetDistributionInput,
|
||||
type BudgetDistributionOption,
|
||||
} from '@/components/budget-distribution-fields';
|
||||
import {
|
||||
FormErrors,
|
||||
PageTitle,
|
||||
@@ -14,6 +26,7 @@ import type { Page } from '@/types/inventory';
|
||||
|
||||
type Source = {
|
||||
id: number;
|
||||
requisition_item_id?: number;
|
||||
description: string;
|
||||
unit: string;
|
||||
unit_price_minor: number;
|
||||
@@ -67,6 +80,7 @@ export default function Invoices({
|
||||
currency?: string;
|
||||
supplier: string;
|
||||
items: Source[];
|
||||
budgetDistributionOptions?: BudgetDistributionOption[] | null;
|
||||
}[];
|
||||
canManage: boolean;
|
||||
focusInvoiceId?: number | null;
|
||||
@@ -89,17 +103,31 @@ export default function Invoices({
|
||||
event_on: string;
|
||||
}[];
|
||||
}) {
|
||||
const [orderId, setOrderId] = useState(
|
||||
const actor = usePage().props.auth?.user;
|
||||
const historyKey = `invoice-preparation:${actor?.tenant_id}:${actor?.id}:${focusOrderId ?? 'all'}`;
|
||||
const [orderId, setOrderId] = useRemember(
|
||||
focusOrderId === null ? '' : String(focusOrderId),
|
||||
`${historyKey}:order`,
|
||||
);
|
||||
const [validationError, setValidationError] = useState('');
|
||||
const [validating, setValidating] = useState<number | null>(null);
|
||||
const form = useForm({
|
||||
const initialData = {
|
||||
number: '',
|
||||
issued_on: '',
|
||||
subtotal: '',
|
||||
tax: '',
|
||||
total: '',
|
||||
reason: '',
|
||||
budget_distribution_preparation: (
|
||||
orders.find((source) => source.id === focusOrderId)
|
||||
?.budgetDistributionOptions ?? []
|
||||
).map(({ allocation_key, line_id, centre_id }) => ({
|
||||
allocation_key,
|
||||
line_id,
|
||||
centre_id,
|
||||
subtotal: '',
|
||||
total: '',
|
||||
})) as BudgetDistributionInput[],
|
||||
items: (orders
|
||||
.find((source) => source.id === focusOrderId)
|
||||
?.items.map((line) => ({
|
||||
@@ -109,8 +137,29 @@ export default function Invoices({
|
||||
unit_price: (line.unit_price_minor / 100).toFixed(2),
|
||||
tax_rate: String(line.tax_rate),
|
||||
})) ?? []) as Line[],
|
||||
});
|
||||
};
|
||||
const form = useForm(historyKey, initialData);
|
||||
const formElement = useRef<HTMLFormElement>(null);
|
||||
const dirty = hasPreparationChanges(initialData, form.data);
|
||||
useEffect(() => {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', {
|
||||
detail: { form: formElement.current, dirty },
|
||||
}),
|
||||
);
|
||||
}, [dirty]);
|
||||
const order = orders.find((o) => String(o.id) === orderId);
|
||||
const activeLineIds = form.data.items
|
||||
.filter((line) => line.quantity !== '')
|
||||
.map(
|
||||
(line) =>
|
||||
order?.items.find(
|
||||
(source) => source.id === line.purchase_order_item_id,
|
||||
)?.requisition_item_id,
|
||||
);
|
||||
const activeAllocations = form.data.budget_distribution_preparation.filter(
|
||||
(row) => activeLineIds.includes(row.line_id),
|
||||
);
|
||||
return (
|
||||
<>
|
||||
<Head title="Facturas" />
|
||||
@@ -190,11 +239,19 @@ export default function Invoices({
|
||||
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
|
||||
ref={formElement}
|
||||
className="grid gap-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.transform((d) => ({
|
||||
...d,
|
||||
budget_distribution_preparation:
|
||||
d.budget_distribution_preparation.filter(
|
||||
(row) =>
|
||||
activeLineIds.includes(
|
||||
row.line_id,
|
||||
),
|
||||
),
|
||||
items: d.items
|
||||
.filter((l) => l.quantity !== '')
|
||||
.map(
|
||||
@@ -224,6 +281,30 @@ export default function Invoices({
|
||||
value={orderId}
|
||||
onChange={(e) => {
|
||||
setOrderId(e.target.value);
|
||||
form.setData('reason', '');
|
||||
form.setData(
|
||||
'budget_distribution_preparation',
|
||||
(
|
||||
orders.find(
|
||||
(source) =>
|
||||
String(source.id) ===
|
||||
e.target.value,
|
||||
)?.budgetDistributionOptions ??
|
||||
[]
|
||||
).map(
|
||||
({
|
||||
allocation_key,
|
||||
line_id,
|
||||
centre_id,
|
||||
}) => ({
|
||||
allocation_key,
|
||||
line_id,
|
||||
centre_id,
|
||||
subtotal: '',
|
||||
total: '',
|
||||
}),
|
||||
),
|
||||
);
|
||||
form.setData(
|
||||
'items',
|
||||
orders
|
||||
@@ -371,6 +452,42 @@ export default function Invoices({
|
||||
</label>
|
||||
),
|
||||
)}
|
||||
{order?.budgetDistributionOptions != null && (
|
||||
<div className="space-y-3">
|
||||
{order.currency !== 'MZN' && (
|
||||
<Link
|
||||
className="underline"
|
||||
href={`/ordens/${order.id}/reafectacao`}
|
||||
>
|
||||
Conferir ou reafectar compromisso em
|
||||
MZN
|
||||
</Link>
|
||||
)}
|
||||
<BudgetDistributionFields
|
||||
options={
|
||||
order.budgetDistributionOptions
|
||||
}
|
||||
rows={activeAllocations}
|
||||
reason={form.data.reason}
|
||||
onReason={(value) =>
|
||||
form.setData('reason', value)
|
||||
}
|
||||
onChange={(rows) =>
|
||||
form.setData(
|
||||
'budget_distribution_preparation',
|
||||
form.data.budget_distribution_preparation.map(
|
||||
(row) =>
|
||||
rows.find(
|
||||
(changed) =>
|
||||
changed.allocation_key ===
|
||||
row.allocation_key,
|
||||
) ?? row,
|
||||
),
|
||||
)
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<Button disabled={!orderId || form.processing}>
|
||||
Registar para conferência independente
|
||||
</Button>
|
||||
|
||||
Reference in New Issue
Block a user