diff --git a/src/components/pages/purchase/form/order/PurchaseOrderForm.schema.ts b/src/components/pages/purchase/form/order/PurchaseOrderForm.schema.ts index 42db9d28..d3e6ff17 100644 --- a/src/components/pages/purchase/form/order/PurchaseOrderForm.schema.ts +++ b/src/components/pages/purchase/form/order/PurchaseOrderForm.schema.ts @@ -4,11 +4,17 @@ import { Purchase } from '@/types/api/purchase/purchase'; type PurchaseRequestStaffApprovalFormSchemaType = { notes: string | null; items: { - purchase_item?: { + product?: { value: number; label: string; } | null; - purchase_item_id: number; + product_id: number; + warehouse?: { + value: number; + label: string; + } | null; + warehouse_id: number; + qty: number; price: number | string; total_price: number | string; }[]; @@ -47,11 +53,17 @@ type PurchaseRequestAcceptApprovalFormSchemaType = { }; export type PurchaseStaffApprovalItemSchema = { - purchase_item?: { + product?: { value: number; label: string; } | null; - purchase_item_id: number; + product_id: number; + warehouse?: { + value: number; + label: string; + } | null; + warehouse_id: number; + qty: number; price: number | string; total_price: number | string; }; @@ -87,49 +99,35 @@ export type PurchaseDeleteItemsSchema = { const PurchaseStaffApprovalItemObjectSchema: Yup.ObjectSchema = Yup.object({ - purchase_item: Yup.object({ + product: Yup.object({ value: Yup.number().min(1).required(), label: Yup.string().required(), - }) - .nullable() - .optional(), - purchase_item_id: Yup.number() - .min(1, 'Purchase item is required!') - .required('Purchase item is required!') - .test( - 'is-valid-purchase-item-id', - 'Purchase item ID must be valid!', - function (value) { - return Boolean(value && value > 0); - } - ) - .typeError('Purchase item must be selected!'), - price: Yup.mixed() + }).nullable(), + product_id: Yup.number() + .required('Produk wajib diisi!') + .min(1, 'Produk wajib diisi!') + .typeError('Produk wajib diisi!'), + warehouse: Yup.object({ + value: Yup.number().min(1).required(), + label: Yup.string().required(), + }).nullable(), + warehouse_id: Yup.number() + .required('Gudang wajib diisi!') + .min(1, 'Gudang wajib diisi!') + .typeError('Gudang wajib diisi!'), + qty: Yup.number() + .required('Jumlah wajib diisi!') + .min(1, 'Jumlah harus berupa angka lebih dari 0!') + .typeError('Jumlah harus berupa angka lebih dari 0!'), + price: Yup.number() .required('Harga wajib diisi!') - .test( - 'is-valid-price', - 'Harga harus berupa angka lebih dari atau sama dengan 0!', - function (value) { - if (value === '' || value === null || value === undefined) - return false; - const numValue = - typeof value === 'string' ? parseFloat(value) : value; - return !isNaN(numValue) && numValue >= 0; - } - ) + .min(0, 'Harga harus berupa angka lebih dari atau sama dengan 0!') .typeError('Harga harus berupa angka lebih dari atau sama dengan 0!'), - total_price: Yup.mixed() + total_price: Yup.number() .required('Total harga wajib diisi!') - .test( - 'is-valid-total-price', - 'Total harga harus berupa angka lebih dari atau sama dengan 0!', - function (value) { - if (value === '' || value === null || value === undefined) - return false; - const numValue = - typeof value === 'string' ? parseFloat(value) : value; - return !isNaN(numValue) && numValue >= 0; - } + .min(0, 'Total harga harus berupa angka lebih dari atau sama dengan 0!') + .typeError( + 'Total harga harus berupa angka lebih dari atau sama dengan 0!' ), }); @@ -273,7 +271,11 @@ export const PurchaseRequestStaffApprovalFormInitialValues: PurchaseRequestStaff notes: '', items: [ { - purchase_item_id: 0, + product: null, + product_id: 0, + warehouse: null, + warehouse_id: 0, + qty: 0, price: '', total_price: '', }, @@ -287,13 +289,21 @@ export const PurchaseRequestStaffApprovalFormDefaultValues = ( notes: purchase?.notes ?? null, items: purchase?.items ? purchase.items.map((item) => ({ - purchase_item_id: item.id, - price: '', - total_price: '', + product: null, + product_id: item.product_id, + warehouse: null, + warehouse_id: item.warehouse.id, + qty: item.qty, + price: item.price, + total_price: item.total_price, })) : [ { - purchase_item_id: 0, + product: null, + product_id: 0, + warehouse: null, + warehouse_id: 0, + qty: 0, price: '', total_price: '', }, diff --git a/src/types/api/purchase/purchase.d.ts b/src/types/api/purchase/purchase.d.ts index f20c4549..b4b8b2ee 100644 --- a/src/types/api/purchase/purchase.d.ts +++ b/src/types/api/purchase/purchase.d.ts @@ -76,12 +76,24 @@ export type CreatePurchaseRequestPayload = { export type CreateStaffApprovalRequestPayload = { notes?: string | null; items: { - purchase_item_id: number; + product_id: number; + warehouse_id: number; + qty: number; price: number; total_price: number; }[]; }; +export type UpdateStaffApprovalRequestPayload = { + items: { + purchase_item_id: number; + qty: number; + price: number; + total_price: number; + }[]; + 'notes?': string | null; +}; + export type CreateManagerApprovalRequestPayload = { notes?: string | null; };