src/Entity/Sales/AccountTransaction.php line 21

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-06-26
  5.  * Time: 17:40
  6.  */
  7. namespace App\Entity\Sales;
  8. use App\Entity\User;
  9. use App\Repository\AccountTransactionRepository;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Money\Currency;
  12. use Money\Money;
  13. /**
  14.  * Все финансовые операции по аккаунту
  15.  */
  16. #[ORM\Table(name'account_transactions')]
  17. #[ORM\Entity(repositoryClassAccountTransactionRepository::class)]
  18. class AccountTransaction
  19. {
  20.     #[ORM\Id]
  21.     #[ORM\Column(name'id'type'integer')]
  22.     #[ORM\GeneratedValue(strategy'AUTO')]
  23.     protected int $id;
  24.     #[ORM\JoinColumn(name'account_id'referencedColumnName'id')]
  25.     #[ORM\ManyToOne(targetEntityUser::class, inversedBy'transactions')]
  26.     protected User $account;
  27.     #[ORM\Column(name'amount'type'integer')]
  28.     protected string $amount;
  29.     #[ORM\Column(name'currency'type'string'length3)]
  30.     protected string $currency;
  31.     #[ORM\Column(name'date'type'datetimetz_immutable')]
  32.     protected \DateTimeImmutable $date;
  33.     #[ORM\JoinColumn(name'charge_id'referencedColumnName'id')]
  34.     #[ORM\OneToOne(targetEntityAccountCharge::class, inversedBy'transaction')]
  35.     protected ?AccountCharge $charge;
  36.     #[ORM\JoinColumn(name'enrollment_id'referencedColumnName'id')]
  37.     #[ORM\OneToOne(targetEntityAccountEnrollment::class, inversedBy'transaction')]
  38.     protected ?AccountEnrollment $enrollment;
  39.     #[ORM\JoinColumn(name'refund_id'referencedColumnName'id')]
  40.     #[ORM\OneToOne(targetEntityAccountRefund::class, inversedBy'transaction')]
  41.     protected ?AccountRefund $refund;
  42.     private function __construct(User $accountMoney $money\DateTimeImmutable $date)
  43.     {
  44.         $this->account $account;
  45.         $this->amount $money->getAmount();
  46.         $this->currency $money->getCurrency()->getCode();
  47.         $this->date $date;
  48.     }
  49.     public static function fromEnrollment(AccountEnrollment $enrollment): self
  50.     {
  51.         $transaction = new static($enrollment->getAccount(), $enrollment->getEnrolledMoney(), $enrollment->getDate());
  52.         $transaction->enrollment $enrollment;
  53.         return $transaction;
  54.     }
  55.     public static function fromCharge(AccountCharge $charge): self
  56.     {
  57.         $transaction = new static($charge->getAccount(), $charge->getChargedMoney()->negative(), $charge->getDate());
  58.         $transaction->charge $charge;
  59.         return $transaction;
  60.     }
  61.     public static function fromRefund(AccountRefund $refund): self
  62.     {
  63.         $transaction = new static($refund->getAccount(), $refund->getRefundedMoney(), $refund->getDate());
  64.         $transaction->refund $refund;
  65.         return $transaction;
  66.     }
  67.     public function getId(): int
  68.     {
  69.         return $this->id;
  70.     }
  71.     public function getAccount(): User
  72.     {
  73.         return $this->account;
  74.     }
  75.     public function getMoney(): Money
  76.     {
  77.         return new Money($this->amount, new Currency($this->currency));
  78.     }
  79.     public function getDate(): \DateTimeImmutable
  80.     {
  81.         return $this->date;
  82.     }
  83.     public function getDescription(): string
  84.     {
  85.         return '';
  86.     }
  87.     public function getCharge(): ?AccountCharge
  88.     {
  89.         return $this->charge;
  90.     }
  91.     public function getEnrollment(): ?AccountEnrollment
  92.     {
  93.         return $this->enrollment;
  94.     }
  95.     public function getRefund(): ?AccountRefund
  96.     {
  97.         return $this->refund;
  98.     }
  99. }