-
Notifications
You must be signed in to change notification settings - Fork 1
/
WriterHTML.fs
268 lines (251 loc) · 7.51 KB
/
WriterHTML.fs
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
265
266
267
268
module MinhaCarteira.WriterHTML
open System.IO
open System
open MinhaCarteira.Models
open System.Globalization
open MinhaCarteira.CalculoPosicao
open System.Text.Json
let private culture = CultureInfo.InvariantCulture
let vendasAnchor = "Vendas"
let regra3Pretty x y =
let d = (percent x y) - 100m
let s = if d >= 0m then "+" else "-"
let abs = Math.Abs(d)
s + string abs
let private getStyle =
"
<style>
table {
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #ddd;
padding: 8px;
}
tr:nth-child(even){
background-color: #f2f2f2;
}
tr:hover {
background-color: #ddd;
}
th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
}
.chart-container {
margin-left: 10%;
width: 80%;
margin-bottom: 5%;
}
#summary {
background-color: #FFC785;
}
</style>
"
let private getChart o =
let id = Guid.NewGuid()
let values = String.Join(", ", o.Ativos |> Seq.map(fun x -> x.PercentValorPatrimonio.ToString(culture)))
let names = String.Join(", ", o.Ativos |> Seq.map(fun x -> $"'{x.Ativo}'"))
$"
<div class=\"chart-container\">
<canvas id=\"{id}\"></canvas>
</div>
<script>
(function(){{
var ctx = document.getElementById('{id}').getContext('2d');
var chart = new Chart(ctx, {{
type: 'pie',
data: {{
datasets: [{{
label: 'Colors',
data: [{values}],
backgroundColor: [
'#0074D9', '#FF4136', '#2ECC40', '#FF851B',
'#7FDBFF', '#B10DC9', '#FFDC00', '#001f3f',
'#39CCCC', '#01FF70', '#85144b', '#F012BE',
'#3D9970', '#111111', '#AAAAAA']
}}],
labels: [{names}]
}},
options: {{
responsive:true,
title:{{
display: true,
text: \"%% Patrimonio\"
}},
plugins: {{
datalabels: {{
formatter: (value, ctx) => value + '%%',
color: '#fff',
}}
}}
}}
}});
}})();
</script>"
let private getSummaryTable o =
$"<a name=\"{o.Nome}\" />
<table>
<tr>
<th>Carteira</th>
<th>Qtd Ativos</th>
<th>Total Aplicado</th>
<th>Total Patrimonio</th>
<th>Total Lucro</th>
<th>%% Rentabilidade</th>
<th>Total Venda</th>
</tr>
<tr>
<td>{o.Nome}</td>
<td>{o.Ativos |> Seq.length}</td>
<td>{o.TotalAplicado:C}</td>
<td>{o.TotalPatrimonio:C}</td>
<td>{o.TotalLucro:C}</td>
<td>{regra3Pretty o.TotalAplicado o.TotalPatrimonio}</td>
<td>{o.LucroVenda:C}</td>
</tr>
</table>"
let private getRow (p: CarteiraAtivo) =
let cotacao = p.Cotacao |> function
| Some x -> $"{x:C}"
| _ -> "-"
$"
<tr>
<td>{p.Ativo}</td>
<td>{p.Aplicado:C}</td>
<td>{p.PrecoMedio:C}</td>
<td>{p.Quantidade}</td>
<td>{cotacao}</td>
<td>{p.Patrimonio:C}</td>
<td>{regra3Pretty p.Aplicado p.Patrimonio}</td>
<td>{p.Patrimonio - p.Aplicado:C}</td>
<td>{p.PercentValorAplicado}</td>
<td>{p.PercentValorPatrimonio}</td>
</tr>
"
let private getTable o =
$"
{getSummaryTable o}
<table>
<tr>
<th>Ativo</th>
<th>Aplicado</th>
<th>Pre. Medio</th>
<th>Qtd</th>
<th>Cotacao</th>
<th>Patrimonio</th>
<th>%% Rentab</th>
<th>Lucro</th>
<th>%% val aplicado</th>
<th>%% patrimonio</th>
</tr>
{String.Join('\n', o.Ativos |> Seq.map getRow)}
</table> {getChart o}
"
let private getSummary carteiras =
let getAnchor name = $"<td> <a href=\"#{name}\"> {name} </a> </td>"
let anchors =
carteiras
|> Seq.map (fun x -> getAnchor x.Nome)
|> (fun xs -> Seq.append xs [ getAnchor vendasAnchor ])
$"<table id=\"summary\">
<tr>
{String.Join('\n', anchors)}
</tr>
</table>
"
let private getVendas (vendas: seq<OperacaoVenda>) =
let getSummaryTable =
$"<a name=\"{vendasAnchor}\" />
<table>
<tr>
<th>Qtd Vendas</th>
<th>Qtd Ativos</th>
<th>Total Lucro</th>
</tr>
<tr>
<td>{vendas |> Seq.length}</td>
<td>{vendas |> Seq.distinctBy(fun x -> x.Ativo.TrimEnd('F')) |> Seq.length}</td>
<td>{vendas |> Seq.sumBy(fun x-> x.Lucro):C}</td>
</tr>
</table>"
let getRow p =
$"
<tr>
<td>{p.Data:``yyyy-MM-dd``}</td>
<td>{p.Ativo}</td>
<td>{p.PrecoMedio:C}</td>
<td>{p.Preco:C}</td>
<td>{p.Quantidade}</td>
<td>{p.Lucro:C}</td>
<td>{p.FinanceiroVenda:C}</td>
<td>{regra3Pretty p.PrecoMedio p.Preco}</td>
</tr>
"
$"
{getSummaryTable}
<table>
<tr>
<th>Data</th>
<th>Ativo</th>
<th>Pre. Medio</th>
<th>Preco</th>
<th>Qtd</th>
<th>Lucro</th>
<th>Valor Venda</th>
<th>%% Rentab</th>
</tr>
{String.Join('\n', vendas
|> Seq.sortByDescending (fun x -> x.Data)
|> Seq.map getRow)}
</table>
"
let foo (carteiras: seq<Carteira>) aporte =
carteiras
|> Seq.collect (fun x -> x.Ativos)
|> Seq.filter (fun x -> x.Cotacao.IsSome)
|> Seq.map (fun x ->
let y = NovoPM2 x.Quantidade x.PrecoMedio aporte x.Cotacao.Value
{| y with Ativo = x.Ativo |}
)
let private getHTML carteiras vendas title =
let print =
(fun x -> x, JsonSerializerOptions(WriteIndented = true))
>> JsonSerializer.Serialize
>> fun x -> $"<pre>{x}</pre>"
let values =
[2000M; 3000M; 4000M;]
|> Seq.collect (foo carteiras)
|> Seq.filter (fun x -> x.pmDiffP < 0)
|> Seq.sortBy (fun x -> x.pmDiffP)
|> Seq.take 40
|> Seq.toArray
$"
<!DOCTYPE html>
<html>
<head>
<title>{title}</title>
<script src=\"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js\"></script>
<script src=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/chartjs-plugin-datalabels.min.js\"></script>
{getStyle}
</head>
<body>
{getSummary carteiras}
<br />
{String.Join('\n', carteiras |> Seq.map getTable)}
<br />
{getVendas vendas}
</body>
<br/>
{print values}
</html>
"
let saveAsHTML (destinationPath: string) vendas carteiras =
let pageTitle = Path.GetFileNameWithoutExtension(destinationPath)
let pageContent = getHTML carteiras vendas pageTitle
File.WriteAllTextAsync(destinationPath, pageContent)