All files / projects/organization-management/src/app/components/cost-center-budget cost-center-budget.component.ts

100% Statements 9/9
66.66% Branches 4/6
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 364x   4x             4x               1x 1x         1x 1x   1x           2x      
import { ChangeDetectionStrategy, Component, Input, OnChanges, SimpleChanges } from '@angular/core';
 
import { CostCenter } from 'ish-core/models/cost-center/cost-center.model';
 
@Component({
  selector: 'ish-cost-center-budget',
  templateUrl: './cost-center-budget.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CostCenterBudgetComponent implements OnChanges {
  @Input({ required: true }) costCenter: CostCenter;
  @Input() progressBarClass: string;
 
  spentBudgetPercentage: number;
  remainingBudgetPercentage: number;
 
  ngOnChanges(changes: SimpleChanges) {
    if (changes.costCenter) {
      this.calculate();
    }
  }
 
  private calculate() {
    if (this.costCenter) {
      this.spentBudgetPercentage =
        this.costCenter.budget.value === 0 ? 0 : this.costCenter.spentBudget?.value / this.costCenter.budget.value;
      this.remainingBudgetPercentage =
        this.costCenter.budget.value === 0 ? 0 : this.costCenter.remainingBudget.value / this.costCenter.budget.value;
    }
  }
 
  get progressBarWidth() {
    return Math.min(this.spentBudgetPercentage, 1);
  }
}