Initial commit

This commit is contained in:
2022-03-25 16:42:41 +01:00
commit 34685838bc
235 changed files with 12766 additions and 0 deletions
+76
View File
@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace App\Logic\Crypto;
class MainObject
{
private \DateTime $dateTime;
private string $desc;
private float $value;
private string $currency;
/**
* @return \DateTime
*/
public function getDateTime(): \DateTime
{
return $this->dateTime;
}
/**
* @param \DateTime $dateTime
*/
public function setDateTime(\DateTime $dateTime): void
{
$this->dateTime = $dateTime;
}
/**
* @return string
*/
public function getDesc(): string
{
return $this->desc;
}
/**
* @param string $desc
*/
public function setDesc(string $desc): void
{
$this->desc = $desc;
}
/**
* @return float
*/
public function getValue(): float
{
return $this->value;
}
/**
* @param float $value
*/
public function setValue(float $value): void
{
$this->value = $value;
}
/**
* @return string
*/
public function getCurrency(): string
{
return $this->currency;
}
/**
* @param string $currency
*/
public function setCurrency(string $currency): void
{
$this->currency = $currency;
}
}
@@ -0,0 +1,109 @@
<?php
declare(strict_types=1);
namespace App\Logic\Dividends;
final class DividendsData
{
private int $tax;
private int $count;
private float $value;
private string $currency;
private string $isin;
private \DateTime $dateTime;
/**
* @return string
*/
public function getIsin(): string
{
return $this->isin;
}
/**
* @param string $isin
*/
public function setIsin(string $isin): void
{
$this->isin = $isin;
}
/**
* @return \DateTime
*/
public function getDateTime(): \DateTime
{
return $this->dateTime;
}
/**
* @param \DateTime $dateTime
*/
public function setDateTime(\DateTime $dateTime): void
{
$this->dateTime = $dateTime;
}
/**
* @return int
*/
public function getTax(): int
{
return $this->tax;
}
/**
* @param int $tax
*/
public function setTax(int $tax): void
{
$this->tax = $tax;
}
/**
* @return int
*/
public function getCount(): int
{
return $this->count;
}
/**
* @param int $count
*/
public function setCount(int $count): void
{
$this->count = $count;
}
/**
* @return float
*/
public function getValue(): float
{
return $this->value;
}
/**
* @param float $value
*/
public function setValue(float $value): void
{
$this->value = $value;
}
/**
* @return string
*/
public function getCurrency(): string
{
return $this->currency;
}
/**
* @param string $currency
*/
public function setCurrency(string $currency): void
{
$this->currency = $currency;
}
}
@@ -0,0 +1,109 @@
<?php
declare(strict_types=1);
namespace App\Logic\Dividends;
use App\Helper\Currency;
use Doctrine\ORM\EntityManager;
class DividendsSummary
{
/** @var SheetData|array $sheetData */
private array $sheetData=[];
/** @var XMLReader|array $sheetData */
private array $xmlData=[];
private Currency $currencyObj;
public array $error=[];
private array $allData=[];
private array $countryToCurrency=[];
public function __construct(EntityManager $manager)
{
$this->currencyObj = new Currency($manager);
}
public function getSummaryByCountry() : array
{
$return=[];
$this->countFromSheets();
if (count($this->allData) > 0) {
$sum = 0;
foreach ($this->allData as $key => $item) {
$val = round($item, 2);
$sum += $val;
$return[$key] = [
"country" => \Locale::getDisplayRegion("-".$key),
"value" => $val,
"currency" => $this->countryToCurrency[$key]
];
}
$return['sum'] = intval(ceil($sum));
}
return $return;
}
private function countFromSheets() : void
{
if (!count($this->sheetData)) {
return;
}
$test=[];
$countryToCurrency=[];
/** @var SheetData $sheetDatum */
foreach ($this->sheetData as $sheetDatum) {
$all = $sheetDatum->getByCountry();
foreach ($all as $country => $item) {
/** @var DividendsData $val */
foreach ($item as $val) {
if (!isset($test[$country])) $test[$country] = 0;
$test[$country] += $this->count($val);
if (!isset($countryToCurrency[$country])) {
$countryToCurrency[$country] = $val->getCurrency();
}
}
}
}
$this->allData = array_merge($this->allData,$test);
$this->countryToCurrency = array_merge($this->countryToCurrency,$countryToCurrency);
}
private function getRate(DividendsData $data) : float
{
return $this->currencyObj->getRate($data->getCurrency(),$data->getDateTime(),$this->error);
}
private function count(DividendsData $data) : float
{
$value = round($data->getCount()*$data->getValue(),2);
$tax = 19;
if ($data->getTax() > 0) {
$tax = 4;
}
$valueTax = round($value * $tax / 100);
$rate = $this->getRate($data);
if ($rate > 0) {
return $valueTax * $rate;
}
$this->error[]=$data;
return 0;
}
/**
* @param SheetData $sheetData
*/
public function addSheetData(SheetData $sheetData): void
{
$this->sheetData[] = $sheetData;
}
/**
* @param XMLReader $reader
*/
public function addXml(XMLReader $reader)
{
$this->xmlData[] = $reader;
}
}
+127
View File
@@ -0,0 +1,127 @@
<?php
declare(strict_types=1);
namespace App\Logic\Dividends;
use App\Helper\Currency;
class SheetData
{
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
{
if (isset($this->data)) {
return $this->data;
}
$needIndex = [];
foreach ($this->rawData 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;
}
}
unset($setDatas);
break;
}
$ret = [];
foreach ($this->rawData as $index => $datum) {
if ($index === 0) {
continue;
}
$row=[];
foreach ($datum as $i => $d) {
if (isset($needIndex[$i])) {
$row[$needIndex[$i]] = $d;
}
}
$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'])));
$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;
}
private function getDataType($data,&$setDatas) : ?string
{
if (is_int($data)){
$ret = "count";
$count = &$setDatas['int'];
if ($count===1){
$ret = "tax";
}
if ($count > 1) {
$ret = null;
}
$count++;
return $ret;
}
if (is_float($data)) {
$ret = "value";
$count = &$setDatas['float'];
if ($count > 0) {
$ret = null;
}
$count++;
return $ret;
}
if (in_array(mb_substr($data, 0, 2), ['CA', 'US', 'IE', 'GB']) && intval(mb_substr($data, 2, 1))>0) {
$count = &$setDatas['data'];
$ret = "isin";
if ($count > 0) {
$ret = null;
}
$count++;
return $ret;
}
if (in_array($data, Currency::$currencies)) {
return "currency";
}
if (count(explode(".", $data)) === 3) {
return "date";
}
return null;
}
}
+85
View File
@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace App\Logic\Dividends;
class XMLReader
{
private string $filePath;
private array $datas=[];
public function __construct(string $file)
{
if (file_exists($file)){
$this->filePath = $file;
}
}
public function readFile()
{
if (!is_file($this->filePath)) {
return;
}
$a = $this->tryCashTransaction();
$xml = simplexml_load_file($this->filePath, null, LIBXML_NOCDATA);
$json = json_encode($xml);
$arr = json_decode($json, true);
$this->findValues($arr);
}
private function tryCashTransaction()
{
$obj = new \DOMDocument();
$obj->load($this->filePath);
$node = $obj->getElementsByTagName("CashTransaction");
if (count($node) < 1) {
return;
}
foreach ($node as $item) {
$values = explode(" ", $item->getAttribute('description'));
dd($values);
}
}
private function findValues(array $arr)
{
$obj = new DividendsData();
$isset=false;
foreach ($arr as $key => $value) {
if (is_array($value)) {
$this->findValues($value);
} else {
$isset=true;
if ($key === "currency") {
$obj->setCurrency($value);
}
if ($key === "payDate") {
$tmp = str_split($value, 4);
$y=$tmp[0];
$tmp = str_split($tmp[1],2);
$d = $tmp[1];
$m = $tmp[0];
// $a = implode("-", array_merge(str_split($tmp[1],2),$tmp[0]));
// print_r($a);
$obj->setDateTime(new \DateTime("$y-$m-$d"));
}
if ($key === "quantity") {
$obj->setCount(intval($value));
}
if ($key === "isin") {
$obj->setIsin($value);
}
if ($key === "grossRate") {
$obj->setValue(floatval($value));
}
}
}
if ($isset) {
$this->datas[] = $obj;
}
}
}