Skip to content

Commit

Permalink
support for transactions with misisng units
Browse files Browse the repository at this point in the history
  • Loading branch information
codereverser committed Dec 21, 2024
1 parent c01bbde commit d2987c4
Show file tree
Hide file tree
Showing 4 changed files with 767 additions and 793 deletions.
9 changes: 7 additions & 2 deletions casparser/process/cas_detailed.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
TRANSACTION_RE1,
TRANSACTION_RE2,
TRANSACTION_RE3,
TRANSACTION_RE4,
VALUATION_RE,
)
from .utils import isin_search
Expand Down Expand Up @@ -99,7 +100,7 @@ def get_transaction_type(
txn_type = TransactionType.PURCHASE
elif units < 0:
if re.search(
"reversal|rejection|dishonoured|mismatch|insufficient\s+balance", description, re.I
r"reversal|rejection|dishonoured|mismatch|insufficient\s+balance", description, re.I
):
txn_type = TransactionType.REVERSAL
elif "switch" in description:
Expand Down Expand Up @@ -128,7 +129,7 @@ def get_parsed_scheme_name(scheme) -> str:


def parse_transaction(line) -> Optional[ParsedTransaction]:
for regex in (TRANSACTION_RE1, TRANSACTION_RE2, TRANSACTION_RE3):
for regex in (TRANSACTION_RE1, TRANSACTION_RE2, TRANSACTION_RE3, TRANSACTION_RE4):
if m := re.search(regex, line, re.DOTALL | re.MULTILINE | re.I):
groups = m.groups()
date = description = amount = units = nav = balance = None
Expand All @@ -138,6 +139,10 @@ def parse_transaction(line) -> Optional[ParsedTransaction]:
elif groups.count(None) == 2:
# Segregated Portfolio Entries
date, description, units, balance, *_ = groups
elif groups.count(None) == 1:
# Zero unit entries
date, description, amount, units, nav, balance = groups
units = "0.000"
elif groups.count(None) == 0:
# Normal entries
date, description, amount, units, nav, balance = groups
Expand Down
6 changes: 4 additions & 2 deletions casparser/process/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@

# Normal Transaction entries
TRANSACTION_RE1 = rf"{date_re}\t\t([^0-9].*)\t\t{amt_re}\t\t{amt_re}\t\t{amt_re}\t\t{amt_re}"
# Zero unit transactions
TRANSACTION_RE2 = rf"{date_re}\t\t([^0-9].*)\t\t{amt_re}\t\t(?:{amt_re})*\t\t{amt_re}\t\t{amt_re}"
# Segregated portfolio entries
TRANSACTION_RE2 = rf"{date_re}\t\t([^0-9].*)\t\t{amt_re}\t\t{amt_re}(?:\t\t{amt_re}\t\t{amt_re})*"
TRANSACTION_RE3 = rf"{date_re}\t\t([^0-9].*)\t\t{amt_re}\t\t{amt_re}(?:\t\t{amt_re}\t\t{amt_re})*"
# Tax transactions
TRANSACTION_RE3 = rf"{date_re}\t\t([^0-9].*)\t\t{amt_re}(?:\t\t{amt_re}\t\t{amt_re}\t\t{amt_re})*"
TRANSACTION_RE4 = rf"{date_re}\t\t([^0-9].*)\t\t{amt_re}(?:\t\t{amt_re}\t\t{amt_re}\t\t{amt_re})*"
DESCRIPTION_TAIL_RE = r"(\n.+?)(\t\t|$)"
DIVIDEND_RE = r"(?:div\.|dividend|idcw).+?(reinvest)*.*?@\s*Rs\.\s*([\d\.]+)(?:\s+per\s+unit)?"
SCHEME_TAIL_RE = r"(\n.+?)(?:\t\t|$)"
Loading

0 comments on commit d2987c4

Please sign in to comment.