Skip to content

Commit

Permalink
dev: support jd provider
Browse files Browse the repository at this point in the history
  • Loading branch information
CNLHC committed Apr 9, 2024
1 parent dd479fe commit 13cd731
Show file tree
Hide file tree
Showing 11 changed files with 612 additions and 1 deletion.
27 changes: 27 additions & 0 deletions example/jd/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defaultMinusAccount: Assets:FIXME
defaultPlusAccount: Expenses:FIXME
defaultCurrency: CNY
title: 测试
jd:
rules:
- method: 京东白条
methodAccount: Liabilities:Baitiao
- method: 小金库零用钱
methodAccount: Assets:EPay:JD
- item: 椰子
targetAccount: Expenses:Food
- item: 京东小金库-转入
peer: 京东金融
targetAccount: Assets:EPay:JD
- category: 美妆个护
targetAccount: Expenses:MakeUp
- item: "食品酒饮"
targetAccount: Assets:Food
- peer: 亲密卡
targetAccount: Expenses:Prpaid
- item: 白条,还款
targetAccount: Liabilities:Baitiao
- item: 京东小金库收益
fullMatch: true
targetAccount: Income:PnL:JD
methodAccount: Assets:EPay:JD
155 changes: 155 additions & 0 deletions example/jd/example-jd-output.beancount
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
option "title" "测试"
option "operating_currency" "CNY"

1970-01-01 open Assets:EPay:JD
1970-01-01 open Assets:FIXME
1970-01-01 open Assets:Food
1970-01-01 open Expenses:FIXME
1970-01-01 open Expenses:Food
1970-01-01 open Expenses:MakeUp
1970-01-01 open Expenses:Prpaid
1970-01-01 open Income:PnL:JD
1970-01-01 open Liabilities:Baitiao

2021-11-17 * "京东金融" "京东小金库-转入"
category: "小金库"
merchantId: "100000000000000000000027"
method: "银行卡"
orderId: "2000000000000079"
payTime: "2021-11-17 23:06:50 +0800 CST"
source: "京东金融"
status: "交易成功"
type: "不计收支"
Assets:EPay:JD 2990.13 CNY
Assets:FIXME -2990.13 CNY

2021-12-11 * "亲密卡" "亲密卡"
category: "线下消费"
merchantId: "160000000000000000000006"
method: "京东白条"
payTime: "2021-12-11 23:51:52 +0800 CST"
source: "亲密卡"
status: "交易成功"
type: "支出"
Expenses:Prpaid 238.18 CNY
Liabilities:Baitiao -238.18 CNY

2021-12-12 * "亲密卡" "亲密卡"
category: "线下消费"
merchantId: "100000000000000000000045"
method: "京东白条"
payTime: "2021-12-12 19:06:00 +0800 CST"
source: "亲密卡"
status: "交易成功"
type: "支出"
Expenses:Prpaid 53.00 CNY
Liabilities:Baitiao -53.00 CNY

2021-12-15 * "京东金融" "白条主动还款"
category: "白条"
merchantId: "107500000000000000000058"
method: "小金库零用钱"
orderId: "2000000000000000000"
payTime: "2021-12-15 12:09:23 +0800 CST"
source: "京东金融"
status: "交易成功"
type: "不计收支"
Liabilities:Baitiao 1125.43 CNY
Assets:EPay:JD -1125.43 CNY

2021-12-15 * "京东平台商户" "退款-freeplus洁面乳150ml"
category: "网购"
merchantId: "2000000000000000954"
method: "京东白条"
orderId: "88438843884"
payTime: "2021-12-15 18:35:00 +0800 CST"
source: "京东平台商户"
status: "交易成功"
type: "收入"
Liabilities:Baitiao 112.98 CNY
Assets:FIXME -112.98 CNY

