public IActionResult Put([FromBody] AllAccountsOverview allAccounts)
        {
            var userFromAuth = UserService.GetUserFromClaims(this.User, UserRepo, RequestLogger);

            RequestLogger.UserId = userFromAuth.Id.ToString();

            AccountRepo.UpsertAccountChanges(allAccounts, userFromAuth.UserName);
            var newAllAccounts = AccountService.GetAllAccountsOverview(userFromAuth.Id, allAccounts.RelevantYear, allAccounts.RelevantMonth);

            return(Ok(newAllAccounts));
        }
 public void UpsertAccountChanges(AllAccountsOverview allAccounts, string userName)
 {
     foreach (var accountType in allAccounts.AccountTypes)
     {
         if (accountType.IsChildDirty)
         {
             foreach (var accountOverview in accountType.Accounts)
             {
                 if (accountOverview.IsChildDirty || accountOverview.IsDirty)
                 {
                     var account = accountOverview.ExtractDirtyDbModels();
                     UpsertFromEditableModelStates(account, userName);
                 }
             }
         }
     }
 }
        public AllAccountsOverview GetAllAccountsOverview(Guid ownerId, int year, int month)
        {
            var accountsResponse = new AllAccountsOverview
            {
                RelevantMonth = month,
                RelevantYear  = year
            };

            // Get last day of month previous to passed month.
            var lastDayOfLastMonth = DateTimeExtensions.EndOfPreviousMonth(month, year);

            // Get last day of this month.
            var lastDayOfThisMonth = DateTimeExtensions.EndOfMonth(month, year);

            accountsResponse.StartOfMonth = lastDayOfLastMonth.ToBalanceInfo();
            accountsResponse.EndOfMonth   = lastDayOfThisMonth.ToBalanceInfo();

            var accountTypesCollection          = AccountTypeRepository.GetAllAccountTypes();
            var accountsCollection              = AccountRepo.FindAllByOwnerAndMonth(ownerId, year, month);
            var accountCategoryValuesLastMonth  = AccountBalanceRepo.GetAllAccountBalances(ownerId, lastDayOfLastMonth);
            var accountCategoryValuesEndOfMonth = AccountBalanceRepo.GetAllAccountBalances(ownerId, lastDayOfThisMonth);

            var allAccountsFromAllTypes = new List <AccountsOfTypeOverview>();

            foreach (var accountType in accountTypesCollection.OrderBy(m => m.Id))
            {
                var relevantAccountEntities = accountsCollection.Where(m => m.AccountTypeId == accountType.Id);
                if (relevantAccountEntities.Count() > 0)
                {
                    var allAccountsFromType = new List <AccountOverview>();
                    var accountTypeInfo     = new AccountsOfTypeOverview()
                    {
                        Info            = accountType,
                        AreAccountsOpen = false,
                        StartOfMonth    = lastDayOfLastMonth.ToBalanceInfo(),
                        EndOfMonth      = lastDayOfThisMonth.ToBalanceInfo()
                    };
                    allAccountsFromAllTypes.Add(accountTypeInfo);
                    foreach (var accountEntity in relevantAccountEntities.OrderBy(m => m.DisplayIndex))
                    {
                        allAccountsFromType.Add(accountEntity);

                        foreach (var accountCategory in accountEntity.AccountCategories)
                        {
                            var lastMonthBalance = accountCategoryValuesLastMonth.FirstOrDefault(m => m.Id.CompareTo(accountCategory.Id) == 0);
                            if (lastMonthBalance != null)
                            {
                                accountCategory.StartingBalance        = lastMonthBalance.CurrentBalance;
                                accountEntity.StartingBalance         += lastMonthBalance.CurrentBalance;
                                accountTypeInfo.StartOfMonth.Balance  += lastMonthBalance.CurrentBalance;
                                accountsResponse.StartOfMonth.Balance += lastMonthBalance.CurrentBalance;
                            }
                            var endOfMonthBalance = accountCategoryValuesEndOfMonth.FirstOrDefault(m => m.Id.CompareTo(accountCategory.Id) == 0);
                            if (endOfMonthBalance != null)
                            {
                                accountCategory.EndingBalance        = endOfMonthBalance.CurrentBalance;
                                accountEntity.EndingBalance         += endOfMonthBalance.CurrentBalance;
                                accountTypeInfo.EndOfMonth.Balance  += endOfMonthBalance.CurrentBalance;
                                accountsResponse.EndOfMonth.Balance += endOfMonthBalance.CurrentBalance;
                            }
                            accountCategory.Delta = accountCategory.EndingBalance - accountCategory.StartingBalance;
                        }
                        accountEntity.Delta = accountEntity.EndingBalance - accountEntity.StartingBalance;
                    }
                    accountTypeInfo.Delta    = accountTypeInfo.EndOfMonth.Balance - accountTypeInfo.StartOfMonth.Balance;
                    accountTypeInfo.Accounts = allAccountsFromType.ToArray();
                }
            }
            accountsResponse.Delta        = accountsResponse.EndOfMonth.Balance - accountsResponse.StartOfMonth.Balance;
            accountsResponse.AccountTypes = allAccountsFromAllTypes.ToArray();

            return(accountsResponse);
        }