This commit is contained in:
2025-03-18 15:04:41 +01:00
parent fc1ef6f3ec
commit aa52f216bd
14 changed files with 107 additions and 53 deletions
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace App\Enum;
enum MarketNames: int
{
case IBKR = 1;
case MBANK = 2;
case EXANTE = 3;
public static function tryFromCase(string $value): ?MarketNames
{
$ret = null;
foreach (self::cases() as $case) {
if (str_contains($value, strtolower($case->name))) {
$ret = $case;
break;
}
}
return $ret;
}
}
+1 -2
View File
@@ -7,7 +7,7 @@ use Doctrine\ORM\EntityManager;
class Currency
{
public static array $currencies = ["USD","GBP","PLN","EUR","CAD"];
public static array $currencies = ["USD","GBP","PLN","EUR","CAD","AUD"];
public static array $currenciesCrypto = ["USDT"];
private static string $apiUrl = "https://api.nbp.pl/api/exchangerates/rates/a/:code/:date/?format=json";
private EntityManager $manager;
@@ -30,7 +30,6 @@ class Currency
}
$dateTime->modify("-$minusDay days");
$dateStr = $dateTime->format("Ymd");
if (isset($this->ratesPerDate[$currency]) && isset($this->ratesPerDate[$currency][$dateStr])) {
return $this->ratesPerDate[$currency][$dateStr];
}
@@ -20,6 +20,7 @@ class DividendValues
public static function searchForKeyName(string $val) : ?string
{
$ret=null;
$val = trim($val);
foreach (self::$samples as $key => $sample) {
foreach ($sample as $item) {
if (strpos($val, $item) !== false) {
+7 -2
View File
@@ -2,17 +2,20 @@
declare(strict_types=1);
namespace App\Helper\Sheets;
use App\Enum\MarketNames;
use PhpOffice\PhpSpreadsheet\IOFactory;
class ReadSheet
{
private string $filePath;
private array $arr=[];
private ?MarketNames $marketNames;
public function __construct(string $file)
public function __construct(string $file, ?MarketNames $marketNames)
{
if (file_exists($file)){
$this->filePath = $file;
$this->marketNames = $marketNames;
}
}
@@ -27,10 +30,12 @@ class ReadSheet
return false;
}
$fileType = explode(".", $this->filePath)[1];
$reader = IOFactory::createReader(ucfirst($fileType));
$reader->setLoadAllSheets();
$reader->setReadDataOnly(true);
if ($this->marketNames && $this->marketNames === MarketNames::EXANTE) {
$reader->setInputEncoding("UTF-16");
}
if ($delimiter) {
$reader->setDelimiter($delimiter);
}
@@ -104,6 +104,6 @@ final class DividendsData
*/
public function setCurrency(string $currency): void
{
$this->currency = $currency;
$this->currency = trim($currency);
}
}
@@ -81,13 +81,15 @@ class DividendsSummary
if (!isset($test[$country]['incomes'][$val->getTax()])) {
$test[$country]['incomes'][$val->getTax()] = 0;
}
if ($this->getRate($val) < 1) {
die("Rate for date: ".$val->getDateTime()->format("Y-m-d")." / ".$val->getCurrency() . " is 0");
}
$income = round($val->getCount()*$val->getValue()*$this->getRate($val),5);
$test[$country]['incomeAll'] += $income;
$test[$country]['tax'] += round($this->count($val),5);
$test[$country]['destiny_tax'] += round($this->count($val,true),5);
$test[$country]['tax_cost'] += round($this->countTaxPrice($val),5);
$test[$country]['incomes'][$val->getTax()] += $income;
if (!isset($countryToCurrency[$country])) {
$countryToCurrency[$country] = $val->getCurrency();
}
+9 -7
View File
@@ -14,10 +14,11 @@ class ExanteSheet extends SheetDataAll
continue;
}
foreach ($rawDatum as $key => $value) {
if (strpos($value, "Comment") !== false) {
$value = strtolower($value);
if (str_contains($value, "comment")) {
$commentKey = $key;
}
if (strpos($value, "Operation type") !== false) {
if (str_contains($value, "operation type")) {
$operationKey = $key;
}
}
@@ -27,10 +28,15 @@ class ExanteSheet extends SheetDataAll
if ($index<2) {
continue;
}
if (strtolower($rawDatum[$operationKey]) === "tax") {
$operationType = strtolower($rawDatum[$operationKey]);
if (!str_contains($operationType,"dividend")) {
continue;
}
$comment = $rawDatum[$commentKey];
if (!str_contains($comment, "dividend")) {
continue;
}
$separate = explode(" ",$comment);
$tax = intval(str_replace(["(","-","%",")"],"",$separate[16]));
$value = floatval(str_replace(["(",")"],"",$separate[10]));
@@ -44,10 +50,6 @@ class ExanteSheet extends SheetDataAll
$dividendObj->setValue($value);
$ret[] = $dividendObj;
}
if ($commentKey === -1) {
trigger_error(var_export("false",true));
return [];
}
return $ret;
}
+14 -4
View File
@@ -28,9 +28,8 @@ class IbkrSheet extends SheetDataAll
$dividendArr = [];
$taxArr = [];
$test = [];
foreach ($this->getRawData() as $index => $rawDatum) {
if ($index < 1) {
if ($index < 2) {
continue;
}
$row=[];
@@ -50,18 +49,29 @@ class IbkrSheet extends SheetDataAll
}
}
}
foreach ($dividendArr as $groupId => $dividend) {
if (!isset($taxArr[$groupId])) {
continue;
}
$taxGet = $taxArr[$groupId]['amount'];
// $dividend['amount'] += $taxGet;
$taxPercentage = intval(round(abs($taxGet) / $dividend['amount'], 2) * 100);
if ($taxPercentage === 14) {
$taxPercentage = 15;
}
$separate = explode(" ", $dividend['description']);
$perShare = floatval($separate[4]);
if (!$perShare) {
$perShare = floatval($separate[5]);
}
if (!$perShare) {
continue;
}
$date = explode(";", $dividend['date'])[0];
$date = substr($date, 0, 4) . "-" . substr($date, 4, 2) . "-" . substr($date, 6, 2);
if (substr_count($date, "-") !== 2) {
$date = substr($date, 0, 4) . "-" . substr($date, 4, 2) . "-" . substr($date, 6, 2);
}
// trigger_error(var_export($date,true));
$dividendObj = new DividendsData();
$dividendObj->setCount(intval($dividend['amount'] / $perShare));
$dividendObj->setIsin($dividend['isin']);
@@ -2,6 +2,8 @@
namespace App\Logic\Dividends;
use function Symfony\Component\Translation\t;
abstract class SheetDataAll
{
protected int $countData;
+13 -6
View File
@@ -2,6 +2,7 @@
declare(strict_types=1);
namespace App\Services;
use App\Enum\MarketNames;
use App\Helper\Sheets\ReadSheet;
use App\Logic\Dividends\DividendsSummary;
use App\Logic\Dividends\ExanteSheet;
@@ -18,21 +19,27 @@ class DividendsService extends MasterServices
foreach ($this->allFiles as $file) {
$extension = pathinfo($file)['extension'];
$isExante = strpos($file, "exante") !== false;
$isIbkr = strpos($file, "ibkr") !== false;
$market = MarketNames::tryFromCase($file);
$isExante = str_contains($file, "exante");
$isIbkr = str_contains($file, "ibkr");
if (in_array($extension, ['xlsx','csv'])) {
if ($isExante) {
if ($market === MarketNames::EXANTE) {
$delimiter = ";";
$delimiter = null;
} else {
$delimiter = null;
}
$reader = new ReadSheet($file);
$reader = new ReadSheet($file, $market);
if ($reader->takeFile($delimiter)) {
/** @var SheetData $item */
foreach ($reader->getAll() as $val) {
if ($isExante) {
if ($market === MarketNames::EXANTE) {
$item = new ExanteSheet($val);
} elseif ($isIbkr) {
} elseif ($market === MarketNames::IBKR) {
$item = new IbkrSheet($val);
}
else {