From 608ad73453194a010206daf4bb7e8d4eaab5ad21 Mon Sep 17 00:00:00 2001 From: oxalica Date: Fri, 25 Jun 2021 09:41:09 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20alipay=20example=20of=20ShouKuanMa=20(?= =?UTF-8?q?=E6=94=B6=E6=AC=BE=E7=A0=81)=20and=20normalize=20outputs=20(#24?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/alipay/config.yaml | 10 ++++- .../alipay/example-alipay-output.beancount | 40 ++++++++++++------- example/alipay/example-alipay-records.csv | 2 + example/huobi/example-huobi-output.beancount | 26 ++++++------ .../wechat/example-wechat-output.beancount | 12 +++--- pkg/compiler/beancount/compiler.go | 9 ++++- pkg/compiler/beancount/template.go | 2 +- 7 files changed, 63 insertions(+), 38 deletions(-) diff --git a/example/alipay/config.yaml b/example/alipay/config.yaml index 5800410..9158cf2 100644 --- a/example/alipay/config.yaml +++ b/example/alipay/config.yaml @@ -19,6 +19,14 @@ alipay: - method: 招商银行(9876) methodAccount: Assets:Bank:CN:CMB-9876:Savings + - type: 收入 # 其他转账收款 + targetAccount: Income:FIXME + methodAccount: Assets:Alipay + - type: 收入 # 收款码收款 + item: 商品 + targetAccount: Income:Alipay:ShouKuanMa + methodAccount: Assets:Alipay + # 交易类型为其他 - type: 其他 item: 收益发放 @@ -38,4 +46,4 @@ alipay: - type: 其他 item: 余额宝-单次转入 targetAccount: Assets:Alipay - methodAccount: Assets:Alipay \ No newline at end of file + methodAccount: Assets:Alipay diff --git a/example/alipay/example-alipay-output.beancount b/example/alipay/example-alipay-output.beancount index 1c49f19..3b98441 100644 --- a/example/alipay/example-alipay-output.beancount +++ b/example/alipay/example-alipay-output.beancount @@ -1,17 +1,19 @@ option "title" "测试" option "operating_currency" "CNY" -1970-01-01 open Expenses:FIXME -1970-01-01 open Assets:FIXME 1970-01-01 open Assets:Alipay -1970-01-01 open Assets:Bank:CN:CMB-9876:Savings 1970-01-01 open Assets:Alipay:Invest -1970-01-01 open Expenses:Insurance -1970-01-01 open Income:Alipay:YuEBao:PnL -1970-01-01 open Income:Alipay:Invest:PnL +1970-01-01 open Assets:Bank:CN:CMB-9876:Savings +1970-01-01 open Assets:FIXME +1970-01-01 open Expenses:Electronics +1970-01-01 open Expenses:FIXME 1970-01-01 open Expenses:Food 1970-01-01 open Expenses:Groceries -1970-01-01 open Expenses:Electronics +1970-01-01 open Expenses:Insurance +1970-01-01 open Income:Alipay:Invest:PnL +1970-01-01 open Income:Alipay:ShouKuanMa +1970-01-01 open Income:Alipay:YuEBao:PnL +1970-01-01 open Income:FIXME 2020-06-02 * "蚂蚁财富-蚂蚁(杭州)基金销售有限公司" "蚂蚁财富-ABCD-卖出至余额宝" ; 其他-交易成功-投资理财 Assets:Alipay 15.40 CNY @@ -21,32 +23,40 @@ option "operating_currency" "CNY" 2020-06-09 * "广发基金管理有限公司" "余额宝-2020.06.08-收益发放" ; 其他-交易成功-投资理财 Assets:Alipay 0.11 CNY Income:Alipay:YuEBao:PnL -0.11 CNY - + 2020-06-14 * "东莞肯德基有限公司" "KFCAPIDGB012345" ; 支出-交易成功-餐饮美食 Expenses:Food 79.00 CNY Assets:Alipay -79.00 CNY - + 2021-05-19 * "蚂蚁财富-蚂蚁(杭州)基金销售有限公司" "蚂蚁财富-XZY指数C-买入" ; 其他-交易成功-投资理财 Assets:Alipay:Invest 50.00 CNY Assets:Alipay -50.00 CNY - + 2021-05-20 * "苏宁易购官方旗舰店" "【礼遇价】三星128g内存卡microSD存储卡tf卡行车记录仪卡switch监控摄像头" ; 支出-交易成功-数码电器 Expenses:Electronics 87.90 CNY Assets:Bank:CN:CMB-9876:Savings -87.90 CNY - + 2021-05-30 * "广发基金管理有限公司" "余额宝-单次转入" ; 其他-交易成功-投资理财 Assets:Alipay 2.00 CNY Assets:Alipay -2.00 CNY - + 2021-06-01 * "dettol滴露官方旗舰店" "【直接下单付款】滴露消毒液1.2L*2家用杀菌消毒水衣物地板宠物" ; 支出-交易关闭-日用百货 Expenses:Groceries 109.90 CNY Assets:Bank:CN:CMB-9876:Savings -109.90 CNY - + 2021-06-01 * "dettol滴露官方旗舰店" "退款-【直接下单付款】滴露消毒液1.2L*2家用杀菌消毒水衣物地板宠物" ; 其他-退款成功-退款 Assets:Bank:CN:CMB-9876:Savings 109.90 CNY Expenses:Groceries -109.90 CNY - + 2021-06-14 * "蚂蚁会员(北京)网络技术服务有限公司" "相互宝分摊-6月第1期-*" ; 支出-交易成功-互助保障 Expenses:Insurance 6.51 CNY Assets:Bank:CN:CMB-9876:Savings -6.51 CNY - + +2021-06-24 * "*三" "商品" ; 收入-交易成功-收入 + Assets:Alipay 10.00 CNY + Income:Alipay:ShouKuanMa -10.00 CNY + +2021-06-25 * "*三" "我就随便转着玩" ; 收入-交易成功-转账红包 + Assets:Alipay 42.00 CNY + Income:FIXME -42.00 CNY + diff --git a/example/alipay/example-alipay-records.csv b/example/alipay/example-alipay-records.csv index 0204c4c..32072e4 100644 --- a/example/alipay/example-alipay-records.csv +++ b/example/alipay/example-alipay-records.csv @@ -9,6 +9,8 @@ ,ϲƸ-ϣݣ޹˾ , ,ϲƸ-XZYָC-, ,50.00 ,׳ɹ ,Ͷ ,123456 , ,2021-05-19 09:42:54 , ,ϲƸ-ϣݣ޹˾ , ,ϲƸ-ABCD-, ,15.40 ,׳ɹ ,Ͷ ,123456 , ,2020-06-02 15:34:06 , ֧ ,ݸϵ»޹˾ ,dgb***@yum.com ,KFCAPIDGB012345, ,79.00 ,׳ɹ ,ʳ ,123456 ,ABC9876 ,2020-06-14 12:43:26 , + ,* ,111******11 ,Ʒ , ,10.00,׳ɹ , ,123456 ,123456 ,2021-06-24 00:00:00 , + ,* ,111******11 ,Ҿת , ,42.00 ,׳ɹ ,ת˺ ,123456 , ,2021-06-25 00:00:00 , ------------------------------------------------------------------------------------ Ϣ TripleZ diff --git a/example/huobi/example-huobi-output.beancount b/example/huobi/example-huobi-output.beancount index 98f6310..aee4a60 100644 --- a/example/huobi/example-huobi-output.beancount +++ b/example/huobi/example-huobi-output.beancount @@ -1,26 +1,26 @@ option "title" "测试" option "operating_currency" "CNY" -1970-01-01 open Income:Huobi:PnL -1970-01-01 open Assets:Rule1:Cash -1970-01-01 open Assets:Rule1:Positions -1970-01-01 open Income:Rule1:PnL -1970-01-01 open Expenses:Rule1:Commission 1970-01-01 open Assets:Huobi:Cash 1970-01-01 open Assets:Huobi:Positions +1970-01-01 open Assets:Rule1:Cash +1970-01-01 open Assets:Rule1:Positions 1970-01-01 open Expenses:Huobi:Commission +1970-01-01 open Expenses:Rule1:Commission +1970-01-01 open Income:Huobi:PnL +1970-01-01 open Income:Rule1:PnL 2021-02-23 * "Huobi-币币交易" "买入-BTC/USDT" - Assets:Huobi:Cash -13.98369600 USDT - Assets:Huobi:Positions 0.00030400 BTC {45999.00000000 USDT} @@ 13.98369600 USDT - Assets:Huobi:Cash -0.00000060 BTC @ 45999.00000000 USDT - Expenses:Huobi:Commission 0.00000060 BTC @ 45999.00000000 USDT + Assets:Rule1:Cash -13.98369600 USDT + Assets:Rule1:Positions 0.00030400 BTC {45999.00000000 USDT} @@ 13.98369600 USDT + Assets:Rule1:Cash -0.00000060 BTC @ 45999.00000000 USDT + Expenses:Rule1:Commission 0.00000060 BTC @ 45999.00000000 USDT 2021-02-23 * "Huobi-币币交易" "买入-BTC1S/USDT" - Assets:Huobi:Cash -5.60652159 USDT - Assets:Huobi:Positions 4.57600000 BTC1S {1.22520000 USDT} @@ 5.60652159 USDT - Assets:Huobi:Cash -0.00915201 BTC1S @ 1.22520000 USDT - Expenses:Huobi:Commission 0.00915201 BTC1S @ 1.22520000 USDT + Assets:Rule1:Cash -5.60652159 USDT + Assets:Rule1:Positions 4.57600000 BTC1S {1.22520000 USDT} @@ 5.60652159 USDT + Assets:Rule1:Cash -0.00915201 BTC1S @ 1.22520000 USDT + Expenses:Rule1:Commission 0.00915201 BTC1S @ 1.22520000 USDT 2021-02-24 * "Huobi-币币交易" "卖出-BTC/USDT" Assets:Huobi:Positions -0.00010800 BTC {} @ 49850.00000000 USDT diff --git a/example/wechat/example-wechat-output.beancount b/example/wechat/example-wechat-output.beancount index 8b836ab..075e669 100644 --- a/example/wechat/example-wechat-output.beancount +++ b/example/wechat/example-wechat-output.beancount @@ -1,16 +1,16 @@ option "title" "测试" option "operating_currency" "CNY" +1970-01-01 open Assets:Bank:CN:BOC:Savings +1970-01-01 open Assets:Bank:CN:ICBC:Savings 1970-01-01 open Assets:Digital:WeChat:Cash -1970-01-01 open Income:Service -1970-01-01 open Expenses:FIXME +1970-01-01 open Assets:Digital:Wechat:Cash 1970-01-01 open Assets:FIXME -1970-01-01 open Assets:Bank:CN:BOC:Savings -1970-01-01 open Income:Wechat:RedPacket +1970-01-01 open Expenses:FIXME 1970-01-01 open Expenses:Food:Meal 1970-01-01 open Expenses:Housing:Rent -1970-01-01 open Assets:Digital:Wechat:Cash -1970-01-01 open Assets:Bank:CN:ICBC:Savings +1970-01-01 open Income:Service +1970-01-01 open Income:Wechat:RedPacket 2019-09-24 * "同性好友" "/" Assets:Digital:Wechat:Cash 0.35 CNY diff --git a/pkg/compiler/beancount/compiler.go b/pkg/compiler/beancount/compiler.go index 6dc424f..dfe3ab2 100644 --- a/pkg/compiler/beancount/compiler.go +++ b/pkg/compiler/beancount/compiler.go @@ -111,10 +111,15 @@ func (b *BeanCount) writeHeader(file *os.File) error { } accounts := b.GetAllCandidateAccounts(b.Config) + var sortedAccounts []string for k := range accounts { - if k == "" { - continue + if k != "" { + sortedAccounts = append(sortedAccounts, k) } + } + sort.Strings(sortedAccounts) + + for _, k := range sortedAccounts { _, err = io.WriteString(file, "1970-01-01 open "+k+"\n") if err != nil { return fmt.Errorf("write open account error: %v", err) diff --git a/pkg/compiler/beancount/template.go b/pkg/compiler/beancount/template.go index 452fa1c..346fcc0 100644 --- a/pkg/compiler/beancount/template.go +++ b/pkg/compiler/beancount/template.go @@ -9,7 +9,7 @@ import ( var normalOrder = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}" "{{ .Item }}"{{ if .Note }} ; {{ .Note }}{{ end }} {{ .PlusAccount }} {{ .Money | printf "%.2f" }} {{ .Currency }} {{ .MinusAccount }} -{{ .Money | printf "%.2f" }} {{ .Currency }} - {{ if .PnlAccount }}{{ .PnlAccount }}{{ printf "\n" }}{{ end }} +{{ if .PnlAccount }} {{ .PnlAccount }}{{ printf "\n" }}{{ end }} ` type NormalOrderVars struct {