debt settling with transaction
This commit is contained in:
+3
-6
@@ -1,8 +1,5 @@
|
|||||||
# DOING
|
# DOING
|
||||||
Refactor how the models access the database (move the Tx creation to the models)
|
Implement scanning the invoice QR code to insert stuff into the tables
|
||||||
Refactor the Makefiles
|
|
||||||
Refactor the App start function
|
|
||||||
Add NIF columns to stores and to users
|
|
||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
- [ ] On the categories page, add a column of how many times a given category is
|
- [ ] On the categories page, add a column of how many times a given category is
|
||||||
@@ -10,10 +7,10 @@ used
|
|||||||
- [ ] Multiple categories per expense
|
- [ ] Multiple categories per expense
|
||||||
- [X] Make all the other items update/create page be like expenses
|
- [X] Make all the other items update/create page be like expenses
|
||||||
- [ ] Ability to attach a document to an expense
|
- [ ] Ability to attach a document to an expense
|
||||||
- [ ] Debts table
|
~~- [ ] Debts table~~
|
||||||
- [ ] Ability to export data in multiple formats and with a selection of columns
|
- [ ] Ability to export data in multiple formats and with a selection of columns
|
||||||
- [ ] Migrate every column named "ExpenseSomething" to "ExpSomething"
|
- [ ] Migrate every column named "ExpenseSomething" to "ExpSomething"
|
||||||
- [ ] Make the models just return errors and not do any handling themselves
|
- [X] Make the models just return errors and not do any handling themselves
|
||||||
- [X] Reduce the size of the font file
|
- [X] Reduce the size of the font file
|
||||||
- [X] Add some migration tool
|
- [X] Add some migration tool
|
||||||
- [X] Ownership on the image for the expenses webapp dir need to be changed
|
- [X] Ownership on the image for the expenses webapp dir need to be changed
|
||||||
|
|||||||
@@ -3,8 +3,9 @@ package expenses
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
mod "expenses/expenses/models"
|
mod "expenses/expenses/models"
|
||||||
dec "github.com/shopspring/decimal"
|
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
dec "github.com/shopspring/decimal"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserTab struct {
|
type UserTab struct {
|
||||||
@@ -142,18 +143,19 @@ func CalculateDebts(e *mod.Expense) (mod.Debts, error) {
|
|||||||
return debts, nil
|
return debts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// settleDebt updates a debt settlement on the database
|
||||||
|
// it takes away the payed debt by payment from the creditor payments
|
||||||
|
// TODO
|
||||||
|
// This won't work if the payment was made with multiple payments - but for my
|
||||||
|
// use case that won't be a thing. Right now every expense I introduce has been
|
||||||
|
// fully payed in one go
|
||||||
func (a *ExpensesApp) settleDebt(payment mod.Payment,
|
func (a *ExpensesApp) settleDebt(payment mod.Payment,
|
||||||
credit mod.Payment, eId int32) error {
|
credit mod.Payment, eId int32) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
credit.PayedAmount = credit.PayedAmount.Sub(payment.PayedAmount)
|
credit.PayedAmount = credit.PayedAmount.Sub(payment.PayedAmount)
|
||||||
|
|
||||||
// TODO
|
return a.ExpRepo.SettleDebt(ctx, eId, payment, credit)
|
||||||
// This needs to run as a transaction
|
|
||||||
err := a.ExpRepo.InsertPayment(ctx, eId, payment)
|
|
||||||
err = a.ExpRepo.UpdatePayment(ctx, credit)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) ProcessDebt(expID int32, debt mod.Debt) error {
|
func (a *ExpensesApp) ProcessDebt(expID int32, debt mod.Debt) error {
|
||||||
|
|||||||
@@ -32,5 +32,9 @@ type ExpenseRepository interface {
|
|||||||
InsertPayments(ctx context.Context, eId int32, pm mod.Payments) error
|
InsertPayments(ctx context.Context, eId int32, pm mod.Payments) error
|
||||||
DeletePayment(ctx context.Context, id int32) error
|
DeletePayment(ctx context.Context, id int32) error
|
||||||
|
|
||||||
|
// Debts
|
||||||
|
SettleDebt(ctx context.Context, eId int32,
|
||||||
|
payment mod.Payment, credit mod.Payment) error
|
||||||
|
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (p PgExpRepo) SettleDebt(
|
||||||
|
ctx context.Context, eId int32, payment mod.Payment, credit mod.Payment) error {
|
||||||
|
|
||||||
|
return withTx(p.DB, ctx, func(q *pgsqlc.Queries) error {
|
||||||
|
err := p.insertPayment(ctx, q, eId, payment)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = p.updatePayment(ctx, q, credit)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil // yeah we could just return the last op, but I like this way
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -26,14 +26,18 @@ func (p PgExpRepo) Close() {
|
|||||||
p.DB.Close()
|
p.DB.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *PgExpRepo) withTx(ctx context.Context, fn func(q *pgsqlc.Queries) error) error {
|
func withTx(
|
||||||
tx, err := r.DB.BeginTx(ctx, pgx.TxOptions{})
|
pgPool *pgxpool.Pool,
|
||||||
|
ctx context.Context,
|
||||||
|
fn func(q *pgsqlc.Queries) error) error {
|
||||||
|
|
||||||
|
tx, err := pgPool.BeginTx(ctx, pgx.TxOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer tx.Rollback(ctx)
|
defer tx.Rollback(ctx)
|
||||||
|
|
||||||
q := pgsqlc.New(tx) // bind sqlc to this tx
|
q := pgsqlc.New(tx)
|
||||||
|
|
||||||
if err := fn(q); err != nil {
|
if err := fn(q); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -79,7 +83,7 @@ func (p PgExpRepo) Update(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
exp mod.Expense) error {
|
exp mod.Expense) error {
|
||||||
|
|
||||||
return p.withTx(ctx, func(q *pgsqlc.Queries) error {
|
return withTx(p.DB, ctx, func(q *pgsqlc.Queries) error {
|
||||||
value, err := decimalToNumeric(exp.Value)
|
value, err := decimalToNumeric(exp.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -157,7 +161,7 @@ func (p PgExpRepo) Insert(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
exp mod.Expense) error {
|
exp mod.Expense) error {
|
||||||
|
|
||||||
return p.withTx(ctx, func(q *pgsqlc.Queries) error {
|
return withTx(p.DB, ctx, func(q *pgsqlc.Queries) error {
|
||||||
value, err := decimalToNumeric(exp.Value)
|
value, err := decimalToNumeric(exp.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user