mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
169 lines
4.9 KiB
TypeScript
169 lines
4.9 KiB
TypeScript
import * as Yup from 'yup';
|
|
import { Expense } from '@/types/api/expense';
|
|
import { formatDate } from '@/lib/helper';
|
|
|
|
type ExpenseFormSchemaType = {
|
|
category?: {
|
|
value: 'BOP' | 'NON-BOP';
|
|
label: 'BOP' | 'NON-BOP';
|
|
};
|
|
location?: {
|
|
value: number;
|
|
label: string;
|
|
};
|
|
transaction_date?: string;
|
|
kandangs?: { id: number; name: string }[];
|
|
supplier?: {
|
|
value: number;
|
|
label: string;
|
|
};
|
|
existing_documents?: { id: number; name: string; url: string }[];
|
|
deleted_documents?: number[];
|
|
documents?: File[];
|
|
cost_per_kandangs: {
|
|
kandang_id: number;
|
|
cost_items: {
|
|
nonstock?: {
|
|
value: number;
|
|
label: string;
|
|
};
|
|
quantity?: number;
|
|
total_cost?: number;
|
|
notes?: string;
|
|
}[];
|
|
}[];
|
|
};
|
|
|
|
export const ExpenseRequestFormSchema: Yup.ObjectSchema<ExpenseFormSchemaType> =
|
|
Yup.object({
|
|
category: Yup.object({
|
|
value: Yup.string().oneOf(['BOP', 'NON-BOP']).required(),
|
|
label: Yup.string().oneOf(['BOP', 'NON-BOP']).required(),
|
|
}).required('Kategori wajib diisi!'),
|
|
|
|
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!'),
|
|
|
|
supplier: Yup.object({
|
|
value: Yup.number().min(1).required(),
|
|
label: Yup.string().required(),
|
|
}).required('Vendor wajib diisi!'),
|
|
|
|
existing_documents: Yup.array().of(
|
|
Yup.object({
|
|
id: Yup.number().required(),
|
|
name: Yup.string().required(),
|
|
url: Yup.string().required(),
|
|
})
|
|
),
|
|
|
|
deleted_documents: Yup.array().of(Yup.number().required()).optional(),
|
|
|
|
documents: Yup.array().of(Yup.mixed<File>().required()).optional(),
|
|
|
|
cost_per_kandangs: Yup.array()
|
|
.of(
|
|
Yup.object({
|
|
kandang_id: Yup.number().min(1, 'Wajib memilih kandang!').required(),
|
|
cost_items: Yup.array()
|
|
.of(
|
|
Yup.object({
|
|
nonstock: Yup.object({
|
|
value: Yup.number().min(1).required(),
|
|
label: Yup.string().required(),
|
|
}).required('Nonstock wajib diisi!'),
|
|
quantity: Yup.number().required('Total kuantitas wajib diisi!'),
|
|
total_cost: 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({
|
|
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 {
|
|
category: initialValues?.category
|
|
? {
|
|
value: initialValues.category,
|
|
label: initialValues.category,
|
|
}
|
|
: undefined,
|
|
location: initialValues?.location
|
|
? {
|
|
value: initialValues.location.id,
|
|
label: initialValues.location.name,
|
|
}
|
|
: undefined,
|
|
transaction_date: initialValues?.expense_date
|
|
? formatDate(initialValues.expense_date, 'YYYY-MM-DD')
|
|
: undefined,
|
|
kandangs: initialValues?.kandangs.map((kandang) => ({
|
|
id: kandang.kandang_id,
|
|
name: kandang.name,
|
|
})),
|
|
supplier: initialValues?.supplier
|
|
? {
|
|
value: initialValues.supplier.id,
|
|
label: initialValues.supplier.name,
|
|
}
|
|
: undefined,
|
|
existing_documents: initialValues?.documents?.map((doc) => ({
|
|
id: doc.id,
|
|
name: doc.path,
|
|
url: doc.path,
|
|
})),
|
|
deleted_documents: [],
|
|
documents: [],
|
|
cost_per_kandangs: initialValues?.kandangs
|
|
? initialValues.kandangs.map((kandangExpense) => ({
|
|
kandang_id: kandangExpense.kandang_id,
|
|
cost_items: kandangExpense.pengajuans
|
|
? kandangExpense.pengajuans.map((expenseItem) => ({
|
|
nonstock: {
|
|
value: expenseItem.nonstock.id,
|
|
label: expenseItem.nonstock.name,
|
|
},
|
|
quantity: expenseItem.qty,
|
|
total_cost: expenseItem.total_price,
|
|
notes: expenseItem.note,
|
|
}))
|
|
: [],
|
|
}))
|
|
: [],
|
|
};
|
|
};
|