Implement closing of accounts #36
@ -15,7 +15,7 @@ const createAccount = `-- name: CreateAccount :one
|
||||
INSERT INTO accounts
|
||||
(name, budget_id)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id, budget_id, name, on_budget
|
||||
RETURNING id, budget_id, name, on_budget, is_open
|
||||
`
|
||||
|
||||
type CreateAccountParams struct {
|
||||
@ -31,12 +31,13 @@ func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (A
|
||||
&i.BudgetID,
|
||||
&i.Name,
|
||||
&i.OnBudget,
|
||||
&i.IsOpen,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getAccount = `-- name: GetAccount :one
|
||||
SELECT accounts.id, accounts.budget_id, accounts.name, accounts.on_budget FROM accounts
|
||||
SELECT accounts.id, accounts.budget_id, accounts.name, accounts.on_budget, accounts.is_open FROM accounts
|
||||
WHERE accounts.id = $1
|
||||
`
|
||||
|
||||
@ -48,13 +49,15 @@ func (q *Queries) GetAccount(ctx context.Context, id uuid.UUID) (Account, error)
|
||||
&i.BudgetID,
|
||||
&i.Name,
|
||||
&i.OnBudget,
|
||||
&i.IsOpen,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getAccounts = `-- name: GetAccounts :many
|
||||
SELECT accounts.id, accounts.budget_id, accounts.name, accounts.on_budget FROM accounts
|
||||
SELECT accounts.id, accounts.budget_id, accounts.name, accounts.on_budget, accounts.is_open FROM accounts
|
||||
WHERE accounts.budget_id = $1
|
||||
AND accounts.is_open = TRUE
|
||||
ORDER BY accounts.name
|
||||
`
|
||||
|
||||
@ -72,6 +75,7 @@ func (q *Queries) GetAccounts(ctx context.Context, budgetID uuid.UUID) ([]Accoun
|
||||
&i.BudgetID,
|
||||
&i.Name,
|
||||
&i.OnBudget,
|
||||
&i.IsOpen,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -87,13 +91,14 @@ func (q *Queries) GetAccounts(ctx context.Context, budgetID uuid.UUID) ([]Accoun
|
||||
}
|
||||
|
||||
const getAccountsWithBalance = `-- name: GetAccountsWithBalance :many
|
||||
SELECT accounts.id, accounts.name, accounts.on_budget,
|
||||
SELECT accounts.id, accounts.name, accounts.on_budget, accounts.is_open,
|
||||
(SELECT MAX(transactions.date) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.status = 'Reconciled')::date as last_reconciled,
|
||||
(SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW())::decimal(12,2) as working_balance,
|
||||
(SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW() AND transactions.status IN ('Cleared', 'Reconciled'))::decimal(12,2) as cleared_balance,
|
||||
(SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW() AND transactions.status = 'Reconciled')::decimal(12,2) as reconciled_balance
|
||||
FROM accounts
|
||||
WHERE accounts.budget_id = $1
|
||||
AND accounts.is_open = TRUE
|
||||
ORDER BY accounts.name
|
||||
`
|
||||
|
||||
@ -101,6 +106,7 @@ type GetAccountsWithBalanceRow struct {
|
||||
ID uuid.UUID
|
||||
Name string
|
||||
OnBudget bool
|
||||
IsOpen bool
|
||||
LastReconciled time.Time
|
||||
WorkingBalance numeric.Numeric
|
||||
ClearedBalance numeric.Numeric
|
||||
@ -120,6 +126,7 @@ func (q *Queries) GetAccountsWithBalance(ctx context.Context, budgetID uuid.UUID
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.OnBudget,
|
||||
&i.IsOpen,
|
||||
&i.LastReconciled,
|
||||
&i.WorkingBalance,
|
||||
&i.ClearedBalance,
|
||||
@ -141,6 +148,7 @@ func (q *Queries) GetAccountsWithBalance(ctx context.Context, budgetID uuid.UUID
|
||||
const searchAccounts = `-- name: SearchAccounts :many
|
||||
SELECT accounts.id, accounts.budget_id, accounts.name, 'account' as type FROM accounts
|
||||
WHERE accounts.budget_id = $1
|
||||
AND accounts.is_open = TRUE
|
||||
AND accounts.name LIKE $2
|
||||
ORDER BY accounts.name
|
||||
`
|
||||
@ -188,25 +196,33 @@ func (q *Queries) SearchAccounts(ctx context.Context, arg SearchAccountsParams)
|
||||
const updateAccount = `-- name: UpdateAccount :one
|
||||
UPDATE accounts
|
||||
SET name = $1,
|
||||
on_budget = $2
|
||||
WHERE accounts.id = $3
|
||||
RETURNING id, budget_id, name, on_budget
|
||||
on_budget = $2,
|
||||
is_open = $3
|
||||
WHERE accounts.id = $4
|
||||
RETURNING id, budget_id, name, on_budget, is_open
|
||||
`
|
||||
|
||||
type UpdateAccountParams struct {
|
||||
Name string
|
||||
OnBudget bool
|
||||
IsOpen bool
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateAccount(ctx context.Context, arg UpdateAccountParams) (Account, error) {
|
||||
row := q.db.QueryRowContext(ctx, updateAccount, arg.Name, arg.OnBudget, arg.ID)
|
||||
row := q.db.QueryRowContext(ctx, updateAccount,
|
||||
arg.Name,
|
||||
arg.OnBudget,
|
||||
arg.IsOpen,
|
||||
arg.ID,
|
||||
)
|
||||
var i Account
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.BudgetID,
|
||||
&i.Name,
|
||||
&i.OnBudget,
|
||||
&i.IsOpen,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ type Account struct {
|
||||
BudgetID uuid.UUID
|
||||
Name string
|
||||
OnBudget bool
|
||||
IsOpen bool
|
||||
}
|
||||
|
||||
type Assignment struct {
|
||||
|
@ -11,27 +11,31 @@ WHERE accounts.id = $1;
|
||||
-- name: GetAccounts :many
|
||||
SELECT accounts.* FROM accounts
|
||||
WHERE accounts.budget_id = $1
|
||||
AND accounts.is_open = TRUE
|
||||
ORDER BY accounts.name;
|
||||
|
||||
-- name: GetAccountsWithBalance :many
|
||||
SELECT accounts.id, accounts.name, accounts.on_budget,
|
||||
SELECT accounts.id, accounts.name, accounts.on_budget, accounts.is_open,
|
||||
(SELECT MAX(transactions.date) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.status = 'Reconciled')::date as last_reconciled,
|
||||
(SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW())::decimal(12,2) as working_balance,
|
||||
(SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW() AND transactions.status IN ('Cleared', 'Reconciled'))::decimal(12,2) as cleared_balance,
|
||||
(SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW() AND transactions.status = 'Reconciled')::decimal(12,2) as reconciled_balance
|
||||
FROM accounts
|
||||
WHERE accounts.budget_id = $1
|
||||
AND accounts.is_open = TRUE
|
||||
ORDER BY accounts.name;
|
||||
|
||||
-- name: SearchAccounts :many
|
||||
SELECT accounts.id, accounts.budget_id, accounts.name, 'account' as type FROM accounts
|
||||
WHERE accounts.budget_id = @budget_id
|
||||
AND accounts.is_open = TRUE
|
||||
AND accounts.name LIKE @search
|
||||
ORDER BY accounts.name;
|
||||
|
||||
-- name: UpdateAccount :one
|
||||
UPDATE accounts
|
||||
SET name = $1,
|
||||
on_budget = $2
|
||||
WHERE accounts.id = $3
|
||||
on_budget = $2,
|
||||
is_open = $3
|
||||
WHERE accounts.id = $4
|
||||
RETURNING *;
|
5
postgres/schema/0016_closed-accounts.sql
Normal file
5
postgres/schema/0016_closed-accounts.sql
Normal file
@ -0,0 +1,5 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE accounts ADD COLUMN is_open BOOLEAN NOT NULL DEFAULT TRUE;
|
||||
|
||||
-- +goose Down
|
||||
ALTER TABLE accounts DROP COLUMN is_open;
|
@ -39,6 +39,7 @@ type TransactionsResponse struct {
|
||||
type EditAccountRequest struct {
|
||||
Name string `json:"name"`
|
||||
OnBudget bool `json:"onBudget"`
|
||||
IsOpen bool `json:"isOpen"`
|
||||
}
|
||||
|
||||
func (h *Handler) editAccount(c *gin.Context) {
|
||||
@ -59,6 +60,7 @@ func (h *Handler) editAccount(c *gin.Context) {
|
||||
updateParams := postgres.UpdateAccountParams{
|
||||
Name: request.Name,
|
||||
OnBudget: request.OnBudget,
|
||||
IsOpen: request.IsOpen,
|
||||
ID: accountUUID,
|
||||
}
|
||||
account, err := h.Service.UpdateAccount(c.Request.Context(), updateParams)
|
||||
|
@ -7,7 +7,7 @@ const props = defineProps<{
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'submit'): void,
|
||||
(e: 'submit', event : {cancel:boolean}): boolean,
|
||||
(e: 'open'): void,
|
||||
}>();
|
||||
|
||||
@ -20,8 +20,12 @@ function openDialog() {
|
||||
visible.value = true;
|
||||
};
|
||||
function submitDialog() {
|
||||
const e = {cancel: false};
|
||||
emit("submit", e);
|
||||
if(e.cancel)
|
||||
return;
|
||||
|
||||
visible.value = false;
|
||||
emit("submit");
|
||||
}
|
||||
</script>
|
||||
|
||||
@ -38,9 +42,9 @@ function submitDialog() {
|
||||
v-if="visible"
|
||||
class="fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full"
|
||||
>
|
||||
<div class="relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white">
|
||||
<div class="relative top-20 mx-auto p-5 w-96 shadow-lg rounded-md bg-white dark:bg-black">
|
||||
<div class="mt-3 text-center">
|
||||
<h3 class="mt-3 text-lg leading-6 font-medium text-gray-900">{{ buttonText }}</h3>
|
||||
<h3 class="mt-3 text-lg leading-6 font-medium text-gray-900 dark:text-gray-100">{{ buttonText }}</h3>
|
||||
<slot></slot>
|
||||
<div class="grid grid-cols-2 gap-6">
|
||||
<button
|
||||
|
@ -3,29 +3,50 @@ import { computed, ref } from 'vue';
|
||||
import Modal from '../components/Modal.vue';
|
||||
import { useAccountStore } from '../stores/budget-account';
|
||||
import Input from '../components/Input.vue';
|
||||
import Checkbox from '../components/Checkbox.vue';
|
||||
import { useRouter } from 'vue-router';
|
||||
import { useBudgetsStore } from '../stores/budget';
|
||||
|
||||
const router = useRouter();
|
||||
const accountStore = useAccountStore();
|
||||
const CurrentAccount = computed(() => accountStore.CurrentAccount);
|
||||
|
||||
const accountName = ref("");
|
||||
const accountOnBudget = ref(true);
|
||||
const accountOpen = ref(true);
|
||||
const error = ref("");
|
||||
|
||||
function editAccount(e : any) {
|
||||
accountStore.EditAccount(CurrentAccount.value?.ID ?? "", accountName.value, accountOnBudget.value);
|
||||
function editAccount(e : {cancel:boolean}) : boolean {
|
||||
if(CurrentAccount.value?.ClearedBalance != 0 && !accountOpen.value){
|
||||
e.cancel = true;
|
||||
error.value = "Cannot close account with balance";
|
||||
return false;
|
||||
}
|
||||
|
||||
error.value = "";
|
||||
accountStore.EditAccount(CurrentAccount.value?.ID ?? "", accountName.value, accountOnBudget.value, accountOpen.value);
|
||||
|
||||
// account closed, move to Budget
|
||||
if(!accountOpen.value){
|
||||
const currentBudgetID = useBudgetsStore().CurrentBudgetID;
|
||||
router.replace('/budget/'+currentBudgetID+'/budgeting');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function openEditAccount(e : any) {
|
||||
accountName.value = CurrentAccount.value?.Name ?? "";
|
||||
accountOnBudget.value = CurrentAccount.value?.OnBudget ?? true;
|
||||
accountOpen.value = CurrentAccount.value?.IsOpen ?? true;
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Modal button-text="Edit Account" @open="openEditAccount" @submit="editAccount">
|
||||
<template v-slot:placeholder>✎</template>
|
||||
<template v-slot:placeholder><span class="ml-2">✎</span></template>
|
||||
<div class="mt-2 px-7 py-3">
|
||||
<Input
|
||||
class="border-2"
|
||||
class="border-2 dark:border-gray-700"
|
||||
type="text"
|
||||
v-model="accountName"
|
||||
placeholder="Account name"
|
||||
@ -33,13 +54,23 @@ function openEditAccount(e : any) {
|
||||
/>
|
||||
</div>
|
||||
<div class="mt-2 px-7 py-3">
|
||||
<Input
|
||||
<Checkbox
|
||||
class="border-2"
|
||||
type="checkbox"
|
||||
v-model="accountOnBudget"
|
||||
required
|
||||
/>
|
||||
<label>On Budget</label>
|
||||
</div>
|
||||
<div class="mt-2 px-7 py-3">
|
||||
<Checkbox
|
||||
class="border-2"
|
||||
v-model="accountOpen"
|
||||
required
|
||||
/>
|
||||
<label>Open</label>
|
||||
</div>
|
||||
<div v-if="error != ''" class="dark:text-red-300 text-red-700">
|
||||
{{ error }}
|
||||
</div>
|
||||
</Modal>
|
||||
</template>
|
@ -43,10 +43,12 @@ function createReconcilationTransaction() {
|
||||
|
||||
<template>
|
||||
<div class="grid grid-cols-[1fr_auto]">
|
||||
<div>
|
||||
<h1 class="inline">
|
||||
{{ accounts.CurrentAccount?.Name }}
|
||||
<EditAccount />
|
||||
</h1>
|
||||
<EditAccount />
|
||||
</div>
|
||||
|
||||
<div class="text-right flex flex-wrap flex-col md:flex-row justify-end gap-2 max-w-sm">
|
||||
<span class="rounded-lg p-1 whitespace-nowrap flex-1">
|
||||
|
@ -60,11 +60,11 @@ function getAccountName(account : Account) {
|
||||
<Currency :class="ExpandMenu?'md:inline':'md:hidden'" :value="account.ClearedBalance" />
|
||||
</div>
|
||||
</li>
|
||||
<li class="bg-red-200 dark:bg-red-800 rounded-lg m-1 p-1 px-3">
|
||||
<!--<li class="bg-red-200 dark:bg-red-800 rounded-lg m-1 p-1 px-3">
|
||||
<div class="flex flex-row justify-between font-bold">
|
||||
<span>Closed Accounts</span>
|
||||
</div>
|
||||
</li>
|
||||
</li>-->
|
||||
<!--<li>
|
||||
<router-link :to="'/budget/'+CurrentBudgetID+'/accounts'">Edit accounts</router-link>
|
||||
</li>-->
|
||||
|
@ -16,6 +16,7 @@ export interface Account {
|
||||
ID: string
|
||||
Name: string
|
||||
OnBudget: boolean
|
||||
IsOpen: boolean
|
||||
ClearedBalance: number
|
||||
WorkingBalance: number
|
||||
ReconciledBalance: number
|
||||
@ -123,10 +124,14 @@ export const useAccountStore = defineStore("budget/account", {
|
||||
return;
|
||||
this.addCategoriesForMonth(year, month, response.Categories);
|
||||
},
|
||||
async EditAccount(accountid: string, name: string, onBudget: boolean) {
|
||||
const result = await POST("/account/" + accountid, JSON.stringify({ name: name, onBudget: onBudget }));
|
||||
async EditAccount(accountid: string, name: string, onBudget: boolean, isOpen: boolean) {
|
||||
const result = await POST("/account/" + accountid, JSON.stringify({ name: name, onBudget: onBudget, isOpen: isOpen }));
|
||||
const response = await result.json();
|
||||
useBudgetsStore().MergeBudgetingData(response);
|
||||
|
||||
if(!isOpen) {
|
||||
this.Accounts.delete(accountid);
|
||||
}
|
||||
},
|
||||
addCategoriesForMonth(year: number, month: number, categories: Category[]): void {
|
||||
this.$patch((state) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user