Skip to content

Commit

Permalink
Merge pull request #7038 from ORNL-AMO/issue-7035
Browse files Browse the repository at this point in the history
Issue 7035 - Validate bl and mod chiller load totals
  • Loading branch information
rhernandez-intertech authored Oct 8, 2024
2 parents 783d640 + 0edfe66 commit 92dc233
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { AbstractControl, UntypedFormArray, UntypedFormBuilder, UntypedFormControl, UntypedFormGroup, Validators } from '@angular/forms';
import { AbstractControl, FormGroup, UntypedFormArray, UntypedFormBuilder, UntypedFormControl, UntypedFormGroup, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
import { ChillerStagingInput } from '../../../shared/models/chillers';

@Injectable()
Expand All @@ -23,13 +23,16 @@ export class ChillerStagingFormService {
baselineLoadList: this.formBuilder.array(inputObj.baselineLoadList),
modLoadList: this.formBuilder.array(inputObj.modLoadList),
electricityCost: [inputObj.electricityCost, [Validators.required, Validators.min(0)]]
}, {
validators: this.setTotalLoadEqualityValidator
});

form = this.setWaterTempValidators(form);
let baselineLoadList: UntypedFormArray = this.getLoadFormArray(form.controls.baselineLoadList);
let modLoadList: UntypedFormArray = this.getLoadFormArray(form.controls.modLoadList);
this.setLoadValidators(baselineLoadList);
this.setLoadValidators(modLoadList);
this.setTotalLoadEqualityValidator(form)
return form;
}

Expand Down Expand Up @@ -75,6 +78,35 @@ export class ChillerStagingFormService {
return form;
}

setTotalLoadEqualityValidator: ValidatorFn = (
form: AbstractControl,
): ValidationErrors | null => {
let baselineLoadList: UntypedFormArray = this.getLoadFormArray(form.get('baselineLoadList'));
let modificationLoadList: UntypedFormArray = this.getLoadFormArray(form.get('modLoadList'));

if (baselineLoadList.controls.length > 0 && modificationLoadList && modificationLoadList.controls.length > 0) {
const baselineLoadTotal = baselineLoadList.controls.reduce((total, currentControl: AbstractControl) => total + currentControl.value, 0);
const modificationLoadTotal = modificationLoadList.controls.reduce((total, currentControl: AbstractControl) => total + currentControl.value, 0);
let difference = Math.abs(baselineLoadTotal - modificationLoadTotal);
try {
if (difference === 0) {
return null;
}
}
catch (e) {
console.log(e);
return {
totalLoadEquality: difference
};
}
return {
totalLoadEquality: difference
};
}
else {
return null;
}
};

getChillerStagingInput(form: UntypedFormGroup): ChillerStagingInput {
let obj: ChillerStagingInput = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@
</div>
</div>
<div class="row">
<div class="alert-danger p-2 small" *ngIf="form.hasError('totalLoadEquality') && (form.touched || form.dirty)">
Sum of all baseline chiller loads must be equal to that of modification chiller loads.
</div>
<div class="col-12 text-center">
<a class="click-link small" (click)="addChiller()">Add Chiller</a>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export class ChillerStagingFormComponent implements OnInit {
calculate() {
let updatedInput: ChillerStagingInput = this.chillerStagingFormService.getChillerStagingInput(this.form);
this.form = this.chillerStagingFormService.setWaterTempValidators(this.form);
this.form.setValidators(this.chillerStagingFormService.setTotalLoadEqualityValidator);
this.chillerStagingService.chillerStagingInput.next(updatedInput)
}

Expand Down

0 comments on commit 92dc233

Please sign in to comment.