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) {
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;
}
}
+6 -3
View File
@@ -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;
+37 -8
View File
@@ -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);
}
}
}
+15 -4
View File
@@ -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));
+16 -8
View File
@@ -3,7 +3,7 @@
{% block title %}Tax summary{% endblock %}
{% 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">
{% if taxes %}
<table class="table table-dark table-striped">
@@ -11,21 +11,29 @@
<tr>
<th>L.p.</th>
<th>Country</th>
<th>Value</th>
<th>Income</th>
<th>Cost</th>
<th>Tax</th>
</tr>
</thead>
<tbody>
{% for tax in taxes %}
<tr {% if tax.country is not defined %}class="table-success"{% endif %}>
{% 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>{{ tax.country }}</td>
<td>{{ tax.value }}zł</td>
{% else %}
<td colspan="2">Summary:</td>
<td>{{ tax }}zł</td>
{% endif %}
<td>{{ tax.income }}zł</td>
<td>{{ tax.cost }}zł</td>
<td>{% if tax.taxLoss < 0 %} {{ tax.taxLoss }} {% else %} {{ tax.tax }}{% endif %}</td>
</tr>
{% endif %}
{% else %}
<tr class="">
<td colspan="4">Summary:</td>
<td>{{ tax }}zł</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>