/// <summary>
        /// CreatedBy:Irfan MAM
        /// CreatedDate:2016/2/9
        /// getting all unit types
        /// </summary>
        /// <returns>IList<UnitType></returns>
        /// UpdatedBy:Asanka Senarathna
        /// 
        internal IList<UnitType> getAllUnitTypes()
        {
            List<UnitType> unitTypes = new List<UnitType>();
            DataHandler dataHandler = new DataHandler();

            try
            {
                DataSet dataSet = dataHandler.GetDataSet("spGetAllUnitTypes", null);
                if (dataSet != null && dataSet.Tables.Count != 0)
                {
                    foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                    {
                        UnitType unitType = new UnitType();
                        unitType.unitTypeName = dataRow["unit_type_name"].ToString();

                        unitType.unitTypeId = int.Parse(dataRow["unit_type_id"].ToString());
                        unitType.isSelected = false;
                        unitTypes.Add(unitType);
                    }

                    return unitTypes;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="loanId"></param>
        /// <returns></returns>
        public IList<UnitType> RptGetUnitTypes(int loanId)
        {
            IList<UnitType> unittypes = new List<UnitType>();

            DataHandler dataHandler = new DataHandler();
            List<object[]> paramertList = new List<object[]> { new object[] { "@loan_id", loanId } };

            DataSet dataSet = dataHandler.GetDataSet("spGetLoanUnitTypesByLoanId ", paramertList);
            if (dataSet != null && dataSet.Tables.Count != 0)
            {
                foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                {
                    UnitType unitType = new UnitType();
                    unitType.unitTypeId = Convert.ToInt32((dataRow["unit_type_id"]));
                    unitType.unitTypeName = dataRow["unit_type_name"].ToString();

                    unittypes.Add(unitType);
                }
            }

            return unittypes;
        }
        /// <summary>
        /// CreatedBy:  Kanishka
        /// CreatedDate:02/29/2016
        /// get loan details by loan code
        /// </summary>
        /// <param name="loanCode"></param>
        /// <returns></returns>
        
        
        internal LoanSetupStep1 GetLoanDetailsByLoanCode(string loanCode)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AutoDealersConnection"].ConnectionString))
            {
                try
                {
                    using (SqlCommand cmd = new SqlCommand("spGetLoanDetailsByLoanCode", con))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@loan_code", SqlDbType.VarChar).Value = loanCode;
                        con.Open();
                        SqlDataReader reader = cmd.ExecuteReader();

                        LoanSetupStep1 loanSetupStep1 = new LoanSetupStep1();


                        while (reader.Read())
                        {
                            loanSetupStep1.loanId = Convert.ToInt32(reader["loan_id"]);
                            loanSetupStep1.advancePercentage = int.Parse(reader["advance"].ToString());
                            loanSetupStep1.autoReminderEmail = reader["auto_remind_email"].ToString();
                            loanSetupStep1.autoReminderPeriod = int.Parse(reader["auto_remind_period"].ToString());
                            loanSetupStep1.defaultUnitType = int.Parse(reader["default_unit_type"].ToString());
                            loanSetupStep1.isEditAllowable = Convert.ToBoolean(reader["is_edit_allowable"].ToString());
                            loanSetupStep1.isInterestCalculate = Convert.ToBoolean(reader["is_interest_calculate"].ToString());
                            loanSetupStep1.loanAmount = Convert.ToDecimal(reader["loan_amount"].ToString());
                            loanSetupStep1.loanNumber = reader["loan_number"].ToString();
                            loanSetupStep1.maturityDate = Convert.ToDateTime(reader["maturity_date"].ToString());
                            loanSetupStep1.nonRegisteredBranchId = int.Parse(reader["non_reg_branch_id"].ToString());
                            loanSetupStep1.paymentMethod = reader["payment_method"].ToString();
                            loanSetupStep1.startDate = Convert.ToDateTime(reader["start_date"].ToString());

                            loanSetupStep1.RegisteredBranchId = Convert.ToInt32(reader["branch_id"].ToString());
                            loanSetupStep1.LoanStatus = Convert.ToBoolean(reader["loan_status"]);
                            loanSetupStep1.RegisteredBranchCode = reader["r_branch_code"].ToString();
                            loanSetupStep1.RegisteredCompanyCode = reader["company_code"].ToString();
                            if(reader["curtailment_due_date"].ToString()== "EoM") {
                                loanSetupStep1.CurtailmentDueDate = "End of Month";
                            }
                            else {
                                string dueDate = reader["curtailment_due_date"].ToString();
                                if (dueDate.Length > 0)
                                {
                                    if (dueDate.Length == 1)
                                    {
                                        if (dueDate.Contains("1"))
                                        {
                                            loanSetupStep1.CurtailmentDueDate = dueDate+ "st of each month";
                                        }
                                        else if (dueDate.Contains("2"))
                                        {
                                            loanSetupStep1.CurtailmentDueDate = dueDate + "nd of each month";
                                        }
                                        else if (dueDate.Contains("3"))
                                        {
                                            loanSetupStep1.CurtailmentDueDate = dueDate + "rd of each month";
                                        }
                                        else
                                        {
                                            loanSetupStep1.CurtailmentDueDate = dueDate + "th of each month";
                                        }
                                    }
                                    else if(dueDate.Length > 1)
                                    {
                                        if (!dueDate.ElementAt(dueDate.Length - 2).ToString().Contains("1"))
                                        {
                                            if (dueDate.ElementAt(dueDate.Length - 1).ToString().Contains("1"))
                                            {
                                                loanSetupStep1.CurtailmentDueDate = dueDate + "st of each month";
                                            }
                                            else if (dueDate.ElementAt(dueDate.Length - 1).ToString().Contains("2"))
                                            {
                                                loanSetupStep1.CurtailmentDueDate = dueDate + "nd of each month";
                                            }
                                            else if (dueDate.ElementAt(dueDate.Length - 1).ToString().Contains("3"))
                                            {
                                                loanSetupStep1.CurtailmentDueDate = dueDate + "rd of each month";
                                            }
                                            else
                                            {
                                                loanSetupStep1.CurtailmentDueDate = dueDate + "th of each month";
                                            }
                                        }
                                        else
                                        {
                                            loanSetupStep1.CurtailmentDueDate = dueDate + "th of each month";
                                        }
                                    }
                                }
                                //loanSetupStep1.CurtailmentDueDate = reader["curtailment_due_date"].ToString()+"th of each month";
                            }
                            //loanSetupStep1.CurtailmentDueDate = reader["curtailment_due_date"].ToString();
                        }

                        reader.Close();

                        IList<UnitType> unittypes = new List<UnitType>();
                        using (SqlCommand cmd2 = new SqlCommand("spGetLoanUnitTypesByLoanId", con))
                        {
                            cmd2.CommandType = CommandType.StoredProcedure;
                            cmd2.Parameters.Add("@loan_id", SqlDbType.Int).Value = loanSetupStep1.loanId;
                            //con.Open();
                            SqlDataReader reader2 = cmd2.ExecuteReader();

                            while (reader2.Read())
                            {
                                UnitType unitType = new UnitType();
                                unitType.unitTypeId = int.Parse(reader2["unit_type_id"].ToString());
                                unitType.unitTypeName = reader2["unit_type_name"].ToString();
                                unittypes.Add(unitType);

                            }
                            loanSetupStep1.selectedUnitTypes = unittypes;

                            return loanSetupStep1;

                        }
                    }
                }


                catch (Exception ex)
                {
                    throw ex;

                }
                finally
                {
                    con.Close();
                    con.Dispose();
                }
            }
        }
        internal LoanSetupStep1 GetLoanStepOne(int loanId)
        {
            LoanSetupStep1 loanSetupStep1 = new LoanSetupStep1();
            IList<UnitType> unittypes = new List<UnitType>();

            DataHandler dataHandler = new DataHandler();
            List<object[]> paramertList = new List<object[]>();
            paramertList.Add(new object[] { "@loan_id", loanId });

            try
            {
                DataSet dataSet = dataHandler.GetDataSet("spGetLoanStepOneByLoanId", paramertList);
                if (dataSet != null && dataSet.Tables.Count != 0 && dataSet.Tables[0].Rows.Count != 0)
                {
                    DataRow dataRow = dataSet.Tables[0].Rows[0];

                    loanSetupStep1.advancePercentage = int.Parse(dataRow["advance"].ToString());
                    //loanSetupStep1.allUnitTypes
                    loanSetupStep1.autoReminderEmail = dataRow["auto_remind_email"].ToString();
                    loanSetupStep1.autoReminderPeriod = int.Parse(dataRow["auto_remind_period"].ToString());
                    loanSetupStep1.defaultUnitType = int.Parse(dataRow["default_unit_type"].ToString());
                    loanSetupStep1.isEditAllowable = Convert.ToBoolean(dataRow["is_edit_allowable"].ToString());
                    loanSetupStep1.isInterestCalculate = Convert.ToBoolean(dataRow["is_interest_calculate"].ToString());
                    loanSetupStep1.loanAmount = Convert.ToDecimal(dataRow["loan_amount"].ToString());
                    loanSetupStep1.loanNumber = dataRow["loan_number"].ToString();
                    //loanSetupStep1.loanNumberForDisplay = loanSetupStep1.loanNumber;
                    loanSetupStep1.maturityDate = Convert.ToDateTime(dataRow["maturity_date"].ToString());
                    loanSetupStep1.nonRegisteredBranchId = int.Parse(dataRow["non_reg_branch_id"].ToString());
                    loanSetupStep1.paymentMethod = dataRow["payment_method"].ToString();
                    //loanSetupStep1.payOffPeriod = int.Parse(dataRow["pay_off_period"].ToString());
                    //loanSetupStep1.payOffPeriodType = (Convert.ToChar(dataRow["pay_off_type"].ToString()) == 'd') ? 0 : 1;
                    //loanSetupStep1.selectedUnitTypes
                    loanSetupStep1.startDate = Convert.ToDateTime(dataRow["start_date"].ToString());
                    loanSetupStep1.loanCode = dataRow["loan_code"].ToString();
                }

                DataSet dataSet2 = dataHandler.GetDataSet("spGetLoanUnitTypesByLoanId", paramertList);
                if (dataSet2 != null && dataSet2.Tables.Count != 0 && dataSet2.Tables[0].Rows.Count != 0)
                {
                    foreach (DataRow dataRow2 in dataSet2.Tables[0].Rows)
                    {
                        UnitType unitType = new UnitType();
                        unitType.unitTypeId = int.Parse(dataRow2["unit_type_id"].ToString());
                        unitType.unitTypeName = dataRow2["unit_type_name"].ToString();
                        unittypes.Add(unitType);
                    }
                    loanSetupStep1.selectedUnitTypes = unittypes;
                }
                return loanSetupStep1;
            }
            catch (Exception ex)
            {
                throw ex;
            }



            //using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AutoDealersConnection"].ConnectionString))
            //{
            //    try
            //    {
            //        using (SqlCommand cmd = new SqlCommand("spGetLoanStepOneByLoanId", con))
            //        {
            //            cmd.CommandType = CommandType.StoredProcedure;
            //            cmd.Parameters.Add("@loan_id", SqlDbType.Int).Value = loanId;
            //            con.Open();
            //            SqlDataReader reader = cmd.ExecuteReader();

            //            LoanSetupStep1 loanSetupStep1 = new LoanSetupStep1();


            //            while (reader.Read())
            //            {

            //                loanSetupStep1.advancePercentage = int.Parse(reader["advance"].ToString());
            //                //loanSetupStep1.allUnitTypes
            //                loanSetupStep1.autoReminderEmail = reader["auto_remind_email"].ToString();
            //                loanSetupStep1.autoReminderPeriod = int.Parse(reader["auto_remind_period"].ToString());
            //                loanSetupStep1.defaultUnitType = int.Parse(reader["default_unit_type"].ToString());
            //                loanSetupStep1.isEditAllowable = Convert.ToBoolean(reader["is_edit_allowable"].ToString());
            //                loanSetupStep1.isInterestCalculate = Convert.ToBoolean(reader["is_interest_calculate"].ToString());
            //                loanSetupStep1.loanAmount = Convert.ToDecimal(reader["loan_amount"].ToString());
            //                loanSetupStep1.loanNumber = reader["loan_number"].ToString();
            //                loanSetupStep1.loanNumberForDisplay = loanSetupStep1.loanNumber;
            //                loanSetupStep1.maturityDate = Convert.ToDateTime(reader["maturity_date"].ToString());
            //                loanSetupStep1.nonRegisteredBranchId = int.Parse(reader["non_reg_branch_id"].ToString());
            //                loanSetupStep1.paymentMethod = reader["payment_method"].ToString();
            //                loanSetupStep1.payOffPeriod = int.Parse(reader["pay_off_period"].ToString());
            //                loanSetupStep1.payOffPeriodType = (Convert.ToChar(reader["pay_off_type"].ToString()) == 'd') ? 0 : 1;
            //                //loanSetupStep1.selectedUnitTypes
            //                loanSetupStep1.startDate = Convert.ToDateTime(reader["start_date"].ToString());
            //                loanSetupStep1.loanCode = reader["loan_code"].ToString();

            //            }

            //            reader.Close();
            //            IList<UnitType> unittypes = new List<UnitType>();
            //            using (SqlCommand cmd2 = new SqlCommand("spGetLoanUnitTypesByLoanId", con))
            //            {
            //                cmd2.CommandType = CommandType.StoredProcedure;
            //                cmd2.Parameters.Add("@loan_id", SqlDbType.Int).Value = loanId;
            //                //con.Open();
            //                SqlDataReader reader2 = cmd2.ExecuteReader();

            //                while (reader2.Read())
            //                {
            //                    UnitType unitType = new UnitType();
            //                    unitType.unitTypeId = int.Parse(reader2["unit_type_id"].ToString());
            //                    unitType.unitTypeName = reader2["unit_type_name"].ToString();
            //                    unittypes.Add(unitType);

            //                }
            //                loanSetupStep1.selectedUnitTypes = unittypes;

            //                return loanSetupStep1;
            //            }
            //        }
            //    }


            //    catch (Exception ex)
            //    {
            //        throw ex;

            //    }
            //    finally
            //    {
            //        con.Close();
            //    }
            //}
        }