2021-12-15 * "京东平台商户" "退款-freeplus洁面乳150ml"
category: "网购"
merchantId: "2000000000000000000"
method: "京东白条"
orderId: "88438843884"
payTime: "2021-12-15 19:29:00 +0800 CST"
source: "京东平台商户"
status: "交易成功"
type: "收入"
Liabilities:Baitiao 1.00 CNY
Assets:FIXME -1.00 CNY

2021-12-15 * "Food" "Food"
category: "食品酒饮"
merchantId: "110000000000000000000000"
method: "京东白条"
orderId: "200000000014"
payTime: "2021-12-15 19:35:14 +0800 CST"
source: "Food"
status: "交易成功"
type: "支出"
Expenses:FIXME 79.88 CNY
Liabilities:Baitiao -79.88 CNY

2021-12-16 * "京东平台商户" "退款-freeplus洁面乳150ml"
category: "网购"
merchantId: "1000000000000000000"
method: "京东白条"
orderId: "88438843884"
payTime: "2021-12-16 08:22:00 +0800 CST"
source: "京东平台商户"
status: "交易成功"
type: "收入"
Liabilities:Baitiao 1.00 CNY
Assets:FIXME -1.00 CNY

2021-12-18 * "京东平台商户" "freeplus洁面乳150ml"
category: "美妆个护"
merchantId: "100000000000000000000002"
method: "京东白条"
orderId: "88438843884"
payTime: "2021-12-18 19:45:25 +0800 CST"
source: "京东平台商户"
status: "交易成功"
type: "支出"
Expenses:MakeUp 223.99 CNY
Liabilities:Baitiao -223.99 CNY

2021-12-23 * "Members" "Members Mark 泰国进口 椰子水 1L*6支 椰子汁饮料"
category: "食品酒饮"
merchantId: "110000000000000000000044"
method: "京东白条"
orderId: "234906303557"
payTime: "2021-12-23 12:04:37 +0800 CST"
source: "Members"
status: "交易成功"
type: "支出"
Expenses:Food 91.42 CNY
Liabilities:Baitiao -91.42 CNY

2021-12-28 * "京东小金库" "京东小金库收益"
category: "小金库"
merchantId: "29999999999999999992"
method: "小金库零用钱"
orderId: "20000000000000000002"
payTime: "2021-12-28 12:14:38 +0800 CST"
source: "京东小金库"
status: "交易成功"
type: "收入"
Assets:EPay:JD 0.01 CNY
Income:PnL:JD -0.01 CNY

2021-12-29 * "京东小金库" "京东小金库收益"
category: "小金库"
merchantId: "20000000000000000002"
method: "小金库零用钱"
orderId: "20000000000000000022"
payTime: "2021-12-29 06:37:20 +0800 CST"
source: "京东小金库"
status: "交易成功"
type: "收入"
Assets:EPay:JD 0.01 CNY
Income:PnL:JD -0.01 CNY

34 changes: 34 additions & 0 deletions example/jd/example-jd-records.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
导出信息:
京东账号名:jd_whatthefuck
起始时间:2021-01-01 终止时间:2021-12-30
导出交易类型:全部
导出时间:2023-03-03 23:33:33
共:212笔记录
收入:31笔,15595.04元
支出:154笔,60735.99元
不计收支:27笔,69300.12元

特别提示:
1.本明细与实际交易结果不符时,以实际交易情况为准;
2.京东快捷支付等非余额支付方式可能既产生京东交易也同步产生银行交易,因此请勿使用本回单进行重复记账;
3.明细如经任何涂改、编造,均立即失去效力;
4.部分账单如:个人资金互转等不计入为收入或者支出,记为不计收支类;
5.因统计逻辑不同,明细金额直接累加后,可能会和下方统计金额不一致,请以实际交易金额为准;
6.禁止将本回单用于非法用途;
7.本明细仅展示当前账单中的交易,不包括已删除的记录;
8.本明细仅供个人对账使用。

