-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreport_generator.py
More file actions
167 lines (142 loc) · 6.53 KB
/
Copy pathreport_generator.py
File metadata and controls
167 lines (142 loc) · 6.53 KB
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
from fpdf import FPDF
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import os
# 1. FORCE NON-INTERACTIVE BACKEND (Prevents GUI crashes)
matplotlib.use('Agg')
def create_pdf_report(cost, rent, vacancy, opex, equity_irr, total_profit, break_year, cash_flows,
ltv, interest_rate):
# --- SETUP PATHS ---
base_dir = os.getcwd()
chart1_path = os.path.join(base_dir, 'temp_chart_cumulative.png')
chart2_path = os.path.join(base_dir, 'temp_chart_bar.png')
# --- 1. THE "ROBOT ANALYST" LOGIC (Updated for Leveraged Returns) ---
# Investors want higher returns (20%+) when they take on debt risk
if equity_irr >= 20:
verdict_title = "STRONG BUY / OUTPERFORM"
verdict_color = (0, 100, 0) # Dark Green
analysis_text = (
f"This leveraged simulation demonstrates exceptional capital efficiency. With a Loan-to-Value "
f"of {ltv*100:.0f}%, the project achieves an Equity IRR of {equity_irr:.2f}%, significantly "
f"exceeding standard emerging market hurdles (18-20%). The debt structure successfully amplifies "
f"returns while maintaining a manageable risk profile."
)
elif equity_irr >= 15:
verdict_title = "MODERATE / PROCEED WITH CAUTION"
verdict_color = (200, 150, 0) # Orange
analysis_text = (
f"The project is viable but sensitive. While the Equity IRR of {equity_irr:.2f}% is acceptable, "
f"it sits close to the cost of equity. Management should strictly monitor construction costs "
f"(${cost/1_000_000:.1f}M) and interest rate exposure ({interest_rate*100:.1f}%) to prevent "
f"yield erosion."
)
else:
verdict_title = "HOLD / RESTRUCTURE DEBT"
verdict_color = (139, 0, 0) # Dark Red
analysis_text = (
f"CRITICAL WARNING: The current debt structure is destroying value. An Equity IRR of {equity_irr:.2f}% "
f"is insufficient to justify the leverage risk. Consider reducing the Loan-to-Value ratio "
f"or renegotiating the interest rate ({interest_rate*100:.1f}%) before proceeding."
)
cumulative_cash = np.cumsum(cash_flows)
payback_text = (
f"Based on the leveraged cash flow trajectory, the Equity Investment achieves 'Breakeven' in {break_year}. "
f"The total projected profit (Cash Flows + Exit) is approximately ${total_profit/1_000_000:.1f} Million."
)
# --- 2. GENERATE CHARTS (With Safe Paths) ---
def generate_charts():
# Cumulative Equity Return Chart
years = range(len(cumulative_cash))
plt.figure(figsize=(7, 3.5))
plt.plot(years, cumulative_cash/1_000_000, marker='o', color='#2E86C1', linewidth=2)
plt.axhline(0, color='red', linestyle='--', linewidth=1, label="Equity Breakeven")
plt.title('Cumulative Equity Wealth (Millions USD)', fontsize=10, fontweight='bold')
plt.xlabel('Project Year', fontsize=8)
plt.ylabel('Net Position ($M)', fontsize=8)
plt.grid(True, linestyle=':', alpha=0.6)
plt.legend()
plt.tight_layout()
plt.savefig(chart1_path, dpi=120)
plt.close()
# Bar Chart (Equity Cash Flows)
plt.figure(figsize=(7, 3.5))
colors = ['#C0392B' if x < 0 else '#27AE60' for x in cash_flows]
plt.bar(years, [x/1_000_000 for x in cash_flows], color=colors)
plt.title('Annual Equity Cash Flow (After Debt Service)', fontsize=10, fontweight='bold')
plt.xlabel('Project Year', fontsize=8)
plt.ylabel('Cash Flow ($M)', fontsize=8)
plt.grid(axis='y', linestyle=':', alpha=0.6)
plt.tight_layout()
plt.savefig(chart2_path, dpi=120)
plt.close()
generate_charts()
# --- 3. BUILD THE PDF ---
class PDF(FPDF):
def header(self):
self.set_font('Arial', 'B', 16)
self.cell(0, 8, 'Mall of Zimbabwe: Investment Memo (Leveraged)', 0, 1, 'C')
self.set_font('Arial', 'I', 9)
self.set_text_color(100, 100, 100)
self.cell(0, 5, 'Generated by WestProp Financial Digital Twin | Confidential', 0, 1, 'C')
self.ln(5)
self.set_draw_color(0, 0, 0)
self.line(10, 25, 200, 25)
def footer(self):
self.set_y(-15)
self.set_font('Arial', 'I', 8)
self.set_text_color(128, 128, 128)
self.cell(0, 10, f'Page {self.page_no()}', 0, 0, 'C')
pdf = PDF()
pdf.add_page()
pdf.set_text_color(0, 0, 0)
# SECTION 1: VERDICT
pdf.set_font("Arial", 'B', 12)
pdf.set_fill_color(240, 240, 240)
pdf.cell(0, 8, " 1. Strategic Investment Verdict", 0, 1, 'L', 1)
pdf.ln(3)
pdf.set_font("Arial", 'B', 11)
pdf.set_text_color(*verdict_color)
pdf.cell(0, 8, f"RATING: {verdict_title}", 0, 1)
pdf.set_text_color(0, 0, 0)
pdf.set_font("Arial", size=10)
pdf.multi_cell(0, 5, analysis_text)
pdf.ln(5)
# SECTION 2: LEVERAGE & METRICS (Updated for Debt)
pdf.set_font("Arial", 'B', 12)
pdf.cell(0, 8, " 2. Capital Structure & Returns", 0, 1, 'L', 1)
pdf.ln(3)
pdf.set_font("Arial", size=10)
# Left Column (Project)
pdf.cell(95, 7, f"Total Project Cost: ${cost/1_000_000:.1f} M", 1, 0)
pdf.cell(95, 7, f"Equity IRR (Leveraged): {equity_irr:.2f}%", 1, 1)
# Right Column (Debt)
pdf.cell(95, 7, f"Bank Loan (LTV {ltv*100:.0f}%): ${cost*ltv/1_000_000:.1f} M", 1, 0)
pdf.cell(95, 7, f"Cost of Debt (Interest): {interest_rate*100:.1f}%", 1, 1)
pdf.cell(95, 7, f"Equity Cash Required: ${cost*(1-ltv)/1_000_000:.1f} M", 1, 0)
pdf.cell(95, 7, f"Equity Break-Even: {break_year}", 1, 1)
pdf.ln(8)
# SECTION 3: VISUALS
pdf.set_font("Arial", 'B', 12)
pdf.cell(0, 8, " 3. Equity Performance Trajectory", 0, 1, 'L', 1)
pdf.ln(2)
if os.path.exists(chart1_path):
pdf.image(chart1_path, x=15, w=180)
else:
pdf.cell(0, 10, "Error: Chart 1 missing.", 0, 1)
pdf.ln(2)
if os.path.exists(chart2_path):
pdf.image(chart2_path, x=15, w=180)
else:
pdf.cell(0, 10, "Error: Chart 2 missing.", 0, 1)
pdf.ln(5)
# SECTION 4: LIQUIDITY
pdf.set_font("Arial", 'B', 12)
pdf.cell(0, 8, " 4. Liquidity & Exit Analysis", 0, 1, 'L', 1)
pdf.ln(3)
pdf.set_font("Arial", size=10)
pdf.multi_cell(0, 5, payback_text)
# CLEANUP
if os.path.exists(chart1_path): os.remove(chart1_path)
if os.path.exists(chart2_path): os.remove(chart2_path)
return pdf.output(dest='S').encode('latin-1')