austax: Display CGT assets
This commit is contained in:
parent
af98a800a7
commit
bade02d780
@ -46,6 +46,7 @@ async function initApp() {
|
|||||||
{ path: '/trial-balance', name: 'trial-balance', component: () => import('./reports/TrialBalanceReport.vue') },
|
{ path: '/trial-balance', name: 'trial-balance', component: () => import('./reports/TrialBalanceReport.vue') },
|
||||||
// TODO: Generate this list dynamically
|
// TODO: Generate this list dynamically
|
||||||
{ path: '/austax/cgt-adjustments', name: 'cgt-adjustments', component: () => import('./plugins/austax/CGTAdjustmentsView.vue') },
|
{ path: '/austax/cgt-adjustments', name: 'cgt-adjustments', component: () => import('./plugins/austax/CGTAdjustmentsView.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') },
|
{ path: '/austax/tax-summary', name: 'tax-summary', component: () => import('./plugins/austax/TaxSummaryReport.vue') },
|
||||||
];
|
];
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
|
@ -43,6 +43,7 @@
|
|||||||
<h2 class="font-medium text-gray-700 mb-2">Advanced reports</h2>
|
<h2 class="font-medium text-gray-700 mb-2">Advanced reports</h2>
|
||||||
<ul class="list-disc ml-6">
|
<ul class="list-disc ml-6">
|
||||||
<!-- TODO: Generate this list dynamically -->
|
<!-- TODO: Generate this list dynamically -->
|
||||||
|
<li><RouterLink :to="{ name: 'cgt-assets' }" class="text-gray-900 hover:text-blue-700 hover:underline">CGT assets</RouterLink></li>
|
||||||
<li><RouterLink :to="{ name: 'tax-summary' }" class="text-gray-900 hover:text-blue-700 hover:underline">Tax summary</RouterLink></li>
|
<li><RouterLink :to="{ name: 'tax-summary' }" class="text-gray-900 hover:text-blue-700 hover:underline">Tax summary</RouterLink></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
@ -68,7 +68,7 @@
|
|||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import { ref } from 'vue';
|
import { ref } from 'vue';
|
||||||
|
|
||||||
import { CGTAdjustment } from './model.ts';
|
import { CGTAdjustment, cgtAssetCommodityName } from './cgt.ts';
|
||||||
import { asCost } from '../../amounts.ts';
|
import { asCost } from '../../amounts.ts';
|
||||||
import { db } from '../../db.ts';
|
import { db } from '../../db.ts';
|
||||||
import { pp, ppBracketed } from '../../display.ts';
|
import { pp, ppBracketed } from '../../display.ts';
|
||||||
@ -81,13 +81,9 @@
|
|||||||
cgtAdjustments.value = await session.select(
|
cgtAdjustments.value = await session.select(
|
||||||
`SELECT id, quantity, commodity, account, acquisition_dt, dt, description, cost_adjustment
|
`SELECT id, quantity, commodity, account, acquisition_dt, dt, description, cost_adjustment
|
||||||
FROM austax_cgt_cost_adjustments
|
FROM austax_cgt_cost_adjustments
|
||||||
ORDER BY dt DESC, account, substr(commodity, 1, instr(commodity, ' {')), acquisition_dt, id DESC`
|
ORDER BY dt DESC, account, substr(commodity, 1, instr(commodity, ' {')), acquisition_dt DESC, id DESC`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
load();
|
load();
|
||||||
|
|
||||||
function cgtAssetCommodityName(commodity: string): string {
|
|
||||||
return commodity.substring(0, commodity.indexOf(' {'));
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
152
src/plugins/austax/CGTAssetsView.vue
Normal file
152
src/plugins/austax/CGTAssetsView.vue
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
<!--
|
||||||
|
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">
|
||||||
|
CGT assets
|
||||||
|
</h1>
|
||||||
|
|
||||||
|
<table class="min-w-full">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th></th>
|
||||||
|
<th></th>
|
||||||
|
<th></th>
|
||||||
|
<th class="py-0.5 px-1 text-gray-900 font-semibold text-start border-l border-gray-300" colspan="2">Acquisition</th>
|
||||||
|
<th class="py-0.5 px-1 text-gray-900 font-semibold text-start border-l border-gray-300" colspan="2">Adjustment</th>
|
||||||
|
<th class="print:hidden"></th>
|
||||||
|
<th class="py-0.5 px-1 text-gray-900 font-semibold text-start border-l border-gray-300" colspan="2">Disposal</th>
|
||||||
|
<th class="border-l border-gray-300"></th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th class="py-0.5 text-gray-900 font-semibold text-start">Account</th>
|
||||||
|
<th class="py-0.5 text-gray-900 font-semibold text-start">Asset</th>
|
||||||
|
<th class="py-0.5 px-1 text-gray-900 font-semibold text-end">Units</th>
|
||||||
|
<th class="py-0.5 px-1 text-gray-900 font-semibold text-start border-l border-gray-300">Date</th>
|
||||||
|
<th class="py-0.5 px-1 text-gray-900 font-semibold text-end">Value</th>
|
||||||
|
<th class="py-0.5 px-1 text-gray-900 font-semibold text-end border-l border-gray-300">b/f </th>
|
||||||
|
<th class="py-0.5 px-1 text-gray-900 font-semibold text-end">{{ eofyDate?.format('YYYY') }}</th>
|
||||||
|
<th class="print:hidden"></th>
|
||||||
|
<th class="py-0.5 px-1 text-gray-900 font-semibold text-start border-l border-gray-300">Date</th>
|
||||||
|
<th class="py-0.5 px-1 text-gray-900 font-semibold text-end">Value</th>
|
||||||
|
<th class="py-0.5 pl-1 text-gray-900 font-semibold text-end border-l border-gray-300">Gain </th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr v-for="asset of cgtAssets" class="border-t border-gray-300">
|
||||||
|
<td class="py-0.5 pr-1 text-gray-900">
|
||||||
|
<RouterLink :to="{ name: 'transactions', params: { account: asset.account } }" class="hover:text-blue-700 hover:underline">{{ asset.account }}</RouterLink>
|
||||||
|
</td>
|
||||||
|
<td class="py-0.5 px-1 text-gray-900">{{ cgtAssetCommodityName(asset.commodity) }}</td>
|
||||||
|
<td class="py-0.5 px-1 text-gray-900 text-end">{{ pp(asset.quantity) }}</td>
|
||||||
|
<td class="py-0.5 px-1 text-gray-900 border-l border-gray-300">{{ dayjs(asset.acquisition_dt).format('YYYY-MM-DD') }}</td>
|
||||||
|
<td class="py-0.5 px-1 text-gray-900 text-end">{{ pp(asCost(asset.quantity, asset.commodity)) }}</td>
|
||||||
|
<td class="py-0.5 px-1 text-gray-900 text-end border-l border-gray-300" v-html="costAdjustmentBroughtForward(asset)"></td>
|
||||||
|
<td class="py-0.5 px-1 text-gray-900 text-end" v-html="costAdjustmentCurrentPeriod(asset)"></td>
|
||||||
|
<td class="py-0.5 px-1 text-gray-900 text-center print:hidden">
|
||||||
|
<!--<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">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
|
||||||
|
</svg>
|
||||||
|
</a>-->
|
||||||
|
</td>
|
||||||
|
<td class="py-0.5 px-1 text-gray-900 border-l border-gray-300">{{ asset.disposal_dt !== null ? dayjs(asset.disposal_dt).format('YYYY-MM-DD') : '' }}</td>
|
||||||
|
<td class="py-0.5 px-1 text-gray-900 text-end">{{ asset.disposal_value !== null ? pp(asset.disposal_value) : '' }}</td>
|
||||||
|
<td class="py-0.5 pl-1 text-gray-900 text-end border-l border-gray-300" v-html="cgtAssetGain(asset)"></td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import dayjs from 'dayjs';
|
||||||
|
import { ref } from 'vue';
|
||||||
|
|
||||||
|
import { asCost } from '../../amounts.ts';
|
||||||
|
import { CGTAsset, cgtAssetCommodityName, getCGTAssets } from './cgt.ts';
|
||||||
|
import { db } from '../../db.ts';
|
||||||
|
import { pp, ppBracketed } from '../../display.ts';
|
||||||
|
|
||||||
|
const cgtAssets = ref([] as CGTAsset[]);
|
||||||
|
const eofyDate = ref(null as dayjs.Dayjs | null);
|
||||||
|
|
||||||
|
async function load() {
|
||||||
|
// Load CGT assets
|
||||||
|
const session = await db.load();
|
||||||
|
cgtAssets.value = await getCGTAssets(session);
|
||||||
|
eofyDate.value = dayjs(db.metadata.eofy_date);
|
||||||
|
|
||||||
|
console.log(cgtAssets.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
load();
|
||||||
|
|
||||||
|
function costAdjustmentBroughtForward(asset: CGTAsset): string {
|
||||||
|
const thisEofyDate = eofyDate.value!;
|
||||||
|
const lastEofyDate = thisEofyDate.set('year', thisEofyDate.year() - 1);
|
||||||
|
|
||||||
|
let total = 0;
|
||||||
|
for (const costAdjustment of asset.cost_adjustments) {
|
||||||
|
if (!dayjs(costAdjustment.dt).isAfter(lastEofyDate)) {
|
||||||
|
total += costAdjustment.cost_adjustment;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (total !== 0) {
|
||||||
|
// TODO: Link to CGT adjustments page
|
||||||
|
return ppBracketed(total);
|
||||||
|
} else {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function costAdjustmentCurrentPeriod(asset: CGTAsset): string {
|
||||||
|
const thisEofyDate = eofyDate.value!;
|
||||||
|
const lastEofyDate = thisEofyDate.set('year', thisEofyDate.year() - 1);
|
||||||
|
|
||||||
|
let total = 0;
|
||||||
|
for (const costAdjustment of asset.cost_adjustments) {
|
||||||
|
if (dayjs(costAdjustment.dt).isAfter(lastEofyDate) && !dayjs(costAdjustment.dt).isAfter(thisEofyDate)) {
|
||||||
|
total += costAdjustment.cost_adjustment;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (total !== 0) {
|
||||||
|
// TODO: Link to CGT adjustments page
|
||||||
|
return ppBracketed(total);
|
||||||
|
} else {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function cgtAssetGain(asset: CGTAsset): string {
|
||||||
|
if (asset.disposal_dt === null) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get total cost adjustments
|
||||||
|
let totalCostAdjustment = 0;
|
||||||
|
for (const costAdjustment of asset.cost_adjustments) {
|
||||||
|
totalCostAdjustment += costAdjustment.cost_adjustment;
|
||||||
|
}
|
||||||
|
|
||||||
|
const netGain = asset.disposal_value! - (asCost(asset.quantity, asset.commodity) + totalCostAdjustment);
|
||||||
|
|
||||||
|
return ppBracketed(netGain);
|
||||||
|
}
|
||||||
|
</script>
|
152
src/plugins/austax/cgt.ts
Normal file
152
src/plugins/austax/cgt.ts
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { asCost } from '../../amounts.ts';
|
||||||
|
import { db, joinedToTransactions, JoinedTransactionPosting } from '../../db.ts';
|
||||||
|
import { ExtendedDatabase } from '../../dbutil.ts';
|
||||||
|
import { ppWithCommodity } from '../../display.ts';
|
||||||
|
|
||||||
|
export interface CGTAdjustment {
|
||||||
|
id: number | null,
|
||||||
|
quantity: number,
|
||||||
|
commodity: string,
|
||||||
|
account: string,
|
||||||
|
acquisition_dt: string,
|
||||||
|
dt: string,
|
||||||
|
description: string,
|
||||||
|
cost_adjustment: number,
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CGTAsset {
|
||||||
|
cost_adjustments: CGTAdjustment[];
|
||||||
|
disposal_dt: string | null;
|
||||||
|
disposal_value: number | null;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
public quantity: number,
|
||||||
|
public commodity: string,
|
||||||
|
public account: string,
|
||||||
|
public acquisition_dt: string
|
||||||
|
) {
|
||||||
|
this.cost_adjustments = [];
|
||||||
|
this.disposal_dt = null;
|
||||||
|
this.disposal_value = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load CGT assets and cost adjustments from database
|
||||||
|
export async function getCGTAssets(session: ExtendedDatabase) {
|
||||||
|
// Find all CGT asset accounts
|
||||||
|
const cgtAccounts = (await session.select(
|
||||||
|
`SELECT account FROM account_configurations
|
||||||
|
WHERE kind = 'austax.cgtasset'`
|
||||||
|
) as {account: string}[]).map((a) => a.account);
|
||||||
|
|
||||||
|
// Find all asset accounts (used to calculate disposal values)
|
||||||
|
const assetAccounts = (await session.select(
|
||||||
|
`SELECT account FROM account_configurations
|
||||||
|
WHERE kind = 'drcr.asset'`
|
||||||
|
) as {account: string}[]).map((a) => a.account);
|
||||||
|
|
||||||
|
// Get all transactions involving CGT asset accounts
|
||||||
|
const cgtJoinedTransactions = await session.select(
|
||||||
|
`SELECT joined_transactions.*
|
||||||
|
FROM postings
|
||||||
|
JOIN joined_transactions ON postings.transaction_id = joined_transactions.transaction_id
|
||||||
|
JOIN account_configurations ON postings.account = account_configurations.account
|
||||||
|
WHERE account_configurations.kind = 'austax.cgtasset'`
|
||||||
|
) as JoinedTransactionPosting[];
|
||||||
|
const cgtTransactions = joinedToTransactions(cgtJoinedTransactions);
|
||||||
|
|
||||||
|
// Process postings to determine final balances
|
||||||
|
const assets: CGTAsset[] = [];
|
||||||
|
|
||||||
|
for (const transaction of cgtTransactions) {
|
||||||
|
for (const posting of transaction.postings) {
|
||||||
|
if (cgtAccounts.indexOf(posting.account) < 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (posting.commodity === db.metadata.reporting_commodity) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This posting is to a CGT asset account
|
||||||
|
|
||||||
|
if (posting.quantity >= 0) {
|
||||||
|
// Debit CGT asset - create new CGTAsset
|
||||||
|
assets.push(new CGTAsset(posting.quantity, posting.commodity, posting.account, transaction.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');
|
||||||
|
}
|
||||||
|
|
||||||
|
asset.disposal_dt = transaction.dt;
|
||||||
|
|
||||||
|
// Calculate disposal value for searching for matching asset postings
|
||||||
|
asset.disposal_value = 0;
|
||||||
|
for (const otherPosting of transaction.postings) {
|
||||||
|
if (otherPosting !== posting && assetAccounts.indexOf(otherPosting.account) >= 0) {
|
||||||
|
asset.disposal_value += asCost(otherPosting.quantity, otherPosting.commodity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get all CGT cost adjustments
|
||||||
|
const cgtAdjustments = await session.select(
|
||||||
|
`SELECT id, quantity, commodity, account, acquisition_dt, dt, description, cost_adjustment
|
||||||
|
FROM austax_cgt_cost_adjustments
|
||||||
|
ORDER BY dt DESC, account, substr(commodity, 1, instr(commodity, ' {')), acquisition_dt, id DESC`
|
||||||
|
) as CGTAdjustment[];
|
||||||
|
|
||||||
|
// Process CGT adjustments
|
||||||
|
for (const cgtAdjustment of cgtAdjustments) {
|
||||||
|
// Get corresponding asset
|
||||||
|
const asset = assets.find((a) => a.quantity === cgtAdjustment.quantity && a.commodity === cgtAdjustment.commodity && a.account === cgtAdjustment.account && a.acquisition_dt === cgtAdjustment.acquisition_dt);
|
||||||
|
|
||||||
|
if (!asset) {
|
||||||
|
throw new Error('No matching CGT asset for cost adjustment ' + ppWithCommodity(cgtAdjustment.quantity, cgtAdjustment.commodity));
|
||||||
|
}
|
||||||
|
|
||||||
|
asset.cost_adjustments.push(cgtAdjustment);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort CGT assets
|
||||||
|
assets.sort((a, b) => b.acquisition_dt.localeCompare(a.acquisition_dt));
|
||||||
|
assets.sort((a, b) => cgtAssetCommodityName(a.commodity).localeCompare(cgtAssetCommodityName(b.commodity)));
|
||||||
|
assets.sort((a, b) => a.account.localeCompare(b.account));
|
||||||
|
|
||||||
|
return assets;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function cgtAssetCommodityName(commodity: string): string {
|
||||||
|
return commodity.substring(0, commodity.indexOf(' {'));
|
||||||
|
}
|
@ -1,28 +0,0 @@
|
|||||||
/*
|
|
||||||
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/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
export interface CGTAdjustment {
|
|
||||||
id: number | null,
|
|
||||||
quantity: number,
|
|
||||||
commodity: string,
|
|
||||||
account: string,
|
|
||||||
acquisition_dt: string,
|
|
||||||
dt: string,
|
|
||||||
description: string,
|
|
||||||
cost_adjustment: number,
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user