deduction

package
v1.2.7 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package deduction provides functionality for calculating remaining amounts in subscription billing systems, supporting various time units and traffic-based calculations.

Index

Constants

View Source
const (
	// Time unit constants for subscription billing
	UnitTimeNoLimit = "NoLimit" // Unlimited time subscription
	UnitTimeYear    = "Year"    // Annual subscription
	UnitTimeMonth   = "Month"   // Monthly subscription
	UnitTimeDay     = "Day"     // Daily subscription
	UnitTimeHour    = "Hour"    // Hourly subscription
	UnitTimeMinute  = "Minute"  // Per-minute subscription

	// Reset cycle constants for traffic resets
	ResetCycleNone    = 0 // No reset cycle
	ResetCycle1st     = 1 // Reset on 1st of each month
	ResetCycleMonthly = 2 // Reset monthly based on start date
	ResetCycleYear    = 3 // Reset yearly based on start date

)

Variables

View Source
var (
	ErrInvalidQuantity       = errors.New("order quantity cannot be zero or negative")
	ErrInvalidAmount         = errors.New("order amount cannot be negative")
	ErrInvalidTraffic        = errors.New("traffic values cannot be negative")
	ErrInvalidTimeRange      = errors.New("expire time must be after start time")
	ErrInvalidUnitTime       = errors.New("invalid unit time")
	ErrInvalidDeductionRatio = errors.New("deduction ratio must be between 0 and 100")
	ErrOverflow              = errors.New("calculation overflow")
)

Error definitions for validation and calculation failures

Functions

func CalculateRemainingAmount

func CalculateRemainingAmount(sub Subscribe, order Order) (int64, error)

CalculateRemainingAmount calculates the remaining refund amount for a subscription based on unused time and traffic. Returns the amount and any calculation errors.

Types

type Order

type Order struct {
	Amount   int64 // Total order amount
	Quantity int64 // Order quantity
}

Order represents a purchase order for subscription calculation

func (*Order) Validate added in v1.0.2

func (o *Order) Validate() error

Validate checks if the Order struct contains valid data

type Subscribe

type Subscribe struct {
	StartTime      time.Time // Subscription start time
	ExpireTime     time.Time // Subscription expiration time
	Traffic        int64     // Total traffic allowance in bytes
	Download       int64     // Downloaded traffic in bytes
	Upload         int64     // Uploaded traffic in bytes
	UnitTime       string    // Time unit for billing (Year, Month, Day, etc.)
	UnitPrice      int64     // Price per unit time
	ResetCycle     int64     // Traffic reset cycle
	DeductionRatio int64     // Deduction ratio for weighted calculations (0-100)
}

Subscribe represents a subscription with time and traffic limits

func (*Subscribe) Validate added in v1.0.2

func (s *Subscribe) Validate() error

Validate checks if the Subscribe struct contains valid data

Jump to

Keyboard shortcuts

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