DevTreks.Extensions/NPVCalculators/BIs/BICalculator.cs
/// <summary>
/// Set rules, interest charges, and price adjustments for each output included in a budget or capital investment
/// </summary>
Set rules, interest charges, and price adjustments for each output included in a budget or capital investment
Set rules interest charges and price adjustments for each output included in a budget or capital investment
public double CalculateOutputPrice( 
            Output output, CalculatorParameters calcParameters)
        {
            double dbCalcOutputPrice = 0;
            double dbDaysToEOP = 0;
            double dbNomRate = calcParameters.ParentBudgetInvestment.Local.NominalRate;
            double dbNomDiscountFactor = 0;
            double dbDiscountFactor = 0;
            double dbNomMonths = 0;
            double dbRealYrs = 0;
            double dbRegionMult = 1;
            double dbTimePeriods = 0;
            double dbOutputPrice = 0;
            bool bDiscountYorN = true;
            DateTime dtEOPDate;
            DateTime dtOutputDate;
            double dbDaysFromCRPToEOP = 0;
            double dbDaysPerPeriod = 0;
            double dbRealInterestDays = 0;
            double dbInterest = 0;
            double dbInputandInterestTotal = 0;
            double dbValue = 0;
            System.TimeSpan spanDays;
            double dbSpanDays = 0;
            double dbOutAmount = 0;
            double dbCompositionAmount = 0;
            dbInterest = 0;
            bDiscountYorN = calcParameters.ParentTimePeriod.IsDiscounted;
            dtEOPDate = calcParameters.ParentTimePeriod.Date;
            dtOutputDate = output.Date;
            spanDays = dtEOPDate - dtOutputDate;
            dbDaysToEOP = spanDays.Days;
            if (dbDaysToEOP < 0)
            {
                dbDaysToEOP = -1 * (dbDaysToEOP);
            }
            dbNomMonths = dbDaysToEOP / 30;
            if (bDiscountYorN == true)
            {
                dbNomDiscountFactor = GeneralRules.CalculateDiscountFactorByMonths(dbNomRate, dbNomMonths);
            }
            else
            {
                dbNomDiscountFactor = 1;
                dbNomRate = 0;
            }
            dbOutputPrice = output.Price;
            dbOutputPrice = dbOutputPrice * dbRegionMult;
            dbOutAmount = output.Amount;
            output.Amount = dbOutAmount;
            dbCompositionAmount = output.CompositionAmount;
            output.CompositionAmount = dbCompositionAmount;
            dbCalcOutputPrice = ((dbOutputPrice * dbOutAmount) * dbCompositionAmount * output.Times);
            dbInterest = ((dbCalcOutputPrice * dbNomDiscountFactor) - dbCalcOutputPrice);
            dbValue = dbCalcOutputPrice;
            output.TotalR = dbValue;
            dbValue = dbInterest;
            output.TotalR_INT = dbValue;
            if (dtEOPDate <= calcParameters.ParentBudgetInvestment.InitEOPDate)
            {
                spanDays = dtEOPDate - calcParameters.ParentBudgetInvestment.InitEOPDate;
                dbSpanDays = spanDays.Days;
                dbTimePeriods = dbSpanDays / DOUBLE_YEAR;
                if (dbTimePeriods != 0)
                {
                    spanDays = dtEOPDate - calcParameters.ParentBudgetInvestment.InitEOPDate;
                    dbDaysFromCRPToEOP = spanDays.Days;
                    if (dbDaysFromCRPToEOP < 0)
                    {
                        dbDaysFromCRPToEOP = -1 * (dbDaysFromCRPToEOP);
                    }
                    dbDaysPerPeriod = dbDaysFromCRPToEOP / dbTimePeriods;
                    if (dbDaysPerPeriod < 0)
                    {
                        dbDaysPerPeriod = -1 * (dbDaysPerPeriod);
                    }
                    dbRealInterestDays = dbDaysFromCRPToEOP - dbDaysPerPeriod;
                    if (dbRealInterestDays < 0)
                    {
                        dbRealInterestDays = -1 * dbRealInterestDays;
                    }
                    dbRealYrs = dbRealInterestDays / DOUBLE_YEAR;
                    if (bDiscountYorN == true)
                    {
                        dbDiscountFactor = (System.Math.Pow((1 + calcParameters.ParentBudgetInvestment.Local.RealRate), dbRealYrs));
                    }
                    else
                    {
                        dbDiscountFactor = 1;
                    }
                    dbInterest = output.TotalR_INT;
                    dbInputandInterestTotal = output.TotalR + dbInterest;
                    dbInterest = dbInterest + ((dbInputandInterestTotal * dbDiscountFactor) 
                        - dbInputandInterestTotal);
                    dbValue = dbInterest;
                    dbInputandInterestTotal = output.TotalR + dbInterest;
                    output.TotalR_INT = dbValue;
                    dbInterest = dbInterest + (dbInputandInterestTotal * dbNomRate);
                    dbValue = dbInterest;
                    output.TotalR_INT = dbValue;
                }
            }
            if ((dtOutputDate > calcParameters.ParentBudgetInvestment.InitEOPDate) 
                && (bDiscountYorN))
            {
                spanDays = calcParameters.ParentBudgetInvestment.InitEOPDate - dtEOPDate;
                dbDaysFromCRPToEOP = spanDays.Days;
                if (dbDaysFromCRPToEOP < 0)
                {
                    dbDaysFromCRPToEOP = -1 * (dbDaysFromCRPToEOP);
                }
                dbRealYrs = dbDaysFromCRPToEOP / DOUBLE_YEAR;
                if (bDiscountYorN == true)
                {
                    dbDiscountFactor = 1 / (System.Math.Pow((1 + calcParameters.ParentBudgetInvestment.Local.RealRate), dbRealYrs));
                }
                else
                {
                    dbDiscountFactor = 1;
                }
                dbInputandInterestTotal = output.TotalR + output.TotalR_INT;
                dbInterest = -1 * (dbInputandInterestTotal - (dbInputandInterestTotal * dbDiscountFactor));
                dbValue = dbInterest;
                output.TotalR_INT = dbValue;
            }
            double dbIncentiveAdjustedSum = 0;
            CalculateIncentiveForOutput(output,
                calcParameters, ref dbIncentiveAdjustedSum);
            return dbCalcOutputPrice;
        }
public double CalculateOutputPrice ( Output output , CalculatorParameters calcParameters ) { double dbCalcOutputPrice = 0 ; double dbDaysToEOP = 0 ; double dbNomRate = calcParameters . ParentBudgetInvestment . Local . NominalRate ; double dbNomDiscountFactor = 0 ; double dbDiscountFactor = 0 ; double dbNomMonths = 0 ; double dbRealYrs = 0 ; double dbRegionMult = 1 ; double dbTimePeriods = 0 ; double dbOutputPrice = 0 ; bool bDiscountYorN = true ; DateTime dtEOPDate ; DateTime dtOutputDate ; double dbDaysFromCRPToEOP = 0 ; double dbDaysPerPeriod = 0 ; double dbRealInterestDays = 0 ; double dbInterest = 0 ; double dbInputandInterestTotal = 0 ; double dbValue = 0 ; System . TimeSpan spanDays ; double dbSpanDays = 0 ; double dbOutAmount = 0 ; double dbCompositionAmount = 0 ; dbInterest = 0 ; bDiscountYorN = calcParameters . ParentTimePeriod . IsDiscounted ; dtEOPDate = calcParameters . ParentTimePeriod . Date ; dtOutputDate = output . Date ; spanDays = dtEOPDate - dtOutputDate ; dbDaysToEOP = spanDays . Days ; if ( dbDaysToEOP < 0 ) { dbDaysToEOP = - 1 * ( dbDaysToEOP ) ; } dbNomMonths = dbDaysToEOP / 30 ; if ( bDiscountYorN == true ) { dbNomDiscountFactor = GeneralRules . CalculateDiscountFactorByMonths ( dbNomRate , dbNomMonths ) ; } else { dbNomDiscountFactor = 1 ; dbNomRate = 0 ; } dbOutputPrice = output . Price ; dbOutputPrice = dbOutputPrice * dbRegionMult ; dbOutAmount = output . Amount ; output . Amount = dbOutAmount ; dbCompositionAmount = output . CompositionAmount ; output . CompositionAmount = dbCompositionAmount ; dbCalcOutputPrice = ( ( dbOutputPrice * dbOutAmount ) * dbCompositionAmount * output . Times ) ; dbInterest = ( ( dbCalcOutputPrice * dbNomDiscountFactor ) - dbCalcOutputPrice ) ; dbValue = dbCalcOutputPrice ; output . TotalR = dbValue ; dbValue = dbInterest ; output . TotalR_INT = dbValue ; if ( dtEOPDate <= calcParameters . ParentBudgetInvestment . InitEOPDate ) { spanDays = dtEOPDate - calcParameters . ParentBudgetInvestment . InitEOPDate ; dbSpanDays = spanDays . Days ; dbTimePeriods = dbSpanDays / DOUBLE_YEAR ; if ( dbTimePeriods != 0 ) { spanDays = dtEOPDate - calcParameters . ParentBudgetInvestment . InitEOPDate ; dbDaysFromCRPToEOP = spanDays . Days ; if ( dbDaysFromCRPToEOP < 0 ) { dbDaysFromCRPToEOP = - 1 * ( dbDaysFromCRPToEOP ) ; } dbDaysPerPeriod = dbDaysFromCRPToEOP / dbTimePeriods ; if ( dbDaysPerPeriod < 0 ) { dbDaysPerPeriod = - 1 * ( dbDaysPerPeriod ) ; } dbRealInterestDays = dbDaysFromCRPToEOP - dbDaysPerPeriod ; if ( dbRealInterestDays < 0 ) { dbRealInterestDays = - 1 * dbRealInterestDays ; } dbRealYrs = dbRealInterestDays / DOUBLE_YEAR ; if ( bDiscountYorN == true ) { dbDiscountFactor = ( System . Math . Pow ( ( 1 + calcParameters . ParentBudgetInvestment . Local . RealRate ) , dbRealYrs ) ) ; } else { dbDiscountFactor = 1 ; } dbInterest = output . TotalR_INT ; dbInputandInterestTotal = output . TotalR + dbInterest ; dbInterest = dbInterest + ( ( dbInputandInterestTotal * dbDiscountFactor ) - dbInputandInterestTotal ) ; dbValue = dbInterest ; dbInputandInterestTotal = output . TotalR + dbInterest ; output . TotalR_INT = dbValue ; dbInterest = dbInterest + ( dbInputandInterestTotal * dbNomRate ) ; dbValue = dbInterest ; output . TotalR_INT = dbValue ; } } if ( ( dtOutputDate > calcParameters . ParentBudgetInvestment . InitEOPDate ) && ( bDiscountYorN ) ) { spanDays = calcParameters . ParentBudgetInvestment . InitEOPDate - dtEOPDate ; dbDaysFromCRPToEOP = spanDays . Days ; if ( dbDaysFromCRPToEOP < 0 ) { dbDaysFromCRPToEOP = - 1 * ( dbDaysFromCRPToEOP ) ; } dbRealYrs = dbDaysFromCRPToEOP / DOUBLE_YEAR ; if ( bDiscountYorN == true ) { dbDiscountFactor = 1 / ( System . Math . Pow ( ( 1 + calcParameters . ParentBudgetInvestment . Local . RealRate ) , dbRealYrs ) ) ; } else { dbDiscountFactor = 1 ; } dbInputandInterestTotal = output . TotalR + output . TotalR_INT ; dbInterest = - 1 * ( dbInputandInterestTotal - ( dbInputandInterestTotal * dbDiscountFactor ) ) ; dbValue = dbInterest ; output . TotalR_INT = dbValue ; } double dbIncentiveAdjustedSum = 0 ; CalculateIncentiveForOutput ( output , calcParameters , ref dbIncentiveAdjustedSum ) ; return dbCalcOutputPrice ; }
Set rules, interest charges, and price adjustments for each output included in a budget or capital investment
Set rules interest charges and price adjustments for each output included in a budget or capital investment
//inititalize variables //common reference point DateTime is dtInitEOPDate //end of period DateTime (for all periods //Step 1: calculate a with-in year interest charge on Outputs using a nominal interest rate //   but do not accrue interest on any Output that is part of an operation or component that will //   be amortized; because that capital recovery factor will provide an opportunity interest charge //   for the Output //nominal discount factor used to calculate w/i period nominal interest charge //output price //set it for display //set it for display //v145 bug left out output.times //set the prices //Step 2: if (this Output is a preproduction revenue, add interest to it from it's end of period DateTime //to 1 time period before the common reference point time period, using a real rate //this step is only used to add real interest between annual time periods (not monthly) //a) How many annual time periods in the preproduction period? //b) How many days from this Output's end of period to the common reference point DateTime? //c) How many days in a time period? //d) Subtract out the preproductive year that already has a nominal //interest charge //e) accrue interest on these days using an annual discounting formula, if the periods are at least annual //output price //added 0.9.7 to be consistent with original calcs //f) add nominal interest for the productive year, so that all prices are at common reference point //Step 3. if this Output comes after the common reference point, discount the interest back to the //common reference point. Discount the prices back to the common point and redo the totals //a) first discount it's interest, using end of period DateTime //discounted total output cost (nominal wi period) //discount this total back to the reference point using real discount factor //no need to add the wi period nominal interest - the real interest was based on a total that included that interest
output
Output
calcParameters
CalculatorParameters