@@ -1,5 +1,9 @@
|
||||
import { Head, Link, router, useForm } from '@inertiajs/react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import {
|
||||
ReceiptConversionFields,
|
||||
type ReceiptConversion,
|
||||
} from '@/components/receipt-conversion';
|
||||
import {
|
||||
FormErrors,
|
||||
PageTitle,
|
||||
@@ -39,10 +43,16 @@ type RecordVersion = {
|
||||
warehouse: { name: string };
|
||||
substitute?: { name: string; code: string; unit: string };
|
||||
order?: { number: string };
|
||||
order_line?: { unit: string };
|
||||
quantity_conversion?: ReceiptConversion & {
|
||||
document_unit: string;
|
||||
stock_unit: string;
|
||||
};
|
||||
};
|
||||
};
|
||||
type Source = {
|
||||
id: number;
|
||||
unit?: string;
|
||||
label: string;
|
||||
stock_item_id: number | null;
|
||||
quantity_milli: number;
|
||||
@@ -50,6 +60,7 @@ type Source = {
|
||||
needed_on?: string;
|
||||
};
|
||||
type Props = {
|
||||
conversionQaEnabled?: boolean;
|
||||
recordFilters?: CoordinationFilters;
|
||||
lineSearch?: string;
|
||||
lineFormKey?: string | null;
|
||||
@@ -109,6 +120,14 @@ const quantity = (milli: number) =>
|
||||
(milli / 1000).toLocaleString('pt-MZ', { maximumFractionDigits: 3 });
|
||||
|
||||
function Prepare({ props, old }: { props: Props; old?: RecordVersion }) {
|
||||
const [conversion, setConversion] = useState<ReceiptConversion>(
|
||||
old?.snapshot.quantity_conversion ?? {
|
||||
numerator: '',
|
||||
denominator: '',
|
||||
source: '',
|
||||
version: '',
|
||||
},
|
||||
);
|
||||
const [orderLines, setOrderLines] = useState(props.orderLines);
|
||||
const [requestLines, setRequestLines] = useState(props.requestLines);
|
||||
const lineFormKey = `${props.kind}:${old?.id ?? 'new'}`;
|
||||
@@ -162,6 +181,19 @@ function Prepare({ props, old }: { props: Props; old?: RecordVersion }) {
|
||||
reason: '',
|
||||
data: defaults,
|
||||
});
|
||||
const documentUnit =
|
||||
orderLines.find(
|
||||
(line) => line.id === Number(form.data.data.purchase_order_item_id),
|
||||
)?.unit ?? old?.snapshot.order_line?.unit;
|
||||
const stockUnit = props.items.find(
|
||||
(item) => item.id === Number(form.data.data.stock_item_id),
|
||||
)?.unit;
|
||||
const needsConversion =
|
||||
props.conversionQaEnabled &&
|
||||
props.kind === 'delivery' &&
|
||||
documentUnit &&
|
||||
stockUnit &&
|
||||
documentUnit.toLowerCase() !== stockUnit.toLowerCase();
|
||||
const update = (key: string, value: Fields[string]) =>
|
||||
form.setData('data', { ...form.data.data, [key]: value });
|
||||
const field = (
|
||||
@@ -249,7 +281,13 @@ function Prepare({ props, old }: { props: Props; old?: RecordVersion }) {
|
||||
);
|
||||
delete data.quantity;
|
||||
}
|
||||
return { ...input, data };
|
||||
return {
|
||||
...input,
|
||||
data: {
|
||||
...data,
|
||||
...(needsConversion ? { conversion } : {}),
|
||||
},
|
||||
};
|
||||
});
|
||||
form.post(`/compras/fornecimento/${props.kind}`, {
|
||||
preserveScroll: true,
|
||||
@@ -403,6 +441,14 @@ function Prepare({ props, old }: { props: Props; old?: RecordVersion }) {
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
{needsConversion && (
|
||||
<ReceiptConversionFields
|
||||
value={conversion}
|
||||
onChange={setConversion}
|
||||
documentUnit={documentUnit}
|
||||
stockUnit={stockUnit}
|
||||
/>
|
||||
)}
|
||||
{field(
|
||||
'quantity',
|
||||
props.kind === 'delivery'
|
||||
@@ -767,7 +813,8 @@ function Facts({
|
||||
: 'Quantidade: '}
|
||||
<strong>
|
||||
{quantity(Number(r.data.quantity_milli))}{' '}
|
||||
{r.snapshot.item.unit}
|
||||
{r.snapshot.quantity_conversion?.document_unit ??
|
||||
r.snapshot.item.unit}
|
||||
</strong>
|
||||
{r.data.applications &&
|
||||
` · Pendente: ${quantity(pending ?? Number(r.data.quantity_milli) - applied)}`}
|
||||
|
||||
Reference in New Issue
Block a user