mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-24 23:35:45 +00:00
feat(FE-137): enhance stock product selection in RecordingForm with initial values support
This commit is contained in:
@@ -552,35 +552,67 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
|||||||
|
|
||||||
// Memoized unified products for stock selection
|
// Memoized unified products for stock selection
|
||||||
const unifiedStockProducts = useMemo(() => {
|
const unifiedStockProducts = useMemo(() => {
|
||||||
if (!isResponseSuccess(stockProducts)) return [];
|
|
||||||
const options: OptionType[] = [];
|
const options: OptionType[] = [];
|
||||||
|
|
||||||
stockProducts.data.forEach((product) => {
|
// Add products from API stockProducts
|
||||||
const warehouse = product.warehouse;
|
if (isResponseSuccess(stockProducts)) {
|
||||||
const stockText = product.quantity.toLocaleString('id-ID');
|
stockProducts.data.forEach((product) => {
|
||||||
|
const warehouse = product.warehouse;
|
||||||
|
const stockText = product.quantity.toLocaleString('id-ID');
|
||||||
|
|
||||||
// Check if product has any of the flags
|
// Check if product has any of the flags
|
||||||
const hasPakanFlag = product.product.flags?.includes('PAKAN');
|
const hasPakanFlag = product.product.flags?.includes('PAKAN');
|
||||||
const hasOvkFlag = product.product.flags?.includes('OVK');
|
const hasOvkFlag = product.product.flags?.includes('OVK');
|
||||||
|
|
||||||
// Add products with warehouse and location grouping in label (similar to projectFlockKandangOptions pattern)
|
// Add products with warehouse and location grouping in label (similar to projectFlockKandangOptions pattern)
|
||||||
if (hasPakanFlag) {
|
if (hasPakanFlag) {
|
||||||
options.push({
|
options.push({
|
||||||
value: product.id,
|
value: product.id,
|
||||||
label: `[PAKAN] ${product.product.name} - ${warehouse?.name || ''} (${stockText})`
|
label: `[PAKAN] ${product.product.name} - ${warehouse?.name || ''} (${stockText})`
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasOvkFlag) {
|
if (hasOvkFlag) {
|
||||||
options.push({
|
options.push({
|
||||||
value: product.id,
|
value: product.id,
|
||||||
label: `[OVK] ${product.product.name} - ${warehouse?.name || ''} (${stockText})`
|
label: `[OVK] ${product.product.name} - ${warehouse?.name || ''} (${stockText})`
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add existing stock products from initialValues (for detail/edit mode)
|
||||||
|
if (initialValues && 'stocks' in initialValues && initialValues.stocks && type !== 'add') {
|
||||||
|
const initialValuesWithStocks = initialValues as Recording & {
|
||||||
|
stocks?: Array<{
|
||||||
|
product_warehouse_id: number;
|
||||||
|
usage_amount: number;
|
||||||
|
notes: string;
|
||||||
|
product_warehouse?: {
|
||||||
|
id: number;
|
||||||
|
product_id: number;
|
||||||
|
product_name: string;
|
||||||
|
warehouse_id: number;
|
||||||
|
warehouse_name: string;
|
||||||
|
};
|
||||||
|
}>;
|
||||||
|
};
|
||||||
|
|
||||||
|
initialValuesWithStocks.stocks?.forEach((stock) => {
|
||||||
|
if (stock.product_warehouse && stock.product_warehouse.product_name) {
|
||||||
|
const existingOption = options.find(opt => opt.value === stock.product_warehouse_id);
|
||||||
|
if (!existingOption) {
|
||||||
|
options.push({
|
||||||
|
value: stock.product_warehouse_id,
|
||||||
|
label: `${stock.product_warehouse.product_name} - ${stock.product_warehouse.warehouse_name}`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
}, [stockProducts, getProjectFlockLocation()]);
|
}, [stockProducts, getProjectFlockLocation(), initialValues, type]);
|
||||||
|
|
||||||
|
|
||||||
// Handle stock usage amount change - simplified following MovementForm pattern
|
// Handle stock usage amount change - simplified following MovementForm pattern
|
||||||
|
|||||||
Reference in New Issue
Block a user