Example #1
0
        public void ContactAccountValuesAtPostingDate_WhenCalculateAsyncHasNotBeenCalledOnPostingLineWithContactAccount_ReturnsNotNull()
        {
            IPostingLine sut = CreateSut();

            IContactInfoValues result = sut.ContactAccountValuesAtPostingDate;

            Assert.That(result, Is.Not.Null);
        }
        public void ValuesAtEndOfLastYearFromStatusDate_WhenCalculateAsyncHasNotBeenCalled_ReturnsContactInfoValuesWhereBalanceIsEqualToZero()
        {
            IContactInfoCollection sut = CreateSut();

            IContactInfoValues result = sut.ValuesAtEndOfLastYearFromStatusDate;

            Assert.That(result.Balance, Is.EqualTo(0M));
        }
        public void ValuesAtEndOfLastYearFromStatusDate_WhenCalculateAsyncHasNotBeenCalled_ReturnsNotNull()
        {
            IContactInfoCollection sut = CreateSut();

            IContactInfoValues result = sut.ValuesAtEndOfLastYearFromStatusDate;

            Assert.That(result, Is.Not.Null);
        }
Example #4
0
        public void ContactAccountValuesAtPostingDate_WhenCalculateAsyncHasNotBeenCalledOnPostingLineWithContactAccount_ReturnsCreditInfoValuesWhereBalanceIsEqualToZero()
        {
            IPostingLine sut = CreateSut();

            IContactInfoValues result = sut.ContactAccountValuesAtPostingDate;

            Assert.That(result.Balance, Is.EqualTo(0M));
        }
Example #5
0
        public void ValuesAtStatusDate_WhenCalled_ReturnsNotNull()
        {
            IContactAccount sut = CreateSut();

            IContactInfoValues result = sut.ValuesAtStatusDate;

            Assert.That(result, Is.Not.Null);
        }
Example #6
0
        public void ValuesAtStatusDate_WhenCalled_ReturnsSameContactInfoValuesAsValuesAtStatusDateOnContactInfoCollection()
        {
            IContactInfoCollection contactInfoCollection = _fixture.BuildContactInfoCollectionMock().Object;
            IContactAccount        sut = CreateSut(contactInfoCollection);

            IContactInfoValues result = sut.ValuesAtStatusDate;

            Assert.That(result, Is.SameAs(contactInfoCollection.ValuesAtStatusDate));
        }
Example #7
0
        public async Task ApplyCalculationAsync_WhenCalledOnPostingLineWhereContactAccountValuesAtPostingDateWasGiven_ReturnsSamePostingLineWhereContactAccountValuesAtPostingDateEqualToGivenContactAccountValuesAtPostingDate()
        {
            IContactInfoValues contactAccountValuesAtPostingDate = _fixture.BuildContactInfoValuesMock().Object;
            IPostingLine       sut = CreateSut(contactAccountValuesAtPostingDate: contactAccountValuesAtPostingDate);

            IPostingLine result = await sut.ApplyCalculationAsync(_fixture.BuildContactAccountMock().Object);

            Assert.That(result.ContactAccountValuesAtPostingDate, Is.EqualTo(contactAccountValuesAtPostingDate));
        }
