update penjagaan
This commit is contained in:
@@ -145,45 +145,56 @@ export default function DialogBenefit({requestLog, setOpenDialog, openDialog, cl
|
|||||||
|
|
||||||
const methods = useForm<any>({
|
const methods = useForm<any>({
|
||||||
resolver: yupResolver(validationSchema),
|
resolver: yupResolver(validationSchema),
|
||||||
defaultValues
|
defaultValues,
|
||||||
|
reValidateMode: "onChange"
|
||||||
});
|
});
|
||||||
|
|
||||||
let width = claimInput ? 2 : 2.36;
|
let width = claimInput ? 2 : 2.36;
|
||||||
|
|
||||||
const {fields, append, remove} = useFieldArray({name: 'benefit_data',control: methods.control});
|
const {fields, append, remove} = useFieldArray({name: 'benefit_data',control: methods.control,});
|
||||||
const { handleSubmit, reset, watch, setValue, formState: { isDirty, isSubmitting, errors } } = methods
|
const { handleSubmit, reset, watch, setValue, setError, clearErrors, formState: { isDirty, isSubmitting, errors,isValid } } = methods
|
||||||
|
|
||||||
// Buat total data
|
const errorsExist = errors ? Object.keys(errors).length > 0 : false;
|
||||||
let totalAmountIncurred = (requestLog?.benefit_data || []).reduce((accumulator, item) => {
|
// Calculate
|
||||||
return accumulator + (item.amount_incurred || 0);
|
|
||||||
}, 0);
|
|
||||||
let totalAmountApproved = (requestLog?.benefit_data || []).reduce((accumulator, item) => {
|
|
||||||
return accumulator + (item.amount_approved || 0);
|
|
||||||
}, 0);
|
|
||||||
let totalAmountNotApproved = (requestLog?.benefit_data || []).reduce((accumulator, item) => {
|
|
||||||
return accumulator + (item.amount_not_approved || 0);
|
|
||||||
}, 0);
|
|
||||||
let totalExcessPaid = (requestLog?.benefit_data || []).reduce((accumulator, item) => {
|
|
||||||
return accumulator + (item.excess_paid || 0);
|
|
||||||
}, 0);
|
|
||||||
const benefitData = watch('benefit_data');
|
const benefitData = watch('benefit_data');
|
||||||
const [isDisableSave, setDisableSave] = useState(false)
|
const totalAll = () => {
|
||||||
|
let totalAmountIncurred = (requestLog?.benefit_data || []).reduce((accumulator, item) => {
|
||||||
benefitData?.map((item, index) => {
|
return accumulator + (item.amount_incurred || 0);
|
||||||
totalAmountIncurred += parseFloat(item.amount_incurred);
|
}, 0);
|
||||||
totalAmountApproved += parseFloat(item.amount_approved);
|
let totalAmountApproved = (requestLog?.benefit_data || []).reduce((accumulator, item) => {
|
||||||
totalAmountNotApproved += parseFloat(item.amount_not_approved);
|
return accumulator + (item.amount_approved || 0);
|
||||||
totalExcessPaid += parseFloat(item.excess_paid);
|
}, 0);
|
||||||
|
let totalAmountNotApproved = (requestLog?.benefit_data || []).reduce((accumulator, item) => {
|
||||||
|
return accumulator + (item.amount_not_approved || 0);
|
||||||
|
}, 0);
|
||||||
|
let totalExcessPaid = (requestLog?.benefit_data || []).reduce((accumulator, item) => {
|
||||||
|
return accumulator + (item.excess_paid || 0);
|
||||||
|
}, 0);
|
||||||
|
|
||||||
if (totalAmountApproved != 0 && totalAmountIncurred != 0) {
|
benefitData?.map((item, index) => {
|
||||||
if (totalAmountApproved > totalAmountIncurred){
|
totalAmountIncurred += parseFloat(item.amount_incurred);
|
||||||
setValue(`benefit_data.${index}.amount_approved`, 0)
|
totalAmountApproved += parseFloat(item.amount_approved);
|
||||||
alert('Total Amount Approved tidak boleh lebih dari Total Incurred')
|
totalAmountNotApproved += parseFloat(item.amount_not_approved);
|
||||||
}
|
totalExcessPaid += parseFloat(item.excess_paid);
|
||||||
}
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
totalAmountIncurred,
|
||||||
|
totalAmountApproved,
|
||||||
|
totalAmountNotApproved,
|
||||||
|
totalExcessPaid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleOnChangeNominal = (key) => {
|
||||||
|
if (totalAll().totalAmountApproved > totalAll().totalAmountIncurred){
|
||||||
|
// setValue(`benefit_data.${key}.amount_approved`, 0);
|
||||||
|
setError(`benefit_data.${key}.amount_approved`, {message: 'Amount Approve tidak boleh lebih dari Amount Incurred'});
|
||||||
|
} else {
|
||||||
|
clearErrors(`benefit_data.${key}.amount_approved`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
// Submit Form
|
// Submit Form
|
||||||
// =====================================
|
// =====================================
|
||||||
const submitHandler = async (data: BenefitConfigurationListType) => {
|
const submitHandler = async (data: BenefitConfigurationListType) => {
|
||||||
@@ -206,8 +217,6 @@ export default function DialogBenefit({requestLog, setOpenDialog, openDialog, cl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate
|
|
||||||
|
|
||||||
const getContent = () => !addBenefit ? (
|
const getContent = () => !addBenefit ? (
|
||||||
<Stack spacing={2} sx={{marginTop: 2, padding: 2}} direction="column">
|
<Stack spacing={2} sx={{marginTop: 2, padding: 2}} direction="column">
|
||||||
<Stack direction="row" spacing={2}>
|
<Stack direction="row" spacing={2}>
|
||||||
@@ -281,6 +290,10 @@ export default function DialogBenefit({requestLog, setOpenDialog, openDialog, cl
|
|||||||
name={`benefit_data.${index}.amount_incurred`}
|
name={`benefit_data.${index}.amount_incurred`}
|
||||||
placeholder='Amount Incurred'
|
placeholder='Amount Incurred'
|
||||||
required
|
required
|
||||||
|
onChange={(event) => {
|
||||||
|
setValue(`benefit_data.${index}.amount_incurred`, event.target.value)
|
||||||
|
handleOnChangeNominal(index)}
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
@@ -301,6 +314,10 @@ export default function DialogBenefit({requestLog, setOpenDialog, openDialog, cl
|
|||||||
name={`benefit_data.${index}.amount_approved`}
|
name={`benefit_data.${index}.amount_approved`}
|
||||||
placeholder='Amount Approved'
|
placeholder='Amount Approved'
|
||||||
required
|
required
|
||||||
|
onChange={(event) => {
|
||||||
|
setValue(`benefit_data.${index}.amount_approved`, event.target.value)
|
||||||
|
handleOnChangeNominal(index)}
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
@@ -434,7 +451,7 @@ export default function DialogBenefit({requestLog, setOpenDialog, openDialog, cl
|
|||||||
</Grid>
|
</Grid>
|
||||||
<Grid item xs={12}>
|
<Grid item xs={12}>
|
||||||
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
||||||
{fNumber(totalAmountIncurred)}
|
{fNumber(totalAll().totalAmountIncurred)}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
@@ -450,7 +467,7 @@ export default function DialogBenefit({requestLog, setOpenDialog, openDialog, cl
|
|||||||
</Grid>
|
</Grid>
|
||||||
<Grid item xs={12}>
|
<Grid item xs={12}>
|
||||||
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
||||||
{fNumber(totalAmountApproved)}
|
{fNumber(totalAll().totalAmountApproved)}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
@@ -466,7 +483,7 @@ export default function DialogBenefit({requestLog, setOpenDialog, openDialog, cl
|
|||||||
</Grid>
|
</Grid>
|
||||||
<Grid item xs={12}>
|
<Grid item xs={12}>
|
||||||
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
||||||
{fNumber(totalAmountNotApproved)}
|
{fNumber(totalAll().totalAmountNotApproved)}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
@@ -482,7 +499,7 @@ export default function DialogBenefit({requestLog, setOpenDialog, openDialog, cl
|
|||||||
</Grid>
|
</Grid>
|
||||||
<Grid item xs={12}>
|
<Grid item xs={12}>
|
||||||
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
||||||
{fNumber(totalExcessPaid)}
|
{fNumber(totalAll().totalExcessPaid)}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
@@ -499,7 +516,7 @@ export default function DialogBenefit({requestLog, setOpenDialog, openDialog, cl
|
|||||||
<DialogActions>
|
<DialogActions>
|
||||||
<Stack direction="row" sx={{marginTop:3}} alignItems="center" justifyContent="space-between" spacing={2}>
|
<Stack direction="row" sx={{marginTop:3}} alignItems="center" justifyContent="space-between" spacing={2}>
|
||||||
<Button variant="outlined" onClick={handleCloseDialogBenefit}><Typography>Cancel</Typography></Button>
|
<Button variant="outlined" onClick={handleCloseDialogBenefit}><Typography>Cancel</Typography></Button>
|
||||||
<LoadingButton disabled={isDisableSave} type="submit" variant="contained" loading={isSubmitting}>
|
<LoadingButton disabled={errorsExist} type="submit" variant="contained" loading={isSubmitting}>
|
||||||
Save
|
Save
|
||||||
</LoadingButton>
|
</LoadingButton>
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|||||||
@@ -62,23 +62,36 @@ export default function DialogEditBenefit({id, data, setOpenDialog, openDialog,
|
|||||||
defaultValues
|
defaultValues
|
||||||
});
|
});
|
||||||
|
|
||||||
const { handleSubmit, reset, watch, setValue, formState: { isDirty, isSubmitting, errors } } = methods;
|
const { handleSubmit, reset, watch, setValue, setError, clearErrors, formState: { isDirty, isSubmitting, errors } } = methods;
|
||||||
|
const errorsExist = errors ? Object.keys(errors).length > 0 : false;
|
||||||
|
const totalAll = () => {
|
||||||
|
// Ambil nilai dari form menggunakan watch
|
||||||
|
const amountIncurred = parseFloat(watch('amount_incurred'));
|
||||||
|
const amountApproved = parseFloat(watch('amount_approved'));
|
||||||
|
const amountNotApproved = parseFloat(watch('amount_not_approved'));
|
||||||
|
const excessPaid = parseFloat(watch('excess_paid'));
|
||||||
|
|
||||||
|
// Hitung total baru
|
||||||
|
const totalAmountIncurred = total.totalAmountIncurred - data?.amount_incurred + amountIncurred;
|
||||||
|
const totalAmountApproved = total.totalAmountApproved - data?.amount_approved + amountApproved;
|
||||||
|
const totalAmountNotApproved = total.totalAmountNotApproved - data?.amount_not_approved + amountNotApproved;
|
||||||
|
const totalExcessPaid = total.totalExcessPaid - data?.excess_paid + excessPaid;
|
||||||
|
|
||||||
// Ambil nilai dari form menggunakan watch
|
return {
|
||||||
const amountIncurred = parseFloat(watch('amount_incurred'));
|
totalAmountIncurred,
|
||||||
const amountApproved = parseFloat(watch('amount_approved'));
|
totalAmountApproved,
|
||||||
const amountNotApproved = parseFloat(watch('amount_not_approved'));
|
totalAmountNotApproved,
|
||||||
const excessPaid = parseFloat(watch('excess_paid'));
|
totalExcessPaid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Hitung total baru
|
const handleOnChangeNominal = (key) => {
|
||||||
const totalAmountIncurred = total.totalAmountIncurred - data?.amount_incurred + amountIncurred;
|
if (totalAll().totalAmountApproved > totalAll().totalAmountIncurred){
|
||||||
const totalAmountApproved = total.totalAmountApproved - data?.amount_approved + amountApproved;
|
// setValue(`benefit_data.${key}.amount_approved`, 0);
|
||||||
const totalAmountNotApproved = total.totalAmountNotApproved - data?.amount_not_approved + amountNotApproved;
|
setError(`amount_approved`, {message: 'Amount Approve tidak boleh lebih dari Amount Incurred'});
|
||||||
const totalExcessPaid = total.totalExcessPaid - data?.excess_paid + excessPaid;
|
} else {
|
||||||
|
clearErrors(`amount_approved`);
|
||||||
if (totalAmountApproved > totalAmountIncurred) {
|
}
|
||||||
alert('Total Approve tidak boleh melebihi Total Incurred')
|
|
||||||
setValue('amount_approved', data?.amount_approved)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (totalAmountIncurred !== (totalAmountApproved+totalAmountNotApproved)){
|
// if (totalAmountIncurred !== (totalAmountApproved+totalAmountNotApproved)){
|
||||||
@@ -142,6 +155,10 @@ export default function DialogEditBenefit({id, data, setOpenDialog, openDialog,
|
|||||||
name={`amount_incurred`}
|
name={`amount_incurred`}
|
||||||
placeholder='Amount Incurred'
|
placeholder='Amount Incurred'
|
||||||
required
|
required
|
||||||
|
onChange={(event) => {
|
||||||
|
setValue(`amount_incurred`, event.target.value)
|
||||||
|
handleOnChangeNominal(id)}
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
@@ -162,6 +179,10 @@ export default function DialogEditBenefit({id, data, setOpenDialog, openDialog,
|
|||||||
name={`amount_approved`}
|
name={`amount_approved`}
|
||||||
placeholder='Amount Approved'
|
placeholder='Amount Approved'
|
||||||
required
|
required
|
||||||
|
onChange={(event) => {
|
||||||
|
setValue(`amount_approved`, event.target.value)
|
||||||
|
handleOnChangeNominal(id)}
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
@@ -275,7 +296,7 @@ export default function DialogEditBenefit({id, data, setOpenDialog, openDialog,
|
|||||||
</Grid>
|
</Grid>
|
||||||
<Grid item xs={12}>
|
<Grid item xs={12}>
|
||||||
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
||||||
{totalAmountIncurred ? fNumber(totalAmountIncurred) : 0}
|
{totalAll().totalAmountIncurred ? fNumber(totalAll().totalAmountIncurred) : 0}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
@@ -291,7 +312,7 @@ export default function DialogEditBenefit({id, data, setOpenDialog, openDialog,
|
|||||||
</Grid>
|
</Grid>
|
||||||
<Grid item xs={12}>
|
<Grid item xs={12}>
|
||||||
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
||||||
{fNumber(totalAmountApproved)}
|
{totalAll().totalAmountApproved ? fNumber(totalAll().totalAmountApproved) : 0}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
@@ -307,7 +328,7 @@ export default function DialogEditBenefit({id, data, setOpenDialog, openDialog,
|
|||||||
</Grid>
|
</Grid>
|
||||||
<Grid item xs={12}>
|
<Grid item xs={12}>
|
||||||
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
||||||
{fNumber(totalAmountNotApproved)}
|
{totalAll().totalAmountNotApproved ? fNumber(totalAll().totalAmountNotApproved) : 0}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
@@ -323,7 +344,7 @@ export default function DialogEditBenefit({id, data, setOpenDialog, openDialog,
|
|||||||
</Grid>
|
</Grid>
|
||||||
<Grid item xs={12}>
|
<Grid item xs={12}>
|
||||||
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
<Typography variant="caption" sx={{ fontWeight: 'bold', textAlign: 'right' }}>
|
||||||
{fNumber(totalExcessPaid)}
|
{totalAll().totalExcessPaid ? fNumber(totalAll().totalExcessPaid) : 0}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
@@ -337,7 +358,7 @@ export default function DialogEditBenefit({id, data, setOpenDialog, openDialog,
|
|||||||
<DialogActions>
|
<DialogActions>
|
||||||
<Stack direction="row" sx={{marginTop:3}} alignItems="center" justifyContent="space-between" spacing={2}>
|
<Stack direction="row" sx={{marginTop:3}} alignItems="center" justifyContent="space-between" spacing={2}>
|
||||||
<Button variant="outlined" onClick={handleCloseDialog}><Typography>Cancel</Typography></Button>
|
<Button variant="outlined" onClick={handleCloseDialog}><Typography>Cancel</Typography></Button>
|
||||||
<LoadingButton type="submit" variant="contained" loading={isSubmitting}>
|
<LoadingButton disabled={errorsExist} type="submit" variant="contained" loading={isSubmitting}>
|
||||||
Save
|
Save
|
||||||
</LoadingButton>
|
</LoadingButton>
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|||||||
Reference in New Issue
Block a user