电子回单客户
交易时间,交易分类,商户名称,交易说明,收/支,金额,收/付款方式,交易状态,交易订单号,商家订单号,备注
2021-12-29 06:37:20 ,小金库,京东小金库,京东小金库收益,收入,0.01,小金库零用钱,交易成功,20000000000000000022 ,20000000000000000002 ,
2021-12-28 12:14:38 ,小金库,京东小金库,京东小金库收益,收入,0.01,小金库零用钱,交易成功,20000000000000000002 ,29999999999999999992 ,
2021-12-23 12:04:37 ,食品酒饮,京东平台商户,Members Mark 泰国进口 椰子水 1L*6支 椰子汁饮料,支出,91.42,京东白条,交易成功,234906303557 ,110000000000000000000044 ,
2021-12-18 19:45:25 ,美妆个护,京东平台商户,freeplus洁面乳150ml,支出,223.99,京东白条,交易成功,88438843884 ,100000000000000000000002 ,
2021-12-16 08:22:00 ,网购,京东平台商户,退款-freeplus洁面乳150ml,收入,1.00,京东白条,交易成功,88438843884 ,1000000000000000000 ,
2021-12-12 19:06:00 ,线下消费,京东平台商户,亲密卡,支出,53.00,京东白条,交易成功, ,100000000000000000000045 ,
2021-12-15 12:09:23 ,白条,京东金融,白条主动还款,不计收支,1125.43,小金库零用钱,交易成功,2000000000000000000 ,107500000000000000000058 ,
2021-12-11 23:51:52 ,线下消费,京东平台商户,亲密卡,支出,238.18,京东白条,交易成功, ,160000000000000000000006 ,
2021-12-15 19:35:14 ,食品酒饮,京东平台商户,Food,支出,79.88,京东白条,交易成功,200000000014 ,110000000000000000000000 ,
2021-12-15 19:29:00 ,网购,京东平台商户,退款-freeplus洁面乳150ml,收入,1.00,京东白条,交易成功,88438843884 ,2000000000000000000 ,
2021-12-15 18:35:00 ,网购,京东平台商户,退款-freeplus洁面乳150ml,收入,112.98,京东白条,交易成功,88438843884 ,2000000000000000954 ,
2021-11-17 23:06:50 ,小金库,京东金融,京东小金库-转入,不计收支,2990.13,银行卡,交易成功,2000000000000079 ,100000000000000000000027 ,
4 changes: 4 additions & 0 deletions pkg/analyser/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/deb-sig/double-entry-generator/pkg/analyser/bmo"
"github.com/deb-sig/double-entry-generator/pkg/analyser/icbc"
"github.com/deb-sig/double-entry-generator/pkg/analyser/jd"
"github.com/deb-sig/double-entry-generator/pkg/analyser/td"

"github.com/deb-sig/double-entry-generator/pkg/analyser/alipay"
Expand Down Expand Up @@ -39,6 +40,9 @@ func New(providerName string) (Interface, error) {
return td.Td{}, nil
case consts.ProviderBmo:
return bmo.Bmo{}, nil
case consts.ProviderJD:
return jd.JD{}, nil

default:
return nil, fmt.Errorf("Fail to create the analyser for the given name %s", providerName)
}
Expand Down
133 changes: 133 additions & 0 deletions pkg/analyser/jd/jd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package jd

import (
"log"
"strings"

"github.com/deb-sig/double-entry-generator/pkg/config"
"github.com/deb-sig/double-entry-generator/pkg/ir"
"github.com/deb-sig/double-entry-generator/pkg/util"
)

type JD struct {
}

// GetAllCandidateAccounts returns all accounts defined in config.
func (a JD) GetAllCandidateAccounts(cfg *config.Config) map[string]bool {
// uniqMap will be used to create the concepts.
uniqMap := make(map[string]bool)

if cfg.JD == nil || len(cfg.JD.Rules) == 0 {
return uniqMap
}

for _, r := range cfg.JD.Rules {
if r.MethodAccount != nil {
uniqMap[*r.MethodAccount] = true
}
if r.TargetAccount != nil {
uniqMap[*r.TargetAccount] = true
}
if r.PnlAccount != nil {
uniqMap[*r.PnlAccount] = true
}
}
uniqMap[cfg.DefaultPlusAccount] = true
uniqMap[cfg.DefaultMinusAccount] = true
return uniqMap
}

