diff --git a/project/src/Controller/TaxesController.php b/project/src/Controller/TaxesController.php index 00840e9..dfd213f 100644 --- a/project/src/Controller/TaxesController.php +++ b/project/src/Controller/TaxesController.php @@ -85,14 +85,16 @@ class TaxesController extends AbstractController /** * @Route("/taxes/{hash}", name="app_taxes_show") */ - public function show(CryptoService $tax) : Response + public function show(DividendsService $tax) : Response { if ($tax->readFiles()) { $gen = $tax->generate(); +// dd($gen); return $this->render("taxes/show.html.twig", [ "taxes" => $gen->getSummaryByCountry(), "error" => $gen->error ]); + } return new Response("error"); } diff --git a/project/src/Helper/FIFO/Crypto.php b/project/src/Helper/FIFO/Crypto.php index 67ba599..14c6d2f 100644 --- a/project/src/Helper/FIFO/Crypto.php +++ b/project/src/Helper/FIFO/Crypto.php @@ -9,7 +9,9 @@ use App\Logic\Crypto\MainObject; class Crypto extends Main { - public function takeSumTransactions() : array + private array $notCount = []; + + private function takeTransactions() : array { $all = $this->takeAllRealTransactions(CryptoValues::class); if (!$all || count($all) < 1) { @@ -30,51 +32,116 @@ class Crypto extends Main // } // } // $sum[$item->getCurrency()][$item->getDateTime()->format("Y-m-d")][] = $item; - $date = $item->getDateTime()->format("Y-m-d"); - if (!isset($sum[$date])) { - $sum[$date] = []; + $date = $item->getDateTime()->format("Y-m-d H:i"); + if (!isset($sum[$item->getCurrency()])) { + $sum[$item->getCurrency()] = []; } - if (!isset($sum[$date][$item->getCurrency()])){ - $sum[$date][$item->getCurrency()] = []; + if (!isset($sum[$item->getCurrency()][$date])){ + $sum[$item->getCurrency()][$date] = []; } - $sum[$date][$item->getCurrency()][] = $item; + $sum[$item->getCurrency()][$date] = $item; } $test=[]; - foreach ($sum as $date => $items) { - $keys = array_keys($items); - $crypto=""; - $succ=false; - foreach ($keys as $key) { - if ($key === "PLN") { - $succ = true; - } else { - $crypto = $key; + $realCurrency = []; + foreach ($sum as $currency => $items) { + if (in_array($currency, Currency::$currencies)) { + if (!isset($realCurrency[$currency])) { + $realCurrency[$currency] = []; } + $realCurrency[$currency] = $items; } - if (!$succ) { - continue; - } - if (!isset($test[$crypto])) { - $test[$crypto] = []; - } - $arr = [ - "crypto" => 0, - "pln" => 0 - ]; - /** @var MainObject $obj */ - foreach ($items as $currency => $obj1) { - foreach ($obj1 as $obj) { - if ($currency === $crypto) { - $arr['crypto'] += $obj->getValue(); + } + $keyToDel = array_keys($realCurrency); + foreach ($keyToDel as $item) { + unset($sum[$item]); + } + + return [ + "realCurrency" => $realCurrency, + "transactions" => $sum + ]; + } + + public function takeSumTransactions() : array + { + $transactions = $this->takeTransactions(); + $tmp = [ + 'in' => [], + 'out' =>[] + ]; + + /** @var MainObject $item */ + foreach ($transactions['transactions'] as $currency => $transaction) { + foreach ($transaction as $date => $item) { + if (isset($transactions['realCurrency']['PLN'][$date])) { + $item->setRealValue($transactions['realCurrency']['PLN'][$date]->getValue()); + 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) { + $tmp['in'][$currency][$date] = $item; } else { - $arr['pln'] += $obj->getValue(); + $tmp['out'][$currency][$date] = $item; } } } - $test[$crypto] = $arr; } - return $test; + + foreach ($tmp as &$inout) { + foreach ($inout as $currency => &$items) { + ksort($items,2); + } + } + if (count($tmp["in"]) === 0) { + return []; + } + $arr = []; + foreach ($tmp["in"] as $currency => $items) { + foreach ($items as $item) { + $item->takePrice(); + $arr[$currency] = $this->countTax($item, $tmp['out'][$currency]); + } + } + return $arr; + } + + public function getNotCounted() : array + { + return $this->notCount; + } + + private function countTax(MainObject $mainObject, array $buys) + { + $needCount = abs($mainObject->getValue()); + $priceToTax = 0; + /** @var MainObject $buy */ + foreach ($buys as $buy) { + if ($mainObject->getDateTime()->diff($buy->getDateTime())->invert == 0) { + $this->notCount[] = $mainObject; + break; + } + if ($needCount === 0) { + break; + } + if ($buy->getValue() == 0) { + continue; + } + if ($needCount > $buy->getValue()) { + $needCount -= $buy->getValue(); + $priceToTax += $buy->getRealValue(); + } else { + $newVal = abs($buy->getValue())-$needCount; + $priceToTax += $buy->getPrice() * $needCount; + $needCount = 0; + $buy->setValue($newVal); + } + } + return $mainObject->getRealValue()-$priceToTax; } } \ No newline at end of file diff --git a/project/src/Logic/Crypto/MainObject.php b/project/src/Logic/Crypto/MainObject.php index 0fc7078..2bf349f 100644 --- a/project/src/Logic/Crypto/MainObject.php +++ b/project/src/Logic/Crypto/MainObject.php @@ -2,12 +2,49 @@ declare(strict_types=1); namespace App\Logic\Crypto; +use phpDocumentor\Reflection\DocBlock\Tags\Var_; + class MainObject { private \DateTime $dateTime; private string $desc; private float $value; private string $currency; + private ?string $realCurrency=null; + private float $realValue=0; + private float $price=0; + + /** + * @return float + */ + public function getRealValue(): float + { + return $this->realValue; + } + + /** + * @param float $realValue + */ + public function setRealValue(float $realValue): void + { + $this->realValue = $realValue; + } + + /** + * @return string + */ + public function getRealCurrency(): string + { + return $this->realCurrency; + } + + /** + * @param string $realCurrency + */ + public function setRealCurrency(string $realCurrency): void + { + $this->realCurrency = $realCurrency; + } /** * @return \DateTime @@ -73,4 +110,26 @@ class MainObject $this->currency = $currency; } + public function addValue(float $val) : void + { + $this->value += $val; + } + + public function addRealValue(float $val) : void + { + if (!isset($this->realValue)) { + $this->realValue = 0; + } + $this->realValue += $val; + } + + public function takePrice() : void + { + $this->price = abs(round($this->realValue / $this->getValue(),4)); + } + + public function getPrice() : float + { + return $this->price; + } } \ No newline at end of file diff --git a/project/src/Services/CryptoService.php b/project/src/Services/CryptoService.php index 9aab4cf..86d4ba1 100644 --- a/project/src/Services/CryptoService.php +++ b/project/src/Services/CryptoService.php @@ -11,20 +11,25 @@ use Doctrine\ORM\EntityManager; class CryptoService extends MasterServices { - public function generate() : DividendsSummary + public function generate() : array { $summaryObj = new DividendsSummary($this->manager); - + $sum = 0; + $errorArr = []; foreach ($this->allFiles as $file) { $extension = pathinfo($file)['extension']; $tmp = new ReadSheet($file); $tmp->takeFile(); $a = new Crypto($tmp->getAll()[0]); - dd($a->takeSumTransactions()); + $sum += array_sum($a->takeSumTransactions()); + $errorArr = array_merge($errorArr, $a->getNotCounted()); } - return $summaryObj; + return [ + "sum" => $sum, + "error" => $errorArr + ]; } } \ No newline at end of file