All files / projects/requisition-management/src/app/pages/requisition-detail/requisition-cost-center-approval requisition-cost-center-approval.component.ts

100% Statements 18/18
78.57% Branches 11/14
100% Functions 5/5
100% Lines 18/18

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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 762x     2x   2x   2x                         2x                       6x     6x       5x   5x           5x 4x   4x   1x 4x           8x 3x     5x   5x                    
import { ChangeDetectionStrategy, Component, Input, OnChanges, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
 
import { AccountFacade } from 'ish-core/facades/account.facade';
import { CostCenter, CostCenterBuyer } from 'ish-core/models/cost-center/cost-center.model';
import { Price, PriceHelper } from 'ish-core/models/price/price.model';
 
import { Requisition } from '../../../models/requisition/requisition.model';
 
interface BudgetValues {
  spentPercentage: number;
  spentBudgetIncludingThisRequisition: Price;
  spentPercentageIncludingThisRequisition: number;
}
 
@Component({
  selector: 'ish-requisition-cost-center-approval',
  templateUrl: './requisition-cost-center-approval.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class RequisitionCostCenterApprovalComponent implements OnInit, OnChanges {
  @Input({ required: true }) requisition: Requisition;
 
  costCenter: CostCenter;
  orderTotal: Price;
  buyer: CostCenterBuyer;
 
  ccVal: BudgetValues;
  bVal: BudgetValues; // buyer budget values
 
  userEmail$: Observable<string>;
 
  constructor(private accountFacade: AccountFacade) {}
 
  ngOnInit() {
    this.userEmail$ = this.accountFacade.userEmail$;
  }
 
  ngOnChanges() {
    this.costCenter = this.requisition?.approval?.costCenterApproval?.costCenter;
 
    this.orderTotal = {
      type: 'Money',
      value: this.requisition?.totals?.total?.gross,
      currency: this.requisition?.totals?.total?.currency,
    };
 
    if (this.costCenter) {
      this.ccVal = this.determineBudgetValues(this.costCenter);
 
      this.buyer =
        this.costCenter.buyers?.length &&
        this.costCenter.buyers?.find(buyer => buyer.email === this.requisition?.email);
      this.bVal = this.determineBudgetValues(this.buyer);
    }
  }
 
  /**  calculates all displayed prices and percentages for a budget related object */
  private determineBudgetValues(data: CostCenter | CostCenterBuyer): BudgetValues {
    if (!data) {
      return;
    }
 
    const spentBudgetIncludingThisRequisition = PriceHelper.sum(data?.spentBudget, this.orderTotal);
 
    return {
      spentPercentage: data?.spentBudget?.value ? data.spentBudget?.value / data.budget.value : 0,
      spentBudgetIncludingThisRequisition,
      spentPercentageIncludingThisRequisition:
        spentBudgetIncludingThisRequisition?.value && data?.budget?.value
          ? spentBudgetIncludingThisRequisition.value / data?.budget?.value
          : 0,
    };
  }
}