Move creation of starting balances to backend
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
b215d3975e
commit
581ab042fc
2 changed files with 36 additions and 41 deletions
|
@ -128,7 +128,6 @@ import { Modal } from 'bootstrap'
|
||||||
import accList from './accountsSidebarAccList.vue'
|
import accList from './accountsSidebarAccList.vue'
|
||||||
|
|
||||||
import { formatNumber } from '../helpers'
|
import { formatNumber } from '../helpers'
|
||||||
import { unallocatedMoneyAcc } from '../constants'
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: { accList },
|
components: { accList },
|
||||||
|
@ -172,50 +171,13 @@ export default {
|
||||||
return fetch('/api/accounts', {
|
return fetch('/api/accounts', {
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
name: this.modals.addAccount.name,
|
name: this.modals.addAccount.name,
|
||||||
|
startingBalance: this.modals.addAccount.startingBalance,
|
||||||
type: this.modals.addAccount.type,
|
type: this.modals.addAccount.type,
|
||||||
}),
|
}),
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
})
|
|
||||||
.then(resp => resp.json())
|
|
||||||
.then(account => {
|
|
||||||
if (account.type === 'budget') {
|
|
||||||
return fetch('/api/transactions', {
|
|
||||||
body: JSON.stringify({
|
|
||||||
account: account.id,
|
|
||||||
amount: this.modals.addAccount.startingBalance,
|
|
||||||
category: unallocatedMoneyAcc,
|
|
||||||
cleared: true,
|
|
||||||
description: 'Starting Balance',
|
|
||||||
time: new Date(),
|
|
||||||
}),
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
method: 'POST',
|
|
||||||
})
|
|
||||||
} else if (account.type === 'tracking') {
|
|
||||||
return fetch('/api/transactions', {
|
|
||||||
body: JSON.stringify({
|
|
||||||
account: account.id,
|
|
||||||
amount: this.modals.addAccount.startingBalance,
|
|
||||||
cleared: true,
|
|
||||||
description: 'Starting Balance',
|
|
||||||
time: new Date(),
|
|
||||||
}),
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
method: 'POST',
|
|
||||||
})
|
|
||||||
} else if (account.type === 'category') {
|
|
||||||
return fetch(`/api/accounts/${unallocatedMoneyAcc}/transfer/${account.id}?amount=${this.modals.addAccount.startingBalance}`, {
|
|
||||||
method: 'PUT',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
throw new Error('invalid account type detected')
|
|
||||||
})
|
})
|
||||||
.then(() => this.$emit('update-accounts'))
|
.then(() => this.$emit('update-accounts'))
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.luzifer.io/luzifer/accounting/pkg/database"
|
"git.luzifer.io/luzifer/accounting/pkg/database"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
@ -15,6 +16,7 @@ import (
|
||||||
func (a apiServer) handleCreateAccount(w http.ResponseWriter, r *http.Request) {
|
func (a apiServer) handleCreateAccount(w http.ResponseWriter, r *http.Request) {
|
||||||
var payload struct {
|
var payload struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
StartingBalance float64 `json:"startingBalance"`
|
||||||
Type database.AccountType `json:"type"`
|
Type database.AccountType `json:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,6 +41,37 @@ func (a apiServer) handleCreateAccount(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if payload.StartingBalance != 0 {
|
||||||
|
switch payload.Type {
|
||||||
|
case database.AccountTypeBudget:
|
||||||
|
_, err = a.dbc.CreateTransaction(database.Transaction{
|
||||||
|
Time: time.Now(),
|
||||||
|
Description: "Starting Balance",
|
||||||
|
Amount: payload.StartingBalance,
|
||||||
|
Account: uuid.NullUUID{UUID: acc.ID, Valid: true},
|
||||||
|
Category: uuid.NullUUID{UUID: database.UnallocatedMoney, Valid: true},
|
||||||
|
Cleared: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
case database.AccountTypeCategory:
|
||||||
|
err = a.dbc.TransferMoney(database.UnallocatedMoney, acc.ID, payload.StartingBalance)
|
||||||
|
|
||||||
|
case database.AccountTypeTracking:
|
||||||
|
_, err = a.dbc.CreateTransaction(database.Transaction{
|
||||||
|
Time: time.Now(),
|
||||||
|
Description: "Starting Balance",
|
||||||
|
Amount: payload.StartingBalance,
|
||||||
|
Account: uuid.NullUUID{UUID: acc.ID, Valid: true},
|
||||||
|
Cleared: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
a.errorResponse(w, err, "creating starting balance transaction", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
u, err := a.router.Get("GetAccount").URL("id", acc.ID.String())
|
u, err := a.router.Get("GetAccount").URL("id", acc.ID.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, err, "getting redirect url", http.StatusInternalServerError)
|
a.errorResponse(w, err, "getting redirect url", http.StatusInternalServerError)
|
||||||
|
|
Loading…
Reference in a new issue