diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..bc1eb9a --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,16 @@ +name: Test + +on: + push: + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: dart-lang/setup-dart@v1 + + - run: dart pub get + - run: dart analyze + - run: dart test diff --git a/README.md b/README.md index 5dcd175..e037528 100644 --- a/README.md +++ b/README.md @@ -3,28 +3,27 @@ [creating packages](https://dart.dev/guides/libraries/create-library-packages) --> -Library for sales calculations. +# Salert -## Features - -TODO: List what your package can do. Maybe include images, gifs, or videos. +Library for sales data calculations. -## Getting started +## Features -TODO: List prerequisites and provide or point to information on how to -start using the package. +- Sales items calculations (discount, subtotal, taxes, total). +- Sales calculations (discount, subtotal, taxes, total). ## Usage -TODO: Include short and useful examples for package users. Add longer examples -to `/example` folder. - +Example for SRI invoice (Ecuador): ```dart -const like = 'sample'; +// VAT tax with 13%. +final vat = Tax(code: 'IVA13', unitValue: 13); +// Invoice item (product or service). +final item = Item(code: 'productX', quantity: 1, unitPrice: 10, taxes: [vat]); +// The sale (aka Invoice) +final sale = Sale(items: [item], tip: 1); +// Calculate sale values. +print('SUBTOTAL ${sale.subtotal}'); +print('TAX ${sale.tax}'); +print('TOTAL ${sale.total}'); ``` - -## Additional information - -TODO: Tell users more about the package: where to find more information, how to -contribute to the package, how to file issues, what response they can expect -from the package authors, and more. diff --git a/example/salert_example.dart b/example/salert_example.dart index a4482e9..e2dab27 100644 --- a/example/salert_example.dart +++ b/example/salert_example.dart @@ -9,7 +9,7 @@ void ecuadorSriExample() { final ice = Tax(code: 'iceX', unitValue: 15, affectTax: true); // VAT tax with 13%. final vat = Tax(code: 'IVA13', unitValue: 13); - // Some item (product / service). + // Some item (product or service). final item = Item( code: 'productX', quantity: 2, diff --git a/lib/src/discount.dart b/lib/src/discount.dart index 96aef34..b030408 100644 --- a/lib/src/discount.dart +++ b/lib/src/discount.dart @@ -1,9 +1,14 @@ import 'dart:math'; +/// Represents a discount. class Discount { + /// Amount for calculations. final double amount; + /// Unitary applies calculation for every item. final bool isUnitary; + /// If amount represents a percentage. final bool isPercentage; + /// True if applies before taxes. final bool affectTax; Discount({ diff --git a/lib/src/item.dart b/lib/src/item.dart index a5177f9..3620fe1 100644 --- a/lib/src/item.dart +++ b/lib/src/item.dart @@ -66,6 +66,7 @@ class Item implements Sellable { return 0; } + /// Calculates tax amount. @override double get tax { return _taxAmountOf(subtotal); @@ -94,6 +95,7 @@ class Item implements Sellable { return baseSubtotal - subtotal; } + /// Calculates total amount. @override double get total { return subtotal + tax; diff --git a/lib/src/sale.dart b/lib/src/sale.dart index 69c2e1f..5f6a159 100644 --- a/lib/src/sale.dart +++ b/lib/src/sale.dart @@ -29,7 +29,7 @@ class Sale implements Sellable { .reduce((s1, s2) => s1 + s2); } - /// Calculates tax amount including [discount]. + /// Calculates tax amount. @override double get tax { return _items.map((item) => item.tax).reduce((s1, s2) => s1 + s2); @@ -51,9 +51,10 @@ class Sale implements Sellable { .reduce((s1, s2) => s1 + s2); } + /// Calculates total amount. @override double get total { - return subtotal + tax; + return subtotal + tax + tip; } List get _items { @@ -81,7 +82,7 @@ class Sale implements Sellable { final baseSubtotal = items.map((item) => item.subtotal).reduce((s1, s2) => s1 + s2); final baseTax = items.map((item) => item.tax).reduce((s1, s2) => s1 + s2); - final baseTotal = baseSubtotal + baseTax; + final baseTotal = baseSubtotal + baseTax + tip; var discountValue = discount!.discountOf(baseTotal); final discountedSubtotal = _inverseSubtotalOf(baseTotal - discountValue); discountValue = baseSubtotal - discountedSubtotal; diff --git a/lib/src/sellable.dart b/lib/src/sellable.dart index 5241daa..5221612 100644 --- a/lib/src/sellable.dart +++ b/lib/src/sellable.dart @@ -5,6 +5,7 @@ abstract class Sellable { /// Calculates subtotal amount including [discount] for given [taxCode] (before taxes). double subtotalOf(String taxCode); + /// Calculates tax amount. double get tax; /// Calculates tax amount including [discount] for given [taxCode]. @@ -13,5 +14,6 @@ abstract class Sellable { // Calculates discount amount before taxes. double get discountAmount; + /// Calculates total amount. double get total; }