protected override IBudgetInfoCollection Calculate(DateTime statusDate, IReadOnlyCollection <IBudgetInfo> calculatedBudgetInfoCollection) { NullGuard.NotNull(calculatedBudgetInfoCollection, nameof(calculatedBudgetInfoCollection)); IBudgetInfo budgetInfoForMonthOfStatusDate = calculatedBudgetInfoCollection .AsParallel() .SingleOrDefault(budgetInfo => budgetInfo.IsMonthOfStatusDate); IBudgetInfo budgetInfoForLastMonthOfStatusDate = calculatedBudgetInfoCollection .AsParallel() .SingleOrDefault(budgetInfo => budgetInfo.IsLastMonthOfStatusDate); IBudgetInfo[] budgetInfoCollectionForYearToDateOfStatusDate = calculatedBudgetInfoCollection .AsParallel() .Where(budgetInfo => budgetInfo.IsYearToDateOfStatusDate) .ToArray(); IBudgetInfo[] budgetInfoCollectionForLastYearOfStatusDate = calculatedBudgetInfoCollection .AsParallel() .Where(budgetInfo => budgetInfo.IsLastYearOfStatusDate) .ToArray(); ValuesForMonthOfStatusDate = ToBudgetInfoValues(budgetInfoForMonthOfStatusDate); ValuesForLastMonthOfStatusDate = ToBudgetInfoValues(budgetInfoForLastMonthOfStatusDate); ValuesForYearToDateOfStatusDate = ToBudgetInfoValues(budgetInfoCollectionForYearToDateOfStatusDate); ValuesForLastYearOfStatusDate = ToBudgetInfoValues(budgetInfoCollectionForLastYearOfStatusDate); return(this); }
public void ValuesForLastMonthOfStatusDate_WhenCalculateAsyncHasNotBeenCalled_ReturnsBudgetInfoValuesWherePostedIsEqualToZero() { IBudgetInfoCollection sut = CreateSut(); IBudgetInfoValues result = sut.ValuesForLastMonthOfStatusDate; Assert.That(result.Posted, Is.EqualTo(0M)); }
public void ValuesForLastMonthOfStatusDate_WhenCalculateAsyncHasNotBeenCalled_ReturnsNotNull() { IBudgetInfoCollection sut = CreateSut(); IBudgetInfoValues result = sut.ValuesForLastMonthOfStatusDate; Assert.That(result, Is.Not.Null); }
public void ValuesForMonthOfStatusDate_WhenCalled_ReturnsNotNull() { IBudgetAccount sut = CreateSut(); IBudgetInfoValues result = sut.ValuesForMonthOfStatusDate; Assert.That(result, Is.Not.Null); }
public void BudgetAccountValuesAtPostingDate_WhenCalculateAsyncHasNotBeenCalledOnPostingLineWithBudgetAccount_ReturnsCreditInfoValuesWherePostedIsEqualToZero() { IPostingLine sut = CreateSut(); IBudgetInfoValues result = sut.BudgetAccountValuesAtPostingDate; Assert.That(result.Posted, Is.EqualTo(0M)); }
public void BudgetAccountValuesAtPostingDate_WhenCalculateAsyncHasNotBeenCalledOnPostingLineWithBudgetAccount_ReturnsNotNull() { IPostingLine sut = CreateSut(); IBudgetInfoValues result = sut.BudgetAccountValuesAtPostingDate; Assert.That(result, Is.Not.Null); }
public void ValuesForMonthOfStatusDate_WhenCalled_ReturnsSameBudgetInfoValuesAsValuesForMonthOfStatusDateOnBudgetInfoCollection() { IBudgetInfoCollection budgetInfoCollection = _fixture.BuildBudgetInfoCollectionMock().Object; IBudgetAccount sut = CreateSut(budgetInfoCollection); IBudgetInfoValues result = sut.ValuesForMonthOfStatusDate; Assert.That(result, Is.SameAs(budgetInfoCollection.ValuesForMonthOfStatusDate)); }
public async Task ApplyCalculationAsync_WhenCalledOnPostingLineWhereBudgetAccountValuesAtPostingDateWasGiven_ReturnsSamePostingLineWhereBudgetAccountValuesAtPostingDateEqualToBudgetAccountValuesAtPostingDate() { IBudgetInfoValues budgetAccountValuesAtPostingDate = _fixture.BuildBudgetInfoValuesMock().Object; IPostingLine sut = CreateSut(budgetAccountValuesAtPostingDate: budgetAccountValuesAtPostingDate); IPostingLine result = await sut.ApplyCalculationAsync(_fixture.BuildBudgetAccountMock().Object); Assert.That(result.BudgetAccountValuesAtPostingDate, Is.EqualTo(budgetAccountValuesAtPostingDate)); }
public async Task ApplyCalculationAsync_WhenCalledOnPostingLineWhereBudgetAccountValuesAtPostingDateWasGiven_AssertPostingLineCollectionWasNotCalledOnCalculatedBudgetAccount() { IBudgetInfoValues budgetAccountValuesAtPostingDate = _fixture.BuildBudgetInfoMock().Object; IPostingLine sut = CreateSut(budgetAccountValuesAtPostingDate: budgetAccountValuesAtPostingDate); Mock <IBudgetAccount> calculatedBudgetAccountMock = _fixture.BuildBudgetAccountMock(); await sut.ApplyCalculationAsync(calculatedBudgetAccountMock.Object); calculatedBudgetAccountMock.Verify(m => m.PostingLineCollection, Times.Never); }
public void ValuesForMonthOfStatusDate_WhenCalculateAsyncHasNotBeenCalled_ReturnsBudgetInfoValuesWherePostedIsEqualToZero() { IBudgetAccountCollection sut = CreateSut(); sut.Add(_fixture.CreateMany <IBudgetAccount>(_random.Next(5, 10)).ToArray()); IBudgetInfoValues result = sut.ValuesForMonthOfStatusDate; Assert.That(result.Posted, Is.EqualTo(0M)); }
public void ValuesForMonthOfStatusDate_WhenCalculateAsyncHasNotBeenCalled_ReturnsNotNull() { IBudgetAccountCollection sut = CreateSut(); sut.Add(_fixture.CreateMany <IBudgetAccount>(_random.Next(5, 10)).ToArray()); IBudgetInfoValues result = sut.ValuesForMonthOfStatusDate; Assert.That(result, Is.Not.Null); }
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); } }
protected override IBudgetAccountCollection Calculate(DateTime statusDate, IReadOnlyCollection <IBudgetAccount> calculatedBudgetAccountCollection) { NullGuard.NotNull(calculatedBudgetAccountCollection, nameof(calculatedBudgetAccountCollection)); ValuesForMonthOfStatusDate = ToBudgetInfoValues(calculatedBudgetAccountCollection, budgetAccount => budgetAccount.ValuesForMonthOfStatusDate); ValuesForLastMonthOfStatusDate = ToBudgetInfoValues(calculatedBudgetAccountCollection, budgetAccount => budgetAccount.ValuesForLastMonthOfStatusDate); ValuesForYearToDateOfStatusDate = ToBudgetInfoValues(calculatedBudgetAccountCollection, budgetAccount => budgetAccount.ValuesForYearToDateOfStatusDate); ValuesForLastYearOfStatusDate = ToBudgetInfoValues(calculatedBudgetAccountCollection, budgetAccount => budgetAccount.ValuesForLastYearOfStatusDate); return(this); }
public void ValuesForMonthOfStatusDate_WhenCalled_AssertValuesForMonthOfStatusDateWasCalledOnBudgetInfoCollection() { Mock <IBudgetInfoCollection> budgetInfoCollectionMock = _fixture.BuildBudgetInfoCollectionMock(); IBudgetAccount sut = CreateSut(budgetInfoCollectionMock.Object); IBudgetInfoValues result = sut.ValuesForMonthOfStatusDate; Assert.That(result, Is.Not.Null); budgetInfoCollectionMock.Verify(m => m.ValuesForMonthOfStatusDate, Times.Once); }
public async Task CalculateAsync_WhenCalledOnPostingLineWithBudgetAccount_ReturnsSamePostingLineWhereBudgetAccountValuesAtPostingDateHasNotBeenChanged() { IAccounting accounting = _fixture.BuildAccountingMock().Object; IAccount account = _fixture.BuildAccountMock(accounting).Object; IBudgetAccount budgetAccount = _fixture.BuildBudgetAccountMock(accounting, statusDate: DateTime.MinValue).Object; IPostingLine sut = CreateSut(account: account, budgetAccount: budgetAccount); IBudgetInfoValues budgetAccountValuesAtPostingDate = sut.BudgetAccountValuesAtPostingDate; IPostingLine result = await sut.CalculateAsync(DateTime.Now.AddDays(_random.Next(1, 365) * -1)); Assert.That(result.BudgetAccountValuesAtPostingDate, Is.SameAs(budgetAccountValuesAtPostingDate)); }
private static Task <IPostingWarning> CalculateAsync(IBudgetAccount budgetAccount, IBudgetInfoValues budgetInfoValues, IPostingLine postingLine) { NullGuard.NotNull(postingLine, nameof(postingLine)); return(Task.Run(() => { if (budgetAccount == null || budgetInfoValues == null) { return null; } decimal budget = budgetInfoValues.Budget; decimal posted = budgetInfoValues.Posted; if (budget > 0M && posted < budget) { return new PostingWarning(PostingWarningReason.ExpectedIncomeHasNotBeenReachedYet, budgetAccount, budget - posted, postingLine); } if (budget < 0M && posted < budget) { return new PostingWarning(PostingWarningReason.ExpectedExpensesHaveAlreadyBeenReached, budgetAccount, Math.Abs(posted) - Math.Abs(budget), postingLine); } return (IPostingWarning)null; })); }
private IPostingLine CreateSut(DateTime?postingDate = null, int?sortOrder = null, IBudgetInfoValues budgetAccountValuesAtPostingDate = 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>(), _fixture.BuildBudgetAccountMock(accounting).Object, Math.Abs(_fixture.Create <decimal>()), Math.Abs(_fixture.Create <decimal>()), null, sortOrder ?? Math.Abs(_fixture.Create <int>()), null, budgetAccountValuesAtPostingDate)); }
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; }