private void InterestProcess(ref LoanContractScheduleDS ds, BNEWNORMALLOAN normalLoanEntryM, int replaymentTimes, DateTime? disbursalDate)
        {
            NewLoanControlRepository facade = new NewLoanControlRepository();
            BNewLoanControl it = facade.FindLoanControl(normalLoanEntryM.Code, replaymentTimes, "I+P").
                Union(facade.FindLoanControl(normalLoanEntryM.Code, replaymentTimes, "I")).FirstOrDefault();//All get Priciple date
            bool isNewInterest = false;
            bool isNewInterestActive = false;

            if (it == null || String.IsNullOrEmpty(it.Freq))
            {
                it = facade.FindLoanControl(normalLoanEntryM.Code, replaymentTimes, "EP").FirstOrDefault();//All get Priciple date
            }
            if (it == null || String.IsNullOrEmpty(it.Freq))
            {
                return;
            }

            int freq = 0;
            String rateType = "1";
            DateTime drawdownDate = normalLoanEntryM.Drawdown == null ? (disbursalDate == null ? (DateTime)normalLoanEntryM.ValueDate : (DateTime)disbursalDate) : (DateTime)normalLoanEntryM.Drawdown;
            DateTime startDate = (DateTime)normalLoanEntryM.ValueDate;
            DateTime endDate = (DateTime)normalLoanEntryM.MaturityDate;
            DateTime startInterestDate = drawdownDate;
            DateTime prevInterestDate = drawdownDate;
            int durationDate = endDate.Subtract(startDate).Days;
            int interestDay = 0;
            int perios = 0;
            decimal interestedValue = 0;
            decimal interestedValue2 = 0;

            rateType = String.IsNullOrEmpty(normalLoanEntryM.RateType) ? "1" : normalLoanEntryM.RateType;
            if (rateType.Equals("3"))//periodic interest = interestedRate + int speed
            {
                interestedValue = (normalLoanEntryM.InterestRate == null ? 0 : (decimal)normalLoanEntryM.InterestRate)
                    + (String.IsNullOrEmpty(normalLoanEntryM.IntSpread) ? 0 : Decimal.Parse(normalLoanEntryM.IntSpread));
                PeriodicProcess(ref ds, normalLoanEntryM, drawdownDate, replaymentTimes, ref interestedValue2);
            }
            else // interest = interestedRate
            {
                interestedValue = (normalLoanEntryM.InterestRate == null ? 0 : (decimal)normalLoanEntryM.InterestRate);
            }

            ds.DtInfor.Rows[0][ds.Cl_interest] = interestedValue;
            ds.DtInfor.Rows[0][ds.Cl_interestKey] = durationDate / 30;

            if (it.Freq.Equals("E"))
            {
                freq = 0;
                startInterestDate = it.Date == null ? (DateTime)endDate : (DateTime)it.Date;
            }
            else
            {
                freq = int.Parse(it.Freq);
                startInterestDate = it.Date == null ? ((DateTime)drawdownDate) : (DateTime)it.Date;
            }
            interestDay = startInterestDate.Day;

            if (freq > 0)
            {
                perios = durationDate / (freq * 30);
            }
            else
            {
                perios = 1;
            }

            for (int i = 0; i < perios; i++)
            {
                if (i == perios - 1)
                {
                    it = facade.FindLoanControl(normalLoanEntryM.Code, replaymentTimes, "EP").FirstOrDefault();
                    if (it != null)
                    {
                        startInterestDate = it.Date == null ? startInterestDate : (DateTime)it.Date;
                    }
                    else
                    {
                        startInterestDate = (DateTime)normalLoanEntryM.MaturityDate;
                    }
                }

                DataRow dr = findInstallmantRow(startInterestDate, ds);
                if (dr == null)
                {
                    dr = ds.DtItems.NewRow();
                    dr[ds.Cl_dueDate.ColumnName] = startInterestDate;
                    dr[ds.Cl_isDisbursalRow.ColumnName] = false;
                    dr[ds.Cl_isPaymentRow.ColumnName] = false;
                    dr[ds.Cl_isPeriodicAutomaticRow.ColumnName] = false;
                    dr[ds.Cl_principle.ColumnName] = 0;
                    dr[ds.Cl_PrintOs.ColumnName] = 0;
                    ds.DtItems.Rows.Add(dr);
                }
                dr[ds.Cl_isInterestedRow.ColumnName] = true;
                dr[ds.Cl_durationDate.ColumnName] = startInterestDate.Subtract(prevInterestDate).Days;

                prevInterestDate = startInterestDate;
                startInterestDate = startInterestDate.AddMonths(freq);

                try
                {
                    startInterestDate = new DateTime(((DateTime)startInterestDate).Year, ((DateTime)startInterestDate).Month, interestDay);
                }
                catch
                {
                }

                if (startInterestDate.Subtract(endDate).Days > 0)
                    startInterestDate = endDate;

            }

            ds.DtItems.DefaultView.Sort = "DueDate asc";
            ds.DtItems = ds.DtItems.DefaultView.ToTable();

            decimal interestAmount = 0;
            decimal currentAmount = getCurrentLoanAmount(normalLoanEntryM, replaymentTimes);

            decimal amountTemp = 0;
            prevInterestDate = drawdownDate;
            int durationsDay = 0;

            int periousIndex = 0;
            List<DataRow> removeL = new List<DataRow>();
            for (int i = 0; i < ds.DtItems.Rows.Count; i++)
            {
                DataRow dr = ds.DtItems.Rows[i];
                durationsDay = ((DateTime)dr[ds.Cl_dueDate.ColumnName]).Subtract(prevInterestDate).Days;

                if (dr[ds.Cl_isInterestedRow.ColumnName] != null && (bool)dr[ds.Cl_isInterestedRow.ColumnName] && isNewInterest && isNewInterestActive)
                {
                    interestedValue = interestedValue2;
                }

                if (rateType.Equals("2"))//fix for initial
                {
                    interestAmount = durationsDay * ((interestedValue / 36000) * (decimal)normalLoanEntryM.LoanAmount);
                }
                else
                {
                    interestAmount = durationsDay * ((interestedValue / 36000) * currentAmount);
                }

                dr[ds.Cl_durationDate.ColumnName] = durationsDay;

                if (!(bool)dr[ds.Cl_isPaymentRow.ColumnName] && !(bool)dr[ds.Cl_isDisbursalRow.ColumnName])
                {
                    dr[ds.Cl_PrintOs.ColumnName] = currentAmount;
                }

                if (dr[ds.Cl_isInterestedRow.ColumnName] != null && (bool)dr[ds.Cl_isInterestedRow.ColumnName])
                {

                    dr[ds.Cl_interestAmount.ColumnName] = interestAmount + amountTemp;
                    amountTemp = 0;
                }
                else
                {
                    amountTemp += interestAmount;
                    dr[ds.Cl_interestAmount.ColumnName] = 0;

                }

                if (dr[ds.Cl_isPeriodicAutomaticRow.ColumnName] != null && (bool)dr[ds.Cl_isPeriodicAutomaticRow.ColumnName])
                {
                    isNewInterest = true;
                    //interestedValue = interestedValue2;
                }

                if (dr[ds.Cl_isInterestedRow.ColumnName] != null && (bool)dr[ds.Cl_isInterestedRow.ColumnName] && isNewInterest)
                {
                    isNewInterestActive = true;
                }

                if (!(bool)dr[ds.Cl_isInterestedRow.ColumnName] && !(bool)dr[ds.Cl_isPaymentRow.ColumnName]
                     && ((bool)dr[ds.Cl_isPeriodicAutomaticRow.ColumnName] || (bool)dr[ds.Cl_isDisbursalRow.ColumnName]))
                {
                    removeL.Add(dr);
                    //removeRow = dr;
                }
                else
                {
                    periousIndex++;
                    dr[ds.Cl_perious.ColumnName] = periousIndex;
                }

                currentAmount = dr[ds.Cl_PrintOs.ColumnName] != null ? (decimal)dr[ds.Cl_PrintOs.ColumnName] : 0;

                prevInterestDate = (DateTime)dr[ds.Cl_dueDate.ColumnName];

            }

            foreach (var removeit in removeL)
            {
                ds.DtItems.Rows.Remove(removeit);
            }
        }
        /// <summary>
        /// Vi du: 
        ///- 2 Thang dau  Lai suat 5%
        ///- Tu thang thu 3  Lai suat Tiet Kiem 13T + Bien do 2%
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="normalLoanEntryM"></param>
        /// <param name="startDrawdownDate"></param>
        /// <param name="replaymentTimes"></param>
        /// <param name="newInterestKey"></param>
        private void PeriodicProcess(ref LoanContractScheduleDS ds, BNEWNORMALLOAN normalLoanEntryM, DateTime startDrawdownDate, int replaymentTimes, ref decimal newInterestKey)
        {
            if (normalLoanEntryM == null || String.IsNullOrEmpty(normalLoanEntryM.Code))
            {
                return;
            }
            int rateType = 1; //default is Fix A/ Periodic --> Du no giam dan. (fix B is du no ban dau)
            rateType = String.IsNullOrEmpty(normalLoanEntryM.RateType) ? 1 : int.Parse(normalLoanEntryM.RateType);

            if (rateType != 3) //peridodic case
            {
                return;
            }
            NewLoanControlRepository facadeL = new NewLoanControlRepository();
            BNewLoanControl it = facadeL.FindLoanControl(normalLoanEntryM.Code, replaymentTimes, "AC").FirstOrDefault();
            NewLoanInterestedKeyRepository facade = new NewLoanInterestedKeyRepository();
            BLOANINTEREST_KEY interestKey = null;

            //Only process if user define AC
            if (it == null || it.Date == null)
            {
                return;
            }

            //if (it != null && !String.IsNullOrEmpty(it.Freq))
            //{
            //    interestKey = facade.GetInterestKey(int.Parse(it.Freq)).FirstOrDefault();
            //}
            //else
            //{
                if (String.IsNullOrEmpty(normalLoanEntryM.InterestKey))
                {
                    return;
                }
                interestKey = facade.GetInterestKey(int.Parse(normalLoanEntryM.InterestKey)).FirstOrDefault();
            //}

            if (interestKey != null)
            {
                if (normalLoanEntryM.Currency.Equals("VND"))
                {
                    newInterestKey = (interestKey.VND_InterestRate == null ? 0 : (decimal)interestKey.VND_InterestRate)
                    + (String.IsNullOrEmpty(normalLoanEntryM.IntSpread) ? 0 : Decimal.Parse(normalLoanEntryM.IntSpread));
                }
                else if (normalLoanEntryM.Currency.Equals("USD"))
                {
                    newInterestKey = (interestKey.USD_InterestRate == null ? 0 : (decimal)interestKey.USD_InterestRate)
                    + (String.IsNullOrEmpty(normalLoanEntryM.IntSpread) ? 0 : Decimal.Parse(normalLoanEntryM.IntSpread));
                }
                else
                {
                    newInterestKey = (normalLoanEntryM.InterestRate == null ? 0 : (decimal)normalLoanEntryM.InterestRate)
                   + (String.IsNullOrEmpty(normalLoanEntryM.IntSpread) ? 0 : Decimal.Parse(normalLoanEntryM.IntSpread));
                }

                //DateTime newrateDate = startDrawdownDate.AddMonths((int)(interestKey.MonthLoanRateNo));
                //Start date is defined date in AC
                DateTime newrateDate = (DateTime)it.Date;

                DataRow dr = findInstallmantRow(newrateDate, ds);
                if (dr == null)
                {
                    dr = ds.DtItems.NewRow();
                    dr[ds.Cl_dueDate.ColumnName] = newrateDate;
                    dr[ds.Cl_isDisbursalRow.ColumnName] = false;
                    dr[ds.Cl_isPaymentRow.ColumnName] = false;
                    dr[ds.Cl_isPeriodicAutomaticRow.ColumnName] = true;
                    dr[ds.Cl_isInterestedRow.ColumnName] = false;
                    dr[ds.Cl_principle.ColumnName] = 0;
                    dr[ds.Cl_PrintOs.ColumnName] = 0;
                    ds.DtItems.Rows.Add(dr);
                }
                else
                {
                    dr[ds.Cl_isPeriodicAutomaticRow.ColumnName] = true;
                }

            }
        }
        private void getPaymentInputControl(ref DateTime? periosStartDate, ref DateTime? periosEndDate, ref int numberOfPerios,
            ref decimal instalmant, ref decimal instalmantEnd, ref int freg, BNEWNORMALLOAN normalLoanEntryM, int replaymentTimes)
        {
            NewLoanControlRepository facade = new NewLoanControlRepository();
            BNewLoanControl it = facade.FindLoanControl(normalLoanEntryM.Code, replaymentTimes, "I+P").
                Union(facade.FindLoanControl(normalLoanEntryM.Code, replaymentTimes, "P")).FirstOrDefault();//All get Priciple date

            if (it != null)
            {
                periosStartDate = it.Date;
                instalmant = it.AmountAction == null ? 0 : (decimal)it.AmountAction;
                numberOfPerios = it.No == null ? 0 : (int)it.No;
                if (!String.IsNullOrEmpty(it.Freq))
                {
                    if (it.Freq.Equals("E"))
                    {
                        freg = 0;
                    }
                    else
                    {
                        freg = int.Parse(it.Freq);
                    }
                }
                else
                {
                    freg = 0;
                }

            }

            if (periosStartDate == null)
            {
                DateTime? drawdown = normalLoanEntryM.Drawdown;
                if (drawdown == null)
                    drawdown = normalLoanEntryM.ValueDate;
                periosStartDate = ((DateTime)drawdown).AddMonths(freg);
            }

            if (numberOfPerios == 0)
            {
                DateTime startDate = (DateTime)normalLoanEntryM.ValueDate;
                DateTime endDate = (DateTime)normalLoanEntryM.MaturityDate;
                //int durationLoan = endDate.Subtract(startDate).Days;
                int durationMonthLoan = ((endDate.Year - startDate.Year) * 12) + endDate.Month - startDate.Month;
                int fregV = freg * 30;

                if (freg > 0)
                {
                    numberOfPerios = durationMonthLoan / freg;
                }

            }

            if (instalmant == 0)
            {
                if (numberOfPerios > 0)
                {

                    instalmant = (Int32)((getCurrentLoanAmount(normalLoanEntryM, replaymentTimes)) / numberOfPerios);

                }

            }

            if (numberOfPerios > 0 && (it.No == null || it.No == 0))
            {
                numberOfPerios--;
            }

            it = facade.FindLoanControl(normalLoanEntryM.Code, replaymentTimes, "EP").FirstOrDefault();
            if (it != null)
            {
                instalmantEnd = it.AmountAction == null ? 0 : (decimal)it.AmountAction;
                periosEndDate = it.Date;
            }

            if (instalmantEnd == 0)
            {
                instalmantEnd = getCurrentLoanAmount(normalLoanEntryM, replaymentTimes) - numberOfPerios * instalmant;
            }

            if (periosEndDate == null)
            {
                periosEndDate = normalLoanEntryM.MaturityDate;
            }
        }