Skip to content

Commit

Permalink
feat: support price range in alipay/wechat providers (#131)
Browse files Browse the repository at this point in the history
* feat: support price range in `alipay`/`wechat` providers

* chore: lint code

Signed-off-by: TripleZ <[email protected]>

* chore: fix lint

* chore: fix lint

Signed-off-by: TripleZ <[email protected]>

* fix: check error rule

Signed-off-by: TripleZ <[email protected]>

---------

Signed-off-by: TripleZ <[email protected]>
  • Loading branch information
Triple-Z authored Sep 17, 2024
1 parent d0cf159 commit ef964f3
Show file tree
Hide file tree
Showing 18 changed files with 178 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*.out

.vscode/
.idea/
bin/
wasm-dist/

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ alipay:
> - `11:00-13:00`
> - `11:00:00-13:00:00`
> 24 小时制,起始时间和终止之间之间使用 `-` 分隔。
- `minPrice`(最小金额)和 `maxPrice`(最大金额)的区间匹配。

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

Expand Down Expand Up @@ -556,6 +557,7 @@ wechat:
> - `11:00-13:00`
> - `11:00:00-13:00:00`
> 24 小时制,起始时间和终止之间之间使用 `-` 分隔。
- `minPrice`(最小金额)和 `maxPrice`(最大金额)的区间匹配。

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

Expand Down
5 changes: 5 additions & 0 deletions example/alipay/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ alipay:
methodAccount: Assets:Alipay

- category: 日用百货
minPrice: 10
targetAccount: Expenses:Groceries
- category: 日用百货
minPrice: 0
maxPrice: 9.99
targetAccount: Expenses:Food:Drink
- category: 餐饮美食
time: 11:00-14:00
targetAccount: Expenses:Food:Lunch
Expand Down
23 changes: 23 additions & 0 deletions example/alipay/example-alipay-output.beancount
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ option "operating_currency" "CNY"
1970-01-01 open Expenses:Electronics
1970-01-01 open Expenses:FIXME
1970-01-01 open Expenses:Food:Dinner
1970-01-01 open Expenses:Food:Drink
1970-01-01 open Expenses:Food:Lunch
1970-01-01 open Expenses:Groceries
1970-01-01 open Expenses:Insurance
Expand Down Expand Up @@ -75,3 +76,25 @@ option "operating_currency" "CNY"
Expenses:FIXME 49.74 CNY
Liabilities:CC:COMM:7449 -49.74 CNY

2023-07-10 * "xxxx" "xxxx"
category: "日用百货"
merchantId: "xxxx"
orderId: "xxxx"
payTime: "2023-07-10 13:10:16 +0800 CST"
source: "支付宝"
status: "交易成功"
type: "支出"
Expenses:Food:Drink 9.90 CNY
Assets:FIXME -9.90 CNY

2023-07-10 * "xxxx" "xxxx"
category: "日用百货"
merchantId: "xxxx"
orderId: "xxxx"
payTime: "2023-07-10 13:20:16 +0800 CST"
source: "支付宝"
status: "交易成功"
type: "支出"
Expenses:Groceries 82.00 CNY
Assets:FIXME -82.00 CNY

23 changes: 23 additions & 0 deletions example/alipay/example-alipay-output.ledger
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Expenses:Electronics 0 CNY
Expenses:FIXME 0 CNY
Expenses:Food:Dinner 0 CNY
Expenses:Food:Drink 0 CNY
Expenses:Food:Lunch 0 CNY
Expenses:Groceries 0 CNY
Expenses:Insurance 0 CNY
Expand Down Expand Up @@ -73,3 +74,25 @@
Expenses:FIXME 49.74 CNY
Liabilities:CC:COMM:7449 - 49.74 CNY

2023/07/10 * xxxx - xxxx
; category: "日用百货"
; merchantId: "xxxx"
; orderId: "xxxx"
; payTime: "2023-07-10 13:10:16 &#43;0800 CST"
; source: "支付宝"
; status: "交易成功"
; type: "支出"
Expenses:Food:Drink 9.90 CNY
Assets:FIXME - 9.90 CNY

2023/07/10 * xxxx - xxxx
; category: "日用百货"
; merchantId: "xxxx"
; orderId: "xxxx"
; payTime: "2023-07-10 13:20:16 &#43;0800 CST"
; source: "支付宝"
; status: "交易成功"
; type: "支出"
Expenses:Groceries 82.00 CNY
Assets:FIXME - 82.00 CNY

2 changes: 2 additions & 0 deletions example/alipay/example-alipay-records.csv
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@
2023-01-10 13:10:16,���ðٻ�,xxxx,/,xxxx,������֧,82.00,,���׹ر�,xxxx ,xxxx ,,
2023-01-09 18:22:28,�˿�,һ��ͨ,fin***@jieyisoft.com,�˿�-һ��ͨ��ֵ,������֧,50.00,��,�˿�ɹ�,2023xxxxx88_2023xx57 ,D12*****14 ,,
2023-01-09 18:21:50,��ͨ����,һ��ͨ,fin***@jieyisoft.com,һ��ͨ��ֵ,֧��,50.00,��,���׹ر�,2023xxxxx88 ,D12*****14 ,,
2023-07-10 13:10:16,���ðٻ�,xxxx,/,xxxx,֧��,9.90,,���׳ɹ�,xxxx ,xxxx ,,
2023-07-10 13:20:16,���ðٻ�,xxxx,/,xxxx,֧��,82.00,,���׳ɹ�,xxxx ,xxxx ,,
10 changes: 10 additions & 0 deletions example/wechat/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ wechat:
time: 23:50-00:05 # test T-1
targetAccount: Expenses:Food:Meal:MidNight

- peer: 美团平台商户
type: 支出
minPrice: 0
maxPrice: 9.99
targetAccount: Expenses:Food:Drink
- peer: 美团平台商户
type: 支出
minPrice: 10
targetAccount: Expenses:Food:Meal

- peer: 房东
type: 支出
targetAccount: Expenses:Housing:Rent
Expand Down
26 changes: 26 additions & 0 deletions example/wechat/example-wechat-output.beancount
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ option "operating_currency" "CNY"
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:Drink
1970-01-01 open Expenses:Food:Meal
1970-01-01 open Expenses:Food:Meal:Dinner
1970-01-01 open Expenses:Food:Meal:Lunch
1970-01-01 open Expenses:Food:Meal:MidNight
Expand Down Expand Up @@ -276,3 +278,27 @@ option "operating_currency" "CNY"
Assets:Digital:Wechat:Cash 5.00 CNY
Income:Wechat:RedPacket -5.00 CNY

2023-07-09 * "美团平台商户" "美团订单-12345"
merchantId: "654321"
method: "工商银行"
orderId: "123456"
payTime: "2023-07-09 13:25:22 +0800 CST"
source: "微信支付"
status: "支付成功"
txType: "商户消费"
type: "支出"
Expenses:Food:Drink 9.90 CNY
Assets:Bank:CN:ICBC:Savings -9.90 CNY

2023-07-09 * "美团平台商户" "美团订单-54321"
merchantId: "654321"
method: "工商银行"
orderId: "123456"
payTime: "2023-07-09 13:30:22 +0800 CST"
source: "微信支付"
status: "支付成功"
txType: "商户消费"
type: "支出"
Expenses:Food:Meal 50.00 CNY
Assets:Bank:CN:ICBC:Savings -50.00 CNY

26 changes: 26 additions & 0 deletions example/wechat/example-wechat-output.ledger
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
Assets:FIXME 0 CNY
Assets:Trade:Tencent:LiCaiTong 0 CNY
Expenses:FIXME 0 CNY
Expenses:Food:Drink 0 CNY
Expenses:Food:Meal 0 CNY
Expenses:Food:Meal:Dinner 0 CNY
Expenses:Food:Meal:Lunch 0 CNY
Expenses:Food:Meal:MidNight 0 CNY
Expand Down Expand Up @@ -275,3 +277,27 @@
Assets:Digital:Wechat:Cash 5.00 CNY
Income:Wechat:RedPacket - 5.00 CNY

2023/07/09 * 美团平台商户 - 美团订单-12345
; merchantId: "654321"
; method: "工商银行"
; orderId: "123456"
; payTime: "2023-07-09 13:25:22 &#43;0800 CST"
; source: "微信支付"
; status: "支付成功"
; txType: "商户消费"
; type: "支出"
Expenses:Food:Drink 9.90 CNY
Assets:Bank:CN:ICBC:Savings - 9.90 CNY

2023/07/09 * 美团平台商户 - 美团订单-54321
; merchantId: "654321"
; method: "工商银行"
; orderId: "123456"
; payTime: "2023-07-09 13:30:22 &#43;0800 CST"
; source: "微信支付"
; status: "支付成功"
; txType: "商户消费"
; type: "支出"
Expenses:Food:Meal 50.00 CNY
Assets:Bank:CN:ICBC:Savings - 50.00 CNY

2 changes: 2 additions & 0 deletions example/wechat/example-wechat-records.csv
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@
2022-07-18 10:48:09,商户消费,测试时间戳,点击底部"多多视频","/",收入,¥0.07,/,充值成功,160572459521071810106004542906137497131422938 ,10101265586742107189275763431049 ,"/"
2022-09-24 02:24:20,赞赏码,YingDev,"/",支出,¥36.99,零钱,朋友已收钱,100010810122092400064222561891707533 ,1000108101202209241820542253348 ,"/"
2023-06-23 11:01:51,其他,赞赏作者的收款_20230623,"/",收入,¥5.00,/,已到账,180000737623062310106004541373333333333333333 ,10101008588132306239222222222222 ,"赞赏作者的收款_20230623"
2023-07-09 13:25:22,商户消费,美团平台商户,"美团订单-12345",支出,¥9.90,工商银行,支付成功,123456 ,654321 ,"/"
2023-07-09 13:30:22,商户消费,美团平台商户,"美团订单-54321",支出,¥50.0,工商银行,支付成功,123456 ,654321 ,"/"
10 changes: 8 additions & 2 deletions pkg/analyser/alipay/alipay.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,21 @@ func (a Alipay) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, prov
if r.Time != nil {
match, err = util.SplitFindTimeInterval(*r.Time, o.PayTime, match)
if err != nil {
log.Fatalf(err.Error())
log.Fatalf("%v", err)
}
}
if r.TimestampRange != nil {
match, err = util.SplitFindTimeStampInterval(*r.TimestampRange, o.PayTime, match)
if err != nil {
log.Fatalf(err.Error())
log.Fatalf("%v", err)
}
}
if r.MinPrice != nil && o.Money < *r.MinPrice {
match = false
}
if r.MaxPrice != nil && o.Money > *r.MaxPrice {
match = false
}

if match {
if r.Ignore {
Expand Down
4 changes: 2 additions & 2 deletions pkg/analyser/htsec/htsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ func (h Htsec) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, provi
if r.Time != nil {
match, err = util.SplitFindTimeInterval(*r.Time, o.PayTime, match)
if err != nil {
log.Fatalf(err.Error())
log.Fatalf("%v", err)
}
}
if r.TimestampRange != nil {
match, err = util.SplitFindTimeStampInterval(*r.TimestampRange, o.PayTime, match)
if err != nil {
log.Fatalf(err.Error())
log.Fatalf("%v", err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/analyser/huobi/huobi.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ func (h Huobi) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, provi
if r.Time != nil {
match, err = util.SplitFindTimeInterval(*r.Time, o.PayTime, match)
if err != nil {
log.Fatalf(err.Error())
log.Fatalf("%v", err)
}
}
if r.TimestampRange != nil {
match, err = util.SplitFindTimeStampInterval(*r.TimestampRange, o.PayTime, match)
if err != nil {
log.Fatalf(err.Error())
log.Fatalf("%v", err)
}
}
if match {
Expand Down
4 changes: 2 additions & 2 deletions pkg/analyser/jd/jd.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ func (a JD) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, provider
if r.Time != nil {
match, err = util.SplitFindTimeInterval(*r.Time, o.PayTime, match)
if err != nil {
log.Fatalf(err.Error())
log.Fatalf("%v", err)
}
}
if r.TimestampRange != nil {
match, err = util.SplitFindTimeStampInterval(*r.TimestampRange, o.PayTime, match)
if err != nil {
log.Fatalf(err.Error())
log.Fatalf("%v", err)
}
}

Expand Down
13 changes: 11 additions & 2 deletions pkg/analyser/wechat/wechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,24 @@ func (w Wechat) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, prov
if r.Time != nil {
match, err = util.SplitFindTimeInterval(*r.Time, o.PayTime, match)
if err != nil {
log.Fatalf(err.Error())
log.Fatalf("Error in SplitFindTimeInterval: %v", err)
}
}
if r.TimestampRange != nil {
match, err = util.SplitFindTimeStampInterval(*r.TimestampRange, o.PayTime, match)
if err != nil {
log.Fatalf(err.Error())
log.Fatalf("Error in SplitFindTimeStampInterval: %v", err)
}
}
if r.MinPrice != nil && r.MaxPrice != nil && *r.MinPrice > *r.MaxPrice {
log.Fatalf("Error rule: minPrice > maxPrice!")
}
if r.MinPrice != nil && o.Money < *r.MinPrice {
match = false
}
if r.MaxPrice != nil && o.Money > *r.MaxPrice {
match = false
}

if match {
if r.Ignore {
Expand Down
30 changes: 16 additions & 14 deletions pkg/provider/alipay/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ type Config struct {

// Rule is the type for match rules.
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: ,
Time *string `mapstructure:"time,omitempty"`
TimestampRange *string `mapstructure:"timestamp_range,omitempty"`
MethodAccount *string `mapstructure:"methodAccount,omitempty"`
TargetAccount *string `mapstructure:"targetAccount,omitempty"`
PnlAccount *string `mapstructure:"pnlAccount,omitempty"`
FullMatch bool `mapstructure:"fullMatch,omitempty"`
Tags *string `mapstructure:"tags,omitempty"`
Ignore bool `mapstructure:"ignore,omitempty"` // default: false
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: ,
Time *string `mapstructure:"time,omitempty"`
TimestampRange *string `mapstructure:"timestamp_range,omitempty"`
MethodAccount *string `mapstructure:"methodAccount,omitempty"`
TargetAccount *string `mapstructure:"targetAccount,omitempty"`
PnlAccount *string `mapstructure:"pnlAccount,omitempty"`
FullMatch bool `mapstructure:"fullMatch,omitempty"`
Tags *string `mapstructure:"tags,omitempty"`
Ignore bool `mapstructure:"ignore,omitempty"` // default: false
MinPrice *float64 `mapstructure:"minPrice,omitempty"`
MaxPrice *float64 `mapstructure:"maxPrice,omitempty"`
}
30 changes: 16 additions & 14 deletions pkg/provider/wechat/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ type Config struct {

// Rule is the type for match rules.
type Rule struct {
Peer *string `mapstructure:"peer,omitempty"`
Item *string `mapstructure:"item,omitempty"`
Type *string `mapstructure:"type,omitempty"`
TxType *string `mapstructure:"txType,omitempty"`
Separator *string `mapstructure:"sep,omitempty"` // default: ,
Method *string `mapstructure:"method,omitempty"`
Time *string `mapstructure:"time,omitempty"`
TimestampRange *string `mapstructure:"timestamp_range,omitempty"`
MethodAccount *string `mapstructure:"methodAccount,omitempty"`
TargetAccount *string `mapstructure:"targetAccount,omitempty"`
CommissionAccount *string `mapstructure:"commissionAccount,omitempty"`
FullMatch bool `mapstructure:"fullMatch,omitempty"`
Tag *string `mapstructure:"tag,omitempty"`
Ignore bool `mapstructure:"ignore,omitempty"` // default: false
Peer *string `mapstructure:"peer,omitempty"`
Item *string `mapstructure:"item,omitempty"`
Type *string `mapstructure:"type,omitempty"`
TxType *string `mapstructure:"txType,omitempty"`
Separator *string `mapstructure:"sep,omitempty"` // default: ,
Method *string `mapstructure:"method,omitempty"`
Time *string `mapstructure:"time,omitempty"`
TimestampRange *string `mapstructure:"timestamp_range,omitempty"`
MethodAccount *string `mapstructure:"methodAccount,omitempty"`
TargetAccount *string `mapstructure:"targetAccount,omitempty"`
CommissionAccount *string `mapstructure:"commissionAccount,omitempty"`
FullMatch bool `mapstructure:"fullMatch,omitempty"`
Tag *string `mapstructure:"tag,omitempty"`
Ignore bool `mapstructure:"ignore,omitempty"` // default: false
MinPrice *float64 `mapstructure:"minPrice,omitempty"`
MaxPrice *float64 `mapstructure:"maxPrice,omitempty"`
}
2 changes: 1 addition & 1 deletion pkg/provider/wechat/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func getTxType(tt string) TxType {
return TxTypeFamilyCard
} else if strings.Contains(tt, string(TxTypeSponsorCode)) {
return TxTypeSponsorCode
} else if strings.Contains(tt, string(TxTypeOther)){
} else if strings.Contains(tt, string(TxTypeOther)) {
return TxTypeOther
} else {
return TxTypeUnknown
Expand Down

0 comments on commit ef964f3

Please sign in to comment.