Files
taxes/project/src/Logic/Crypto/MainObject.php
T
2023-04-12 15:13:55 +02:00

271 lines
6.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Logic\Crypto;
use App\Helper\Currency;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
class MainObject
{
const _CRYPTO_DIVIDER = 10000000;
private \DateTime $dateTime;
private string $desc;
private int $value;
private string $currency;
private ?string $realCurrency=null;
private int $realValue=0;
private float $price=0;
private bool $isCrypto = false;
private string $transactionType="";
private string $marketName="";
/**
* @return string
*/
public function getTransactionType(): string
{
return $this->transactionType;
}
/**
* @return float
*/
public function getRealValue(): float
{
return $this->realValue;
}
/**
* @param int $realValue
*/
public function setRealValue(int $realValue): void
{
$this->realValue = $realValue;
}
/**
* @return string|null
*/
public function getRealCurrency(): ?string
{
return $this->realCurrency;
}
/**
* @param string $realCurrency
*/
public function setRealCurrency(string $realCurrency): void
{
$this->realCurrency = $realCurrency;
}
/**
* @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 int
*/
public function getValue(): int
{
return $this->value;
}
/**
* @param int $value
*/
public function setValue(int $value): void
{
$this->value = $value;
}
/**
* @return string
*/
public function getCurrency(): string
{
return $this->currency;
}
/**
* @param string $currency
*/
public function setCurrency(string $currency): void
{
if (!in_array($currency, Currency::$currencies) && !in_array($currency, Currency::$currenciesCrypto)) {
$this->isCrypto = true;
}
$this->currency = $currency;
}
/**
* @param string $transactionType
*/
public function setTransactionType(string $transactionType): void
{
$this->transactionType = $transactionType;
}
/**
* @param string $marketName
*/
public function setMarketName(string $marketName): void
{
$this->marketName = $marketName;
}
public function addValue(int $val) : void
{
$this->value += $val;
}
public function addRealValue(float $val) : void
{
if (!isset($this->realValue)) {
$this->realValue = 0;
}
$this->realValue += $val;
}
public function takePrice(bool $force = false) : void
{
if ($this->price && !$force) {
return;
}
$real = $this->getRealValue() / 100;
$val = $this->getValue();
if ($this->isCrypto) {
$val = $this->getValueCrypto();
}
$price = round($real/$val,8);
// if ($this->isCrypto) {
// $price *= self::_CRYPTO_DIVIDER;
// } else {
// $price *= 100;
// }
$this->price = $price;
}
/**
* @param float|int $price
*/
public function setPrice($price): void
{
$this->price = $price;
}
public function getPrice() : float
{
return $this->price;
}
public function getValueCrypto() : float
{
return $this->value / self::_CRYPTO_DIVIDER;
}
public function subtract(MainObject $mainObject)
{
$this->value = $this->getValue() - $mainObject->getValue();
}
public function checkCurrency() : void
{
if (!$this->isCrypto) {
$this->value = intval($this->value/self::_CRYPTO_DIVIDER*100);
}
}
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);
if ($this->transactionType === "BUY") {
$this->setCurrency($cur1);
$this->setRealCurrency($cur2);
// $this->setPrice(-$this->getPrice());
} else {
$this->setCurrency($cur2);
$this->setRealCurrency($cur1);
}
} else {
foreach ($cryptoFiat as $item) {
$currReal = strpos($this->marketName, $item);
if ($currReal === false) {
continue;
}
if ($currReal > 0) {
$cur1 = substr($this->marketName, 0,strlen($this->marketName) - strlen($item));
$cur2 = $item;
} else {
$cur1 = $item;
$cur2 = substr($this->marketName, 0, strlen($this->marketName) - strlen($item));
}
if ($this->transactionType === "BUY") {
$this->setCurrency($cur1);
$this->setRealCurrency($cur2);
// $this->setPrice(-$this->getPrice());
} else {
$this->setCurrency($cur2);
$this->setRealCurrency($cur1);
}
}
}
if ($this->getRealCurrency() === "USDT") {
$this->setRealValue(-intval($this->getRealValue()));
}
}
}
public function currencyConversion() : void
{
if ($this->realCurrency === "PLN" || (!in_array($this->realCurrency, Currency::$currencies) && !in_array($this->realCurrency, Currency::$currenciesCrypto))) {
return;
}
$eur = 4.5755;
if ($this->realCurrency === "EUR") {
$this->setRealValue(intval(round($this->getRealValue() * $eur)));
$this->setRealCurrency("PLN");
$this->takePrice(true);
}
// if ($this->realCurrency === "USDT") {
// dd($this);
// $this->setRealValue(intval(round($this->getRealValue() * ($eur/$this->getPrice()))));
// $this->setRealCurrency("PLN");
// $this->takePrice(true);
// }
}
}