Skip to content

Commit

Permalink
feat(alipay): add PnL account config (#17)
Browse files Browse the repository at this point in the history
Signed-off-by: Triple-Z <me@triplez.cn>
  • Loading branch information
Triple-Z authored Jun 6, 2021
1 parent e204a82 commit 91bf9b9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions example/alipay/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ alipay:
- item: 收益
plusAccount: Assets:Alipay
minusAccount: Income:Earnings
pnlAccount: Income:Alipay:PnL
- peer: 医院
plusAccount: Expenses:Medical
17 changes: 9 additions & 8 deletions example/alipay/example-alipay-output.beancount
Original file line number Diff line number Diff line change
@@ -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

11 changes: 9 additions & 2 deletions pkg/analyser/alipay/alipay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/compiler/beancount/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/compiler/beancount/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -19,6 +19,7 @@ type NormalOrderVars struct {
Money float64
PlusAccount string
MinusAccount string
PnlAccount string
Currency string
}

Expand Down
1 change: 1 addition & 0 deletions pkg/provider/alipay/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 91bf9b9

Please sign in to comment.