Skip to content

Commit 349ab35

Browse files
committed
Optimera summering av belopprader
1 parent a5c8a66 commit 349ab35

File tree

2 files changed

+52
-22
lines changed

2 files changed

+52
-22
lines changed

src/components/edit/blocks/belopprad/EditBeloppradMonetary.vue

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
type BeloppradMonetary,
99
calculateValuesIntoBelopprad,
1010
} from "@/model/arsredovisning/beloppradtyper/BeloppradMonetary.ts";
11-
import { computed, watch } from "vue";
11+
import { computed, onMounted, watch } from "vue";
1212
import {
1313
type Belopprad,
1414
getTaxonomyItemForBelopprad,
@@ -41,14 +41,26 @@ const taxonomyItem = computed(() => {
4141
return getTaxonomyItemForBelopprad(props.taxonomyManager, belopprad.value);
4242
});
4343
44-
watch(belopprader.value, () => {
45-
// Räkna automatiskt ut summor
44+
onMounted(() => {
4645
if (taxonomyItem.value.additionalData.isCalculatedItem) {
47-
calculateValuesIntoBelopprad(
48-
props.taxonomyManager.calculationProcessor,
49-
belopprader.value,
50-
belopprad.value,
46+
// Räkna automatiskt ut summor
47+
48+
const sumPartBelopprader = belopprader.value.filter((otherBelopprad) =>
49+
props.taxonomyManager.calculationProcessor.isConceptIncludedInSum(
50+
otherBelopprad.taxonomyItemName,
51+
belopprad.value.taxonomyItemName,
52+
),
5153
);
54+
55+
for (const sumPartBelopprad of sumPartBelopprader) {
56+
watch(sumPartBelopprad, () => {
57+
calculateValuesIntoBelopprad(
58+
props.taxonomyManager.calculationProcessor,
59+
sumPartBelopprader,
60+
belopprad.value,
61+
);
62+
});
63+
}
5264
}
5365
});
5466
</script>

src/util/CalculationProcessor.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface CalculationConceptValue {
1717

1818
export class CalculationProcessor {
1919
private readonly calculationTree: CalculationNode[];
20+
private readonly nodes: Map<string, CalculationNode>; // <conceptName, node>
2021

2122
constructor(
2223
rootName: TaxonomyRootName,
@@ -25,6 +26,9 @@ export class CalculationProcessor {
2526
},
2627
) {
2728
this.calculationTree = CalculationParser.parse(rootName, calculationJson);
29+
30+
this.nodes = new Map<string, CalculationNode>();
31+
this.buildNodeMap(this.calculationTree, this.nodes);
2832
}
2933

3034
/**
@@ -39,7 +43,7 @@ export class CalculationProcessor {
3943
values: CalculationConceptValue[],
4044
): number {
4145
const valueMap = new Map(values.map((v) => [v.conceptName, v.value]));
42-
const node = this.findNodeByConceptName(conceptName, this.calculationTree);
46+
const node = this.nodes.get(conceptName);
4347
if (!node) {
4448
throw new Error(`Concept ${conceptName} not found in calculation tree`);
4549
}
@@ -53,26 +57,28 @@ export class CalculationProcessor {
5357
* @returns Sant om konceptet är ett lövnod, annars falskt.
5458
*/
5559
public isLeafConcept(conceptName: string): boolean {
56-
const node = this.findNodeByConceptName(conceptName, this.calculationTree);
60+
const node = this.nodes.get(conceptName);
5761
return !node || !node.children || node.children.length === 0;
5862
}
5963

60-
private findNodeByConceptName(
64+
public isConceptIncludedInSum(
6165
conceptName: string,
62-
nodes: CalculationNode[],
63-
): CalculationNode | null {
64-
for (const node of nodes) {
65-
if (node.concept.name === conceptName) {
66-
return node;
67-
}
68-
if (node.children) {
69-
const found = this.findNodeByConceptName(conceptName, node.children);
70-
if (found) {
71-
return found;
72-
}
66+
sumConceptName: string,
67+
): boolean {
68+
let result = false;
69+
70+
const sumNode = this.nodes.get(sumConceptName);
71+
for (const sumNodeChild of sumNode?.children ?? []) {
72+
if (sumNodeChild.concept.name === conceptName) {
73+
result = true;
74+
} else if (
75+
this.isConceptIncludedInSum(conceptName, sumNodeChild.concept.name)
76+
) {
77+
result = true;
7378
}
7479
}
75-
return null;
80+
81+
return result;
7682
}
7783

7884
private calculateNode(
@@ -94,6 +100,18 @@ export class CalculationProcessor {
94100

95101
return sum;
96102
}
103+
104+
private buildNodeMap(
105+
nodes: CalculationNode[],
106+
map: Map<string, CalculationNode>,
107+
): void {
108+
for (const node of nodes) {
109+
this.nodes.set(node.concept.name, node);
110+
if (node.children) {
111+
this.buildNodeMap(node.children, map);
112+
}
113+
}
114+
}
97115
}
98116

99117
class CalculationParser {

0 commit comments

Comments
 (0)