From da0c4e719c5bb07dbd2876daf41b983eb70d9f84 Mon Sep 17 00:00:00 2001 From: wchen9 Date: Wed, 1 Mar 2023 12:36:48 +0800 Subject: [PATCH] Fix alipay format change (#85) --- example/alipay/config.yaml | 4 +- .../alipay/example-alipay-output.beancount | 203 ++++-------------- example/alipay/example-alipay-records.csv | 47 ++-- pkg/provider/alipay/alipay.go | 17 +- pkg/provider/alipay/parse.go | 45 ++-- pkg/provider/alipay/types.go | 2 +- 6 files changed, 89 insertions(+), 229 deletions(-) diff --git a/example/alipay/config.yaml b/example/alipay/config.yaml index 6eb6329..ce63c76 100644 --- a/example/alipay/config.yaml +++ b/example/alipay/config.yaml @@ -36,9 +36,9 @@ alipay: - method: 余额宝 fullMatch: true methodAccount: Assets:Alipay - - method: 招商银行(9876) + - method: 交通银行信用卡(7449) fullMatch: true - methodAccount: Assets:Bank:CN:CMB-9876:Savings + methodAccount: Liabilities:CC:COMM:7449 # 交易类型为其他 - type: 其他 diff --git a/example/alipay/example-alipay-output.beancount b/example/alipay/example-alipay-output.beancount index a3e076c..69f74af 100644 --- a/example/alipay/example-alipay-output.beancount +++ b/example/alipay/example-alipay-output.beancount @@ -4,7 +4,6 @@ option "operating_currency" "CNY" 1970-01-01 open Assets:Alipay 1970-01-01 open Assets:Alipay:Invest:Fund 1970-01-01 open Assets:Alipay:Invest:Gold -1970-01-01 open Assets:Bank:CN:CMB-9876:Savings 1970-01-01 open Assets:FIXME 1970-01-01 open Expenses:Electronics 1970-01-01 open Expenses:FIXME @@ -16,187 +15,63 @@ option "operating_currency" "CNY" 1970-01-01 open Income:Alipay:ShouKuanMa 1970-01-01 open Income:Alipay:YuEBao:PnL 1970-01-01 open Income:FIXME +1970-01-01 open Liabilities:CC:COMM:7449 -2020-06-02 * "蚂蚁财富-蚂蚁(杭州)基金销售有限公司" "蚂蚁财富-ABCD-卖出至余额宝" - category: "投资理财" - method: "余额宝" - orderId: "123456" - payTime: "2020-06-02 15:34:06 +0800 CST" - source: "支付宝" - status: "交易成功" - txType: "其他" - Assets:Alipay 15.40 CNY - Assets:Alipay:Invest:Fund -15.40 CNY - Income:Alipay:Invest:PnL - -2020-06-09 * "广发基金管理有限公司" "余额宝-2020.06.08-收益发放" - category: "投资理财" - method: "余额宝" - orderId: "123456" - payTime: "2020-06-09 02:57:13 +0800 CST" - source: "支付宝" - status: "交易成功" - txType: "其他" - Assets:Alipay 0.11 CNY - Income:Alipay:YuEBao:PnL -0.11 CNY - -2020-06-14 * "东莞肯德基有限公司" "KFCAPIDGB012345" - category: "餐饮美食" - merchantId: "ABC9876" - method: "余额宝" - orderId: "123456" - payTime: "2020-06-14 12:43:26 +0800 CST" - source: "支付宝" - status: "交易成功" - txType: "支出" - Expenses:Food:Lunch 79.00 CNY - Assets:Alipay -79.00 CNY - -2021-05-19 * "蚂蚁财富-蚂蚁(杭州)基金销售有限公司" "蚂蚁财富-XZY指数C-买入" - category: "投资理财" - orderId: "123456" - payTime: "2021-05-19 09:42:54 +0800 CST" - source: "支付宝" - status: "交易成功" - txType: "其他" - Assets:Alipay:Invest:Fund 50.00 CNY - Assets:Alipay -50.00 CNY - -2021-05-20 * "苏宁易购官方旗舰店" "【礼遇价】三星128g内存卡microSD存储卡tf卡行车记录仪卡switch监控摄像头" - category: "数码电器" - merchantId: "ABC9876" - method: "招商银行(9876)" - orderId: "123456" - payTime: "2021-05-20 12:34:34 +0800 CST" - source: "支付宝" - status: "交易成功" - txType: "支出" - Expenses:Electronics 87.90 CNY - Assets:Bank:CN:CMB-9876:Savings -87.90 CNY - -2021-05-30 * "广发基金管理有限公司" "余额宝-单次转入" - category: "投资理财" - merchantId: "ABC9876" - method: "余额" - orderId: "123456" - payTime: "2021-05-30 23:43:37 +0800 CST" - source: "支付宝" - status: "交易成功" - txType: "其他" - Assets:Alipay 2.00 CNY - Assets:Alipay -2.00 CNY - -2021-06-01 * "dettol滴露官方旗舰店" "【直接下单付款】滴露消毒液1.2L*2家用杀菌消毒水衣物地板宠物" - category: "日用百货" - merchantId: "ABC9876" - method: "招商银行(9876)" - orderId: "123456" - payTime: "2021-06-01 00:10:22 +0800 CST" - source: "支付宝" - status: "交易关闭" - txType: "支出" - Expenses:Groceries 109.90 CNY - Assets:Bank:CN:CMB-9876:Savings -109.90 CNY - -2021-06-01 * "dettol滴露官方旗舰店" "退款-【直接下单付款】滴露消毒液1.2L*2家用杀菌消毒水衣物地板宠物" - category: "退款" - merchantId: "ABC9876" - method: "招商银行(9876)" - orderId: "123456" - payTime: "2021-06-01 00:13:56 +0800 CST" - source: "支付宝" - status: "退款成功" - txType: "其他" - Assets:Bank:CN:CMB-9876:Savings 109.90 CNY - Expenses:Groceries -109.90 CNY - -2021-06-01 * "dettol滴露官方旗舰店" "退款-【直接下单付款】滴露消毒液1.2L*2家用杀菌消毒水衣物地板宠物" - category: "退款" - merchantId: "ABC9876" - method: "招商银行(9876)" - orderId: "123456" - payTime: "2021-06-01 00:13:56 +0800 CST" - source: "支付宝" - status: "退款成功" - txType: "收入" - Assets:Bank:CN:CMB-9876:Savings 109.90 CNY - Expenses:Groceries -109.90 CNY - -2021-06-14 * "蚂蚁会员(北京)网络技术服务有限公司" "相互宝分摊-6月第1期-*" - category: "互助保障" - merchantId: "ABC9876" - method: "招商银行(9876)" - orderId: "123456" - payTime: "2021-06-14 14:00:38 +0800 CST" - source: "支付宝" - status: "交易成功" - txType: "支出" - Expenses:Insurance 6.51 CNY - Assets:Bank:CN:CMB-9876:Savings -6.51 CNY - -2021-06-24 * "*三" "商品" - category: "收入" - merchantId: "123456" - orderId: "123456" - payTime: "2021-06-24 00:00:00 +0800 CST" - source: "支付宝" - status: "交易成功" - txType: "收入" - Assets:Alipay 10.00 CNY - Income:Alipay:ShouKuanMa -10.00 CNY - -2021-06-25 * "*三" "我就随便转着玩" +2023-01-18 * "xxxx" "转账" category: "转账红包" method: "余额" - orderId: "123456" - payTime: "2021-06-25 00:00:00 +0800 CST" + orderId: "2xxxxxxxxxxxxxxxxxxxxxxxxx9" + payTime: "2023-01-18 10:17:29 +0800 CST" source: "支付宝" status: "交易成功" txType: "收入" - Assets:Alipay 42.00 CNY - Income:FIXME -42.00 CNY + Assets:Alipay 222228.50 CNY + Income:FIXME -222228.50 CNY -2021-09-29 * "蚂蚁财富-蚂蚁(杭州)基金销售有限公司" "蚂蚁财富-财通资管鸿达纯债债券E-买入" +2023-02-02 * "蚂蚁财富-蚂蚁(杭州)基金销售有限公司" "蚂蚁财富-交银定期支付双息平衡混合-卖出至余额宝" category: "投资理财" - orderId: "2021092900108" - payTime: "2021-09-29 09:35:12 +0800 CST" + method: "余额宝" + orderId: "2xxxxxxxxxxxxxxxxxxxxxxxxxx8" + payTime: "2023-02-02 15:24:35 +0800 CST" source: "支付宝" status: "交易成功" - txType: "其他" - Assets:Alipay:Invest:Fund 20.00 CNY - Assets:Alipay -20.00 CNY + txType: "不计收支" + Expenses:FIXME 99.34 CNY + Assets:Alipay -99.34 CNY -2021-09-29 * "易方达黄金-蚂蚁(杭州)基金销售有限公司" "易方达黄金-买入" - category: "投资理财" - orderId: "2021092900108" - payTime: "2021-09-29 10:31:41 +0800 CST" +2023-02-04 * "xxxxxxx" "退款-亲情卡" + category: "退款" + merchantId: "20xxxxxxxxxxxxxxxx5" + method: "交通银行信用卡(7449)" + orderId: "2xxxxxxxxxxxxxxxx8" + payTime: "2023-02-04 18:21:04 +0800 CST" source: "支付宝" - status: "交易成功" - txType: "其他" - Assets:Alipay:Invest:Fund 3000.00 CNY - Assets:Alipay -3000.00 CNY + status: "退款成功" + txType: "不计收支" + Liabilities:CC:COMM:7449 16.03 CNY + Expenses:FIXME -16.03 CNY -2021-12-09 * "好邻居连锁便利店" "好邻居订单" +2023-02-08 * "x4***6" "商品示例" category: "日用百货" - merchantId: "10000891202112098830044" - method: "招商银行(9876)" - orderId: "2021120922001468815735046417" - payTime: "2021-12-09 09:25:42 +0800 CST" + merchantId: "Txxxxxxxxxxxxx0" + method: "余额" + orderId: "2xxxxxxxxxxxxxx0" + payTime: "2023-02-08 14:16:52 +0800 CST" source: "支付宝" - status: "交易成功" + status: "等待确认收货" txType: "支出" - Expenses:Groceries 7.50 CNY - Assets:Bank:CN:CMB-9876:Savings -7.50 CNY + Expenses:Groceries 20.00 CNY + Assets:Alipay -20.00 CNY -2021-12-11 * "北京稻香村食品有限责任公司" "北京稻香村食品有限责任公司" - category: "餐饮美食" - merchantId: "322017092208872157020004030330000032112115225418" - method: "招商银行(9876)" - orderId: "2021121122001468815736847417" - payTime: "2021-12-11 16:53:51 +0800 CST" +2023-02-12 * "xxxxxxxxxxxx" "亲情卡" + category: "亲友代付" + merchantId: "20230xxxxxxx014741014xxxxxx" + method: "交通银行信用卡(7449)" + orderId: "202302xxxxxx0011000103xxxxxx" + payTime: "2023-02-12 21:32:14 +0800 CST" source: "支付宝" status: "交易成功" txType: "支出" - Expenses:Food:Dinner 31.00 CNY - Assets:Bank:CN:CMB-9876:Savings -31.00 CNY + Expenses:FIXME 49.74 CNY + Liabilities:CC:COMM:7449 -49.74 CNY diff --git a/example/alipay/example-alipay-records.csv b/example/alipay/example-alipay-records.csv index 46e3664..93f0059 100644 --- a/example/alipay/example-alipay-records.csv +++ b/example/alipay/example-alipay-records.csv @@ -1,39 +1,30 @@ -------------------------֧й缼޹˾ ӿͻص------------------------ -/֧ ,׶Է ,Է˺ ,Ʒ˵ ,/ʽ , ,״̬ ,׷ ,׶ ,̼Ҷ ,ʱ , -֧ ,ϻԱ缼޹˾ , ,໥̯-6µ1-* ,(9876) ,6.51 ,׳ɹ , ,123456 ,ABC9876 ,2021-06-14 14:00:38 , - ,dettol¶ٷ콢 ,zhi***@hkmanon.com ,˿-ֱµ¶Һ1.2L*2ɱˮذ,(9876) ,109.90 ,˿ɹ ,˿ ,123456 ,ABC9876 ,2021-06-01 00:13:56 , - ,dettol¶ٷ콢 ,zhi***@hkmanon.com ,˿-ֱµ¶Һ1.2L*2ɱˮذ,(9876) ,109.90 ,˿ɹ ,˿ ,123456 ,ABC9876 ,2021-06-01 00:13:56 , -֧ ,dettol¶ٷ콢 ,zhi***@hkmanon.com ,ֱµ¶Һ1.2L*2ɱˮذ,(9876) ,109.90 ,׹ر ,ðٻ ,123456 ,ABC9876 ,2021-06-01 00:10:22 , - ,㷢޹˾ ,e-f***@gffunds.com.cn,-ת , ,2.00 ,׳ɹ ,Ͷ ,123456 ,ABC9876 ,2021-05-30 23:43:37 , - ,㷢޹˾ , ,-2020.06.08-淢 , ,0.11 ,׳ɹ ,Ͷ ,123456 , ,2020-06-09 02:57:13 , -֧ ,׹ٷ콢 ,sns***@cnsuning.com ,ۡ128gڴ濨microSD洢tfг¼ǿswitchͷ,(9876) ,87.90 ,׳ɹ , ,123456 ,ABC9876 ,2021-05-20 12:34:34 , - ,ϲƸ-ϣݣ޹˾ , ,ϲƸ-XZYָC-, ,50.00 ,׳ɹ ,Ͷ ,123456 , ,2021-05-19 09:42:54 , - ,ϲƸ-ϣݣ޹˾ , ,ϲƸ-ABCD-, ,15.40 ,׳ɹ ,Ͷ ,123456 , ,2020-06-02 15:34:06 , -֧ ,ݸϵ»޹˾ ,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 , - ,׷ƽ-ϣݣ޹˾, ,׷ƽ- , ,3000.00 ,׳ɹ ,Ͷ ,2021092900108 , ,2021-09-29 10:31:41 , - ,ϲƸ-ϣݣ޹˾ , ,ϲƸ-ͨʹܺ﴿ծծȯE- , ,20.00 ,׳ɹ ,Ͷ ,2021092900108 , ,2021-09-29 09:35:12 , ------------------------------------------------------------------------------------ Ϣ -TripleZ -֧˻abc@triplez.cn -ʼʱ䣺[2021-05-19 00:00:00] ֹʱ䣺[2021-06-19 23:59:59] -ʱ䣺[2021-06-19 03:10:16] -21ʼ¼ -룺0,0.00Ԫ -֧x,1234.56Ԫ -x,9876.54Ԫ +xx +֧˻xx@gmail.com +ʼʱ䣺[2023-02-10 00:00:00] ֹʱ䣺[2023-02-13 23:59:59] +ͣ[ȫ] +ʱ䣺[2023-02-13 09:12:52] +66ʼ¼ +룺1 28.50Ԫ +֧63 16.54Ԫ +֧2 16.37Ԫ رʾ 1.صݿɱ֧Ӧ֧룬ϵͳԭͨѶϵżص±صʵʽ׽ʱʵʽΪ׼ 2.𽫱صΪտƾʹã֤˻ʵʵٽз 3.֧֧ȷ֧ʽܼȲ֧Ҳͬнףʹñصظˣ -4.ص羭Ϳġ죬ʧȥЧ -5.˵磺ֵ֡˻ת߸֧ȲΪ֧Ϊࣻ +4.ص羭κͿġ죬ʧȥЧ +5.˵磺ֵ֡˻ת߸֧ȲΪ֧Ϊ֧ࣻ 6.ͳ߼ͬϸֱۼӺ󣬿ܻ·ͳƽһ£ʵʽ׽Ϊ׼ 7.ֹصڷǷ; 8.ϸչʾǰ˵еĽףɾļ¼ 9.ϸ˶ʹá + +------------------------֧й缼޹˾ ӿͻص------------------------ +ʱ ,׷ ,׶Է ,Է˺ ,Ʒ˵ ,/֧ , ,/ʽ ,״̬ ,׶ ,̼Ҷ ,ע , +2023-02-12 21:32:14 ,Ѵ ,xxxxxxxxxxxx ,/ ,鿨 ,֧ ,49.74 ,ͨÿ(7449) ,׳ɹ ,202302xxxxxx0011000103xxxxxx ,20230xxxxxxx014741014xxxxxx , , +2023-02-08 14:16:52 ,ðٻ ,x4***6 ,rim***@qq.com ,Ʒʾ ,֧ ,20.00 , ,ȴȷջ ,2xxxxxxxxxxxxxx0 ,Txxxxxxxxxxxxx0 , , +2023-02-04 18:21:04 ,˿ ,xxxxxxx ,/ ,˿-鿨 ,֧ ,16.03 ,ͨÿ(7449) ,˿ɹ ,2xxxxxxxxxxxxxxxx8 ,20xxxxxxxxxxxxxxxx5 , , +2023-02-02 15:24:35 ,Ͷ ,ϲƸ-ϣݣ޹˾ ,/ ,ϲƸ-֧˫Ϣƽ-,֧ ,99.34 , ,׳ɹ ,2xxxxxxxxxxxxxxxxxxxxxxxxxx8 , , , +2023-01-18 10:17:29 ,ת˺ ,xxxx ,xxx***@163.com ,ת , ,222228.50 , ,׳ɹ ,2xxxxxxxxxxxxxxxxxxxxxxxxx9 , , , diff --git a/pkg/provider/alipay/alipay.go b/pkg/provider/alipay/alipay.go index f65502e..bd4071f 100644 --- a/pkg/provider/alipay/alipay.go +++ b/pkg/provider/alipay/alipay.go @@ -19,11 +19,11 @@ package alipay import ( "encoding/csv" "fmt" - "io" - "log" - "github.com/deb-sig/double-entry-generator/pkg/io/reader" "github.com/deb-sig/double-entry-generator/pkg/ir" + "io" + "log" + "strings" ) // Alipay is the provider for alipay. @@ -70,9 +70,14 @@ func (a *Alipay) Translate(filename string) (*ir.IR, error) { return nil, err } - if len(line) != 12 { - // TODO(gaocegege): Support statistics. - a.LineNum++ + if a.LineNum == 0 && strings.Contains(line[0], "支付宝") { + return nil, fmt.Errorf("可能为支付宝老版本 csv 账单,请使用 1.7.0 及之前的版本尝试转换") + } + + a.LineNum++ + + if a.LineNum <= 23 { + // bypass the useless continue } diff --git a/pkg/provider/alipay/parse.go b/pkg/provider/alipay/parse.go index f99aa51..baa7bcc 100644 --- a/pkg/provider/alipay/parse.go +++ b/pkg/provider/alipay/parse.go @@ -13,17 +13,6 @@ import ( // Modified by TripleZ at Shenzhen(2021) func (a *Alipay) translateToOrders(array []string) error { var err error - a.LineNum++ - - if len(array) != 12 { - return fmt.Errorf("Length mismatch: Expected 12, got %d", len(array)) - } - - // Ignore the title row. - if !a.TitleParsed { - a.TitleParsed = true - return nil - } for idx, a := range array { a = strings.Trim(a, " ") @@ -31,34 +20,34 @@ func (a *Alipay) translateToOrders(array []string) error { array[idx] = a } var bill Order - bill.Type = getTxType(array[0]) + bill.Type = getTxType(array[5]) if bill.Type == TypeNil { - log.Println("get tx type error:", array[0], array) - return fmt.Errorf("Failed to get the tx type %s", array[0]) - } - bill.TypeOriginal = array[0] - bill.Peer = array[1] - bill.PeerAccount = array[2] - bill.ItemName = array[3] - bill.Method = array[4] - bill.Money, err = strconv.ParseFloat(array[5], 32) + log.Println("get tx type error:", array[5], array) + return fmt.Errorf("Failed to get the tx type %s", array[5]) + } + bill.TypeOriginal = array[5] + bill.Peer = array[2] + bill.PeerAccount = array[3] + bill.ItemName = array[4] + bill.Method = array[7] + bill.Money, err = strconv.ParseFloat(array[6], 32) if err != nil { - log.Println("parse money error:", array[5], err) + log.Println("parse money error:", array[6], err) return err } - bill.Status = array[6] + bill.Status = array[8] if bill.Status == "交易关闭" { log.Printf("Line %d: There is a mole, The tx is canceled.", a.LineNum) } if bill.Status == "退款成功" { log.Printf("Lind %d: There has a refund transaction.", a.LineNum) } - bill.Category = array[7] - bill.DealNo = array[8] - bill.MerchantId = array[9] - bill.PayTime, err = time.Parse(localTimeFmt, array[10]+" +0800 CST") + bill.Category = array[1] + bill.DealNo = array[9] + bill.MerchantId = array[10] + bill.PayTime, err = time.Parse(localTimeFmt, array[0]+" +0800 CST") if err != nil { - log.Println("parse create time error:", array[10], err) + log.Println("parse create time error:", array[0], err) return err } diff --git a/pkg/provider/alipay/types.go b/pkg/provider/alipay/types.go index bcae023..80ff364 100644 --- a/pkg/provider/alipay/types.go +++ b/pkg/provider/alipay/types.go @@ -48,7 +48,7 @@ type Type string const ( TypeSend Type = "支出" TypeRecv Type = "收入" - TypeOthers Type = "其他" + TypeOthers Type = "不计收支" TypeEmpty Type = "" TypeNil Type = "未知" )