Esempio n. 1
0
        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);
            }
        }
        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);
        }