This commit is contained in:
2022-03-29 16:08:41 +02:00
parent 5cd788c765
commit 98c6814863
4 changed files with 172 additions and 39 deletions
+3 -1
View File
@@ -85,14 +85,16 @@ class TaxesController extends AbstractController
/** /**
* @Route("/taxes/{hash}", name="app_taxes_show") * @Route("/taxes/{hash}", name="app_taxes_show")
*/ */
public function show(CryptoService $tax) : Response public function show(DividendsService $tax) : Response
{ {
if ($tax->readFiles()) { if ($tax->readFiles()) {
$gen = $tax->generate(); $gen = $tax->generate();
// dd($gen);
return $this->render("taxes/show.html.twig", [ return $this->render("taxes/show.html.twig", [
"taxes" => $gen->getSummaryByCountry(), "taxes" => $gen->getSummaryByCountry(),
"error" => $gen->error "error" => $gen->error
]); ]);
} }
return new Response("error"); return new Response("error");
} }
+105 -38
View File
@@ -9,7 +9,9 @@ use App\Logic\Crypto\MainObject;
class Crypto extends Main class Crypto extends Main
{ {
public function takeSumTransactions() : array private array $notCount = [];
private function takeTransactions() : array
{ {
$all = $this->takeAllRealTransactions(CryptoValues::class); $all = $this->takeAllRealTransactions(CryptoValues::class);
if (!$all || count($all) < 1) { if (!$all || count($all) < 1) {
@@ -30,51 +32,116 @@ class Crypto extends Main
// } // }
// } // }
// $sum[$item->getCurrency()][$item->getDateTime()->format("Y-m-d")][] = $item; // $sum[$item->getCurrency()][$item->getDateTime()->format("Y-m-d")][] = $item;
$date = $item->getDateTime()->format("Y-m-d"); $date = $item->getDateTime()->format("Y-m-d H:i");
if (!isset($sum[$date])) { if (!isset($sum[$item->getCurrency()])) {
$sum[$date] = []; $sum[$item->getCurrency()] = [];
} }
if (!isset($sum[$date][$item->getCurrency()])){ if (!isset($sum[$item->getCurrency()][$date])){
$sum[$date][$item->getCurrency()] = []; $sum[$item->getCurrency()][$date] = [];
} }
$sum[$date][$item->getCurrency()][] = $item; $sum[$item->getCurrency()][$date] = $item;
} }
$test=[]; $test=[];
foreach ($sum as $date => $items) { $realCurrency = [];
$keys = array_keys($items); foreach ($sum as $currency => $items) {
$crypto=""; if (in_array($currency, Currency::$currencies)) {
$succ=false; if (!isset($realCurrency[$currency])) {
foreach ($keys as $key) { $realCurrency[$currency] = [];
if ($key === "PLN") { }
$succ = true; $realCurrency[$currency] = $items;
} else {
$crypto = $key;
} }
} }
if (!$succ) { $keyToDel = array_keys($realCurrency);
continue; foreach ($keyToDel as $item) {
} unset($sum[$item]);
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();
} else {
$arr['pln'] += $obj->getValue();
}
}
}
$test[$crypto] = $arr;
} }
return $test; 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 {
$tmp['out'][$currency][$date] = $item;
}
}
}
}
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;
} }
} }
+59
View File
@@ -2,12 +2,49 @@
declare(strict_types=1); declare(strict_types=1);
namespace App\Logic\Crypto; namespace App\Logic\Crypto;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
class MainObject class MainObject
{ {
private \DateTime $dateTime; private \DateTime $dateTime;
private string $desc; private string $desc;
private float $value; private float $value;
private string $currency; 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 * @return \DateTime
@@ -73,4 +110,26 @@ class MainObject
$this->currency = $currency; $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;
}
} }
+9 -4
View File
@@ -11,20 +11,25 @@ use Doctrine\ORM\EntityManager;
class CryptoService extends MasterServices class CryptoService extends MasterServices
{ {
public function generate() : DividendsSummary public function generate() : array
{ {
$summaryObj = new DividendsSummary($this->manager); $summaryObj = new DividendsSummary($this->manager);
$sum = 0;
$errorArr = [];
foreach ($this->allFiles as $file) { foreach ($this->allFiles as $file) {
$extension = pathinfo($file)['extension']; $extension = pathinfo($file)['extension'];
$tmp = new ReadSheet($file); $tmp = new ReadSheet($file);
$tmp->takeFile(); $tmp->takeFile();
$a = new Crypto($tmp->getAll()[0]); $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
];
} }
} }