diff --git a/src/commodities.ts b/src/commodities.ts
new file mode 100644
index 0000000..8a27866
--- /dev/null
+++ b/src/commodities.ts
@@ -0,0 +1,38 @@
+/*
+ DrCr: Web-based double-entry bookkeeping framework
+ Copyright (C) 2022–2024 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 .
+*/
+
+import { db } from './db.ts';
+
+export function asCost(quantity: number, commodity: string): number {
+ // Convert the amount to cost price in the reporting commodity
+
+ if (commodity === db.metadata.reporting_commodity) {
+ return quantity;
+ }
+ if (commodity.indexOf('{{') >= 0) {
+ // Total price
+ const price = parseFloat(commodity.substring(commodity.indexOf('{{') + 2, commodity.indexOf('}}', commodity.indexOf('{{'))));
+ return Math.round(price * Math.pow(10, db.metadata.dps));
+ }
+ if (commodity.indexOf('{') >= 0) {
+ // Unit price
+ const price = parseFloat(commodity.substring(commodity.indexOf('{') + 1, commodity.indexOf('}', commodity.indexOf('{'))));
+ return Math.round(quantity * price);
+ }
+ throw new Error('No cost base specified: ' + quantity + ' ' + commodity);
+}
diff --git a/src/display.ts b/src/display.ts
index b8fff6e..639d7d5 100644
--- a/src/display.ts
+++ b/src/display.ts
@@ -19,6 +19,7 @@
import { db } from './db.ts';
export function pp(quantity: number): string {
+ // Pretty print the quantity
if (quantity < 0) {
return '−' + pp(-quantity);
}
@@ -29,3 +30,12 @@ export function pp(quantity: number): string {
return wholePart.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '\u202F') + '.' + fracPart.toString().padStart(db.metadata.dps, '0');
}
+
+export function ppWithCommodity(quantity: number, commodity: string): string {
+ // Pretty print the amount including commodity
+ if (commodity.length === 1) {
+ return commodity + pp(quantity);
+ } else {
+ return pp(quantity) + ' ' + commodity;
+ }
+}
diff --git a/src/main.ts b/src/main.ts
index 41f6d23..c0fdd63 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -23,6 +23,7 @@ import { createRouter, createWebHistory } from 'vue-router';
import App from './App.vue';
import HomeView from './HomeView.vue';
+import GeneralLedgerView from './GeneralLedgerView.vue';
import TrialBalanceView from './TrialBalanceView.vue';
import { db } from './db.ts';
@@ -31,6 +32,7 @@ async function initApp() {
// Init router
const routes = [
{ path: '/', component: HomeView },
+ { path: '/general-ledger', component: GeneralLedgerView },
{ path: '/trial-balance', component: TrialBalanceView },
];
const router = createRouter({
@@ -41,7 +43,7 @@ async function initApp() {
// Init state
const dbFilename: string = await invoke('get_open_filename');
if (dbFilename !== null) {
- db.init(dbFilename);
+ await db.init(dbFilename); // Ensure all metadata cached before loading Vue
}
// Create Vue app