// GetAccounts returns minus and plus account.
func (a JD) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, provider string) (bool, string, string, map[ir.Account]string, []string) {
ignore := false

if cfg.JD == nil || len(cfg.JD.Rules) == 0 {
return ignore, cfg.DefaultMinusAccount, cfg.DefaultPlusAccount, nil, nil
}
resMinus := cfg.DefaultMinusAccount
resPlus := cfg.DefaultPlusAccount
var extraAccounts map[ir.Account]string
var tags = make([]string, 0)

var err error
for _, r := range cfg.JD.Rules {
match := true
// get separator
sep := ","
if r.Separator != nil {
sep = *r.Separator
}

matchFunc := util.SplitFindContains
if r.FullMatch {
matchFunc = util.SplitFindEquals
}

if r.Peer != nil {
match = matchFunc(*r.Peer, o.Peer, sep, match)
}
if r.Type != nil {
match = matchFunc(*r.Type, o.TypeOriginal, sep, match)
}
if r.Item != nil {
match = matchFunc(*r.Item, o.Item, sep, match)
}
if r.Method != nil {
match = matchFunc(*r.Method, o.Method, sep, match)
}
if r.Category != nil {
match = matchFunc(*r.Category, o.Category, sep, match)
}
if r.Time != nil {
match, err = util.SplitFindTimeInterval(*r.Time, o.PayTime, match)
if err != nil {
log.Fatalf(err.Error())
}
}
if r.TimestampRange != nil {
match, err = util.SplitFindTimeStampInterval(*r.TimestampRange, o.PayTime, match)
if err != nil {
log.Fatalf(err.Error())
}
}

if match {
if r.Ignore {
ignore = true
break
}

if r.TargetAccount != nil {
if o.Type == ir.TypeRecv {
resMinus = *r.TargetAccount
} else {
resPlus = *r.TargetAccount
}
}
if r.MethodAccount != nil {
if o.Type == ir.TypeRecv {
resPlus = *r.MethodAccount
} else {
resMinus = *r.MethodAccount
}
}
if r.PnlAccount != nil {
extraAccounts = map[ir.Account]string{
ir.PnlAccount: *r.PnlAccount,
}
}

log.Println(resPlus, resMinus, o.Item, r.TargetAccount, r.MethodAccount)

if r.Tags != nil {
tags = strings.Split(*r.Tags, sep)
}

}
}

if strings.HasPrefix(o.Item, "退款-") && ir.TypeRecv != o.Type {
return ignore, resPlus, resMinus, extraAccounts, tags
}
return ignore, resMinus, resPlus, extraAccounts, tags
}
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/deb-sig/double-entry-generator/pkg/provider/htsec"
"github.com/deb-sig/double-entry-generator/pkg/provider/huobi"
"github.com/deb-sig/double-entry-generator/pkg/provider/icbc"
"github.com/deb-sig/double-entry-generator/pkg/provider/jd"
"github.com/deb-sig/double-entry-generator/pkg/provider/td"
"github.com/deb-sig/double-entry-generator/pkg/provider/wechat"
)
Expand All @@ -27,4 +28,5 @@ type Config struct {
Icbc *icbc.Config `yaml:"icbc,omitempty"`
Td *td.Config `yaml:"td,omitempty"`
Bmo *bmo.Config `yaml:"bmo,omitempty"`
JD *jd.Config `yaml:"jd,omitempty"`
}
2 changes: 2 additions & 0 deletions pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ const (
ProviderTd = "td"
//ProviderBmo is the name for BMO provider.
ProviderBmo = "bmo"
//ProviderJD is the name for JD provider
ProviderJD = "jd"
)
Loading

0 comments on commit 13cd731

Please sign in to comment.