refactor: deduplicate net/discount calculation in line item strategies#17
Merged
Conversation
Introduce CalculateNetAndDiscount and CalculateGrossAndNetDiscount on InvoiceLineItem to compute both values in a single pass, avoiding a redundant second invocation of TotalNetWith / TotalGrossWith. All three calculation strategies now use these combined methods. Remove the default Calculate(lineItems, rounding) overload from IVatCalculationStrategy — callers must provide explicit discount behavior. Also: - Change Quantity.One from a computed property to a static readonly field to avoid unnecessary allocations - Multi-target test project: net8.0;net9.0;net10.0 Co-authored-by: Copilot <copilot@github.com>
There was a problem hiding this comment.
Pull request overview
This PR refactors VAT line-item calculations to compute net/gross and net-equivalent discounts in a single pass, reducing duplicate work across calculation strategies while also tightening strategy API usage and broadening test TFMs.
Changes:
- Add combined
InvoiceLineItemhelpers for net+discount and gross+net-discount calculations, and update all strategies to use them. - Remove the default
IVatCalculationStrategy.Calculate(lineItems, rounding)overload to require explicit discount behavior. - Multi-target the test project (
net8.0;net9.0;net10.0) and changeQuantity.Oneimplementation to avoid repeated allocations.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Inflop.VatSharp.Tests/Inflop.VatSharp.Tests.csproj | Multi-target test project across net8/net9/net10. |
| src/Inflop.VatSharp/ValueObjects/Quantity.cs | Refactor Quantity.One from computed member to cached instance. |
| src/Inflop.VatSharp/ValueObjects/InvoiceLineItem.cs | Add combined calculation helpers and switch Calculate to use them. |
| src/Inflop.VatSharp/Strategies/Calculation/SumOfLineItemVatAmountsStrategy.cs | Use combined net+discount helper per item. |
| src/Inflop.VatSharp/Strategies/Calculation/IVatCalculationStrategy.cs | Remove default overload that implied a discount behavior. |
| src/Inflop.VatSharp/Strategies/Calculation/FromSumOfNetValuesStrategy.cs | Use combined net+discount helper inside grouping loop. |
| src/Inflop.VatSharp/Strategies/Calculation/FromSumOfGrossValuesStrategy.cs | Use combined gross+net-discount helper inside grouping loop. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
30
to
+31
| /// <summary>A quantity of exactly one.</summary> | ||
| public static Quantity One => new(1m); | ||
| public static readonly Quantity One = Of(1); |
Comment on lines
+111
to
+112
| /// avoiding a redundant second invocation of <see cref="TotalGrossWith"/> via | ||
| /// <see cref="DiscountAmountNetWith"/>. |
Comment on lines
+53
to
55
| var (itemGross, itemDiscount) = item.CalculateGrossAndNetDiscount(discountBehavior, rounding); | ||
|
|
||
| sumGross += itemGross; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduce CalculateNetAndDiscount and CalculateGrossAndNetDiscount
on InvoiceLineItem to compute both values in a single pass, avoiding
a redundant second invocation of TotalNetWith / TotalGrossWith.
All three calculation strategies now use these combined methods.
Remove the default Calculate(lineItems, rounding) overload from
IVatCalculationStrategy — callers must provide explicit discount behavior.
Also: