domain

package
v3.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: MIT Imports: 7 Imported by: 19

Documentation

Index

Constants

View Source
const (
	// ChargeTypeGiftCard  used as a charge type for gift cards
	ChargeTypeGiftCard = "giftcard"
	// ChargeTypeMain used as default for a Charge
	ChargeTypeMain = "main"

	// RoundingModeFloor use if you want to cut (round down)
	RoundingModeFloor = "floor"
	// RoundingModeCeil use if you want to round up always
	RoundingModeCeil = "ceil"
	// RoundingModeHalfUp round up if the discarded fraction is ≥ 0.5, otherwise round down. Default for GetPayable()
	RoundingModeHalfUp = "halfup"
	// RoundingModeHalfDown round up if the discarded fraction is > 0.5, otherwise round down.
	RoundingModeHalfDown = "halfdown"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Charge

type Charge struct {
	// Price that is paid, can be in a certain currency
	Price Price
	// Value of the "Price" in another (base) currency
	Value Price
	// Type of the charge - can be ChargeTypeMain or something else. Used to differentiate between different charges of a single thing
	Type string
	// Reference contains further information to distinguish charges of the same type
	Reference string
}

Charge is a Amount of a certain Type. Charge is used as value object

func (Charge) Add

func (p Charge) Add(add Charge) (Charge, error)

Add the given Charge to the current Charge and returns a new Charge

func (Charge) GetPayable

func (p Charge) GetPayable() Charge

GetPayable rounds the charge

func (Charge) Mul

func (p Charge) Mul(qty int) Charge

Mul the given Charge and returns a new Charge

type ChargeQualifier

type ChargeQualifier struct {
	// Type represents charge type
	Type string
	// Reference contains further information to distinguish charges of the same type
	Reference string
}

ChargeQualifier distinguishes charges by type and reference

type Charges

type Charges struct {
	// contains filtered or unexported fields
}

Charges - Represents the Charges the product need to be paid with

func NewCharges

func NewCharges(chargesByType map[string]Charge) *Charges

NewCharges creates a new Charges object

func (Charges) Add

func (c Charges) Add(toadd Charges) Charges

Add returns new Charges with the given added

func (Charges) AddCharge

func (c Charges) AddCharge(toadd Charge) Charges

AddCharge returns new Charges with the given Charge added

func (Charges) GetAllByType

func (c Charges) GetAllByType(ctype string) map[ChargeQualifier]Charge

GetAllByType returns all charges of type

func (Charges) GetAllCharges

func (c Charges) GetAllCharges() map[ChargeQualifier]Charge

GetAllCharges returns all charges

func (Charges) GetByChargeQualifier

func (c Charges) GetByChargeQualifier(qualifier ChargeQualifier) (Charge, bool)

GetByChargeQualifier returns a charge of given qualifier. If it was not found a Zero amount is returned and the second return value is false

func (Charges) GetByChargeQualifierForced

func (c Charges) GetByChargeQualifierForced(qualifier ChargeQualifier) Charge

GetByChargeQualifierForced returns a charge of given qualifier. If it was not found a Zero amount is returned. This method might be useful to call in View (template) directly.

func (Charges) GetByType

func (c Charges) GetByType(ctype string) (Charge, bool)

GetByType returns a charge of given type. If it was not found a Zero amount is returned and the second return value is false sums up charges by a certain type if there are multiple

func (Charges) GetByTypeForced

func (c Charges) GetByTypeForced(ctype string) Charge

GetByTypeForced returns a charge of given type. If it was not found a Zero amount is returned. This method might be useful to call in View (template) directly where you need one return value sums up charges by a certain type if there are multiple

func (Charges) HasChargeQualifier

func (c Charges) HasChargeQualifier(qualifier ChargeQualifier) bool

HasChargeQualifier returns a true if any charges include a charge with given type and concrete key values provided by additional

func (Charges) HasType

func (c Charges) HasType(ctype string) bool

HasType returns a true if any charges include a charge with given type

func (Charges) Items added in v3.0.1

func (c Charges) Items() []Charge

Items returns all charges items

func (Charges) Mul

func (c Charges) Mul(qty int) Charges

Mul returns new Charges with the given multiplied

type Price

type Price struct {
	// contains filtered or unexported fields
}

Price is a Type that represents a Amount - it is immutable DevHint: We use Amount and Charge as Value - so we do not pass pointers. (According to Go Wiki's code review comments page suggests passing by value when structs are small and likely to stay that way)

func NewFromBigFloat

func NewFromBigFloat(amount big.Float, currency string) Price

NewFromBigFloat - factory method

func NewFromFloat

func NewFromFloat(amount float64, currency string) Price

NewFromFloat - factory method

func NewFromInt

func NewFromInt(amount int64, precision int, currency string) Price

NewFromInt use to set money by smallest payable unit - e.g. to set 2.45 EUR you should use NewFromInt(245, 100, "EUR")

func NewZero

func NewZero(currency string) Price

NewZero Zero price

func SumAll

func SumAll(prices ...Price) (Price, error)

SumAll returns new price with sum of all given prices

func (Price) Add

func (p Price) Add(add Price) (Price, error)

Add the given price to the current price and returns a new price

func (Price) Amount

func (p Price) Amount() *big.Float

Amount returns exact amount as bigFloat

func (Price) Clone

func (p Price) Clone() Price

Clone returns a copy of the price - the amount gets Excat acc

func (Price) Currency

func (p Price) Currency() string

Currency returns currency

func (Price) Discounted

func (p Price) Discounted(percent float64) Price

Discounted returns new price reduced by given percent

func (Price) Divided

func (p Price) Divided(qty int) Price

Divided returns a new price with the amount Divided

func (Price) Equal

func (p Price) Equal(cmp Price) bool

Equal compares the prices exact

func (Price) FloatAmount

func (p Price) FloatAmount() float64

FloatAmount gets the current amount as float

func (Price) ForceAdd

func (p Price) ForceAdd(add Price) Price

ForceAdd tries to add the given price to the current price - will not return errors

func (Price) GetPayable

func (p Price) GetPayable() Price

GetPayable rounds the price with the precision required by the currency in a price that can actually be paid e.g. an internal amount of 1,23344 will get rounded to 1,23

func (Price) GetPayableByRoundingMode

func (p Price) GetPayableByRoundingMode(mode string, precision int) Price

GetPayableByRoundingMode returns the price rounded you can pass the used rounding mode and precision Example for precision 100:

1.115 >  1.12 (RoundingModeHalfUp)  / 1.11 (RoundingModeFloor)
-1.115 > -1.11 (RoundingModeHalfUp) / -1.12 (RoundingModeFloor)

func (Price) Inverse

func (p Price) Inverse() Price

Inverse returns the price multiplied with -1

func (Price) IsGreaterThen

func (p Price) IsGreaterThen(cmp Price) bool

IsGreaterThen compares the current price with a given one

func (Price) IsGreaterThenValue

func (p Price) IsGreaterThenValue(amount big.Float) bool

IsGreaterThenValue compares the price with a given amount value (assuming same currency)

func (Price) IsLessThen

func (p Price) IsLessThen(cmp Price) bool

IsLessThen compares the current price with a given one

func (Price) IsLessThenValue

func (p Price) IsLessThenValue(amount big.Float) bool

IsLessThenValue compares the price with a given amount value (assuming same currency)

func (Price) IsNegative

func (p Price) IsNegative() bool

IsNegative returns true if the price represents a negative value

func (Price) IsPayable

func (p Price) IsPayable() bool

IsPayable returns true if the price represents a payable (rounded) value

func (Price) IsPositive

func (p Price) IsPositive() bool

IsPositive returns true if the price represents a positive value

func (Price) IsZero

func (p Price) IsZero() bool

IsZero returns true if the price represents zero value

func (Price) LikelyEqual

func (p Price) LikelyEqual(cmp Price) bool

LikelyEqual compares the prices with some tolerance

func (Price) MarshalBinary

func (p Price) MarshalBinary() (data []byte, err error)

MarshalBinary implements interface required by gob

func (Price) MarshalJSON

func (p Price) MarshalJSON() (data []byte, err error)

MarshalJSON implements interface required by json marshal

func (Price) Multiply

func (p Price) Multiply(qty int) Price

Multiply returns a new price with the amount Multiply

func (Price) SplitInPayables

func (p Price) SplitInPayables(count int) ([]Price, error)

SplitInPayables returns "count" payable prices (each rounded) that in sum matches the given price

  • Given a price of 12.456 (Payable 12,46) - Splitted in 6 will mean: 6 * 2.076
  • but having them payable requires rounding them each (e.g. 2.07) which would mean we have 0.03 difference (=12,45-6*2.07)
  • so that the sum is as close as possible to the original value in this case the correct return will be:
  • 2.07 + 2.07+2.08 +2.08 +2.08 +2.08

func (Price) Sub

func (p Price) Sub(sub Price) (Price, error)

Sub the given price from the current price and returns a new price

func (Price) TaxFromGross

func (p Price) TaxFromGross(percent big.Float) Price

TaxFromGross returns new price representing the tax amount (assuming the current price is gross 100+percent)

func (Price) TaxFromNet

func (p Price) TaxFromNet(percent big.Float) Price

TaxFromNet returns new price representing the tax amount (assuming the current price is net 100%)

func (Price) Taxed

func (p Price) Taxed(percent big.Float) Price

Taxed returns new price added with Tax (assuming current price is net)

func (*Price) UnmarshalBinary

func (p *Price) UnmarshalBinary(data []byte) error

UnmarshalBinary implements interface required by gob. Modifies the receiver so it must take a pointer receiver!

func (*Price) UnmarshalJSON added in v3.4.0

func (p *Price) UnmarshalJSON(data []byte) error

UnmarshalJSON implements encode Unmarshaler

Jump to

Keyboard shortcuts

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