fix dividend view

This commit is contained in:
2022-04-01 17:32:29 +02:00
parent 7e55e5097f
commit 2c5a40eb28
5 changed files with 141 additions and 20 deletions
@@ -110,6 +110,7 @@ class TaxesController extends AbstractController
if ($gen) { if ($gen) {
return $this->render("taxes/show.html.twig", [ return $this->render("taxes/show.html.twig", [
"taxes" => $tax->takeAllByCurrency(), "taxes" => $tax->takeAllByCurrency(),
"empty" => $tax->takeAllByCurrencyEmpty()
// "error" => $gen->error // "error" => $gen->error
]); ]);
} }
@@ -29,14 +29,19 @@ class DividendsSummary
if (count($this->allData) > 0) { if (count($this->allData) > 0) {
$sum = 0; $sum = 0;
foreach ($this->allData as $key => $item) { foreach ($this->allData as $key => $item) {
$val = round($item, 2); $val = round($item['tax'], 2);
$sum += $val; $sum += $val;
$return[$key] = [ $return[$key] = [
"country" => \Locale::getDisplayRegion("-".$key), "country" => \Locale::getDisplayRegion("-".$key),
"value" => $val, "income" => $item['incomeAll'],
"currency" => $this->countryToCurrency[$key] "currency" => $this->countryToCurrency[$key],
"tax" => $item['tax'],
"destiny_tax" => $item['destiny_tax'],
"tax_cost" => $item['tax_cost'],
"incomesArray" => $item['incomes']
]; ];
} }
// >
$return['sum'] = intval(ceil($sum)); $return['sum'] = intval(ceil($sum));
} }
@@ -56,16 +61,36 @@ class DividendsSummary
foreach ($all as $country => $item) { foreach ($all as $country => $item) {
/** @var DividendsData $val */ /** @var DividendsData $val */
foreach ($item as $val) { foreach ($item as $val) {
if (!isset($test[$country])) $test[$country] = 0; if (!isset($test[$country])){
$test[$country] += $this->count($val); $test[$country] = [
"incomeAll" => 0,
"tax" => 0,
"destiny_tax" => 0,
"tax_cost" => 0,
"incomes" => []
];
}
if (!isset($test[$country]['incomes'][$val->getTax()])) {
$test[$country]['incomes'][$val->getTax()] = 0;
}
$income = round($val->getCount()*$val->getValue()*$this->getRate($val),5);
$test[$country]['incomeAll'] += $income;
$test[$country]['tax'] += round($this->count($val),5);
$test[$country]['destiny_tax'] += round($this->count($val,true),5);
$test[$country]['tax_cost'] += round($this->countTaxPrice($val),5);
$test[$country]['incomes'][$val->getTax()] += $income;
if (!isset($countryToCurrency[$country])) { if (!isset($countryToCurrency[$country])) {
$countryToCurrency[$country] = $val->getCurrency(); $countryToCurrency[$country] = $val->getCurrency();
} }
} }
} }
} }
foreach ($test as &$item) {
$this->allData = array_merge($this->allData,$test); ksort($item['incomes']);
}
unset($item);
$this->allData = $test;
$this->countryToCurrency = array_merge($this->countryToCurrency,$countryToCurrency); $this->countryToCurrency = array_merge($this->countryToCurrency,$countryToCurrency);
} }
@@ -74,14 +99,18 @@ class DividendsSummary
return $this->currencyObj->getRate($data->getCurrency(),$data->getDateTime(),$this->error); return $this->currencyObj->getRate($data->getCurrency(),$data->getDateTime(),$this->error);
} }
private function count(DividendsData $data) : float private function count(DividendsData $data, bool $fromDestiny = false) : float
{ {
$value = round($data->getCount()*$data->getValue(),2); $value = round($data->getCount()*$data->getValue(),5);
$tax = 19; $tax = 19;
if ($data->getTax() > 0) { if ($data->getTax() > 0) {
$tax = 4; $tax = 4;
} }
$valueTax = round($value * $tax / 100); if ($fromDestiny) {
$tax = $data->getTax();
}
$valueTax = round($value * $tax / 100,5);
$rate = $this->getRate($data); $rate = $this->getRate($data);
if ($rate > 0) { if ($rate > 0) {
return $valueTax * $rate; return $valueTax * $rate;
@@ -90,6 +119,21 @@ class DividendsSummary
return 0; return 0;
} }
private function countTaxPrice(DividendsData $data) : float
{
if ($data->getTax() == 0) {
return 0;
}
$value = round($data->getCount()*$data->getValue(),5);
$tax = min($data->getTax(),15);
$valueTax = round($value * $tax / 100,5);
$rate = $this->getRate($data);
if ($rate > 0) {
return $valueTax * $rate;
}
return 0;
}
/** /**
* @param SheetData $sheetData * @param SheetData $sheetData
*/ */
+15 -3
View File
@@ -43,9 +43,8 @@ class CryptoService extends MasterServices
$sum = 0; $sum = 0;
foreach ($this->transactions as $key => $transaction) { foreach ($this->transactions as $key => $transaction) {
$tax = $transaction['income'] - $transaction['cost']; $tax = $transaction['income'] - $transaction['cost'];
$taxLoss = 0; $profit = $tax;
if ($tax < 0) { if ($tax < 0) {
$taxLoss = $tax;
$tax = 0; $tax = 0;
} else { } else {
$tax = round($tax * 0.19,2); $tax = round($tax * 0.19,2);
@@ -55,7 +54,7 @@ class CryptoService extends MasterServices
// "value" => round($transaction,4), // "value" => round($transaction,4),
"currency" => "", "currency" => "",
"tax" => $tax, "tax" => $tax,
"taxLoss" => $taxLoss "profit" => $profit
]; ];
$return[$key] = array_merge($return[$key], $transaction); $return[$key] = array_merge($return[$key], $transaction);
if ($tax > 0) { if ($tax > 0) {
@@ -67,5 +66,18 @@ class CryptoService extends MasterServices
// dd($return, $this->transactionsError); // dd($return, $this->transactionsError);
return $return; return $return;
} }
public function takeAllByCurrencyEmpty() : array
{
$return = [];
foreach ($this->transactions as $key => $transaction) {
if ($transaction['income'] === 0) {
$return[] = $key;
}
}
return $return;
}
} }
//LML, EXY, AMLT, XRP, XLM, OMG, ETH, LTC //LML, EXY, AMLT, XRP, XLM, OMG, ETH, LTC
+22
View File
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class AppExtension extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter("price", [$this, 'formatPrice'])
];
}
public function formatPrice(float $number, string $currency = "PLN", int $decimal = 2): string
{
return number_format($number, $decimal, ",", " ")." $currency";
}
}
+48 -6
View File
@@ -12,26 +12,60 @@
<th>L.p.</th> <th>L.p.</th>
<th>Country</th> <th>Country</th>
<th>Income</th> <th>Income</th>
{% if taxes|first.cost is defined %}
<th>Cost</th> <th>Cost</th>
{% endif %}
{% if taxes|first.destiny_tax is defined %}
<th>Origin tax</th>
{% endif %}
{% if taxes|first.tax_cost is defined %}
<th>Deductable tax (15%)</th>
{% endif %}
{% if taxes|first.profit is defined %}
<th>Profit/Loss</th>
{% endif %}
<th>Tax</th> <th>Tax</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for tax in taxes %} {% for tax in taxes %}
{% if tax.country is defined %} {% if tax.country is defined %}
{% set colspan = tax|length %}
{% if tax.income > 0 %} {% if tax.income > 0 %}
<tr class="{% if tax.taxLoss < 0 %}table-danger{% else %}table-success{% endif %}"> <tr class="{% if tax.profit is defined and tax.profit < 0 %}table-danger{% else %}table-success{% endif %}">
<td>{{ loop.index }}</td> <td>{{ loop.index }}</td>
<td>{{ tax.country }}</td> <td>{{ tax.country }}</td>
<td>{{ tax.income }}</td> <td>{{ tax.income|price }}</td>
<td>{{ tax.cost }}zł</td> {% if tax.cost is defined %}
<td>{% if tax.taxLoss < 0 %} {{ tax.taxLoss }} {% else %} {{ tax.tax }}{% endif %}</td> <td>{{ tax.cost|price }}</td>
{% endif %}
{% if tax.destiny_tax is defined %}
<td>{{ tax.destiny_tax|price }}</td>
{% endif %}
{% if tax.tax_cost is defined %}
<td>{{ tax.tax_cost|price }}</td>
{% endif %}
{% if tax.profit is defined %}
<td>{{ tax.profit|price }}</td>
{% endif %}
<td>{{ tax.tax|price }}</td>
</tr> </tr>
{% if tax.incomesArray is defined and tax.incomesArray|length > 0 %}
{% set colspan = colspan-1 %}
{% for incomePerc,value in tax.incomesArray %}
<tr>
<td>#</td>
<td></td>
<td>{{ incomePerc }}% - {{ value|price }}</td>
<td colspan="3"></td>
</tr>
{% endfor %}
{% endif %}
{% endif %} {% endif %}
{% else %} {% else %}
<tr class=""> <tr class="">
<td colspan="4">Summary:</td> <td colspan="{{ colspan-1 }}">Summary:</td>
<td>{{ tax }}</td> <td>{{ tax|price("PLN",0) }}</td>
</tr> </tr>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
@@ -40,4 +74,12 @@
{% endif %} {% endif %}
</div> </div>
</div> </div>
{# {% if empty %}#}
{# <div class="col col-md-6">#}
{# {% for item in empty %}#}
{# <div>{{ item }}</div>#}
{# {% endfor %}#}
{# </div>#}
{# {% endif %}#}
{% endblock %} {% endblock %}