Пример #1
0
        private async Task FillRecurringExpenses(List <PaymentProjectionModel> list, List <DateTime> dates, IEnumerable <PaymentType> types, IEnumerable <CreditCard> cards, BaseFilter filter)
        {
            var currentRecurringExpenses = await _recurringExpenseRepository.GetSome(new BaseFilter()
            {
                UserId = filter.UserId, StartDate = CurrentDate.FixFirstDayInMonth(), EndDate = CurrentDate.FixLastDayInMonth()
            });

            var projectionRecurringExpenses = await _recurringExpenseRepository.GetSome(new BaseFilter()
            {
                UserId = filter.UserId, Active = true
            });

            foreach (var date in dates)
            {
                if (date.SameMonthYear(CurrentDate))
                {
                    var expenses = currentRecurringExpenses.Where(p => p.HasHistory() && p.History.Any(x => date.SameMonthYear(x.Date)));
                    foreach (var item in expenses)
                    {
                        list.Add(new PaymentProjectionModel()
                        {
                            Description = $"{item.Description} (Recorrente)",
                            CreditCard  = cards.FirstOrDefault(p => p.Id == item.CreditCardId),
                            MonthYear   = date.ToString("MM/yyyy"),
                            Type        = types.First(p => p.Id == (int)PaymentTypeEnum.Expense),
                            Value       = item.History.First(p => date.SameMonthYear(p.Date)).PaidValue
                        });
                    }

                    foreach (var item in projectionRecurringExpenses.Where(p => !expenses.Select(x => x.Id).Contains(p.Id)))
                    {
                        list.Add(new PaymentProjectionModel()
                        {
                            Description = $"{item.Description} (Recorrente)",
                            CreditCard  = cards.FirstOrDefault(p => p.Id == item.CreditCardId),
                            MonthYear   = date.ToString("MM/yyyy"),
                            Type        = types.First(p => p.Id == (int)PaymentTypeEnum.Expense),
                            Value       = item.Value
                        });
                    }
                }
                else
                {
                    foreach (var item in projectionRecurringExpenses)
                    {
                        list.Add(new PaymentProjectionModel()
                        {
                            Description = $"{item.Description} (Recorrente)",
                            CreditCard  = cards.FirstOrDefault(p => p.Id == item.CreditCardId),
                            MonthYear   = date.ToString("MM/yyyy"),
                            Type        = types.First(p => p.Id == (int)PaymentTypeEnum.Expense),
                            Value       = item.Value
                        });
                    }
                }
            }
        }
Пример #2
0
        public async Task <ResultModel> Recalculate(int userId, DateTime date, bool force = false)
        {
            var resultModel = new ResultModel();
            var current     = await _remainingBalanceRepository.GetByMonthYear(userId, date);

            if (current != null && !force)
            {
                return(resultModel);
            }

            date = date.AddMonths(-1);
            var filter = new BaseFilter()
            {
                StartDate = new DateTime(date.Year, date.Month, 1).FixStartTimeFilter(),
                EndDate   = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)).FixEndTimeFilter(),
                UserId    = userId
            };

            filter.InactiveTo = filter.EndDate?.FixEndTimeFilter();

            decimal total = 0;

            foreach (var item in (await _earningRepository.GetSome(filter)))
            {
                total += item.Value;
            }

            foreach (var item in (await _vehicleRepository.GetSome(filter)))
            {
                total -= item.FuelExpenses.Sum(p => p.ValueSupplied);
            }

            foreach (var item in (await _householdExpenseRepository.GetSome(filter)))
            {
                total -= item.Value;
            }

            foreach (var item in (await _paymentRepository.GetSome(filter)))
            {
                if (item.Type.In)
                {
                    total += item.Installments.Where(p => p.PaidDate?.SameMonthYear(date) ?? false).Sum(p => p.PaidValue.Value);
                }
                else
                {
                    total -= item.Installments.Where(p => p.PaidDate?.SameMonthYear(date) ?? false).Sum(p => p.PaidValue.Value);
                }
            }

            var lastRemainingBalance = await _remainingBalanceRepository.GetByMonthYear(userId, date);

            if (lastRemainingBalance != null)
            {
                total += lastRemainingBalance.Value;
            }

            foreach (var item in (await _recurringExpenseRepository.GetSome(filter)))
            {
                total -= item.History.First().PaidValue;
            }

            date = date.AddMonths(1);

            if (current != null)
            {
                current.Value = total;
                await _remainingBalanceRepository.Update(current);
            }
            else
            {
                await _remainingBalanceRepository.Add(new Infra.Entity.RemainingBalance()
                {
                    Value  = total,
                    Month  = date.Month,
                    Year   = date.Year,
                    UserId = userId
                });
            }

            return(resultModel);
        }
Пример #3
0
 public async Task <ResultDataModel <IEnumerable <RecurringExpense> > > GetByUser(int userId, bool?active) => new ResultDataModel <IEnumerable <RecurringExpense> >(await _recurringExpenseRepository.GetSome(new BaseFilter()
 {
     UserId = userId, Active = active
 }));