From 0edfe660ebf606846bbe5c79851f0ae8e52686c5 Mon Sep 17 00:00:00 2001 From: nblondheim Date: Wed, 2 Oct 2024 10:21:51 -0500 Subject: [PATCH] Validate bl and mod chiller load totals --- .../chiller-staging-form.service.ts | 34 ++++++++++++++++++- .../chiller-staging-form.component.html | 3 ++ .../chiller-staging-form.component.ts | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/app/calculator/process-cooling/chiller-staging/chiller-staging-form.service.ts b/src/app/calculator/process-cooling/chiller-staging/chiller-staging-form.service.ts index 4c7f78c826..ae20119824 100644 --- a/src/app/calculator/process-cooling/chiller-staging/chiller-staging-form.service.ts +++ b/src/app/calculator/process-cooling/chiller-staging/chiller-staging-form.service.ts @@ -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() @@ -23,6 +23,8 @@ 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); @@ -30,6 +32,7 @@ export class ChillerStagingFormService { let modLoadList: UntypedFormArray = this.getLoadFormArray(form.controls.modLoadList); this.setLoadValidators(baselineLoadList); this.setLoadValidators(modLoadList); + this.setTotalLoadEqualityValidator(form) return form; } @@ -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 = { diff --git a/src/app/calculator/process-cooling/chiller-staging/chiller-staging-form/chiller-staging-form.component.html b/src/app/calculator/process-cooling/chiller-staging/chiller-staging-form/chiller-staging-form.component.html index d4209dc567..f4f8e2b552 100644 --- a/src/app/calculator/process-cooling/chiller-staging/chiller-staging-form/chiller-staging-form.component.html +++ b/src/app/calculator/process-cooling/chiller-staging/chiller-staging-form/chiller-staging-form.component.html @@ -229,6 +229,9 @@
+
+ Sum of all baseline chiller loads must be equal to that of modification chiller loads. +
diff --git a/src/app/calculator/process-cooling/chiller-staging/chiller-staging-form/chiller-staging-form.component.ts b/src/app/calculator/process-cooling/chiller-staging/chiller-staging-form/chiller-staging-form.component.ts index bff544df8b..bdb6433c81 100644 --- a/src/app/calculator/process-cooling/chiller-staging/chiller-staging-form/chiller-staging-form.component.ts +++ b/src/app/calculator/process-cooling/chiller-staging/chiller-staging-form/chiller-staging-form.component.ts @@ -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) }