refactor(FE-212): enhance quantity and price handling in PurchaseOrderStaffApprovalForm with improved validation and formatting

This commit is contained in:
rstubryan
2025-11-22 13:47:24 +07:00
parent 62c3d2af53
commit 274322606d
@@ -23,6 +23,7 @@ import {
PurchaseStaffApprovalItemSchema, PurchaseStaffApprovalItemSchema,
} from './PurchaseOrderForm.schema'; } from './PurchaseOrderForm.schema';
import { isResponseError } from '@/lib/api-helper'; import { isResponseError } from '@/lib/api-helper';
import { formatNumber } from '@/lib/helper';
import { PurchaseApi } from '@/services/api/purchase'; import { PurchaseApi } from '@/services/api/purchase';
import { import {
CreateStaffApprovalRequestPayload, CreateStaffApprovalRequestPayload,
@@ -590,7 +591,11 @@ const PurchaseOrderStaffApprovalForm = ({
if (field === 'qty') { if (field === 'qty') {
const numValue = const numValue =
typeof value === 'string' ? parseFloat(value) || 0 : value; typeof value === 'string'
? value === ''
? ''
: parseFloat(value) || 0
: value;
formik.setFieldValue(`items.${idx}.qty`, numValue); formik.setFieldValue(`items.${idx}.qty`, numValue);
@@ -600,16 +605,30 @@ const PurchaseOrderStaffApprovalForm = ({
if (field === 'price' || field === 'total_price') { if (field === 'price' || field === 'total_price') {
const numValue = const numValue =
typeof value === 'string' ? parseFloat(value) || 0 : value; typeof value === 'string'
? value === ''
? ''
: parseFloat(value) || 0
: value;
formik.setFieldValue(`items.${idx}.${field}`, numValue); formik.setFieldValue(`items.${idx}.${field}`, numValue);
if (field === 'price' && formItem.qty > 0 && numValue >= 0) { if (
field === 'price' &&
formItem.qty > 0 &&
numValue !== '' &&
numValue >= 0
) {
const calculatedTotal = numValue * formItem.qty; const calculatedTotal = numValue * formItem.qty;
formik.setFieldValue(`items.${idx}.total_price`, calculatedTotal); formik.setFieldValue(`items.${idx}.total_price`, calculatedTotal);
} }
if (field === 'total_price' && formItem.qty > 0 && numValue >= 0) { if (
field === 'total_price' &&
formItem.qty > 0 &&
numValue !== '' &&
numValue >= 0
) {
const calculatedPrice = numValue / formItem.qty; const calculatedPrice = numValue / formItem.qty;
formik.setFieldValue(`items.${idx}.price`, calculatedPrice); formik.setFieldValue(`items.${idx}.price`, calculatedPrice);
} }
@@ -692,6 +711,7 @@ const PurchaseOrderStaffApprovalForm = ({
handleProductChange(formItemIndex, val) handleProductChange(formItemIndex, val)
} }
options={supplierProductOptions} options={supplierProductOptions}
placeholder='Pilih produk'
isClearable={false} isClearable={false}
isSearchable={false} isSearchable={false}
isDisabled={type === 'add'} isDisabled={type === 'add'}
@@ -699,6 +719,9 @@ const PurchaseOrderStaffApprovalForm = ({
wrapper: wrapper:
'min-w-52 md:min-w-72 lg:min-w-80', 'min-w-52 md:min-w-72 lg:min-w-80',
}} }}
bottomLabel={
'Previous: ' + purchaseItem.product.name
}
/> />
</td> </td>
<td> <td>
@@ -726,9 +749,8 @@ const PurchaseOrderStaffApprovalForm = ({
<NumberInput <NumberInput
name={`items.${formItemIndex}.qty`} name={`items.${formItemIndex}.qty`}
value={ value={
formItem?.qty || formik.values.items?.[formItemIndex]
purchaseItem?.quantity || ?.qty ?? ''
''
} }
onChange={(e) => onChange={(e) =>
handlePurchaseItemChange( handlePurchaseItemChange(
@@ -741,8 +763,9 @@ const PurchaseOrderStaffApprovalForm = ({
placeholder='Masukkan jumlah' placeholder='Masukkan jumlah'
allowNegative={false} allowNegative={false}
decimalScale={0} decimalScale={0}
bottomLabel={`Previous: ${formatNumber(purchaseItem.quantity)}`}
className={{ className={{
wrapper: 'min-w-24', wrapper: 'min-w-32',
}} }}
/> />
) : ( ) : (
@@ -797,6 +820,7 @@ const PurchaseOrderStaffApprovalForm = ({
thousandSeparator=',' thousandSeparator=','
decimalSeparator='.' decimalSeparator='.'
inputPrefix={'Rp'} inputPrefix={'Rp'}
bottomLabel={`Previous: Rp${formatNumber(initialValues?.items?.find((item) => item.id === purchaseItem.id)?.price || 0, 'id-ID', 2, 2)}`}
isError={ isError={
isRepeaterInputError( isRepeaterInputError(
formItemIndex, formItemIndex,
@@ -834,6 +858,7 @@ const PurchaseOrderStaffApprovalForm = ({
thousandSeparator=',' thousandSeparator=','
decimalSeparator='.' decimalSeparator='.'
inputPrefix={'Rp'} inputPrefix={'Rp'}
bottomLabel={`Previous: Rp${formatNumber(initialValues?.items?.find((item) => item.id === purchaseItem.id)?.total_price || 0, 'id-ID', 2, 2)}`}
isError={ isError={
isRepeaterInputError( isRepeaterInputError(
formItemIndex, formItemIndex,