aboutsummaryrefslogtreecommitdiffstats
path: root/python/monthend_interest_recon.py
blob: 1e29a520aa31a83cb1d88955cb4e677ede2affd8 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import datetime
import logging
import argparse
from collections import defaultdict
from io import StringIO
import os

import pandas as pd
from dateutil.relativedelta import relativedelta
from dataclasses import dataclass
import openpyxl
from zipfile import BadZipFile

from serenitas.utils.env import DAILY_DIR
from serenitas.utils.exchange import ExchangeMessage, FileAttachment

from collateral.common import load_pdf
from interest_statement import export_data
from report_ops.misc import em_date_filter


logger = logging.getLogger(__name__)


def get_pat(month, year, counterparty):
    match counterparty:
        case "BoA":
            month_abbr = datetime.datetime.strptime(str(month), "%m").strftime("%b")
            pattern = f"??{month_abbr}{year}*.pdf"
        case "BNP":
            month_name = datetime.datetime.strptime(str(month), "%m").strftime("%B")
            pattern = (
                f"Interest Statement SCAR GCM_RVM - USD - ?? {month_name} {year}.pdf"
            )
        case "CITI":
            pattern = f"*_USD_ICS_SYSTEM.*.{year}{str(month).zfill(2)}??.xlsx"
        case "JPM":
            pattern = f"CSINSTMT-*-{str(year)[2:]}{str(month).zfill(2)}??-*_*_*.pdf"
        case "GS":
            pattern = f"MVPInterestStatement_{year}{str(month).zfill(2)}??_*_*.pdf"
        case "MS":
            pattern = f"*IntStmt_{year}{str(month).zfill(2)}??.xls"
        case _:
            raise ValueError(f"Missing counterparty: {counterparty}")
    return pattern


def download_messages(em, counterparties, start, end):
    for counterparty in counterparties:
        for msg in em.get_msgs(
            20, path=["Interest", counterparty], **em_date_filter(em, start, end)
        ):
            BASE_DIR = (
                DAILY_DIR
                / "Serenitas"
                / f"{counterparty}_reports"
                / "Interest Statements"
            )
            for attach in msg.attachments:
                p = BASE_DIR / attach.name
                p.parent.mkdir(parents=True, exist_ok=True)
                if not p.exists():
                    p.write_bytes(attach.content)


@dataclass
class InterestCounterparty:
    month: datetime.date.month
    year: datetime.date.year
    _registry = {}

    def __init_subclass__(cls, name):
        cls.name = name
        cls._registry[name] = cls

    def __class_getitem__(cls, name: str):
        return cls._registry[name]

    def yield_files(self):
        BASE_DIR = (
            DAILY_DIR / "Serenitas" / f"{self.name}_reports" / "Interest Statements"
        )
        yield sorted(BASE_DIR.glob(self.pat), key=lambda x: -os.path.getmtime(x))[0]

    @property
    def pat(self):
        statement_month = datetime.date(
            year=self.year, month=self.month, day=1
        ) + relativedelta(months=1)
        return get_pat(statement_month.month, statement_month.year, self.name)


class BoA(InterestCounterparty, name="BoA"):
    @staticmethod
    def get_interest_amount(file_path):
        pdf = load_pdf(file_path)
        for e, n in zip(pdf, pdf[1:]):
            if "Net interest Amount" in e.text:
                return -float(
                    n.text.replace("(", "-").replace(")", "").replace(",", "")
                )


class BNP(InterestCounterparty, name="BNP"):
    @staticmethod
    def get_interest_amount(file_path):
        pdf = load_pdf(file_path)
        for e, n in zip(pdf, pdf[1:]):
            if "Due to" in e.text:
                value = n.text.replace(",", "")
                return -float(value)

    @property
    def pat(self):
        # BNP files reflect month of interest rather than month of statement
        return get_pat(self.month, self.year, self.name)


class CITI(InterestCounterparty, name="CITI"):
    @staticmethod
    def get_interest_amount(file_path):
        sheet = openpyxl.load_workbook(file_path).active
        for row in sheet.rows:
            if row[1].value in (
                "Net Interest Due To CP",
                "Net Interest Due to Citi",
            ):
                return -row[5].value

    def yield_files(self):
        BASE_DIR = (
            DAILY_DIR / "Serenitas" / f"{self.name}_reports" / "Interest Statements"
        )
        yield from BASE_DIR.glob(self.pat)  # Citi has two files VM and IM files


