better counting

This commit is contained in:
2022-04-01 12:26:34 +02:00
parent 6b005cb5b6
commit 7e55e5097f
5 changed files with 94 additions and 35 deletions
+17 -9
View File
@@ -25,8 +25,8 @@ class Crypto extends Main
if ($item->getValue() == 0) { if ($item->getValue() == 0) {
continue; continue;
} }
if ($item->getCurrency() === "BTC") // if ($item->getCurrency() === "BTC")
dd($item,$item->getValueCrypto()); // dd($item,$item->getValueCrypto());
// if (!isset($sum[$item->getCurrency()])) { // if (!isset($sum[$item->getCurrency()])) {
// $sum[$item->getCurrency()] = []; // $sum[$item->getCurrency()] = [];
// if (!isset($sum[$item->getCurrency()][$item->getDateTime()->format("Y-m-d")])){ // if (!isset($sum[$item->getCurrency()][$item->getDateTime()->format("Y-m-d")])){
@@ -83,7 +83,6 @@ class Crypto extends Main
'in' => [], 'in' => [],
'out' =>[] 'out' =>[]
]; ];
/** @var MainObject $item */ /** @var MainObject $item */
foreach ($transactions['transactions'] as $currency => $transaction) { foreach ($transactions['transactions'] as $currency => $transaction) {
foreach ($transaction as $date => $item) { foreach ($transaction as $date => $item) {
@@ -124,7 +123,6 @@ class Crypto extends Main
$arr = []; $arr = [];
foreach ($tmp["in"] as $currency => $items) { foreach ($tmp["in"] as $currency => $items) {
foreach ($items as $item) { foreach ($items as $item) {
$item->takePrice();
$arr[$currency] = $this->countTax($item, $tmp['out'][$currency]); $arr[$currency] = $this->countTax($item, $tmp['out'][$currency]);
} }
} }
@@ -136,9 +134,9 @@ class Crypto extends Main
return $this->notCount; 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; $priceToTax = 0;
/** @var MainObject $buy */ /** @var MainObject $buy */
foreach ($buys as $buy) { foreach ($buys as $buy) {
@@ -154,14 +152,24 @@ class Crypto extends Main
} }
if ($needCount - $buy->getValue() > 0) { if ($needCount - $buy->getValue() > 0) {
$needCount -= $buy->getValue(); $needCount -= $buy->getValue();
$priceToTax += $buy->getRealValue(); $priceToTax += abs($buy->getRealValue()/100);
$buy->setValue(0);
} else { } else {
$newVal = $buy->getValue() - $needCount; $newVal = $buy->getValue() - $needCount;
$priceToTax += $buy->getPrice() * $needCount; $priceToTax += round(abs($buy->getPrice() * $needCount / MainObject::_CRYPTO_DIVIDER),4);
$needCount = 0; $needCount = 0;
$buy->setValue($newVal); $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;
} }
} }
+6 -3
View File
@@ -41,15 +41,18 @@ class Main
foreach ($names as $key => $name) { foreach ($names as $key => $name) {
if ($name === "setValue") { if ($name === "setValue") {
$data = "$datum[$key]"; $data = "$datum[$key]";
$data = str_replace(",","",$data); if (strpos($data,",") !== false || strpos($data,".") !== false) {
$data = str_replace(".","",$data); $data = str_replace(",",".",$data);
$datum[$key] = $data; }
$realInt = floatval($data)*MainObject::_CRYPTO_DIVIDER;
$datum[$key] = intval($realInt);
} }
if ($name === "setDateTime") { if ($name === "setDateTime") {
$datum[$key] = new \DateTime($datum[$key]); $datum[$key] = new \DateTime($datum[$key]);
} }
$obj->$name($datum[$key]); $obj->$name($datum[$key]);
} }
$obj->checkCurrency();
$tmp[] = $obj; $tmp[] = $obj;
} }
return $tmp; return $tmp;
+37 -8
View File
@@ -2,17 +2,20 @@
declare(strict_types=1); declare(strict_types=1);
namespace App\Logic\Crypto; namespace App\Logic\Crypto;
use App\Helper\Currency;
use phpDocumentor\Reflection\DocBlock\Tags\Var_; use phpDocumentor\Reflection\DocBlock\Tags\Var_;
class MainObject class MainObject
{ {
const _CRYPTO_DIVIDER = 10000000;
private \DateTime $dateTime; private \DateTime $dateTime;
private string $desc; private string $desc;
private string $value; private int $value;
private string $currency; private string $currency;
private ?string $realCurrency=null; private ?string $realCurrency=null;
private float $realValue=0; private int $realValue=0;
private float $price=0; private float $price=0;
private bool $isCrypto = false;
/** /**
* @return float * @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; $this->realValue = $realValue;
} }
@@ -81,7 +84,7 @@ class MainObject
/** /**
* @return int * @return int
*/ */
public function getValue(): string public function getValue(): int
{ {
return $this->value; return $this->value;
} }
@@ -89,7 +92,7 @@ class MainObject
/** /**
* @param int $value * @param int $value
*/ */
public function setValue(string $value): void public function setValue(int $value): void
{ {
$this->value = $value; $this->value = $value;
} }
@@ -107,6 +110,9 @@ class MainObject
*/ */
public function setCurrency(string $currency): void public function setCurrency(string $currency): void
{ {
if (!in_array($currency, Currency::$currencies)) {
$this->isCrypto = true;
}
$this->currency = $currency; $this->currency = $currency;
} }
@@ -125,7 +131,18 @@ class MainObject
public function takePrice() : void 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 public function getPrice() : float
@@ -135,6 +152,18 @@ class MainObject
public function getValueCrypto() : float 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);
}
} }
} }
+15 -4
View File
@@ -42,13 +42,24 @@ class CryptoService extends MasterServices
$return = []; $return = [];
$sum = 0; $sum = 0;
foreach ($this->transactions as $key => $transaction) { 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] = [ $return[$key] = [
"country" => $key, "country" => $key,
"value" => round($transaction,4), // "value" => round($transaction,4),
"currency" => "" "currency" => "",
"tax" => $tax,
"taxLoss" => $taxLoss
]; ];
if ($transaction > 0) { $return[$key] = array_merge($return[$key], $transaction);
$sum += round($transaction,4); if ($tax > 0) {
$sum += $tax;
} }
} }
$return['sum'] = intval(ceil($sum)); $return['sum'] = intval(ceil($sum));
+16 -8
View File
@@ -3,7 +3,7 @@
{% block title %}Tax summary{% endblock %} {% block title %}Tax summary{% endblock %}
{% block body %} {% block body %}
<div class="row justify-content-md-center" style="margin-top: 150px;"> <div class="row justify-content-md-center" >
<div class="col col-md-6"> <div class="col col-md-6">
{% if taxes %} {% if taxes %}
<table class="table table-dark table-striped"> <table class="table table-dark table-striped">
@@ -11,21 +11,29 @@
<tr> <tr>
<th>L.p.</th> <th>L.p.</th>
<th>Country</th> <th>Country</th>
<th>Value</th> <th>Income</th>
<th>Cost</th>
<th>Tax</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for tax in taxes %} {% for tax in taxes %}
<tr {% if tax.country is not defined %}class="table-success"{% endif %}>
{% if tax.country is defined %} {% if tax.country is defined %}
{% if tax.income > 0 %}
<tr class="{% if tax.taxLoss < 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.value }}zł</td> <td>{{ tax.income }}zł</td>
{% else %} <td>{{ tax.cost }}zł</td>
<td colspan="2">Summary:</td> <td>{% if tax.taxLoss < 0 %} {{ tax.taxLoss }} {% else %} {{ tax.tax }}{% endif %}</td>
<td>{{ tax }}zł</td>
{% endif %}
</tr> </tr>
{% endif %}
{% else %}
<tr class="">
<td colspan="4">Summary:</td>
<td>{{ tax }}zł</td>
</tr>
{% endif %}
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>