Use decimal.Decimal to represent floats more easily
This commit is contained in:
+4
-2
@@ -3,6 +3,8 @@ package cmd
|
||||
import (
|
||||
"expenses/config"
|
||||
"expenses/pages"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"fmt"
|
||||
@@ -22,8 +24,8 @@ var (
|
||||
"formatDate": func(ts int64) string {
|
||||
return time.Unix(ts, 0).Format("02-Jan-2006")
|
||||
},
|
||||
"formatPrice": func(v float64) string {
|
||||
return fmt.Sprintf("%.2f", v)
|
||||
"formatPrice": func(v decimal.Decimal) string {
|
||||
return fmt.Sprintf("%s", v.Round(2).StringFixed(2))
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
-- expenses
|
||||
CREATE TABLE expenses_new (
|
||||
"ExpID" integer PRIMARY KEY AUTOINCREMENT,
|
||||
"Description" text NOT NULL,
|
||||
"Value" DECIMAL(10,2) NOT NULL DEFAULT 0,
|
||||
"ExpID" INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
"Description" TEXT NOT NULL,
|
||||
"Value" TEXT NOT NULL DEFAULT 0,
|
||||
"StoreID" int NOT NULL DEFAULT 0,
|
||||
"CategoryID" int NOT NULL DEFAULT 0,
|
||||
"OwnerUserID" int NOT NULL DEFAULT 0,
|
||||
@@ -25,7 +25,7 @@ CREATE TABLE expensesShares_new (
|
||||
"ExpShareID" integer PRIMARY KEY AUTOINCREMENT,
|
||||
"ExpID" int NOT NULL,
|
||||
"UserID" int NOT NULL,
|
||||
"Share" DECIMAL(10,2) NOT NULL DEFAULT 0.5,
|
||||
"Share" TEXT NOT NULL DEFAULT 0.5,
|
||||
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT,
|
||||
FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT
|
||||
);
|
||||
@@ -38,7 +38,7 @@ CREATE TABLE expensesPayments_new (
|
||||
"ExpPaymID" integer PRIMARY KEY AUTOINCREMENT,
|
||||
"ExpID" int NOT NULL,
|
||||
"UserID" int NOT NULL,
|
||||
"Payed" DECIMAL(10,2) NOT NULL DEFAULT 0,
|
||||
"Payed" TEXT NOT NULL DEFAULT 0,
|
||||
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT,
|
||||
FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT
|
||||
);
|
||||
|
||||
@@ -163,7 +163,7 @@ VALUES(?, ?, ? , ?, ?, ?, ?, ?)
|
||||
|
||||
type InsertExpenseParams struct {
|
||||
Description string
|
||||
Value float64
|
||||
Value string
|
||||
StoreID int64
|
||||
CategoryID int64
|
||||
TypeID int64
|
||||
@@ -200,7 +200,7 @@ WHERE "ExpID" = ?
|
||||
|
||||
type UpdateExpenseParams struct {
|
||||
Description string
|
||||
Value float64
|
||||
Value string
|
||||
StoreID int64
|
||||
CategoryID int64
|
||||
TypeID int64
|
||||
|
||||
@@ -12,7 +12,7 @@ type Category struct {
|
||||
type Expense struct {
|
||||
ExpID int64
|
||||
Description string
|
||||
Value float64
|
||||
Value string
|
||||
StoreID int64
|
||||
CategoryID int64
|
||||
OwnerUserID int64
|
||||
@@ -30,14 +30,14 @@ type ExpensesPayment struct {
|
||||
ExpPaymID int64
|
||||
ExpID int64
|
||||
UserID int64
|
||||
Payed float64
|
||||
Payed string
|
||||
}
|
||||
|
||||
type ExpensesShare struct {
|
||||
ExpShareID int64
|
||||
ExpID int64
|
||||
UserID int64
|
||||
Share float64
|
||||
Share string
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
|
||||
@@ -74,7 +74,7 @@ VALUES(?, ?, ?)
|
||||
type InsertPaymentParams struct {
|
||||
ExpID int64
|
||||
UserID int64
|
||||
Payed float64
|
||||
Payed string
|
||||
}
|
||||
|
||||
func (q *Queries) InsertPayment(ctx context.Context, arg InsertPaymentParams) (sql.Result, error) {
|
||||
@@ -89,7 +89,7 @@ WHERE "ExpPaymID" = ?
|
||||
|
||||
type UpdatePaymentParams struct {
|
||||
UserID int64
|
||||
Payed float64
|
||||
Payed string
|
||||
ExpPaymID int64
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ VALUES(?, ?, ?)
|
||||
type InsertShareParams struct {
|
||||
ExpID int64
|
||||
UserID int64
|
||||
Share float64
|
||||
Share string
|
||||
}
|
||||
|
||||
func (q *Queries) InsertShare(ctx context.Context, arg InsertShareParams) (sql.Result, error) {
|
||||
@@ -81,7 +81,7 @@ WHERE "ExpShareID" = ?
|
||||
|
||||
type UpdateShareParams struct {
|
||||
UserID int64
|
||||
Share float64
|
||||
Share string
|
||||
ExpShareID int64
|
||||
}
|
||||
|
||||
|
||||
@@ -11,12 +11,3 @@ sql:
|
||||
expenseTypes: "ExpenseTypes"
|
||||
expenseShares: "ExpenseShares"
|
||||
expensePayments: "ExpensePayments"
|
||||
overrides:
|
||||
- db_type: "float"
|
||||
go_type:
|
||||
import: "github.com/shopspring/decimal"
|
||||
type: "DECIMAL"
|
||||
- db_type: "DECIMAL"
|
||||
go_type:
|
||||
import: "github.com/shopspring/decimal"
|
||||
type: "DECIMAL"
|
||||
|
||||
@@ -2,20 +2,24 @@ package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type Debt struct {
|
||||
Debtor mod.User
|
||||
Sum float64
|
||||
Sum decimal.Decimal
|
||||
}
|
||||
|
||||
func sortBySum(a, b Debt) int {
|
||||
if a.Sum < b.Sum {
|
||||
if a.Sum.LessThan(b.Sum) {
|
||||
return -1
|
||||
}
|
||||
if a.Sum > b.Sum {
|
||||
|
||||
if a.Sum.GreaterThan(b.Sum) {
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
@@ -2,18 +2,20 @@ package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type DebtCalculator struct {
|
||||
Shares map[mod.User]float64
|
||||
Payments map[mod.User]float64
|
||||
Shares map[mod.User]decimal.Decimal
|
||||
Payments map[mod.User]decimal.Decimal
|
||||
Expense *mod.Expense
|
||||
}
|
||||
|
||||
func NewDebtCalculator(e *mod.Expense) *DebtCalculator {
|
||||
return &DebtCalculator{
|
||||
Shares: make(map[mod.User]float64),
|
||||
Payments: make(map[mod.User]float64),
|
||||
Shares: make(map[mod.User]decimal.Decimal),
|
||||
Payments: make(map[mod.User]decimal.Decimal),
|
||||
Expense: e,
|
||||
}
|
||||
}
|
||||
@@ -21,13 +23,13 @@ func NewDebtCalculator(e *mod.Expense) *DebtCalculator {
|
||||
func (dc *DebtCalculator) mapShares() {
|
||||
for _, share := range dc.Expense.Shares {
|
||||
dc.Shares[share.User] = share.Share
|
||||
dc.Payments[share.User] = 0
|
||||
dc.Payments[share.User] = decimal.NewFromInt(0)
|
||||
}
|
||||
}
|
||||
|
||||
func (dc *DebtCalculator) mapPayments() {
|
||||
for _, payment := range dc.Expense.Payments {
|
||||
dc.Payments[payment.User] += payment.PayedAmount
|
||||
dc.Payments[payment.User] = dc.Payments[payment.User].Add(payment.PayedAmount)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,9 +37,10 @@ func (dc *DebtCalculator) getDebts() []Debt {
|
||||
debts := []Debt{}
|
||||
|
||||
for user := range dc.Payments {
|
||||
debt := (dc.Shares[user] * dc.Expense.Value) - dc.Payments[user]
|
||||
// debt := (dc.Shares[user] * dc.Expense.Value) - dc.Payments[user]
|
||||
debt := (dc.Shares[user].Mul(dc.Expense.Value)).Sub(dc.Payments[user])
|
||||
|
||||
if debt > 0 {
|
||||
if debt.GreaterThan(decimal.NewFromInt(0)) {
|
||||
debts = append(debts, Debt{Debtor: user, Sum: debt})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@ package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
"strconv"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
// TODO
|
||||
@@ -13,12 +14,12 @@ func ParseFormPayments(userIDs []string, paymentsIDs []string,
|
||||
values []string) ([]mod.ExpensePayment, error) {
|
||||
payments := []mod.ExpensePayment{}
|
||||
for k := range userIDs {
|
||||
userID, err := strconv.ParseInt(userIDs[k], 10, 16)
|
||||
userID, err := ParseID(userIDs[k])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payed, err := strconv.ParseFloat(values[k], 32)
|
||||
payed, err := decimal.NewFromString(values[k])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -32,7 +33,7 @@ func ParseFormPayments(userIDs []string, paymentsIDs []string,
|
||||
}
|
||||
|
||||
if paymentsIDs[k] != "" {
|
||||
id, err := strconv.ParseInt(paymentsIDs[k], 10, 16)
|
||||
id, err := ParseID(paymentsIDs[k])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
// TODO
|
||||
@@ -14,12 +14,12 @@ func ParseFormShares(userIDs []string, shares []string, sharesIDs []string,
|
||||
) ([]mod.ExpenseShare, error) {
|
||||
shareList := []mod.ExpenseShare{}
|
||||
for i := range userIDs {
|
||||
userID, err := strconv.ParseInt(userIDs[i], 10, 16)
|
||||
userID, err := ParseID(userIDs[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
share, err := strconv.ParseFloat(shares[i], 32)
|
||||
share, err := decimal.NewFromString(shares[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func ParseFormShares(userIDs []string, shares []string, sharesIDs []string,
|
||||
}
|
||||
|
||||
if sharesIDs[i] != "" {
|
||||
id, err := strconv.ParseInt(sharesIDs[i], 10, 16)
|
||||
id, err := ParseID(sharesIDs[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -7,12 +7,14 @@ import (
|
||||
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type Expense struct {
|
||||
ExpID int64
|
||||
Description string
|
||||
Value float64
|
||||
Value decimal.Decimal
|
||||
Store Store
|
||||
Type Type
|
||||
Category Category
|
||||
@@ -27,7 +29,7 @@ func NewExpense() Expense {
|
||||
return Expense{
|
||||
ExpID: -1,
|
||||
Description: "",
|
||||
Value: 0.0,
|
||||
Value: decimal.NewFromFloat(0.0),
|
||||
Store: NewStore(),
|
||||
Category: NewCategory(),
|
||||
Owner: NewUser(),
|
||||
@@ -71,7 +73,7 @@ func (exp *Expense) Insert(tx *sql.Tx) error {
|
||||
queries := repo.New(tx)
|
||||
res, err := queries.InsertExpense(ctx, repo.InsertExpenseParams{
|
||||
Description: exp.Description,
|
||||
Value: exp.Value,
|
||||
Value: exp.Value.String(),
|
||||
StoreID: exp.Store.StoreID,
|
||||
CategoryID: exp.Category.CategoryID,
|
||||
TypeID: exp.Type.TypeID,
|
||||
@@ -137,7 +139,7 @@ func (e *Expense) Update(tx *sql.Tx) error {
|
||||
res, err := queries.UpdateExpense(ctx, repo.UpdateExpenseParams{
|
||||
ExpID: e.ExpID,
|
||||
Description: e.Description,
|
||||
Value: e.Value,
|
||||
Value: e.Value.String(),
|
||||
StoreID: e.Store.StoreID,
|
||||
CategoryID: e.Category.CategoryID,
|
||||
TypeID: e.Type.TypeID,
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
package models
|
||||
|
||||
import repo "expenses/expenses/db/repository"
|
||||
import (
|
||||
repo "expenses/expenses/db/repository"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func mapRepoGetPaymentsRow(s repo.GetPaymentsRow) ExpensePayment {
|
||||
payed, _ := decimal.NewFromString(s.ExpensesPayment.Payed)
|
||||
|
||||
return ExpensePayment{
|
||||
ExpPaymID: s.ExpensesPayment.ExpPaymID,
|
||||
PayedAmount: s.ExpensesPayment.Payed,
|
||||
PayedAmount: payed,
|
||||
User: User{
|
||||
UserID: s.User.UserID,
|
||||
UserName: s.User.UserName,
|
||||
|
||||
@@ -9,12 +9,13 @@ import (
|
||||
"fmt"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type ExpensePayment struct {
|
||||
ExpPaymID int64
|
||||
User User
|
||||
PayedAmount float64
|
||||
PayedAmount decimal.Decimal
|
||||
}
|
||||
|
||||
func (e *Expense) GetPayments() error {
|
||||
@@ -44,7 +45,7 @@ func (p *ExpensePayment) Insert(tx *sql.Tx, expID int64) error {
|
||||
res, err := queries.InsertPayment(ctx, repo.InsertPaymentParams{
|
||||
ExpID: expID,
|
||||
UserID: p.User.UserID,
|
||||
Payed: p.PayedAmount,
|
||||
Payed: p.PayedAmount.String(),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -73,7 +74,7 @@ func (p *ExpensePayment) Update() error {
|
||||
queries := repo.New(db)
|
||||
res, err := queries.UpdatePayment(ctx, repo.UpdatePaymentParams{
|
||||
ExpPaymID: p.ExpPaymID,
|
||||
Payed: p.PayedAmount,
|
||||
Payed: p.PayedAmount.String(),
|
||||
UserID: p.User.UserID,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
package models
|
||||
|
||||
import repo "expenses/expenses/db/repository"
|
||||
import (
|
||||
repo "expenses/expenses/db/repository"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func mapRepoGetExpenseRow(e repo.GetExpenseRow) Expense {
|
||||
value, _ := decimal.NewFromString(e.Expense.Value)
|
||||
|
||||
return Expense{
|
||||
ExpID: e.Expense.ExpID,
|
||||
Description: e.Expense.Description,
|
||||
Value: e.Expense.Value,
|
||||
Value: value,
|
||||
Store: Store{
|
||||
StoreID: e.Store.StoreID,
|
||||
StoreName: e.Store.StoreName,
|
||||
@@ -31,10 +37,12 @@ func mapRepoGetExpenseRow(e repo.GetExpenseRow) Expense {
|
||||
}
|
||||
|
||||
func mapRepoGetExpenseRowMulti(e repo.GetExpensesRow) Expense {
|
||||
value, _ := decimal.NewFromString(e.Expense.Value)
|
||||
|
||||
return Expense{
|
||||
ExpID: e.Expense.ExpID,
|
||||
Description: e.Expense.Description,
|
||||
Value: e.Expense.Value,
|
||||
Value: value,
|
||||
Store: Store{
|
||||
StoreID: e.Store.StoreID,
|
||||
StoreName: e.Store.StoreName,
|
||||
@@ -65,4 +73,3 @@ func mapRepoGetExpensesRows(er []repo.GetExpensesRow) []Expense {
|
||||
}
|
||||
return expenses
|
||||
}
|
||||
|
||||
|
||||
@@ -10,12 +10,13 @@ import (
|
||||
"fmt"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type ExpenseShare struct {
|
||||
ExpShareID int64
|
||||
User User
|
||||
Share float64
|
||||
Share decimal.Decimal
|
||||
}
|
||||
|
||||
func (e *Expense) GetShares() error {
|
||||
@@ -44,7 +45,7 @@ func (sh *ExpenseShare) Insert(tx *sql.Tx, expID int64) error {
|
||||
queries := repo.New(tx)
|
||||
res, err := queries.InsertShare(ctx, repo.InsertShareParams{
|
||||
ExpID: expID,
|
||||
Share: sh.Share,
|
||||
Share: sh.Share.String(),
|
||||
UserID: sh.User.UserID,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -74,7 +75,7 @@ func (sh *ExpenseShare) Update() error {
|
||||
queries := repo.New(db)
|
||||
res, err := queries.UpdateShare(ctx, repo.UpdateShareParams{
|
||||
ExpShareID: sh.ExpShareID,
|
||||
Share: sh.Share,
|
||||
Share: sh.Share.String(),
|
||||
UserID: sh.User.UserID,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
package models
|
||||
|
||||
import repo "expenses/expenses/db/repository"
|
||||
import (
|
||||
repo "expenses/expenses/db/repository"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func mapRepoGetSharesRow(s repo.GetSharesRow) ExpenseShare {
|
||||
share, _ := decimal.NewFromString(s.ExpensesShare.Share)
|
||||
|
||||
return ExpenseShare{
|
||||
ExpShareID: s.ExpensesShare.ExpShareID,
|
||||
Share: s.ExpensesShare.Share,
|
||||
Share: share,
|
||||
User: User{
|
||||
UserID: s.User.UserID,
|
||||
UserName: s.User.UserName,
|
||||
|
||||
@@ -3,10 +3,10 @@ package pages
|
||||
import (
|
||||
"expenses/expenses"
|
||||
mod "expenses/expenses/models"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func processFormShares(c *gin.Context) ([]mod.ExpenseShare, error) {
|
||||
@@ -36,7 +36,7 @@ func expenseFromForm(c *gin.Context) (*mod.Expense, error) {
|
||||
date := formattedDate.Unix()
|
||||
|
||||
newValue := c.PostForm("expense-value")
|
||||
value, err := strconv.ParseFloat(newValue, 32)
|
||||
value, err := decimal.NewFromString(newValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type ExpenseDebtOverview struct {
|
||||
@@ -56,7 +57,7 @@ func getResults(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
userDebtSummary := make(map[mod.User]float64)
|
||||
userDebtSummary := make(map[mod.User]decimal.Decimal)
|
||||
expensesWithDebts := []ExpenseDebtOverview{}
|
||||
for _, e := range queriedExpenses {
|
||||
if len(e.Shares) <= 1 {
|
||||
@@ -77,7 +78,7 @@ func getResults(c *gin.Context) {
|
||||
})
|
||||
|
||||
for _, debt := range expenseDebts {
|
||||
userDebtSummary[debt.Debtor] += debt.Sum
|
||||
userDebtSummary[debt.Debtor] = userDebtSummary[debt.Debtor].Add(debt.Sum)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user