Exemplo n.º 1
0
        private ProfileForUpdateVm[] FetchDBProfilesByInvestor(string currentInvestor)
        {
            // to be called by controller

            if (currentInvestor == null || currentInvestor == string.Empty)
            {
                return(null);
            }

            InvestorSvc service = new InvestorSvc(_ctx);

            Data.Entities.Investor investor = service.GetByLogin(currentInvestor);

            IQueryable <ProfileForUpdateVm> joinData = _ctx.Position.Where(p => p.PositionAsset.InvestorId == investor.InvestorId && p.Status == "A")
                                                       .Join(_ctx.Profile, p => p.PositionAsset.Profile.ProfileId,
                                                             pr => pr.ProfileId,
                                                             (position, profile) => new ProfileForUpdateVm
            {
                ProfileId      = profile.ProfileId,
                TickerSymbol   = profile.TickerSymbol,
                DividendFreq   = profile.DividendFreq,
                DividendMonths = profile.DividendMonths,
                DividendPayDay = profile.DividendPayDay
            })
                                                       .OrderBy(results => results.TickerSymbol)
                                                       .AsQueryable();

            return(joinData.ToArray());
        }
Exemplo n.º 2
0
 public ImportFileProcessing(DataImportVm viewModel, PIMS3Context ctx, InvestorSvc investorSvc)
 {
     _viewModel   = viewModel;
     _ctx         = ctx;
     _investorSvc = investorSvc;
 }
