From 1952100bbb24384dd7906d02638621c2329d407f Mon Sep 17 00:00:00 2001 From: Ramsay Date: Thu, 1 Jun 2023 20:49:15 -0700 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/provider/icbc/config.go | 2 +- pkg/provider/icbc/icbc.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/provider/icbc/config.go b/pkg/provider/icbc/config.go index 85decea..63d874b 100644 --- a/pkg/provider/icbc/config.go +++ b/pkg/provider/icbc/config.go @@ -1,6 +1,6 @@ package icbc -// Config is the configuration for Alipay. +// Config is the configuration for ICBC. type Config struct { Rules []Rule `mapstructure:"rules,omitempty"` } diff --git a/pkg/provider/icbc/icbc.go b/pkg/provider/icbc/icbc.go index 31d537f..f13bf37 100644 --- a/pkg/provider/icbc/icbc.go +++ b/pkg/provider/icbc/icbc.go @@ -31,7 +31,7 @@ func New() *Icbc { } } -// Translate translates the alipay bill records to IR. +// Translate translates the icbc bill records to IR. func (icbc *Icbc) Translate(filename string) (*ir.IR, error) { log.SetPrefix("[Provider-ICBC] ") From ddf171a010c3c483fae805899d1177f47c8a2b52 Mon Sep 17 00:00:00 2001 From: Ramsay Date: Sun, 11 Jun 2023 11:42:27 -0700 Subject: [PATCH 2/4] Add end to end test for td provider --- Makefile | 7 ++++++- test/td-test-beancount.sh | 30 ++++++++++++++++++++++++++++++ test/td-test-ledger.sh | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 test/td-test-beancount.sh create mode 100644 test/td-test-ledger.sh diff --git a/Makefile b/Makefile index 624fa49..1a8226c 100644 --- a/Makefile +++ b/Makefile @@ -90,7 +90,7 @@ clean: ## Clean all the temporary files @rm -rf ./double-entry-generator @rm -rf ./wasm-dist -test: test-go test-alipay-beancount test-alipay-ledger test-wechat-beancount test-wechat-ledger test-huobi-beancount test-huobi-ledger test-htsec-beancount test-htsec-ledger test-icbc-beancount test-icbc-ledger ## Run all tests +test: test-go test-alipay-beancount test-alipay-ledger test-wechat-beancount test-wechat-ledger test-huobi-beancount test-huobi-ledger test-htsec-beancount test-htsec-ledger test-icbc-beancount test-icbc-ledger test-td-beancount test-td-ledger ## Run all tests test-go: ## Run Golang tests @go test ./... @@ -122,6 +122,11 @@ test-icbc-beancount: ## Run tests for ICBC provider against beancount compiler test-icbc-ledger: ## Run tests for ICBC provider against ledger compiler @$(SHELL) ./test/icbc-test-ledger.sh +test-td-beancount: ## Run tests for TD provider against beancount compiler + @$(SHELL) ./test/td-test-beancount.sh +test-td-ledger: ## Run tests for TD provider against ledger compiler + @$(SHELL) ./test/td-test-ledger.sh + format: ## Format code @gofmt -s -w pkg @goimports -w pkg diff --git a/test/td-test-beancount.sh b/test/td-test-beancount.sh new file mode 100644 index 0000000..d3412a2 --- /dev/null +++ b/test/td-test-beancount.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# +# E2E test for td provider. + +# set -x # debug +set -eo errexit + +TEST_DIR=`dirname "$(realpath $0)"` +ROOT_DIR="$TEST_DIR/.." + +make -f "$ROOT_DIR/Makefile" build +mkdir -p "$ROOT_DIR/test/output" + +# generate td bills output in beancount format +"$ROOT_DIR/bin/double-entry-generator" translate \ + --provider td \ + --config "$ROOT_DIR/example/td/config.yaml" \ + --output "$ROOT_DIR/test/output/test-td-output.beancount" \ + "$ROOT_DIR/example/td/example-td-records.csv" + +diff -u --color \ + "$ROOT_DIR/example/td/example-td-output.beancount" \ + "$ROOT_DIR/test/output/test-td-output.beancount" + +if [ $? -ne 0 ]; then + echo "[FAIL] td provider output is different from expected output." + exit 1 +fi + +echo "[PASS] All td provider tests!" diff --git a/test/td-test-ledger.sh b/test/td-test-ledger.sh new file mode 100644 index 0000000..14bb0ac --- /dev/null +++ b/test/td-test-ledger.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +# +# E2E test for td provider. + +# set -x # debug +set -eo errexit + +TEST_DIR=$(dirname "$(realpath $0)") +ROOT_DIR="$TEST_DIR/.." +OUTPUT="$ROOT_DIR/test/output/test-td-output.ledger" + +make -f "$ROOT_DIR/Makefile" build +mkdir -p "$ROOT_DIR/test/output" + +# generate td bills output in ledger format +"$ROOT_DIR/bin/double-entry-generator" translate \ + --provider td \ + --target ledger \ + --config "$ROOT_DIR/example/td/config.yaml" \ + --output "$OUTPUT" \ + "$ROOT_DIR/example/td/example-td-records.csv" + +diff -u --color \ + "$ROOT_DIR/example/td/example-td-output.ledger" \ + "$OUTPUT" + +if [ $? -ne 0 ]; then + echo "[FAIL] td provider output is different from expected output." + exit 1 +fi + +echo "[PASS] All td provider for ledger target tests!" From 3554f4d48c6068683c6a6498133cfc8eaa02412c Mon Sep 17 00:00:00 2001 From: Ramsay Date: Sun, 11 Jun 2023 11:43:00 -0700 Subject: [PATCH 3/4] Add tag support for ledger backend --- example/td/config.yaml | 2 + example/td/example-td-output.beancount | 86 +++++++++++---------- example/td/example-td-output.ledger | 5 +- example/wechat/example-wechat-output.ledger | 1 + 4 files changed, 51 insertions(+), 43 deletions(-) diff --git a/example/td/config.yaml b/example/td/config.yaml index 74ec7e4..ee2d6ac 100644 --- a/example/td/config.yaml +++ b/example/td/config.yaml @@ -7,8 +7,10 @@ td: rules: - item: "T T" targetAccount: Expenses:Grocery + tag: tt_tag - item: "DOLLARAMA" targetAccount: Expenses:Grocery + tag: grocery_tag1,cheap_tag2 - item: "DEVELOPM MSP" targetAccount: Income:Salary - type: 收入 diff --git a/example/td/example-td-output.beancount b/example/td/example-td-output.beancount index 2fc4af0..db34a9f 100644 --- a/example/td/example-td-output.beancount +++ b/example/td/example-td-output.beancount @@ -1,43 +1,45 @@ -1970-01-01 * Open Balance - Assets:FIXME 0 CAD - Expenses:FIXME 0 CAD - Expenses:Grocery 0 CAD - Income:FIXME 0 CAD - Income:Salary 0 CAD - Equity:Opening Balances -2023-05-01 * TD - DOLLARAMA # 845 _M - Expenses:Grocery 1.96 CAD - Assets:DebitCard:TDChequing - 1.96 CAD - -2023-05-01 * TD - T T SUPERMARKET _M - Expenses:Grocery 12.10 CAD - Assets:DebitCard:TDChequing - 12.10 CAD - -2023-05-01 * TD - PTS TO: 02816700587 - Expenses:FIXME 10.00 CAD - Assets:DebitCard:TDChequing - 10.00 CAD - -2023-05-01 * TD - SFCA MSP - Assets:DebitCard:TDChequing 0.01 CAD - Assets:FIXME - 0.01 CAD - -2023-05-01 * TD - ICBC AGENT #942 - Expenses:FIXME 35.00 CAD - Assets:DebitCard:TDChequing - 35.00 CAD - -2023-05-01 * TD - DOLLARAMA # 845 _M - Expenses:Grocery 15.68 CAD - Assets:DebitCard:TDChequing - 15.68 CAD - -2023-05-01 * TD - T T SUPERMARKET _M - Expenses:Grocery 5.30 CAD - Assets:DebitCard:TDChequing - 5.30 CAD - -2023-05-02 * TD - SEND E-TFR ***Y4h - Expenses:FIXME 239.13 CAD - Assets:DebitCard:TDChequing - 239.13 CAD - -2023-05-03 * TD - DEVELOPM MSP - Assets:DebitCard:TDChequing 332.22 CAD - Income:Salary - 332.22 CAD +option "title" "测试" +option "operating_currency" "CAD" + +1970-01-01 open Assets:FIXME +1970-01-01 open Expenses:FIXME +1970-01-01 open Expenses:Grocery +1970-01-01 open Income:FIXME +1970-01-01 open Income:Salary + +2023-05-01 * "TD" "DOLLARAMA # 845 _M" #grocery_tag1 #cheap_tag2 + Expenses:Grocery 1.96 CAD + Assets:DebitCard:TDChequing -1.96 CAD + +2023-05-01 * "TD" "T T SUPERMARKET _M" #tt_tag + Expenses:Grocery 12.10 CAD + Assets:DebitCard:TDChequing -12.10 CAD + +2023-05-01 * "TD" "PTS TO: 02816700587" + Expenses:FIXME 10.00 CAD + Assets:DebitCard:TDChequing -10.00 CAD + +2023-05-01 * "TD" "SFCA MSP" + Assets:DebitCard:TDChequing 0.01 CAD + Assets:FIXME -0.01 CAD + +2023-05-01 * "TD" "ICBC AGENT #942" + Expenses:FIXME 35.00 CAD + Assets:DebitCard:TDChequing -35.00 CAD + +2023-05-01 * "TD" "DOLLARAMA # 845 _M" #grocery_tag1 #cheap_tag2 + Expenses:Grocery 15.68 CAD + Assets:DebitCard:TDChequing -15.68 CAD + +2023-05-01 * "TD" "T T SUPERMARKET _M" #tt_tag + Expenses:Grocery 5.30 CAD + Assets:DebitCard:TDChequing -5.30 CAD + +2023-05-02 * "TD" "SEND E-TFR ***Y4h" + Expenses:FIXME 239.13 CAD + Assets:DebitCard:TDChequing -239.13 CAD + +2023-05-03 * "TD" "DEVELOPM MSP" + Assets:DebitCard:TDChequing 332.22 CAD + Income:Salary -332.22 CAD diff --git a/example/td/example-td-output.ledger b/example/td/example-td-output.ledger index 15b2902..470338a 100644 --- a/example/td/example-td-output.ledger +++ b/example/td/example-td-output.ledger @@ -5,12 +5,13 @@ Income:FIXME 0 CAD Income:Salary 0 CAD Equity:Opening Balances - 2023-05-01 * TD - DOLLARAMA # 845 _M + ; :grocery_tag1:cheap_tag2: Expenses:Grocery 1.96 CAD Assets:DebitCard:TDChequing - 1.96 CAD 2023-05-01 * TD - T T SUPERMARKET _M + ; :tt_tag: Expenses:Grocery 12.10 CAD Assets:DebitCard:TDChequing - 12.10 CAD @@ -27,10 +28,12 @@ Assets:DebitCard:TDChequing - 35.00 CAD 2023-05-01 * TD - DOLLARAMA # 845 _M + ; :grocery_tag1:cheap_tag2: Expenses:Grocery 15.68 CAD Assets:DebitCard:TDChequing - 15.68 CAD 2023-05-01 * TD - T T SUPERMARKET _M + ; :tt_tag: Expenses:Grocery 5.30 CAD Assets:DebitCard:TDChequing - 5.30 CAD diff --git a/example/wechat/example-wechat-output.ledger b/example/wechat/example-wechat-output.ledger index 959cfc2..86394c0 100644 --- a/example/wechat/example-wechat-output.ledger +++ b/example/wechat/example-wechat-output.ledger @@ -101,6 +101,7 @@ Assets:Digital:Wechat:Cash - 5505.00 CNY 2020-11-27 * 用户A - 收款方备注:二维码收款 + ; :income:service: ; merchantId: "/" ; method: "零钱" ; orderId: "3985734" From 758f638042c4ae5ec616eed3019a10b45aac593c Mon Sep 17 00:00:00 2001 From: Ramsay Date: Sun, 11 Jun 2023 11:43:21 -0700 Subject: [PATCH 4/4] Ledger add meta tag support --- pkg/compiler/ledger/compiler.go | 1 + pkg/compiler/ledger/template.go | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/pkg/compiler/ledger/compiler.go b/pkg/compiler/ledger/compiler.go index 8481ebf..e50e5bc 100644 --- a/pkg/compiler/ledger/compiler.go +++ b/pkg/compiler/ledger/compiler.go @@ -195,6 +195,7 @@ func (ledger *Ledger) writeBill(file io.Writer, index int) error { CommissionAccount: order.ExtraAccounts[ir.CommissionAccount], Metadata: order.Metadata, Currency: ledger.Config.DefaultCurrency, + Tags: order.Tags, }) case ir.OrderTypeHuobiTrade: // Huobi trades switch order.Type { diff --git a/pkg/compiler/ledger/template.go b/pkg/compiler/ledger/template.go index 845c585..d410a6a 100644 --- a/pkg/compiler/ledger/template.go +++ b/pkg/compiler/ledger/template.go @@ -16,8 +16,12 @@ import ( */ // 普通账单的模版(消费账) +// ledger的 tag语法定义: https://ledger-cli.org/doc/ledger3.html#Metadata-tags +// 单个tag定义: `; :TAG:` +// 多个tag定义: `; :TAG1:TAG2:TAG3:` var normalOrder = `{{ .PayTime.Format "2006-01-02" }} * {{ EscapeString .Peer }} {{- if .Item }} - {{ EscapeString .Item }} {{ end }} {{- if .Note}}; {{ .Note }}{{ end }} + {{- if .Tags}}{{printf "\n"}} ; :{{- range $index, $tag := .Tags}}{{ if $index }}:{{ end }}{{ $tag }}{{ end }}:{{ end }} {{- range $key, $value := .Metadata }}{{ if $value }}{{ printf "\n" }} ; {{ $key }}: "{{ $value }}"{{end}}{{end}} {{ .PlusAccount }} {{ .Money | printf "%.2f" }} {{ .Currency }} {{ .MinusAccount }} - {{ .Money | printf "%.2f" }} {{ .Currency }} @@ -40,6 +44,7 @@ type NormalOrderVars struct { CommissionAccount string // 佣金账户 Metadata map[string]string // 元数据 Currency string // 货币 + Tags []string // 标签 } // 火币买入模版(手续费单位为购买单位货币)