Skip to content

Commit

Permalink
improvements on discount calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
zp1ke committed Jul 3, 2024
1 parent 3d035b9 commit f1a1c34
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/src/discount.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ class Discount {
}) : assert(amount >= 0, 'Amount must be zero or positive.'),
amount = !isPercentage || amount <= 1 ? amount : amount / 100;

bool get affectsTotal => !affectSubtotalBeforeTaxes && amount > 0;

static Discount empty() => Discount(amount: .0);

/// Calculates discount amount for given [value].
double discountOf(double value) {
if (amount == 0) {
return 0;
}
if (isPercentage) {
return value * amount;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/src/item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class Item implements Sellable {
/// Calculates subtotal amount including [discount] (before taxes).
@override
double get subtotal {
if (discount == null || discount!.affectSubtotalBeforeTaxes) {
if (discount == null ||
discount!.affectSubtotalBeforeTaxes ||
discount!.amount == 0) {
return _subtotalOf(unitPrice, discount);
}
final base = _subtotalOf(unitPrice);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/sale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Sale implements Sellable {
}

Discount? get _discount {
if (discount != null && !discount!.affectSubtotalBeforeTaxes) {
if (discount?.affectsTotal ?? false) {
final baseSubtotal =
items.map((item) => item.subtotal).reduce((s1, s2) => s1 + s2);
final baseTax = items.map((item) => item.tax).reduce((s1, s2) => s1 + s2);
Expand Down

0 comments on commit f1a1c34

Please sign in to comment.