private static void CheckForLastStepCalculator(
     AgBudgetingHelpers.CALCULATOR_TYPES calculatorType,
     ContractHelpers.EXTENSION_STEPS stepNumber,
     ExtensionContentURI extDocToCalcURI)
 {
     //if (calculatorType
     //    == AgBudgetingHelpers.CALCULATOR_TYPES.locals)
     //{
     //    if (stepNumber == ContractHelpers.EXTENSION_STEPS.stepthree)
     //    {
     //        //locals use 2 steps
     //        saveTempDocInDb = true;
     //    }
     //}
     //else -remaining calculators in this extension use 4 steps
 }
        /// <summary>
        /// Calculate amoritized capital service costs
        /// </summary>
        /// <param name="calculatorType"></param>
        /// <param name="i"></param>
        /// <param name="timePeriods"></param>
        /// <param name="plannedUsePeriods"></param>
        /// <param name="usefulLifePeriods"></param>
        /// <param name="realRate"></param>
        /// <param name="timeType"></param>
        /// <param name="sumCosts"></param>
        /// <param name="sumOutputs"></param>
        /// <param name="amortizedAvgCostperPeriod"></param>
        public static void CalcAmortizdServiceCosts(AgBudgetingHelpers.CALCULATOR_TYPES calculatorType,
                                                    int i, int timePeriods, int plannedUsePeriods, int usefulLifePeriods,
                                                    double realRate, GeneralRules.TIME_TYPES timeType, double sumCosts, double sumOutputs,
                                                    out double amortizedAvgCostperPeriod)
        {
            amortizedAvgCostperPeriod = 0;
            double dbAnnuityFactor = 0;

            //new machinery
            if (timePeriods > 1)
            {
                //convert to annuity
                dbAnnuityFactor = GeneralRules.CapRecovFactor(timePeriods, realRate, 0);
                sumCosts        = sumCosts * dbAnnuityFactor;
                if (timeType == GeneralRules.TIME_TYPES.costsvary)
                {
                    amortizedAvgCostperPeriod = sumCosts / plannedUsePeriods;
                }
                else if (timeType == GeneralRules.TIME_TYPES.costsandoutputsvary)
                {
                    if (sumOutputs == 0)
                    {
                        //shouldn't be using outputs
                        amortizedAvgCostperPeriod = sumCosts / plannedUsePeriods;
                    }
                    else
                    {
                        amortizedAvgCostperPeriod = sumCosts / (sumOutputs / timePeriods);
                    }
                }
            }
            else
            {
                if ((i == 3) && (calculatorType == AgBudgetingHelpers.CALCULATOR_TYPES.agmachinery))
                {
                    amortizedAvgCostperPeriod = sumCosts / usefulLifePeriods;
                }
                else
                {
                    amortizedAvgCostperPeriod = sumCosts / plannedUsePeriods;
                }
            }
        }
        private bool RunInputOrOutputCalculation(XElement currentElement,
                                                 XElement currentCalculationsElement, IDictionary <string, string> updates)
        {
            bool bHasCalculations = false;

            AgBudgetingHelpers.CALCULATOR_TYPES eCalculatorType
                = AgBudgetingHelpers.GetCalculatorType(
                      this.GCCalculatorParams.CalculatorType);
            switch (eCalculatorType)
            {
            case AgBudgetingHelpers.CALCULATOR_TYPES.agmachinery:
                bHasCalculations
                    = Machinery1InputCalculator.SetMachinery1Calculations(
                          eCalculatorType, this.GCCalculatorParams, currentCalculationsElement,
                          currentElement, updates);
                break;

            case AgBudgetingHelpers.CALCULATOR_TYPES.irrpower:
                bHasCalculations
                    = Machinery1InputCalculator.SetMachinery1Calculations(
                          eCalculatorType, this.GCCalculatorParams, currentCalculationsElement,
                          currentElement, updates);
                break;

            case AgBudgetingHelpers.CALCULATOR_TYPES.lifecycle:
                bHasCalculations
                    = Machinery1InputCalculator.SetMachinery1Calculations(
                          eCalculatorType, this.GCCalculatorParams, currentCalculationsElement,
                          currentElement, updates);
                break;

            case AgBudgetingHelpers.CALCULATOR_TYPES.gencapital:
                bHasCalculations
                    = Machinery1InputCalculator.SetMachinery1Calculations(
                          eCalculatorType, this.GCCalculatorParams, currentCalculationsElement,
                          currentElement, updates);
                break;

            default:
                break;
            }
            return(bHasCalculations);
        }
        /// <summary>
        /// Calculate used machinery repair costs
        /// </summary>
        /// <param name="calculatorType"></param>
        /// <param name="timeUsed"></param>
        /// <param name="timePeriods"></param>
        /// <param name="plannedUsePeriods"></param>
        /// <param name="startingPeriods"></param>
        /// <param name="remainingPeriods"></param>
        /// <param name="realRate"></param>
        /// <param name="inflationRate"></param>
        /// <param name="timeType"></param>
        /// <param name="RF1"></param>
        /// <param name="RF2"></param>
        /// <param name="OC3Amount"></param>
        /// <param name="sumCosts"></param>
        /// <param name="sumOutputs"></param>
        /// <param name="sumUsedCost"></param>
        /// <param name="amortizedAvgCostperPeriod"></param>
        public static void CalcUsedMachineryRepairCosts(AgBudgetingHelpers.CALCULATOR_TYPES calculatorType, int timeUsed, int timePeriods, int plannedUsePeriods,
                                                        int startingPeriods, int remainingPeriods, double realRate, double inflationRate, GeneralRules.TIME_TYPES timeType,
                                                        double RF1, double RF2, double OC3Amount, double sumCosts, double sumOutputs, double sumUsedCost,
                                                        out double amortizedAvgCostperPeriod)
        {
            amortizedAvgCostperPeriod = 0;
            double dbAnnuityFactor = 0;

            //repair costs for used machinery
            if (timePeriods > 1)
            {
                //only difference is the period over which the amortization takes place -note: this is ad hoc
                //sumCosts = sumCosts;
                //amortize this remainder over the remaining service life of the equipment
                dbAnnuityFactor = GeneralRules.CapRecovFactor((timePeriods - timeUsed), realRate, 0);
                sumCosts        = sumCosts * dbAnnuityFactor;
                if (timeType == GeneralRules.TIME_TYPES.costsvary)
                {
                    amortizedAvgCostperPeriod = sumCosts / plannedUsePeriods;
                }
                else if (timeType == GeneralRules.TIME_TYPES.costsandoutputsvary)
                {
                    if (sumOutputs == 0)
                    {
                        //shouldn't be using outputs
                        amortizedAvgCostperPeriod = sumCosts / plannedUsePeriods;
                    }
                    else
                    {
                        //simple avg unit cost - or use a particular year in ui
                        amortizedAvgCostperPeriod = sumCosts / (sumOutputs / timePeriods);
                    }
                }
                else
                {
                    //the used costs will not have been set in for...next loop, set it here
                    if (calculatorType == AgBudgetingHelpers.CALCULATOR_TYPES.agmachinery)
                    {
                        sumUsedCost = (((RF1 * OC3Amount) * System.Math.Pow((startingPeriods / 1000.00), RF2))) * (1 + inflationRate);
                        amortizedAvgCostperPeriod = (sumCosts - sumUsedCost) / remainingPeriods;
                    }
                    else
                    {
                        amortizedAvgCostperPeriod = (sumCosts) / plannedUsePeriods;
                    }
                }
            }
            else
            {
                //12/9 : the costsdonotvary sets timeperiod to 1
                //the used costs will not have been set in for...next loop, set it here
                if (calculatorType == AgBudgetingHelpers.CALCULATOR_TYPES.agmachinery)
                {
                    sumUsedCost = (((RF1 * OC3Amount) * System.Math.Pow((startingPeriods / 1000.00), RF2))) * (1 + inflationRate);
                    amortizedAvgCostperPeriod = (sumCosts - sumUsedCost) / remainingPeriods;
                }
                else
                {
                    amortizedAvgCostperPeriod = (sumCosts) / plannedUsePeriods;
                }
            }
        }
 //constructor sets class (base) properties
 public ABIOSubscriber(CalculatorParameters calcParameters,
                       AgBudgetingHelpers.CALCULATOR_TYPES calculatorType)
     : base(calcParameters)
 {
     this.CalculatorType = calculatorType;
 }
        public async Task <bool> RunCalculatorStep(
            ExtensionContentURI extDocToCalcURI, ExtensionContentURI extCalcDocURI,
            string stepNumber, IList <string> urisToAnalyze,
            IDictionary <string, string> updates, CancellationToken cancellationToken)
        {
            bool bHasCalculation = false;
            CalculatorHelpers    eCalcHelpers = new CalculatorHelpers();
            CalculatorParameters ABCalcParams
                = CalculatorHelpers.SetCalculatorParameters(
                      extDocToCalcURI, extCalcDocURI, stepNumber, urisToAnalyze,
                      updates);
            AgBudgetingHelpers agBudgetHelpers
                = new AgBudgetingHelpers(ABCalcParams);

            ContractHelpers.EXTENSION_STEPS eStepNumber
                = ContractHelpers.GetEnumStepNumber(stepNumber);
            bool bHasUpdates = false;

            switch (eStepNumber)
            {
            case ContractHelpers.EXTENSION_STEPS.stepzero:
                bHasCalculation = true;
                break;

            case ContractHelpers.EXTENSION_STEPS.stepone:
                bHasCalculation = true;
                break;

            case ContractHelpers.EXTENSION_STEPS.steptwo:
                //clear updates collection
                updates.Clear();
                if (ABCalcParams != null)
                {
                    //set constants for this step
                    bHasCalculation
                        = await AgBudgetingHelpers.SetConstants(ABCalcParams);
                }
                extDocToCalcURI.URIDataManager.NeedsFullView    = false;
                extDocToCalcURI.URIDataManager.NeedsSummaryView = false;
                break;

            case ContractHelpers.EXTENSION_STEPS.stepthree:
                //get rid of any update member that was added after running the same step 2x
                bHasUpdates = await CalculatorHelpers.RefreshUpdates(ABCalcParams, stepNumber, updates);

                if (ABCalcParams != null)
                {
                    bHasCalculation
                        = await AgBudgetingHelpers.SetConstants(ABCalcParams);
                }
                extDocToCalcURI.URIDataManager.NeedsFullView    = false;
                extDocToCalcURI.URIDataManager.NeedsSummaryView = false;
                break;

            case ContractHelpers.EXTENSION_STEPS.stepfour:
                //get rid of any update member that was added after running the same step 2x
                bHasUpdates = await CalculatorHelpers.RefreshUpdates(ABCalcParams, stepNumber, updates);

                if (ABCalcParams != null)
                {
                    if (ABCalcParams.ExtensionDocToCalcURI.URIDataManager.SubAppType
                        != Constants.SUBAPPLICATION_TYPES.devpacks.ToString())
                    {
                        //run the calculations
                        bHasCalculation = await agBudgetHelpers.RunCalculations();
                    }
                    else
                    {
                        //run custom document calculations
                        bHasCalculation
                            = await agBudgetHelpers.RunDevPacksCalculations(ABCalcParams);
                    }
                    if (!bHasCalculation)
                    {
                        extDocToCalcURI.ErrorMessage = (extDocToCalcURI.ErrorMessage == string.Empty) ?
                                                       Errors.MakeStandardErrorMsg("CALCULATORS_URI_MISMATCH")
                                : extDocToCalcURI.ErrorMessage;
                        return(bHasCalculation);
                    }
                    if (string.IsNullOrEmpty(extDocToCalcURI.ErrorMessage))
                    {
                        //two step calculators need to be saved now
                        AgBudgetingHelpers.CALCULATOR_TYPES eCalculatorType
                            = AgBudgetingHelpers.GetCalculatorType(
                                  ABCalcParams.CalculatorType);
                        CheckForLastStepCalculator(eCalculatorType,
                                                   eStepNumber, extDocToCalcURI);
                        //replace the old calculator with the new one
                        //and save the new calculations document
                        bHasCalculation
                            = await CalculatorHelpers.SaveNewCalculationsDocument(ABCalcParams);
                    }
                    else
                    {
                        bHasCalculation = false;
                    }
                }
                extDocToCalcURI.URIDataManager.NeedsFullView    = true;
                extDocToCalcURI.URIDataManager.NeedsSummaryView = false;
                break;

            case ContractHelpers.EXTENSION_STEPS.stepfive:
                extDocToCalcURI.URIDataManager.NeedsFullView    = false;
                extDocToCalcURI.URIDataManager.NeedsSummaryView = true;
                extCalcDocURI.URIFileExtensionType = CalculatorHelpers.GetAttribute(ABCalcParams.LinkedViewElement,
                                                                                    Calculator1.cFileExtensionType);
                bHasCalculation = true;
                //tells addinhelper to save calcs
                CalculatorHelpers.SetTempDocSaveCalcsProperty(extDocToCalcURI);
                break;

            default:
                //as many steps as needed can be added to this addin
                break;
            }
            extDocToCalcURI.ErrorMessage += ABCalcParams.ErrorMessage;
            return(bHasCalculation);
        }