Exemple #1
0
        /// <summary>
        /// Generates a report with estimated payment amounts on a Target Date. Only
        /// Loans that have payments on the Target Date are contemplated. Balances are
        /// calculated using todays date.
        /// </summary>
        /// <param name="financialInstitution">Financial Institution</param>
        /// <param name="targetDate">Target Date to be used for calculation</param>
        /// <param name="startIndex">startIndex</param>
        /// <param name="maxResults">maxResults</param>
        /// <param name="totalRecords">totalRecords</param>
        /// <returns>LoanRecoveryEstimatedRecoveryReport</returns>
        public IList<LoanRecoveryEstimatedRecoveryReport> GetLoanRecoveryEstimatedRecoveryReport(
            string financialInstitution,
            DateTime targetDate,
            int startIndex,
            int maxResults,
            out int totalRecords)
        {
            try
            {
                ILoanRequestDAO loanRequestDAO = _daoFactory.GetLoanRequestDAO ();
                ILoanDAO loanDAO = _daoFactory.GetLoanDAO ();
                IInterestRateDAO interestRateDAO = _daoFactory.GetInterestRateDAO ();
                IInterestRateValueDAO interestRateValueDAO = _daoFactory.GetInterestRateValueDAO ();
                IVatDAO vatDAO = _daoFactory.GetVatDAO ();

                IList<LoanRecoveryEstimatedRecoveryReport> reports
                = loanRequestDAO.GetLoanRecoveryEstimatedRecoveryReport (financialInstitution,
                                                                         targetDate,
                                                                         startIndex,
                                                                         maxResults,
                                                                         out totalRecords);

                /* Make sure we got at least one */
                if (reports.Count <= 0)
                {
                    throw new ZiblerBusinessComponentsException (
                        Resources.ReportOperationsLoanRequestNotExist);
                }

                /* Hash table to store Loan Ids along with their respective InterestRate Ids */
                Hashtable hashLoanIds = new Hashtable ();

                /* Hash table to store unique Interest Rate Ids and the earliest date from
                * where their values are needed. This is to avoid getting the same
                * Interest Rates more than onces if they are repeated. */
                Hashtable hashIntRtDates = new Hashtable ();

                /* List of Interest Rate values for a given Interest Rate Id */
                Hashtable hashIntRtValues = new Hashtable ();

                /* Earliest date from where the VAT values are needed */
                DateTime? vatStart = null;

                /* Loop through the returned report objects and get the
                * Loan Ids, Interest Rates and VAT dates */
                foreach (LoanRecoveryEstimatedRecoveryReport rep in reports)
                {
                    /* Add the loan Id and its Interest Rate Id to the list of
                    * hashLoanIds */
                    if (!hashLoanIds.Contains (rep.LoanId))
                        hashLoanIds.Add (rep.LoanId, rep.InterestRateId);

                    /* Add the Interest Rate Id and the Loan authorization date
                    * to the hashIntRtDates list. This list is used to minimize the number
                    * of queries made to the database if the Interest Rate Id is the
                    * same for multiple loans */
                    if (!hashIntRtDates.Contains (rep.InterestRateId))
                    {
                        hashIntRtDates.Add (rep.InterestRateId, rep.AuthorizationDate);
                    }

                    /*Add the start date only if it is previous to the current one */
                    if (hashIntRtDates[rep.InterestRateId] == null)
                        hashIntRtDates[rep.InterestRateId] = rep.AuthorizationDate;
                    else
                    {
                        /* Get the date associated to the Interest Rate */
                        DateTime currDate = Convert.ToDateTime (hashIntRtDates[rep.InterestRateId]);

                        /* Determine if there is another loan (rep.AuthorizationDate) whose
                        * date is earlier than the previously assigned to the Interest Rate Id.
                        * If so (replace) then use this date as the new data so later we get
                        * all the interest rate values from there */
                        bool replace = DateUtilities.IsFirstDateGreaterThanSecondDate (currDate,
                                                                                       rep.AuthorizationDate);

                        if (replace)
                            hashIntRtDates[rep.InterestRateId] = rep.AuthorizationDate;
                    }

                    /* If we don't have a VAT date yet, use the current AuthorizationDate */
                    if (vatStart == null)
                        vatStart = Convert.ToDateTime (rep.AuthorizationDate);
                    else
                    {
                        /* Determine if the loan has an earlier date than the currently selected for
                        * the VAT. If so, then this date should be used to search for the VAT */
                        bool replace = DateUtilities.IsFirstDateGreaterThanSecondDate (
                            vatStart.Value,
                            rep.AuthorizationDate);

                        if (replace)
                            vatStart = Convert.ToDateTime (rep.AuthorizationDate);
                    }
                }

                /* Verify we have at least one loan Id */
                if (hashLoanIds.Count <= 0)
                    throw new ZiblerBusinessComponentsException (
                        Resources.ReportOperationsLoanRequestWithOutLoans);

                /* Get all the loan Ids from our hash table.  */
                IDictionaryEnumerator enLoanIds = hashLoanIds.GetEnumerator ();

                IList<int> processLoanIds = new List<int> ();
                while (enLoanIds.MoveNext ())
                {
                    /* Add the loan Id to a list of loan Ids which will be used
                    * to retrieved them from the Database */
                    processLoanIds.Add (Convert.ToInt32 (enLoanIds.Key));
                }

                /* Retrieve the Loans from the Database */
                IList<Loan> loans = loanDAO.GetLoan (processLoanIds);

                /* Get all the Interests and their values to be used to calculate
                * the statements for all the loans */
                IDictionaryEnumerator en = hashIntRtDates.GetEnumerator ();
                while (en.MoveNext ())
                {
                    /* Interest Rate needed */
                    int intRateId = Convert.ToInt32 (en.Key);

                    /* Values needed from this date */
                    DateTime dt = Convert.ToDateTime (en.Value);

                    IList<InterestRateValue> list = interestRateValueDAO.GetInterestRateValuesForStatement (
                        intRateId,
                        dt,
                        DateTime.Today);

                    /* If no interest rate value was returned, it means that a value does not exists
                    * for a given Interest Rate Id */
                    if (list == null)
                    {
                        /* Get the interest rate for informational purposes */
                        InterestRate intRate = interestRateDAO.FindById (intRateId);
                        if (intRate != null)
                        {
                            string intRateName = intRate.Name;
                            throw new ZiblerBusinessComponentsException (
                                String.Format (
                                    Resources.ReportOperationsInterestRateDateNotAvailable,
                                    intRateName,
                                    dt.ToShortDateString ()));
                        }
                    }

                    /* Add the values to a hash list and move on */
                    hashIntRtValues.Add (intRateId, list);
                }

                /* Get all the VATs */
                IList<VAT> vats = vatDAO.GetVATsForStatement (financialInstitution,
                                                              vatStart.Value,
                                                              DateTime.Today);

                if (vats.Count <= 0)
                    throw new ZiblerBusinessComponentsException (
                        Resources.ReportOperationsNoVatsAvailable);

                /* Now that we have all the required data, loop through the list of reports one more time
                * and calculate the Statement for each one of them */
                foreach (LoanRecoveryEstimatedRecoveryReport report in reports)
                {
                    /* Calculated the statement on each loan of the report */
                    foreach (Loan loan in loans)
                    {
                        if (report.LoanId == loan.Id)
                        {
                            int loanId = loan.Id;
                            int intValueId = Convert.ToInt32 (hashLoanIds[loanId]);
                            IList<InterestRateValue> intValues = (IList<InterestRateValue>)hashIntRtValues[intValueId];

                            /* Calculate the statement to today's day */
                            LoanStatement stmnt = new LoanStatement (loan,
                                                                     DateTime.Today,
                                                                     vats,
                                                                     intValues);
                            stmnt.GenerateStatement ();

                            report.DelayedDays = stmnt.DelayedDays;
                            report.DelinquentInterests = stmnt.DelinquentInterests;
                            report.DelinquentPrincipal = stmnt.DelinquentPrincipal;
                            report.DelinquentVAT = stmnt.DelinquentVAT;
                            report.DelinquentBalance = stmnt.DelinquentBalance;

                            report.LoanedAmount = stmnt.LoanedAmount;
                            report.LoanType = stmnt.LoanType;
                            report.MoratoryInterests = stmnt.MoratoryInterests;

                            /* Get the payment number on the target date, so we can
                            * determine what the estimated payment will be */
                            report.EstmtdNxtPymnt = stmnt.GetEstimatedPayment (report.PaymentNumber);

                            break;
                        }
                    }
                }

                return reports;
            }
            /* If the exception was thrown here, just pass it up */
            catch (ZiblerBusinessComponentsException ex)
            {
                throw;
            }
            /* Catch any Data Layer or other exception and throw an unkown exception */
            catch (Exception ex)
            {
                ZiblerBusinessComponentsUnknownException exc
                = new ZiblerBusinessComponentsUnknownException (ex);

                /* Throw the new exception */
                throw exc;
            }
        }