diff --git a/project/src/Controller/TaxesController.php b/project/src/Controller/TaxesController.php index f6e1e25..ac4e939 100644 --- a/project/src/Controller/TaxesController.php +++ b/project/src/Controller/TaxesController.php @@ -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() ]); } diff --git a/project/src/Helper/Currency.php b/project/src/Helper/Currency.php index 3b18d9e..179070e 100644 --- a/project/src/Helper/Currency.php +++ b/project/src/Helper/Currency.php @@ -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; diff --git a/project/src/Helper/DataNeed/CryptoValues.php b/project/src/Helper/DataNeed/CryptoValues.php index c7a0fa1..00499ff 100644 --- a/project/src/Helper/DataNeed/CryptoValues.php +++ b/project/src/Helper/DataNeed/CryptoValues.php @@ -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 diff --git a/project/src/Helper/FIFO/Crypto.php b/project/src/Helper/FIFO/Crypto.php index c72deb4..d20acf4 100644 --- a/project/src/Helper/FIFO/Crypto.php +++ b/project/src/Helper/FIFO/Crypto.php @@ -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); + } + } } \ No newline at end of file diff --git a/project/src/Helper/FIFO/Main.php b/project/src/Helper/FIFO/Main.php index a3e0f44..0d3bdb8 100644 --- a/project/src/Helper/FIFO/Main.php +++ b/project/src/Helper/FIFO/Main.php @@ -10,50 +10,93 @@ 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); - $names = []; - - foreach ($keys as $keyId => $key) { - /** @var CryptoValues $abstract */ - if ($abstract::isValue("values",$key)) { - $names[$keyId] = "setValue"; + foreach ($this->data as $datum1) { + $keys = array_shift($datum1); + $names = []; + foreach ($keys as $keyId => $key) { + /** @var CryptoValues $abstract */ + if ($abstract::isValue("values",$key)) { + $names[$keyId] = "setValue"; + } + if ($abstract::isValue("date",$key)) { + $names[$keyId] = "setDateTime"; + } + if ($abstract::isValue("currency",$key)) { + $names[$keyId] = "setCurrency"; + } + if ($abstract::isValue("desc",$key)) { + $names[$keyId] = "setDesc"; + } + if ($abstract::isValue("price",$key)) { + $names[$keyId] = "setPrice"; + } + if ($abstract::isValue("transactionType",$key)) { + $names[$keyId] = "setTransactionType"; + } + if ($abstract::isValue("market",$key)) { + $names[$keyId] = "setMarketName"; + } + if ($abstract::isValue("realValue",$key)) { + $names[$keyId] = "setRealValue"; + } } - if ($abstract::isValue("date",$key)) { - $names[$keyId] = "setDateTime"; - } - if ($abstract::isValue("currency",$key)) { - $names[$keyId] = "setCurrency"; - } - if ($abstract::isValue("desc",$key)) { - $names[$keyId] = "setDesc"; - } - } - foreach ($this->data as $datum) { - $obj = new MainObject(); - foreach ($names as $key => $name) { - if ($name === "setValue") { - $data = "$datum[$key]"; - if (strpos($data,",") !== false || strpos($data,".") !== false) { - $data = str_replace(",",".",$data); + $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]); } - $realInt = floatval($data)*MainObject::_CRYPTO_DIVIDER; - $datum[$key] = intval($realInt); + if ($name === "setValue") { + $data = "$datum[$key]"; + 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]); + } + if ($name === "setRealValue") { + $datum[$key] = intval($datum[$key]*100); + } + $obj->$name($datum[$key]); } - if ($name === "setDateTime") { - $datum[$key] = new \DateTime($datum[$key]); + $obj->trySetCurrencies(); + $obj->checkCurrency(); + if ($i == 30) { +// dd($obj); } - $obj->$name($datum[$key]); + $tmp[] = $obj; + $i++; } - $obj->checkCurrency(); - $tmp[] = $obj; } return $tmp; } diff --git a/project/src/Helper/Sheets/ReadSheet.php b/project/src/Helper/Sheets/ReadSheet.php index 74717ae..a4a410e 100644 --- a/project/src/Helper/Sheets/ReadSheet.php +++ b/project/src/Helper/Sheets/ReadSheet.php @@ -35,7 +35,6 @@ class ReadSheet foreach ($spreadsheet->getAllSheets() as $sheetName) { $this->arr[] = $sheetName->toArray(); } - return count($this->arr) > 0; } } diff --git a/project/src/Logic/Crypto/MainObject.php b/project/src/Logic/Crypto/MainObject.php index cfa1773..d7d7fbd 100644 --- a/project/src/Logic/Crypto/MainObject.php +++ b/project/src/Logic/Crypto/MainObject.php @@ -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,19 +156,31 @@ 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) { $val = $this->getValueCrypto(); } $price = round($real/$val,8); -// if ($this->isCrypto) { -// $price *= self::_CRYPTO_DIVIDER; -// } else { -// $price *= 100; -// } + // if ($this->isCrypto) { + // $price *= self::_CRYPTO_DIVIDER; + // } else { + // $price *= 100; + // } + $this->price = $price; + + } + + /** + * @param float|int $price + */ + public function setPrice($price): void + { $this->price = $price; } @@ -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); +// } + } } \ No newline at end of file diff --git a/project/src/Logic/Dividends/DividendsSummary.php b/project/src/Logic/Dividends/DividendsSummary.php index 5d16da6..6b86186 100644 --- a/project/src/Logic/Dividends/DividendsSummary.php +++ b/project/src/Logic/Dividends/DividendsSummary.php @@ -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; diff --git a/project/src/Services/CryptoService.php b/project/src/Services/CryptoService.php index 5702ff4..8284de1 100644 --- a/project/src/Services/CryptoService.php +++ b/project/src/Services/CryptoService.php @@ -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 \ No newline at end of file diff --git a/project/src/Services/MasterServices.php b/project/src/Services/MasterServices.php index dce7d01..c84885c 100644 --- a/project/src/Services/MasterServices.php +++ b/project/src/Services/MasterServices.php @@ -34,7 +34,6 @@ class MasterServices } closedir($handle); $this->allFiles=$fileNames; - return count($fileNames) > 0; } } \ No newline at end of file diff --git a/project/templates/taxes/show.html.twig b/project/templates/taxes/show.html.twig index f13b389..a427ffc 100644 --- a/project/templates/taxes/show.html.twig +++ b/project/templates/taxes/show.html.twig @@ -28,7 +28,7 @@
- {% 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" %} +| {{ curr }} | + {% endfor %} +
| {{ val }} | + {% endfor %} +