Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored receipt calculation. #145

Merged
merged 11 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Faktur.Domain.Products;
using Faktur.Domain.Receipts;
using Faktur.Domain.Stores;
using Faktur.Domain.Taxes;
using FluentValidation;
using Logitar.Identity.Domain.Shared;
using MediatR;
Expand All @@ -15,13 +14,11 @@ internal class CreateOrReplaceReceiptItemCommandHandler : IRequestHandler<Create
{
private readonly IReceiptItemQuerier _receiptItemQuerier;
private readonly IReceiptRepository _receiptRepository;
private readonly ITaxRepository _taxRepository;

public CreateOrReplaceReceiptItemCommandHandler(IReceiptItemQuerier receiptItemQuerier, IReceiptRepository receiptRepository, ITaxRepository taxRepository)
public CreateOrReplaceReceiptItemCommandHandler(IReceiptItemQuerier receiptItemQuerier, IReceiptRepository receiptRepository)
{
_receiptItemQuerier = receiptItemQuerier;
_receiptRepository = receiptRepository;
_taxRepository = taxRepository;
}

public async Task<CreateOrReplaceReceiptItemResult?> Handle(CreateOrReplaceReceiptItemCommand command, CancellationToken cancellationToken)
Expand Down Expand Up @@ -105,9 +102,6 @@ public CreateOrReplaceReceiptItemCommandHandler(IReceiptItemQuerier receiptItemQ
item = new(gtin, sku, label, flags, quantity, unitPrice, price, departmentNumber, department);
receipt.SetItem(command.ItemNumber, item, command.ActorId);

IEnumerable<TaxAggregate> taxes = await _taxRepository.LoadAsync(cancellationToken);
receipt.Calculate(taxes, command.ActorId);

await _receiptRepository.SaveAsync(receipt, cancellationToken);

ReceiptItem result = await _receiptItemQuerier.ReadAsync(receipt, command.ItemNumber, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Faktur.Contracts.Receipts;
using Faktur.Domain.Receipts;
using Faktur.Domain.Stores;
using Faktur.Domain.Taxes;
using FluentValidation;
using MediatR;

Expand All @@ -13,12 +14,15 @@ internal class CreateReceiptCommandHandler : IRequestHandler<CreateReceiptComman
private readonly IReceiptQuerier _receiptQuerier;
private readonly IReceiptRepository _receiptRepository;
private readonly IStoreRepository _storeRepository;
private readonly ITaxRepository _taxRepository;

public CreateReceiptCommandHandler(IReceiptQuerier receiptQuerier, IReceiptRepository receiptRepository, IStoreRepository storeRepository)
public CreateReceiptCommandHandler(IReceiptQuerier receiptQuerier, IReceiptRepository receiptRepository,
IStoreRepository storeRepository, ITaxRepository taxRepository)
{
_receiptQuerier = receiptQuerier;
_receiptRepository = receiptRepository;
_storeRepository = storeRepository;
_taxRepository = taxRepository;
}

public async Task<Receipt> Handle(CreateReceiptCommand command, CancellationToken cancellationToken)
Expand All @@ -30,7 +34,8 @@ public async Task<Receipt> Handle(CreateReceiptCommand command, CancellationToke
?? throw new StoreNotFoundException(payload.StoreId, nameof(payload.StoreId));

NumberUnit? number = NumberUnit.TryCreate(payload.Number);
ReceiptAggregate receipt = new(store, payload.IssuedOn, number, command.ActorId);
IEnumerable<TaxAggregate> taxes = await _taxRepository.LoadAsync(cancellationToken);
ReceiptAggregate receipt = new(store, payload.IssuedOn, number, taxes, command.ActorId);

await _receiptRepository.SaveAsync(receipt, cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,9 @@ public async Task<Receipt> Handle(ImportReceiptCommand command, CancellationToke
}

NumberUnit? number = NumberUnit.TryCreate(payload.Number);
ReceiptAggregate receipt = ReceiptAggregate.Import(store, payload.IssuedOn, number, items, command.ActorId);

IEnumerable<TaxAggregate> taxes = await _taxRepository.LoadAsync(cancellationToken);
receipt.Calculate(taxes, command.ActorId);
ReceiptAggregate receipt = ReceiptAggregate.Import(store, payload.IssuedOn, number, items, taxes, command.ActorId);

await _receiptRepository.SaveAsync(receipt, cancellationToken);

return await _receiptQuerier.ReadAsync(receipt, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Faktur.Contracts.Receipts;
using Faktur.Domain.Receipts;
using Faktur.Domain.Taxes;
using MediatR;

namespace Faktur.Application.Receipts.Commands;
Expand All @@ -9,13 +8,11 @@ internal class RemoveReceiptItemCommandHandler : IRequestHandler<RemoveReceiptIt
{
private readonly IReceiptItemQuerier _receiptItemQuerier;
private readonly IReceiptRepository _receiptRepository;
private readonly ITaxRepository _taxRepository;

public RemoveReceiptItemCommandHandler(IReceiptItemQuerier receiptItemQuerier, IReceiptRepository receiptRepository, ITaxRepository taxRepository)
public RemoveReceiptItemCommandHandler(IReceiptItemQuerier receiptItemQuerier, IReceiptRepository receiptRepository)
{
_receiptItemQuerier = receiptItemQuerier;
_receiptRepository = receiptRepository;
_taxRepository = taxRepository;
}

public async Task<ReceiptItem?> Handle(RemoveReceiptItemCommand command, CancellationToken cancellationToken)
Expand All @@ -29,9 +26,6 @@ public RemoveReceiptItemCommandHandler(IReceiptItemQuerier receiptItemQuerier, I

receipt.RemoveItem(command.ItemNumber, command.ActorId);

IEnumerable<TaxAggregate> taxes = await _taxRepository.LoadAsync(cancellationToken);
receipt.Calculate(taxes, command.ActorId);

await _receiptRepository.SaveAsync(receipt, cancellationToken);

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Faktur.Domain.Products;
using Faktur.Domain.Receipts;
using Faktur.Domain.Stores;
using Faktur.Domain.Taxes;
using FluentValidation;
using Logitar.Identity.Domain.Shared;
using MediatR;
Expand All @@ -15,13 +14,11 @@ internal class UpdateReceiptItemCommandHandler : IRequestHandler<UpdateReceiptIt
{
private readonly IReceiptItemQuerier _receiptItemQuerier;
private readonly IReceiptRepository _receiptRepository;
private readonly ITaxRepository _taxRepository;

public UpdateReceiptItemCommandHandler(IReceiptItemQuerier receiptItemQuerier, IReceiptRepository receiptRepository, ITaxRepository taxRepository)
public UpdateReceiptItemCommandHandler(IReceiptItemQuerier receiptItemQuerier, IReceiptRepository receiptRepository)
{
_receiptItemQuerier = receiptItemQuerier;
_receiptRepository = receiptRepository;
_taxRepository = taxRepository;
}

public async Task<ReceiptItem?> Handle(UpdateReceiptItemCommand command, CancellationToken cancellationToken)
Expand Down Expand Up @@ -78,9 +75,6 @@ public UpdateReceiptItemCommandHandler(IReceiptItemQuerier receiptItemQuerier, I
item = new(gtin, sku, label, flags, quantity, unitPrice, price, departmentNumber, department);
receipt.SetItem(command.ItemNumber, item, command.ActorId);

IEnumerable<TaxAggregate> taxes = await _taxRepository.LoadAsync(cancellationToken);
receipt.Calculate(taxes, command.ActorId);

await _receiptRepository.SaveAsync(receipt, cancellationToken);

return await _receiptItemQuerier.ReadAsync(receipt, command.ItemNumber, cancellationToken);
Expand Down
1 change: 1 addition & 0 deletions backend/src/Faktur.Domain/Faktur.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

<ItemGroup>
<Using Include="System.Globalization" />
<Using Include="System.Text.Json.Serialization" />
</ItemGroup>

</Project>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

namespace Faktur.Domain.Receipts.Events;

public record ReceiptCreatedEvent(StoreId StoreId, DateTime IssuedOn, NumberUnit? Number) : DomainEvent, INotification;
public record ReceiptCreatedEvent(StoreId StoreId, DateTime IssuedOn, NumberUnit? Number, IReadOnlyDictionary<string, ReceiptTaxUnit> Taxes)
: DomainEvent, INotification;
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,23 @@

namespace Faktur.Domain.Receipts.Events;

public record ReceiptImportedEvent(StoreId StoreId, DateTime IssuedOn, NumberUnit? Number, IReadOnlyDictionary<ushort, ReceiptItemUnit> Items) : DomainEvent, INotification;
public record ReceiptImportedEvent(StoreId StoreId, DateTime IssuedOn, NumberUnit? Number, IReadOnlyDictionary<ushort, ReceiptItemUnit> Items,
decimal SubTotal, IReadOnlyDictionary<string, ReceiptTaxUnit> Taxes, decimal Total) : DomainEvent, INotification, IReceiptTotal
{
public static ReceiptImportedEvent Create(StoreAggregate store, DateTime? issuedOn, NumberUnit? number, IEnumerable<ReceiptItemUnit>? items, ReceiptTotal total)
{
Dictionary<ushort, ReceiptItemUnit> receiptItems = [];
if (items != null)
{
receiptItems = new(capacity: items.Count());
ushort itemNumber = 0;
foreach (ReceiptItemUnit item in items)
{
receiptItems[itemNumber] = item;
itemNumber++;
}
}

return new ReceiptImportedEvent(store.Id, issuedOn ?? DateTime.Now, number, receiptItems, total.SubTotal, total.Taxes, total.Total);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@

namespace Faktur.Domain.Receipts.Events;

public record ReceiptItemChangedEvent(ushort Number, ReceiptItemUnit Item) : DomainEvent, INotification;
public record ReceiptItemChangedEvent(ushort Number, ReceiptItemUnit Item, decimal SubTotal, IReadOnlyDictionary<string, ReceiptTaxUnit> Taxes, decimal Total)
: DomainEvent, INotification, IReceiptTotal
{
public static ReceiptItemChangedEvent Create(ushort number, ReceiptItemUnit item, ReceiptTotal total)
{
return new ReceiptItemChangedEvent(number, item, total.SubTotal, total.Taxes, total.Total);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@

namespace Faktur.Domain.Receipts.Events;

public record ReceiptItemRemovedEvent(ushort Number) : DomainEvent, INotification;
public record ReceiptItemRemovedEvent(ushort Number, decimal SubTotal, IReadOnlyDictionary<string, ReceiptTaxUnit> Taxes, decimal Total)
: DomainEvent, INotification, IReceiptTotal
{
public static ReceiptItemRemovedEvent Create(ushort number, ReceiptTotal total)
{
return new ReceiptItemRemovedEvent(number, total.SubTotal, total.Taxes, total.Total);
}
}
8 changes: 8 additions & 0 deletions backend/src/Faktur.Domain/Receipts/IReceiptTotal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Faktur.Domain.Receipts;

public interface IReceiptTotal
{
decimal SubTotal { get; }
IReadOnlyDictionary<string, ReceiptTaxUnit> Taxes { get; }
decimal Total { get; }
}
Loading