private void FillData()
        {
            try
            {
                Loans loan = new Loans();
                LoansBLL bll = new LoansBLL();
                loan = bll.GetLoanByID(Convert.ToInt16(Session["EditLoanID"]));
                ddlAddLoanCode.SelectedValue = loan.CodeName;
                txtBoxAddLoanCode.Text = loan.CodeName;
                ddlBorrower.SelectedValue = loan.Borrower;
                //  txtBoxAddSector.Text = loan.Sector;
                ddlSector.SelectedValue = loan.Sector;
                if (loan.Signing_Date != null)
                    txtBoxAddSigningDate.SelectedDate = Convert.ToDateTime(loan.Signing_Date);
                if (loan.Maturity_Date != null)
                    txtBoxAddMaturityDate.SelectedDate = Convert.ToDateTime(loan.Maturity_Date);
                ddlAddFixedOrFloating.SelectedValue = loan.FixedOrFloating;

                txtBoxAddMargin.Text = loan.Margin;
                ddlAddCurrency.SelectedValue = loan.Currency;

                bindCountry();
                if (loan.Country != null)
                {
                    CountryBL countryBL = new CountryBL();
                    int id = countryBL.GetCountryFromName(loan.Country);
                    if (id != 0)
                        ddlCountry.SelectedValue = Convert.ToString(id);
                }
                ddlAddCouponFrequency.SelectedValue = loan.CouponFrequency;

                txtBoxFacilitySize.Text = Convert.ToDecimal(loan.FacilitySize).ToString("N");
                if (loan.Bilateral == true)
                    ddlAddBilateral.SelectedValue = "Yes";
                else
                    ddlAddBilateral.SelectedValue = "No";
                ddlAmortizing.SelectedValue = loan.Amortizing;
                CheckNodes(tvCreditRating, loan);
                //txtCouponDate.
                //txtNotional
                txtPP.Text = loan.PP;
                //     txtCRating.Text = loan.CreditRating;
                txtStructureID.Text = loan.StructureID;
                txtBoxAddMargin.Text = loan.Margin;
                if (loan.CouponDate != null)
                    txtCouponDate.SelectedDate = Convert.ToDateTime(loan.CouponDate);
                txtNotional.Text = Convert.ToDecimal(loan.Notional).ToString("N");
                if (ddlAmortizing.SelectedValue == "Yes")
                {
                    if (loan.AmortisationsStartPoint != null)
                        txtAmortisationsStartDate.SelectedDate = Convert.ToDateTime(loan.AmortisationsStartPoint);
                    txtAmortisations.Text = loan.NoOfAmortisationPoint.ToString();
                    trDate.Visible = true;
                    trNo.Visible = true;
                    tblAmortisation.Visible = true;
                }
                txtSummitCredit.Text = loan.SummitCreditEntity;
                txtGrid.Text = loan.Grid;
                txtGurantor.Text = loan.Gurantor;
                LoanScheduleBL scheduleBL = new LoanScheduleBL();
                List<LoanSchedule> scheduleList = scheduleBL.GetLoanByID(loan.ID);

                if (scheduleList != null && scheduleList.Count > 0)
                {
                    dt1.Clear();
                    //dt1.Columns.Add("ID", typeof(int));
                    //dt1.Columns.Add("StartDate", typeof(DateTime));
                    //dt1.Columns.Add("EndDate", typeof(DateTime));
                    //dt1.Columns.Add("Notional", typeof(long));
                    //dt1.Columns.Add("Factor", typeof(float));

                    for (int i = 0; i < scheduleList.Count; i++)
                    {
                        //  dt1.Rows.Add(scheduleList[i].ID, scheduleList[i].StartDate, scheduleList[i].EndDate, scheduleList[i].Notation, scheduleList[i].Factor);
                        dt1.Rows.Add(i + 1, scheduleList[i].StartDate.Value.ToShortDateString(), scheduleList[i].EndDate.Value.ToShortDateString(), scheduleList[i].CoupFrac, scheduleList[i].Notation, scheduleList[i].Amortisation, scheduleList[i].Factor);
                    }
                    pnlAmortizing.Visible = true;
                    grdAmortizing.Visible = true;
                    btnCalculatSchedule.Visible = true;
                    grdAmortizing.DataSource = dt1;
                    grdAmortizing.DataBind();
                }
                hfLoanID.Value = loan.ID.ToString();
                lblConfirm.Text = "Loan already exists, do you wish to overwrite?";
                _operation = "Edit";
            }
            catch (Exception)
            {
            }
        }
 private List<LoanSchedule> GetLoanBySchedule(int loanID)
 {
     LoanScheduleBL loanScheduleBL = new LoanScheduleBL();
     return loanScheduleBL.GetLoanByID(loanID);
 }
        private string CalculateIRR(string loanName, string marketPrice)
        {
            LoansBLL bll = new LoansBLL();
            Loans loan = bll.GetLoanByCode(loanName);
            int loanID = loan.ID;
            double notional = Convert.ToDouble(loan.Notional);
            LoanScheduleBL loanScheduleBL = new LoanScheduleBL();
            List<LoanSchedule> loanSchedule = new List<LoanSchedule>();
            loanSchedule = loanScheduleBL.GetLoanByID(loanID);
            double[] cashFlows;
            double[] days;
            List<DateTime> scheduleDateTime = new List<DateTime>();
            if (loanSchedule != null)
            {
                List<double> cashFlowDouble = new List<double>();
                double mktPrice = 0.99;
                if (marketPrice != string.Empty)
                {
                    mktPrice = Convert.ToDouble(marketPrice) / 100;
                }

                double dFirstCashFlow = (Convert.ToDouble(mktPrice) * Convert.ToDouble(notional)) * -1;
                scheduleDateTime.Add(loanSchedule.First().StartDate.Value);
                cashFlowDouble.Add(dFirstCashFlow);

                List<double> day = new List<double>();
                foreach (var item in loanSchedule)
                {
                    cashFlowDouble.Add(Convert.ToDouble(item.AmortisationInt));
                    day.Add(Convert.ToInt16(item.Days));
                    scheduleDateTime.Add(Convert.ToDateTime(item.EndDate));
                }
                cashFlows = cashFlowDouble.ToArray();

                days = day.ToArray();
            }
            else
            {
                cashFlows = new double[1] { 0.0 };
                days = new double[1] { 0.0 };

            }

            //Zainco.NewtonRaphson.IRRCalculator.Domain.ICalculator calculator = new NewtonRaphsonIRRCalc(cashFlows);
            //Double d = calculator.Execute();
            IRR irr = new IRR();

            //double xirr = irr.Newtons_method(0.1,
            //                           irr.total_f_xirr(cashFlows, days),
            //                           irr.total_df_xirr(cashFlows, days));

            double xirr = System.Numeric.Financial.XIrr(cashFlows, scheduleDateTime) * 100;
            return Convert.ToDecimal(xirr).ToString("0.00");
            // return d;
        }
 public List<LoanSchedule> BindLoanScheduleData()
 {
     LoanScheduleBL bll = new LoanScheduleBL();
     if (ddlLoanDetailsCode.SelectedValue == string.Empty)
     {
         BindLoanDetails();
     }
     List<LoanSchedule> lst = bll.GetLoanByID(Convert.ToInt16(ddlLoanDetailsCode.SelectedValue));
     return lst;
 }
 private void BindLoanDetailData(string loanCode)
 {
     LoanScheduleBL bll = new LoanScheduleBL();
     LoansBLL loanBL = new LoansBLL();
     if (loanCode != string.Empty)
     {
         Loans loan = loanBL.GetLoanByID(Convert.ToInt32(loanCode));
         txtLoanDetailCurrency.Text = loan.Currency;
         txtLoanDetailMaturityDate.Text = loan.Maturity_Date;
         txtLoanDetailTradeDate.SelectedDate = DateTime.Now;//loan.Signing_Date;
         txtLoanDetailSettlementDate.SelectedDate = AddBusinessDays(DateTime.Now.Date, 10);
         txtLoanDetailNotional.Text = Convert.ToDecimal(loan.Notional).ToString("N");
         hdnLoanDetailMargin.Value = loan.Margin.ToString();
         txtLoanDetailCouponFreq.Text = loan.CouponFrequency;
         txtLoanDetailLastCouponDate.Text = loan.CouponDate;
         txtLoanDetailFixedOrFloating.Text = loan.FixedOrFloating;
         txtLoanDetailIRR.Text = CalculateIRR(loan.CodeName, txtLoanDetailPrice.Text);
         txtLoanDetailAvgLife.Text = AverageLife(loan.CodeName);
         txtAvgLifeNonDisc.Text = AverageLifeNonDiscount(loan.CodeName);
         txtLoanDetailAvgLifeRiskDisc.Text = AverageLifeRiskDisc(loan.CodeName);
         txtLoanDetailAvgLifeDisc.Text = AverageLifeDisc(loan.CodeName);
         if (txtLoanDetailPrice.Text != string.Empty)
             txtLoanDetailConsideration.Text = Convert.ToDecimal(Convert.ToDecimal(txtLoanDetailPrice.Text) * Convert.ToDecimal(loan.Notional)).ToString("0.00");
         else
             txtLoanDetailConsideration.Text = Convert.ToDecimal(1 * Convert.ToDecimal(loan.Notional)).ToString("0.00");
         //txtLoanDetailDiscountMargin.Text = loan.di
         grdLoanDetail.DataSource = bll.GetLoanByID(Convert.ToInt16(loanCode));
         grdLoanDetail.DataBind();
     }
 }
        //private void BindLoanCodeDetails()
        //{
        //    try
        //    {
        //        LoansBLL bll = new LoansBLL();
        //        string strLoanName = ddlAddLoanCode.Text.ToString();
        //        if (strLoanName.Contains(';'))
        //        {
        //            strLoanName = strLoanName.Replace(';', ' ');
        //        }
        //        Loans loan = bll.GetLoanByCode(strLoanName.Trim());
        //        if (loan != null)
        //        {
        //            txtBoxAddBorrower.Text = Convert.ToString(loan.Borrower);
        //            txtBoxAddSector.Text = Convert.ToString(loan.Sector);
        //            if (loan.Signing_Date != "")
        //                txtBoxAddSigningDate.SelectedDate = Convert.ToDateTime(loan.Signing_Date);
        //            else
        //                txtBoxAddSigningDate.Clear();
        //            if (loan.Maturity_Date != "")
        //                txtBoxAddMaturityDate.SelectedDate = Convert.ToDateTime(loan.Maturity_Date);
        //            else
        //                txtBoxAddMaturityDate.Clear();
        //            txtBoxFacilitySize.Text = loan.FacilitySize;
        //            ddlAddFixedOrFloating.SelectedValue = loan.FixedOrFloating;
        //            txtBoxAddMargin.Text = loan.Margin;
        //            ddlAddCurrency.SelectedValue = loan.Currency;
        //            ddlCountry.SelectedValue = loan.Country;
        //            ddlAddCouponFrequency.SelectedValue = loan.CouponFrequency;
        //            if (loan.Bilateral == true)
        //                ddlAddBilateral.SelectedValue = "Yes";
        //            else
        //                ddlAddBilateral.SelectedValue = "No";
        //            ddlAmortizing.SelectedValue = loan.Amortizing;
        //            lblConfirm.Text = "Loan already exists, do you wish to overwrite?";
        //            _operation = "Edit";
        //        }
        //        else
        //        {
        //            _operation = "Add";
        //        }
        //        //else
        //        //{
        //        //    Clear();
        //        //}
        //    }
        //    catch (Exception)
        //    {
        //    }
        //}
        private void BindLoanCodeDetails()
        {
            try
            {
                LoansBLL bll = new LoansBLL();
                string strLoanName = ddlAddLoanCode.Text.ToString();
                if (strLoanName.Contains(';'))
                {
                    strLoanName = strLoanName.Replace(';', ' ');
                }
                Loans loan = bll.GetLoanByCode(strLoanName.Trim());
                if (loan != null)
                {
                    if (loan.Borrower != null)
                    {
                        ddlBorrower.SelectedValue = Convert.ToString(loan.Borrower);
                        BorrowersBL borrowerBL = new BorrowersBL();
                        if (ddlBorrower.SelectedValue != string.Empty)
                        {
                            if (ddlBorrower.SelectedItem != null)
                            {
                                Borrower borrower = borrowerBL.GetBorrower(ddlBorrower.SelectedItem.Text);
                                txtGrid.Text = loan.Grid;
                                txtSummitCredit.Text = loan.SummitCreditEntity;
                            }
                        }

                    }

                    //  txtBoxAddSector.Text = Convert.ToString(loan.Sector);
                    if (loan.Sector != null)
                    {
                        ddlSector.SelectedValue = Convert.ToString(loan.Sector);
                    }

                    if (loan.Signing_Date != "" && loan.Signing_Date != null)
                        txtBoxAddSigningDate.SelectedDate = Convert.ToDateTime(loan.Signing_Date);
                    else
                        txtBoxAddSigningDate.Clear();
                    if (loan.Maturity_Date != "" && loan.Maturity_Date != null)
                        txtBoxAddMaturityDate.SelectedDate = Convert.ToDateTime(loan.Maturity_Date);
                    else
                        txtBoxAddMaturityDate.Clear();
                    if (loan.CouponDate != "" && loan.CouponDate != null)
                        txtCouponDate.SelectedDate = Convert.ToDateTime(loan.CouponDate);
                    else
                        txtCouponDate.Clear();
                    if (loan.FacilitySize != null)
                    {
                        txtBoxFacilitySize.Text = Convert.ToDecimal(loan.FacilitySize).ToString("N");

                    }
                    if (loan.Fixed_Floating != null)
                    {
                        ddlAddFixedOrFloating.SelectedValue = loan.FixedOrFloating;
                    }

                    txtBoxAddMargin.Text = loan.Margin;
                    if (loan.Currency != null)
                    {
                        ddlAddCurrency.SelectedValue = loan.Currency;
                    }
                    if (loan.Country != null)
                    {
                        ddlCountry.SelectedValue = loan.Country;
                    }

                    txtPP.Text = loan.PP;
                    //  txtCRating.Text = loan.CreditRating;
                    txtStructureID.Text = loan.StructureID;
                    if (loan.CouponFrequency != null)
                    {
                        ddlAddCouponFrequency.SelectedValue = loan.CouponFrequency;
                    }

                    txtNotional.Text = Convert.ToDecimal(loan.Notional).ToString("N");
                    if (loan.Bilateral == true)
                        ddlAddBilateral.SelectedValue = "Yes";
                    else
                        ddlAddBilateral.SelectedValue = "No";
                    if (loan.Amortizing == "Yes")
                    {
                        ddlAmortizing.SelectedValue = loan.Amortizing;
                        if (ddlAmortizing.SelectedValue == "Yes")
                        {
                            if (loan.AmortisationsStartPoint != null)
                                txtAmortisationsStartDate.SelectedDate = Convert.ToDateTime(loan.AmortisationsStartPoint);
                            txtAmortisations.Text = loan.NoOfAmortisationPoint.ToString();
                            trDate.Visible = true;
                            trNo.Visible = true;
                            tblAmortisation.Visible = true;
                        }
                    }
                    else
                    {
                        ddlAmortizing.SelectedValue = "No";
                        trDate.Visible = false;
                        trNo.Visible = false;
                        tblAmortisation.Visible = false;
                    }
                    txtGurantor.Text = loan.Gurantor;
                    txtGrid.Text = loan.Grid;
                    txtSummitCredit.Text = loan.SummitCreditEntity;

                    CheckNodes(tvCreditRating, loan);

                    txtBoxAddMargin.Text = loan.Margin;

                    LoanScheduleBL scheduleBL = new LoanScheduleBL();
                    List<LoanSchedule> scheduleList = scheduleBL.GetLoanByID(loan.ID);

                    if (scheduleList != null && scheduleList.Count > 0)
                    {
                        dt1.Clear();
                        //dt1.Columns.Add("ID", typeof(int));
                        //dt1.Columns.Add("StartDate", typeof(DateTime));
                        //dt1.Columns.Add("EndDate", typeof(DateTime));
                        //dt1.Columns.Add("Notional", typeof(long));
                        //dt1.Columns.Add("Factor", typeof(float));

                        for (int i = 0; i < scheduleList.Count; i++)
                        {
                            // dt1.Rows.Add(scheduleList[i].ID, scheduleList[i].StartDate, scheduleList[i].EndDate, scheduleList[i].Notation, scheduleList[i].Factor);
                            dt1.Rows.Add(i + 1, scheduleList[i].StartDate.Value.ToShortDateString(), scheduleList[i].EndDate.Value.ToShortDateString(), scheduleList[i].CoupFrac, scheduleList[i].Notation, scheduleList[i].Amortisation, scheduleList[i].Factor, scheduleList[i].Spread, scheduleList[i].CouponPaymentDate, scheduleList[i].RiskFreeDP1, scheduleList[i].RiskFreeDP2, scheduleList[i].FloatingRate, scheduleList[i].AllInRate, scheduleList[i].Interest, scheduleList[i].Days, scheduleList[i].AmortisationInt);
                        }
                        pnlAmortizing.Visible = true;
                        grdAmortizing.Visible = true;
                        btnCalculatSchedule.Visible = true;
                        Session.Add("LoanScheduleData", dt1);
                        grdAmortizing.DataSource = dt1;
                        grdAmortizing.DataBind();
                    }
                    else
                    {
                        Session.Remove("LoanScheduleData");
                        dt1.Clear();
                        pnlAmortizing.Visible = false;
                        btnCalculatSchedule.Visible = false;
                        grdAmortizing.Visible = false;
                    }
                    hfLoanID.Value = loan.ID.ToString();
                    Session.Add("EditLoanID", loan.ID.ToString());
                    lblConfirm.Text = "Loan already exists, do you wish to overwrite?";
                    _operation = "Edit";
                }
                else
                {
                    hfLoanID.Value = string.Empty;
                    _operation = "Add";
                    lblConfirm.Text = "Are you sure you want to add this loan to database?";
                    // Clear();
                }
            }
            catch (Exception)
            {

            }
        }
        protected DataTable GetCompactChartData(string loanName)
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("ID");
                dt.Columns.Add("EndDate");
                dt.Columns.Add("Notation");

                LoansBLL loanBL = new LoansBLL();
                int loanID = loanBL.GetLoanByCode(loanName).ID;
                LoanScheduleBL loanScheduleBL = new LoanScheduleBL();
                List<LoanSchedule> loanSchedule = loanScheduleBL.GetLoanByID(loanID);

                if (loanSchedule != null)
                {
                    for (int i = 0; i < loanSchedule.Count; i++)
                    {
                        dt.Rows.Add((i + 1), new DateTime(loanSchedule[i].EndDate.Value.Year, loanSchedule[i].EndDate.Value.Month, loanSchedule[i].EndDate.Value.Day), loanSchedule[i].Notation.ToString());
                    }
                }
                return dt;
            }
            catch (Exception)
            {
                return null;
            }
        }