From 2c5a40eb28b05d0464c6f850f7e88ea73786efa6 Mon Sep 17 00:00:00 2001 From: Ryjek Date: Fri, 1 Apr 2022 17:32:29 +0200 Subject: [PATCH] fix dividend view --- project/src/Controller/TaxesController.php | 1 + .../src/Logic/Dividends/DividendsSummary.php | 64 ++++++++++++++++--- project/src/Services/CryptoService.php | 18 +++++- project/src/Twig/AppExtension.php | 22 +++++++ project/templates/taxes/show.html.twig | 56 ++++++++++++++-- 5 files changed, 141 insertions(+), 20 deletions(-) create mode 100644 project/src/Twig/AppExtension.php diff --git a/project/src/Controller/TaxesController.php b/project/src/Controller/TaxesController.php index a1d91a4..f6e1e25 100644 --- a/project/src/Controller/TaxesController.php +++ b/project/src/Controller/TaxesController.php @@ -110,6 +110,7 @@ class TaxesController extends AbstractController if ($gen) { return $this->render("taxes/show.html.twig", [ "taxes" => $tax->takeAllByCurrency(), + "empty" => $tax->takeAllByCurrencyEmpty() // "error" => $gen->error ]); } diff --git a/project/src/Logic/Dividends/DividendsSummary.php b/project/src/Logic/Dividends/DividendsSummary.php index 6647b9a..5d16da6 100644 --- a/project/src/Logic/Dividends/DividendsSummary.php +++ b/project/src/Logic/Dividends/DividendsSummary.php @@ -29,14 +29,19 @@ class DividendsSummary if (count($this->allData) > 0) { $sum = 0; foreach ($this->allData as $key => $item) { - $val = round($item, 2); + $val = round($item['tax'], 2); $sum += $val; $return[$key] = [ "country" => \Locale::getDisplayRegion("-".$key), - "value" => $val, - "currency" => $this->countryToCurrency[$key] + "income" => $item['incomeAll'], + "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)); } @@ -56,16 +61,36 @@ class DividendsSummary foreach ($all as $country => $item) { /** @var DividendsData $val */ foreach ($item as $val) { - if (!isset($test[$country])) $test[$country] = 0; - $test[$country] += $this->count($val); + if (!isset($test[$country])){ + $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])) { $countryToCurrency[$country] = $val->getCurrency(); } } } } - - $this->allData = array_merge($this->allData,$test); + foreach ($test as &$item) { + ksort($item['incomes']); + } + unset($item); + $this->allData = $test; $this->countryToCurrency = array_merge($this->countryToCurrency,$countryToCurrency); } @@ -74,14 +99,18 @@ class DividendsSummary 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; if ($data->getTax() > 0) { $tax = 4; } - $valueTax = round($value * $tax / 100); + if ($fromDestiny) { + $tax = $data->getTax(); + } + + $valueTax = round($value * $tax / 100,5); $rate = $this->getRate($data); if ($rate > 0) { return $valueTax * $rate; @@ -90,6 +119,21 @@ class DividendsSummary 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 */ diff --git a/project/src/Services/CryptoService.php b/project/src/Services/CryptoService.php index 54529f4..5702ff4 100644 --- a/project/src/Services/CryptoService.php +++ b/project/src/Services/CryptoService.php @@ -43,9 +43,8 @@ class CryptoService extends MasterServices $sum = 0; foreach ($this->transactions as $key => $transaction) { $tax = $transaction['income'] - $transaction['cost']; - $taxLoss = 0; + $profit = $tax; if ($tax < 0) { - $taxLoss = $tax; $tax = 0; } else { $tax = round($tax * 0.19,2); @@ -55,7 +54,7 @@ class CryptoService extends MasterServices // "value" => round($transaction,4), "currency" => "", "tax" => $tax, - "taxLoss" => $taxLoss + "profit" => $profit ]; $return[$key] = array_merge($return[$key], $transaction); if ($tax > 0) { @@ -67,5 +66,18 @@ class CryptoService extends MasterServices // dd($return, $this->transactionsError); 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 \ No newline at end of file diff --git a/project/src/Twig/AppExtension.php b/project/src/Twig/AppExtension.php new file mode 100644 index 0000000..7269a51 --- /dev/null +++ b/project/src/Twig/AppExtension.php @@ -0,0 +1,22 @@ +L.p. Country Income - Cost + {% if taxes|first.cost is defined %} + Cost + {% endif %} + {% if taxes|first.destiny_tax is defined %} + Origin tax + {% endif %} + {% if taxes|first.tax_cost is defined %} + Deductable tax (15%) + {% endif %} + {% if taxes|first.profit is defined %} + Profit/Loss + {% endif %} Tax {% for tax in taxes %} {% if tax.country is defined %} + {% set colspan = tax|length %} {% if tax.income > 0 %} - + {{ loop.index }} {{ tax.country }} - {{ tax.income }}zł - {{ tax.cost }}zł - {% if tax.taxLoss < 0 %} {{ tax.taxLoss }} {% else %} {{ tax.tax }} zł{% endif %} + {{ tax.income|price }} + {% if tax.cost is defined %} + {{ tax.cost|price }} + {% endif %} + {% if tax.destiny_tax is defined %} + {{ tax.destiny_tax|price }} + {% endif %} + {% if tax.tax_cost is defined %} + {{ tax.tax_cost|price }} + {% endif %} + {% if tax.profit is defined %} + {{ tax.profit|price }} + {% endif %} + {{ tax.tax|price }} + {% if tax.incomesArray is defined and tax.incomesArray|length > 0 %} + {% set colspan = colspan-1 %} + {% for incomePerc,value in tax.incomesArray %} + + # + + {{ incomePerc }}% - {{ value|price }} + + + {% endfor %} + {% endif %} {% endif %} {% else %} - Summary: - {{ tax }}zł + Summary: + {{ tax|price("PLN",0) }} {% endif %} {% endfor %} @@ -40,4 +74,12 @@ {% endif %} + +{# {% if empty %}#} +{#
#} +{# {% for item in empty %}#} +{#
{{ item }}
#} +{# {% endfor %}#} +{#
#} +{# {% endif %}#} {% endblock %} \ No newline at end of file