private void Create()
        {
            newEntity = new LoanEntity();

            newEntity.Amount = Convert.ToDecimal(this.txtAmount.Text.Trim());
            newEntity.Period = Convert.ToInt32(txtPeriod.Text.Trim());
            newEntity.TermId = 0;
            newEntity.Date = DateTime.Now;
            newEntity.Interest = 6;
            newEntity.CollateralId = Convert.ToInt32(ddlCollateral.SelectedValue);
            newEntity.CollateralDetails = this.txtCollateralDetails.Text.Trim();
            newEntity.ComakerId = 0;
            newEntity.BorrowerId = this.userid;
            newEntity.InvestorId = 0;
            newEntity.Notes = this.txtNotes.Text.Trim();
            newEntity.Penalty = 0;
            newEntity.PenaltyDetails = string.Empty;
            newEntity.Status = 0;

            newService.SaveApplyLoan(ActionType.Create, newEntity);

            Response.Redirect("Default.aspx");
        }
        private void PopulateFields(int id)
        {
            newEntity = new LoanEntity();
            newEntity = newService.GetApplyLoan(id);

            txtPeriod.Text = newEntity.Period.ToString();
            //ddlLoanTerm.SelectedValue = newEntity.TermId.ToString();
            //this.txtDate.Text = newEntity.Date.ToString("yyyy-MM-dd");
            //this.txtInterest.Text = newEntity.Interest.ToString();
            ddlCollateral.SelectedValue = newEntity.CollateralId.ToString();
            this.txtCollateralDetails.Text = newEntity.CollateralDetails;
            //ddlBorrower.SelectedValue = newEntity.BorrowerId.ToString();
            this.txtNotes.Text = newEntity.Notes;
            this.txtAmount.Text = newEntity.Amount.ToString();
            //this.txtPenalty.Text = newEntity.Penalty.ToString();
            //this.txtPenaltyDetails.Text = newEntity.PenaltyDetails;

            //LoadComaker(newEntity.BorrowerId);
            //ddlComaker.SelectedValue = newEntity.ComakerId.ToString();

            //LoadInvestors(-1);
            //ddlInvestor.SelectedValue = newEntity.InvestorId.ToString();

            //ddlLoanTerm.Enabled = false;
            //ddlComaker.Enabled = false;
            //ddlBorrower.Enabled = false;
            //ddlInvestor.Enabled = false;
            //txtDate.Enabled = false;
            //txtInterest.Enabled = false;
            //this.txtAmount.Enabled = false;
            //txtPeriod.Enabled = false;
        }
        private void Update()
        {
            newEntity = new LoanEntity();
            newEntity.Amount = Convert.ToDecimal(this.txtAmount.Text.Trim());
            newEntity.Period = Convert.ToInt32(txtPeriod.Text.Trim());
            newEntity.ID = loanid;
            newEntity.CollateralId = Convert.ToInt32(ddlCollateral.SelectedValue);
            newEntity.CollateralDetails = this.txtCollateralDetails.Text.Trim();
            newEntity.ComakerId = 0;
            newEntity.Date = DateTime.Now; //set dummy value to prevent error
            newEntity.Notes = this.txtNotes.Text.Trim();
            newEntity.Penalty = 0;
            newEntity.PenaltyDetails = string.Empty;

            newService.SaveApplyLoan(ActionType.Update, newEntity);

            if (this.userid >= 0) { Response.Redirect("Default.aspx"); } else { Response.Redirect("AdminDefault.aspx"); }
        }
        private void Create()
        {
            newEntity = new LoanEntity();

            newEntity.Amount = Convert.ToDecimal(this.txtAmount.Text.Trim());
            newEntity.Period = Convert.ToInt32(txtPeriod.Text.Trim());
            newEntity.TermId = Convert.ToInt32(ddlLoanTerm.SelectedValue);
            newEntity.Date = Convert.ToDateTime(this.txtDate.Text.Trim());
            newEntity.Interest = Convert.ToDecimal(this.txtInterest.Text.Trim());
            newEntity.CollateralId = Convert.ToInt32(ddlCollateral.SelectedValue);
            newEntity.CollateralDetails = this.txtCollateralDetails.Text.Trim();
            newEntity.ComakerId = Convert.ToInt32(ddlComaker.SelectedValue);
            newEntity.BorrowerId = Convert.ToInt32(ddlBorrower.SelectedValue);
            newEntity.InvestorId = Convert.ToInt32(ddlInvestor.SelectedValue);
            newEntity.Notes = this.txtNotes.Text.Trim();
            newEntity.Penalty = string.IsNullOrEmpty(this.txtPenalty.Text) ? 0 : Convert.ToDecimal(this.txtPenalty.Text);
            newEntity.PenaltyDetails = this.txtPenaltyDetails.Text.Trim();
            newEntity.Status = 1;

            newService.Save(ActionType.Create, newEntity);

            Response.Redirect("ManageLoans.aspx");
        }
        private void Update()
        {
            newEntity = new LoanEntity();
            newEntity.ID = id;
            newEntity.CollateralId = Convert.ToInt32(ddlCollateral.SelectedValue);
            newEntity.CollateralDetails = this.txtCollateralDetails.Text.Trim();
            newEntity.ComakerId = Convert.ToInt32(ddlComaker.SelectedValue);
            newEntity.Date = DateTime.Now; //set dummy value to prevent error
            newEntity.Notes = this.txtNotes.Text.Trim();
            newEntity.Penalty = string.IsNullOrEmpty(this.txtPenalty.Text) ? 0 : Convert.ToDecimal(this.txtPenalty.Text);
            newEntity.PenaltyDetails = this.txtPenaltyDetails.Text.Trim();

            newService.Save(ActionType.Update, newEntity);

            Response.Redirect("ManageLoans.aspx");
        }
        private LoanEntity SetData(DataRow oRow)
        {
            try
            {
                LoanEntity ent = new LoanEntity();
                ent.ID = Convert.ToInt32(oRow["id"]);
                ent.Amount = Convert.ToDecimal(oRow["amount"]);
                ent.Period = Convert.ToInt32(oRow["period"]);
                ent.TermId = Convert.ToInt32(oRow["term_id"]);
                ent.Date = Convert.ToDateTime(oRow["date"]);
                ent.Interest = Convert.ToDecimal(oRow["interest"]);
                ent.CollateralId = Convert.ToInt32(oRow["collateral_id"]);
                ent.CollateralDetails = oRow["collateral_details"].ToString();
                ent.ComakerId = Convert.ToInt32(oRow["comaker_id"]);
                ent.BorrowerId = Convert.ToInt32(oRow["borrower_id"]);
                ent.InvestorId = Convert.ToInt32(oRow["investor_id"]);
                ent.Notes = oRow["notes"].ToString();
                ent.Penalty = Convert.ToDecimal(oRow["penalty"]);
                ent.PenaltyDetails = oRow["penalty_details"].ToString();
                ent.Status = Convert.ToInt32(oRow["status"]);

                return ent;

            }
            catch (Exception ex) { throw ex; }
        }
        private LoanEntity SetApplyLoanData(DataRow oRow)
        {
            try
            {
                LoanEntity ent = new LoanEntity();
                ent.ID = Convert.ToInt32(oRow["id"]);
                ent.Amount = Convert.ToDecimal(oRow["amount"]);
                ent.Period = Convert.ToInt32(oRow["period"]);

                ent.CollateralId = Convert.ToInt32(oRow["collateral_id"]);
                ent.CollateralDetails = oRow["collateral_details"].ToString();

                ent.BorrowerId = Convert.ToInt32(oRow["borrower_id"]);

                ent.Notes = oRow["notes"].ToString();

                return ent;

            }
            catch (Exception ex) { throw ex; }
        }
        public void SaveApplyLoan(ActionType type, LoanEntity ent)
        {
            try
            {

                using (Database db = new Database(GlobalObjects.CONNECTION_STRING))
                {
                    db.Open();
                    int ret = 0;
                    int typ = (int)type;
                    string sql = "SaveApplyLoan";
                    string[] asParams;
                    DbType[] atParamTypes;
                    object[] aoValues;

                    asParams = new string[] {   "@actiontype",
                                                "@id",
                                                "@amount",
                                                "@period",
                                                "@termid",
                                                "@date",
                                                "@interest",
                                                "@collateralid",
                                                "@collateraldetails",
                                                "@comakerid",
                                                "@investorid",
                                                "@borrowerid",
                                                "@notes",
                                                "@penalty",
                                                "@penaltydetails",
                                                "@status",
                                                "@createdby",
                                                "@createddate",
                                                "@updatedby",
                                                "@updateddate"};

                    atParamTypes = new DbType[] {
                                                    DbType.Int16,
                                                    DbType.Int32,
                                                    DbType.Decimal,
                                                    DbType.Int32,
                                                    DbType.Int32,
                                                    DbType.Date,
                                                    DbType.Decimal,
                                                    DbType.Int32,
                                                    DbType.String,
                                                    DbType.Int32,
                                                    DbType.Int32,
                                                    DbType.Int32,
                                                    DbType.String,
                                                    DbType.Decimal,
                                                    DbType.String,
                                                    DbType.Int32,
                                                    DbType.String,
                                                    DbType.DateTime,
                                                    DbType.String,
                                                    DbType.DateTime };

                    aoValues = new object[] {
                                                typ,
                                                ent.ID,
                                                ent.Amount,
                                                ent.Period,
                                                ent.TermId,
                                                ent.Date,
                                                ent.Interest,
                                                ent.CollateralId,
                                                ent.CollateralDetails,
                                                ent.ComakerId,
                                                ent.InvestorId,
                                                ent.BorrowerId,
                                                ent.Notes,
                                                ent.Penalty,
                                                ent.PenaltyDetails,
                                                ent.Status,
                                                appUsr.UserName,
                                                DateTime.Now,
                                                appUsr.UserName,
                                                DateTime.Now
                                            };

                    db.ExecuteCommandNonQuery(sql, asParams, atParamTypes, aoValues, out ret, CommandTypeEnum.StoredProcedure);

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public LoanEntity GetOne(int userId)
        {
            try
            {

                using (Database db = new Database(GlobalObjects.CONNECTION_STRING))
                {
                    db.Open();
                    string sql;
                    int ret = 0;
                    DataTable oTable = new DataTable();
                    sql = "GetLoan";
                    db.ExecuteCommandReader(sql,
                        new string[] { "@id" },
                        new DbType[] { DbType.Int32 },
                        new object[] { userId },
                        out ret, ref oTable, CommandTypeEnum.StoredProcedure);

                    LoanEntity user = new LoanEntity();
                    if (oTable.Rows.Count > 0)
                    {
                        DataRow oRow = oTable.Rows[0];
                        user = SetData(oRow);

                    }

                    return user;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void PopulateFields(int id)
        {
            newEntity = new LoanEntity();
            newEntity = newService.GetOne(id);

            lblPeriod.InnerText = newEntity.Period.ToString() + " month(s)";
            lblDate.InnerText = newEntity.Date.ToString("M/d/yyyy");
            lblNotes.InnerText = newEntity.Notes;
            lblAmount.InnerText = "Php " + newEntity.Amount.ToString();
            lblPenalty.InnerText = "Php " + newEntity.Penalty.ToString();
            lblPenaltyDetails.InnerText = newEntity.PenaltyDetails;
            lblInterest.InnerText = newEntity.Interest.ToString() + " %";
            lblCollateralDetails.InnerText = newEntity.CollateralDetails;
            lblLoanId.InnerText = newEntity.ID.ToString();

            DataTable oTable = newService.GetBorrowers(0);
            oTable.DefaultView.RowFilter = " id = " + newEntity.BorrowerId.ToString();
            if (oTable.DefaultView.ToTable().Rows.Count > 0)
            {
                DataRow oRow = oTable.DefaultView.ToTable().Rows[0];

                hlBorrower.Text = oRow["name"].ToString();
                hlBorrower.NavigateUrl = string.Format("~/Profile.aspx?id={0}", LoanMac.Core.Utility.EncryptQueryString(HttpUtility.UrlEncode(newEntity.BorrowerId.ToString())));
            }

            oTable = newService.GetLoanTerms();
            oTable.DefaultView.RowFilter = " id = " + newEntity.TermId.ToString();
            if (oTable.DefaultView.ToTable().Rows.Count > 0)
            {
                DataRow oRow = oTable.DefaultView.ToTable().Rows[0];
                this.lblTerm.InnerText = oRow["name"].ToString();
            }

            oTable = newService.GetCollateral();
            oTable.DefaultView.RowFilter = " id = " + newEntity.CollateralId.ToString();
            if (oTable.DefaultView.ToTable().Rows.Count > 0)
            {
                DataRow oRow = oTable.DefaultView.ToTable().Rows[0];
                this.lblCollateral.InnerText = oRow["name"].ToString();
            }

            oTable = newService.GetBorrowers(0);
            oTable.DefaultView.RowFilter = " id = " + newEntity.ComakerId.ToString();
            if (oTable.DefaultView.ToTable().Rows.Count > 0)
            {
                DataRow oRow = oTable.DefaultView.ToTable().Rows[0];
                hlComaker.Text = oRow["name"].ToString();
                hlComaker.NavigateUrl = string.Format("~/Profile.aspx?id={0}", LoanMac.Core.Utility.EncryptQueryString(HttpUtility.UrlEncode(newEntity.ComakerId.ToString())));
            }

            oTable = newService.GetBorrowers(0);
            oTable.DefaultView.RowFilter = " id = " + newEntity.InvestorId.ToString();
            if (oTable.DefaultView.ToTable().Rows.Count > 0)
            {
                DataRow oRow = oTable.DefaultView.ToTable().Rows[0];
                hlInvestor.Text = oRow["name"].ToString();
                hlInvestor.NavigateUrl = string.Format("~/Profile.aspx?id={0}", LoanMac.Core.Utility.EncryptQueryString(HttpUtility.UrlEncode(newEntity.InvestorId.ToString())));
            }
        }