internal static void ExtractBudgetInfos(this BudgetAccountModel budgetAccountModel, IReadOnlyCollection <BudgetInfoModel> budgetInfoModelCollection) { NullGuard.NotNull(budgetAccountModel, nameof(budgetAccountModel)) .NotNull(budgetInfoModelCollection, nameof(budgetInfoModelCollection)); budgetAccountModel.BudgetInfos = budgetInfoModelCollection.Where(budgetInfoModel => budgetInfoModel.BudgetAccountIdentifier == budgetAccountModel.BudgetAccountIdentifier).ToList(); }
internal static IBudgetAccount ToDomain(this BudgetAccountModel budgetAccountModel, IAccounting accounting, MapperCache mapperCache, IConverter accountingModelConverter) { NullGuard.NotNull(budgetAccountModel, nameof(budgetAccountModel)) .NotNull(accounting, nameof(accounting)) .NotNull(mapperCache, nameof(mapperCache)) .NotNull(accountingModelConverter, nameof(accountingModelConverter)); lock (mapperCache.SyncRoot) { IBudgetAccount budgetAccount = budgetAccountModel.Resolve(mapperCache.BudgetAccountDictionary); if (budgetAccount != null) { return(budgetAccount); } IBudgetAccountGroup budgetAccountGroup = accountingModelConverter.Convert <BudgetAccountGroupModel, IBudgetAccountGroup>(budgetAccountModel.BudgetAccountGroup); budgetAccount = new BudgetAccount(accounting, budgetAccountModel.AccountNumber, budgetAccountModel.BasicAccount.AccountName, budgetAccountGroup) { Description = budgetAccountModel.BasicAccount.Description, Note = budgetAccountModel.BasicAccount.Note }; budgetAccountModel.CopyAuditInformationTo(budgetAccount); budgetAccount.SetDeletable(budgetAccountModel.Deletable); mapperCache.BudgetAccountDictionary.Add(budgetAccountModel.BudgetAccountIdentifier, budgetAccount); accounting.BudgetAccountCollection.Add(budgetAccount); if (budgetAccountModel.BudgetInfos != null) { budgetAccount.BudgetInfoCollection.Populate(budgetAccount, budgetAccountModel.BudgetInfos .Where(budgetInfoModel => budgetInfoModel.Convertible() && (budgetInfoModel.YearMonth.Year < budgetAccountModel.StatusDateForInfos.Year || budgetInfoModel.YearMonth.Year == budgetAccountModel.StatusDateForInfos.Year && budgetInfoModel.YearMonth.Month <= budgetAccountModel.StatusDateForInfos.Month)) .Select(budgetInfoModel => budgetInfoModel.ToDomain(budgetAccount)) .ToArray(), budgetAccountModel.StatusDate, budgetAccountModel.StatusDateForInfos); } if (budgetAccountModel.PostingLines != null) { budgetAccount.PostingLineCollection.Add(budgetAccountModel.PostingLines .Where(postingLineModel => postingLineModel.Convertible() && postingLineModel.PostingDate >= budgetAccountModel.GetFromDateForPostingLines() && postingLineModel.PostingDate < budgetAccountModel.GetToDateForPostingLines(1)) .Select(postingLineModel => postingLineModel.ToDomain(accounting, budgetAccount, mapperCache, accountingModelConverter)) .Where(postingLine => budgetAccount.PostingLineCollection.Contains(postingLine) == false) .ToArray()); } return(budgetAccount); } }
internal static bool Convertible(this BudgetAccountModel budgetAccountModel) { NullGuard.NotNull(budgetAccountModel, nameof(budgetAccountModel)); return(budgetAccountModel.Accounting != null && budgetAccountModel.Accounting.Convertible() && budgetAccountModel.BasicAccount != null && budgetAccountModel.BudgetAccountGroup != null); }
internal static IBudgetAccount Resolve(this BudgetAccountModel budgetAccountModel, IDictionary <int, IBudgetAccount> budgetAccountDictionary) { NullGuard.NotNull(budgetAccountModel, nameof(budgetAccountModel)) .NotNull(budgetAccountDictionary, nameof(budgetAccountDictionary)); return(budgetAccountDictionary.TryGetValue(budgetAccountModel.BudgetAccountIdentifier, out IBudgetAccount budgetAccount) ? budgetAccount : null); }
internal static void ExtractPostingLines(this BudgetAccountModel budgetAccountModel, IReadOnlyCollection <PostingLineModel> postingLineModelCollection) { NullGuard.NotNull(budgetAccountModel, nameof(budgetAccountModel)) .NotNull(postingLineModelCollection, nameof(postingLineModelCollection)); budgetAccountModel.PostingLines = postingLineModelCollection .Where(postingLineModel => postingLineModel.BudgetAccountIdentifier != null && postingLineModel.BudgetAccountIdentifier.Value == budgetAccountModel.BudgetAccountIdentifier && postingLineModel.PostingDate >= budgetAccountModel.GetFromDateForPostingLines() && postingLineModel.PostingDate < budgetAccountModel.GetToDateForPostingLines(1)) .ToList(); }
internal static IBudgetAccount ToDomain(this BudgetAccountModel budgetAccountModel, MapperCache mapperCache, IConverter accountingModelConverter) { NullGuard.NotNull(budgetAccountModel, nameof(budgetAccountModel)) .NotNull(mapperCache, nameof(mapperCache)) .NotNull(accountingModelConverter, nameof(accountingModelConverter)); lock (mapperCache.SyncRoot) { IAccounting accounting = accountingModelConverter.Convert <AccountingModel, IAccounting>(budgetAccountModel.Accounting); return(budgetAccountModel.ToDomain(accounting, mapperCache, accountingModelConverter)); } }
private async Task CreateAsync(IBudgetInfo budgetInfo, BudgetAccountModel budgetAccountModel) { NullGuard.NotNull(budgetInfo, nameof(budgetInfo)) .NotNull(budgetAccountModel, nameof(budgetAccountModel)); BudgetInfoModel budgetInfoModel = ModelConverter.Convert <IBudgetInfo, BudgetInfoModel>(budgetInfo); budgetInfoModel.BudgetAccountIdentifier = budgetAccountModel.BudgetAccountIdentifier; budgetInfoModel.BudgetAccount = budgetAccountModel; EntityEntry <BudgetInfoModel> budgetInfoModelEntityEntry = await Entities.AddAsync(await OnCreateAsync(budgetInfo, budgetInfoModel)); if (budgetAccountModel.BudgetInfos.Contains(budgetInfoModelEntityEntry.Entity) == false) { budgetAccountModel.BudgetInfos.Add(budgetInfoModelEntityEntry.Entity); } }
private static IBudgetAccount ResolveBudgetAccount(BudgetAccountModel budgetAccountModel, IAccounting accounting, MapperCache mapperCache, IConverter accountingModelConverter) { NullGuard.NotNull(accounting, nameof(accounting)) .NotNull(mapperCache, nameof(mapperCache)) .NotNull(accountingModelConverter, nameof(accountingModelConverter)); if (budgetAccountModel == null) { return(null); } IBudgetAccount budgetAccount = budgetAccountModel.Resolve(mapperCache.BudgetAccountDictionary) ?? budgetAccountModel.ToDomain(accounting, mapperCache, accountingModelConverter); if (accounting.BudgetAccountCollection.Contains(budgetAccount) == false) { accounting.BudgetAccountCollection.Add(budgetAccount); } return(budgetAccount); }
internal async Task CreateOrUpdateAsync(IBudgetInfoCollection budgetInfoCollection, BudgetAccountModel budgetAccountModel) { NullGuard.NotNull(budgetInfoCollection, nameof(budgetInfoCollection)) .NotNull(budgetAccountModel, nameof(budgetAccountModel)); IBudgetInfo budgetInfo = budgetInfoCollection.First(); while (budgetInfo != null) { BudgetInfoModel currentBudgetInfoModel = budgetAccountModel.BudgetInfos.SingleOrDefault(budgetInfoModel => budgetInfoModel.YearMonth.Year == budgetInfo.Year && budgetInfoModel.YearMonth.Month == budgetInfo.Month); BudgetInfoModel previousBudgetInfoModel = budgetAccountModel.BudgetInfos.Where(budgetInfoModel => budgetInfoModel.YearMonth.Year < budgetInfo.Year || budgetInfoModel.YearMonth.Year == budgetInfo.Year && budgetInfoModel.YearMonth.Month < budgetInfo.Month) .OrderByDescending(budgetInfoModel => budgetInfoModel.YearMonth.Year) .ThenByDescending(budgetInfoModel => budgetInfoModel.YearMonth.Month) .FirstOrDefault(); if (currentBudgetInfoModel != null) { if (previousBudgetInfoModel != null && budgetInfo.Income == previousBudgetInfoModel.Income && budgetInfo.Expenses == previousBudgetInfoModel.Expenses) { budgetAccountModel.BudgetInfos.Remove(await OnDeleteAsync(currentBudgetInfoModel)); budgetInfo = budgetInfoCollection.Next(budgetInfo); continue; } if (budgetInfo.Income == currentBudgetInfoModel.Income && budgetInfo.Expenses == currentBudgetInfoModel.Expenses) { budgetInfo = budgetInfoCollection.Next(budgetInfo); continue; } await OnUpdateAsync(budgetInfo, currentBudgetInfoModel); budgetInfo = budgetInfoCollection.Next(budgetInfo); continue; } if (previousBudgetInfoModel != null) { if (budgetInfo.Income == previousBudgetInfoModel.Income && budgetInfo.Expenses == previousBudgetInfoModel.Expenses) { budgetInfo = budgetInfoCollection.Next(budgetInfo); continue; } await CreateAsync(budgetInfo, budgetAccountModel); budgetInfo = budgetInfoCollection.Next(budgetInfo); continue; } if (budgetInfo.Income == 0M && budgetInfo.Expenses == 0M) { budgetInfo = budgetInfoCollection.Next(budgetInfo); continue; } await CreateAsync(budgetInfo, budgetAccountModel); budgetInfo = budgetInfoCollection.Next(budgetInfo); } }