austax: Implement creating, editing, deleting CGT adjustments

This commit is contained in:
RunasSudo 2025-06-06 21:53:39 +10:00
parent bade02d780
commit 74be08bc3d
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
6 changed files with 468 additions and 7 deletions

View File

@ -46,6 +46,9 @@ async function initApp() {
{ path: '/trial-balance', name: 'trial-balance', component: () => import('./reports/TrialBalanceReport.vue') },
// TODO: Generate this list dynamically
{ path: '/austax/cgt-adjustments', name: 'cgt-adjustments', component: () => import('./plugins/austax/CGTAdjustmentsView.vue') },
{ path: '/austax/cgt-adjustments/edit/:id', name: 'cgt-adjustments-edit', component: () => import('./plugins/austax/EditCGTAdjustmentView.vue') },
{ path: '/austax/cgt-adjustments/new', name: 'cgt-adjustments-new', component: () => import('./plugins/austax/NewCGTAdjustmentView.vue') },
{ path: '/austax/cgt-adjustments/multinew', name: 'cgt-adjustments-multinew', component: () => import('./plugins/austax/MultiNewCGTAdjustmentView.vue') },
{ path: '/austax/cgt-assets', name: 'cgt-assets', component: () => import('./plugins/austax/CGTAssetsView.vue') },
{ path: '/austax/tax-summary', name: 'tax-summary', component: () => import('./plugins/austax/TaxSummaryReport.vue') },
];

View File

