quick debt settlement in the overview page
This commit is contained in:
@@ -30,7 +30,8 @@
|
|||||||
<a hx-post="/payments/add"
|
<a hx-post="/payments/add"
|
||||||
hx-swap="none"
|
hx-swap="none"
|
||||||
hx-vals='{"ExpID":"{{ $exp.ExpID }}",
|
hx-vals='{"ExpID":"{{ $exp.ExpID }}",
|
||||||
"UserID":"{{ $debt.Debtor.UserID }}",
|
"DebtorID":"{{ $debt.Debtor.UserID }}",
|
||||||
|
"CreditorID":"{{ $debt.Creditor.UserID }}",
|
||||||
"Sum": "{{ $debt.Sum }}"}'>PAY</a>
|
"Sum": "{{ $debt.Sum }}"}'>PAY</a>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
@@ -7,6 +7,17 @@ JOIN
|
|||||||
users as users ON users.UserID = payments.UserID
|
users as users ON users.UserID = payments.UserID
|
||||||
WHERE "ExpID" = ?;
|
WHERE "ExpID" = ?;
|
||||||
|
|
||||||
|
-- name: GetExpensePaymentByUser :one
|
||||||
|
SELECT
|
||||||
|
sqlc.embed(payments), sqlc.embed(users)
|
||||||
|
FROM
|
||||||
|
"expensesPayments" as payments
|
||||||
|
JOIN
|
||||||
|
users as users ON users.UserID = payments.UserID
|
||||||
|
WHERE
|
||||||
|
"ExpID" = ? AND users."UserID" = ?;
|
||||||
|
|
||||||
|
|
||||||
-- name: InsertPayment :execresult
|
-- name: InsertPayment :execresult
|
||||||
INSERT INTO "expensesPayments"(
|
INSERT INTO "expensesPayments"(
|
||||||
"ExpID", "UserID", "Payed"
|
"ExpID", "UserID", "Payed"
|
||||||
|
|||||||
@@ -18,6 +18,42 @@ func (q *Queries) DeletePayment(ctx context.Context, exppaymid int64) (sql.Resul
|
|||||||
return q.db.ExecContext(ctx, deletePayment, exppaymid)
|
return q.db.ExecContext(ctx, deletePayment, exppaymid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getExpensePaymentByUser = `-- name: GetExpensePaymentByUser :one
|
||||||
|
SELECT
|
||||||
|
payments.ExpPaymID, payments.ExpID, payments.UserID, payments.Payed, users.UserID, users.UserName, users.UserPass
|
||||||
|
FROM
|
||||||
|
"expensesPayments" as payments
|
||||||
|
JOIN
|
||||||
|
users as users ON users.UserID = payments.UserID
|
||||||
|
WHERE
|
||||||
|
"ExpID" = ? AND users."UserID" = ?
|
||||||
|
`
|
||||||
|
|
||||||
|
type GetExpensePaymentByUserParams struct {
|
||||||
|
ExpID int64
|
||||||
|
UserID int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetExpensePaymentByUserRow struct {
|
||||||
|
ExpensesPayment ExpensesPayment
|
||||||
|
User User
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) GetExpensePaymentByUser(ctx context.Context, arg GetExpensePaymentByUserParams) (GetExpensePaymentByUserRow, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getExpensePaymentByUser, arg.ExpID, arg.UserID)
|
||||||
|
var i GetExpensePaymentByUserRow
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ExpensesPayment.ExpPaymID,
|
||||||
|
&i.ExpensesPayment.ExpID,
|
||||||
|
&i.ExpensesPayment.UserID,
|
||||||
|
&i.ExpensesPayment.Payed,
|
||||||
|
&i.User.UserID,
|
||||||
|
&i.User.UserName,
|
||||||
|
&i.User.UserPass,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
const getPayments = `-- name: GetPayments :many
|
const getPayments = `-- name: GetPayments :many
|
||||||
SELECT
|
SELECT
|
||||||
payments.ExpPaymID, payments.ExpID, payments.UserID, payments.Payed, users.UserID, users.UserName, users.UserPass
|
payments.ExpPaymID, payments.ExpID, payments.UserID, payments.Payed, users.UserID, users.UserName, users.UserPass
|
||||||
|
|||||||
@@ -141,3 +141,47 @@ func CalculateDebts(e *mod.Expense) (mod.Debts, error) {
|
|||||||
|
|
||||||
return debts, nil
|
return debts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *ExpensesApp) settleDebt(payment mod.ExpensePayment,
|
||||||
|
credit mod.ExpensePayment, eId int64) error {
|
||||||
|
tx, err := a.DB.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
credit.PayedAmount = credit.PayedAmount.Sub(payment.PayedAmount)
|
||||||
|
|
||||||
|
err = payment.Insert(tx, eId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = credit.Update(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ExpensesApp) ProcessDebt(expID int64, debt mod.Debt) error {
|
||||||
|
// We have to balance the debtor and creditor payments
|
||||||
|
payment := mod.ExpensePayment{
|
||||||
|
User: mod.User{
|
||||||
|
UserID: debt.Debtor.UserID,
|
||||||
|
},
|
||||||
|
PayedAmount: debt.Sum,
|
||||||
|
}
|
||||||
|
|
||||||
|
creditorPayment, err := a.GetExpensePaymentByUserID(expID, debt.Creditor.UserID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = a.settleDebt(payment, creditorPayment, expID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -45,6 +45,19 @@ func ParseFormPayments(userIDs []string, paymentsIDs []string,
|
|||||||
return payments, nil
|
return payments, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *ExpensesApp) GetExpensePaymentByUserID(eId int64, uId int64,
|
||||||
|
) (mod.ExpensePayment, error) {
|
||||||
|
tx, err := a.DB.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return mod.ExpensePayment{}, err
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
payment, err := mod.GetExpensePaymentByUserID(tx, eId, uId)
|
||||||
|
|
||||||
|
return payment, tx.Commit()
|
||||||
|
}
|
||||||
|
|
||||||
// insertPayment allows us to insert a payment manually
|
// insertPayment allows us to insert a payment manually
|
||||||
// for now its private, need to figure out if it needs to be public
|
// for now its private, need to figure out if it needs to be public
|
||||||
func (a *ExpensesApp) insertPayment(payment mod.ExpensePayment, eIdx int64) error {
|
func (a *ExpensesApp) insertPayment(payment mod.ExpensePayment, eIdx int64) error {
|
||||||
@@ -81,6 +94,21 @@ func (a *ExpensesApp) DeletePayment(id int64) error {
|
|||||||
return tx.Commit()
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *ExpensesApp) UpdatePayment(payment mod.ExpensePayment) error {
|
||||||
|
tx, err := a.DB.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
err = payment.Update(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) AddPayment(expID int64, userID int64, sum dec.Decimal) error {
|
func (a *ExpensesApp) AddPayment(expID int64, userID int64, sum dec.Decimal) error {
|
||||||
payment := mod.ExpensePayment{
|
payment := mod.ExpensePayment{
|
||||||
User: mod.User{
|
User: mod.User{
|
||||||
|
|||||||
@@ -26,3 +26,16 @@ func mapRepoGetPaymentsRows(ep []repo.GetPaymentsRow) []ExpensePayment {
|
|||||||
}
|
}
|
||||||
return payments
|
return payments
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mapRepoGetPaymentRow(s repo.GetExpensePaymentByUserRow) ExpensePayment {
|
||||||
|
payed, _ := decimal.NewFromString(s.ExpensesPayment.Payed)
|
||||||
|
|
||||||
|
return ExpensePayment{
|
||||||
|
ExpPaymID: s.ExpensesPayment.ExpPaymID,
|
||||||
|
PayedAmount: payed,
|
||||||
|
User: User{
|
||||||
|
UserID: s.User.UserID,
|
||||||
|
UserName: s.User.UserName,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -38,6 +38,22 @@ func (e *Expense) GetPayments(tx *sql.Tx) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetExpensePaymentByUserID(tx *sql.Tx, eId int64, uId int64,
|
||||||
|
) (ExpensePayment, error) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
queries := repo.New(tx)
|
||||||
|
payment, err := queries.GetExpensePaymentByUser(ctx, repo.GetExpensePaymentByUserParams{
|
||||||
|
ExpID: eId,
|
||||||
|
UserID: uId,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return ExpensePayment{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapRepoGetPaymentRow(payment), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p *ExpensePayment) Insert(tx *sql.Tx, expID int64) error {
|
func (p *ExpensePayment) Insert(tx *sql.Tx, expID int64) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
|||||||
+17
-2
@@ -3,6 +3,7 @@ package pages
|
|||||||
import (
|
import (
|
||||||
exp "expenses/expenses"
|
exp "expenses/expenses"
|
||||||
experr "expenses/expenses/errors"
|
experr "expenses/expenses/errors"
|
||||||
|
mod "expenses/expenses/models"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
dec "github.com/shopspring/decimal"
|
dec "github.com/shopspring/decimal"
|
||||||
@@ -37,7 +38,11 @@ func addPayment(c *gin.Context) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
}
|
}
|
||||||
|
|
||||||
userID, err := exp.ParseID(c.PostForm("UserID"))
|
debtorID, err := exp.ParseID(c.PostForm("DebtorID"))
|
||||||
|
if err != nil {
|
||||||
|
}
|
||||||
|
|
||||||
|
creditorID, err := exp.ParseID(c.PostForm("CreditorID"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,7 +50,17 @@ func addPayment(c *gin.Context) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
}
|
}
|
||||||
|
|
||||||
err = exp.App.AddPayment(expID, userID, sum)
|
debt := mod.Debt{
|
||||||
|
Debtor: mod.User{
|
||||||
|
UserID: debtorID,
|
||||||
|
},
|
||||||
|
Creditor: mod.User{
|
||||||
|
UserID: creditorID,
|
||||||
|
},
|
||||||
|
Sum: sum,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = exp.App.ProcessDebt(expID, debt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user