tax

package
v0.22.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2022 License: Apache-2.0 Imports: 11 Imported by: 14

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterRegion added in v0.20.0

func RegisterRegion(region *Region)

RegisterRegion adds a new region to the shared global list of tax regions.

Types

type Category

type Category struct {
	Code Code        `json:"code" jsonschema:"title=Code"`
	Name i18n.String `json:"name" jsonschema:"title=Name"`
	Desc i18n.String `json:"desc,omitempty" jsonschema:"title=Description"`

	// Retained when true implies that the tax amount will be retained
	// by the buyer on behalf of the supplier, and thus subtracted from
	// the invoice taxable base total. Typically used for taxes related to
	// income.
	Retained bool `json:"retained,omitempty" jsonschema:"title=Retained"`

	// Specific tax definitions inside this category.
	Rates []*Rate `json:"rates" jsonschema:"title=Rates"`
}

Category contains the definition of a general type of tax inside a region.

func (*Category) Rate added in v0.20.0

func (c *Category) Rate(key Key) *Rate

Rate provides the rate definition with a matching key for the category.

func (*Category) Validate

func (c *Category) Validate() error

Validate ensures the Category's contents are correct.

type CategoryTotal

type CategoryTotal struct {
	Code     Code         `json:"code" jsonschema:"title=Code"`
	Retained bool         `json:"retained,omitempty" jsonschema:"title=Retained"`
	Rates    []*RateTotal `json:"rates" jsonschema:"title=Rates"`
	Base     num.Amount   `json:"base" jsonschema:"title=Base"`
	Amount   num.Amount   `json:"amount" jsonschema:"title=Amount"`
}

CategoryTotal groups together all rates inside a given category.

func NewCategoryTotal

func NewCategoryTotal(code Code, retained bool, zero num.Amount) *CategoryTotal

NewCategoryTotal prepares a category total calculation.

func (*CategoryTotal) Rate added in v0.13.0

func (ct *CategoryTotal) Rate(key Key) *RateTotal

Rate grabs the matching rate from the category total, or nil.

type Code

type Code string

Code represents a string used to uniquely identify the data we're looking at. We use "code" instead of "id", to reenforce the fact that codes should be more easily set and used by humans within definitions than IDs or UUIDs. Tax codes are standardised so that when validated they must contain between 2 and 6 inclusive upper-case letters or numbers.

func (Code) IsEmpty added in v0.13.0

func (c Code) IsEmpty() bool

IsEmpty returns true if no code is specified.

func (Code) String added in v0.22.1

func (c Code) String() string

String returns string representation of code.

func (Code) Validate

func (c Code) Validate() error

Validate ensures that the code complies with the expected rules.

type Combo added in v0.21.0

type Combo struct {
	// Tax category code from those available inside a region.
	Category Code `json:"cat" jsonschema:"title=Category"`
	// Rate within a category to apply.
	Rate Key `json:"rate" jsonschema:"title=Rate"`
	// contains filtered or unexported fields
}

Combo represents the tax combination of a category code and rate key.

func (*Combo) Validate added in v0.21.0

func (c *Combo) Validate() error

Validate ensures the Combo contains all the details required.

type Error added in v0.22.1

type Error Key

Error is a general wrapper around tax errors produced during run time, typically during calculations. Not to be confused with errors produced from definition validation.

const (
	ErrInvalidCategory      Error = "invalid-category"
	ErrInvalidRate          Error = "invalid-rate"
	ErrInvalidDate          Error = "invalid-date"
	ErrInvalidPricesInclude Error = "invalid-prices-include"
)

Standard list of tax errors

func (Error) Error added in v0.22.1

func (e Error) Error() string

Error serializes the error's message.

func (Error) WithMessage added in v0.22.1

func (e Error) WithMessage(msg string, s ...interface{}) error

WithMessage wraps around the original error so we can use if for matching and adds a human message.

type Key added in v0.20.0

type Key string

Key is used to define an ID or code that more closely represents a human name.

func (Key) String added in v0.22.1

func (k Key) String() string

String provides string representation of key

func (Key) Validate added in v0.20.0

func (k Key) Validate() error

Validate ensures the key complies with the basic syntax requirements.

type Rate

type Rate struct {
	// Key identifies this rate within the system
	Key Key `json:"key" jsonschema:"title=Key"`

	Name i18n.String `json:"name" jsonschema:"title=Name"`
	Desc i18n.String `json:"desc,omitempty" jsonschema:"title=Description"`

	// Values contains a list of Value objects that contain the
	// current and historical percentage values for the rate;
	// order is important, newer values should come before
	// older values.
	Values []*RateValue `json:"values" jsonschema:"title=Values"`
}

Rate defines a single rate inside a category

func (*Rate) On added in v0.20.0

func (r *Rate) On(date cal.Date) *RateValue

On determines the tax rate value for the provided date.

func (*Rate) Validate

func (r *Rate) Validate() error

Validate checks that our tax definition is valid. This is only really meant to be used when testing new regional tax definitions.

type RateTotal

type RateTotal struct {
	Key     Key            `json:"key" jsonschema:"title=Key"`
	Base    num.Amount     `json:"base" jsonschema:"title=Base"`
	Percent num.Percentage `json:"percent" jsonschema:"title=Percent"`
	Amount  num.Amount     `json:"amount" jsonschema:"title=Amount"`
}

RateTotal contains a sum of all the tax rates in the document with a matching category and definition.

func NewRateTotal

func NewRateTotal(key Key, percent num.Percentage, zero num.Amount) *RateTotal

