Skip to content

Commit

Permalink
coin2html: add location, list accounts depth first
Browse files Browse the repository at this point in the history
  • Loading branch information
mkobetic committed Dec 7, 2024
1 parent c2d2b1c commit aa3e639
Show file tree
Hide file tree
Showing 9 changed files with 2,637 additions and 30 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ examples/yearly/viewer/index.html: export COINDB=./examples/yearly
examples/yearly/viewer/index.html: coin2html
coin2html >$(COINDB)/viewer/index.html

examples: examples/yearly/viewer/index.html

dfa: dfa.bash
cp ./dfa.bash $(GOPATH1)/bin/

Expand Down
1 change: 1 addition & 0 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ func (a *Account) MarshalJSON() ([]byte, error) {
"name": a.Name,
"fullName": a.FullName,
"commodity": a.Commodity.Id,
"location": a.Location(),
}
if !a.Closed.IsZero() {
value["closed"] = a.Closed.Format(DateFormat)
Expand Down
2 changes: 1 addition & 1 deletion cmd/coin2html/js/spec/commodity.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Commodity } from "../src/models";

test("create commodity", () =>
expect(new Commodity("CAD", "Canadian Dollar", 2)).toBeTruthy());
expect(new Commodity("CAD", "Canadian Dollar", 2, "")).toBeTruthy());
31 changes: 24 additions & 7 deletions cmd/coin2html/js/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export class Commodity {
constructor(
readonly id: string,
readonly name: string,
readonly decimals: number
readonly decimals: number,
readonly location: string
) {}
toString(): string {
return this.id;
Expand Down Expand Up @@ -140,7 +141,8 @@ export class Price {
constructor(
readonly commodity: Commodity,
readonly date: Date,
readonly value: Amount
readonly value: Amount,
readonly location: string
) {
commodity.prices.push(this);
}
Expand All @@ -163,15 +165,18 @@ export class Account {
readonly fullName: string,
readonly commodity: Commodity,
readonly parent: Account,
readonly location: string,
readonly closed?: Date
) {
if (parent) {
parent.children.push(this);
}
}
allChildren(): Account[] {
return this.children.concat(
this.children.map((c) => c.allChildren()).flat()
return this.children.reduce(
(total: Account[], acc: Account) =>
total.concat([acc, ...acc.allChildren()]),
[]
);
}
toString(): string {
Expand Down Expand Up @@ -247,6 +252,7 @@ export class Transaction {
constructor(
readonly posted: Date,
readonly description: string,
readonly location: string,
readonly notes?: string[],
readonly code?: string
) {}
Expand All @@ -270,13 +276,14 @@ export class Transaction {

type importedCommodities = Record<
string,
{ id: string; name: string; decimals: number }
{ id: string; name: string; decimals: number; location: string }
>;
type importedPrices = {
commodity: string;
currency: string;
time: string;
value: string;
location: string;
}[];

const Commodities: Record<string, Commodity> = {};
Expand All @@ -288,7 +295,8 @@ function loadCommodities() {
const commodity = new Commodity(
impCommodity.id,
impCommodity.name,
impCommodity.decimals
impCommodity.decimals,
impCommodity.location
);
Commodities[commodity.id] = commodity;
}
Expand All @@ -308,7 +316,12 @@ function loadCommodities() {
`Parsed amount "${amount}" doesn't match imported "${imported.value}"`
);
}
const price = new Price(commodity, new Date(imported.time), amount);
const price = new Price(
commodity,
new Date(imported.time),
amount,
imported.location
);
commodity.prices.push(price);
}
}
Expand All @@ -322,11 +335,13 @@ type importedAccounts = Record<
commodity: string;
parent: string;
closed?: string;
location: string;
}
>;
type importedTransactions = {
posted: string;
description: string;
location: string;
postings: {
account: string;
balance: string;
Expand Down Expand Up @@ -358,6 +373,7 @@ function loadAccounts() {
impAccount.fullName,
Commodities[impAccount.commodity],
parent,
impAccount.location,
impAccount.closed ? new Date(impAccount.closed) : undefined
);
Accounts[account.fullName] = account;
Expand All @@ -376,6 +392,7 @@ function loadAccounts() {
const transaction = new Transaction(
posted,
impTransaction.description,
impTransaction.location,
impTransaction.notes,
impTransaction.code
);
Expand Down
2 changes: 1 addition & 1 deletion cmd/coin2html/js/src/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ function viewChart(options?: {
negated?: boolean; // is this negatively denominated account (e.g. Income/Liability)
}) {
const containerSelector = MainView;
const account = State.SelectedAccount.getRootAccount();
const account = State.SelectedAccount;
const opts = { negated: false }; // defaults
Object.assign(opts, options);
// clear out the container
Expand Down
1 change: 1 addition & 0 deletions commodity.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func (c *Commodity) MarshalJSON() ([]byte, error) {
"id": c.Id,
"name": c.Name,
"decimals": c.Decimals,
"location": c.Location(),
}
if c.Code != "" {
value["code"] = c.Code
Expand Down
Loading

0 comments on commit aa3e639

Please sign in to comment.