31 lines
1.0 KiB
TypeScript
31 lines
1.0 KiB
TypeScript
import type { RawMaterialTableGroup, RawMaterialTableRow } from '@/types/raw-material';
|
|
|
|
export function groupRawMaterialRows(rows: RawMaterialTableRow[]): RawMaterialTableGroup[] {
|
|
const groups: RawMaterialTableGroup[] = [];
|
|
const indexByMaterialId = new Map<number, number>();
|
|
|
|
for (const row of rows) {
|
|
const existingIndex = indexByMaterialId.get(row.raw_material_id);
|
|
|
|
if (existingIndex === undefined) {
|
|
indexByMaterialId.set(row.raw_material_id, groups.length);
|
|
groups.push({
|
|
raw_material_id: row.raw_material_id,
|
|
raw_material_row_number: row.raw_material_row_number,
|
|
raw_material_name: row.raw_material_name,
|
|
unit: row.unit,
|
|
unit_label: row.unit_label,
|
|
unit_abbreviation: row.unit_abbreviation,
|
|
is_active: row.is_active,
|
|
variants: [row],
|
|
});
|
|
|
|
continue;
|
|
}
|
|
|
|
groups[existingIndex].variants.push(row);
|
|
}
|
|
|
|
return groups;
|
|
}
|