Compare commits

...

6 Commits

12 changed files with 668 additions and 6 deletions

View File

@ -22,6 +22,10 @@
-- false = Charge income tax expense in one transaction at end of financial year
local charge_tax_monthly = true
-- true = Include the Medicare levy surcharge
-- false = Do not include the Medicare levy surcharge
local include_mls = false
-----------------
-- Reporting code
@ -246,7 +250,10 @@ function reporting.CalculateIncomeTax.execute(args, context, kinds_for_account,
local rfb_grossedup = calc.rfb_grossup(rfb_taxable, context)
-- Medicare levy surcharge row
local tax_mls = calc.medicare_levy_surcharge(net_taxable, rfb_grossedup, context)
local tax_mls = 0
if include_mls then
tax_mls = calc.medicare_levy_surcharge(net_taxable, rfb_grossedup, context)
end
if tax_mls ~= 0 then
table.insert(report.entries, { Row = {
text = 'Medicare levy surcharge',

View File

@ -1,5 +1,5 @@
-- DrCr: Web-based double-entry bookkeeping framework
-- Copyright (C) 20222025 Lee Yingtong Li (RunasSudo)
-- 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
@ -80,6 +80,21 @@ CREATE TABLE transactions (
PRIMARY KEY(id)
);
---------
-- austax
CREATE TABLE austax_cgt_cost_adjustments (
id INTEGER NOT NULL,
quantity INTEGER,
commodity VARCHAR,
account VARCHAR,
acquisition_dt DATETIME,
dt DATETIME,
description VARCHAR,
cost_adjustment INTEGER,
PRIMARY KEY (id)
)
--------
-- Views

View File

@ -1,6 +1,6 @@
<!--
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 20222024 Lee Yingtong Li (RunasSudo)
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
@ -33,7 +33,6 @@
<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>
<!-- TODO: Display existing credit assertion as credit, not as negative debit -->
<input type="number" class="bordered-field pl-7 pr-16" step="0.01" v-model="assertion.amount_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="assertion.sign">

View File

@ -44,6 +44,11 @@ async function initApp() {
{ path: '/statement-lines/import', name: 'import-statement', component: () => import('./pages/ImportStatementView.vue') },
{ path: '/transactions/:account', name: 'transactions', component: () => import('./pages/TransactionsView.vue') },
{ 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/new', name: 'cgt-adjustments-new', component: () => import('./plugins/austax/NewCGTAdjustmentView.vue') },
{ path: '/austax/cgt-adjustments/edit/:id', name: 'cgt-adjustments-edit', component: () => import('./plugins/austax/EditCGTAdjustmentView.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') },
];
const router = createRouter({

View File

@ -52,7 +52,7 @@
<XMarkIcon class="w-4 h-4 text-red-500" v-if="assertion.is_valid === false" />
</td>
<td class="py-0.5 pl-1 text-gray-900 text-end">
<a :href="'/balance-assertions/edit/' + assertion.id" class="text-gray-500 hover:text-gray-700" onclick="return openLinkInNewWindow(this);">
<a :href="$router.resolve({name: 'balance-assertions-edit', params: {id: assertion.id}}).fullPath" class="text-gray-500 hover:text-gray-700" onclick="return openLinkInNewWindow(this);">
<PencilIcon class="w-4 h-4" />
</a>
</td>

View File

@ -25,7 +25,9 @@
<li><RouterLink :to="{ name: 'statement-lines' }" class="text-gray-900 hover:text-blue-700 hover:underline">Statement lines</RouterLink></li>
<li><RouterLink :to="{ name: 'balance-assertions' }" class="text-gray-900 hover:text-blue-700 hover:underline">Balance assertions</RouterLink></li>
<li><RouterLink :to="{ name: 'chart-of-accounts' }" class="text-gray-900 hover:text-blue-700 hover:underline">Chart of accounts</RouterLink></li>
<!-- TODO: Plugin reports -->
<!-- Plugin reports -->
<!-- TODO: Generate this list dynamically -->
<li><RouterLink :to="{ name: 'cgt-adjustments' }" class="text-gray-900 hover:text-blue-700 hover:underline">CGT adjustments</RouterLink></li>
</ul>
</div>
<div class="px-4">
@ -41,6 +43,7 @@
<h2 class="font-medium text-gray-700 mb-2">Advanced reports</h2>
<ul class="list-disc ml-6">
<!-- 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>
</ul>
</div>

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

@ -0,0 +1,89 @@
<!--
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 adjustments
</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);">
<PlusIcon class="w-4 h-4" />
New CGT adjustment
</a>
</div>
<table class="min-w-full">
<thead>
<tr class="border-b border-gray-300">
<th class="py-0.5 pr-1 text-gray-900 font-semibold text-start">Account</th>
<th class="py-0.5 px-1 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">Acquisition date</th>
<th class="py-0.5 px-1 text-gray-900 font-semibold text-end">Acquisition value</th>
<th class="py-0.5 px-1 text-gray-900 font-semibold text-start">Adjustment date</th>
<th class="py-0.5 px-1 text-gray-900 font-semibold text-start">Description</th>
<th class="py-0.5 px-1 text-gray-900 font-semibold text-end">Cost adjustment&nbsp;</th>
<th></th>
</tr>
</thead>
<tbody>
<tr v-for="cgt_adjustment of cgtAdjustments">
<td class="py-0.5 pr-1 text-gray-900">{{ cgt_adjustment.account }}</td>
<td class="py-0.5 px-1 text-gray-900">{{ cgtAssetCommodityName(cgt_adjustment.commodity) }}</td>
<td class="py-0.5 px-1 text-gray-900 text-end">{{ pp(cgt_adjustment.quantity) }}</td>
<td class="py-0.5 px-1 text-gray-900">{{ dayjs(cgt_adjustment.acquisition_dt).format('YYYY-MM-DD') }}</td>
<td class="py-0.5 px-1 text-gray-900 text-end">{{ pp(asCost(cgt_adjustment.quantity, cgt_adjustment.commodity)) }}</td>
<td class="py-0.5 px-1 text-gray-900">{{ dayjs(cgt_adjustment.dt).format('YYYY-MM-DD') }}</td>
<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="$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>
</table>
</template>
<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';
import { asCost } from '../../amounts.ts';
import { db } from '../../db.ts';
import { pp, ppBracketed } from '../../display.ts';
const cgtAdjustments = ref([] as CGTAdjustment[]);
async function load() {
// Load CGT adjustments from database
const session = await db.load();
cgtAdjustments.value = 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 DESC, id DESC`
);
}
load();
</script>

View 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&nbsp;</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&nbsp;</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>

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,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>

152
src/plugins/austax/cgt.ts Normal file
View 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(' {'));
}