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

100% Statements 10/10
72.72% Branches 8/11
100% Functions 2/2
100% Lines 10/10

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 521x       1x                     1x                 2x         2x 2x           2x       2x   2x   2x              
import { ChangeDetectionStrategy, Component, Input, OnChanges } from '@angular/core';
 
import { Price } from 'ish-core/models/price/price.model';
 
import { Requisition } from '../../../models/requisition/requisition.model';
 
/**
 * The buyer approval info box
 *
 */
@Component({
  selector: 'ish-requisition-buyer-approval',
  templateUrl: './requisition-buyer-approval.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class RequisitionBuyerApprovalComponent implements OnChanges {
  @Input({ required: true }) requisition: Requisition;
 
  orderTotal: Price;
  spentPercentage: number;
  spentPercentageIncludingThisRequisition: number;
  leftPercentage: number;
 
  ngOnChanges() {
    this.calculate();
  }
 
  /**  calculates all displayed prices and percentages */
  private calculate() {
    if (this.requisition) {
      this.orderTotal = {
        type: 'Money',
        value: this.requisition.totals.total.gross,
        currency: this.requisition.totals.total.currency,
      };
 
      this.spentPercentage =
        this.requisition.userBudget?.spentBudget?.value && this.requisition.userBudget.budget?.value
          ? this.requisition.userBudget.spentBudget.value / this.requisition.userBudget.budget.value
          : 0;
      this.leftPercentage = this.spentPercentage < 1 ? 1 - this.spentPercentage : 0;
 
      const spentBudgetIncludingThisOrder = this.requisition.userBudget?.spentBudgetIncludingThisRequisition;
 
      this.spentPercentageIncludingThisRequisition =
        spentBudgetIncludingThisOrder?.value && this.requisition.userBudget?.budget?.value
          ? spentBudgetIncludingThisOrder.value / this.requisition.userBudget?.budget.value
          : 0;
    }
  }
}