diff options
Diffstat (limited to 'python/report_ops/cash.py')
| -rw-r--r-- | python/report_ops/cash.py | 77 |
1 files changed, 40 insertions, 37 deletions
diff --git a/python/report_ops/cash.py b/python/report_ops/cash.py index ffc5f4c8..94837697 100644 --- a/python/report_ops/cash.py +++ b/python/report_ops/cash.py @@ -1,15 +1,15 @@ -from serenitas.analytics.dates import prev_business_day import datetime import pandas as pd -from serenitas.utils.db import dbconn from typing import ClassVar -from .misc import get_dir, dt_from_fname -from .custodians import get_custodian_download_fun from functools import partial from dataclasses import dataclass - from serenitas.utils.env import DAILY_DIR +from serenitas.utils.db import dbconn +from serenitas.analytics.dates import prev_business_day + +from .misc import get_dir, dt_from_fname +from .custodians import get_custodian_download_fun @dataclass @@ -30,21 +30,19 @@ class CashReport: def __class_getitem__(cls, key): return cls._registry[key] - def get_cash_report(self, report_prefix): - report_dir = get_dir(self.date) + @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(self.date).iterdir() - if f.name.startswith(report_prefix) - ], - key=partial(dt_from_fname, dt_format=self.dtkey), + [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 ValueError( - f"No reports found for fund: {self.fund} date: {self.date}" + f"No reports found for fund: {prefix.split('_')[-1]} date: {date}" ) return p @@ -58,13 +56,14 @@ class CashReport: def clear(cls): cls._staging_queue.clear() - def stage_from_row(self, row, fund): + @classmethod + def stage_from_row(cls, row, date, fund): (account, currency), amount = row - self._staging_queue.add( + cls._staging_queue.add( ( - prev_business_day(self.date), - self.fund, - f"{self.custodian} Custody Account {self.fund}", + prev_business_day(date), + fund, + f"{cls.custodian} Custody Account {fund}", account, currency, amount, @@ -72,16 +71,17 @@ class CashReport: ) @classmethod - def to_db(self, fund): - for row in self.yield_rows(): - self.stage_from_row(row, fund) - self.commit() - self.clear() + 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"): - def yield_rows(self, fund): - p = self.get_cash_report(f"NT_CASH_{fund}") + @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"]] @@ -91,8 +91,9 @@ class NTCashReport(CashReport, custodian="NT", dtkey="%Y%m%d%H%M"): class UMBCashReport(CashReport, custodian="UMB", dtkey="%Y%m%d%H%M"): - def yield_rows(self, fund): - p = self.get_cash_report("UMB_CASH_{fund}") + @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" @@ -100,8 +101,9 @@ class UMBCashReport(CashReport, custodian="UMB", dtkey="%Y%m%d%H%M"): class BNYCashReport(CashReport, custodian="BNY", dtkey="%Y%m%d%H%M%S"): - def yield_rows(self): - p = self.get_cash_report("BNY_CASH_{fund}") + @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 @@ -115,8 +117,9 @@ class BNYCashReport(CashReport, custodian="BNY", dtkey="%Y%m%d%H%M%S"): class ScotiaCashReport(CashReport, custodian="SCOTIA", dtkey="%Y%m%d%H%M%S"): - def yield_rows(self): - p = self.get_cash_report() + @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 @@ -126,14 +129,14 @@ class ScotiaCashReport(CashReport, custodian="SCOTIA", dtkey="%Y%m%d%H%M%S"): df.loc[0]["Closing Bal."], ) - def get_cash_report(self, prefix=None): - self.download_reports(self.date) + @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(self.date):%d-%b-%Y}_*_xlsx.JOAAPKO3.JOAAPKO1" + f"IsoSelene_{prev_business_day(date):%d-%b-%Y}_*_xlsx.JOAAPKO3.JOAAPKO1" ) ) - except StopIteration as e: # File doesn't exist, let's get it" - raise ValueError(f"No file available for Scotia on {self.date}") from e + except StopIteration as e: + raise ValueError(f"No file available for Scotia on {date}") from e |
