Skip to content

Commit

Permalink
[feat] support category in alipay config (#39)
Browse files Browse the repository at this point in the history
resolves #37
  • Loading branch information
ppodxiang authored Dec 14, 2021
1 parent 48d1f5d commit cf071c9
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,13 @@ clean: ## Clean all the temporary files
@rm -rf ./bin
@rm -rf ./test/output

test: test-alipay test-wechat ## Run all tests

test-alipay: ## Run tests for Alipay provider
@$(SHELL) ./test/alipay-test.sh

test-wechat: ## Run tests for WeChat provider
@$(SHELL) ./test/wechat-test.sh

format: ## Format code
@gofmt -s -w pkg
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ alipay:
- item(商品说明)的包含匹配。
- type(收/支)的包含匹配。
- method(收/付款方式)的包含匹配。
- category(交易分类)的包含匹配。

在单条规则中可以使用分隔符(sep)填写多个关键字,在同一对象中,每个关键字之间是或的关系。

匹配成功则使用规则中定义的 `targetAccount` 、 `methodAccount` 等账户覆盖默认定义账户。

Expand Down Expand Up @@ -259,6 +262,8 @@ wechat:
- txType(交易类型)的包含匹配。
- method(支付方式)的包含匹配。

在单条规则中可以使用分隔符(sep)填写多个关键字,在同一对象中,每个关键字之间是或的关系。

匹配成功则使用规则中定义的 `targetAccount` 、 `methodAccount` 等账户覆盖默认定义账户。

微信账单提供了“交易方式”字段来标识资金出入账户。这样就可以直接通过“交易方式”,并辅以“收/支”字段确认该账户为增加账户还是减少账户。而复式记账法每笔交易至少需要两个账户,另一个账户则可通过“交易对方”(peer)、“商品”(item)、“收/支”(type)以及“交易方式”(method)的多种包含匹配得出。如支付宝配置类似,匹配成功则使用规则中定义的 `targetAccount` 和 `methodAccount` ,并通过确认该笔交易是收入还是支出,决定 `targetAccount` 和 `methodAccount` 的正负关系,来覆盖默认定义的增减账户。
Expand Down
5 changes: 5 additions & 0 deletions example/alipay/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ defaultCurrency: CNY
title: 测试
alipay:
rules:
- category: 日用百货
targetAccount: Expenses:Groceries
- category: 餐饮美食
targetAccount: Expenses:Food

- peer: 肯德基|麦当劳
sep: '|'
targetAccount: Expenses:Food
Expand Down
24 changes: 24 additions & 0 deletions example/alipay/example-alipay-output.beancount
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,27 @@ option "operating_currency" "CNY"
status: "交易成功"
txType: "收入"

2021-12-09 * "好邻居连锁便利店" "好邻居订单"
Expenses:Groceries 7.50 CNY
Assets:Bank:CN:CMB-9876:Savings -7.50 CNY
category: "日用百货"
merchantId: "10000891202112098830044"
method: "招商银行(9876)"
orderId: "2021120922001468815735046417"
payTime: "2021-12-09 09:25:42 +0800 CST"
source: "支付宝"
status: "交易成功"
txType: "支出"

2021-12-11 * "北京稻香村食品有限责任公司" "北京稻香村食品有限责任公司"
Expenses:Food 31.00 CNY
Assets:Bank:CN:CMB-9876:Savings -31.00 CNY
category: "餐饮美食"
merchantId: "322017092208872157020004030330000032112115225418"
method: "招商银行(9876)"
orderId: "2021121122001468815736847417"
payTime: "2021-12-11 16:53:51 +0800 CST"
source: "支付宝"
status: "交易成功"
txType: "支出"

2 changes: 2 additions & 0 deletions example/alipay/example-alipay-records.csv
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
֧�� ,��ݸ�ϵ»����޹�˾ ,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 ,
֧�� ,���������ʳƷ�������ι�˾ , ,���������ʳƷ�������ι�˾ ,��������(9876) ,31.00 ,���׳ɹ� ,������ʳ ,2021121122001468815736847417 ,322017092208872157020004030330000032112115225418 ,2021-12-11 16:53:51 ,
֧�� ,���ھ����������� ,hao***@qq.com ,���ھӶ��� ,��������(9876) ,7.50 ,���׳ɹ� ,���ðٻ� ,2021120922001468815735046417 ,10000891202112098830044 ,2021-12-09 09:25:42 ,
------------------------------------------------------------------------------------
������Ϣ��
������TripleZ
Expand Down
3 changes: 3 additions & 0 deletions pkg/analyser/alipay/alipay.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func (a Alipay) GetAccounts(o *ir.Order, cfg *config.Config, target, provider st
if r.Method != nil {
match = util.SplitFindContains(*r.Method, o.Method, sep, match)
}
if r.Category != nil {
match = util.SplitFindContains(*r.Category, o.Category, sep, match)
}
if r.StartTime != nil && r.EndTime != nil {
// TODO(gaocegege): Support it.
}
Expand Down
1 change: 1 addition & 0 deletions pkg/ir/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Order struct {
OrderType OrderType
Peer string
Item string
Category string
MerchantOrderID *string
OrderID *string
Money float64
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 @@ -25,6 +25,7 @@ type Config struct {
type Rule struct {
Peer *string `mapstructure:"peer,omitempty"`
Item *string `mapstructure:"item,omitempty"`
Category *string `mapstructure:"Category,omitempty"`
Type *string `mapstructure:"type,omitempty"`
Method *string `mapstructure:"method,omitempty"`
Separator *string `mapstructure:"sep,omitempty"` // default: ,
Expand Down
1 change: 1 addition & 0 deletions pkg/provider/alipay/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func (a *Alipay) convertToIR() *ir.IR {
irO := ir.Order{
Peer: o.Peer,
Item: o.ItemName,
Category: o.Category,
Method: o.Method,
PayTime: o.PayTime,
Money: o.Money,
Expand Down
30 changes: 30 additions & 0 deletions test/alipay-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
#
# E2E test for alipay 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 alipay bills output in beancount format
"$ROOT_DIR/bin/double-entry-generator" translate \
--provider alipay \
--config "$ROOT_DIR/example/alipay/config.yaml" \
--output "$ROOT_DIR/test/output/test-alipay-output.beancount" \
"$ROOT_DIR/example/alipay/example-alipay-records.csv"

diff -u --color \
"$ROOT_DIR/example/alipay/example-alipay-output.beancount" \
"$ROOT_DIR/test/output/test-alipay-output.beancount"

if [ $? -ne 0 ]; then
echo "[FAIL] alipay provider output is different from expected output."
exit 1
fi

echo "[PASS] All alipay provider tests!"

0 comments on commit cf071c9

Please sign in to comment.