From 7e55e5097f6a62049e25e288308b5e3f00bf2926 Mon Sep 17 00:00:00 2001 From: Ryjek Date: Fri, 1 Apr 2022 12:26:34 +0200 Subject: [PATCH] better counting --- project/src/Helper/FIFO/Crypto.php | 26 +++++++++----- project/src/Helper/FIFO/Main.php | 9 +++-- project/src/Logic/Crypto/MainObject.php | 45 ++++++++++++++++++++----- project/src/Services/CryptoService.php | 19 ++++++++--- project/templates/taxes/show.html.twig | 30 +++++++++++------ 5 files changed, 94 insertions(+), 35 deletions(-) diff --git a/project/src/Helper/FIFO/Crypto.php b/project/src/Helper/FIFO/Crypto.php index 5935151..c72deb4 100644 --- a/project/src/Helper/FIFO/Crypto.php +++ b/project/src/Helper/FIFO/Crypto.php @@ -25,8 +25,8 @@ class Crypto extends Main if ($item->getValue() == 0) { continue; } - if ($item->getCurrency() === "BTC") - dd($item,$item->getValueCrypto()); +// if ($item->getCurrency() === "BTC") +// dd($item,$item->getValueCrypto()); // if (!isset($sum[$item->getCurrency()])) { // $sum[$item->getCurrency()] = []; // if (!isset($sum[$item->getCurrency()][$item->getDateTime()->format("Y-m-d")])){ @@ -83,7 +83,6 @@ class Crypto extends Main 'in' => [], 'out' =>[] ]; - /** @var MainObject $item */ foreach ($transactions['transactions'] as $currency => $transaction) { foreach ($transaction as $date => $item) { @@ -124,7 +123,6 @@ class Crypto extends Main $arr = []; foreach ($tmp["in"] as $currency => $items) { foreach ($items as $item) { - $item->takePrice(); $arr[$currency] = $this->countTax($item, $tmp['out'][$currency]); } } @@ -136,9 +134,9 @@ class Crypto extends Main return $this->notCount; } - private function countTax(MainObject $mainObject, array $buys) + private function countTax(MainObject $mainObject, array $buys) : array { - $needCount = $mainObject->getValue(); + $needCount = abs($mainObject->getValue()); $priceToTax = 0; /** @var MainObject $buy */ foreach ($buys as $buy) { @@ -154,14 +152,24 @@ class Crypto extends Main } if ($needCount - $buy->getValue() > 0) { $needCount -= $buy->getValue(); - $priceToTax += $buy->getRealValue(); + $priceToTax += abs($buy->getRealValue()/100); + $buy->setValue(0); } else { $newVal = $buy->getValue() - $needCount; - $priceToTax += $buy->getPrice() * $needCount; + $priceToTax += round(abs($buy->getPrice() * $needCount / MainObject::_CRYPTO_DIVIDER),4); $needCount = 0; $buy->setValue($newVal); } } - return $mainObject->getRealValue() + $priceToTax; + $return = [ + 'income' => 0, + 'cost' => 0, + ]; + if ($needCount > 0) { + return $return; + } + $return['income'] = $mainObject->getRealValue() / 100; + $return['cost'] = round($priceToTax,2); + return $return; } } \ No newline at end of file diff --git a/project/src/Helper/FIFO/Main.php b/project/src/Helper/FIFO/Main.php index b51b90a..a3e0f44 100644 --- a/project/src/Helper/FIFO/Main.php +++ b/project/src/Helper/FIFO/Main.php @@ -41,15 +41,18 @@ class Main foreach ($names as $key => $name) { if ($name === "setValue") { $data = "$datum[$key]"; - $data = str_replace(",","",$data); - $data = str_replace(".","",$data); - $datum[$key] = $data; + if (strpos($data,",") !== false || strpos($data,".") !== false) { + $data = str_replace(",",".",$data); + } + $realInt = floatval($data)*MainObject::_CRYPTO_DIVIDER; + $datum[$key] = intval($realInt); } if ($name === "setDateTime") { $datum[$key] = new \DateTime($datum[$key]); } $obj->$name($datum[$key]); } + $obj->checkCurrency(); $tmp[] = $obj; } return $tmp; diff --git a/project/src/Logic/Crypto/MainObject.php b/project/src/Logic/Crypto/MainObject.php index d34f9b8..cfa1773 100644 --- a/project/src/Logic/Crypto/MainObject.php +++ b/project/src/Logic/Crypto/MainObject.php @@ -2,17 +2,20 @@ declare(strict_types=1); namespace App\Logic\Crypto; +use App\Helper\Currency; use phpDocumentor\Reflection\DocBlock\Tags\Var_; class MainObject { + const _CRYPTO_DIVIDER = 10000000; private \DateTime $dateTime; private string $desc; - private string $value; + private int $value; private string $currency; private ?string $realCurrency=null; - private float $realValue=0; + private int $realValue=0; private float $price=0; + private bool $isCrypto = false; /** * @return float @@ -23,9 +26,9 @@ class MainObject } /** - * @param float $realValue + * @param int $realValue */ - public function setRealValue(float $realValue): void + public function setRealValue(int $realValue): void { $this->realValue = $realValue; } @@ -81,7 +84,7 @@ class MainObject /** * @return int */ - public function getValue(): string + public function getValue(): int { return $this->value; } @@ -89,7 +92,7 @@ class MainObject /** * @param int $value */ - public function setValue(string $value): void + public function setValue(int $value): void { $this->value = $value; } @@ -107,6 +110,9 @@ class MainObject */ public function setCurrency(string $currency): void { + if (!in_array($currency, Currency::$currencies)) { + $this->isCrypto = true; + } $this->currency = $currency; } @@ -125,7 +131,18 @@ class MainObject public function takePrice() : void { - $this->price = floatval(abs(round($this->realValue / $this->getValue(),4))); + $real = $this->getRealValue() / 100; + $val = $this->getValue(); + if ($this->isCrypto) { + $val = $this->getValueCrypto(); + } + $price = round($real/$val,8); +// if ($this->isCrypto) { +// $price *= self::_CRYPTO_DIVIDER; +// } else { +// $price *= 100; +// } + $this->price = $price; } public function getPrice() : float @@ -135,6 +152,18 @@ class MainObject public function getValueCrypto() : float { - return intval($this->value) / 100000000; + return $this->value / self::_CRYPTO_DIVIDER; + } + + public function subtract(MainObject $mainObject) + { + $this->value = $this->getValue() - $mainObject->getValue(); + } + + public function checkCurrency() : void + { + if (!$this->isCrypto) { + $this->value = intval($this->value/self::_CRYPTO_DIVIDER*100); + } } } \ No newline at end of file diff --git a/project/src/Services/CryptoService.php b/project/src/Services/CryptoService.php index eb310f3..54529f4 100644 --- a/project/src/Services/CryptoService.php +++ b/project/src/Services/CryptoService.php @@ -42,13 +42,24 @@ class CryptoService extends MasterServices $return = []; $sum = 0; foreach ($this->transactions as $key => $transaction) { + $tax = $transaction['income'] - $transaction['cost']; + $taxLoss = 0; + if ($tax < 0) { + $taxLoss = $tax; + $tax = 0; + } else { + $tax = round($tax * 0.19,2); + } $return[$key] = [ "country" => $key, - "value" => round($transaction,4), - "currency" => "" +// "value" => round($transaction,4), + "currency" => "", + "tax" => $tax, + "taxLoss" => $taxLoss ]; - if ($transaction > 0) { - $sum += round($transaction,4); + $return[$key] = array_merge($return[$key], $transaction); + if ($tax > 0) { + $sum += $tax; } } $return['sum'] = intval(ceil($sum)); diff --git a/project/templates/taxes/show.html.twig b/project/templates/taxes/show.html.twig index c61f388..73efb17 100644 --- a/project/templates/taxes/show.html.twig +++ b/project/templates/taxes/show.html.twig @@ -3,7 +3,7 @@ {% block title %}Tax summary{% endblock %} {% block body %} -
+
{% if taxes %} @@ -11,22 +11,30 @@ - + + + - {% for tax in taxes %} - - {% if tax.country is defined %} + {% for tax in taxes %} + {% if tax.country is defined %} + {% if tax.income > 0 %} + - - {% else %} - - - {% endif %} + + + + + {% endif %} + {% else %} + + + - {% endfor %} + {% endif %} + {% endfor %}
L.p. CountryValueIncomeCostTax
{{ loop.index }} {{ tax.country }}{{ tax.value }}złSummary:{{ tax }}zł{{ tax.income }}zł{{ tax.cost }}zł{% if tax.taxLoss < 0 %} {{ tax.taxLoss }} {% else %} {{ tax.tax }} zł{% endif %}
Summary:{{ tax }}zł
{% endif %}