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 { formatNumber } from '../helpers'
|
||||
import { unallocatedMoneyAcc } from '../constants'
|
||||
|
||||
export default {
|
||||
components: { accList },
|
||||
|
@ -172,50 +171,13 @@ export default {
|
|||
return fetch('/api/accounts', {
|
||||
body: JSON.stringify({
|
||||
name: this.modals.addAccount.name,
|
||||
startingBalance: this.modals.addAccount.startingBalance,
|
||||
type: this.modals.addAccount.type,
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
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(() => {
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.luzifer.io/luzifer/accounting/pkg/database"
|
||||
"github.com/google/uuid"
|
||||
|
@ -15,6 +16,7 @@ import (
|
|||
func (a apiServer) handleCreateAccount(w http.ResponseWriter, r *http.Request) {
|
||||
var payload struct {
|
||||
Name string `json:"name"`
|
||||
StartingBalance float64 `json:"startingBalance"`
|
||||
Type database.AccountType `json:"type"`
|
||||
}
|
||||
|
||||
|
@ -39,6 +41,37 @@ func (a apiServer) handleCreateAccount(w http.ResponseWriter, r *http.Request) {
|
|||
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())
|
||||
if err != nil {
|
||||
a.errorResponse(w, err, "getting redirect url", http.StatusInternalServerError)
|
||||
|
|
Loading…
Reference in a new issue