aboutsummaryrefslogtreecommitdiffstats
path: root/python/notify_fx_cashflows.py
blob: 71659e09f79ed923386f99a0b0ed7d46b059c0dd (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
import datetime
from tabulate import tabulate
from exchangelib import HTMLBody

from serenitas.utils.db2 import dbconn
from serenitas.utils.exchange import ExchangeMessage

from report_ops.utils import Monitor

from report_ops.misc import _settlement_recipients, _cc_recipients


class FXMonitor(
    Monitor,
    headers=(
        "dealid",
        "counterparty_id",
        "trade_date",
        "settle_date",
        "cp_code",
        "name",
        "buy_currency",
        "buy_amount",
        "sell_currency",
        "sell_amount",
    ),
    num_format=[("{0:,.2f}", 7), ("{0:,.2f}", 9)],
):
    @classmethod
    def email(cls, fund, settle_date):
        if not cls._staging_queue:
            return
        cls._em.send_email(
            f"FX Settlements {fund}: {settle_date}",
            HTMLBody(
                f"""
<html>
  <head>
    <style>
      table, th, td {{ border: 1px solid black;  border-collapse: collapse;}}
      th, td {{ padding: 5px; }}
    </style>
  </head>
  <body>
  Hello,<br><br>We see the below FX Settlements for VD {settle_date}. Could you please draft wires and reach out to the counterparty? As these are electronically agreed, please send out the funds regardless of confirmation from counterparty.<br><br>{cls.to_tabulate()}
  </body>
</html>"""
            ),
            to_recipients=_settlement_recipients[fund],
            cc_recipients=_cc_recipients[fund],
        )


def email_setts(settle_date, conn, fund):
    QUERY = "SELECT dealid, trade_date, settle_date, ft.cp_code, ft.name, buy_currency, buy_amount, sell_currency, sell_amount, cpty_id as counterparty_id FROM forward_trades ft LEFT JOIN accounts2 USING (cash_account) WHERE accounts2.fund=%s AND ft.settle_date=%s AND accounts2.account_type='Cash';"
    with conn.cursor() as c:
        c.execute(
            QUERY,
            (fund, settle_date),
        )
        for rec in c:
            d = rec._asdict()
            FXMonitor.stage(d)
        FXMonitor.email(fund, settle_date)
        FXMonitor.clear()


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "settle_date",
        nargs="?",
        type=datetime.date.fromisoformat,
        default=datetime.date.today(),
        help="settle date",
    )
    args = parser.parse_args()
    conn = dbconn("dawndb")
    for fund in (
        "SERCGMAST",
        "ISOSEL",
        # "BOWDST",
    ):
        email_setts(args.settle_date, conn, fund)