class JPM(InterestCounterparty, name="JPM"):
    @staticmethod
    def get_interest_amount(file_path):
        pdf = load_pdf(file_path)
        for e in pdf:
            if "Page" in e.text:
                return float(value.replace(",", ""))
            value = e.text

    @property
    def pat(self):
        # JPM files reflect month of interest rather than month of statement
        return get_pat(self.month, self.year, self.name)


class GS(InterestCounterparty, name="GS"):
    @staticmethod
    def get_interest_amount(file_path):
        pdf = load_pdf(file_path)
        for e, n in zip(pdf, pdf[1:]):
            if "due to" in e.text:
                return float(n.text.replace("USD", "").replace(",", ""))

    @property
    def pat(self):
        # GS files reflect month of interest rather than month of statement
        return get_pat(self.month, self.year, self.name)


class MS(InterestCounterparty, name="MS"):
    @staticmethod
    def get_interest_amount(file_path):
        df = pd.read_excel(file_path)
        return -round(df["LOCAL_ACCRUAL"].sum(), 2)

    @property
    def pat(self):
        # MS files reflect previous business day, could potenitally be the same month so we'll have to edit accordingly
        return get_pat(self.month, self.year, self.name)


def parse_args():
    """Parses command line arguments"""
    parser = argparse.ArgumentParser(description="Generate IAM file for globeop")
    parser.add_argument(
        "monthend",
        nargs="?",
        type=datetime.date.fromisoformat,
        default=datetime.date.today().replace(day=1) - datetime.timedelta(days=1),
    )
    parser.add_argument(
        "--accept",
        "-a",
        action="store_true",
        default=False,
        help="accept the differences are within tolerance and edit the csv accordingly",
    )
    return parser.parse_args()


def get_statement_totals(counterparties, month, year):
    interest_amounts = defaultdict(float)
    for counterparty in counterparties:
        interest_counterparty = InterestCounterparty[counterparty](month, year)
        for f in interest_counterparty.yield_files():
            interest_amounts[counterparty] += interest_counterparty.get_interest_amount(
                f
            )
    return pd.DataFrame.from_dict(
        interest_amounts, orient="index", columns=["statement_interest"]
    ).rename(index={"BoA": "BAML_ISDA"})


def main():
    args = parse_args()
    em = ExchangeMessage()
    download_messages(
        em,
        InterestCounterparty._registry.keys(),
        args.monthend.replace(day=1),
        args.monthend + relativedelta(months=1),
    )
    global df
    df = get_statement_totals(
        InterestCounterparty._registry.keys(), args.monthend.month, args.monthend.year
    )
    serenitas_df = export_data(
        args.monthend - relativedelta(months=1) + datetime.timedelta(days=1),
        args.monthend,
    )
    df = pd.merge(
        serenitas_df.groupby("broker").sum(),
        df,
        how="outer",
        left_index=True,
        right_index=True,
    )
    df = df.fillna(0)
    df["difference"] = df["amount"] - df["statement_interest"]
    if args.accept:
        for cp, difference in df["difference"].items():
            # Match with counterparty if within tolerance
            try:
                serenitas_df.at[(cp, "CSH_CASH"), "amount"] -= difference
            except KeyError:
                serenitas_df = serenitas_df.reindex(
                    serenitas_df.index.append(
                        pd.MultiIndex.from_tuples([(cp, "CSH_CASH")])
                    )
                )
                serenitas_df.at[(cp, "CSH_CASH"), "amount"] = -difference
        buf = StringIO()
        serenitas_df.to_csv(buf)
        em.send_email(
            subject=f"Allocation of Interest for {args.monthend:%b-%y}",
            body="Please see attached for allocation of interest by strategy and counterparty",
            to_recipients=("serenitas.otc@sscinc.com", "SERENITAS.ops@sscinc.com"),
            cc_recipients=("nyops@lmcg.com",),
            attach=[
                FileAttachment(
                    name=f"{args.monthend:%b-%y}.csv", content=buf.getvalue().encode()
                )
            ],
        )


if __name__ == "__main__":
    main()