Example #8
0
        public async Task ApplyCalculationAsync_WhenCalledOnPostingLineWhereContactAccountValuesAtPostingDateWasGiven_AssertPostingLineCollectionWasNotCalledOnCalculatedContactAccount()
        {
            IContactInfoValues contactAccountValuesAtPostingDate = _fixture.BuildContactInfoValuesMock().Object;
            IPostingLine       sut = CreateSut(contactAccountValuesAtPostingDate: contactAccountValuesAtPostingDate);

            Mock <IContactAccount> calculatedContactAccountMock = _fixture.BuildContactAccountMock();
            await sut.ApplyCalculationAsync(calculatedContactAccountMock.Object);

            calculatedContactAccountMock.Verify(m => m.PostingLineCollection, Times.Never);
        }
        internal static IPostingLine ToDomain(this PostingLineModel postingLineModel, IAccount account, IBudgetAccount budgetAccount, IContactAccount contactAccount, MapperCache mapperCache)
        {
            NullGuard.NotNull(postingLineModel, nameof(postingLineModel))
            .NotNull(account, nameof(account))
            .NotNull(mapperCache, nameof(mapperCache));

            Guid postingLineIdentification = Guid.Parse(postingLineModel.PostingLineIdentification);

            lock (mapperCache.SyncRoot)
            {
                if (mapperCache.PostingLineDictionary.TryGetValue(postingLineIdentification, out IPostingLine postingLine))
                {
                    return(postingLine);
                }

                ICreditInfo       creditInfo = account.CreditInfoCollection.Find(postingLineModel.PostingDate);
                ICreditInfoValues accountValuesAtPostingDate = new CreditInfoValues(creditInfo?.Credit ?? 0M, postingLineModel.PostingValueForAccount);

                IBudgetInfoValues budgetAccountValuesAtPostingDate = null;
                if (budgetAccount != null)
                {
                    IBudgetInfo budgetInfo = budgetAccount.BudgetInfoCollection.Find(postingLineModel.PostingDate);
                    budgetAccountValuesAtPostingDate = new BudgetInfoValues(budgetInfo?.Budget ?? 0M, postingLineModel.PostingValueForBudgetAccount ?? 0M);
                }

                IContactInfoValues contactAccountValuesAtPostingDate = null;
                if (contactAccount != null)
                {
                    contactAccountValuesAtPostingDate = new ContactInfoValues(postingLineModel.PostingValueForContactAccount ?? 0M);
                }

                postingLine = new PostingLine(postingLineIdentification, postingLineModel.PostingDate, postingLineModel.Reference, account, postingLineModel.Details, budgetAccount, postingLineModel.Debit ?? 0M, postingLineModel.Credit ?? 0M, contactAccount, postingLineModel.PostingLineIdentifier, accountValuesAtPostingDate, budgetAccountValuesAtPostingDate, contactAccountValuesAtPostingDate);
                postingLine.AddAuditInformation(postingLineModel.CreatedUtcDateTime, postingLineModel.CreatedByIdentifier, postingLineModel.ModifiedUtcDateTime, postingLineModel.ModifiedByIdentifier);

                mapperCache.PostingLineDictionary.Add(postingLineIdentification, postingLine);

                if (account.PostingLineCollection.Contains(postingLine) == false)
                {
                    account.PostingLineCollection.Add(postingLine);
                }

                if (budgetAccount != null && budgetAccount.PostingLineCollection.Contains(postingLine) == false)
                {
                    budgetAccount.PostingLineCollection.Add(postingLine);
                }

                if (contactAccount != null && contactAccount.PostingLineCollection.Contains(postingLine) == false)
                {
                    contactAccount.PostingLineCollection.Add(postingLine);
                }

                return(postingLine);
            }
        }
Example #10
0
        public void ValuesAtStatusDate_WhenCalled_AssertValuesAtStatusDateWasCalledOnContactInfoCollection()
        {
            Mock <IContactInfoCollection> contactInfoCollectionMock = _fixture.BuildContactInfoCollectionMock();
            IContactAccount sut = CreateSut(contactInfoCollectionMock.Object);

            IContactInfoValues result = sut.ValuesAtStatusDate;

            Assert.That(result, Is.Not.Null);

            contactInfoCollectionMock.Verify(m => m.ValuesAtStatusDate, Times.Once);
        }
Example #11
0
        public void ContactAccountType_WhenBalanceBelowZeroOnAccountingIsCreditorsAndBalanceInValuesAtStatusDateIsEqualToZero_ReturnsNone()
        {
            IAccounting            accounting            = _fixture.BuildAccountingMock(balanceBelowZero: BalanceBelowZeroType.Creditors).Object;
            IContactInfoValues     valuesAtStatusDate    = _fixture.BuildContactInfoValuesMock(0M).Object;
            IContactInfoCollection contactInfoCollection = _fixture.BuildContactInfoCollectionMock(valuesAtStatusDate: valuesAtStatusDate).Object;
            IContactAccount        sut = CreateSut(accounting, contactInfoCollection);

            ContactAccountType result = sut.ContactAccountType;

            Assert.That(result, Is.EqualTo(ContactAccountType.None));
        }
