<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-06-26
* Time: 17:40
*/
namespace App\Entity\Sales;
use App\Entity\User;
use App\Repository\AccountTransactionRepository;
use Doctrine\ORM\Mapping as ORM;
use Money\Currency;
use Money\Money;
/**
* Все финансовые операции по аккаунту
*/
#[ORM\Table(name: 'account_transactions')]
#[ORM\Entity(repositoryClass: AccountTransactionRepository::class)]
class AccountTransaction
{
#[ORM\Id]
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
protected int $id;
#[ORM\JoinColumn(name: 'account_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'transactions')]
protected User $account;
#[ORM\Column(name: 'amount', type: 'integer')]
protected string $amount;
#[ORM\Column(name: 'currency', type: 'string', length: 3)]
protected string $currency;
#[ORM\Column(name: 'date', type: 'datetimetz_immutable')]
protected \DateTimeImmutable $date;
#[ORM\JoinColumn(name: 'charge_id', referencedColumnName: 'id')]
#[ORM\OneToOne(targetEntity: AccountCharge::class, inversedBy: 'transaction')]
protected ?AccountCharge $charge;
#[ORM\JoinColumn(name: 'enrollment_id', referencedColumnName: 'id')]
#[ORM\OneToOne(targetEntity: AccountEnrollment::class, inversedBy: 'transaction')]
protected ?AccountEnrollment $enrollment;
#[ORM\JoinColumn(name: 'refund_id', referencedColumnName: 'id')]
#[ORM\OneToOne(targetEntity: AccountRefund::class, inversedBy: 'transaction')]
protected ?AccountRefund $refund;
private function __construct(User $account, Money $money, \DateTimeImmutable $date)
{
$this->account = $account;
$this->amount = $money->getAmount();
$this->currency = $money->getCurrency()->getCode();
$this->date = $date;
}
public static function fromEnrollment(AccountEnrollment $enrollment): self
{
$transaction = new static($enrollment->getAccount(), $enrollment->getEnrolledMoney(), $enrollment->getDate());
$transaction->enrollment = $enrollment;
return $transaction;
}
public static function fromCharge(AccountCharge $charge): self
{
$transaction = new static($charge->getAccount(), $charge->getChargedMoney()->negative(), $charge->getDate());
$transaction->charge = $charge;
return $transaction;
}
public static function fromRefund(AccountRefund $refund): self
{
$transaction = new static($refund->getAccount(), $refund->getRefundedMoney(), $refund->getDate());
$transaction->refund = $refund;
return $transaction;
}
public function getId(): int
{
return $this->id;
}
public function getAccount(): User
{
return $this->account;
}
public function getMoney(): Money
{
return new Money($this->amount, new Currency($this->currency));
}
public function getDate(): \DateTimeImmutable
{
return $this->date;
}
public function getDescription(): string
{
return '';
}
public function getCharge(): ?AccountCharge
{
return $this->charge;
}
public function getEnrollment(): ?AccountEnrollment
{
return $this->enrollment;
}
public function getRefund(): ?AccountRefund
{
return $this->refund;
}
}