mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
145 lines
4.2 KiB
TypeScript
145 lines
4.2 KiB
TypeScript
import * as Yup from 'yup';
|
|
import { Expense } from '@/types/api/expense';
|
|
import { formatDate } from '@/lib/helper';
|
|
|
|
type ExpenseFormSchemaType = {
|
|
location?: {
|
|
value: number;
|
|
label: string;
|
|
};
|
|
transaction_date?: string;
|
|
kandangs?: { id: number; name: string }[];
|
|
vendor?: {
|
|
value: number;
|
|
label: string;
|
|
};
|
|
existing_documents?: { name: string; url: string }[];
|
|
request_documents?: File[];
|
|
kandangExpenses: {
|
|
kandangId: number;
|
|
expenses: {
|
|
nonstock?: {
|
|
value: number;
|
|
label: string;
|
|
};
|
|
totalQuantity?: number;
|
|
totalExpense?: number;
|
|
notes?: string;
|
|
}[];
|
|
}[];
|
|
};
|
|
|
|
export const ExpenseRequestFormSchema: Yup.ObjectSchema<ExpenseFormSchemaType> =
|
|
Yup.object({
|
|
location: Yup.object({
|
|
value: Yup.number().min(1).required(),
|
|
label: Yup.string().required(),
|
|
}).required('Lokasi wajib diisi!'),
|
|
|
|
transaction_date: Yup.string().required('Tanggal transaksi wajib diisi!'),
|
|
kandangs: Yup.array()
|
|
.of(
|
|
Yup.object({
|
|
id: Yup.number().required('Kandang wajib dipilih!'),
|
|
name: Yup.string().required('Kandang wajib dipilih!'),
|
|
})
|
|
)
|
|
.min(1, 'Kandang wajib dipilih!')
|
|
.required('Kandang wajib dipilih!'),
|
|
|
|
vendor: Yup.object({
|
|
value: Yup.number().min(1).required(),
|
|
label: Yup.string().required(),
|
|
}).required('Vendor wajib diisi!'),
|
|
|
|
existing_documents: Yup.array().of(
|
|
Yup.object({
|
|
name: Yup.string().required(),
|
|
url: Yup.string().required(),
|
|
})
|
|
),
|
|
|
|
request_documents: Yup.array().of(Yup.mixed<File>().required()).optional(),
|
|
|
|
kandangExpenses: Yup.array()
|
|
.of(
|
|
Yup.object({
|
|
kandangId: Yup.number().min(1, 'Wajib memilih kandang!').required(),
|
|
expenses: Yup.array()
|
|
.of(
|
|
Yup.object({
|
|
nonstock: Yup.object({
|
|
value: Yup.number().min(1).required(),
|
|
label: Yup.string().required(),
|
|
}).required('Nonstock wajib diisi!'),
|
|
totalQuantity: Yup.number().required(
|
|
'Total kuantitas wajib diisi!'
|
|
),
|
|
totalExpense: Yup.number().required('Total biaya wajib diisi!'),
|
|
notes: Yup.string(),
|
|
})
|
|
)
|
|
.min(1, 'Kandang harus memiliki setidaknya 1 biaya!')
|
|
.required('Biaya kandang wajib diisi!'),
|
|
})
|
|
)
|
|
.min(1, 'Biaya kandang wajib diisi!')
|
|
.required('Biaya kandang wajib diisi!'),
|
|
});
|
|
|
|
export const UpdateExpenseRequestFormSchema = ExpenseRequestFormSchema;
|
|
|
|
export const UploadRequestDocumentsFormSchema = Yup.object({
|
|
request_documents: Yup.array().of(Yup.mixed<File>().required()).required(),
|
|
});
|
|
|
|
export type ExpenseRequestFormValues = Yup.InferType<
|
|
typeof ExpenseRequestFormSchema
|
|
>;
|
|
|
|
export type UploadRequestDocumentsFormValues = Yup.InferType<
|
|
typeof UploadRequestDocumentsFormSchema
|
|
>;
|
|
|
|
export const getExpenseFormInitialValues = (
|
|
initialValues?: Expense
|
|
): ExpenseRequestFormValues => {
|
|
return {
|
|
location: initialValues?.location
|
|
? {
|
|
value: initialValues.location.id,
|
|
label: initialValues.location.name,
|
|
}
|
|
: undefined,
|
|
transaction_date: initialValues?.transaction_date
|
|
? formatDate(initialValues.transaction_date, 'YYYY-MM-DD')
|
|
: undefined,
|
|
kandangs: initialValues?.kandangs.map((kandang) => ({
|
|
id: kandang.id,
|
|
name: kandang.name,
|
|
})),
|
|
vendor: initialValues?.vendor
|
|
? {
|
|
value: initialValues.vendor.id,
|
|
label: initialValues.vendor.name,
|
|
}
|
|
: undefined,
|
|
existing_documents: initialValues?.request_documents,
|
|
request_documents: [],
|
|
kandangExpenses: initialValues?.kandang_expenses
|
|
? initialValues.kandang_expenses.map((kandangExpense) => ({
|
|
kandangId: kandangExpense.kandang.id,
|
|
expenses: kandangExpense.expenses.map((expenseItem) => ({
|
|
nonstock: {
|
|
value: expenseItem.nonstock.id,
|
|
label: expenseItem.nonstock.name,
|
|
},
|
|
totalQuantity: expenseItem.total_quantity,
|
|
totalExpense: expenseItem.total_expense,
|
|
notes: expenseItem.notes,
|
|
})),
|
|
}))
|
|
: [],
|
|
};
|
|
};
|