Skip to content

Commit

Permalink
fix(wechat): support more tx types (#30)
Browse files Browse the repository at this point in the history
* fix(wechat): support more tx types

Signed-off-by: Triple-Z <[email protected]>

* update README

Signed-off-by: Triple-Z <[email protected]>

* chore(wechat): rename TxTypeCash2Cash_2 to TxTypeCash2CashLooseChange
  • Loading branch information
Triple-Z authored Aug 3, 2021
1 parent 5120b92 commit 4b4a4a3
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ bin/
alipay_records_*.csv
default_output.beancount
dist/

# test outputs
test/output
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ BIN_DIR := $(GOPATH)/bin
GOLANGCI_LINT := $(BIN_DIR)/golangci-lint

# All targets.
.PHONY: lint test build container push help
.PHONY: lint test build container push help clean

help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
Expand All @@ -62,3 +62,10 @@ build-local:
install: build ## Install the double-entry-generator binary
@install ./bin/double-entry-generator /usr/local/bin
@echo "Installed double-entry-generator at /usr/local/bin/double-entry-generator !"

clean: ## Clean all the temporary files
@rm -rf ./bin
@rm -rf ./test/output

test-wechat: ## Run tests for WeChat provider
@$(SHELL) ./test/wechat-test.sh
45 changes: 33 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,10 @@ defaultMinusAccount: Assets:FIXME
defaultPlusAccount: Expenses:FIXME
defaultCommissionAccount: Expenses:Commission:FIXME
defaultCurrency: CNY
title: Test WeChat bills config
title: 测试
wechat:
rules:
# type (additional condition)
- type: 收入 # 微信红包
method: /
item: /
Expand All @@ -206,27 +207,47 @@ wechat:
txType: 转入零钱
peer: /
item: /
targetAccount: Assets:Digital:WeChat:Cash
targetAccount: Assets:Digital:Wechat:Cash
- type: / # 零钱提现
txType: 零钱提现
targetAccount: Assets:Digital:WeChat:Cash
targetAccount: Assets:Digital:Wechat:Cash
commissionAccount: Expenses:Wechat:Commission
- type: / # 零钱充值
txType: 零钱充值
targetAccount: Assets:Digital:Wechat:Cash
- type: / # 零钱通转出-到工商银行(9876)
txType: 零钱通转出-到工商银行(9876)
targetAccount: Assets:Bank:CN:ICBC:Savings
- peer: 云膳过桥米线,餐厅
sep: ','
targetAccount: Expenses:Food:Meal
- peer: 房东
type: 支出
targetAccount: Expenses:Housing:Rent
- peer: 用户
type: 收入
targetAccount: Income:Service
- peer: 理财通
type: /
targetAccount: Assets:Trade:Tencent:LiCaiTong
- peer: 建设银行
txType: 信用卡还款
targetAccount: Liabilities:Bank:CN:CCB
- peer: 滴滴
targetAccount: Expenses:Transport:Taxi
- peer: 公交
targetAccount: Expenses:Transport:Bus
- peer: 地铁
targetAccount: Expenses:Transport:Metro
- peer: 中国铁路
targetAccount: Expenses:Transport:Train
- method: / # 一般为收入,存入零钱
methodAccount: Assets:Digital:Wechat:Cash
- method: 零钱 # 零钱/零钱通
methodAccount: Assets:Digital:Wechat:Cash
- method: 工商银行
methodAccount: Assets:Bank:CN:ICBC:Savings
- method: 中国银行
methodAccount: Assets:Bank:CN:BOC:Savings
```

`defaultMinusAccount`, `defaultPlusAccount` 和 `defaultCurrency` 是全局的必填默认值。其中 `defaultMinusAccount` 是默认金额减少的账户,`defaultPlusAccount` 是默认金额增加的账户。 `defaultCurrency` 是默认货币。
Expand Down
14 changes: 14 additions & 0 deletions example/wechat/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ wechat:
txType: 零钱提现
targetAccount: Assets:Digital:Wechat:Cash
commissionAccount: Expenses:Wechat:Commission
- type: / # 零钱充值
txType: 零钱充值
targetAccount: Assets:Digital:Wechat:Cash
- type: / # 零钱通转出-到工商银行(9876)
txType: 零钱通转出-到工商银行(9876)
targetAccount: Assets:Bank:CN:ICBC:Savings

- peer: 云膳过桥米线,餐厅
sep: ','
Expand All @@ -32,6 +38,14 @@ wechat:
type: 收入
targetAccount: Income:Service

- peer: 理财通
type: /
targetAccount: Assets:Trade:Tencent:LiCaiTong

- peer: 建设银行
txType: 信用卡还款
targetAccount: Liabilities:Bank:CN:CCB

- method: / # 一般为收入,存入零钱
methodAccount: Assets:Digital:Wechat:Cash
- method: 零钱 # 零钱/零钱通
Expand Down
66 changes: 66 additions & 0 deletions example/wechat/example-wechat-output.beancount
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,36 @@ option "operating_currency" "CNY"
1970-01-01 open Assets:Bank:CN:ICBC:Savings
1970-01-01 open Assets:Digital:Wechat:Cash
1970-01-01 open Assets:FIXME
1970-01-01 open Assets:Trade:Tencent:LiCaiTong
1970-01-01 open Expenses:FIXME
1970-01-01 open Expenses:Food:Meal
1970-01-01 open Expenses:Housing:Rent
1970-01-01 open Expenses:Wechat:Commission
1970-01-01 open Income:Service
1970-01-01 open Income:Wechat:RedPacket
1970-01-01 open Liabilities:Bank:CN:CCB

2017-10-20 * "建设银行信用卡还款" "/"
Liabilities:Bank:CN:CCB 548.58 CNY
Assets:Digital:Wechat:Cash -548.58 CNY
merchantId: "1000019741201710201961089024"
method: "零钱"
orderId: "4200000069201710299246843141"
payTime: "2017-10-20 18:36:44 +0800 CST"
source: "微信支付"
txType: "/"
type: "信用卡还款"

2019-04-16 * "工商银行(9876)" "/"
Assets:Digital:Wechat:Cash 1300.00 CNY
Assets:Bank:CN:ICBC:Savings -1300.00 CNY
merchantId: "/"
method: "工商银行(9876)"
orderId: "110190416100031243293946287587"
payTime: "2019-04-16 10:28:55 +0800 CST"
source: "微信支付"
txType: "/"
type: "零钱充值"

2019-09-24 * "同性好友" "/"
Assets:Digital:Wechat:Cash 0.35 CNY
Expand All @@ -21,6 +45,7 @@ option "operating_currency" "CNY"
payTime: "2019-09-24 10:10:11 +0800 CST"
source: "微信支付"
txType: "收入"
type: "微信红包"

2019-09-26 * "云膳过桥米线(传奇广场店)" "总共消费:28.16"
Expenses:Food:Meal 28.16 CNY
Expand All @@ -31,6 +56,40 @@ option "operating_currency" "CNY"
payTime: "2019-09-26 12:45:27 +0800 CST"
source: "微信支付"
txType: "支出"
type: "商户消费"

2020-02-14 * "理财通" "中欧医疗健康混合C(003096)"
Assets:Trade:Tencent:LiCaiTong 3000.00 CNY
Assets:Bank:CN:ICBC:Savings -3000.00 CNY
merchantId: "1800007030102002143310679508"
method: "工商银行"
orderId: "1800045030312002144223548046"
payTime: "2020-02-14 01:24:33 +0800 CST"
source: "微信支付"
txType: "/"
type: "购买理财通"

2020-02-14 * "理财通" "鹏华增值宝(000569)"
Assets:Trade:Tencent:LiCaiTong 10000.00 CNY
Assets:Bank:CN:ICBC:Savings -10000.00 CNY
merchantId: "1800007308102002143430655989"
method: "工商银行"
orderId: "1800007367312002141165262215"
payTime: "2020-02-14 01:32:14 +0800 CST"
source: "微信支付"
txType: "/"
type: "购买理财通"

2020-07-06 * "/" "/"
Assets:Bank:CN:ICBC:Savings 5505.00 CNY
Assets:Digital:Wechat:Cash -5505.00 CNY
merchantId: "/"
method: "零钱通"
orderId: "18000070282007060048243102923587"
payTime: "2020-07-06 14:54:38 +0800 CST"
source: "微信支付"
txType: "/"
type: "零钱通转出-到工商银行(9876)"

2020-11-27 * "用户A" "收款方备注:二维码收款"
Assets:Digital:Wechat:Cash 23.00 CNY
Expand All @@ -41,6 +100,7 @@ option "operating_currency" "CNY"
payTime: "2020-11-27 19:29:00 +0800 CST"
source: "微信支付"
txType: "收入"
type: "二维码收款"

2021-01-17 * "/" "/"
Assets:Digital:Wechat:Cash 2000.00 CNY
Expand All @@ -51,6 +111,7 @@ option "operating_currency" "CNY"
payTime: "2021-01-17 10:07:31 +0800 CST"
source: "微信支付"
txType: "/"
type: "转入零钱通-来自工商银行(9876)"

2021-01-17 * "某餐厅" "收款方备注:二维码收款"
Expenses:Food:Meal 12.00 CNY
Expand All @@ -61,6 +122,7 @@ option "operating_currency" "CNY"
payTime: "2021-01-17 18:03:35 +0800 CST"
source: "微信支付"
txType: "支出"
type: "扫二维码付款"

2021-01-22 * "房东" "转账备注:微信转账"
Expenses:Housing:Rent 500.00 CNY
Expand All @@ -71,6 +133,7 @@ option "operating_currency" "CNY"
payTime: "2021-01-22 12:34:56 +0800 CST"
source: "微信支付"
txType: "支出"
type: "转账"

2021-07-11 * "招商银行()" "/"
Assets:Bank:CN:BOC:Savings 1000.10 CNY
Expand All @@ -83,6 +146,7 @@ option "operating_currency" "CNY"
payTime: "2021-07-11 14:17:52 +0800 CST"
source: "微信支付"
txType: "/"
type: "零钱提现"

2021-07-14 * "招商银行()" "/"
Assets:Bank:CN:ICBC:Savings 10.00 CNY
Expand All @@ -95,6 +159,7 @@ option "operating_currency" "CNY"
payTime: "2021-07-14 22:18:10 +0800 CST"
source: "微信支付"
txType: "/"
type: "零钱提现"

2021-07-15 * "招商银行()" "/"
Assets:Bank:CN:ICBC:Savings 100.00 CNY
Expand All @@ -107,4 +172,5 @@ option "operating_currency" "CNY"
payTime: "2021-07-15 16:29:37 +0800 CST"
source: "微信支付"
txType: "/"
type: "零钱提现"

8 changes: 7 additions & 1 deletion example/wechat/example-wechat-records.csv
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@
2021-01-17 10:07:31,转入零钱通-来自工商银行(9876),/,/,/,¥2000.00,工商银行(9876),支付成功,3985734,129847129,/
2021-07-15 16:29:37,零钱提现,招商银行(),"/",/,¥100.10,工商银行,提现已到账,207210715100077148235523883175 ,/ ,"服务费¥0.10"
2021-07-14 22:18:10,零钱提现,招商银行(),"/",/,¥10.10,工商银行,提现已到账,207210714100077147459276708175 ,/ ,"服务费¥0.10"
2021-07-11 14:17:52,零钱提现,招商银行(),"/",/,¥1001.10,中国银行,提现已到账,207210711100077147832088993175 ,/ ,"服务费¥1.00"
2021-07-11 14:17:52,零钱提现,招商银行(),"/",/,¥1001.10,中国银行,提现已到账,207210711100077147832088993175 ,/ ,"服务费¥1.00"
2017-10-20 18:36:44,信用卡还款,建设银行信用卡还款,"/",/,¥548.58,零钱,支付成功,4200000069201710299246843141 ,1000019741201710201961089024 ,"/"
2020-02-14 01:32:14,购买理财通,理财通,"鹏华增值宝(000569)",/,¥10000.00,工商银行,支付成功,1800007367312002141165262215 ,1800007308102002143430655989 ,"/"
2020-02-14 01:24:33,购买理财通,理财通,"中欧医疗健康混合C(003096)",/,¥3000.00,工商银行,支付成功,1800045030312002144223548046 ,1800007030102002143310679508 ,"/"
2020-02-14 01:19:39,零钱通转出-到零钱,/,"/",/,¥2634.78,零钱通,支付成功,18000070012002140012244807617587 ,/ ,"/"
2020-07-06 14:54:38,零钱通转出-到工商银行(9876),/,"/",/,¥5505.00,零钱通,支付成功,18000070282007060048243102923587 ,/ ,"/"
2019-04-16 10:28:55,零钱充值,工商银行(9876),"/",/,¥1300.00,工商银行(9876),充值完成,110190416100031243293946287587 ,/ ,"/"
7 changes: 6 additions & 1 deletion pkg/provider/wechat/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func conevertType(t OrderType) ir.TxType {
func getMetadata(o Order) map[string]string {
// FIXME(TripleZ): hard-coded, bad pattern
source := "微信支付"
var status, method, category, txType, orderId, merchantId, paytime string
var status, method, category, txType, typeOriginal, orderId, merchantId, paytime string

paytime = o.PayTime.String()

Expand All @@ -59,6 +59,10 @@ func getMetadata(o Order) map[string]string {
txType = o.TypeOriginal
}

if o.TxTypeOriginal != "" {
typeOriginal = o.TxTypeOriginal
}

if o.Method != "" {
method = o.Method
}
Expand All @@ -73,6 +77,7 @@ func getMetadata(o Order) map[string]string {
"orderId": orderId,
"merchantId": merchantId,
"txType": txType,
"type": typeOriginal,
"category": category,
"method": method,
"status": status,
Expand Down
5 changes: 4 additions & 1 deletion pkg/provider/wechat/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wechat

import (
"fmt"
"log"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -31,7 +32,9 @@ func (w *Wechat) translateToOrders(array []string) error {
bill.TxType = getTxType(array[1])
switch bill.TxType {
case TxTypeCash2Cash:
fmt.Printf("Get an unusable tx type, ignore it: %s\n", bill.TxType)
fallthrough
case TxTypeCash2CashLooseChange:
log.Printf("Get an unusable tx type, ignore it: %s\n", bill.TxType)
return nil
case TxTypeUnknown:
return fmt.Errorf("Failed to get the tx type %s: %v", array[1], err)
Expand Down
27 changes: 16 additions & 11 deletions pkg/provider/wechat/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,20 @@ const (
type TxType string

const (
TxTypeConsume TxType = "商户消费"
TxTypeLucky = "微信红包"
TxTypeTransfer = "转账"
TxTypeQRIncome = "二维码收款"
TxTypeQRSend = "扫二维码付款"
TxTypeGroup = "群收款"
TxTypeRefund = "退款"
TxTypeCash2Cash = "转入零钱通-来自零钱"
TxTypeIntoCash = "转入零钱通"
TxTypeCashWithdraw = "零钱提现"
TxTypeUnknown = "Unknown"
TxTypeConsume TxType = "商户消费"
TxTypeLucky = "微信红包"
TxTypeTransfer = "转账"
TxTypeQRIncome = "二维码收款"
TxTypeQRSend = "扫二维码付款"
TxTypeGroup = "群收款"
TxTypeRefund = "退款"
TxTypeCash2Cash = "转入零钱通-来自零钱"
TxTypeIntoCash = "转入零钱通"
TxTypeCashIn = "零钱充值"
TxTypeCashWithdraw = "零钱提现"
TxTypeCreditCardRefund = "信用卡还款"
TxTypeBuyLiCaiTong = "购买理财通"
TxTypeCash2CashLooseChange = "零钱通转出-到零钱"
TxTypeCash2Others = "零钱通转出"
TxTypeUnknown = "Unknown"
)
10 changes: 10 additions & 0 deletions pkg/provider/wechat/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ func getTxType(tt string) TxType {
return TxTypeCash2Cash
} else if strings.Contains(tt, string(TxTypeIntoCash)) {
return TxTypeIntoCash
} else if strings.Contains(tt, string(TxTypeCashIn)) {
return TxTypeCashIn
} else if strings.Contains(tt, string(TxTypeCashWithdraw)) {
return TxTypeCashWithdraw
} else if strings.Contains(tt, string(TxTypeCreditCardRefund)) {
return TxTypeCreditCardRefund
} else if strings.Contains(tt, string(TxTypeBuyLiCaiTong)) {
return TxTypeBuyLiCaiTong
} else if strings.Contains(tt, string(TxTypeCash2CashLooseChange)) {
return TxTypeCash2CashLooseChange
} else if strings.Contains(tt, string(TxTypeCash2Others)) {
return TxTypeCash2Others
} else {
return TxTypeUnknown
}
Expand Down
Loading

0 comments on commit 4b4a4a3

Please sign in to comment.