Exemplo n.º 3
0
        public IEnumerable <AssetCreationVm> ParsePortfolioSpreadsheetForAssetRecords(string filePath, ImportFileDataProcessing dataAccessComponent, string id)
        {
            List <AssetCreationVm> assetsToCreateList  = new List <AssetCreationVm>();
            var             profileDataAccessComponent = new ProfileDataProcessing(_ctx);
            var             existingProfileId          = string.Empty;
            var             existingAssetClassId       = "6215631D-5788-4718-A1D0-A2FC00A5B1A7";;
            var             newAssetId         = Guid.NewGuid().ToString();
            List <Position> positionsToBeSaved = null;

            try
            {
                string lastTickerProcessed = string.Empty;
                var    importFile          = new FileInfo(filePath);

                using (ExcelPackage package = new ExcelPackage(importFile))
                {
                    ExcelWorksheet  workSheet    = package.Workbook.Worksheets[0];
                    int             totalRows    = workSheet.Dimension.End.Row;
                    int             totalColumns = workSheet.Dimension.End.Column;
                    AssetCreationVm newAsset     = new AssetCreationVm();

                    // Iterate XLS/CSV, ignoring column headings (row 1).
                    for (var rowNum = 2; rowNum <= totalRows; rowNum++)
                    {
                        // Validate XLS
                        var headerRow = workSheet.Cells[1, 1, rowNum, totalColumns].Select(c => c.Value == null ? string.Empty : c.Value.ToString());
                        if (!ValidateFileType(filePath))
                        {
                            return(null);
                        }

                        // Args: Cells[fromRow, fromCol, toRow, toCol]
                        var row                         = workSheet.Cells[rowNum, 1, rowNum, totalColumns].Select(c => c.Value == null ? string.Empty : c.Value.ToString());
                        var enumerableCells             = row as string[] ?? row.ToArray();
                        var positionDataAccessComponent = new PositionDataProcessing(_ctx);

                        // Existing Position-Account implies Profile existence.
                        IQueryable <Asset> positionAccount = positionDataAccessComponent.GetPositionAssetByTickerAndAccount(enumerableCells.ElementAt(1).Trim(),
                                                                                                                            enumerableCells.ElementAt(0).Trim(),
                                                                                                                            id);
                        if (!positionAccount.Any())  // No Position-Account found.
                        {
                            IQueryable <Profile> profilePersisted = null;

                            // Are we processing a different ticker symbol?
                            if (lastTickerProcessed.Trim().ToUpper() != enumerableCells.ElementAt(1).Trim().ToUpper())
                            {
                                lastTickerProcessed = enumerableCells.ElementAt(1).Trim().ToUpper();

                                // Do we first have a standard web-derived Profile (via 3rd party) record in our database?
                                profilePersisted = profileDataAccessComponent.FetchDbProfile(enumerableCells.ElementAt(1).Trim(), "");
                                if (profilePersisted == null)
                                {
                                    // Do we secondly have a Customized Profile (via 3rd party) record in our database?
                                    // Check for lost _investorSvc reference.
                                    if (_investorSvc == null)
                                    {
                                        _investorSvc = new InvestorSvc(_ctx);
                                    }
                                    ;
                                    Investor currentInvestor = _investorSvc.GetById(id);
                                    profilePersisted = profileDataAccessComponent.FetchDbProfile(enumerableCells.ElementAt(1).Trim(), currentInvestor.LoginName);
                                }

                                if (profilePersisted != null)
                                {
                                    // Bypassing Profile creation for new Position.
                                    existingProfileId = profilePersisted.First().ProfileId;

                                    if (assetIdForPosition == string.Empty)
                                    {
                                        assetIdForPosition = newAssetId;
                                    }

                                    // Are we processing our first XLSX Position record?
                                    if (positionsToBeSaved == null)
                                    {
                                        positionsToBeSaved = InitializePositions(new List <Position>(), enumerableCells);
                                    }
                                    else
                                    {
                                        positionsToBeSaved = InitializePositions(positionsToBeSaved, enumerableCells);
                                    }

                                    // Error seeding collection.
                                    if (positionsToBeSaved == null)
                                    {
                                        return(null);
                                    }

                                    assetsToCreateList.Add(new AssetCreationVm
                                    {
                                        AssetId      = Guid.NewGuid().ToString(),
                                        AssetClassId = existingAssetClassId,
                                        InvestorId   = id,
                                        ProfileId    = existingProfileId,
                                        LastUpdate   = DateTime.Now,
                                        Positions    = positionsToBeSaved
                                    });
                                }
                                else
                                {
                                    // Obtain a new Profile via Tiingo API.
                                    var webProfileData = profileDataAccessComponent.BuildProfile(enumerableCells.ElementAt(1).Trim().ToUpper());
                                    if (webProfileData == null)
                                    {
                                        dataAccessComponent._exceptionTickers = enumerableCells.ElementAt(1).Trim().ToUpper();
                                        return(null);  // any necessary logging done via component.
                                    }
                                    else
                                    {
                                        // Dividend freq, months, & payDay all may be edited via 'Asset Profile'
                                        // functionality for any created *customized* Profile only. Any standard
                                        // Profile may NOT be edited this way, as this would impact many investors.
                                        Profile newProfile = new Profile
                                        {
                                            ProfileId     = webProfileData.ProfileId,
                                            DividendYield = webProfileData.DividendYield > 0
                                                ? webProfileData.DividendYield
                                                : 0,
                                            CreatedBy        = null,
                                            DividendRate     = webProfileData.DividendRate > 0 ? webProfileData.DividendRate : 0,
                                            ExDividendDate   = webProfileData.ExDividendDate ?? new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day),
                                            DividendFreq     = webProfileData.DividendFreq ?? "M",
                                            DividendMonths   = null,
                                            DividendPayDay   = 15,
                                            EarningsPerShare = webProfileData.EarningsPerShare > 0
                                                ? webProfileData.EarningsPerShare
                                                : 0,
                                            LastUpdate = DateTime.Now,
                                            PERatio    = webProfileData.PERatio > 0
                                                ? webProfileData.PERatio
                                                : 0,
                                            TickerDescription = webProfileData.TickerDescription.Trim(),
                                            TickerSymbol      = webProfileData.TickerSymbol.ToUpper().Trim(),
                                            UnitPrice         = webProfileData.UnitPrice
                                        };
                                        assetIdForPosition = newAssetId;
                                        assetsToCreateList.Add(new AssetCreationVm
                                        {
                                            AssetId      = Guid.NewGuid().ToString(),
                                            AssetClassId = existingAssetClassId,
                                            InvestorId   = id, //INVESTORID,
                                            ProfileId    = newProfile.ProfileId,
                                            LastUpdate   = DateTime.Now,
                                            Positions    = positionsToBeSaved == null
                                                ? InitializePositions(new List <Position>(), enumerableCells)
                                                : InitializePositions(positionsToBeSaved, enumerableCells),
                                            Profile = newProfile
                                        });
                                        foreach (var asset in assetsToCreateList)
                                        {
                                            if (asset.Positions.Count == 0)
                                            {
                                                Log.Error("Error creating new Position(s) for assetsToCreateList in ImportFileProcessing.ParsePortfolioSpreadsheetForAssetRecords().");
                                                return(null);
                                            }
                                        }
                                        ;
                                    }
                                }
                            }
                            else
                            {
                                // Asset header initialization & Profile check bypassed; processing SAME ticker - DIFFERENT account.
                                // Adding to existing AssetCreationVm.Positions.
                                var updatedPositions = InitializePositions(assetsToCreateList.Last().Positions.ToList(), enumerableCells);
                                assetsToCreateList.Last().Positions.Add(updatedPositions.Last());
                            }
                        }
                        else
                        {
                            // Attempted duplicate Position-Account insertion.
                            lastTickerProcessed = enumerableCells.ElementAt(1).Trim();
                        }
                    }   // end 'for'
                }       // end 'using'
            }
            catch (Exception ex)
            {
                if (ex.Message.Length > 0)
                {
                    Log.Error("Error found within ImportFileProcessing.ParsePortfolioSpreadsheetForAssetRecords(), due to {0}.", ex.Message);
                }
                else
                {
                    Log.Error("Error found within ImportFileProcessing.ParsePortfolioSpreadsheetForAssetRecords().");
                }

                return(null);
            }

            return(assetsToCreateList);
        }
Exemplo n.º 4
0
 public InvestorController(PIMS3Context context, InvestorSvc investorSvc, IOptions <AppSettings> appSettings)
 {
     _context     = context;
     _investorSvc = investorSvc;
     _appSettings = appSettings.Value;
 }