add exante and ibkr

This commit is contained in:
2023-04-12 15:13:55 +02:00
parent 90952d883e
commit fc1ef6f3ec
17 changed files with 439 additions and 75 deletions
+1 -1
View File
@@ -209,6 +209,7 @@ class MainObject
public function trySetCurrencies() : void
{
if ($this->marketName) {
$cryptoFiat = ["USDT","BUSD"];
if (strlen($this->marketName) === 6) {
$cur1 = substr($this->marketName,0,3);
$cur2 = substr($this->marketName,3);
@@ -221,7 +222,6 @@ class MainObject
$this->setRealCurrency($cur1);
}
} else {
$cryptoFiat = ["USDT"];
foreach ($cryptoFiat as $item) {
$currReal = strpos($this->marketName, $item);
if ($currReal === false) {
@@ -7,14 +7,17 @@ use Doctrine\ORM\EntityManager;
class DividendsSummary
{
/** @var SheetData|array $sheetData */
/** @var SheetDataAll|array $sheetData */
private array $sheetData=[];
/** @var XMLReader|array $sheetData */
private array $xmlData=[];
private Currency $currencyObj;
public array $error=[];
/** @var array|DividendsData $error */
private ?array $errorDividend=null;
private ?array $errorString=null;
private array $allData=[];
private array $countryToCurrency=[];
private int $realCalculatedRows = 0;
public function __construct(EntityManager $manager)
{
@@ -88,6 +91,7 @@ class DividendsSummary
if (!isset($countryToCurrency[$country])) {
$countryToCurrency[$country] = $val->getCurrency();
}
$this->realCalculatedRows++;
}
}
}
@@ -101,7 +105,11 @@ class DividendsSummary
private function getRate(DividendsData $data) : float
{
return $this->currencyObj->getRate($data->getCurrency(),$data->getDateTime(),$this->error);
if (!Currency::isCurrencyExists($data->getCurrency())) {
$this->errorString[$data->getCurrency()] = "Currency: {$data->getCurrency()} is not supported! Please contact with administrator.";
return 0;
}
return $this->currencyObj->getRate($data->getCurrency(),$data->getDateTime());
}
private function count(DividendsData $data, bool $fromDestiny = false) : float
@@ -120,7 +128,7 @@ class DividendsSummary
if ($rate > 0) {
return $valueTax * $rate;
}
$this->error[]=$data;
$this->errorDividend[]=$data;
return 0;
}
@@ -140,9 +148,9 @@ class DividendsSummary
}
/**
* @param SheetData $sheetData
* @param SheetDataAll $sheetData
*/
public function addSheetData(SheetData $sheetData): void
public function addSheetData(SheetDataAll $sheetData): void
{
$this->sheetData[] = $sheetData;
}
@@ -155,4 +163,27 @@ class DividendsSummary
$this->xmlData[] = $reader;
}
public function takeCountRawData(): array
{
$rowCount = 0;
/** @var SheetData $sheetDatum */
foreach ($this->sheetData as $sheetDatum) {
$rowCount += $sheetDatum->getCount();
}
return [
'countSheet' => count($this->sheetData),
'countRows' => $rowCount,
'realCountRows' => $this->realCalculatedRows
];
}
public function getErrorDividend(): ?array
{
return $this->errorDividend;
}
public function getErrorString(): ?array
{
return $this->errorString;
}
}
@@ -0,0 +1,54 @@
<?php
namespace App\Logic\Dividends;
class ExanteSheet extends SheetDataAll
{
protected function takeData(): array
{
$commentKey = -1;
$operationKey = -1;
$ret = [];
foreach ($this->getRawData() as $index => $rawDatum) {
if (!isset($rawDatum[1])) {
continue;
}
foreach ($rawDatum as $key => $value) {
if (strpos($value, "Comment") !== false) {
$commentKey = $key;
}
if (strpos($value, "Operation type") !== false) {
$operationKey = $key;
}
}
break;
}
foreach ($this->getRawData() as $index => $rawDatum) {
if ($index<2) {
continue;
}
if (strtolower($rawDatum[$operationKey]) === "tax") {
continue;
}
$comment = $rawDatum[$commentKey];
$separate = explode(" ",$comment);
$tax = intval(str_replace(["(","-","%",")"],"",$separate[16]));
$value = floatval(str_replace(["(",")"],"",$separate[10]));
$date = new \DateTime($separate[3]);
$dividendObj = new DividendsData();
$dividendObj->setCount($separate[0]);
$dividendObj->setCurrency($separate[9]);
$dividendObj->setTax($tax);
$dividendObj->setDateTime($date);
$dividendObj->setIsin($separate[18]);
$dividendObj->setValue($value);
$ret[] = $dividendObj;
}
if ($commentKey === -1) {
trigger_error(var_export("false",true));
return [];
}
return $ret;
}
}
+78
View File
@@ -0,0 +1,78 @@
<?php
namespace App\Logic\Dividends;
use App\Helper\DataNeed\DividendValues;
class IbkrSheet extends SheetDataAll
{
protected function takeData(): array
{
$needIndex = [];
foreach ($this->getRawData() as $index => $rawDatum) {
if ($index === 0) {
foreach ($rawDatum as $i => $item) {
if (!$item) {
continue;
}
$keyName = DividendValues::searchForKeyName($item);
if ($keyName) {
$needIndex[$i] = $keyName;
}
}
break;
}
}
$ret = [];
$dividendArr = [];
$taxArr = [];
$test = [];
foreach ($this->getRawData() as $index => $rawDatum) {
if ($index < 1) {
continue;
}
$row=[];
foreach ($rawDatum as $i => $d) {
if (isset($needIndex[$i])) {
$row[$needIndex[$i]] = $d;
}
}
$test[$row['groupId']][] = $row['amount'];
if (strpos($row['type'], "Dividend") !== false) {
$dividendArr[$row['groupId']] = $row;
} else {
if (!isset($taxArr[$row['groupId']])) {
$taxArr[$row['groupId']] = $row;
} else {
$taxArr[$row['groupId']]['amount'] += $row['amount'];
}
}
}
foreach ($dividendArr as $groupId => $dividend) {
$taxGet = $taxArr[$groupId]['amount'];
// $dividend['amount'] += $taxGet;
$taxPercentage = intval(round(abs($taxGet) / $dividend['amount'], 2) * 100);
$separate = explode(" ", $dividend['description']);
$perShare = floatval($separate[4]);
if (!$perShare) {
$perShare = floatval($separate[5]);
}
$date = explode(";", $dividend['date'])[0];
$date = substr($date, 0, 4) . "-" . substr($date, 4, 2) . "-" . substr($date, 6, 2);
$dividendObj = new DividendsData();
$dividendObj->setCount(intval($dividend['amount'] / $perShare));
$dividendObj->setIsin($dividend['isin']);
$dividendObj->setCurrency($dividend['currency']);
$dividendObj->setTax($taxPercentage);
$dividendObj->setValue($perShare);
$dividendObj->setDateTime(new \DateTime($date));
$ret[] = $dividendObj;
}
return $ret;
}
}
+45 -43
View File
@@ -3,43 +3,36 @@ declare(strict_types=1);
namespace App\Logic\Dividends;
use App\Helper\Currency;
use App\Helper\DataNeed\DividendValues;
use PhpOffice\PhpSpreadsheet\Shared\Date;
class SheetData
class SheetData extends SheetDataAll
{
private array $rawData;
// eg. 0 -> name, 1 -> ISIN
private array $dataIndex;
private array $data;
public function __construct(array $data)
{
$this->rawData = $data;
}
public function get() : array
public function takeData() : array
{
if (isset($this->data)) {
return $this->data;
}
$needIndex = [];
foreach ($this->rawData as $index => $rawDatum) {
foreach ($this->getRawData() as $index => $rawDatum) {
if ($index === 0) {
$this->dataIndex = $rawDatum;
continue;
}
$setDatas=[];
foreach ($rawDatum as $i => $data) {
$dataType = $this->getDataType($data,$setDatas);
if ($dataType){
$needIndex[$i] = $dataType;
foreach ($rawDatum as $i => $item) {
if (!$item) {
continue;
}
$keyName = DividendValues::searchForKeyName($item);
if ($keyName) {
$needIndex[$i] = $keyName;
}
}
break;
// continue;
}
unset($setDatas);
break;
// trigger_error(var_export($rawDatum,true));
}
$ret = [];
foreach ($this->rawData as $index => $datum) {
foreach ($this->getRawData() as $index => $datum) {
if ($index === 0) {
continue;
}
@@ -49,35 +42,30 @@ class SheetData
$row[$needIndex[$i]] = $d;
}
}
if (count($row) < 1) {
continue;
}
$date = &$row['date'];
if (is_int($date)) {
$date = Date::excelToDateTimeObject($date);
} else if (is_string($date)) {
$date = new \DateTime(str_replace(".", "-", $date));
}
if (is_float($row['tax'])) {
$row['tax'] = intval(round($row['tax']*100));
}
$dividendObj = new DividendsData();
$dividendObj->setCount($row['count']);
$dividendObj->setIsin($row['isin']);
$dividendObj->setCurrency($row['currency']);
$dividendObj->setTax($row['tax']);
$dividendObj->setValue($row['value']);
$dividendObj->setDateTime(new \DateTime(str_replace(".","-",$row['date'])));
$dividendObj->setDateTime($date);
$ret[] = $dividendObj;
}
$this->data = $ret;
return $ret;
}
public function getByCountry() : array
{
$data = $this->get();
if (!$data) {
return [];
}
$newArr = [];
/** @var DividendsData $datum */
foreach ($data as $datum) {
$key = mb_substr($datum->getIsin(), 0, 2);
$newArr[$key][] = $datum;
}
return $newArr;
}
public function takeColumnNames() : array
{
return $this->dataIndex;
@@ -118,10 +106,24 @@ class SheetData
if (in_array($data, Currency::$currencies)) {
return "currency";
}
if (count(explode(".", $data)) === 3) {
if (count(explode(".", $data)) === 3 || count(explode("-", $data)) === 3) {
return "date";
}
return null;
}
private function searchForDateIndex() : int
{
$ret = -1;
foreach ($this->takeColumnNames() as $index => $takeColumnName) {
if (strpos($takeColumnName,"DATE") !== false || strpos($takeColumnName,"DATA") !== false) {
$ret = $index;
break;
}
}
return $ret;
}
}
@@ -0,0 +1,83 @@
<?php
namespace App\Logic\Dividends;
abstract class SheetDataAll
{
protected int $countData;
/**
* @var array|DividendsData $data
*/
protected array $data = [];
private array $rawData;
/**
* Assign raw data to field, then create array of DividendsData
*
* @param array $rawData
*/
public function __construct(array $rawData)
{
$this->rawData = $rawData;
$this->countData = count($this->rawData) - 1;
$this->data = $this->takeData();
}
/**
* Get array grouped values by country
*
* @return array
*/
public function getByCountry() : array
{
$data = $this->getData();
if (!$data) {
return [];
}
$newArr = [];
/** @var DividendsData $datum */
foreach ($data as $datum) {
$key = mb_substr($datum->getIsin(), 0, 2);
$newArr[$key][] = $datum;
}
return $newArr;
}
/**
* Get count of rows
*
* @return int
*/
public function getCount(): int
{
return $this->countData;
}
/**
* Get array of DividendsData
*
* @return array
*/
protected function getData() : array
{
return $this->data;
}
/**
* Get array of raw data
*
* @return array
*/
protected function getRawData() : array
{
return $this->rawData;
}
/**
* Generate array of DividendsData from $rawData
*
* @return array
*/
abstract protected function takeData() : array;
}