/// <summary>
        /// Frontend Page: Report page(Individual curtailment summary report)
        /// Title: Display Individual curtailment summary report
        /// Designed: Piyumi Perera
        /// User story: DFP 488
        /// Developed: Piyumi Perera
        /// Date created: 7/5/2016
        /// </summary>
        /// <param name="loanId"></param>
        public void RenderReport(int loanId, string idNumbe, List<RptIndividualCurtailmentSummary> indiCurtailment)
        {
            //Check authentication session is null then return
            if (Session["AuthenticatedUser"] == null) return;
            User userData = (User)Session["AuthenticatedUser"];

            //Dynamicaly set report viwer propertiece
            rptViewerIndividualCurtailmentSummary.ProcessingMode = ProcessingMode.Local;
            rptViewerIndividualCurtailmentSummary.Reset();
            rptViewerIndividualCurtailmentSummary.LocalReport.EnableExternalImages = true;
            rptViewerIndividualCurtailmentSummary.LocalReport.ReportPath = Server.MapPath("~/Reports/RptIndividualCurtailmentSummary.rdlc");
            rptViewerIndividualCurtailmentSummary.ZoomMode = ZoomMode.PageWidth;

            //Get details of loan
            ReportAccess ra = new ReportAccess();
            List<LoanDetailsRpt> details = ra.TopHeaderDetails(loanId, userData.UserId);
            foreach (var dates in details)
            {

                dates.ReportDate = DateTime.Now.ToString("MM/dd/yyyy");
            }

            //Set data set to report
            rptViewerIndividualCurtailmentSummary.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));

            //Set data set to report
            rptViewerIndividualCurtailmentSummary.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", indiCurtailment));
        }
        /// <summary>
        /// Frontend Page: Report page(Advance Fee Receipt Report)
        /// Title: Display Advance Fee Receipt
        /// Designed: Kasun Samarawickrama
        /// User story: 
        /// Developed: Kasun Samarawickrama
        /// Date created: 
        /// </summary>
        public void RenderReport(int loanId, DateTime startDate, DateTime endDate, List<RptFee> advanceFeeReceipt)
        {
            //Check authentication session is null then return
            if (Session["AuthenticatedUser"] == null) return;
            User userData = (User)Session["AuthenticatedUser"];

            rptViewerAdvanceFeeReceipt.ProcessingMode = ProcessingMode.Local;
            rptViewerAdvanceFeeReceipt.Reset();
            rptViewerAdvanceFeeReceipt.LocalReport.EnableExternalImages = true;
            rptViewerAdvanceFeeReceipt.LocalReport.ReportPath = Server.MapPath("~/Reports/RptAdvanceFeeReceipt.rdlc");
            rptViewerAdvanceFeeReceipt.ZoomMode = ZoomMode.PageWidth;

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

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

            //Set data set to report
            rptViewerAdvanceFeeReceipt.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));
            rptViewerAdvanceFeeReceipt.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", advanceFeeReceipt));
        }
        /// <summary>
        /// Frontend Page: Report Page(Curtailment Receipt Report)
        /// Title: Display Curtailment Receipt report
        /// Designed: Kanishka SHM
        /// User story: 
        /// Developed: Kanishka SHM
        /// Date created: 
        /// </summary>
        public void RenderReport(int loanId, DateTime startDate, DateTime endDate, List<CurtailmentShedule> curtailments)
        {
            //check authentication session is null, if null return
            if (Session["AuthenticatedUser"] == null) return;
            User userData = (User)Session["AuthenticatedUser"];

            //set report viewr property dynamically
            rptViewerCurtailmentReceipt.ProcessingMode=ProcessingMode.Local;
            rptViewerCurtailmentReceipt.Reset();
            rptViewerCurtailmentReceipt.LocalReport.EnableExternalImages = true;
            rptViewerCurtailmentReceipt.LocalReport.ReportPath = Server.MapPath("~/Reports/RptCurtailmentReceipt.rdlc");
            rptViewerCurtailmentReceipt.ZoomMode = ZoomMode.PageWidth;

            //get report header details
            ReportAccess ra = new ReportAccess();
            List<LoanDetailsRpt> details = ra.TopHeaderDetails(loanId, userData.UserId);

            //add dates, date range and current date
            foreach (var dates in details)
            {
                dates.StartRange = startDate.ToString("MM/dd/yyyy");
                dates.EndRange = endDate.ToString("MM/dd/yyyy");
                dates.ReportDate = DateTime.Now.ToString("MM/dd/yyyy");
            }

            //set data source to report viwer
            rptViewerCurtailmentReceipt.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));
            rptViewerCurtailmentReceipt.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", curtailments));
        }
        /*

            Frontend page: Report Page
            Title: Load details to report and show on browser
            Designed: Kanishka SHM
            User story: 
            Developed: Kanishka SHM
            Date created: 

        */
        public void RenderReport(int loanId, int titleStatus, List<Unit> units)
        {
            //check authentication session is null, if null return
            if (Session["AuthenticatedUser"] == null) return;
            User userData = (User)Session["AuthenticatedUser"];

            //set report viewr property dynamically
            rptViewerTitleStatus.ProcessingMode=ProcessingMode.Local;
            rptViewerTitleStatus.Reset();
            rptViewerTitleStatus.LocalReport.EnableExternalImages = true;
            rptViewerTitleStatus.LocalReport.ReportPath = Server.MapPath("~/Reports/RptTitleStatus.rdlc");
            rptViewerTitleStatus.ZoomMode = ZoomMode.PageWidth;

            //get report header details
            ReportAccess ra = new ReportAccess();
            List<LoanDetailsRpt> details = ra.TopHeaderDetails(loanId, userData.UserId);

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

            //set data source to report viwer
            rptViewerTitleStatus.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));
            rptViewerTitleStatus.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", units));
        }
        /// <summary>
        /// Frontend Page: Pay Curtailment Page(Curtailment Receipt Report)
        /// Title: Display Curtailment Receipt report during session
        /// Designed: Kanishka SHM
        /// User story: 
        /// Developed: Kanishka SHM
        /// Date created: 
        /// </summary>
        public void RenderReport(int loanId)
        {
            //Check authentication session is null then return
            if (Session["AuthenticatedUser"] == null) return;
            User userData = (User)Session["AuthenticatedUser"];

            //set reportviewr properties
            rptViewerCurtailmentReceiptDuringSession.ProcessingMode = ProcessingMode.Local;
            rptViewerCurtailmentReceiptDuringSession.Reset();
            rptViewerCurtailmentReceiptDuringSession.LocalReport.EnableExternalImages = true;
            rptViewerCurtailmentReceiptDuringSession.LocalReport.ReportPath = Server.MapPath("~/Reports/RptCurtailmentDuringSession.rdlc");
            rptViewerCurtailmentReceiptDuringSession.ZoomMode = ZoomMode.PageWidth;

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

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

            //Set data set to report
            rptViewerCurtailmentReceiptDuringSession.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));

            //get unit list from session
            List<CurtailmentShedule> selectedCurtailmentSchedules =
                (List<CurtailmentShedule>) Session["CurtUnitDuringSession"];

            //assign unit ids
            List<string> uniList = (from item in selectedCurtailmentSchedules
                                    select item.UnitId).Distinct().ToList();

            //create xml string
            XElement xEle = new XElement("Curtailments",
                    from id in uniList
                    select new XElement("Unit",
                        new XElement("UnitId", id)
                        ));
            string xmlDoc = xEle.ToString();

            //get unit curatilment details during session
            List<CurtailmentShedule> unitWithAdvanceAmount = ra.GetCurtailmentPaidDetailsDuringSession(xmlDoc);

            foreach (var item in selectedCurtailmentSchedules)
            {
                item.PurchasePrice = unitWithAdvanceAmount.First(x => x.UnitId == item.UnitId).PurchasePrice;
            }

            if (selectedCurtailmentSchedules != null && selectedCurtailmentSchedules.Count > 0)
            {
                //Set data set to report
                rptViewerCurtailmentReceiptDuringSession.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", selectedCurtailmentSchedules));
            }
        }
        /// <summary>
        /// Frontend Page: Advance Unit Page(Advance Unit Report)
        /// Title: Display Advance Units during session
        /// Designed: Kanishka SHM
        /// User story: 
        /// Developed: Kanishka SHM
        /// Date created: 
        /// </summary>
        public void RenderReport(int loanId)
        {
            //Check authentication session is null then return
            if (Session["AuthenticatedUser"] == null) return;
            User userData = (User)Session["AuthenticatedUser"];

            //set reportviewr properties
            rptViewerAdvanceUnit.ProcessingMode = ProcessingMode.Local;
            rptViewerAdvanceUnit.Reset();
            rptViewerAdvanceUnit.LocalReport.EnableExternalImages = true;
            rptViewerAdvanceUnit.LocalReport.ReportPath = Server.MapPath("~/Reports/RptAdvanceUnit.rdlc");
            rptViewerAdvanceUnit.ZoomMode = ZoomMode.PageWidth;
            List<Unit> units = (List<Unit>)Session["AdvItems"];

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

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

            //Set data set to report
            rptViewerAdvanceUnit.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));

            //if unit count is not null and count is greater than 0, create xml string
            if (units != null && units.Count > 0)
            {
                try
                {
                    //create xml string
                    XElement xEle = new XElement("Units",
                    from unit in units
                    select new XElement("Unit",
                        new XElement("UnitId", unit.UnitId)
                        ));
                    string xmlDoc = xEle.ToString();
                    
                    //get all advance unit during session
                    var advanceUnits = ra.AdvanceUnitsDuringSession(xmlDoc);

                    //Set data set to report
                    rptViewerAdvanceUnit.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", advanceUnits));
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            else
            {
                units = new List<Unit>();
            }
        }
        /*

            Frontend page: Report Page
            Title: Load pdf view on browser
            Designed: Kanishka SHM
            User story: 
            Developed: Kanishka SHM
            Date created: 

        */
        public ReportViewer PrintPage(int loanId)
        {
            //check authentication session is null, if null return
            if (Session["AuthenticatedUser"] == null) return null;
            User userData = (User)Session["AuthenticatedUser"];

            //set report viewr property dynamically
            ReportViewer rptViewerFullInventoryPrint = new ReportViewer();
            rptViewerFullInventoryPrint.ProcessingMode = ProcessingMode.Local;
            rptViewerFullInventoryPrint.Reset();
            rptViewerFullInventoryPrint.LocalReport.EnableExternalImages = true;
            rptViewerFullInventoryPrint.LocalReport.ReportPath = Server.MapPath("~/Reports/RptFullInventory.rdlc");

            //get report header details
            ReportAccess ra = new ReportAccess();
            List<LoanDetailsRpt> details = ra.TopHeaderDetails(loanId, userData.UserId);

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

            //Get unit details with payment details
            List<ReportFullInventoryUnit> units = ra.GetFullInventoryByLoanId(loanId);

            //set data source to report viwer
            rptViewerFullInventoryPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));
            rptViewerFullInventoryPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", units));
            
            //return report view
            return rptViewerFullInventoryPrint;
        }
        public ReportViewer PrintPage(int loanId, string idNumber)
        {
            //Check authentication session is null then return
            if (Session["AuthenticatedUser"] == null) return null;
            User userData = (User)Session["AuthenticatedUser"];

            ReportViewer rptViewerIndividualCurtailmentSummaryPrint = new ReportViewer();

            rptViewerIndividualCurtailmentSummaryPrint.ProcessingMode = ProcessingMode.Local;
            rptViewerIndividualCurtailmentSummaryPrint.Reset();
            rptViewerIndividualCurtailmentSummaryPrint.LocalReport.EnableExternalImages = true;
            rptViewerIndividualCurtailmentSummaryPrint.LocalReport.ReportPath = Server.MapPath("~/Reports/RptIndividualCurtailmentSummary.rdlc");
            rptViewerIndividualCurtailmentSummaryPrint.ZoomMode = ZoomMode.PageWidth;

            ReportAccess ra = new ReportAccess();
            List<LoanDetailsRpt> details = ra.TopHeaderDetails(loanId, userData.UserId);
            foreach (var dates in details)
            {

                dates.ReportDate = DateTime.Now.ToString("MM/dd/yyyy");
            }

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

            //Get all curtailment summary details
            List<RptIndividualCurtailmentSummary> indiCurtailment = ra.GetIndividualCurtailmentSummarRptDetails(loanId, idNumber);
            rptViewerIndividualCurtailmentSummaryPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", indiCurtailment));

            return rptViewerIndividualCurtailmentSummaryPrint;
        }
        /*

            Frontend page: Report Page
            Title: Lot Inspection Report print page
            Designed: Kanishka SHM
            User story: 
            Developed: Kanishka SHM
            Date created: 

        */
        public ReportViewer PrintPage(int loanId)
        {
            //check authentication session is null, if null return
            if (Session["AuthenticatedUser"] == null) return null;
            User userData = (User)Session["AuthenticatedUser"];

            //set report viewr property dynamically
            ReportViewer rptViewerLotInspectionPrint = new ReportViewer();
            rptViewerLotInspectionPrint.ProcessingMode = ProcessingMode.Local;
            rptViewerLotInspectionPrint.Reset();
            rptViewerLotInspectionPrint.LocalReport.EnableExternalImages = true;
            rptViewerLotInspectionPrint.LocalReport.ReportPath = Server.MapPath("~/Reports/RptLotInspection.rdlc");

            //get report header details
            ReportAccess ra = new ReportAccess();
            List<LoanDetailsRpt> details = ra.TopHeaderDetails(loanId, userData.UserId);

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

            //set data source to report viwer - 1
            rptViewerLotInspectionPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));

            //get all active units
            List<ReportUnitModels> units = ra.GetAllActiveUnitDetailsRpt(loanId);

            foreach (var unit in units)
            {
                unit.View = false;
            }

            //set data source to report viwer - 2
            rptViewerLotInspectionPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", units));

            //return report viwer
            return rptViewerLotInspectionPrint;
        }
        /*

        Frontend page   : Report page
        Title           : Display Delete units report print page
        Designed        : Kanishka SHM
        User story      : 
        Developed       : Kanishka SHM
        Date created    : 06/23/2016

        */
        public ReportViewer PrintPage(int loanId, DateTime startDate, DateTime endDate)
        {
            //Check authentication session is null then return
            if (Session["AuthenticatedUser"] == null) return null;
            User userData = (User)Session["AuthenticatedUser"];

            //create reportviwer & set reportviewr properties
            ReportViewer rptViewerDeletedUnitsPrint = new ReportViewer();
            rptViewerDeletedUnitsPrint.ProcessingMode = ProcessingMode.Local;
            rptViewerDeletedUnitsPrint.Reset();
            rptViewerDeletedUnitsPrint.LocalReport.EnableExternalImages = true;
            rptViewerDeletedUnitsPrint.LocalReport.ReportPath = Server.MapPath("~/Reports/RptDeletedUnits.rdlc");
            rptViewerDeletedUnitsPrint.ZoomMode = ZoomMode.PageWidth;

            //Get details of loan
            ReportAccess ra = new ReportAccess();
            List<LoanDetailsRpt> details = ra.TopHeaderDetails(loanId, userData.UserId);

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

            //set data set to report
            rptViewerDeletedUnitsPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));

            //Get all delete units details and set data set to report
            List<RptDeletedUnit> deletedUnits = ra.RptGetDeletedUnitByLoanIdDateRange(loanId, startDate, endDate);
            rptViewerDeletedUnitsPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", deletedUnits));

            //return report viwer
            return rptViewerDeletedUnitsPrint;
        }
        /*

            Frontend page: Report Page
            Title: Load details to report and show on browser
            Designed: Kanishka SHM
            User story: 
            Developed: Kanishka SHM
            Date created: 

        */
        public ReportViewer PrintPage(int loanId, DateTime startDate, DateTime endDate)
        {
            //check authentication session is null, if null return
            if (Session["AuthenticatedUser"] == null) return null;
            User userData = (User)Session["AuthenticatedUser"];

            //set report viewr property dynamically
            ReportViewer rptViewerPayOffPrint = new ReportViewer();
            rptViewerPayOffPrint.ProcessingMode = ProcessingMode.Local;
            rptViewerPayOffPrint.Reset();
            rptViewerPayOffPrint.LocalReport.EnableExternalImages = true;
            rptViewerPayOffPrint.LocalReport.ReportPath = Server.MapPath("~/Reports/RptPayOff.rdlc");

            //get report header details
            ReportAccess ra = new ReportAccess();
            List<LoanDetailsRpt> details = ra.TopHeaderDetails(loanId, userData.UserId);

            //add dates, date range and current date
            foreach (var dates in details)
            {
                dates.StartRange = startDate.ToString("MM/dd/yyyy");
                dates.EndRange = endDate.ToString("MM/dd/yyyy");
                dates.ReportDate = DateTime.Now.ToString("MM/dd/yyyy");
            }

            //get unit payoff details
            List<ReportPayOff> curtailments = ra.GetPayOffDetailsByLoanId(loanId, startDate, endDate);

            //set data source to report viwer
            rptViewerPayOffPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));
            rptViewerPayOffPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", curtailments));

            //return report viwer
            return rptViewerPayOffPrint;
        }
        //[HttpPost]
        public FileResult PrintPage()
        {
            if (Session["curtLaonId"] == null || Session["curtLaonId"].ToString() == "")
            {
                return null;
            }

            Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string filenameExtension;
            ReportViewer rptViewerPrint = new ReportViewer();

            int loanId = (int) Session["curtLaonId"];
            //RptDivCUrtailmentReceiptDuringSession curtThisSession = new RptDivCUrtailmentReceiptDuringSession();
            //return curtThisSession.PrintPage(loanId);


            ReportViewer rptViewerCurtailmentReceiptDuringSessionPrint = new ReportViewer();
            rptViewerCurtailmentReceiptDuringSessionPrint.ProcessingMode = ProcessingMode.Local;
            rptViewerCurtailmentReceiptDuringSessionPrint.Reset();
            rptViewerCurtailmentReceiptDuringSessionPrint.LocalReport.EnableExternalImages = true;
            rptViewerCurtailmentReceiptDuringSessionPrint.LocalReport.ReportPath = Server.MapPath("~/Reports/RptCurtailmentDuringSession.rdlc");

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

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

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

            List<CurtailmentShedule> selectedCurtailmentSchedules =
                (List<CurtailmentShedule>)Session["CurtUnitDuringSession"];

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

            var bytes = rptViewerCurtailmentReceiptDuringSessionPrint.LocalReport.Render(
                "PDF", null, out mimeType, out encoding, out filenameExtension,
                out streamids, out warnings);

            var fsResult = File(bytes, "application/pdf");
            return fsResult;
        }
        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;
        }
        /// <summary>
        /// Frontend Page: Report page(Advance Fee invoice Report)
        /// Title: Display Advance Fee invoice
        /// Designed: Kasun Samarawickrama
        /// User story: 
        /// Developed: Kasun Samarawickrama
        /// Date created: 
        /// </summary>
        public ReportViewer PrintPage(int loanId, DateTime startDate, DateTime endDate)
        {
            //Check authentication session is null then return
            if (Session["AuthenticatedUser"] == null) return null;
            User userData = (User)Session["AuthenticatedUser"];

            ReportViewer rptViewerAdvanceFeeInvoicePrint = new ReportViewer();
            rptViewerAdvanceFeeInvoicePrint.ProcessingMode = ProcessingMode.Local;
            rptViewerAdvanceFeeInvoicePrint.Reset();
            rptViewerAdvanceFeeInvoicePrint.LocalReport.EnableExternalImages = true;
            rptViewerAdvanceFeeInvoicePrint.LocalReport.ReportPath = Server.MapPath("~/Reports/RptAdvanceFeeInvoice.rdlc");

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

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

            //Get Advance Fee invoice details during time period
            List<RptFee> advanceFeeInvoice = ra.GetFeeInvoiceByDateRange(loanId, "advanceFee", startDate, endDate);

            //Set data set to report
            rptViewerAdvanceFeeInvoicePrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));
            rptViewerAdvanceFeeInvoicePrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", advanceFeeInvoice));

            //return reportviwer
            return rptViewerAdvanceFeeInvoicePrint;
        }
        /// <summary>
        /// Frontend Page: Add Unit(Add Unit Report)
        /// Title: Display Advance Units print page
        /// Designed: Kanishka SHM
        /// User story: 
        /// Developed: Kanishka SHM
        /// Date created: 
        /// </summary>
        public ReportViewer PrintPage(int loanId, int userId)
        {
            //Check authentication session is null then return
            if (Session["AuthenticatedUser"] == null) return null;
            User userData = (User)Session["AuthenticatedUser"];

            ReportViewer rptViewerAddUnitPrint = new ReportViewer();

            rptViewerAddUnitPrint.ProcessingMode = ProcessingMode.Local;
            rptViewerAddUnitPrint.Reset();
            rptViewerAddUnitPrint.LocalReport.EnableExternalImages = true;
            rptViewerAddUnitPrint.LocalReport.ReportPath = Server.MapPath("~/Reports/RptAddUnit.rdlc");
            rptViewerAddUnitPrint.ZoomMode = ZoomMode.PageWidth;

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

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

            //Set data set to report
            rptViewerAddUnitPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));

            //
            List<RptAddUnit> units = ra.GetJustAddedUnitDetails(userId, loanId);

            //Set data set to report
            rptViewerAddUnitPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", units));

            //return reportviwer
            return rptViewerAddUnitPrint;
        }
        /// <summary>
        /// Frontend Page: Pay Curtailment Page(Curtailment Receipt Report)
        /// Title: Display Curtailment Receipt print page
        /// Designed: Kanishka SHM
        /// User story: 
        /// Developed: Kanishka SHM
        /// Date created: 
        /// </summary>
        public ReportViewer PrintPage(int loanId)
        {
            //Check authentication session is null then return
            if (Session["AuthenticatedUser"] == null) return null;
            User userData = (User)Session["AuthenticatedUser"];

            //create reportviwer & set reportviewr properties
            ReportViewer rptViewerCurtailmentReceiptDuringSessionPrint = new ReportViewer();
            rptViewerCurtailmentReceiptDuringSessionPrint.ProcessingMode = ProcessingMode.Local;
            rptViewerCurtailmentReceiptDuringSessionPrint.Reset();
            rptViewerCurtailmentReceiptDuringSessionPrint.LocalReport.EnableExternalImages = true;
            rptViewerCurtailmentReceiptDuringSessionPrint.LocalReport.ReportPath = Server.MapPath("~/Reports/RptCurtailmentDuringSession.rdlc");

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

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

            //Set data set to report
            rptViewerCurtailmentReceiptDuringSessionPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));

            //get unit curatilment details during session
            List<CurtailmentShedule> selectedCurtailmentSchedules =
                (List<CurtailmentShedule>)Session["CurtUnitDuringSession"];

            if (selectedCurtailmentSchedules != null && selectedCurtailmentSchedules.Count > 0)
            {
                rptViewerCurtailmentReceiptDuringSessionPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", selectedCurtailmentSchedules));
            }
            
            return rptViewerCurtailmentReceiptDuringSessionPrint;
        }
        public ReportViewer PrintPage(int loanId, DateTime startDate, DateTime endDate)
        {
            //check authentication session is null, if null return
            if (Session["AuthenticatedUser"] == null) return null;
            User userData = (User)Session["AuthenticatedUser"];

            //set report viewr property dynamically
            ReportViewer rptViewerLoanSummaryPrint = new ReportViewer();
            rptViewerLoanSummaryPrint.ProcessingMode = ProcessingMode.Local;
            rptViewerLoanSummaryPrint.Reset();
            rptViewerLoanSummaryPrint.LocalReport.EnableExternalImages = true;
            rptViewerLoanSummaryPrint.LocalReport.ReportPath = Server.MapPath("~/Reports/RptTransactionHistory.rdlc");

            //get report header details
            ReportAccess ra = new ReportAccess();
            List<LoanDetailsRpt> details = ra.TopHeaderDetails(loanId, userData.UserId);

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


            List<ReportTransactionHistory> loanSumm = ra.GetTransactionHistoryByDateRange(loanId, startDate, endDate);

            rptViewerLoanSummaryPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", details));
            rptViewerLoanSummaryPrint.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", loanSumm));

            return rptViewerLoanSummaryPrint;
        }