Working on the debts
Also started adding support for lua scripts, still working on that part tho;
This commit is contained in:
@@ -14,6 +14,8 @@ used
|
||||
- [X] The button for deleting share rows isn't working at all unlike the payments
|
||||
- [X] Use shopspring/decimal for money handling
|
||||
- [ ] Rename this file
|
||||
- [ ] Better UX when adding expenses on the payments and share values (default even split)
|
||||
- [ ] Verification popups for delete actions
|
||||
|
||||
### Stack
|
||||
- Go
|
||||
|
||||
@@ -67,6 +67,14 @@
|
||||
{{ template "expensePayments" . }}
|
||||
</fieldset>
|
||||
|
||||
{{ if eq .method "put" }}
|
||||
<fieldset>
|
||||
<legend>DEBTS</legend>
|
||||
{{ template "expenseDebts" . }}
|
||||
</fieldset>
|
||||
{{ end }}
|
||||
|
||||
|
||||
<br>
|
||||
<button
|
||||
class="subbutton"
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
{{ define "expenseDebts" }}
|
||||
<table id="debts-table">
|
||||
<tr>
|
||||
<th>CREDITOR</th>
|
||||
<th>DEBTOR</th>
|
||||
<th>SUM</th>
|
||||
</tr>
|
||||
|
||||
{{ range .expense.Debts }}
|
||||
{{ $debt := . }}
|
||||
<tr>
|
||||
<td>
|
||||
{{ $debt.Creditor.UserName }}
|
||||
</td>
|
||||
<td>
|
||||
{{ $debt.Debtor.UserName }}
|
||||
</td>
|
||||
<td>
|
||||
{{ $debt.Sum }}
|
||||
</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
</table>
|
||||
{{ end }}
|
||||
+15
-1
@@ -3,11 +3,25 @@ package cmd
|
||||
import (
|
||||
"log"
|
||||
|
||||
exp "expenses/expenses"
|
||||
"expenses/luadec"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func debts(cmd *cobra.Command, args []string) {
|
||||
log.Println("Added the debts command!")
|
||||
log.Println("Added the debts command!")
|
||||
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
|
||||
L.SetGlobal("GetAllExpenses", L.NewFunction(exp.Serv.LuaGetAllExpenses))
|
||||
L.SetGlobal("AddDecimal", L.NewFunction(luadec.AddDecimal))
|
||||
|
||||
if err := L.DoFile("./lua-scripts/test.lua"); err != nil {
|
||||
log.Printf("Failed to run lua script: %+v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// shelf
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func (s *Service) GetAllExpenses() ([]mod.Expense, error) {
|
||||
@@ -20,6 +21,32 @@ func (s *Service) GetAllExpenses() ([]mod.Expense, error) {
|
||||
return expenses, tx.Commit()
|
||||
}
|
||||
|
||||
func (s *Service) LuaGetAllExpenses(L *lua.LState) int {
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
expenses, err := mod.GetAllExpenses(tx)
|
||||
|
||||
tbl := L.NewTable()
|
||||
for _, e := range expenses {
|
||||
et := L.NewTable()
|
||||
|
||||
et.RawSetString("id", lua.LNumber(e.ExpID))
|
||||
et.RawSetString("description", lua.LString(e.Description))
|
||||
et.RawSetString("amount", lua.LString(e.Value.StringFixed(2)))
|
||||
|
||||
tbl.Append(et)
|
||||
}
|
||||
|
||||
L.Push(tbl)
|
||||
return 1
|
||||
}
|
||||
|
||||
func (s *Service) GetExpensesRanged(startDate string, endDate string) ([]mod.Expense, error) {
|
||||
startDateTime, err := time.ParseInLocation("02-Jan-2006 15:04:05", startDate, time.UTC)
|
||||
if err != nil {
|
||||
@@ -91,6 +118,13 @@ func (s *Service) LoadExpensePayments(e *mod.Expense) error {
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (s *Service) LoadExpenseDebts(e *mod.Expense) error {
|
||||
debts, _ := CalculateDebts(e)
|
||||
e.Debts = debts
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) DeleteExpense(id int64) error {
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
|
||||
@@ -22,6 +22,7 @@ type Expense struct {
|
||||
Date int64
|
||||
Payments []ExpensePayment
|
||||
Shares []ExpenseShare
|
||||
Debts Debts
|
||||
PaidOff bool
|
||||
SharesEven bool
|
||||
CreationDate int64
|
||||
|
||||
+2
-3
@@ -3,12 +3,13 @@ module expenses
|
||||
go 1.24.4
|
||||
|
||||
require (
|
||||
github.com/Shopify/go-lua v0.0.0-20250718183320-1e37f32ad7d0
|
||||
github.com/gin-gonic/gin v1.10.1
|
||||
github.com/golang-migrate/migrate/v4 v4.18.3
|
||||
github.com/mattn/go-sqlite3 v1.14.28
|
||||
github.com/shopspring/decimal v1.2.0
|
||||
github.com/spf13/cobra v1.9.1
|
||||
github.com/stretchr/testify v1.9.0
|
||||
github.com/yuin/gopher-lua v1.1.1
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
)
|
||||
|
||||
@@ -17,7 +18,6 @@ require (
|
||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
@@ -34,7 +34,6 @@ require (
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/spf13/pflag v1.0.6 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
github.com/Shopify/go-lua v0.0.0-20250718183320-1e37f32ad7d0 h1:oGlw/+ndlFMn8KWLjEX5nULcDwOC4tJy3Kk1Pm84Cys=
|
||||
github.com/Shopify/go-lua v0.0.0-20250718183320-1e37f32ad7d0/go.mod h1:M4CxjVc/1Nwka5atBv7G/sb7Ac2BDe3+FxbiT9iVNIQ=
|
||||
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||
@@ -84,6 +86,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
||||
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
---@diagnostic disable-next-line: undefined-global
|
||||
local expenses, err = GetAllExpenses()
|
||||
|
||||
if not expenses then
|
||||
print("Error:", err)
|
||||
else
|
||||
for _, e in ipairs(expenses) do
|
||||
print(e.id, e.description, e.amount)
|
||||
end
|
||||
end
|
||||
|
||||
---@diagnostic disable-next-line: undefined-global
|
||||
local a = AddDecimal("0.1", "0.25")
|
||||
print(a)
|
||||
@@ -0,0 +1,16 @@
|
||||
package luadec
|
||||
|
||||
import (
|
||||
"github.com/shopspring/decimal"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func AddDecimal(L *lua.LState) int {
|
||||
decA, _ := decimal.NewFromString(L.CheckString(1))
|
||||
decB, _ := decimal.NewFromString(L.CheckString(2))
|
||||
|
||||
res := decA.Add(decB)
|
||||
|
||||
L.Push(lua.LString(res.StringFixed(2)))
|
||||
return 1
|
||||
}
|
||||
@@ -69,6 +69,12 @@ func expensePage(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
err = exp.Serv.LoadExpenseDebts(&expense)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to fetch debts")
|
||||
return
|
||||
}
|
||||
|
||||
categories, err := exp.GetAllCategories()
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to fetch categories")
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
type ExpenseDebtOverview struct {
|
||||
Exp *mod.Expense
|
||||
Debtors []exp.Debt
|
||||
Debtors mod.Debts
|
||||
}
|
||||
|
||||
type UserDebtSummary struct {
|
||||
|
||||
Reference in New Issue
Block a user