-
Notifications
You must be signed in to change notification settings - Fork 2
/
Report.php
474 lines (410 loc) · 16.4 KB
/
Report.php
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?php
namespace CrypTax\Models;
use CrypTax\Exceptions\InvalidYearException;
use CrypTax\ModelloRedditiTemplates\TemplatesManager;
use CrypTax\Models\Transaction;
use CrypTax\Models\TransactionsBag;
use CrypTax\Utils\DateUtils;
use CrypTax\Utils\NumberUtils;
class Report
{
const CAPITAL_GAINS_NO_TAX_AREA_THRESHOLD = 51645.69;
const CAPITAL_GAINS_TAX_RATE = 0.26;
const INTERESTS_EARNING_TAX_RATE = 0.26;
/**
* Contains the transactions parsed from the input file.
*
* @var TransactionsBag
*/
private TransactionsBag $transactionsBag;
/**
* Contains all cryptocurrencies in the report with their information.
*
* @var CryptoInfoBag
*/
private CryptoInfoBag $cryptoInfoBag;
/**
* Contains the details about the various categories of earnings.
*
* @var EarningsBag
*/
private EarningsBag $earningsBag;
/**
* Cryptocurrency purchases minus cryptocurrency sales in selected fiscal year.
*
* @var float
*/
private float $currentYearInvestment = 0.0;
/**
* Purchase cost related to sales made in the selected fiscal year.
*
* @var float
*/
public float $currentYearPurchaseCost = 0.0;
/**
* Incomes from the sale of cryptocurrencies in the selected fiscal year.
*
* @var float
*/
public float $currentYearIncome = 0.0;
/**
* Volumes for each exchange.
* es. $exchangeVolume['Binance'] is the current year volume of Binance.
*
* @var float[]
*/
private array $exchangeVolumes = [];
/**
* Interest type for each exchange/service (RL o RM)
*
* @var array
*/
private array $exchangeInterestTypes = [];
/**
* Consider earns and expenses as investment.
*
* @var bool
*/
private bool $considerEarningsAndExpensesAsInvestment = true;
/**
* There is at least an earn or expense transaction.
*
* @var bool
*/
private bool $hasEarningsOrExpenses = false;
/**
* Selected fiscal year.
*
* @var integer|null
*/
private ?int $fiscalYear = null;
public function __construct($transactionsFileContent, $exchangeInterestTypes = [], bool $considerEarningsAndExpensesAsInvestment = true) {
$this->transactionsBag = new TransactionsBag($transactionsFileContent);
$this->exchangeInterestTypes = $exchangeInterestTypes;
$this->considerEarningsAndExpensesAsInvestment = $considerEarningsAndExpensesAsInvestment;
}
/**
* Processes the transactions, calculates capital gains and earnings.
*
* @param integer $year
* @return void
*/
public function elaborateReport(int $year) {
if ($this->fiscalYear === $year) {
return;
}
$this->setFiscalYear($year);
$firstDayOfYear = DateUtils::getFirstDayOfYear($this->fiscalYear);
$lastDayOfYear = DateUtils::getLastDayOfYear($this->fiscalYear);
foreach ($this->transactionsBag->transactions AS $tx) {
if ($tx->date > $lastDayOfYear) {
// Don't elaborate transactions made after the end of the selected fiscal year.
break;
} elseif ($tx->date >= $firstDayOfYear) {
// When the first transaction of the fiscal year is encountered,
// save the start of year balance and value for each cryptocurrency.
$this->cryptoInfoBag->saveStartOfYearSnapshot();
}
if ($tx->earningCategory || $tx->type === Transaction::EXPENSE) {
$this->hasEarningsOrExpenses = true;
}
if (DateUtils::getYearFromDate($tx->date) === $this->fiscalYear) {
// Set the daily cryptocurrencies balances until reach the current transaction date.
$this->cryptoInfoBag->setBalancesUntilDay(DateUtils::getDayOfYear($tx->date));
}
if ($tx->type === Transaction::PURCHASE) {
$this->cryptoInfoBag->incrementCryptoBalance($tx->ticker, $tx->amount, $tx);
if (DateUtils::getYearFromDate($tx->date) === $this->fiscalYear) {
if ($tx->earningCategory) {
$this->earningsBag->addEarning($tx->exchange, $tx->earningCategory, EarningsBag::NR, $tx->value);
}
if (!$tx->earningCategory || $this->considerEarningsAndExpensesAsInvestment) {
$this->currentYearInvestment += $tx->value;
$this->incrementExchangeVolume($tx->exchange, $tx->value);
}
}
} elseif ($tx->type === Transaction::SALE || $tx->type === Transaction::EXPENSE) {
$this->cryptoInfoBag->decrementCryptoBalance($tx->ticker, $tx->amount, $tx);
if (DateUtils::getYearFromDate($tx->date) === $this->fiscalYear) {
$this->earningsBag->addEarning($tx->exchange, EarningsBag::CAPITAL_GAINS, null, $tx->getCapitalGain());
if ($tx->type === Transaction::SALE || $this->considerEarningsAndExpensesAsInvestment) {
$this->currentYearInvestment -= $tx->value;
$this->incrementExchangeVolume($tx->exchange, $tx->value);
}
$this->currentYearIncome += $tx->value;
$this->currentYearPurchaseCost += $tx->getRelativePurchaseCost();
foreach ($tx->getRelativePurchases() AS $purchase) {
$purchaseTx = $purchase['transaction'];
if ($purchaseTx->earningCategory) {
// this is just the profit realized by selling a crypto earning, it doesn't include capital gain
$realizedProfit = $purchaseTx->value / $purchaseTx->amount * $purchase['amount'];
if (DateUtils::getYearFromDate($purchaseTx->date) === $this->fiscalYear) {
$type = EarningsBag::RAC;
} else {
$type = EarningsBag::RAP;
}
$this->earningsBag->addEarning($purchaseTx->exchange, $purchaseTx->earningCategory, $type, $realizedProfit);
}
}
}
}
}
$this->cryptoInfoBag->saveStartOfYearSnapshot();
$this->cryptoInfoBag->setBalancesUntilDay(DateUtils::old_getNumerOfDaysInYear($this->fiscalYear) + 1);
$this->cryptoInfoBag->sortByAverageValue();
}
/**
* Get the current selected fiscal year.
*
* @return integer
*/
public function getCurrentYear() {
return $this->fiscalYear;
}
/**
* Get the first fiscal year available for the report.
*
* @return integer|null
*/
public function getFirstYear() {
$firstTransaction = $this->transactionsBag->getFirstTransaction();
if ($firstTransaction === null) {
return null;
}
return DateUtils::getYearFromDate($firstTransaction->date);
}
/**
* Get the last fiscal year available for the report.
* Actually it always return the current year, if the first fiscal year is not null.
*
* @return integer|null
*/
public function getLastYear() {
if ($this->getFirstYear() === null) {
return null;
}
return DateUtils::getCurrentYear();
}
/**
* Return true if the total holdings values has exceeded €51645.69 for at least
* 7 working days during the current fiscal year, using the prices at
* the start of the year. Art. 67, comma 1-ter TUIR.
*
* @return boolean
*/
public function get51kThresholdExceeded() {
return once(function () {
$dailyValues = $this->cryptoInfoBag->getDailyValuesStartOfYear();
$daysOverThreshold = 0;
$holidays = DateUtils::getHolidays($this->fiscalYear);
for ($i = 0; $i <= DateUtils::old_getNumerOfDaysInYear($this->fiscalYear); $i++) {
if (DateUtils::getDayOfWeek($i, $this->fiscalYear) > 0 && !in_array($i, $holidays)) {
if ($dailyValues[$i] > self::CAPITAL_GAINS_NO_TAX_AREA_THRESHOLD) {
$daysOverThreshold++;
} else {
$daysOverThreshold = 0;
}
if ($daysOverThreshold === 7) {
return true;
}
}
}
return false;
});
}
/**
* Get the exchanges trading volumes.
*
* @return integer[]
*/
public function getExchangeVolumes() {
arsort($this->exchangeVolumes);
return $this->exchangeVolumes;
}
/**
* Get the capital gains amount for the selected fiscal year.
* Airdrop are considered as capital gains.
*
* @return float
*/
public function getCapitalGains($withoutAirdrop = false) {
return $this->earningsBag->getCapitalGains() + ($withoutAirdrop ? 0.0 : $this->earningsBag->getAirdropReceived());
}
/**
* Return the capital gains tax amount for the selected fiscal year.
*
* @return float
*/
public function getCapitalGainsTax($allowNegative = false) {
if (!$this->get51kThresholdExceeded() || ($this->getCapitalGains() < 0 && !$allowNegative)) {
return 0.0;
}
return $this->getCapitalGains() * self::CAPITAL_GAINS_TAX_RATE;
}
public function getInterests($type = EarningsBag::INTEREST_RL) {
return $this->earningsBag->getInterests($type);
}
/**
* Filing the Modello Redditi is required if at least one section is required.
*
* @return boolean
*/
public function shouldFillModelloRedditi() {
return $this->shouldFillRW() || $this->shouldFillRT() || $this->shouldFillRM();
}
/**
* Filling the RW section is required if you owned cryptocurrencies in the fiscal year.
*
* @return boolean
*/
public function shouldFillRW() {
return $this->cryptoInfoBag->getTotalValues()['average_value'] > 0.01;
}
/**
* Filling the RT section is required if you have to pay taxes on capital gains.
*
* @return boolean
*/
public function shouldFillRT() {
return $this->get51kThresholdExceeded() && $this->getCapitalGains() !== 0.0;
}
/**
* Filling the RM section is required if you have earned RM-like interests.
*
* @return boolean
*/
public function shouldFillRM() {
return $this->getInterests(EarningsBag::INTEREST_RM) > 0.0;
}
/**
* Filling the RL section is required if you have earned RL-like interests.
*
* @return boolean
*/
public function shouldFillRL() {
return $this->getInterests(EarningsBag::INTEREST_RL) > 0.0;
}
/**
* Get the summary of the report.
*
* @return array
*/
public function getSummary($rawValues = false) {
return NumberUtils::recursiveFormatNumbers([
'current_year_investment' => $this->currentYearInvestment,
'current_year_purchase_cost' => $this->currentYearPurchaseCost,
'current_year_income' => $this->currentYearIncome,
'capital_gains' => $this->getCapitalGains(true),
'capital_gains_and_airdrop' => $this->getCapitalGains(),
'capital_gains_tax' => $this->getCapitalGainsTax(),
'interests_rm' => $this->earningsBag->getInterests(EarningsBag::INTEREST_RM),
'interests_rm_tax' => $this->earningsBag->getInterests(EarningsBag::INTEREST_RM) * self::INTERESTS_EARNING_TAX_RATE,
'interests_rl' => $this->earningsBag->getInterests(EarningsBag::INTEREST_RL),
'total_values' => $this->cryptoInfoBag->getTotalValues()
], 2, false, $rawValues) + [
'interests_rl_raw' => $this->earningsBag->getInterests(EarningsBag::INTEREST_RL),
'no_tax_area_threshold_exceeded' => $this->get51kThresholdExceeded(),
'should_fill_modello_redditi' => $this->shouldFillRL() || $this->shouldFillRW() || $this->shouldFillRT() || $this->shouldFillRM(),
'modello_redditi_available' => TemplatesManager::isTemplateAvailable($this->getCurrentYear()),
'should_fill_f24' => $this->shouldFillRT() || $this->shouldFillRM(),
'interest_exchanges' => array_values($this->earningsBag->getExchangeInterestList()),
'has_earnings_or_expenses' => $this->hasEarningsOrExpenses
];
}
/**
* Get the info for generate the report file.
*
* @return array
*/
public function getInfoForRender() {
return [
'fiscal_year' => $this->fiscalYear,
'summary' => $this->getSummary(),
'crypto_info' => $this->cryptoInfoBag->getInfoForRender(),
'earnings_categories' => $this->earningsBag->getCategoriesForRender(),
'days_list' => DateUtils::getListDaysInYear($this->fiscalYear),
'exchange_interest_types' => $this->earningsBag->getExchangeInterestTypes()
] + NumberUtils::recursiveFormatNumbers([
'earnings' => $this->earningsBag->getInfoForRender(),
'detailed_earnings' => $this->earningsBag->getDetailedInfoForRender(),
]) + NumberUtils::recursiveFormatNumbers([
'daily_values_start_of_year' => $this->cryptoInfoBag->getDailyValuesStartOfYear(),
'daily_values_end_of_year' => $this->cryptoInfoBag->getDailyValuesEndOfYear(),
'daily_values_real' => $this->cryptoInfoBag->getDailyValues(),
'exchange_volumes' => $this->getExchangeVolumes()
], 2, true);
}
/**
* Get the info for generate the "Modello Redditi" form.
*
* @return array
*/
public function getInfoForModelloRedditi() {
return [
'fiscal_year' => $this->fiscalYear,
'sections_required' => [
'rl' => $this->shouldFillRL(),
'rw' => $this->shouldFillRW(),
'rt' => $this->shouldFillRT(),
'rm' => $this->shouldFillRM()
]
];
}
/**
* Get the info for generate the "Modello Redditi" section RW form.
*
* @return array
*/
public function getModelloRedditiSectionRwInfo($finalValueMethod = 'average_value') {
if ($finalValueMethod === 'real_value') {
$finalValue = $this->cryptoInfoBag->getTotalValues()['value_end_of_year'];
} elseif ($finalValueMethod === 'real_value_more_incomes') {
$finalValue = $this->cryptoInfoBag->getTotalValues()['value_end_of_year'] - min($this->currentYearInvestment, 0);
} else {
// fallback to average_value
$finalValue = $this->cryptoInfoBag->getTotalValues()['average_value'];
}
return [
'initial_value' => (
$this->fiscalYear === $this->getFirstYear() ?
$this->transactionsBag->transactions[1]->value :
$this->cryptoInfoBag->getTotalValues()['value_start_of_year']
),
'final_value' => $finalValue
];
}
/**
* Set the report fiscal year and reset all the year related properties.
*
* @param integer $year
*/
private function setFiscalYear($year) {
$this->fiscalYear = intval($year);
if ($this->fiscalYear < $this->getFirstYear() || $this->fiscalYear > $this->getLastYear()) {
throw new InvalidYearException($year);
}
$this->cryptoInfoBag = new CryptoInfoBag($this->transactionsBag->transactions, $this->fiscalYear);
$this->earningsBag = new EarningsBag($this->exchangeInterestTypes);
$this->currentYearInvestment = 0.0;
$this->currentYearPurchaseCost = 0.0;
$this->currentYearIncome = 0.0;
$this->exchangeVolumes = [];
$this->hasEarningsOrExpenses = false;
\Spatie\Once\Cache::flush();
}
/**
* Increment the trading volume of an exchange.
*
* @param string $exchange
* @param float $value
* @return void
*/
private function incrementExchangeVolume($exchange, $value) {
if (strlen($exchange) === 0) {
return;
} elseif (!isset($this->exchangeVolumes[$exchange])) {
$this->exchangeVolumes[$exchange] = 0.0;
}
$this->exchangeVolumes[$exchange] += $value;
}
}