Skip to content

Instantly share code, notes, and snippets.

@Sampath-Lokuge
Last active October 22, 2017 08:30
Show Gist options
  • Save Sampath-Lokuge/bb07d184f05dc800716d0dcd80b24981 to your computer and use it in GitHub Desktop.
Save Sampath-Lokuge/bb07d184f05dc800716d0dcd80b24981 to your computer and use it in GitHub Desktop.
Multi Budget
@Sampath-Lokuge
Copy link
Author

  calculateChartDetails(data: ProjectDetail) {
    this.budgetGroupViewList = [];
    let totalContingency: number = 0;
    let totalOfBudgetAndContingency: number = 0;
    let totalBudget: number = 0;
    if (data.budgetList == null) return;
    forEach(data.budgetList, (b: Budget) => {
      let budgetGroupViewData = new DtoBudgetGroup();
      budgetGroupViewData.budgetGroup = b.budgetGroup;
      budgetGroupViewData.budgetTotal = b.amount;
      budgetGroupViewData.transactions = data.transactions;
      totalContingency = totalContingency + Number(b.amount) * b.contingency / 100;
      totalOfBudgetAndContingency = totalOfBudgetAndContingency + Number(b.amount) + totalContingency;
      totalBudget = totalBudget + Number(b.amount);

      let transactionTotal = 0;
      forEach(data.transactions, (t: Transaction) => {
        if (b.budgetGroup.id == t.budget.budgetGroup.id) {
          transactionTotal = transactionTotal + t.totalPrice;
        }
      });
      if (transactionTotal > budgetGroupViewData.budgetTotal) {
        budgetGroupViewData.isTransactionOver = true;
      }
      budgetGroupViewData.transactionTotal = transactionTotal;
      this.budgetGroupViewList.push(budgetGroupViewData);
    });
    this.totalOfBudgetAndContingency = totalOfBudgetAndContingency;
    this.daysToGo = moment(data.completionDate).diff(moment(), 'days');

    let amountPaid: number = 0;
    let spent: number = 0;
    forEach(data.transactions, (t: Transaction) => {
      if (t.isPaid) amountPaid = amountPaid + t.totalPrice;
      spent = spent + t.totalPrice;
    });
    this.amountPaid = amountPaid;

    if (spent > this.totalOfBudgetAndContingency) {
      this.spentOnChart = this.totalOfBudgetAndContingency;
      this.spent = spent;
    } else {
      this.spent = this.spentOnChart = spent;
    }
    if (spent > totalBudget) {
      this.spentOverColor = "#9B2915";
    }
    this.remainingAmount = this.totalOfBudgetAndContingency - spent;
    this.paidPercentage = (this.amountPaid / this.totalOfBudgetAndContingency) * 100;
    this.contingencyPercentage = (totalContingency / this.totalOfBudgetAndContingency) * 100;

    if (this.paidPercentage > (100 - this.contingencyPercentage)) {
      this.remainingPercentageExcludingPaid = 0;
      this.paidPercentage = 100 - this.contingencyPercentage;
    } else {
      this.remainingPercentageExcludingPaid = (100 - this.contingencyPercentage - this.paidPercentage);
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment