mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-06-11 11:01:43 +00:00
182 lines
5.3 KiB
TypeScript
182 lines
5.3 KiB
TypeScript
import * as Yup from 'yup';
|
|
import { Purchase } from '@/types/api/purchase/purchase';
|
|
|
|
type PurchaseRequestFormSchemaType = {
|
|
supplier?: {
|
|
value: number;
|
|
label: string;
|
|
} | null;
|
|
supplier_id: number;
|
|
area?: {
|
|
value: number;
|
|
label: string;
|
|
} | null;
|
|
area_id: number;
|
|
location?: {
|
|
value: number;
|
|
label: string;
|
|
} | null;
|
|
location_id: number;
|
|
credit_term: number | string | undefined;
|
|
notes: string | null;
|
|
purchase_items: {
|
|
warehouse?: {
|
|
value: number;
|
|
label: string;
|
|
} | null;
|
|
warehouse_id: number;
|
|
product?: {
|
|
value: number;
|
|
label: string;
|
|
} | null;
|
|
product_id: number;
|
|
product_warehouse?: {
|
|
value: number;
|
|
label: string;
|
|
} | null;
|
|
product_warehouse_id: number;
|
|
sub_qty: number | string;
|
|
}[];
|
|
};
|
|
|
|
export type PurchaseItemSchema = {
|
|
warehouse?: {
|
|
value: number;
|
|
label: string;
|
|
} | null;
|
|
warehouse_id: number;
|
|
product?: {
|
|
value: number;
|
|
label: string;
|
|
} | null;
|
|
product_id: number;
|
|
product_warehouse?: {
|
|
value: number;
|
|
label: string;
|
|
} | null;
|
|
product_warehouse_id: number;
|
|
sub_qty: number | string;
|
|
};
|
|
|
|
const PurchaseItemObjectSchema: Yup.ObjectSchema<PurchaseItemSchema> =
|
|
Yup.object({
|
|
warehouse: Yup.object({
|
|
value: Yup.number().min(1).required(),
|
|
label: Yup.string().required(),
|
|
}).nullable(),
|
|
warehouse_id: Yup.number()
|
|
.required('Gudang wajib dipilih!')
|
|
.min(1, 'Gudang wajib dipilih!')
|
|
.typeError('Gudang wajib dipilih'),
|
|
product: Yup.object({
|
|
value: Yup.number().min(1).required(),
|
|
label: Yup.string().required(),
|
|
}).nullable(),
|
|
product_id: Yup.number()
|
|
.required('Produk wajib dipilih!')
|
|
.min(1, 'Produk wajib dipilih!')
|
|
.typeError('Produk wajib dipilih!'),
|
|
product_warehouse: Yup.object({
|
|
value: Yup.number().min(1).required(),
|
|
label: Yup.string().required(),
|
|
}).nullable(),
|
|
product_warehouse_id: Yup.number()
|
|
.required('Produk wajib dipilih!')
|
|
.min(1, 'Produk wajib dipilih!')
|
|
.typeError('Produk wajib dipilih!'),
|
|
sub_qty: Yup.mixed<string | number>()
|
|
.required('Kuantitas wajib diisi!')
|
|
.test(
|
|
'is-valid-sub-qty',
|
|
'Kuantitas harus berupa angka lebih dari 0!',
|
|
function (value) {
|
|
if (value === '' || value === null || value === undefined)
|
|
return false;
|
|
const numValue =
|
|
typeof value === 'string' ? parseFloat(value) : value;
|
|
return !isNaN(numValue) && numValue > 0;
|
|
}
|
|
),
|
|
});
|
|
|
|
export const PurchaseRequestFormSchema: Yup.ObjectSchema<PurchaseRequestFormSchemaType> =
|
|
Yup.object({
|
|
supplier: Yup.object({
|
|
value: Yup.number().min(1).required(),
|
|
label: Yup.string().required(),
|
|
}).nullable(),
|
|
supplier_id: Yup.number()
|
|
.required('Supplier wajib dipilih!')
|
|
.test('is-valid-supplier', 'Supplier wajib dipilih!', function (value) {
|
|
if (!this.parent.supplier) return true;
|
|
return Boolean(value && value > 0);
|
|
})
|
|
.typeError('Supplier wajib dipilih!'),
|
|
area: Yup.object({
|
|
value: Yup.number().min(1).required(),
|
|
label: Yup.string().required(),
|
|
}).nullable(),
|
|
area_id: Yup.number()
|
|
.required('Area wajib dipilih!')
|
|
.test('is-valid-area', 'Area wajib dipilih!', function (value) {
|
|
if (!this.parent.area) return true;
|
|
return Boolean(value && value > 0);
|
|
})
|
|
.typeError('Area wajib dipilih!'),
|
|
location: Yup.object({
|
|
value: Yup.number().min(1).required(),
|
|
label: Yup.string().required(),
|
|
}).nullable(),
|
|
location_id: Yup.number()
|
|
.required('Lokasi wajib dipilih!')
|
|
.test('is-valid-location', 'Lokasi wajib dipilih!', function (value) {
|
|
if (!this.parent.location) return true;
|
|
return Boolean(value && value > 0);
|
|
})
|
|
.typeError('Lokasi wajib dipilih!'),
|
|
credit_term: Yup.mixed<string | number>().when('supplier_id', {
|
|
is: (supplier_id: number) => supplier_id && supplier_id > 0,
|
|
then: () =>
|
|
Yup.number()
|
|
.required('Jumlah hari jatuh tempo wajib diisi!')
|
|
.min(1, 'Jumlah hari jatuh tempo minimal 1 hari!')
|
|
.typeError('Jumlah hari jatuh tempo harus berupa angka!'),
|
|
otherwise: () =>
|
|
Yup.mixed<string | number>()
|
|
.nullable()
|
|
.default(null)
|
|
.transform(() => null),
|
|
}),
|
|
notes: Yup.string().nullable().default(null),
|
|
purchase_items: Yup.array()
|
|
.of(PurchaseItemObjectSchema)
|
|
.min(1, 'Minimal harus ada 1 item pembelian!')
|
|
.required('Item pembelian wajib diisi!')
|
|
.typeError('Item pembelian wajib diisi!'),
|
|
});
|
|
|
|
export const UpdatePurchaseRequestFormSchema = PurchaseRequestFormSchema;
|
|
|
|
export type PurchaseRequestFormValues = Yup.InferType<
|
|
typeof PurchaseRequestFormSchema
|
|
>;
|
|
|
|
export const getPurchaseRequestFormInitialValues = (
|
|
initialValues?: Purchase
|
|
): PurchaseRequestFormValues => ({
|
|
supplier: initialValues?.supplier
|
|
? {
|
|
value: initialValues.supplier.id,
|
|
label: initialValues.supplier.name,
|
|
}
|
|
: null,
|
|
supplier_id: initialValues?.supplier?.id ?? 0,
|
|
area: null,
|
|
area_id: 0,
|
|
location: null,
|
|
location_id: 0,
|
|
credit_term: initialValues?.credit_term ?? '',
|
|
notes: initialValues?.notes ?? null,
|
|
purchase_items: [],
|
|
});
|