package org.innovateuk.ifs.project.finance.transactional;

import org.innovateuk.ifs.project.finance.domain.CostCategory;

import java.math.BigDecimal;

import static java.math.RoundingMode.HALF_EVEN;

/**
 * Holder of summary information used to generate Spend Profiles
 */
class SpendProfileCostCategorySummary {

    private CostCategory category;
    private BigDecimal total;
    private BigDecimal firstMonthSpend;
    private BigDecimal otherMonthsSpend;

    SpendProfileCostCategorySummary(CostCategory category, BigDecimal total, long projectDurationInMonths) {
        this.category = category;
        this.total = total;

        BigDecimal durationInMonths = BigDecimal.valueOf(projectDurationInMonths);
        BigDecimal monthlyCost = total.divide(durationInMonths, 0, HALF_EVEN);
        BigDecimal remainder = total.subtract(monthlyCost.multiply(durationInMonths)).setScale(0, HALF_EVEN);

        this.firstMonthSpend = monthlyCost.add(remainder);
        this.otherMonthsSpend = monthlyCost;
    }

    public CostCategory getCategory() {
        return category;
    }

    public BigDecimal getTotal() {
        return total;
    }

    public BigDecimal getFirstMonthSpend() {
        return firstMonthSpend;
    }

    public BigDecimal getOtherMonthsSpend() {
        return otherMonthsSpend;
    }
}

@@ -730,27 +730,25 @@
 Decimal 
-monthlyCost
+remainder
  = total
@@ -752,14 +752,17 @@
 tal.
-divi
+remain
 de
+r
 (dur
@@ -778,25 +778,12 @@
 nths
-, 0, HALF_EVEN
 );%0A
+%0A
     
@@ -801,55 +801,125 @@
 mal 
-remainder = total.subtract(monthlyCost.multiply
+perfectlyDivisibleTotal = total.subtract(remainder);%0A        BigDecimal costPerMonth = perfectlyDivisibleTotal.divide
 (dur
@@ -935,20 +935,10 @@
 nths
-)).setScale(
+, 
 0, H
@@ -980,27 +980,28 @@
 Spend = 
-monthlyCost
+costPerMonth
 .add(rem
@@ -1045,19 +1045,20 @@
 d = 
-monthlyCost
+costPerMonth
 ;%0A  
@@ -1366,11 +1366,1259 @@
 d;%0A    %7D
+%0A%0A    public static void main(String%5B%5D args) %7B%0A        System.out.println(%22Test one%22);%0A%0A        //class vars%0A        BigDecimal firstMonthSpend;%0A        BigDecimal otherMonthsSpend;%0A%0A        //params%0A        BigDecimal total = new BigDecimal(32);%0A        long projectDurationInMonths = 36;%0A%0A/*        BigDecimal durationInMonths = BigDecimal.valueOf(projectDurationInMonths);%0A        BigDecimal monthlyCost = total.divide(durationInMonths, 0, HALF_EVEN);%0A        BigDecimal remainder = total.subtract(monthlyCost.multiply(durationInMonths)).setScale(0, HALF_EVEN);%0A%0A        firstMonthSpend = monthlyCost.add(remainder);%0A        otherMonthsSpend = monthlyCost;*/%0A%0A%0A        BigDecimal durationInMonths = BigDecimal.valueOf(projectDurationInMonths);%0A        BigDecimal remainder = total.remainder(durationInMonths);%0A%0A        BigDecimal perfectlyDivisibleTotal = total.subtract(remainder);%0A        BigDecimal costPerMonth = perfectlyDivisibleTotal.divide(durationInMonths, 0, HALF_EVEN);%0A%0A        firstMonthSpend = costPerMonth.add(remainder);%0A        otherMonthsSpend = costPerMonth;%0A%0A        System.out.println(%22First Month spend: %22 + firstMonthSpend.toString());%0A        System.out.println(%22Other Month spend: %22 + otherMonthsSpend.toString());%0A    %7D
 %0A%7D%0A

INFUND-6148-Negative_Numbers-SP-Generation