Example #12
0
        public void ContactAccountType_WhenBalanceBelowZeroOnAccountingIsDebtorsAndBalanceInValuesAtStatusDateAboveToZero_ReturnsCreditor()
        {
            IAccounting            accounting            = _fixture.BuildAccountingMock(balanceBelowZero: BalanceBelowZeroType.Debtors).Object;
            IContactInfoValues     valuesAtStatusDate    = _fixture.BuildContactInfoValuesMock(Math.Abs(_fixture.Create <decimal>())).Object;
            IContactInfoCollection contactInfoCollection = _fixture.BuildContactInfoCollectionMock(valuesAtStatusDate: valuesAtStatusDate).Object;
            IContactAccount        sut = CreateSut(accounting, contactInfoCollection);

            ContactAccountType result = sut.ContactAccountType;

            Assert.That(result, Is.EqualTo(ContactAccountType.Creditor));
        }
        public async Task CalculateAsync_WhenCalledOnPostingLineWithContactAccount_ReturnsSamePostingLineWhereContactAccountValuesAtPostingDateHasNotBeenChanged()
        {
            IAccounting     accounting     = _fixture.BuildAccountingMock().Object;
            IAccount        account        = _fixture.BuildAccountMock(accounting).Object;
            IContactAccount contactAccount = _fixture.BuildContactAccountMock(accounting, statusDate: DateTime.MinValue).Object;
            IPostingLine    sut            = CreateSut(account: account, contactAccount: contactAccount);

            IContactInfoValues contactAccountValuesAtPostingDate = sut.ContactAccountValuesAtPostingDate;
            IPostingLine       result = await sut.CalculateAsync(DateTime.Now.AddDays(_random.Next(1, 365) * -1));

            Assert.That(result.ContactAccountValuesAtPostingDate, Is.SameAs(contactAccountValuesAtPostingDate));
        }
