This commit is contained in:
2022-04-20 15:11:37 +02:00
parent 2c5a40eb28
commit 90952d883e
11 changed files with 348 additions and 85 deletions
+2 -2
View File
@@ -110,8 +110,8 @@ class TaxesController extends AbstractController
if ($gen) {
return $this->render("taxes/show.html.twig", [
"taxes" => $tax->takeAllByCurrency(),
"empty" => $tax->takeAllByCurrencyEmpty()
// "error" => $gen->error
"empty" => $tax->takeAllByCurrencyEmpty(),
"error" => $tax->takeError()
]);
}
+2 -1
View File
@@ -7,7 +7,8 @@ use Doctrine\ORM\EntityManager;
class Currency
{
public static array $currencies = ["USD","GBP","PLN"];
public static array $currencies = ["USD","GBP","PLN","EUR"];
public static array $currenciesCrypto = ["USDT"];
private static string $apiUrl = "https://api.nbp.pl/api/exchangerates/rates/a/:code/:date/?format=json";
private EntityManager $manager;
private array $ratesPerDate;
+7 -3
View File
@@ -7,10 +7,14 @@ use App\Logic\Crypto\MainObject;
final class CryptoValues
{
private static array $values = [
"values" => ["Wartość","Values"],
"values" => ["Wartość","Values","Amount"],
"currency" => ["Currency", "Waluta"],
"date" => ["Data operacji"],
"desc" => ["Rodzaj"]
"date" => ["Data operacji","Date(UTC)"],
"desc" => ["Rodzaj"],
"price" => ["Price"],
"transactionType" => ["Type"],
"market" => ["Market"],
"realValue" => ["Total"]
];
public static function isValue(string $type, string $text) : bool
+103 -10
View File
@@ -17,9 +17,7 @@ class Crypto extends Main
if (!$all || count($all) < 1) {
return [];
}
$sum = [
];
$sum = [];
/** @var MainObject $item */
foreach ($all as $item) {
if ($item->getValue() == 0) {
@@ -44,14 +42,15 @@ class Crypto extends Main
$sum[$item->getCurrency()][$date][] = $item;
}
$test=[];
$realCurrency = [];
foreach ($sum as $currency => $itemsAll) {
foreach ($itemsAll as $date => $i) {
if (count($i) > 1) {
$realObj = $i[0];
$realObj->setValue($i[1]->getValue() + $realObj->getValue());
$realObj = array_pop($i);
foreach ($i as $item) {
$realObj->setValue($item->getValue() + $realObj->getValue());
}
$sum[$currency][$date] = $realObj;
} else {
$sum[$currency][$date] = $i[0];
@@ -59,7 +58,7 @@ class Crypto extends Main
}
}
foreach ($sum as $currency => $items) {
if (in_array($currency, Currency::$currencies)) {
if (in_array($currency, Currency::$currencies) || in_array($currency,Currency::$currenciesCrypto)) {
if (!isset($realCurrency[$currency])) {
$realCurrency[$currency] = [];
}
@@ -70,6 +69,19 @@ class Crypto extends Main
foreach ($keyToDel as $item) {
unset($sum[$item]);
}
foreach ($realCurrency as $currency => $i) {
if (in_array($currency,Currency::$currenciesCrypto)) {
$curr=null;
foreach ($i as $item) {
if (!($curr instanceof MainObject)) {
$curr = $realCurrency[$item->getRealCurrency()][array_key_first($realCurrency[$item->getRealCurrency()])];
}
$item->setRealCurrency("PLN");
$item->setPrice(round($curr->getPrice()/$item->getPrice(),4));
$item->setRealValue(intval(round(($item->getValue()/100)*$item->getPrice()*100)));
}
}
}
return [
"realCurrency" => $realCurrency,
"transactions" => $sum
@@ -86,18 +98,28 @@ class Crypto extends Main
/** @var MainObject $item */
foreach ($transactions['transactions'] as $currency => $transaction) {
foreach ($transaction as $date => $item) {
$date = $item->getDateTime()->format("Y-m-d H:i");
$yearTrans = intval($item->getDateTime()->format("Y"));
if (isset($transactions['realCurrency']['PLN'][$date])) {
$obj = $transactions['realCurrency']['PLN'][$date];
$item->setRealValue($obj->getValue());
$item->setRealCurrency($obj->getCurrency());
// unset($transactions['realCurrency']['PLN'][$date]);
}
if (in_array($item->getRealCurrency(), Currency::$currenciesCrypto) && isset($transactions['realCurrency'][$item->getRealCurrency()])) {
$this->changeValue($item, $transactions['realCurrency'][$item->getRealCurrency()]);
}
if ($currency === "LML" && $yearTrans == "2020") {
// dd($item,$transactions['realCurrency']['PLN']);
}
if ($item->getRealValue() != 0) {
if (!isset($tmp['in'][$currency])) {
$tmp['in'][$currency] = [];
}
if (!isset($tmp['out'][$currency])) {
$tmp['out'][$currency] = [];
}
$date = $item->getDateTime()->format("Y-m-d H:i");
if ($item->getRealValue() > 0) {
if ($yearTrans === intval(date("Y"))-1 && ($item->getTransactionType() !== "" && $item->getTransactionType() !== "BUY") || $item->getRealValue()>0) {
$tmp['in'][$currency][$date] = $item;
} else {
$tmp['out'][$currency][$date] = $item;
@@ -105,11 +127,13 @@ class Crypto extends Main
}
}
}
// dd($tmp['out']['LML']);
foreach ($tmp as &$inout) {
foreach ($inout as $currency => &$items) {
if (count($items) > 0) {
ksort($items,2);
foreach ($items as $item) {
$item->currencyConversion();
$item->takePrice();
}
}
@@ -120,12 +144,35 @@ class Crypto extends Main
if (count($tmp["in"]) === 0) {
return [];
}
/** @var MainObject $i */
foreach ($tmp as $type => $value) {
foreach ($value as $cur => $item) {
if (in_array($cur, Currency::$currenciesCrypto) && count($item) > 0) {
foreach ($item as $i) {
if ($i->getRealCurrency() === "PLN") {
continue;
}
$real = $transactions['realCurrency'][$i->getRealCurrency()][array_key_first($transactions['realCurrency'][$i->getRealCurrency()])];
$i->setRealCurrency($real->getRealCurrency());
$newVal = $i->getRealValue()/100 * $real->getPrice();
$newVal = intval(round($newVal * 100));
$i->setRealValue($newVal);
$newPrice = $i->getPrice() * $real->getPrice();
$newPrice = intval(round($newPrice * 100));
$i->setPrice($newPrice);
}
}
}
}
$arr = [];
foreach ($tmp["in"] as $currency => $items) {
foreach ($items as $item) {
$arr[$currency] = $this->countTax($item, $tmp['out'][$currency]);
}
}
return $arr;
}
@@ -138,10 +185,15 @@ class Crypto extends Main
{
$needCount = abs($mainObject->getValue());
$priceToTax = 0;
$getedValue = 0;
/** @var MainObject $buy */
foreach ($buys as $buy) {
if ($mainObject->getDateTime()->diff($buy->getDateTime())->invert == 0) {
$this->notCount[] = $mainObject;
$value = abs($mainObject->getValue())-$getedValue;
if ($value > 1) {
$value /= MainObject::_CRYPTO_DIVIDER;
}
$this->notCount[$mainObject->getCurrency()] = $value;
break;
}
if ($needCount === 0) {
@@ -150,8 +202,12 @@ class Crypto extends Main
if ($buy->getValue() == 0) {
continue;
}
if ($mainObject->getCurrency() == "LML") {
// dd($needCount - $buy->getValue());
}
if ($needCount - $buy->getValue() > 0) {
$needCount -= $buy->getValue();
$getedValue += $buy->getValue();
$priceToTax += abs($buy->getRealValue()/100);
$buy->setValue(0);
} else {
@@ -159,6 +215,7 @@ class Crypto extends Main
$priceToTax += round(abs($buy->getPrice() * $needCount / MainObject::_CRYPTO_DIVIDER),4);
$needCount = 0;
$buy->setValue($newVal);
break;
}
}
$return = [
@@ -166,10 +223,46 @@ class Crypto extends Main
'cost' => 0,
];
if ($needCount > 0) {
if ($mainObject->getCurrency() == "GRT") {
// dd($needCount,$getedValue,$mainObject, $buys);
}
$value = abs($mainObject->getValue())-$getedValue;
if ($value > 1) {
$value /= MainObject::_CRYPTO_DIVIDER;
}
$this->notCount[$mainObject->getCurrency()] = $value;
return $return;
}
$return['income'] = $mainObject->getRealValue() / 100;
$return['cost'] = round($priceToTax,2);
return $return;
}
private function changeValue(MainObject $mainObject, array $buys)
{
$needCount = abs($mainObject->getRealValue());
$newCount = 0;
/** @var MainObject $buy */
foreach ($buys as $buy) {
if ($mainObject->getDateTime()->diff($buy->getDateTime())->invert == 0) {
continue;
}
if ($buy->getValue() === 0) {
continue;
}
if ($needCount - $buy->getValue() > 0) {
$newCount += $buy->getValue()/100 * $buy->getPrice();
$buy->setValue(0);
} else {
$newCount = $needCount/100 * $buy->getPrice();
$buy->setValue(intval($buy->getValue()-$needCount));
}
}
if ($newCount > 0) {
$mainObject->setRealValue(intval(round($newCount*100)));
$mainObject->setRealCurrency("PLN");
$mainObject->takePrice(true);
}
}
}
+48 -5
View File
@@ -10,17 +10,32 @@ class Main
{
protected array $data;
public function __construct(array $data)
public function __construct()
{
$this->data = $data;
}
public function addData(array $data)
{
if (isset($data[0]) && $data[0][0]==='Date(UTC)') {
$first = array_shift($data);
array_unshift($data, $first, [
"2021-04-14 13:00:00",
"EURPLN",
"BUY",
4.5755,
100,
457.55
]);
}
$this->data[] = $data;
}
protected function takeAllRealTransactions(string $abstract): array
{
$tmp = [];
$keys = array_shift($this->data);
foreach ($this->data as $datum1) {
$keys = array_shift($datum1);
$names = [];
foreach ($keys as $keyId => $key) {
/** @var CryptoValues $abstract */
if ($abstract::isValue("values",$key)) {
@@ -35,10 +50,29 @@ class Main
if ($abstract::isValue("desc",$key)) {
$names[$keyId] = "setDesc";
}
if ($abstract::isValue("price",$key)) {
$names[$keyId] = "setPrice";
}
foreach ($this->data as $datum) {
if ($abstract::isValue("transactionType",$key)) {
$names[$keyId] = "setTransactionType";
}
if ($abstract::isValue("market",$key)) {
$names[$keyId] = "setMarketName";
}
if ($abstract::isValue("realValue",$key)) {
$names[$keyId] = "setRealValue";
}
}
$i = 0;
foreach ($datum1 as $key1 => $datum) {
$obj = new MainObject();
if ($datum[0] === $keys[0]) {
continue;
}
foreach ($names as $key => $name) {
if ($name === "setCurrency" && is_float($datum[$key])) {
dd($names,$datum[$key]);
}
if ($name === "setValue") {
$data = "$datum[$key]";
if (strpos($data,",") !== false || strpos($data,".") !== false) {
@@ -50,10 +84,19 @@ class Main
if ($name === "setDateTime") {
$datum[$key] = new \DateTime($datum[$key]);
}
if ($name === "setRealValue") {
$datum[$key] = intval($datum[$key]*100);
}
$obj->$name($datum[$key]);
}
$obj->trySetCurrencies();
$obj->checkCurrency();
if ($i == 30) {
// dd($obj);
}
$tmp[] = $obj;
$i++;
}
}
return $tmp;
}
-1
View File
@@ -35,7 +35,6 @@ class ReadSheet
foreach ($spreadsheet->getAllSheets() as $sheetName) {
$this->arr[] = $sheetName->toArray();
}
return count($this->arr) > 0;
}
}
+106 -4
View File
@@ -4,6 +4,7 @@ namespace App\Logic\Crypto;
use App\Helper\Currency;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
class MainObject
{
@@ -16,6 +17,16 @@ class MainObject
private int $realValue=0;
private float $price=0;
private bool $isCrypto = false;
private string $transactionType="";
private string $marketName="";
/**
* @return string
*/
public function getTransactionType(): string
{
return $this->transactionType;
}
/**
* @return float
@@ -34,9 +45,9 @@ class MainObject
}
/**
* @return string
* @return string|null
*/
public function getRealCurrency(): string
public function getRealCurrency(): ?string
{
return $this->realCurrency;
}
@@ -110,12 +121,28 @@ class MainObject
*/
public function setCurrency(string $currency): void
{
if (!in_array($currency, Currency::$currencies)) {
if (!in_array($currency, Currency::$currencies) && !in_array($currency, Currency::$currenciesCrypto)) {
$this->isCrypto = true;
}
$this->currency = $currency;
}
/**
* @param string $transactionType
*/
public function setTransactionType(string $transactionType): void
{
$this->transactionType = $transactionType;
}
/**
* @param string $marketName
*/
public function setMarketName(string $marketName): void
{
$this->marketName = $marketName;
}
public function addValue(int $val) : void
{
$this->value += $val;
@@ -129,8 +156,11 @@ class MainObject
$this->realValue += $val;
}
public function takePrice() : void
public function takePrice(bool $force = false) : void
{
if ($this->price && !$force) {
return;
}
$real = $this->getRealValue() / 100;
$val = $this->getValue();
if ($this->isCrypto) {
@@ -143,6 +173,15 @@ class MainObject
// $price *= 100;
// }
$this->price = $price;
}
/**
* @param float|int $price
*/
public function setPrice($price): void
{
$this->price = $price;
}
public function getPrice() : float
@@ -166,4 +205,67 @@ class MainObject
$this->value = intval($this->value/self::_CRYPTO_DIVIDER*100);
}
}
public function trySetCurrencies() : void
{
if ($this->marketName) {
if (strlen($this->marketName) === 6) {
$cur1 = substr($this->marketName,0,3);
$cur2 = substr($this->marketName,3);
if ($this->transactionType === "BUY") {
$this->setCurrency($cur1);
$this->setRealCurrency($cur2);
// $this->setPrice(-$this->getPrice());
} else {
$this->setCurrency($cur2);
$this->setRealCurrency($cur1);
}
} else {
$cryptoFiat = ["USDT"];
foreach ($cryptoFiat as $item) {
$currReal = strpos($this->marketName, $item);
if ($currReal === false) {
continue;
}
if ($currReal > 0) {
$cur1 = substr($this->marketName, 0,strlen($this->marketName) - strlen($item));
$cur2 = $item;
} else {
$cur1 = $item;
$cur2 = substr($this->marketName, 0, strlen($this->marketName) - strlen($item));
}
if ($this->transactionType === "BUY") {
$this->setCurrency($cur1);
$this->setRealCurrency($cur2);
// $this->setPrice(-$this->getPrice());
} else {
$this->setCurrency($cur2);
$this->setRealCurrency($cur1);
}
}
}
if ($this->getRealCurrency() === "USDT") {
$this->setRealValue(-intval($this->getRealValue()));
}
}
}
public function currencyConversion() : void
{
if ($this->realCurrency === "PLN" || (!in_array($this->realCurrency, Currency::$currencies) && !in_array($this->realCurrency, Currency::$currenciesCrypto))) {
return;
}
$eur = 4.5755;
if ($this->realCurrency === "EUR") {
$this->setRealValue(intval(round($this->getRealValue() * $eur)));
$this->setRealCurrency("PLN");
$this->takePrice(true);
}
// if ($this->realCurrency === "USDT") {
// dd($this);
// $this->setRealValue(intval(round($this->getRealValue() * ($eur/$this->getPrice()))));
// $this->setRealCurrency("PLN");
// $this->takePrice(true);
// }
}
}
@@ -28,9 +28,11 @@ class DividendsSummary
if (count($this->allData) > 0) {
$sum = 0;
$profitSum = 0;
foreach ($this->allData as $key => $item) {
$val = round($item['tax'], 2);
$sum += $val;
$profitSum += $item['incomeAll'];
$return[$key] = [
"country" => \Locale::getDisplayRegion("-".$key),
"income" => $item['incomeAll'],
@@ -42,7 +44,10 @@ class DividendsSummary
];
}
// >
$return['sum'] = intval(ceil($sum));
$return['sum'] = [
"tax" => intval(ceil($sum)),
"profit" => $profitSum
];
}
return $return;
+20 -23
View File
@@ -17,30 +17,24 @@ class CryptoService extends MasterServices
public function generate() : bool
{
$summaryObj = new DividendsSummary($this->manager);
$sum = [];
$errorArr = [];
$a = new Crypto();
foreach ($this->allFiles as $file) {
$extension = pathinfo($file)['extension'];
$tmp = new ReadSheet($file);
$tmp->takeFile();
$a = new Crypto($tmp->getAll()[0]);
$sum = array_merge($sum, $a->takeSumTransactions());
$errorArr = array_merge($errorArr, $a->getNotCounted());
if($tmp->takeFile()){
$a->addData($tmp->getAll()[0]);
}
$this->transactions = $sum;
$this->transactionsError = $errorArr;
return count($sum) > 0;
}
$this->transactions = $a->takeSumTransactions();
$this->transactionsError = $a->getNotCounted();
return true;
}
public function takeAllByCurrency() : array
{
$return = [];
$sum = 0;
$profitSum = 0;
foreach ($this->transactions as $key => $transaction) {
$tax = $transaction['income'] - $transaction['cost'];
$profit = $tax;
@@ -51,19 +45,17 @@ class CryptoService extends MasterServices
}
$return[$key] = [
"country" => $key,
// "value" => round($transaction,4),
"currency" => "",
"tax" => $tax,
"profit" => $profit
];
$profitSum += $profit;
$return[$key] = array_merge($return[$key], $transaction);
if ($tax > 0) {
$sum += $tax;
}
}
$return['sum'] = intval(ceil($sum));
// dd($return, $this->transactionsError);
$return['sum'] = [
'tax' => intval(ceil($profitSum*0.19)),
'profit' => $profitSum
];
return $return;
}
@@ -79,5 +71,10 @@ class CryptoService extends MasterServices
return $return;
}
public function takeError()
{
return $this->transactionsError;
}
}
//LML, EXY, AMLT, XRP, XLM, OMG, ETH, LTC
-1
View File
@@ -34,7 +34,6 @@ class MasterServices
}
closedir($handle);
$this->allFiles=$fileNames;
return count($fileNames) > 0;
}
}
+21 -1
View File
@@ -28,7 +28,7 @@
</tr>
</thead>
<tbody>
{% for tax in taxes %}
{% for key,tax in taxes %}
{% if tax.country is defined %}
{% set colspan = tax|length %}
{% if tax.income > 0 %}
@@ -62,6 +62,12 @@
{% endfor %}
{% endif %}
{% endif %}
{% elseif key == "sum" %}
<tr class="">
<td colspan="{{ colspan-2 }}">Summary:</td>
<td>{{ tax.profit|price("PLN",0) }}</td>
<td>{{ tax.tax|price("PLN",0) }}</td>
</tr>
{% else %}
<tr class="">
<td colspan="{{ colspan-1 }}">Summary:</td>
@@ -72,6 +78,20 @@
</tbody>
</table>
{% endif %}
{% if error %}
<table class="table table-dark table-striped">
<tr>
{% for curr,val in error %}
<td>{{ curr }}</td>
{% endfor %}
</tr>
<tr>
{% for curr,val in error %}
<td>{{ val }}</td>
{% endfor %}
</tr>
</table>
{% endif %}
</div>
</div>