From a29ee0d5b788da3c8f20df4a7fd1a72ba4f336f4 Mon Sep 17 00:00:00 2001 From: Ryjek Date: Thu, 31 Mar 2022 16:24:11 +0200 Subject: [PATCH] . --- project/src/Controller/IndexController.php | 18 ++++++ project/src/Controller/TaxesController.php | 73 ++++++++++++++-------- project/src/Helper/FIFO/Crypto.php | 38 ++++++++--- project/src/Helper/FIFO/Main.php | 7 ++- project/src/Logic/Crypto/MainObject.php | 2 +- project/src/Services/CryptoService.php | 41 +++++++++--- project/templates/index/index.html.twig | 14 +++++ 7 files changed, 146 insertions(+), 47 deletions(-) create mode 100644 project/src/Controller/IndexController.php create mode 100644 project/templates/index/index.html.twig diff --git a/project/src/Controller/IndexController.php b/project/src/Controller/IndexController.php new file mode 100644 index 0000000..721ec1b --- /dev/null +++ b/project/src/Controller/IndexController.php @@ -0,0 +1,18 @@ +render("index/index.html.twig"); + } +} \ No newline at end of file diff --git a/project/src/Controller/TaxesController.php b/project/src/Controller/TaxesController.php index dfd213f..a1d91a4 100644 --- a/project/src/Controller/TaxesController.php +++ b/project/src/Controller/TaxesController.php @@ -3,10 +3,6 @@ namespace App\Controller; use App\Helper\Currency; -use App\Helper\MyReader; -use App\Helper\ReaderSheet; -use App\Helper\SheetData; -use App\Helper\DividendsSummary; use App\Services\CryptoService; use App\Services\DividendsService; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; @@ -24,10 +20,14 @@ use Symfony\Component\Validator\Constraints\File; class TaxesController extends AbstractController { /** - * @Route("/taxes", name="app_taxes") + * @Route("/taxes/{type}", name="app_taxes") */ - public function index(Request $request): Response + public function index(Request $request, string $type): Response { + if (!in_array($type, ["crypto", "dividend"])) { + return $this->redirectToRoute("homepage"); + } + $a=[]; $form = $this->createFormBuilder([]) ->add("uploads",FileType::class, [ @@ -36,24 +36,26 @@ class TaxesController extends AbstractController 'attr'=>[ 'multiple' => 'multiple', ], -// 'constraints' => [ -// new All([ -// 'constraints' => [ -// new File([ -// 'maxSize' => '2048k', -// 'mimeTypes' => [ -// 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', -// 'application/atom+xml', -// 'application/xml', -// 'text/xml', -// 'text/xml-external-parsed-entity', -//// 'application/x-pdf', -// ], -// 'mimeTypesMessage' => 'Please upload a valid XLSX or XML document', -// ]) -// ] -// ]) -// ], + 'constraints' => [ + new All([ + 'constraints' => [ + new File([ + 'maxSize' => '2048k', + 'mimeTypes' => [ + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'application/atom+xml', + 'application/xml', + 'text/xml', + 'text/xml-external-parsed-entity', + 'text/csv', + 'text/plain', +// 'application/x-pdf', + ], + 'mimeTypesMessage' => 'Please upload a valid XLSX or XML document', + ]) + ] + ]) + ], ]) ->add("upload",SubmitType::class) ->getForm(); @@ -71,7 +73,7 @@ class TaxesController extends AbstractController ); $i++; } - return $this->redirectToRoute("app_taxes_show",["hash" => $hash]); + return $this->redirectToRoute("app_taxes_{$type}_show",["hash" => $hash]); } } @@ -83,9 +85,9 @@ class TaxesController extends AbstractController } /** - * @Route("/taxes/{hash}", name="app_taxes_show") + * @Route("/taxes/dividend/{hash}", name="app_taxes_dividend_show") */ - public function show(DividendsService $tax) : Response + public function showDividend(DividendsService $tax) : Response { if ($tax->readFiles()) { $gen = $tax->generate(); @@ -98,4 +100,21 @@ class TaxesController extends AbstractController } return new Response("error"); } + /** + * @Route("/taxes/crypto/{hash}", name="app_taxes_crypto_show") + */ + public function showCrypto(CryptoService $tax) : Response + { + if ($tax->readFiles()) { + $gen = $tax->generate(); + if ($gen) { + return $this->render("taxes/show.html.twig", [ + "taxes" => $tax->takeAllByCurrency(), +// "error" => $gen->error + ]); + } + + } + return new Response("error"); + } } diff --git a/project/src/Helper/FIFO/Crypto.php b/project/src/Helper/FIFO/Crypto.php index 14c6d2f..b370a05 100644 --- a/project/src/Helper/FIFO/Crypto.php +++ b/project/src/Helper/FIFO/Crypto.php @@ -40,10 +40,22 @@ class Crypto extends Main $sum[$item->getCurrency()][$date] = []; } - $sum[$item->getCurrency()][$date] = $item; + $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()); + $sum[$currency][$date] = $realObj; + } else { + $sum[$currency][$date] = $i[0]; + } + } + } foreach ($sum as $currency => $items) { if (in_array($currency, Currency::$currencies)) { if (!isset($realCurrency[$currency])) { @@ -56,7 +68,6 @@ class Crypto extends Main foreach ($keyToDel as $item) { unset($sum[$item]); } - return [ "realCurrency" => $realCurrency, "transactions" => $sum @@ -75,7 +86,9 @@ class Crypto extends Main foreach ($transactions['transactions'] as $currency => $transaction) { foreach ($transaction as $date => $item) { if (isset($transactions['realCurrency']['PLN'][$date])) { - $item->setRealValue($transactions['realCurrency']['PLN'][$date]->getValue()); + $obj = $transactions['realCurrency']['PLN'][$date]; + $item->setRealValue($obj->getValue()); + $item->setRealCurrency($obj->getCurrency()); if (!isset($tmp['in'][$currency])) { $tmp['in'][$currency] = []; } @@ -91,13 +104,18 @@ class Crypto extends Main } } } - - foreach ($tmp as &$inout) { foreach ($inout as $currency => &$items) { - ksort($items,2); + if (count($items) > 0) { + ksort($items,2); + foreach ($items as $item) { + $item->takePrice(); + } + } } } + unset($items); + unset($inout); if (count($tmp["in"]) === 0) { return []; } @@ -118,7 +136,7 @@ class Crypto extends Main private function countTax(MainObject $mainObject, array $buys) { - $needCount = abs($mainObject->getValue()); + $needCount = $mainObject->getValue(); $priceToTax = 0; /** @var MainObject $buy */ foreach ($buys as $buy) { @@ -132,16 +150,16 @@ class Crypto extends Main if ($buy->getValue() == 0) { continue; } - if ($needCount > $buy->getValue()) { + if ($needCount - $buy->getValue() > 0) { $needCount -= $buy->getValue(); $priceToTax += $buy->getRealValue(); } else { - $newVal = abs($buy->getValue())-$needCount; + $newVal = $buy->getValue() - $needCount; $priceToTax += $buy->getPrice() * $needCount; $needCount = 0; $buy->setValue($newVal); } } - return $mainObject->getRealValue()-$priceToTax; + return $mainObject->getRealValue() + $priceToTax; } } \ No newline at end of file diff --git a/project/src/Helper/FIFO/Main.php b/project/src/Helper/FIFO/Main.php index a61db8d..5954b1a 100644 --- a/project/src/Helper/FIFO/Main.php +++ b/project/src/Helper/FIFO/Main.php @@ -40,7 +40,12 @@ class Main $obj = new MainObject(); foreach ($names as $key => $name) { if ($name === "setValue") { - $datum[$key] = floatval($datum[$key]); +// if ($datum[$key]!=="0,00") dd(); + $data = $datum[$key]; + if (is_string($data)) { + $data = strtr($data,",","."); + } + $datum[$key] = floatval($data); } if ($name === "setDateTime") { $datum[$key] = new \DateTime($datum[$key]); diff --git a/project/src/Logic/Crypto/MainObject.php b/project/src/Logic/Crypto/MainObject.php index 2bf349f..76e48cc 100644 --- a/project/src/Logic/Crypto/MainObject.php +++ b/project/src/Logic/Crypto/MainObject.php @@ -125,7 +125,7 @@ class MainObject public function takePrice() : void { - $this->price = abs(round($this->realValue / $this->getValue(),4)); + $this->price = floatval(abs(round($this->realValue / $this->getValue(),4))); } public function getPrice() : float diff --git a/project/src/Services/CryptoService.php b/project/src/Services/CryptoService.php index 86d4ba1..eb310f3 100644 --- a/project/src/Services/CryptoService.php +++ b/project/src/Services/CryptoService.php @@ -4,6 +4,7 @@ namespace App\Services; use App\Helper\FIFO\Crypto; use App\Helper\Sheets\ReadSheet; +use App\Logic\Crypto\MainObject; use App\Logic\Dividends\DividendsSummary; use App\Logic\Dividends\SheetData; use App\Logic\Dividends\XMLReader; @@ -11,25 +12,49 @@ use Doctrine\ORM\EntityManager; class CryptoService extends MasterServices { - public function generate() : array + private array $transactions = []; + private array $transactionsError = []; + + public function generate() : bool { $summaryObj = new DividendsSummary($this->manager); - $sum = 0; + $sum = []; $errorArr = []; foreach ($this->allFiles as $file) { $extension = pathinfo($file)['extension']; $tmp = new ReadSheet($file); $tmp->takeFile(); $a = new Crypto($tmp->getAll()[0]); - $sum += array_sum($a->takeSumTransactions()); + $sum = array_merge($sum, $a->takeSumTransactions()); $errorArr = array_merge($errorArr, $a->getNotCounted()); } + $this->transactions = $sum; + $this->transactionsError = $errorArr; - return [ - "sum" => $sum, - "error" => $errorArr - ]; + + return count($sum) > 0; } -} \ No newline at end of file + + public function takeAllByCurrency() : array + { + $return = []; + $sum = 0; + foreach ($this->transactions as $key => $transaction) { + $return[$key] = [ + "country" => $key, + "value" => round($transaction,4), + "currency" => "" + ]; + if ($transaction > 0) { + $sum += round($transaction,4); + } + } + $return['sum'] = intval(ceil($sum)); + +// dd($return, $this->transactionsError); + return $return; + } +} +//LML, EXY, AMLT, XRP, XLM, OMG, ETH, LTC \ No newline at end of file diff --git a/project/templates/index/index.html.twig b/project/templates/index/index.html.twig new file mode 100644 index 0000000..bd640a5 --- /dev/null +++ b/project/templates/index/index.html.twig @@ -0,0 +1,14 @@ +{% extends 'base.html.twig' %} + +{% block title %}Index{% endblock %} + +{% block body %} +
+
+ Dividends +
+ +
+{% endblock %} \ No newline at end of file