Example #14
0
        protected override IContactInfoCollection Calculate(DateTime statusDate, IReadOnlyCollection <IContactInfo> calculatedContactInfoCollection)
        {
            NullGuard.NotNull(calculatedContactInfoCollection, nameof(calculatedContactInfoCollection));

            IContactInfo contactInfoAtStatusDate = calculatedContactInfoCollection
                                                   .AsParallel()
                                                   .SingleOrDefault(contactInfo => contactInfo.IsMonthOfStatusDate);
            IContactInfo contactInfoAtEndOfLastMonthFromStatusDate = calculatedContactInfoCollection
                                                                     .AsParallel()
                                                                     .SingleOrDefault(contactInfo => contactInfo.IsLastMonthOfStatusDate);
            IContactInfo contactInfoAtEndOfLastYearFromStatusDate = calculatedContactInfoCollection
                                                                    .AsParallel()
                                                                    .Where(contactInfo => contactInfo.IsLastYearOfStatusDate)
                                                                    .OrderByDescending(contactInfo => contactInfo.Year)
                                                                    .ThenByDescending(contactInfo => contactInfo.Month)
                                                                    .FirstOrDefault();

            ValuesAtStatusDate = ToContactInfoValues(contactInfoAtStatusDate);
            ValuesAtEndOfLastMonthFromStatusDate = ToContactInfoValues(contactInfoAtEndOfLastMonthFromStatusDate);
            ValuesAtEndOfLastYearFromStatusDate  = ToContactInfoValues(contactInfoAtEndOfLastYearFromStatusDate);

            return(this);
        }
        public PostingLine(Guid identifier, DateTime postingDate, string reference, IAccount account, string details, IBudgetAccount budgetAccount, decimal debit, decimal credit, IContactAccount contactAccount, int sortOrder, ICreditInfoValues accountValuesAtPostingDate = null, IBudgetInfoValues budgetAccountValuesAtPostingDate = null, IContactInfoValues contactAccountValuesAtPostingDate = null)
        {
            NullGuard.NotNull(account, nameof(account))
            .NotNullOrWhiteSpace(details, nameof(details));

            if (postingDate.Year < InfoBase <ICreditInfo> .MinYear || postingDate.Year > InfoBase <ICreditInfo> .MaxYear)
            {
                throw new ArgumentException($"Year for the posting data should be between {InfoBase<ICreditInfo>.MinYear} and {InfoBase<ICreditInfo>.MaxYear}.", nameof(postingDate));
            }

            if (postingDate.Month < InfoBase <ICreditInfo> .MinMonth || postingDate.Month > InfoBase <ICreditInfo> .MaxMonth)
            {
                throw new ArgumentException($"Month for the posting data should be between {InfoBase<ICreditInfo>.MinMonth} and {InfoBase<ICreditInfo>.MaxMonth}.", nameof(postingDate));
            }

            if (budgetAccount != null && budgetAccount.Accounting.Number != account.Accounting.Number)
            {
                throw new ArgumentException("Accounting on the given budget account does not match the accounting on the given account.", nameof(budgetAccount));
            }

            if (debit < 0M)
            {
                throw new ArgumentException("Debit cannot be lower than 0.", nameof(debit));
            }

            if (credit < 0M)
            {
                throw new ArgumentException("Credit cannot be lower than 0.", nameof(credit));
            }

            if (contactAccount != null && contactAccount.Accounting.Number != account.Accounting.Number)
            {
                throw new ArgumentException("Accounting on the given contact account does not match the accounting on the given account.", nameof(contactAccount));
            }

            if (sortOrder < 0)
            {
                throw new ArgumentException("Sort order cannot be lower than 0.", nameof(sortOrder));
            }

            Identifier  = identifier;
            Accounting  = account.Accounting;
            PostingDate = postingDate.Date;
            Reference   = string.IsNullOrWhiteSpace(reference) ? null : reference.Trim();
            Account     = account;
            AccountValuesAtPostingDate = accountValuesAtPostingDate ?? new CreditInfoValues(0M, 0M);
            Details       = details.Trim();
            BudgetAccount = budgetAccount;
            BudgetAccountValuesAtPostingDate = budgetAccount != null ? budgetAccountValuesAtPostingDate ?? new BudgetInfoValues(0M, 0M) : null;
            Debit          = debit;
            Credit         = credit;
            ContactAccount = contactAccount;
            ContactAccountValuesAtPostingDate = contactAccount != null ? contactAccountValuesAtPostingDate ?? new ContactInfoValues(0M) : null;
            SortOrder = sortOrder;

            _calculateAccountValuesAtPostingDate        = accountValuesAtPostingDate == null;
            _calculateBudgetAccountValuesAtPostingDate  = budgetAccountValuesAtPostingDate == null;
            _calculateContactAccountValuesAtPostingDate = contactAccountValuesAtPostingDate == null;
        }
Example #16
0
        private IPostingLine CreateSut(DateTime?postingDate = null, int?sortOrder = null, IContactInfoValues contactAccountValuesAtPostingDate = null)
        {
            int year  = _random.Next(InfoBase <ICreditInfo> .MinYear, Math.Min(DateTime.Today.Year, InfoBase <ICreditInfo> .MaxYear));
            int month = _random.Next(InfoBase <ICreditInfo> .MinMonth, Math.Min(DateTime.Today.Month, InfoBase <ICreditInfo> .MaxMonth));
            int day   = _random.Next(1, DateTime.DaysInMonth(year, month));

            IAccounting accounting = _fixture.BuildAccountingMock().Object;

            return(new Domain.Accounting.PostingLine(Guid.NewGuid(), postingDate ?? new DateTime(year, month, day), _fixture.Create <string>(), _fixture.BuildAccountMock(accounting).Object, _fixture.Create <string>(), null, Math.Abs(_fixture.Create <decimal>()), Math.Abs(_fixture.Create <decimal>()), _fixture.BuildContactAccountMock(accounting).Object, sortOrder ?? Math.Abs(_fixture.Create <int>()), null, null, contactAccountValuesAtPostingDate));
        }
Example #17
0
        private static Task <IPostingWarning> CalculateAsync(IContactAccount contactAccount, IContactInfoValues contactInfoValues, IPostingLine postingLine)
        {
            NullGuard.NotNull(postingLine, nameof(postingLine));

            return(Task.Run(() => (IPostingWarning)null));
        }