From 91bf9b91b8ff0e141db11e978678c0de53953210 Mon Sep 17 00:00:00 2001 From: Zhenzhen Zhao Date: Sun, 6 Jun 2021 22:33:46 +0800 Subject: [PATCH] feat(alipay): add PnL account config (#17) Signed-off-by: Triple-Z --- example/alipay/config.yaml | 1 + example/alipay/example-alipay-output.beancount | 17 +++++++++-------- pkg/analyser/alipay/alipay.go | 11 +++++++++-- pkg/compiler/beancount/compiler.go | 1 + pkg/compiler/beancount/template.go | 3 ++- pkg/provider/alipay/config.go | 1 + 6 files changed, 23 insertions(+), 11 deletions(-) diff --git a/example/alipay/config.yaml b/example/alipay/config.yaml index fb7b63f..2d3bdd6 100644 --- a/example/alipay/config.yaml +++ b/example/alipay/config.yaml @@ -10,5 +10,6 @@ alipay: - item: 收益 plusAccount: Assets:Alipay minusAccount: Income:Earnings + pnlAccount: Income:Alipay:PnL - peer: 医院 plusAccount: Expenses:Medical diff --git a/example/alipay/example-alipay-output.beancount b/example/alipay/example-alipay-output.beancount index 699d296..1728bde 100644 --- a/example/alipay/example-alipay-output.beancount +++ b/example/alipay/example-alipay-output.beancount @@ -1,38 +1,39 @@ option "title" "测试" option "operating_currency" "CNY" -1970-01-01 open Expenses:Food -1970-01-01 open Income:Earnings 1970-01-01 open Assets:Alipay 1970-01-01 open Expenses:Medical 1970-01-01 open Expenses:Test 1970-01-01 open Liabilities:CreditCard:Test +1970-01-01 open Expenses:Food +1970-01-01 open Income:Earnings 2019-09-30 * "中欧基金管理有限公司" "余额宝-2019.09.29-收益发放" Assets:Alipay 0.01 CNY Income:Earnings -0.01 CNY + Income:Alipay:PnL 2019-09-30 * "肯德基(张江高科餐厅)" "张江高科餐厅" Expenses:Food 27.00 CNY Liabilities:CreditCard:Test -27.00 CNY - + 2019-09-30 * "麦当劳" "麦当劳" Expenses:Food 27.00 CNY Liabilities:CreditCard:Test -27.00 CNY - + 2020-11-01 * "肯德基会员官方旗舰店" "【狂欢价】电子券码 Y132 肯德基 潮汉堡双人餐兑换券" Expenses:Food 83.00 CNY Liabilities:CreditCard:Test -83.00 CNY - + 2020-11-01 * "肯德基会员官方旗舰店" "退款-【狂欢价】电子券码 Y132 肯德基 潮汉堡双人餐兑换券" Liabilities:CreditCard:Test 83.00 CNY Expenses:Food -83.00 CNY - + 2021-02-06 * "医院" "住院预交金" Expenses:Medical 3000.00 CNY Liabilities:CreditCard:Test -3000.00 CNY - + 2021-02-06 * "医院" "退款-住院预交金" Liabilities:CreditCard:Test 2725.42 CNY Expenses:Medical -2725.42 CNY - + diff --git a/pkg/analyser/alipay/alipay.go b/pkg/analyser/alipay/alipay.go index a5d01f2..f870d86 100644 --- a/pkg/analyser/alipay/alipay.go +++ b/pkg/analyser/alipay/alipay.go @@ -59,17 +59,24 @@ func (a Alipay) GetAccounts(o *ir.Order, cfg *config.Config, target, provider st if match { resMinus := cfg.DefaultMinusAccount resPlus := cfg.DefaultPlusAccount + var extraAccounts map[ir.Account]string + if r.MinusAccount != nil { resMinus = *r.MinusAccount } if r.PlusAccount != nil { resPlus = *r.PlusAccount } + if r.PnlAccount != nil { + extraAccounts = map[ir.Account]string{ + ir.PnlAccount: *r.PnlAccount, + } + } if strings.HasPrefix(o.Item, "退款-") { - return resPlus, resMinus, nil + return resPlus, resMinus, extraAccounts } - return resMinus, resPlus, nil + return resMinus, resPlus, extraAccounts } } diff --git a/pkg/compiler/beancount/compiler.go b/pkg/compiler/beancount/compiler.go index 7aa210e..31a0eef 100644 --- a/pkg/compiler/beancount/compiler.go +++ b/pkg/compiler/beancount/compiler.go @@ -162,6 +162,7 @@ func (b *BeanCount) writeBill(file *os.File, index int) error { Money: o.Money, PlusAccount: o.PlusAccount, MinusAccount: o.MinusAccount, + PnlAccount: o.ExtraAccounts[ir.PnlAccount], Currency: b.Config.DefaultCurrency, }) case ir.OrderTypeHuobiTrade: // Huobi trades diff --git a/pkg/compiler/beancount/template.go b/pkg/compiler/beancount/template.go index 465761a..679e8d3 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 }}" {{ .PlusAccount }} {{ .Money | printf "%.2f" }} {{ .Currency }} {{ .MinusAccount }} -{{ .Money | printf "%.2f" }} {{ .Currency }} - + {{ if .PnlAccount }}{{ .PnlAccount }}{{ printf "\n" }}{{ end }} ` type NormalOrderVars struct { @@ -19,6 +19,7 @@ type NormalOrderVars struct { Money float64 PlusAccount string MinusAccount string + PnlAccount string Currency string } diff --git a/pkg/provider/alipay/config.go b/pkg/provider/alipay/config.go index a86df87..912c2da 100644 --- a/pkg/provider/alipay/config.go +++ b/pkg/provider/alipay/config.go @@ -30,4 +30,5 @@ type Rule struct { EndTime *string `mapstructure:"endTime,omitempty"` MinusAccount *string `mapstructure:"minusAccount,omitempty"` PlusAccount *string `mapstructure:"plusAccount,omitempty"` + PnlAccount *string `mapstructure:"pnlAccount,omitempty"` }