@ -0,0 +1,127 @@
<!--
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 2022-2025 Lee Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<template>
<div class="grid grid-cols-[max-content_1fr] space-y-2 mb-4 items-baseline">
<h2 class="col-span-2 text-xl text-gray-900 font-semibold">CGT asset</h2>
<label for="acquisition_date" class="block text-gray-900 pr-4">Acquisition date</label>
<div>
<input type="date" class="bordered-field" name="acquisition_date" id="acquisition_date" v-model="adjustment.acquisition_dt">
</div>
<label for="account" class="block text-gray-900 pr-4">Account</label>
<ComboBoxAccounts v-model="adjustment.account" />
<label for="asset" class="block text-gray-900 pr-4">Asset</label>
<div>
<input type="text" class="bordered-field" name="asset" id="asset" v-model="adjustment.asset">
</div>
<h2 class="col-span-2 text-xl text-gray-900 font-semibold pt-4">CGT adjustment</h2>
<label for="dt" class="block text-gray-900 pr-4">Adjustment date</label>
<div>
<input type="date" class="bordered-field" name="dt" id="dt" v-model="adjustment.dt">
</div>
<label for="description" class="block text-gray-900 pr-4">Description</label>
<div>
<input type="text" class="bordered-field" name="description" id="description" v-model="adjustment.description">
</div>
<label for="cost_adjustment" class="block text-gray-900 pr-4">Cost adjustment</label>
<div class="relative shadow-sm">
<div class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
<span class="text-gray-500">{{ db.metadata.reporting_commodity }}</span>
</div>
<input type="number" class="bordered-field pl-7 pr-16" step="0.01" v-model="adjustment.cost_adjustment_abs" placeholder="0.00">
<div class="absolute inset-y-0 right-0 flex items-center">
<select class="h-full border-0 bg-transparent py-0 pl-2 pr-8 text-gray-900 focus:ring-2 focus:ring-inset focus:ring-indigo-600" v-model="adjustment.sign">
<option value="dr">Dr</option>
<option value="cr">Cr</option>
</select>
</div>
</div>
</div>
<div class="flex justify-end mt-4 space-x-2">
<button class="btn-secondary text-red-600 ring-red-500" @click="deleteAdjustment" v-if="adjustment.id !== null">Delete</button>
<button class="btn-primary" @click="saveAdjustment">Save</button>
</div>
</template>
<script setup lang="ts">
import dayjs from 'dayjs';
import { getCurrentWindow } from '@tauri-apps/api/window';
import ComboBoxAccounts from '../../components/ComboBoxAccounts.vue';
import { DT_FORMAT, db, deserialiseAmount } from '../../db.ts';
export interface EditingCGTAdjustment {
id: number | null,
asset: string,
account: string,
acquisition_dt: string,
dt: string,
description: string,
sign: string,
cost_adjustment_abs: string,
}
const { adjustment } = defineProps<{ adjustment: EditingCGTAdjustment }>();
async function saveAdjustment() {
// Save changes to the CGT adjustment
const asset = deserialiseAmount('' + adjustment.asset);
const cost_adjustment_abs = deserialiseAmount('' + adjustment.cost_adjustment_abs);
const cost_adjustment = adjustment.sign === 'dr' ? cost_adjustment_abs.quantity : -cost_adjustment_abs.quantity;
const session = await db.load();
if (adjustment.id === null) {
await session.execute(
`INSERT INTO austax_cgt_cost_adjustments (quantity, commodity, account, acquisition_dt, dt, description, cost_adjustment)
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
[asset.quantity, asset.commodity, adjustment.account, dayjs(adjustment.acquisition_dt).format(DT_FORMAT), dayjs(adjustment.dt).format(DT_FORMAT), adjustment.description, cost_adjustment]
);
} else {
await session.execute(
`UPDATE austax_cgt_cost_adjustments
SET quantity = $1, commodity = $2, account = $3, acquisition_dt = $4, dt = $5, description = $6, cost_adjustment = $7
WHERE id = $8`,
[asset.quantity, asset.commodity, adjustment.account, dayjs(adjustment.acquisition_dt).format(DT_FORMAT), dayjs(adjustment.dt).format(DT_FORMAT), adjustment.description, cost_adjustment, adjustment.id]
);
}
await getCurrentWindow().close();
}
async function deleteAdjustment() {
// Delete the current CGT adjustment
if (!await confirm('Are you sure you want to delete this CGT adjustment? This operation is irreversible.')) {
return;
}
const session = await db.load();
await session.execute(
`DELETE FROM austax_cgt_cost_adjustments
WHERE id = $1`,
[adjustment.id]
);
await getCurrentWindow().close();
}
</script>

View File

@ -22,10 +22,14 @@
</h1>
<div class="my-4 flex gap-x-2">
<!--<a :href="$router.resolve({name: 'cgt-adjustments-new'}).fullPath" class="btn-primary pl-2" onclick="return openLinkInNewWindow(this);">
<a :href="$router.resolve({name: 'cgt-adjustments-new'}).fullPath" class="btn-primary pl-2" onclick="return openLinkInNewWindow(this);">
<PlusIcon class="w-4 h-4" />
New CGT adjustment
</a>-->
</a>
<a :href="$router.resolve({name: 'cgt-adjustments-multinew'}).fullPath" class="btn-secondary pl-2 text-emerald-700 ring-emerald-600" onclick="return openLinkInNewWindow(this);">
<PlusIcon class="w-4 h-4" />
Multiple CGT adjustments
</a>
</div>
<table class="min-w-full">
@ -53,11 +57,9 @@
<td class="py-0.5 px-1 text-gray-900">{{ cgt_adjustment.description }}</td>
<td class="py-0.5 px-1 text-gray-900 text-end" v-html="ppBracketed(cgt_adjustment.cost_adjustment)"></td>
<td class="py-0.5 pl-1 text-end">
<!--<a href="#" class="text-gray-500 hover:text-gray-700">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4 inline align-middle -mt-0.5">
<path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L6.832 19.82a4.5 4.5 0 0 1-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 0 1 1.13-1.897L16.863 4.487Zm0 0L19.5 7.125" />
</svg>
</a>-->
<a :href="$router.resolve({name: 'cgt-adjustments-edit', params: {id: cgt_adjustment.id}}).fullPath" class="text-gray-500 hover:text-gray-700" onclick="return openLinkInNewWindow(this);">
<PencilIcon class="w-4 h-4" />
</a>
</td>
</tr>
</tbody>
@ -66,6 +68,8 @@
<script setup lang="ts">
import dayjs from 'dayjs';
import { PencilIcon } from '@heroicons/vue/24/outline';
import { PlusIcon } from '@heroicons/vue/16/solid';
import { ref } from 'vue';
import { CGTAdjustment, cgtAssetCommodityName } from './cgt.ts';

View File

@ -0,0 +1,70 @@
<!--
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 2022-2025 Lee Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<template>
<h1 class="page-heading mb-4">
Edit CGT adjustment
</h1>
<CGTAdjustmentEditor :adjustment="adjustment" />
</template>
<script setup lang="ts">
import dayjs from 'dayjs';
import { ref } from 'vue';
import { useRoute } from 'vue-router';
import CGTAdjustmentEditor, { EditingCGTAdjustment } from './CGTAdjustmentEditor.vue';
import { db, serialiseAmount } from '../../db.ts';
const route = useRoute();
const adjustment = ref({
id: null,
asset: null!,
account: null!,
acquisition_dt: null!,
dt: null!,
description: null!,
sign: null!,
cost_adjustment_abs: null!,
} as EditingCGTAdjustment);
async function load() {
const session = await db.load();
const rawAdjustments: any[] = await session.select(
`SELECT id, quantity, commodity, account, acquisition_dt, dt, description, cost_adjustment
FROM austax_cgt_cost_adjustments
WHERE id = $1`,
[route.params.id]
);
const rawAdjustment = rawAdjustments[0];
// Format parameters for display
rawAdjustment.asset = serialiseAmount(rawAdjustment.quantity, rawAdjustment.commodity);
rawAdjustment.acquisition_dt = dayjs(rawAdjustment.acquisition_dt).format('YYYY-MM-DD');
rawAdjustment.dt = dayjs(rawAdjustment.dt).format('YYYY-MM-DD');
rawAdjustment.sign = rawAdjustment.cost_adjustment >= 0 ? 'dr' : 'cr';
rawAdjustment.cost_adjustment_abs = serialiseAmount(Math.abs(rawAdjustment.cost_adjustment), db.metadata.reporting_commodity);
adjustment.value = rawAdjustment as EditingCGTAdjustment;
}
load();
</script>

View File

@ -0,0 +1,214 @@
<!--
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 2022-2025 Lee Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<template>
<h1 class="page-heading mb-4">
Multiple CGT adjustments
</h1>
<div class="grid grid-cols-[max-content_1fr] space-y-2 mb-4 items-baseline">
<h2 class="col-span-2 text-xl text-gray-900 font-semibold">CGT assets</h2>
<label for="account" class="block text-gray-900 pr-4">Account</label>
<ComboBoxAccounts v-model="account" />
<label for="commodity" class="block text-gray-900 pr-4">Commodity</label>
<div>
<input type="text" class="bordered-field" name="commodity" id="commodity" placeholder=" " v-model="commodity">
</div>
<div class="rounded-md bg-blue-50 p-4 col-span-2">
<div class="flex">
<div class="flex-shrink-0">
<InformationCircleIcon class="w-5 h-5 text-blue-400" />
</div>
<div class="ml-3 flex-1">
<p class="text-sm text-blue-700">The total cost adjustment will be distributed proportionally across all matching CGT assets.</p>
</div>
</div>
</div>
<h2 class="col-span-2 text-xl text-gray-900 font-semibold pt-4">CGT adjustment</h2>
<label for="dt" class="block text-gray-900 pr-4">Adjustment date</label>
<div>
<input type="date" class="bordered-field" name="dt" id="dt" v-model="dt">
</div>
<label for="description" class="block text-gray-900 pr-4">Description</label>
<div>
<input type="text" class="bordered-field" name="description" id="description" placeholder=" " v-model="description">
</div>
<label for="cost_adjustment" class="block text-gray-900 pr-4">Total cost adjustment</label>
<div class="relative shadow-sm">
<div class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
<span class="text-gray-500">{{ db.metadata.reporting_commodity }}</span>
</div>
<input type="number" class="bordered-field pl-7 pr-16" step="0.01" v-model="cost_adjustment_abs" placeholder="0.00">
<div class="absolute inset-y-0 right-0 flex items-center">
<select class="h-full border-0 bg-transparent py-0 pl-2 pr-8 text-gray-900 focus:ring-2 focus:ring-inset focus:ring-indigo-600" v-model="sign">
<option value="dr">Dr</option>
<option value="cr">Cr</option>
</select>
</div>
</div>
</div>
<div class="flex justify-end mt-4 space-x-2">
<button class="btn-primary" @click="saveAdjustment">Save</button>
</div>
</template>
<script setup lang="ts">
import dayjs from 'dayjs';
import { InformationCircleIcon } from '@heroicons/vue/20/solid';
import { getCurrentWindow } from '@tauri-apps/api/window';
import { ref } from 'vue';
import { CGTAsset } from './cgt.ts';
import ComboBoxAccounts from '../../components/ComboBoxAccounts.vue';
import { DT_FORMAT, JoinedTransactionPosting, db, deserialiseAmount } from '../../db.ts';
import { ppWithCommodity } from '../../display.ts';
const account = ref('');
const commodity = ref('');
const dt = ref(dayjs().format('YYYY-MM-DD'));
const description = ref('');
const cost_adjustment_abs = ref(null! as number);
const sign = ref('dr');
async function saveAdjustment() {
// TODO: Preview mode?
const totalAdjustmentAbs = deserialiseAmount('' + cost_adjustment_abs.value);
const totalAdjustment = sign.value === 'dr' ? totalAdjustmentAbs.quantity : -totalAdjustmentAbs.quantity;
// Get all postings to the CGT asset account
const session = await db.load();
const cgtPostings = await session.select(
`SELECT *
FROM joined_transactions
WHERE account = $1`,
[account.value]
) as JoinedTransactionPosting[];
// Process postings to determine final balances
// Based on cgt.ts getCGTAssets
const assets: CGTAsset[] = [];
for (const posting of cgtPostings) {
// Check for matching asset
if (posting.commodity.indexOf(' {') < 0) {
if (posting.commodity !== commodity.value) {
continue;
}
} else {
const postingCommodityName = posting.commodity.substring(0, posting.commodity.indexOf(' {'));
if (postingCommodityName !== commodity.value) {
continue;
}
}
// This is a matching CGT asset
if (posting.quantity >= 0) {
// Debit CGT asset - create new CGTAsset
assets.push(new CGTAsset(posting.quantity, posting.commodity, posting.account, posting.dt))
} else {
// Credit CGT asset
// Currently only a full disposal of a CGT asset is implemented
// Find matching CGT asset
const asset = assets.find((a) => a.commodity === posting.commodity && a.account === posting.account);
if (!asset) {
throw new Error('Attempted credit of ' + ppWithCommodity(posting.quantity, posting.commodity) + ' without preceding debit balance');
}
if (asset.quantity + posting.quantity < 0) {
throw new Error('Attempted credit of ' + ppWithCommodity(posting.quantity, posting.commodity) + ' which exceeds debit balance of ' + ppWithCommodity(asset.quantity, asset.commodity));
}
if (asset.quantity + posting.quantity != 0) {
throw new Error('Partial disposal of CGT asset not implemented');
}
assets.splice(assets.indexOf(asset), 1);
}
}
if (assets.length === 0) {
throw new Error('No matching CGT assets');
}
// Distribute total adjustment across matching assets
const totalQuantity = assets.reduce((acc, asset) => acc + asset.quantity, 0)
const cgtAdjustments = [];
for (let i = 0; i < assets.length; i++) {
// This might be fractional at this stage
cgtAdjustments[i] = totalAdjustment * assets[i].quantity / totalQuantity;
}
// Compute the difference due to rounding
const roundingShortfall = cgtAdjustments.reduce((acc, adj) => acc - Math.floor(Math.abs(adj)), Math.abs(totalAdjustment));
// Sort by largest remainder
const largestRemainders = cgtAdjustments.map((adj, i) => [Math.abs(adj) - Math.floor(Math.abs(adj)), i]);
largestRemainders.sort((a, b) => b[0] - a[0]);
// Round up as many as required to equal the total adjustment
let i = 0;
for (; i < roundingShortfall; i++) {
const assetIndex = largestRemainders[i][1];
const cgtAdjustment: number = cgtAdjustments[assetIndex];
// Round away from zero
const cgtAdjustmentAbs = Math.floor(Math.abs(cgtAdjustment)) + 1;
cgtAdjustments[assetIndex] = cgtAdjustmentAbs * Math.sign(cgtAdjustment);
}
// Round others down
for (; i < largestRemainders.length; i++) {
const assetIndex = largestRemainders[i][1];
const cgtAdjustment: number = cgtAdjustments[assetIndex];
// Round towards zero
const cgtAdjustmentAbs = Math.floor(Math.abs(cgtAdjustment));
cgtAdjustments[assetIndex] = cgtAdjustmentAbs * Math.sign(cgtAdjustment);
}
// Sanity check
const totalRoundedAdjustment = cgtAdjustments.reduce((acc, adj) => acc + adj, 0);
if (totalRoundedAdjustment !== totalAdjustment) {
throw new Error('Rounding unexpectedly changed total CGT adjustment amount');
}
// Add adjustments to database atomically
const dbTransaction = await session.begin();
for (let i = 0; i < assets.length; i++) {
const asset = assets[i];
const cgtAdjustment = cgtAdjustments[i];
await dbTransaction.execute(
`INSERT INTO austax_cgt_cost_adjustments (quantity, commodity, account, acquisition_dt, dt, description, cost_adjustment)
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
[asset.quantity, asset.commodity, asset.account, asset.acquisition_dt, dayjs(dt.value).format(DT_FORMAT), description.value, cgtAdjustment]
);
}
await dbTransaction.commit();
await getCurrentWindow().close();
}
</script>

View File

@ -0,0 +1,43 @@
<!--
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 2022-2025 Lee Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<template>
<h1 class="page-heading mb-4">
New CGT adjustment
</h1>
<CGTAdjustmentEditor :adjustment="adjustment" />
</template>
<script setup lang="ts">
import dayjs from 'dayjs';
import { ref } from 'vue';
import CGTAdjustmentEditor, { EditingCGTAdjustment } from './CGTAdjustmentEditor.vue';
const adjustment = ref({
id: null,
asset: '',
account: '',
acquisition_dt: dayjs().format('YYYY-MM-DD'),
dt: dayjs().format('YYYY-MM-DD'),
description: '',
sign: 'dr',
cost_adjustment_abs: '',
} as EditingCGTAdjustment);
</script>