NewRateTotal returns a rate total.

type RateValue added in v0.20.0

type RateValue struct {
	// Date from which this value should be applied.
	Since *cal.Date `json:"since,omitempty" jsonschema:"title=Since"`
	// Rate that should be applied
	Percent num.Percentage `json:"percent" jsonschema:"title=Percent"`
	// When true, this value should no longer be used.
	Disabled bool `json:"disabled,omitempty" jsonschema:"title=Disabled"`
}

RateValue contains a percentage rate or fixed amount for a given date range. Fiscal policy changes mean that rates are not static so we need to be able to apply the correct rate for a given period.

func (*RateValue) Validate added in v0.20.0

func (v *RateValue) Validate() error

Validate ensures the tax rate contains all the required fields.

type Region

type Region struct {
	// Name of the region
	Name i18n.String `json:"name" jsonschema:"title=Name"`

	// Country code for the region
	Country l10n.Code `json:"country" jsonschema:"title=Code"`
	// Locality, city, region, or similar code inside the country, if needed.
	Locality l10n.Code `json:"locality,omitempty" jsonschema:"title=Locality"`

	// Currency used by the region for tax purposes.
	Currency currency.Code `json:"currency" jsonschema:"title=Currency"`

	// Set of specific scheme definitions inside the region.
	Schemes Schemes `json:"schemes,omitempty" jsonschema:"title=Schemes"`

	// List of tax categories.
	Categories []*Category `json:"categories" jsonschema:"title=Categories"`

	// ValidateDocument is a method to use to validate a document in a given region.
	ValidateDocument func(doc interface{}) error `json:"-"`
}

Region defines the holding structure for a regions categories and subsequent Rates and Values.

func AllRegions added in v0.20.0

func AllRegions() []*Region

AllRegions provides an array of all the region codes to definitions.

func RegionFor added in v0.20.0

func RegionFor(country, locality l10n.Code) *Region

RegionFor returns the region definition for country and locality combination or nil if no match was found.

func (*Region) Category

func (r *Region) Category(code Code) *Category

Category provides the requested category by its code.

func (*Region) CurrencyDef added in v0.20.0

func (r *Region) CurrencyDef() *currency.Def

CurrencyDef provides the currency definition object for the region.

func (*Region) Validate

func (r *Region) Validate() error

Validate enures the region definition is valid, including all subsequent categories.

type Scheme added in v0.20.0

type Scheme struct {
	// Key used to identify this scheme
	Key Key `json:"key" jsonschema:"title=Key"`

	// Name of this scheme.
	Name i18n.String `json:"name" jsonschema:"title=Name"`
	// Human details describing what this scheme is used for.
	Description i18n.String `json:"description,omitempty" jsonschema:"title=Description"`

	// List of tax category codes that can be used when this scheme is
	// applied.
	Categories []Code `json:"categories,omitempty" jsonschema:"title=Category Codes"`

	// Notes defines messages that should be added to a document
	// when this scheme is used.
	Note *org.Note `json:"note,omitempty" jsonschema:"title=Note"`
}

Scheme contains the definition of a scheme that belongs to a region and can be used to simplify validation processes for document contents.

type SchemeKeys added in v0.20.0

type SchemeKeys []Key

SchemeKeys stores a list of keys that makes it easier to perform matches.

func (SchemeKeys) Contains added in v0.20.0

func (sk SchemeKeys) Contains(key Key) bool

Contains returns true if the list of keys contains a match for the provided key.

type Schemes added in v0.20.0

type Schemes []*Scheme

Schemes defines an array of scheme objects with helper functions.

func (Schemes) ForKey added in v0.20.0

func (ss Schemes) ForKey(key Key) *Scheme

ForKey finds the scheme with a matching key.

type Set added in v0.21.0

type Set []*Combo

Set defines a list of tax categories and their rates to be used alongside taxable items.

func (Set) Equals added in v0.21.0

func (s Set) Equals(s2 Set) bool

Equals returns true if the sets match, regardless of order.

func (Set) Get added in v0.21.0

func (s Set) Get(cat Code) *Combo

Get the Rate key for the given category

func (Set) Rate added in v0.21.0

func (s Set) Rate(cat Code) Key

Rate returns the rate from the matching category, if set.

func (Set) Validate added in v0.21.0

func (s Set) Validate() error

Validate ensures the set of tax combos looks correct

type TaxableLine

type TaxableLine interface {
	GetTaxes() Set
	GetTotal() num.Amount
}

TaxableLine defines what we expect from a line in order to subsequently calculate the taxes that need to be added or retained.

type Total

type Total struct {
	// Grouping of all the taxes by their category
	Categories []*CategoryTotal `json:"categories,omitempty" jsonschema:"title=Categories"`
	// Total value of all the taxes applied.
	Sum num.Amount `json:"sum" jsonschema:"title=Sum"`
}

Total contains a set of Category Totals which in turn contain all the accumulated taxes contained in the document. The resulting `sum` is that value that should be added to the payable total.

func NewTotal

func NewTotal(zero num.Amount) *Total

NewTotal initiates a new total instance.

func (*Total) Calculate

func (t *Total) Calculate(reg *Region, lines []TaxableLine, taxIncluded Code, date cal.Date, zero num.Amount) error

Calculate figures out the total taxes for the set of `TaxableLine`s provided.

func (*Total) Category added in v0.13.0

func (t *Total) Category(code Code) *CategoryTotal

Category provides the category total for the matching code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL