public ActionResult CreateManualCapitalCall(FormCollection collection)
        {
            CreateCapitalCallModel model=new CreateCapitalCallModel();
            ResultModel resultModel=new ResultModel();
            List<InvestorFund> investorFunds=new List<InvestorFund>();
            this.TryUpdateModel(model,collection);
            if(ModelState.IsValid) {

                // Attempt to create manual capital call.

                Models.Entity.CapitalCall capitalCall=new Models.Entity.CapitalCall();
                CapitalCallLineItem item;

                capitalCall.CapitalAmountCalled=model.CapitalAmountCalled;
                capitalCall.CapitalCallDate=model.CapitalCallDate;
                capitalCall.CapitalCallDueDate=model.CapitalCallDueDate;
                capitalCall.CapitalCallTypeID=(int)Models.CapitalCall.Enums.CapitalCallType.Manual;
                capitalCall.CreatedBy=Authentication.CurrentUser.UserID;
                capitalCall.CreatedDate=DateTime.Now;
                capitalCall.LastUpdatedBy=Authentication.CurrentUser.UserID;
                capitalCall.LastUpdatedDate=DateTime.Now;
                capitalCall.ExistingInvestmentAmount=model.ExistingInvestmentAmount??0;
                capitalCall.NewInvestmentAmount=model.NewInvestmentAmount;
                capitalCall.FundID=model.FundId;
                capitalCall.InvestmentAmount=model.InvestedAmount??0;
                capitalCall.InvestedAmountInterest=model.InvestedAmountInterest??0;
                capitalCall.FundExpenses=model.FundExpenses??0;
                capitalCall.ManagementFees=model.ManagementFees??0;
                capitalCall.ManagementFeeInterest=model.ManagementFeeInterest??0;
                capitalCall.CapitalCallNumber=Convert.ToString(CapitalCallRepository.FindCapitalCallNumber(model.FundId));
                int index;
                for(index=1;index<model.InvestorCount+1;index++) {
                    item=new CapitalCallLineItem();
                    item.CreatedBy=Authentication.CurrentUser.UserID;
                    item.CreatedDate=DateTime.Now;
                    item.LastUpdatedBy=Authentication.CurrentUser.UserID;
                    item.LastUpdatedDate=DateTime.Now;
                    item.CapitalAmountCalled=DataTypeHelper.ToDecimal(collection[index.ToString()+"_"+"CapitalAmountCalled"]);
                    item.ManagementFeeInterest=DataTypeHelper.ToDecimal(collection[index.ToString()+"_"+"ManagementFeeInterest"]);
                    item.InvestedAmountInterest=DataTypeHelper.ToDecimal(collection[index.ToString()+"_"+"InvestedAmountInterest"]);
                    item.ManagementFees=DataTypeHelper.ToDecimal(collection[index.ToString()+"_"+"ManagementFees"]);
                    item.FundExpenses=DataTypeHelper.ToDecimal(collection[index.ToString()+"_"+"FundExpenses"]);
                    item.InvestorID=DataTypeHelper.ToInt32(collection[index.ToString()+"_"+"InvestorId"]);
                    if(item.InvestorID>0) {
                        InvestorFund investorFund=InvestorRepository.FindInvestorFund(item.InvestorID,capitalCall.FundID);
                        if(investorFund!=null) {
                            // Reduce investor unfunded amount = investor unfunded amount – capital call amount for investor.
                            investorFund.UnfundedAmount=investorFund.UnfundedAmount-item.CapitalAmountCalled;
                            investorFunds.Add(investorFund);
                        }
                        capitalCall.CapitalCallLineItems.Add(item);
                    }
                }
                if(capitalCall.CapitalCallLineItems.Count==0) {
                    ModelState.AddModelError("InvestorCount","Select any one investor");
                } else {
                    IEnumerable<ErrorInfo> errorInfo=CapitalCallRepository.SaveCapitalCall(capitalCall);
                    if(errorInfo!=null) {
                        resultModel.Result+=ValidationHelper.GetErrorInfo(errorInfo);
                    } else {
                        foreach(var investorFund in investorFunds) {
                            errorInfo=InvestorRepository.SaveInvestorFund(investorFund);
                            resultModel.Result+=ValidationHelper.GetErrorInfo(errorInfo);
                        }
                    }
                    if(string.IsNullOrEmpty(resultModel.Result)) {
                        resultModel.Result+="True||"+(CapitalCallRepository.FindCapitalCallNumber(model.FundId));
                    }
                }
            }
            if(ModelState.IsValid==false) {
                foreach(var values in ModelState.Values.ToList()) {
                    foreach(var err in values.Errors.ToList()) {
                        if(string.IsNullOrEmpty(err.ErrorMessage)==false) {
                            resultModel.Result+=err.ErrorMessage+"\n";
                        }
                    }
                }
            }
            return View("Result",resultModel);
        }
 //
 // GET: /CapitalCall/New
 public ActionResult New()
 {
     ViewData["MenuName"]="FundManagement";
     ViewData["SubmenuName"]="CapitalCall";
     ViewData["PageName"]="CapitalCall";
     CreateCapitalCallModel model=new CreateCapitalCallModel();
     Models.Fund.FundDetail fundDetail=FundRepository.FindLastFundDetail();
     if(fundDetail!=null) {
         model.FundId=fundDetail.FundId;
     }
     return View(model);
 }
        public ActionResult UpdateCapitalCall(FormCollection collection)
        {
            CreateCapitalCallModel model=new CreateCapitalCallModel();
            ResultModel resultModel=new ResultModel();
            IEnumerable<ErrorInfo> errorInfo=null;
            this.TryUpdateModel(model,collection);
            if((model.CapitalCallID??0)==0) {
                ModelState.AddModelError("CapitalCallID","CapitalCallID is required");
            }
            if((model.AddManagementFees??false)==true) {
                DateTime fromDate=(model.FromDate??Convert.ToDateTime("01/01/1900"));
                DateTime toDate=(model.ToDate??Convert.ToDateTime("01/01/1900"));
                if(fromDate.Year<=1900)
                    ModelState.AddModelError("FromDate","From Date is required");
                if(toDate.Year<=1900)
                    ModelState.AddModelError("ToDate","To Date is required");
                if(fromDate.Year>1900&&toDate.Year>1900) {
                    if(fromDate.Subtract(toDate).Days>=0) {
                        ModelState.AddModelError("ToDate","To Date must be greater than From Date");
                    }
                }
                if((model.ManagementFees??0)<=1) {
                    ModelState.AddModelError("ManagementFees","Fee Amount is required");
                }
            }
            if(ModelState.IsValid) {

                // Attempt to update capital call.

                Models.Entity.CapitalCall capitalCall=CapitalCallRepository.FindCapitalCall((model.CapitalCallID??0));

                if(capitalCall!=null) {

                    capitalCall.CapitalAmountCalled=model.CapitalAmountCalled;
                    capitalCall.CapitalCallDate=model.CapitalCallDate;
                    capitalCall.CapitalCallDueDate=model.CapitalCallDueDate;
                    capitalCall.CapitalCallTypeID=(int)Models.CapitalCall.Enums.CapitalCallType.Reqular;
                    capitalCall.LastUpdatedBy=Authentication.CurrentUser.UserID;
                    capitalCall.LastUpdatedDate=DateTime.Now;
                    capitalCall.NewInvestmentAmount=model.NewInvestmentAmount;
                    capitalCall.FundID=model.FundId;

                    if((model.AddFundExpenses??false)==true) {
                        capitalCall.FundExpenses=model.FundExpenseAmount??0;
                    } else {
                        capitalCall.FundExpenses=0;
                    }

                    if((model.AddManagementFees??false)==true) {
                        capitalCall.ManagementFeeStartDate=model.FromDate;
                        capitalCall.ManagementFeeEndDate=model.ToDate;
                        capitalCall.ManagementFees=model.ManagementFees;
                    } else {
                        capitalCall.ManagementFees=0;
                    }

                    // Check new investment amount and existing investment amount.

                    decimal investmentAmount=(capitalCall.NewInvestmentAmount??0)+(capitalCall.ExistingInvestmentAmount??0);
                    decimal capitalAmount=(capitalCall.CapitalAmountCalled)-(capitalCall.ManagementFees??0)-(capitalCall.FundExpenses??0);

                    if(((decimal.Round(investmentAmount)==decimal.Round(capitalAmount))==false)) {
                        ModelState.AddModelError("NewInvestmentAmount","(New Investment Amount + Existing Investment Amount) should be equal to (Capital Amount - Management Fees - Fund Expenses).");
                    } else {

                        errorInfo=CapitalCallRepository.SaveCapitalCall(capitalCall);
                        resultModel.Result+=ValidationHelper.GetErrorInfo(errorInfo);

                        // Attempt to update capital call line item for each investor fund.
                        int rowIndex;
                        FormCollection rowCollection;
                        CapitalCallLineItemModel itemModel=null;

                        if(string.IsNullOrEmpty(resultModel.Result)) {
                            for(rowIndex=0;rowIndex<model.CapitalCallLineItemsCount;rowIndex++) {
                                rowCollection=FormCollectionHelper.GetFormCollection(collection,rowIndex,typeof(CapitalCallLineItemModel),"_");
                                itemModel=new CapitalCallLineItemModel();
                                this.TryUpdateModel(itemModel,rowCollection);
                                errorInfo=ValidationHelper.Validate(itemModel);
                                resultModel.Result+=ValidationHelper.GetErrorInfo(errorInfo);
                                if(string.IsNullOrEmpty(resultModel.Result)) {
                                    CapitalCallLineItem capitalCallLineItem=CapitalCallRepository.FindCapitalCallLineItem(itemModel.CapitalCallLineItemID);
                                    if(capitalCallLineItem!=null) {
                                        capitalCallLineItem.LastUpdatedBy=Authentication.CurrentUser.UserID;
                                        capitalCallLineItem.LastUpdatedDate=DateTime.Now;

                                        capitalCallLineItem.CapitalAmountCalled=itemModel.CapitalAmountCalled;
                                        capitalCallLineItem.FundExpenses=itemModel.FundExpenses;
                                        capitalCallLineItem.ManagementFees=itemModel.ManagementFees;
                                        capitalCallLineItem.NewInvestmentAmount=itemModel.NewInvestmentAmount;

                                        errorInfo=CapitalCallRepository.SaveCapitalCallLineItem(capitalCallLineItem);

                                        resultModel.Result+=ValidationHelper.GetErrorInfo(errorInfo);
                                    }
                                } else {
                                    break;
                                }
                            }

                        }

                        if(string.IsNullOrEmpty(resultModel.Result)) {
                            resultModel.Result+="True";
                        }
                    }
                }
            }
            if(ModelState.IsValid==false) {
                foreach(var values in ModelState.Values.ToList()) {
                    foreach(var err in values.Errors.ToList()) {
                        if(string.IsNullOrEmpty(err.ErrorMessage)==false) {
                            resultModel.Result+=err.ErrorMessage+"\n";
                        }
                    }
                }
            }
            return View("Result",resultModel);
        }
        public ActionResult Create(FormCollection collection)
        {
            CreateCapitalCallModel model=new CreateCapitalCallModel();
            ResultModel resultModel=new ResultModel();
            this.TryUpdateModel(model,collection);
            if((model.AddManagementFees??false)==true) {
                DateTime fromDate=(model.FromDate??Convert.ToDateTime("01/01/1900"));
                DateTime toDate=(model.ToDate??Convert.ToDateTime("01/01/1900"));
                if(fromDate.Year<=1900)
                    ModelState.AddModelError("FromDate","From Date is required");
                if(toDate.Year<=1900)
                    ModelState.AddModelError("ToDate","To Date is required");
                if(fromDate.Year>1900&&toDate.Year>1900) {
                    if(fromDate.Subtract(toDate).Days>=0) {
                        ModelState.AddModelError("ToDate","To Date must be greater than From Date");
                    }
                }
                if((model.ManagementFees??0)<=1) {
                    ModelState.AddModelError("ManagementFees","Fee Amount is required");
                }
            }
            if(ModelState.IsValid) {

                // Attempt to create capital call.

                Models.Entity.CapitalCall capitalCall=new Models.Entity.CapitalCall();
                CapitalCallLineItem item;

                capitalCall.CapitalAmountCalled=model.CapitalAmountCalled;
                capitalCall.CapitalCallDate=model.CapitalCallDate;
                capitalCall.CapitalCallDueDate=model.CapitalCallDueDate;
                capitalCall.CapitalCallNumber=string.Empty;
                capitalCall.CapitalCallTypeID=(int)Models.CapitalCall.Enums.CapitalCallType.Reqular;
                capitalCall.CreatedBy=Authentication.CurrentUser.UserID;
                capitalCall.CreatedDate=DateTime.Now;
                capitalCall.LastUpdatedBy=Authentication.CurrentUser.UserID;
                capitalCall.LastUpdatedDate=DateTime.Now;
                capitalCall.ExistingInvestmentAmount=model.ExistingInvestmentAmount??0;
                capitalCall.NewInvestmentAmount=model.NewInvestmentAmount;
                capitalCall.FundID=model.FundId;
                capitalCall.CapitalCallNumber=Convert.ToString(CapitalCallRepository.FindCapitalCallNumber(model.FundId));
                capitalCall.InvestmentAmount=(capitalCall.NewInvestmentAmount??0)+(capitalCall.ExistingInvestmentAmount??0);

                List<InvestorFund> investorFunds=CapitalCallRepository.GetAllInvestorFunds(capitalCall.FundID);

                if(investorFunds!=null) {

                    // Find non managing total commitment.
                    decimal nonManagingMemberTotalCommitment=investorFunds.Where(fund => fund.InvestorTypeID==(int)DeepBlue.Models.Investor.Enums.InvestorType.NonManagingMember).Sum(fund => fund.TotalCommitment);
                    // Find managing total commitment.
                    decimal managingMemberTotalCommitment=investorFunds.Where(fund => fund.InvestorTypeID==(int)DeepBlue.Models.Investor.Enums.InvestorType.ManagingMember).Sum(fund => fund.TotalCommitment);
                    // Calculate managing total commitment.
                    decimal totalCommitment=nonManagingMemberTotalCommitment+managingMemberTotalCommitment;

                    if((model.AddFundExpenses??false)==true) {
                        capitalCall.FundExpenses=model.FundExpenseAmount??0;
                    } else {
                        capitalCall.FundExpenses=0;
                    }

                    if((model.AddManagementFees??false)==true) {
                        capitalCall.ManagementFeeStartDate=model.FromDate;
                        capitalCall.ManagementFeeEndDate=model.ToDate;
                        capitalCall.ManagementFees=model.ManagementFees;
                    } else {
                        capitalCall.ManagementFees=0;
                    }

                    // Check new investment amount and existing investment amount.

                    decimal investmentAmount=(capitalCall.NewInvestmentAmount??0)+(capitalCall.ExistingInvestmentAmount??0);
                    decimal capitalAmount=capitalCall.CapitalAmountCalled-(capitalCall.ManagementFees??0)-(capitalCall.FundExpenses??0);

                    if(((decimal.Round(investmentAmount)==decimal.Round(capitalAmount))==false)) {
                        ModelState.AddModelError("NewInvestmentAmount","(New Investment Amount + Existing Investment Amount) should be equal to (Capital Amount - Management Fees - Fund Expenses).");
                    } else {
                        foreach(var investorFund in investorFunds) {

                            // Attempt to create capital call line item for each investor fund.

                            item=new CapitalCallLineItem();

                            item.CreatedBy=Authentication.CurrentUser.UserID;
                            item.CreatedDate=DateTime.Now;
                            item.LastUpdatedBy=Authentication.CurrentUser.UserID;
                            item.LastUpdatedDate=DateTime.Now;

                            // Calculate Management Fees investor fund

                            if((model.AddManagementFees??false)==true) {
                                if(investorFund.InvestorTypeID==(int)DeepBlue.Models.Investor.Enums.InvestorType.NonManagingMember) {
                                    item.ManagementFees=decimal.Multiply(
                                                            decimal.Divide(investorFund.TotalCommitment,nonManagingMemberTotalCommitment)
                                                            ,(capitalCall.ManagementFees??0));
                                }
                            }

                            // Calculate Fund Expense for investor fund

                            if((model.AddFundExpenses??false)==true) {
                                item.FundExpenses=decimal.Multiply(
                                                    decimal.Divide(investorFund.TotalCommitment,totalCommitment)
                                                    ,(capitalCall.FundExpenses??0));
                            }

                            // Calculate Capital Amount for investor fund

                            item.CapitalAmountCalled=CalculateCapitalAmountCalled(investorFund.TotalCommitment,
                                totalCommitment,
                                nonManagingMemberTotalCommitment,
                                capitalCall.CapitalAmountCalled,
                                (capitalCall.ManagementFees??0),
                                (DeepBlue.Models.Investor.Enums.InvestorType)investorFund.InvestorTypeID);

                            item.ExistingInvestmentAmount=(investorFund.TotalCommitment/totalCommitment)*capitalCall.ExistingInvestmentAmount;
                            item.NewInvestmentAmount=(investorFund.TotalCommitment/totalCommitment)*capitalCall.NewInvestmentAmount;
                            item.InvestmentAmount=(investorFund.TotalCommitment/totalCommitment)*capitalCall.InvestmentAmount;
                            item.InvestorID=investorFund.InvestorID;

                            // Reduce investor unfunded amount = investor unfunded amount - (capital call amount + management fees + fund expenses).

                            investorFund.UnfundedAmount=decimal.Subtract((investorFund.UnfundedAmount??0)
                                                                           ,
                                                                           decimal.Add(item.CapitalAmountCalled,
                                                                                       decimal.Add(
                                                                                                    (item.ManagementFees??0)
                                                                                                    ,(item.FundExpenses??0))
                                                                                                    )
                                                                           );

                            capitalCall.CapitalCallLineItems.Add(item);
                        }
                        IEnumerable<ErrorInfo> errorInfo=CapitalCallRepository.SaveCapitalCall(capitalCall);
                        if(errorInfo!=null) {
                            resultModel.Result+=ValidationHelper.GetErrorInfo(errorInfo);
                        } else {
                            foreach(var investorFund in investorFunds) {
                                errorInfo=InvestorRepository.SaveInvestorFund(investorFund);
                                resultModel.Result+=ValidationHelper.GetErrorInfo(errorInfo);
                            }
                        }
                        if(string.IsNullOrEmpty(resultModel.Result)) {
                            resultModel.Result+="True||"+(CapitalCallRepository.FindCapitalCallNumber(model.FundId))+"||"+capitalCall.CapitalCallID;
                        }
                    }
                }
            }
            if(ModelState.IsValid==false) {
                foreach(var values in ModelState.Values.ToList()) {
                    foreach(var err in values.Errors.ToList()) {
                        if(string.IsNullOrEmpty(err.ErrorMessage)==false) {
                            resultModel.Result+=err.ErrorMessage+"\n";
                        }
                    }
                }
            }
            return View("Result",resultModel);
        }