internal string updateCurtailmets(SelectedCurtailmentList curtailmentScheduleModel, int loanId, string dealerEmail)
        {
            try
            {
                int i = 1;
                XElement xEle = new XElement("Curtailments",
                    from curtailmentShedule in curtailmentScheduleModel.SelectedCurtailmentSchedules
                    select new XElement("CurtailmentShedule",
                        new XElement("CurtNo", curtailmentShedule.CurtNumber),
                        new XElement("UnitId", curtailmentShedule.UnitId),
                        new XElement("CurtAmount", curtailmentShedule.CurtAmount),
                        new XElement("PayDate", curtailmentShedule.PayDate),
                        new XElement("id", i++)
                        ));
                string xmlDoc = xEle.ToString();


                DataHandler dataHandler = new DataHandler();
                List<object[]> paramertList2 = new List<object[]>();
                paramertList2.Add(new object[] { "@loan_id", loanId });
                paramertList2.Add(new object[] { "@dealer_email", dealerEmail ?? ""});
                paramertList2.Add(new object[] { "@Input", xmlDoc });


                return dataHandler.ExecuteSQLWithStringReturnVal("spUpdateCurtailmentSchedule", paramertList2);


            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public string PayCurtailments(SelectedCurtailmentList selectedCurtailmentList, string needSend, string dealerEmail, string dueDate)
        {
            // if session expired -- return null 
            if (Session["loanCode"] == null || Session["loanCode"].ToString() == "")
                return null;

            var loanCode = Session["loanCode"].ToString(); // take loan code from session 
            string paidDate = "";

            var loanDetails = (new LoanSetupAccess()).GetLoanDetailsByLoanCode(loanCode); // take loan details of the loan code

            CurtailmentAccess curtailmentAccess = new CurtailmentAccess();
            string returnValue = curtailmentAccess.updateCurtailmets(selectedCurtailmentList, loanDetails.loanId, dealerEmail); // update curtailment details as paid


            // if curtailment successfully updated
            if (returnValue != null)
            {
                // saving for reporting purpose
                decimal totalpaid = 0.00M;
                List<CurtailmentShedule> selectedCurtailmentSchedules = selectedCurtailmentList.SelectedCurtailmentSchedules;
                foreach (var items in selectedCurtailmentSchedules)
                {
                    items.PaidDate = items.PayDate.ToString("MM/dd/yyyy");
                    totalpaid += items.CurtAmount;
                    paidDate = items.PaidDate;
                }

                foreach (var items in selectedCurtailmentSchedules)
                {
                    items.TotalAmountPaid = totalpaid;
                }

                Session["CurtUnitDuringSession"] = selectedCurtailmentSchedules;
                if (needSend == "Yes")
                {
                    ReportViewer rptViewerCurtailmentReceiptDuringSession = new ReportViewer();
                    rptViewerCurtailmentReceiptDuringSession.ProcessingMode = ProcessingMode.Local;
                    rptViewerCurtailmentReceiptDuringSession.Reset();
                    rptViewerCurtailmentReceiptDuringSession.LocalReport.EnableExternalImages = true;
                    rptViewerCurtailmentReceiptDuringSession.LocalReport.ReportPath = Server.MapPath("~/Reports/RptCurtailmentDuringSession.rdlc");

                    ReportAccess ra = new ReportAccess();
                    List<LoanDetailsRpt> details = ra.TopHeaderDetails(loanDetails.loanId, userData.UserId);

                    foreach (var dates in details)
                    {
                        dates.ReportDate = DateTime.Now.ToString("MM/dd/yyyy");
                    }

                    rptViewerCurtailmentReceiptDuringSession.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));

                    if (selectedCurtailmentSchedules != null && selectedCurtailmentSchedules.Count > 0)
                    {
                        try
                        {
                            rptViewerCurtailmentReceiptDuringSession.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", selectedCurtailmentSchedules));

                            Warning[] warnings;
                            string[] streamids;
                            string mimeType;
                            string encoding;
                            string filenameExtension;

                            //create pdf file
                            byte[] bytes = rptViewerCurtailmentReceiptDuringSession.LocalReport.Render(
                                "PDF", null, out mimeType, out encoding, out filenameExtension,
                                out streamids, out warnings);

                            if (dealerEmail != "")
                            {
                                string mailSubject = "Curtailment Paid Receipt - Loan " + loanDetails.loanNumber;
                                string mailBody =
                                    "Curtailments for Loan " + loanDetails.loanNumber + " which were due on or before " + dueDate  + " have been paid on " + paidDate + ". " +
                                    "Please view the attached PDF file for full curtailment payment details. " +
                                    Environment.NewLine + Environment.NewLine +
                                    "Thank you," +
                                    Environment.NewLine +
                                    "Dealer Floor Plan Software Team";

                                Thread thread = new Thread(delegate ()
                                {
                                    Email email = new Email(dealerEmail);
                                    email.SendMailWithAttachment(mailSubject, mailBody, bytes);
                                });

                                thread.IsBackground = true;
                                thread.Start();

                            }

                        }
                        catch (Exception e)
                        {
                            throw e;
                        }
                    }
                }

                //insert to log
                string[] arrList = new string[selectedCurtailmentList.SelectedCurtailmentSchedules.Count];
                int i = 0;
                // add all paid curtailment details to array
                foreach (var x in selectedCurtailmentList.SelectedCurtailmentSchedules)
                {
                    if (!string.IsNullOrEmpty(x.UnitId))
                    {
                        arrList[i] = "Pay Curtailment(s) for unit(s): " + x.IDNumber + " ,Curtailment No: " + x.CurtNumber + " ,Curtailment Amount:" + x.CurtAmount + " ,Paid Date:" + x.PayDate;
                        i++;
                    }
                }

                // join the array with comma seperated
                string units = string.Join(",", arrList);
                Log log = new Log(userData.UserId, userData.Company_Id, userData.BranchId, loanDetails.loanId, "Pay Curtailments", units, DateTime.Now);
                // insert into log
                int islog = (new LogAccess()).InsertLog(log);
            }
            // return the value
            return returnValue;
        }