'use client'; import { FormikContextType } from 'formik'; import { Icon } from '@iconify/react'; import Card from '@/components/Card'; import SelectInput, { OptionType, useSelect, } from '@/components/input/SelectInput'; import NumberInput from '@/components/input/NumberInput'; import TextInput from '@/components/input/TextInput'; import Button from '@/components/Button'; import { ExpenseRequestFormValues } from '@/components/pages/expense/form/ExpenseRequestForm.schema'; import { cn } from '@/lib/helper'; import { NonstockApi } from '@/services/api/master-data'; import { Nonstock } from '@/types/api/master-data/nonstock'; import { removeArrayItemAndSync } from '@/lib/utils/formik'; interface ExpenseRequestKandangDetailExpenseProps { type?: 'add' | 'edit' | 'detail'; formik: FormikContextType; supplierId?: number; location?: { value: number; label: string; } | null; className?: { wrapper?: string; }; } const ExpenseRequestKandangDetailExpense: React.FC< ExpenseRequestKandangDetailExpenseProps > = ({ type, formik, supplierId, location, className }) => { const { setInputValue: setNonstockInputValue, options: nonstockOptions, isLoadingOptions: isLoadingNonstockOptions, } = useSelect( NonstockApi.basePath, 'id', 'name', 'search', supplierId ? { supplier_id: String(supplierId) } : undefined ); const nonstockChangeHandler = ( kandangExpenseIdx: number, expenseIdx: number, val: OptionType | OptionType[] | null ) => { formik.setFieldTouched( `expense_nonstocks[${kandangExpenseIdx}].cost_items[${expenseIdx}].nonstock`, true ); formik.setFieldTouched( `expense_nonstocks[${kandangExpenseIdx}].cost_items[${expenseIdx}].nonstock_id`, true ); formik.setFieldValue( `expense_nonstocks[${kandangExpenseIdx}].cost_items[${expenseIdx}].nonstock`, val ); const nonstockId = Array.isArray(val) ? val[0]?.value : val?.value; formik.setFieldValue( `expense_nonstocks[${kandangExpenseIdx}].cost_items[${expenseIdx}].nonstock_id`, nonstockId ?? 0 ); }; const addExpenseItemHandler = (kandangExpenseIdx: number) => { const newExpensesValue = [ ...formik.values.expense_nonstocks[kandangExpenseIdx].cost_items, { nonstock: null, nonstock_id: 0, price: undefined, quantity: undefined, notes: '', }, ]; formik.setFieldValue( `expense_nonstocks[${kandangExpenseIdx}].cost_items`, newExpensesValue ); }; const deleteExpenseItemHandler = ( kandangExpenseIdx: number, expenseIdx: number ) => { const path = `expense_nonstocks[${kandangExpenseIdx}].cost_items`; // trims values, errors, and touched at expenseIdx removeArrayItemAndSync(formik, path, expenseIdx); }; const isExpenseRepeaterInputError = ( column: 'nonstock_id' | 'quantity' | 'price' | 'notes', kandangExpenseIdx: number, expenseIdx: number ) => { return ( formik.touched.expense_nonstocks?.[kandangExpenseIdx]?.cost_items?.[ expenseIdx ]?.[column] && Boolean( formik.errors.expense_nonstocks?.[kandangExpenseIdx] && typeof formik.errors.expense_nonstocks?.[kandangExpenseIdx] === 'object' && formik.errors.expense_nonstocks?.[kandangExpenseIdx].cost_items?.[ expenseIdx ] && typeof formik.errors.expense_nonstocks?.[kandangExpenseIdx] .cost_items?.[expenseIdx] === 'object' && formik.errors.expense_nonstocks?.[kandangExpenseIdx].cost_items?.[ expenseIdx ]?.[column] ) ); }; const getExpenseRepeaterErrorMessage = ( column: 'nonstock_id' | 'quantity' | 'price' | 'notes', kandangExpenseIdx: number, expenseIdx: number ): string => { const kandangError = formik.errors.expense_nonstocks?.[kandangExpenseIdx]; if (!kandangError || typeof kandangError !== 'object') return ''; if (!('cost_items' in kandangError)) return ''; const costItemsError = kandangError.cost_items?.[expenseIdx]; if (!costItemsError || typeof costItemsError !== 'object') return ''; const fieldError = costItemsError[column as keyof typeof costItemsError]; if (!fieldError) return ''; if (typeof fieldError === 'object' && fieldError !== null) { return 'Nonstock wajib diisi!'; } return String(fieldError); }; return (

Rincian Pengajuan Biaya Operasional

{!formik.values.supplier?.value && (

Pilih supplier terlebih dahulu!

)} {formik.values.expense_nonstocks.length === 0 && formik.values.supplier?.value && (

Belum ada item biaya. Silakan pilih lokasi terlebih dahulu.

)} {formik.values.expense_nonstocks.length > 0 && formik.values.supplier?.value && formik.values.expense_nonstocks.map( (kandangExpense, kandangExpenseIdx) => { const kandangName = kandangExpense.kandang_id ? formik.values.kandangs?.find( (kandang) => kandang.id === kandangExpense.kandang_id ) : null; return ( (kandangName?.name || !kandangExpense.kandang_id) && (
Biaya {kandangName?.name || location?.label || 'Umum'}
{type !== 'detail' && } {kandangExpense.cost_items.map( (expenseItem, expenseIdx) => ( {type !== 'detail' && ( )} ) )}
Nonstock Total Kuantitas Harga Satuan CatatanAksi
{ nonstockChangeHandler( kandangExpenseIdx, expenseIdx, val ); }} isError={isExpenseRepeaterInputError( 'nonstock_id', kandangExpenseIdx, expenseIdx )} errorMessage={getExpenseRepeaterErrorMessage( 'nonstock_id', kandangExpenseIdx, expenseIdx )} options={nonstockOptions} isLoading={isLoadingNonstockOptions} onInputChange={setNonstockInputValue} className={{ wrapper: 'min-w-48' }} isClearable={true} /> Rp } className={{ wrapper: 'min-w-24' }} />
{type !== 'detail' && ( )}
) ); } )}
); }; export default ExpenseRequestKandangDetailExpense;