Little Summary of what was payed on a given expense
This commit is contained in:
@@ -88,4 +88,19 @@
|
||||
</fieldset>
|
||||
</form>
|
||||
</div>
|
||||
outside
|
||||
|
||||
Shares:
|
||||
<table>
|
||||
<tr>
|
||||
<th>USER</th>
|
||||
<th>SHARE</th>
|
||||
<th>PAYED</th>
|
||||
</tr>
|
||||
{{- range .summary }}
|
||||
<tr>
|
||||
<td>{{ .User.UserName }}</td>
|
||||
<td>{{ .Share }}</td>
|
||||
<td>{{ .PayedTotal }}</td>
|
||||
</tr>
|
||||
{{- end }}
|
||||
</table>
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type ExpensePayment struct {
|
||||
ExpPaymID int
|
||||
User User
|
||||
PayedAmount float32
|
||||
}
|
||||
|
||||
func (e *Expense) GetPayments() error {
|
||||
db, err := sql.Open("sqlite3", "./data/data.db")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
query := "SELECT ExpPaymID,Payed,users.UserID,users.UserName " +
|
||||
"FROM expensesPayments " +
|
||||
"JOIN users ON users.UserID = expensesPayments.UserID " +
|
||||
"WHERE ExpID = ?"
|
||||
|
||||
rows, err := db.Query(query, e.ExpID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
paym := &ExpensePayment{}
|
||||
err := rows.Scan(
|
||||
&paym.ExpPaymID, &paym.PayedAmount,
|
||||
&paym.User.UserID, &paym.User.UserName,
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse data from db: %v", err)
|
||||
}
|
||||
e.Payments = append(e.Payments, *paym)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type ExpenseShare struct {
|
||||
ExpShareID int
|
||||
User User
|
||||
Share float32
|
||||
}
|
||||
|
||||
func (e *Expense) GetShares() error {
|
||||
db, err := sql.Open("sqlite3", "./data/data.db")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
query := "SELECT ExpShareID,Share,users.UserID,users.UserName " +
|
||||
"FROM expensesShares " +
|
||||
"JOIN users ON users.UserID = expensesShares.UserID " +
|
||||
"WHERE ExpID = ?"
|
||||
|
||||
rows, err := db.Query(query, e.ExpID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
share := &ExpenseShare{}
|
||||
err := rows.Scan(
|
||||
&share.ExpShareID, &share.Share,
|
||||
&share.User.UserID, &share.User.UserName,
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse data from db: %v", err)
|
||||
}
|
||||
e.Shares = append(e.Shares, *share)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package expenses
|
||||
|
||||
import "log"
|
||||
|
||||
type UserExpenseSummary struct {
|
||||
User User
|
||||
Share float32
|
||||
PayedTotal float32
|
||||
}
|
||||
|
||||
func NewUserExpenseSummary() UserExpenseSummary {
|
||||
return UserExpenseSummary{
|
||||
User: NewUser(),
|
||||
Share: 0.0,
|
||||
PayedTotal: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Expense) GetSummary() ([]UserExpenseSummary, error) {
|
||||
summary := []UserExpenseSummary{}
|
||||
|
||||
for _, share := range e.Shares {
|
||||
curSummary := NewUserExpenseSummary()
|
||||
curSummary.User = share.User
|
||||
curSummary.Share = share.Share
|
||||
|
||||
for _, payment := range e.Payments {
|
||||
if payment.User.UserID == curSummary.User.UserID {
|
||||
curSummary.PayedTotal += payment.PayedAmount
|
||||
}
|
||||
}
|
||||
|
||||
log.Println(curSummary)
|
||||
summary = append(summary, curSummary)
|
||||
}
|
||||
|
||||
return summary, nil
|
||||
}
|
||||
@@ -17,6 +17,8 @@ type Expense struct {
|
||||
ExpCategory Category
|
||||
OwnerUser User
|
||||
ExpDate int64
|
||||
Payments []ExpensePayment
|
||||
Shares []ExpenseShare
|
||||
CreationDate int64
|
||||
}
|
||||
|
||||
@@ -29,6 +31,8 @@ func NewExpense() Expense {
|
||||
ExpCategory: NewCategory(),
|
||||
OwnerUser: NewUser(),
|
||||
ExpDate: 0,
|
||||
Payments: []ExpensePayment{},
|
||||
Shares: []ExpenseShare{},
|
||||
CreationDate: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"expenses/expenses"
|
||||
"expenses/templating"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"net/http"
|
||||
@@ -78,6 +79,19 @@ func expensePage(c *gin.Context) {
|
||||
}
|
||||
|
||||
expense, err := expenses.GetExpense(expenseID)
|
||||
err = expense.GetPayments()
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to fetch payments")
|
||||
return
|
||||
}
|
||||
|
||||
err = expense.GetShares()
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to fetch payments")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
categories, err := expenses.GetAllCategories()
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to fetch categories")
|
||||
@@ -96,6 +110,15 @@ func expensePage(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Println(expense.Shares)
|
||||
log.Println(expense.Payments)
|
||||
|
||||
summary, err := expense.GetSummary()
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to get summary")
|
||||
return
|
||||
}
|
||||
|
||||
content := templating.HtmlTemplate(
|
||||
fp.Join(cfg.AssetsDir, "/htmx/expense.html"),
|
||||
map[string]any{
|
||||
@@ -104,6 +127,7 @@ func expensePage(c *gin.Context) {
|
||||
"categories": categories,
|
||||
"stores": stores,
|
||||
"types": types,
|
||||
"summary": summary,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user