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 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)
            {
            }
        }
        protected void grdLoans_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                Session.Add("EditLoanID", id);
                Response.Redirect("default.aspx?page=addstaticloan");
            }
            else if (e.CommandName == "Delete")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                LoansBLL loansBll = new LoansBLL();
                string loanName = Convert.ToString(loansBll.GetLoanByID(id).CodeName);
                loansBll.RemoveLoan(id);

                DAL.Login login = Session["LogedInUser"] as DAL.Login;
                LoanHistory history = new LoanHistory();
                LoanHistoryBL historyBL = new LoanHistoryBL();
                history.Action = "Delete";
                history.LastModified = DateTime.Now;
                history.LoanName = loanName;
                history.UserName = login.Name;
                historyBL.SaveHistory(history);

                RadWindowManager1.RadAlert("Loan removed successfully", 330, 180, "realedge associates", "alertCallBackFn");
            }
            BindLoansData();
        }