diff options
Diffstat (limited to 'python/report_ops/custodians.py')
| -rw-r--r-- | python/report_ops/custodians.py | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/python/report_ops/custodians.py b/python/report_ops/custodians.py index 8da504d7..5702b278 100644 --- a/python/report_ops/custodians.py +++ b/python/report_ops/custodians.py @@ -5,10 +5,47 @@ import datetime from .misc import get_dir, dt_from_fname import gpg import datetime -from serenitas.ops.custodians import upload_to_bbh, upload_to_umb +from serenitas.ops.trade_dataclasses import BondDeal +from serenitas.ops.funds import Service from typing import ClassVar from dataclasses import dataclass +_sql = ( + "INSERT INTO bond_csv_upload (allocationid, cusip, principal, interest) SELECT id, identifier, principal_payment, " + "accrued_payment FROM bond_trades WHERE trade_date=%s AND account=%s AND tradeid IS NOT NULL ON CONFLICT DO NOTHING RETURNING allocationid;" +) + +_bond_query = "SELECT * FROM bond_trades WHERE id in %s;" + + +def upload_to_custodian(account, trade_date, conn, upload): + _fund = {"BBH": "BRINKER", "UMB": "UMB"} + custodian = Service[_fund[account]] + with conn.cursor() as c: + c.execute( + _sql, + ( + trade_date, + account, + ), + ) + tids = tuple(row.allocationid for row in c) + if not tids: + return + c.execute(_bond_query, (tids,)) + for row in c: + trade = BondDeal.from_dict(**row._asdict(), scaled=True) + match account: + case "BBH": + custodian.staging_queue.append(trade.to_bbh("NEW")) + case "UMB": + custodian.staging_queue.append(trade.to_umb("NEW")) + buf, dest = custodian.build_buffer("bond") + custodian.staging_queue.clear() + conn.commit() + if upload: + custodian.upload(buf, dest.name, confirm=account != "UMB") + @dataclass class Custodian: |
