Ingredient in purchase
This commit is contained in:
parent
0b51b29474
commit
140822763e
@ -1,6 +1,6 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import React from 'react'
|
import React, { useMemo } from 'react'
|
||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
Typography,
|
Typography,
|
||||||
@ -11,12 +11,14 @@ import {
|
|||||||
TableContainer,
|
TableContainer,
|
||||||
TableHead,
|
TableHead,
|
||||||
TableRow,
|
TableRow,
|
||||||
Paper
|
Paper,
|
||||||
|
CircularProgress
|
||||||
} from '@mui/material'
|
} from '@mui/material'
|
||||||
import Grid from '@mui/material/Grid2'
|
import Grid from '@mui/material/Grid2'
|
||||||
import CustomAutocomplete from '@/@core/components/mui/Autocomplete'
|
import CustomAutocomplete from '@/@core/components/mui/Autocomplete'
|
||||||
import CustomTextField from '@/@core/components/mui/TextField'
|
import CustomTextField from '@/@core/components/mui/TextField'
|
||||||
import { IngredientItem, PurchaseOrderFormData } from '@/types/apps/purchaseOrderTypes'
|
import { IngredientItem, PurchaseOrderFormData } from '@/types/apps/purchaseOrderTypes'
|
||||||
|
import { useIngredients } from '@/services/queries/ingredients'
|
||||||
|
|
||||||
interface PurchaseIngredientsTableProps {
|
interface PurchaseIngredientsTableProps {
|
||||||
formData: PurchaseOrderFormData
|
formData: PurchaseOrderFormData
|
||||||
@ -31,14 +33,21 @@ const PurchaseIngredientsTable: React.FC<PurchaseIngredientsTableProps> = ({
|
|||||||
addIngredientItem,
|
addIngredientItem,
|
||||||
removeIngredientItem
|
removeIngredientItem
|
||||||
}) => {
|
}) => {
|
||||||
const ingredientOptions = [
|
const { data: ingredients, isLoading } = useIngredients()
|
||||||
{ label: 'Tepung Terigu Premium', value: 'tepung_terigu_premium' },
|
|
||||||
{ label: 'Gula Pasir Halus', value: 'gula_pasir_halus' },
|
// Transform ingredients data to autocomplete options format
|
||||||
{ label: 'Mentega Unsalted', value: 'mentega_unsalted' },
|
const ingredientOptions = useMemo(() => {
|
||||||
{ label: 'Telur Ayam Grade A', value: 'telur_ayam_grade_a' },
|
if (!ingredients || isLoading) {
|
||||||
{ label: 'Vanilla Extract', value: 'vanilla_extract' },
|
return []
|
||||||
{ label: 'Coklat Chips', value: 'coklat_chips' }
|
}
|
||||||
]
|
|
||||||
|
return ingredients?.data.map((ingredient: any) => ({
|
||||||
|
label: ingredient.name || ingredient.nama || ingredient.ingredient_name,
|
||||||
|
value: ingredient.id || ingredient.code || ingredient.value,
|
||||||
|
id: ingredient.id || ingredient.code || ingredient.value,
|
||||||
|
originalData: ingredient
|
||||||
|
}))
|
||||||
|
}, [ingredients, isLoading])
|
||||||
|
|
||||||
const satuanOptions = [
|
const satuanOptions = [
|
||||||
{ label: 'KG', value: 'kg' },
|
{ label: 'KG', value: 'kg' },
|
||||||
@ -63,6 +72,40 @@ const PurchaseIngredientsTable: React.FC<PurchaseIngredientsTableProps> = ({
|
|||||||
{ label: 'Custom', value: 'custom' }
|
{ label: 'Custom', value: 'custom' }
|
||||||
]
|
]
|
||||||
|
|
||||||
|
// Handle ingredient selection with additional data population
|
||||||
|
const handleIngredientSelection = (index: number, selectedIngredient: any) => {
|
||||||
|
handleIngredientChange(index, 'ingredient', selectedIngredient)
|
||||||
|
|
||||||
|
// Auto-populate related fields if available in the ingredient data
|
||||||
|
if (selectedIngredient) {
|
||||||
|
// Get ingredient data from originalData or directly from selectedIngredient
|
||||||
|
const ingredientData = selectedIngredient.originalData || selectedIngredient
|
||||||
|
|
||||||
|
// Auto-fill unit if available
|
||||||
|
if (ingredientData.unit || ingredientData.satuan) {
|
||||||
|
const unit = ingredientData.unit || ingredientData.satuan
|
||||||
|
// Convert unit to string and make it safe
|
||||||
|
const unitString = String(unit).toLowerCase()
|
||||||
|
const unitOption = satuanOptions.find(
|
||||||
|
option => option.value === unit || option.label.toLowerCase() === unitString
|
||||||
|
)
|
||||||
|
if (unitOption) {
|
||||||
|
handleIngredientChange(index, 'satuan', unitOption)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auto-fill price if available
|
||||||
|
if (ingredientData.price || ingredientData.harga) {
|
||||||
|
handleIngredientChange(index, 'harga', ingredientData.price || ingredientData.harga)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auto-fill description if available
|
||||||
|
if (ingredientData.description || ingredientData.deskripsi) {
|
||||||
|
handleIngredientChange(index, 'deskripsi', ingredientData.description || ingredientData.deskripsi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Grid size={{ xs: 12 }} sx={{ mt: 4 }}>
|
<Grid size={{ xs: 12 }} sx={{ mt: 4 }}>
|
||||||
<Typography variant='h6' sx={{ mb: 2, fontWeight: 600 }}>
|
<Typography variant='h6' sx={{ mb: 2, fontWeight: 600 }}>
|
||||||
@ -92,9 +135,36 @@ const PurchaseIngredientsTable: React.FC<PurchaseIngredientsTableProps> = ({
|
|||||||
<CustomAutocomplete
|
<CustomAutocomplete
|
||||||
size='small'
|
size='small'
|
||||||
options={ingredientOptions}
|
options={ingredientOptions}
|
||||||
value={item.ingredient}
|
value={item.ingredient || null}
|
||||||
onChange={(event, newValue) => handleIngredientChange(index, 'ingredient', newValue)}
|
onChange={(event, newValue) => handleIngredientSelection(index, newValue)}
|
||||||
renderInput={params => <CustomTextField {...params} placeholder='Pilih Bahan Baku' />}
|
loading={isLoading}
|
||||||
|
getOptionLabel={(option: any) => {
|
||||||
|
if (!option) return ''
|
||||||
|
return option.label || option.name || option.nama || ''
|
||||||
|
}}
|
||||||
|
isOptionEqualToValue={(option: any, value: any) => {
|
||||||
|
if (!option || !value) return false
|
||||||
|
// Handle different value structures
|
||||||
|
const optionId = option.value || option.id
|
||||||
|
const valueId = value.value || value.id
|
||||||
|
return optionId === valueId
|
||||||
|
}}
|
||||||
|
renderInput={params => (
|
||||||
|
<CustomTextField
|
||||||
|
{...params}
|
||||||
|
placeholder={isLoading ? 'Loading ingredients...' : 'Pilih Bahan Baku'}
|
||||||
|
InputProps={{
|
||||||
|
...params.InputProps,
|
||||||
|
endAdornment: (
|
||||||
|
<>
|
||||||
|
{isLoading ? <CircularProgress color='inherit' size={20} /> : null}
|
||||||
|
{params.InputProps.endAdornment}
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
disabled={isLoading}
|
||||||
/>
|
/>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
@ -215,6 +285,7 @@ const PurchaseIngredientsTable: React.FC<PurchaseIngredientsTableProps> = ({
|
|||||||
variant='outlined'
|
variant='outlined'
|
||||||
size='small'
|
size='small'
|
||||||
sx={{ mt: 1 }}
|
sx={{ mt: 1 }}
|
||||||
|
disabled={isLoading}
|
||||||
>
|
>
|
||||||
Tambah bahan baku
|
Tambah bahan baku
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user