Esempio n. 1
0
        public IQueryable <IncomeReceivablesVm> GetPositionsWithIncomeDue(string investor)
        {
            PositionDataProcessing positionDataAccessComponent = new PositionDataProcessing(_ctx);

            // Get eligible Positions with currently due income, i.e., status = "A" & pymtDue.
            IQueryable <IncomeReceivablesVm> filteredJoinedPositionProfileData = positionDataAccessComponent.GetPositionsForIncomeReceivables(investor);

            // 'tickersWithIncomeDue'          - for read/write UI only.
            // 'currentOverduePositionIncomes' - for Db I/O only.
            int currentMonth = DateTime.Now.Month;

            if (!filteredJoinedPositionProfileData.Any())
            {
                return(null);
            }
            ;

            // Filter Positions for applicable dividend frequency distribution.
            foreach (IncomeReceivablesVm position in filteredJoinedPositionProfileData)
            {
                if (position.DividendFreq == "M")
                {
                    tickersWithIncomeDue.Add(new IncomeReceivablesVm
                    {
                        PositionId      = position.PositionId,
                        TickerSymbol    = position.TickerSymbol,
                        AccountTypeDesc = position.AccountTypeDesc,
                        DividendPayDay  = position.DividendPayDay,
                        DividendFreq    = position.DividendFreq,
                        MonthDue        = DateTime.Now.Month
                    });
                }

                if (position.DividendFreq == "Q" || position.DividendFreq == "S" ||
                    position.DividendFreq == "A")
                {
                    if (CurrentMonthIsApplicable(position.DividendMonths, currentMonth.ToString()))
                    {
                        tickersWithIncomeDue.Add(new IncomeReceivablesVm
                        {
                            PositionId      = position.PositionId,
                            TickerSymbol    = position.TickerSymbol,
                            AccountTypeDesc = position.AccountTypeDesc,
                            DividendPayDay  = position.DividendPayDay,
                            DividendFreq    = position.DividendFreq,
                            MonthDue        = DateTime.Now.Month
                        });
                    }
                }
            }

            // Do we have any persisted delinquencies to add ?
            savedDelinquencies = positionDataAccessComponent.GetSavedDelinquentRecords(investor, "").AsQueryable();

            if (savedDelinquencies.Any())
            {
                foreach (DelinquentIncome delinquentIncome in savedDelinquencies)
                {
                    tickersWithIncomeDue.Add(new IncomeReceivablesVm
                    {
                        PositionId      = delinquentIncome.PositionId,
                        TickerSymbol    = delinquentIncome.TickerSymbol,
                        DividendFreq    = delinquentIncome.DividendFreq,
                        AccountTypeDesc = _ctx.Position.Where(x => x.PositionId == delinquentIncome.PositionId)
                                          .Select(y => y.AccountType.AccountTypeDesc)
                                          .First()
                                          .ToString(),
                        MonthDue = int.Parse(delinquentIncome.MonthDue)
                    });
                }
            }

            return(tickersWithIncomeDue.OrderByDescending(r => r.MonthDue)
                   .ThenBy(r => r.DividendFreq)
                   .ThenBy(r => r.TickerSymbol)
                   .AsQueryable());
        }
