//Calcuation Controller Action public void CalculateAction(List<CategoryViewModel> jCategory) { foreach (var group in jCategory) { foreach (var item in group.Functions) { if (item.Function == "Input") { item.Output = InputFunctions.Output(item.Type, item.Output); OutputList.Add(new OutputList { ID = Convert.ToString(item.ID), Field = item.Name, Value = item.Output, Group = group.Name }); } else { //Logic check at Column Level string colLogic = null; bool colLogicParse = true; if(group.Logic != null) { foreach (var bit in group.Logic) { var grouplastLogic = group.Logic.Last(); string grouplastLogicOperator = grouplastLogic.Operator; Logic Logic = new Logic(); colLogic = Logic.Output(jCategory, bit, group.ID, 0); Expression ex = new Expression(colLogic); try { colLogicParse = Convert.ToBoolean(ex.Evaluate()); } catch (Exception exception) { logger.Error(exception); throw new HttpException(exception.ToString()); } if (grouplastLogicOperator == "AND" && colLogicParse == false) { break; } else if (grouplastLogicOperator == "OR" && colLogicParse == true) { colLogicParse = true; break; } } } if (item.Parameter.Count > 0) { string logic = null; bool logicparse = true; string MathString = null; bool PowOpen = false; //Logic check at column level if (colLogicParse == true) { foreach (var bit in item.Logic) { var lastLogic = item.Logic.Last(); string lastLogicOperator = lastLogic.Operator; Logic Logic = new Logic(); logic = Logic.Output(jCategory, bit, group.ID, item.ID); Expression ex = new Expression(logic); try { logicparse = Convert.ToBoolean(ex.Evaluate()); } catch (Exception exception) { logger.Error(exception); throw new HttpException(exception.ToString()); } if (lastLogicOperator == "AND" && logicparse == false) { break; } else if (lastLogicOperator == "OR" && logicparse == true) { logicparse = true; break; } } } else { logicparse = false; } //Run code if logic if met at column and row level if (logicparse == true) { int paramCount = 1; foreach (var param in item.Parameter) { string jparameters = Newtonsoft.Json.JsonConvert.SerializeObject(param); logger.Debug("Column Name(" + group.ID + ") - " + group.Name + " || Row Name(" + item.ID +") - " + item.Name); if (item.Function == "Maths") { Maths Maths = new Maths(); Maths parameters = (Maths)javaScriptSerializer.Deserialize(jparameters, typeof(Maths)); MathString = Maths.Output(jparameters,jCategory,group.ID,item.ID,MathString,PowOpen); PowOpen = Maths.PowOpen(jparameters, PowOpen); if (paramCount == item.Parameter.Count) { Expression e = new Expression(MathString); var Calculation = e.Evaluate(); bool DeciParse; decimal CalculationDeci; string Rounding; DeciParse = decimal.TryParse(Convert.ToString(Calculation), out CalculationDeci); Rounding = Convert.ToString(parameters.Rounding); if (Rounding == null || Rounding == "") { Rounding = "2"; } if (DeciParse == true) { decimal Output = CalculationDeci; MathematicalFunctions MathematicalFunctions = new MathematicalFunctions(); try { Output = MathematicalFunctions.Rounding(Convert.ToString(parameters.RoundingType), Rounding, Output); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } item.Output = Convert.ToString(Output); } else { item.Output = "0"; } } paramCount = paramCount + 1; } else if (item.Function == "ErrorsWarnings") { ErrorsWarnings Errors = new ErrorsWarnings(); ErrorsWarnings parameters = (ErrorsWarnings)javaScriptSerializer.Deserialize(jparameters, typeof(ErrorsWarnings)); item.Name = parameters.Type; item.Output = parameters.String1; } else if (item.Function == "Comments") { Comments Errors = new Comments(); Comments parameters = (Comments)javaScriptSerializer.Deserialize(jparameters, typeof(Comments)); item.Output = parameters.String1; } else if (item.Function == "Period") { DateFunctions DateFunctions = new DateFunctions(); Period Periods = new Period(); try { item.Output = Periods.Output(jparameters, jCategory, group.ID, item.ID); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } } else if (item.Function == "Factors") { Factors Factors = new Factors(); Factors parameters = (Factors)javaScriptSerializer.Deserialize(jparameters, typeof(Factors)); try { item.Output = Factors.Output(jparameters, jCategory, group.ID, item.ID); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } item.Type = parameters.OutputType; } else if (item.Function == "DateAdjustment") { Dates Dates = new Dates(); try { item.Output = Dates.Output(jparameters, jCategory, group.ID, item.ID); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } } else if (item.Function == "DatePart") { DatePart DateParts = new DatePart(); try { item.Output = DateParts.Output(jparameters, jCategory, group.ID, item.ID); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } } else if (item.Function == "MathsFunctions") { MathsFunctions MathsFunctions = new MathsFunctions(); try { item.Output = MathsFunctions.Output(jparameters, jCategory, group.ID, item.ID); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } } else if (item.Function == "ArrayFunctions") { ArrayFunctions ArrayFunctions = new ArrayFunctions(); ArrayFunctions parameters = (ArrayFunctions)javaScriptSerializer.Deserialize(jparameters, typeof(ArrayFunctions)); try { item.Output = ArrayFunctions.Output(jparameters, jCategory, group.ID, item.ID); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } if(parameters.Function == "Count") { item.Type = "Decimal"; } else { item.Type = parameters.LookupType; } } else if (item.Function == "StringFunctions") { StringFunctions StringFunctions = new StringFunctions(); StringFunctions parameters = (StringFunctions)javaScriptSerializer.Deserialize(jparameters, typeof(StringFunctions)); try { item.Output = StringFunctions.Output(jparameters, jCategory, group.ID, item.ID); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } if(parameters.Type == "Len") { item.Type = "Decimal"; } else { item.Type = "String"; } } } //Expected results on the builder this sets the required ones if (item.ExpectedResult == null || item.ExpectedResult == "") { item.Pass = "******"; } else if (item.ExpectedResult == item.Output) { item.Pass = "******"; } else { item.Pass = "******"; } OutputList.Add(new OutputList { ID = Convert.ToString(item.ID), Field = item.Name, Value = item.Output, Group = group.Name }); } else { //Ignores the row if logic is not met dynamic LogicReplace = Config.VariableReplace(jCategory, item.Name, group.ID, item.ID); if(Convert.ToString(LogicReplace) == Convert.ToString(item.Name)) { item.Output = null; } else { item.Output = Convert.ToString(LogicReplace); } item.Pass = "******"; OutputList.Add(new OutputList { ID = Convert.ToString(item.ID), Field = item.Name, Value = item.Output, Group = group.Name }); } } } } } }
//Calcuation Controller Action public void CalculateAction(List <CategoryViewModel> jCategory) { foreach (var group in jCategory) { foreach (var item in group.Functions) { if (item.Function == "Input") { item.Output = InputFunctions.Output(item.Type, item.Output); OutputList.Add(new OutputList { ID = Convert.ToString(item.ID), Field = item.Name, Value = item.Output, Group = group.Name }); } else { //Logic check at Column Level string colLogic = null; bool colLogicParse = true; if (group.Logic != null) { foreach (var bit in group.Logic) { var grouplastLogic = group.Logic.Last(); string grouplastLogicOperator = grouplastLogic.Operator; Logic Logic = new Logic(); colLogic = Logic.Output(jCategory, bit, group.ID, 0); Expression ex = new Expression(colLogic); try { colLogicParse = Convert.ToBoolean(ex.Evaluate()); } catch (Exception exception) { logger.Error(exception); throw new HttpException(exception.ToString()); } if (grouplastLogicOperator == "AND" && colLogicParse == false) { break; } else if (grouplastLogicOperator == "OR" && colLogicParse == true) { colLogicParse = true; break; } } } if (item.Parameter.Count > 0) { string logic = null; bool logicparse = true; string MathString = null; bool PowOpen = false; //Logic check at column level if (colLogicParse == true) { foreach (var bit in item.Logic) { var lastLogic = item.Logic.Last(); string lastLogicOperator = lastLogic.Operator; Logic Logic = new Logic(); logic = Logic.Output(jCategory, bit, group.ID, item.ID); Expression ex = new Expression(logic); try { logicparse = Convert.ToBoolean(ex.Evaluate()); } catch (Exception exception) { logger.Error(exception); throw new HttpException(exception.ToString()); } if (lastLogicOperator == "AND" && logicparse == false) { break; } else if (lastLogicOperator == "OR" && logicparse == true) { logicparse = true; break; } } } else { logicparse = false; } //Run code if logic if met at column and row level if (logicparse == true) { int paramCount = 1; foreach (var param in item.Parameter) { string jparameters = Newtonsoft.Json.JsonConvert.SerializeObject(param); logger.Debug("Column Name(" + group.ID + ") - " + group.Name + " || Row Name(" + item.ID + ") - " + item.Name); if (item.Function == "Maths") { Maths Maths = new Maths(); Maths parameters = (Maths)javaScriptSerializer.Deserialize(jparameters, typeof(Maths)); MathString = Maths.Output(jparameters, jCategory, group.ID, item.ID, MathString, PowOpen); PowOpen = Maths.PowOpen(jparameters, PowOpen); if (paramCount == item.Parameter.Count) { Expression e = new Expression(MathString); var Calculation = e.Evaluate(); bool DeciParse; decimal CalculationDeci; string Rounding; DeciParse = decimal.TryParse(Convert.ToString(Calculation), out CalculationDeci); Rounding = Convert.ToString(parameters.Rounding); if (Rounding == null || Rounding == "") { Rounding = "2"; } if (DeciParse == true) { decimal Output = CalculationDeci; MathematicalFunctions MathematicalFunctions = new MathematicalFunctions(); try { Output = MathematicalFunctions.Rounding(Convert.ToString(parameters.RoundingType), Rounding, Output); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } item.Output = Convert.ToString(Output); } else { item.Output = "0"; } } paramCount = paramCount + 1; } else if (item.Function == "ErrorsWarnings") { ErrorsWarnings Errors = new ErrorsWarnings(); ErrorsWarnings parameters = (ErrorsWarnings)javaScriptSerializer.Deserialize(jparameters, typeof(ErrorsWarnings)); item.Name = parameters.Type; item.Output = parameters.String1; } else if (item.Function == "Comments") { Comments Errors = new Comments(); Comments parameters = (Comments)javaScriptSerializer.Deserialize(jparameters, typeof(Comments)); item.Output = parameters.String1; } else if (item.Function == "Period") { DateFunctions DateFunctions = new DateFunctions(); Period Periods = new Period(); try { item.Output = Periods.Output(jparameters, jCategory, group.ID, item.ID); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } } else if (item.Function == "Factors") { Factors Factors = new Factors(); Factors parameters = (Factors)javaScriptSerializer.Deserialize(jparameters, typeof(Factors)); try { item.Output = Factors.Output(jparameters, jCategory, group.ID, item.ID); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } item.Type = parameters.OutputType; } else if (item.Function == "DateAdjustment") { Dates Dates = new Dates(); try { item.Output = Dates.Output(jparameters, jCategory, group.ID, item.ID); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } } else if (item.Function == "DatePart") { DatePart DateParts = new DatePart(); try { item.Output = DateParts.Output(jparameters, jCategory, group.ID, item.ID); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } } else if (item.Function == "Return") { Return Return = new Return(); try { item.Output = Return.Output(jparameters, jCategory, group.ID, item.ID); OutputList.Add(new OutputList { ID = Convert.ToString(item.ID), Field = item.Name, Value = item.Output, Group = group.Name }); return; } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } } else if (item.Function == "MathsFunctions") { MathsFunctions MathsFunctions = new MathsFunctions(); try { item.Output = MathsFunctions.Output(jparameters, jCategory, group.ID, item.ID, group); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } } else if (item.Function == "ArrayFunctions") { ArrayFunctions ArrayFunctions = new ArrayFunctions(); ArrayFunctions parameters = (ArrayFunctions)javaScriptSerializer.Deserialize(jparameters, typeof(ArrayFunctions)); try { item.Output = ArrayFunctions.Output(jparameters, jCategory, group.ID, item.ID); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } if (parameters.Function == "Count") { item.Type = "Decimal"; } else { item.Type = parameters.LookupType; } } else if (item.Function == "StringFunctions") { StringFunctions StringFunctions = new StringFunctions(); StringFunctions parameters = (StringFunctions)javaScriptSerializer.Deserialize(jparameters, typeof(StringFunctions)); try { item.Output = StringFunctions.Output(jparameters, jCategory, group.ID, item.ID); } catch (Exception ex) { logger.Error(ex); throw new HttpException(ex.ToString()); } if (parameters.Type == "Len") { item.Type = "Decimal"; } else { item.Type = "String"; } } else if (item.Function == "Function") { Function Functions = new Function(); Function parameters = (Function)javaScriptSerializer.Deserialize(jparameters, typeof(Function)); FunctionConfiguration calcFunction = db.FunctionConfiguration.Find(Convert.ToInt32(parameters.ID)); List <CategoryViewModel> calcFunctionConfig = (List <CategoryViewModel>)javaScriptSerializer.Deserialize(calcFunction.Configuration, typeof(List <CategoryViewModel>)); //replace inputs from main configuration to function foreach (var row in calcFunctionConfig[0].Functions) { int index = parameters.Input.FindIndex(a => a.Name == row.Name); if (index >= 0) { row.Output = parameters.Input[index].Output; } else { row.Output = null; } } foreach (var input in calcFunctionConfig[0].Functions) { if (input.Output != null) { input.Output = Convert.ToString(Functions.Output(jparameters, jCategory, group.ID, item.ID, input.Output, input.Type)); } } //run the function with the new inputs Calculate Calculate = new Calculate(); calcFunctionConfig = Calculate.DebugResults(calcFunctionConfig); item.SubOutput = Calculate.OutputResults(calcFunctionConfig); foreach (var col in calcFunctionConfig) { int index = col.Functions.FindIndex(a => a.Function == "Return"); if (index >= 0) { item.Output = col.Functions[index].Output; foreach (var thing in col.Functions[index].Parameter) { foreach (var test in thing) { if (test.Key == "Datatype") { item.Type = test.Value; } } } } } } } //Expected results on the builder this sets the required ones if (item.ExpectedResult == null || item.ExpectedResult == "") { item.Pass = "******"; } else if (item.ExpectedResult == item.Output) { item.Pass = "******"; } else { item.Pass = "******"; } OutputList.Add(new OutputList { ID = Convert.ToString(item.ID), Field = item.Name, Value = item.Output, Group = group.Name, SubOutput = item.SubOutput }); } else { //Ignores the row if logic is not met dynamic LogicReplace = Config.VariableReplace(jCategory, item.Name, group.ID, item.ID); if (Convert.ToString(LogicReplace) == Convert.ToString(item.Name)) { item.Output = null; } else { item.Output = Convert.ToString(LogicReplace); } item.Pass = "******"; OutputList.Add(new OutputList { ID = Convert.ToString(item.ID), Field = item.Name, Value = item.Output, Group = group.Name, SubOutput = item.SubOutput }); } } } } } }