calculator

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2017 License: MIT Imports: 3 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Coupon

type Coupon interface {
	ValidForType(string) bool
	ValidForPrice(string, uint64) bool
	ValidForProduct(string) bool
	PercentageDiscount() uint64
	FixedDiscount(string) uint64
}

Coupon is the interface for a coupon needed to do price calculation.

type FixedMemberDiscount

type FixedMemberDiscount struct {
	Amount   string `json:"amount"`
	Currency string `json:"currency"`
}

FixedMemberDiscount represents a fixed discount given to members.

type Item

type Item interface {
	ProductSku() string
	PriceInLowestUnit() uint64
	ProductType() string
	FixedVAT() uint64
	TaxableItems() []Item
	GetQuantity() uint64
}

Item is the interface for a single line item needed to do price calculation.

type ItemPrice

type ItemPrice struct {
	Quantity uint64

	Subtotal uint64
	Discount uint64
	Taxes    uint64
	Total    uint64
}

ItemPrice is the price of a single line item.

type MemberDiscount

type MemberDiscount struct {
	Claims       map[string]string      `json:"claims"`
	Percentage   uint64                 `json:"percentage"`
	FixedAmount  []*FixedMemberDiscount `json:"fixed"`
	ProductTypes []string               `json:"product_types"`
	Products     []string               `json:"products"`
}

MemberDiscount represents a discount given to members, either fixed or a percentage.

func (*MemberDiscount) FixedDiscount

func (d *MemberDiscount) FixedDiscount(currency string) uint64

FixedDiscount returns what the fixed discount amount is for a particular currency.

func (*MemberDiscount) ValidForProduct added in v1.2.0

func (d *MemberDiscount) ValidForProduct(productSku string) bool

ValidForProduct returns whether a member discount is valid for a product sku

func (*MemberDiscount) ValidForType

func (d *MemberDiscount) ValidForType(productType string) bool

ValidForType returns whether a member discount is valid for a product type.

type Price

type Price struct {
	Items []ItemPrice

	Subtotal uint64
	Discount uint64
	Taxes    uint64
	Total    uint64
}

Price represents the total price of all line items.

func CalculatePrice

func CalculatePrice(settings *Settings, jwtClaims map[string]interface{}, country, currency string, coupon Coupon, items []Item) Price

CalculatePrice will calculate the final total price. It takes into account currency, country, coupons, and discounts.

type Settings

type Settings struct {
	PricesIncludeTaxes bool              `json:"prices_include_taxes"`
	Taxes              []*Tax            `json:"taxes"`
	MemberDiscounts    []*MemberDiscount `json:"member_discounts"`
}

Settings represent the site-wide settings for price calculation.

type Tax

type Tax struct {
	Percentage   uint64   `json:"percentage"`
	ProductTypes []string `json:"product_types"`
	Countries    []string `json:"countries"`
}

Tax represents a tax, potentially specific to countries and product types.

func (*Tax) AppliesTo

func (t *Tax) AppliesTo(country, productType string) bool

AppliesTo determines if the tax applies to the country AND product type provided.

Jump to

Keyboard shortcuts

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