Esempio n. 2
0
        public DataImportVm SaveRevenue(DataImportVm importVmToUpdate, PIMS3Context _ctx, string investorId)
        {
            //  Processing includes:
            //  1. persisting revenue,
            //  2. persisting revenue delinquencies, and
            //  3. updating Positions ("PymtDue") where applicable.

            ImportFileProcessing   busLayerComponent = new ImportFileProcessing(importVmToUpdate, _ctx, null);
            IEnumerable <Income>   revenueListingToSave;
            PositionDataProcessing positionDataAccessComponent = new PositionDataProcessing(_ctx);

            if (busLayerComponent.ValidateVm())
            {
                revenueListingToSave = busLayerComponent.ParseRevenueSpreadsheetForIncomeRecords(importVmToUpdate.ImportFilePath.Trim(), this, investorId);

                if (revenueListingToSave == null || revenueListingToSave.Count() == 0)
                {
                    if (!string.IsNullOrEmpty(importVmToUpdate.ExceptionTickers))
                    {
                        importVmToUpdate.MiscMessage = "Error saving revenue for " + importVmToUpdate.ExceptionTickers + ". Check position(s).";
                    }
                    else
                    {
                        importVmToUpdate.MiscMessage = BuildLogMessage("revenue");
                    }

                    return(importVmToUpdate);
                }
                else
                {
                    // Persist to 'Income'. Deferring use of using{}: ctx scope needed.
                    _ctx.AddRange(revenueListingToSave);
                    recordsSaved = _ctx.SaveChanges();
                }

                if (recordsSaved > 0)
                {
                    totalAmtSaved = 0M;
                    foreach (var record in revenueListingToSave)
                    {
                        totalAmtSaved += record.AmountRecvd;
                    }

                    // Returned values to UI.
                    importVmToUpdate.AmountSaved  = totalAmtSaved;
                    importVmToUpdate.RecordsSaved = recordsSaved;

                    // Update Positions.
                    positionDataAccessComponent.UpdatePositionPymtDueFlags(ExtractPositionIdsForPymtDueProcessing(revenueListingToSave), true);

                    /* === Month-end delinquent revenue processing === */

                    // If at month end/beginning, we'll query Positions for any now delinquent income receivables via "PymtDue" flag.
                    // Any unpaid income receivables, those not marked as received via 'Income due', will STILL have their 'PymtDue' flag set as
                    // True, and hence will now be considered a delinquent Position, resulting in persistence to 'DelinquentIncome' table.
                    // Delinquent position searches may ONLY be performed during month-end xLSX processing, as income receipts are still being
                    // accepted during the month via 'Income due'. During 'Income due' payments, flags are set accordingly: [PymtDue : True -> False].
                    // Any delinquent Positions will automatically be added to the 'Income due' collection that is broadcast via the UI schedule,
                    // for necessary action.
                    if (DateTime.Now.Day <= 3 && DateTime.Now.DayOfWeek.ToString() != "Saturday" && DateTime.Now.DayOfWeek.ToString() != "Sunday")
                    {
                        //PositionDataProcessing positionDataAccessComponent = new PositionDataProcessing(_ctx);
                        IQueryable <dynamic>     filteredPositionAssetJoinData = positionDataAccessComponent.GetCandidateDelinquentPositions(investorId);
                        IList <DelinquentIncome> pastDueListing = new List <DelinquentIncome>();
                        string delinquentMonth = DateTime.Now.AddMonths(-1).Month.ToString().Trim();
                        int    matchingPositionDelinquencyCount       = 0;
                        IList <DelinquentIncome> existingDelinqencies = positionDataAccessComponent.GetSavedDelinquentRecords(investorId, "");
                        int duplicateDelinquencyCount = 0;

                        foreach (dynamic joinData in filteredPositionAssetJoinData)
                        {
                            // 'DelinquentIncome' PKs: InvestorId, MonthDue, PositionId
                            // Do we have a match based on PositionId & InvestorId ? If so, Position is eligible for omitting setting 'PymtDue' to True.
                            matchingPositionDelinquencyCount = existingDelinqencies.Where(d => d.PositionId == joinData.PositionId &&
                                                                                          d.InvestorId == joinData.InvestorId).Count();

                            if (matchingPositionDelinquencyCount >= 1)
                            {
                                // Do we have a duplicate entry ?
                                duplicateDelinquencyCount = existingDelinqencies.Where(d => d.PositionId == joinData.PositionId &&
                                                                                       d.InvestorId == joinData.InvestorId &&
                                                                                       d.MonthDue == delinquentMonth).Count();
                            }

                            if (joinData.DividendFreq == "M")
                            {
                                if (matchingPositionDelinquencyCount > 0 && duplicateDelinquencyCount == 0)
                                {
                                    pastDueListing.Add(InitializeDelinquentIncome(joinData));
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                string[] divMonths = joinData.DividendMonths.Split(',');
                                for (var i = 0; i < divMonths.Length; i++)
                                {
                                    if (divMonths[i].Trim() == delinquentMonth)
                                    {
                                        if (matchingPositionDelinquencyCount > 0 && duplicateDelinquencyCount == 0)
                                        {
                                            pastDueListing.Add(InitializeDelinquentIncome(joinData));
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                }
                            }
                            matchingPositionDelinquencyCount = 0;
                        }

                        // Persist to 'DelinquentIncome' as needed.
                        if (pastDueListing.Any())
                        {
                            bool pastDueSaved = positionDataAccessComponent.SaveDelinquencies(pastDueListing.ToList());
                            if (pastDueSaved)
                            {
                                importVmToUpdate.MiscMessage = "Found & stored " + pastDueListing.Count() + " Position(s) with delinquent revenue.";
                                Log.Information("Saved {0} delinquent position(s) to 'DelinquentIncome' via ImportFileDataProcessing.SaveRevenue()", pastDueListing.Count());
                            }
                        }

                        // Finally, update PymtDue flags on received XLSX positions.
                        positionDataAccessComponent.UpdatePositionPymtDueFlags(ExtractPositionIdsForPymtDueProcessing(revenueListingToSave), true);
                    }
                }
                _ctx.Dispose();
            }

            // Missing amount & record count reflects error condition.
            return(importVmToUpdate);
        }