Mercurial > louis > ofxstatement-us-hsbc
comparison src/ofxstatement/plugins/us_hsbc/transactions.py @ 7:829eb62755b0
First cut at an HSBC (USA) plugin
author | Louis Opter <kalessin@kalessin.fr> |
---|---|
date | Thu, 17 Nov 2016 16:25:12 -0800 |
parents | |
children | 164da24a2997 |
comparison
equal
deleted
inserted
replaced
6:5692e2a61764 | 7:829eb62755b0 |
---|---|
1 # Copyright (c) 2016, Louis Opter <louis@opter.org> | |
2 # | |
3 # This file is part of ofxstatement-us-hsbc. | |
4 # | |
5 # ofxstatement-us-hsbc is free software: you can redistribute it and/or | |
6 # modify it under the terms of the GNU General Public License as | |
7 # published by the Free Software Foundation, either version 3 of the | |
8 # License, or (at your option) any later version. | |
9 # | |
10 # ofxstatement-us-hsbc is distributed in the hope that it will be | |
11 # useful, but WITHOUT ANY WARRANTY; without even the implied warranty | |
12 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 # GNU General Public License for more details. | |
14 # | |
15 # You should have received a copy of the GNU General Public License | |
16 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | |
18 import dateutil.parser | |
19 import re | |
20 | |
21 from ofxstatement.statement import StatementLine | |
22 from typing import Callable, Dict | |
23 | |
24 from .record import Record | |
25 | |
26 | |
27 def _enrich_date_user(sl: StatementLine, record: Record, date: str) -> None: | |
28 if not date: | |
29 return | |
30 | |
31 dt = dateutil.parser.parse(date, dayfirst=False, yearfirst=False) | |
32 sl.date_user = dt.replace(year=min(dt.year, sl.date.year)) | |
33 | |
34 | |
35 def _enrich_check(sl: StatementLine, record: Record) -> None: | |
36 sl.check_no = record.original_description.split("-")[-1].strip() | |
37 sl.memo = record.user_description | |
38 | |
39 | |
40 def _enrich_cash_withdrawal(sl: StatementLine, record: Record) -> None: | |
41 desc = record.original_description.replace("CASH WITHDRAWAL ON", "").strip() | |
42 date, sl.memo = [part.strip() for part in desc.split("AT", 1)] | |
43 _enrich_date_user(sl, record, date) | |
44 | |
45 | |
46 def _enrich_purchase(sl: StatementLine, record: Record) -> None: | |
47 # ORIGINAL_DESCRIPTION has more informations than SIMPLE_DESCRIPTION even | |
48 # if it's a bit less readable: | |
49 sl.memo = record.original_description | |
50 sl.memo = re.sub(r"^PURCHASE (-|(MADE )?ON)", "", sl.memo, re.I).strip() | |
51 | |
52 | |
53 def _enrich_credit(sl: StatementLine, record: Record) -> None: | |
54 prefix = "CREDIT RECEIVED ON" | |
55 sl.memo = record.original_description.replace(prefix, "").strip() | |
56 parts = [part.strip() for part in sl.memo.split("FROM", 1)] | |
57 if len(parts) > 1: | |
58 date, sl.memo = parts | |
59 _enrich_date_user(sl, record, date) | |
60 | |
61 | |
62 def _enrich_deposit(sl: StatementLine, record: Record) -> None: | |
63 sl.memo = record.original_description.replace("DEPOSIT FROM", "").strip() | |
64 | |
65 | |
66 def _enrich_payment(sl: StatementLine, record: Record) -> None: | |
67 sl.memo = record.original_description | |
68 sl.memo = re.sub(r"^(ONLINE )?PAYMENT (TO|-)", "", sl.memo, re.I).strip() | |
69 | |
70 | |
71 def _enrich_generic(sl: StatementLine, record: Record) -> None: | |
72 sl.memo = record.original_description | |
73 | |
74 _TRNTYPE_TO_METHOD: Dict[str, Callable[[StatementLine, Record], None]] = { | |
75 "CHECK": _enrich_check, | |
76 "CASH": _enrich_cash_withdrawal, | |
77 "PURCHASE": _enrich_purchase, | |
78 "CREDIT": _enrich_credit, | |
79 "DEP": _enrich_deposit, | |
80 "PAYMENT": _enrich_payment, | |
81 } | |
82 | |
83 | |
84 def enrich(sl: StatementLine, record: Record) -> None: | |
85 _TRNTYPE_TO_METHOD.get(sl.trntype, _enrich_generic)(sl, record) |