public LastValueWidgetViewModel(WidgetModel model, TableViewModelFactory vmf,
                                        int?period) : base(model, new LastValueWidgetSettings(model.Properties.ToDictionary(v => v.Key, v => v.Value)))
        {
            _settings = (LastValueWidgetSettings)Settings;
            var vm = vmf.GetVM();

            var column = vm.Headers.First(v =>
                                          v.Provider == _settings.ProviderName &&
                                          (v.AccountName == _settings.AccountName || v.UserFriendlyName == _settings.AccountName));


            var tableRowViewModel = vm.Values.OrderByDescending(v => v.When).FirstOrDefault(v => v.CalculatedCells.GetValueOrDefault(column)?.IsOk == true);

            var matchedCell = tableRowViewModel?.CalculatedCells.GetValueOrDefault(column);

            CurrentValue = matchedCell?.Value;
            CurrentDate  = matchedCell?.Money?.When ?? tableRowViewModel?.When.Date ?? DateTime.MinValue;

            var p = new PercentageCalculator();

            Values = new Dictionary <DateTime, double?>();
            bool first = true;

            foreach (var row in vm.Values.OrderByDescending(v => v.When).Where(v => IsApplicable(v.When, period)))
            {
                var cell = row.CalculatedCells.GetValueOrDefault(column);

                var value = _settings.ExemptTransfers ? cell?.AdjustedValue : cell?.Value;

                if (value == null || double.IsNaN(value.Value))
                {
                    continue;
                }

                p.PushValue(row.When, cell.Value.Value, cell.Adjustment);

                Values[cell.Money?.When ?? row.When.Date] = value;
                IncompleteData |= cell.FailedToResolve.Any();

                if (first)
                {
                    first = false;

                    (Color, Delta) = SetDiffPercenage(cell.DiffPercentage);
                }
            }

            p.Finalize(this);

            IncompleteData |= _settings.NotifyStaleData && (!Values.Any() || Values.Select(v => v.Key).Max() < DateTime.Now.AddHours(-36));

            if (IsCompact)
            {
                Values = null;
            }
        }
Exemplo n.º 2
0
        public LastValueWidgetViewModel(WidgetModel model, ObjectRepository repo) : base(model, new LastValueWidgetSettings(model.Properties.ToDictionary(v => v.Key, v => v.Value)))
        {
            _settings = (LastValueWidgetSettings)Settings;
            try
            {
                var column = repo.Set <MoneyColumnMetadataModel>().First(v =>
                                                                         v.Provider == _settings.ProviderName &&
                                                                         (v.AccountName == _settings.AccountName || v.UserFriendlyName == _settings.AccountName));

                var vm = TableViewModel.GetCachedViewModel(true, false, false, repo);

                Values = new Dictionary <DateTime, double?>();
                bool first = true;
                foreach (var row in vm.Values.OrderByDescending(v => v.When))
                {
                    var cell = row.Cells.FirstOrDefault(v => v.Column == column);
                    if (cell == null)
                    {
                        continue;
                    }

                    Values[row.When] = cell.Value;
                    IncompleteData  |= !cell.Value.HasValue || cell.FailedToResolve.Any();

                    if (first)
                    {
                        first = false;

                        (Color, Delta) = SetDiffPercenage(cell.DiffPercentage);
                    }
                }

                var minValue = Values.OrderBy(v => v.Key).First();
                var maxValue = Values.OrderBy(v => v.Key).Last();

                var dV = (maxValue.Value - minValue.Value) / minValue.Value;
                var dt = (maxValue.Key - minValue.Key).TotalDays;

                var yearDelta = dV / dt * 365.25;

                (ColorYear, DeltaYear) = SetDiffPercenage(yearDelta);
            }
            catch (Exception ex)
            {
                Exception = ex;
            }
        }