private void WriteLoanStatusHTML(int loanID, DateTime dtAsOf, System.IO.StreamWriter sw, List <double> totals, string rowIDName)
        {
            clsEntity titleHolder;
            clsEntity coBorrower;
            clsEntity titleCompany;
            int       totalsIndex = 0;
            double    value;

            clsLoan loan = new clsLoan(loanID).LoanAsOf(dtAsOf);

            titleHolder  = new clsEntity(loan.TitleHolderID());
            titleCompany = new clsEntity(loan.TitleCompanyID());
            coBorrower   = new clsEntity(loan.CoBorrowerID());
            clsLoan.State eStatus = loan.Status();

            sw.WriteLine("<tr ID=" + rowIDName + ">");

            sw.Write("<td align=\"left\">" + loan.Property().Address() + "</td>");
            if (eStatus.ToString().Length > 10)
            {
                sw.Write("<td align=\"left\">" + eStatus.ToString().ToUpper().Substring(0, 10) + "</td>");
            }
            else
            {
                sw.Write("<td align=\"left\">" + eStatus.ToString().ToUpper() + "</td>");
            }

            if (eStatus == clsLoan.State.Cancelled)
            {
                for (int i = 0; i < 16; i++)
                {
                    sw.Write("<td></td>");
                    totalsIndex++;
                }
            }
            else
            {
                if (loan.Status() == clsLoan.State.Sold)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        sw.Write("<td></td>");
                        totalsIndex++;
                    }
                    if (loan.AccruedAdditionalInterest(dtAsOf) > 0D)
                    {
                        value = loan.AccruedAdditionalInterest(dtAsOf);
                        totals[totalsIndex] += value;
                        sw.Write("<td align=\"right\">" + value + "</td>");
                    }
                    for (int i = 0; i < 3; i++)
                    {
                        sw.Write("<td></td>");
                        totalsIndex++;
                    }
                    value = loan.PrincipalPaid(dtAsOf);
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    value = loan.InterestPaid(dtAsOf);
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    value = loan.PointsPaid(dtAsOf);
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    value = loan.AdditionalInterestPaid(dtAsOf);
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    value = loan.Return(false);
                    sw.Write("<td align=\"right\">" + value.ToString("#0.00%") + "</td>");
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    value = loan.IRR(false);
                    sw.Write("<td align=\"right\">" + value.ToString("#0.00%") + "</td>");
                    totals[totalsIndex] += value;
                    totalsIndex++;
                }
                else if (loan.Status() == clsLoan.State.PartiallySold)
                {
                    value = loan.Balance(dtAsOf);
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    value = loan.AccruedInterest(dtAsOf);
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    value = loan.ProjectedHardInterest();
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    value = loan.AccruedAdditionalInterest(dtAsOf);
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    sw.Write("<td align=\"right\">" + value + "</td>");
                    value = loan.Return(false);
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    sw.Write("<td align=\"right\">" + value.ToString("#0.00%") + "</td>");
                    value = loan.IRR(false);
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    sw.Write("<td align=\"right\">" + value.ToString("#0.00%") + "</td>");
                    value = loan.PrincipalPaid(dtAsOf);
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    value = loan.InterestPaid(dtAsOf);
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    value = loan.PointsPaid(dtAsOf);
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    value = loan.AdditionalInterestPaid(dtAsOf);
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    value = loan.Return(false);
                    sw.Write("<td align=\"right\">" + value.ToString("#0.00%") + "</td>");
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    value = loan.IRR(false);
                    sw.Write("<td align=\"right\">" + value.ToString("#0.00%") + "</td>");
                    totals[totalsIndex] += value;
                    totalsIndex++;
                }
                else
                {
                    value = loan.Balance(dtAsOf);
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    value = loan.AccruedInterest(dtAsOf);
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    value = loan.ProjectedHardInterest();
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    value = loan.ProjectedAdditionalInterest(dtAsOf);
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    value = loan.Return(false);
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    sw.Write("<td align=\"right\">" + value.ToString("#0.00%") + "</td>");
                    value = loan.IRR(false);
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    sw.Write("<td align=\"right\">" + value.ToString("#0.00%") + "</td>");
                    for (int i = 0; i < 2; i++)
                    {
                        sw.Write("<td></td>");
                        totalsIndex++;
                    }
                    value = loan.PointsPaid(dtAsOf);
                    sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                    totals[totalsIndex] += value;
                    totalsIndex++;
                    for (int i = 0; i < 3; i++)
                    {
                        sw.Write("<td></td>");
                        totalsIndex++;
                    }
                }
                value = loan.RehabRemain(dtAsOf);
                totals[totalsIndex] += value;
                totalsIndex++;
                sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                value = loan.RehabSpent(dtAsOf);
                totals[totalsIndex] += value;
                totalsIndex++;
                sw.Write("<td align=\"right\">" + value.ToString("#,##0.00") + "</td>");
                sw.Write("<td align=\"right\">" + loan.SaleDate().ToShortDateString() + "</td>");
                sw.Write("<td align=\"right\">" + loan.OriginationDate().ToShortDateString() + "</td>");
                sw.Write("<td align=\"right\">" + (loan.SaleDate() - loan.OriginationDate()).TotalDays.ToString() + "</td>");
            }

            sw.Write("<td>" + titleHolder.Name() + "</td>");
            sw.Write("<td>" + coBorrower.Name() + "</td>");
            sw.Write("<td>" + titleCompany.Name() + "</td>");
            sw.Write("<td>" + loan.Rate().ToString("#0.00%") + "</td>");
            sw.Write("<td>" + loan.PenaltyRate().ToString("#0.00%") + "</td>");
            sw.Write("<td>" + (loan.Points() * 0.01).ToString("#0.00%") + "</td>");
            sw.Write("<td>" + loan.ProfitSplit().ToString("#0.00%") + "</td>");
            sw.Write("<td>" + loan.OriginationDate().ToShortDateString() + "</td>");
            sw.Write("<td>" + loan.MaturityDate().ToShortDateString() + "</td>");
            sw.Write("<td>" + loan.GrossReturn(false).ToString("#0.00%") + "</td>");
            sw.Write("<td>" + loan.GrossReturn(true).ToString("#0.00%") + "</td>");
            sw.Write("<td>" + loan.Return(true).ToString("#0.00%") + "</td>");
            sw.Write("<td>" + loan.IRR(true).ToString("#0.00%") + "</td>");
            sw.Write("<td>" + loan.FirstRehabEstimate().ToString("#,##0.00") + "</td>");
            sw.WriteLine();

            sw.WriteLine("</tr>");
            sw.Flush();
        }
        private void CalculateAggregates(DateTime startDate, DateTime endDate)
        {
            // Balances:  Total made, less each quarters repayment, adding to Net Balance
            double dTotalLent        = 0D;
            double dRepaidPeriod     = 0D;
            double dRepaidPreviously = 0D;
            double dNetBalance       = 0D;
            double dTotalCommitted   = 0D;
            double dTotalRehabRemain = 0D;
            double dSaleContracts    = 0D;
            double dSaleListings     = 0D;
            double dPendingAcq       = 0D;
            double dPartialBalance   = 0D;

            // Accrued:   Total accrued FTD, less this period payments, less prior payments, (=accrued end of period),
            //            less accrued end of prev period,  adding to net accrued this period
            //            (+) paid this period (+) additional interest paid this period (+) add'l accrued this period = Net Income Period
            double dHardInterestPaidThisPeriod              = 0D;
            double dHardInterestPaidPreviously              = 0D;
            double dAccruedThisPeriod                       = 0D;
            double dAdditionalInterestPaidThisPeriod        = 0D;
            double dAdditionalInterestAccruedThisPeriod     = 0D;
            double dAdditionalInterestAccruedPreviousPeriod = 0D;
            double dPointsPaidThisPeriod                    = 0D;
            double dAccruedFTD            = 0D;
            double dAccruedNetAtStartDate = 0D;

            // Loan-by-loan:
            //            (+) Total Accrual FTD (+) interest paid this period (-) interest paid previous periods
            //            (+) Additional Paid this period (+) additional accrued this period (-) additional accrued last period
            int iStateCount = Enum.GetNames(typeof(clsLoan.State)).Length;

            int[,] iLoansByStatus = new int[iStateCount, 2];
            int[] iUncancelledCount = new int[2];
            for (int i = 0; i < iStateCount; i++)
            {
                iLoansByStatus[i, 0] = iLoansByStatus[i, 1] = 0;
            }

            // loop through loans
            clsCSVTable tbl = new clsCSVTable(clsLoan.strLoanPath);

            for (int i = 0; i < tbl.Length(); i++)
            {
                if (this.lenderLoanIDs.Contains(i))
                {
                    clsLoan loan            = new clsLoan(i);
                    clsLoan loanAsOfEndDate = loan.LoanAsOf(endDate);
                    if (loan.FindDate(clsCashflow.Type.AcquisitionPrice, true, false) <= endDate)
                    {
                        iLoansByStatus[(int)loan.LoanAsOf(startDate).Status(), 0]++;
                        iLoansByStatus[(int)loanAsOfEndDate.Status(), 1]++;
                        dTotalLent                               += loan.Balance(endDate) + loan.PrincipalPaid(endDate);
                        dTotalRehabRemain                        += loanAsOfEndDate.RehabRemain(endDate);
                        dRepaidPeriod                            += loan.PrincipalPaid(endDate) - loan.PrincipalPaid(startDate);
                        dRepaidPreviously                        += loan.PrincipalPaid(startDate);
                        dHardInterestPaidPreviously              += loan.HardInterestPaid(startDate);
                        dHardInterestPaidThisPeriod              += loan.HardInterestPaid(endDate) - loan.HardInterestPaid(startDate);
                        dPointsPaidThisPeriod                    += loan.PointsPaid(endDate) - loan.PointsPaid(startDate);
                        dAccruedThisPeriod                       += loan.AccruedInterest(endDate) - loan.AccruedInterest(startDate);
                        dAdditionalInterestPaidThisPeriod        += loan.AdditionalInterestPaid(endDate) - loan.AdditionalInterestPaid(startDate);
                        dAdditionalInterestAccruedThisPeriod     += loan.AccruedAdditionalInterest(endDate);   // needs fixing
                        dAdditionalInterestAccruedPreviousPeriod += loan.AccruedAdditionalInterest(startDate); // needs fixing
                        dAccruedFTD                              += loan.AccruedInterest(endDate) + loan.HardInterestPaid(endDate);
                        dAccruedNetAtStartDate                   += loan.AccruedInterest(startDate);
                        if (loanAsOfEndDate.Status() == clsLoan.State.Listed)
                        {
                            dSaleListings += loan.Balance(endDate);
                        }
                        if (loanAsOfEndDate.Status() == clsLoan.State.PendingSale)
                        {
                            dSaleContracts += loan.Balance(endDate);
                        }
                        if (loanAsOfEndDate.Status() == clsLoan.State.PendingAcquisition)
                        {
                            dPendingAcq += loan.AcquisitionCost(false);
                        }
                        if (loanAsOfEndDate.Status() == clsLoan.State.PartiallySold)
                        {
                            dPartialBalance += loan.Balance(endDate);
                        }
                    }
                }
            }
            dTotalCommitted = dTotalLent + dTotalRehabRemain + dPendingAcq;
            dNetBalance     = dTotalLent - dRepaidPeriod - dRepaidPreviously;
            for (int i = 0; i < 2; i++)
            {
                iUncancelledCount[i]  = iLoansByStatus[(int)clsLoan.State.Listed, i];
                iUncancelledCount[i] += iLoansByStatus[(int)clsLoan.State.PendingAcquisition, i];
                iUncancelledCount[i] += iLoansByStatus[(int)clsLoan.State.PendingSale, i];
                iUncancelledCount[i] += iLoansByStatus[(int)clsLoan.State.Rehab, i];
                iUncancelledCount[i] += iLoansByStatus[(int)clsLoan.State.Sold, i];
                iUncancelledCount[i] += iLoansByStatus[(int)clsLoan.State.PartiallySold, i];
            }
            int iRepaidCount      = iLoansByStatus[(int)clsLoan.State.Sold, 1] - iLoansByStatus[(int)clsLoan.State.Sold, 0];
            int iOutstandingCount = iUncancelledCount[1] - iLoansByStatus[(int)clsLoan.State.Sold, 1];

            // Display Results
            this.ReportSummaryTextLabel.StringValue = "";

            this.ReportSummaryTextLabel.StringValue += " Total Committed:    \t" + dTotalCommitted.ToString("00,000,000.00");
            this.ReportSummaryTextLabel.StringValue += "(" + iUncancelledCount[1].ToString("000") + ")" + "\t";

            this.ReportSummaryTextLabel.StringValue += "\n Total Loan Given:   \t" + dTotalLent.ToString("00,000,000.00");
            this.ReportSummaryTextLabel.StringValue += "(" + (iUncancelledCount[1] - iLoansByStatus[(int)clsLoan.State.PendingAcquisition, 1]).ToString("000") + ")" + "\t";
            this.ReportSummaryTextLabel.StringValue += "FTD Accrual:    \t" + dAccruedFTD.ToString("000,000.00");

            this.ReportSummaryTextLabel.StringValue += "\n Repaid Period:     \t" + dRepaidPeriod.ToString("00,000,000.00");
            this.ReportSummaryTextLabel.StringValue += "(" + iRepaidCount.ToString("000") + ")" + "\t";
            this.ReportSummaryTextLabel.StringValue += "(-)Paid Period: \t" + dHardInterestPaidThisPeriod.ToString("000,000.00");

            this.ReportSummaryTextLabel.StringValue += "\n Repaid Previously: \t" + dRepaidPreviously.ToString("00,000,000.00");
            this.ReportSummaryTextLabel.StringValue += "(" + iLoansByStatus[(int)clsLoan.State.Sold, 0].ToString("000") + ")" + "\t";
            this.ReportSummaryTextLabel.StringValue += "(-)Paid Prev:   \t" + dHardInterestPaidPreviously.ToString("000,000.00");

            this.ReportSummaryTextLabel.StringValue += "\n Loans Outstanding: \t" + dNetBalance.ToString("00,000,000.00");
            this.ReportSummaryTextLabel.StringValue += "(" + iOutstandingCount.ToString("000") + ")" + "\t";
            this.ReportSummaryTextLabel.StringValue += "Net Accrued:    \t" + (dAccruedFTD - dHardInterestPaidThisPeriod - dHardInterestPaidPreviously).ToString("000,000.00");

            this.ReportSummaryTextLabel.StringValue += "\n                    \t                   \t";
            this.ReportSummaryTextLabel.StringValue += "(-)Start Accr:  \t" + dAccruedNetAtStartDate.ToString("000,000.00");

            this.ReportSummaryTextLabel.StringValue += "\n Sale Contracts     \t" + dSaleContracts.ToString("00,000,000.00");
            this.ReportSummaryTextLabel.StringValue += "(" + iLoansByStatus[(int)clsLoan.State.PendingSale, 1].ToString("000") + ")" + "\t";
            this.ReportSummaryTextLabel.StringValue += "Period Accr:    \t" + (dAccruedFTD - dHardInterestPaidThisPeriod - dHardInterestPaidPreviously - dAccruedNetAtStartDate).ToString("000,000.00");

            this.ReportSummaryTextLabel.StringValue += "\n Sale Listings      \t" + dSaleListings.ToString("00,000,000.00");
            this.ReportSummaryTextLabel.StringValue += "(" + iLoansByStatus[(int)clsLoan.State.Listed, 1].ToString("000") + ")" + "\t";
            this.ReportSummaryTextLabel.StringValue += "                           \t";
            this.ReportSummaryTextLabel.StringValue += "(+) Addl Paid  : \t" + dAdditionalInterestPaidThisPeriod.ToString("000,000.00");

            this.ReportSummaryTextLabel.StringValue += "\n                    \t                   \t                           \t";
            this.ReportSummaryTextLabel.StringValue += "(+) Addl Accrue: \t" + dAdditionalInterestAccruedThisPeriod.ToString("000,000.00");

            this.ReportSummaryTextLabel.StringValue += "\n                    \t                   \t                           \t";
            this.ReportSummaryTextLabel.StringValue += "(-) Prior Accru: \t" + dAdditionalInterestAccruedPreviousPeriod.ToString("000,000.00");

            this.ReportSummaryTextLabel.StringValue += "\n                    \t                   \t";
            this.ReportSummaryTextLabel.StringValue += "(+) Additional:  \t" + (dAdditionalInterestPaidThisPeriod + dAdditionalInterestAccruedThisPeriod - dAdditionalInterestAccruedPreviousPeriod).ToString("000,000.00");

            this.ReportSummaryTextLabel.StringValue += "\n                    \t                   \t";
            this.ReportSummaryTextLabel.StringValue += "(+) Hard Paid :  \t" + dHardInterestPaidThisPeriod.ToString("000,000.00");

            this.ReportSummaryTextLabel.StringValue += "\n                    \t                   \t";
            this.ReportSummaryTextLabel.StringValue += "(+) Points Paid :\t" + dPointsPaidThisPeriod.ToString("000,000.00");

            this.ReportSummaryTextLabel.StringValue += "\n                    \t                   \t";
            this.ReportSummaryTextLabel.StringValue += "Net Income Per:  \t" + (dAccruedFTD - dHardInterestPaidPreviously - dAccruedNetAtStartDate + dAdditionalInterestPaidThisPeriod + dAdditionalInterestAccruedThisPeriod - dAdditionalInterestAccruedPreviousPeriod + dPointsPaidThisPeriod).ToString("000,000.00");
        }