1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
import datetime
from typing import ClassVar
from functools import partial
from dataclasses import dataclass
import pandas as pd
from serenitas.utils.env import DAILY_DIR
from serenitas.utils.db2 import dbconn
from serenitas.analytics.dates import prev_business_day
from serenitas.analytics.exceptions import MissingDataError
from .misc import get_dir, dt_from_fname, Custodian
@dataclass
class CashReport:
custodian: ClassVar[Custodian]
date: datetime.date
dtkey: ClassVar
_conn: ClassVar = dbconn("dawndb")
_staging_queue: ClassVar[set] = set()
_insert_sql = "INSERT INTO cash_balances VALUES (%s, %s, %s, %s, %s, %s) ON CONFLICT DO NOTHING"
_registry = {}
def __init_subclass__(cls, custodian, dtkey):
cls.custodian = custodian
cls.dtkey = dtkey
cls._registry[custodian] = cls
def __class_getitem__(cls, key):
return cls._registry[key]
@classmethod
def get_report(cls, date, fund, prefix=None):
report_dir = get_dir(date)
report_dir.mkdir(exist_ok=True, parents=True)
prefix = prefix if prefix else f"{cls.custodian}_CASH_{fund}"
p = max(
[f for f in get_dir(date).iterdir() if f.name.startswith(prefix)],
key=partial(dt_from_fname, dt_format=cls.dtkey),
default=None,
)
if not p:
raise MissingDataError(
f"No reports found for fund: {prefix.split('_')[-1]} date: {date}"
)
return p
@classmethod
def commit(cls):
with cls._conn.cursor() as c:
c.executemany(cls._insert_sql, cls._staging_queue)
cls._conn.commit()
@classmethod
def clear(cls):
cls._staging_queue.clear()
@classmethod
def stage_from_row(cls, row, date, fund):
(account, currency), amount = row
cls._staging_queue.add(
(
prev_business_day(date),
fund,
f"{cls.custodian} Custody Account {fund}",
account,
currency,
amount,
)
)
@classmethod
def to_db(cls, date, fund):
for row in cls.yield_rows(date, fund):
cls.stage_from_row(row, date, fund)
cls.commit()
cls.clear()
class NTCashReport(CashReport, custodian="NT", dtkey="%Y%m%d%H%M"):
@classmethod
def yield_rows(cls, date, fund):
p = cls.get_report(date, fund)
df = pd.read_csv(p, on_bad_lines="warn")
df = df[df["T-NARR-LONG"] == "CLOSING BALANCE"]
df = df[["Consolidation", "Currency code", "A-TRAN-AMT"]]
df.columns = df.columns.str.replace(" |-|_", "", regex=True).str.lower()
df = df.set_index(["consolidation", "currencycode"])
yield from df.itertuples()
class UMBCashReport(CashReport, custodian="UMB", dtkey="%Y%m%d%H%M"):
@classmethod
def yield_rows(cls, date, fund):
p = cls.get_report(date, fund)
df = pd.read_excel(p, skiprows=3)
yield from df.groupby(["Portfolio #", "Currency"]).sum(numeric_only=True)[
"Current Balance"
].items()
class BNYCashReport(CashReport, custodian="BNY", dtkey="%Y%m%d%H%M%S"):
@classmethod
def yield_rows(cls, date, fund):
p = cls.get_report(date, fund)
df = pd.read_csv(p)
df["Beginning Balance Local"] = df["Beginning Balance Local"].apply(
lambda s: "-" + s[1:-1] if s.startswith("(") else s
)
df["Beginning Balance Local"] = pd.to_numeric(
df["Beginning Balance Local"].str.replace(",", "")
)
yield from df.groupby(["Account Number", "Local Currency Code"]).sum(
numeric_only=True
)["Beginning Balance Local"].items()
class ScotiaCashReport(CashReport, custodian="SCOTIA", dtkey="%Y%m%d%H%M%S"):
@classmethod
def yield_rows(cls, date, fund):
p = cls.get_report(date, fund)
df = pd.read_excel(p, skipfooter=1)
if df.empty:
# No wires, so we can't skip the footer
df = pd.read_excel(p)
yield (
(int(df.loc[0]["Account"]), df.loc[0]["Curr."]),
df.loc[0]["Closing Bal."],
)
@classmethod
def get_report(cls, date, fund):
REPORT_DIR = DAILY_DIR / "Selene" / "Scotia_reports"
try:
return next(
REPORT_DIR.glob(
f"IsoSelene_{prev_business_day(date):%d-%b-%Y}_*_xlsx.JOAAPKO3.JOAAPKO1"
)
)
except StopIteration as e:
raise MissingDataError(f"No file available for Scotia on {date}") from e
|