From 8630957dd94de85653bf77a7becd476cb87d168c Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Mon, 11 Nov 2024 10:09:01 +0100 Subject: [PATCH] Use financement entity to hold money amounts drawed between wallet statements --- src/Entity/WalletFinancement.php | 79 ++++++++++++ src/Entity/WalletStatement.php | 122 +++++++++++------- .../WalletFinancementRepository.php | 43 ++++++ 3 files changed, 197 insertions(+), 47 deletions(-) create mode 100644 src/Entity/WalletFinancement.php create mode 100644 src/Repository/WalletFinancementRepository.php diff --git a/src/Entity/WalletFinancement.php b/src/Entity/WalletFinancement.php new file mode 100644 index 0000000..ea13819 --- /dev/null +++ b/src/Entity/WalletFinancement.php @@ -0,0 +1,79 @@ +id; + } + + public function getMoney(): ?Money + { + return $this->money; + } + + public function setMoney(Money $money): static + { + $this->money = $money; + + return $this; + } + + public function getOrigin(): ?WalletStatement + { + return $this->origin; + } + + public function setOrigin(?WalletStatement $origin): static + { + $this->origin = $origin; + + return $this; + } + + public function getTarget(): ?WalletStatement + { + return $this->target; + } + + public function setTarget(?WalletStatement $target): static + { + $this->target = $target; + + return $this; + } +} diff --git a/src/Entity/WalletStatement.php b/src/Entity/WalletStatement.php index 4f109f4..12625bb 100644 --- a/src/Entity/WalletStatement.php +++ b/src/Entity/WalletStatement.php @@ -2,7 +2,7 @@ namespace App\Entity; -use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata as API; use App\Entity\Accounting\Transaction; use App\Gateway\Wallet\StatementDirection; use App\Repository\WalletStatementRepository; @@ -10,7 +10,15 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -#[ApiResource()] +/** + * WalletStatements are used by the Wallet Gateway to track available balances and money movements.\ + * \ + * Statements are relative to a Transaction, which either:\ + * \ + * a. Targets an User's Accounting, for which an incoming Statement will be created and which will hold the money until it is spent.\ + * b. Originates from an User's Accounting via the Wallet Gateway, which will create an outgoing Statement with money financed from previous incoming statements. + */ +#[API\ApiResource()] #[ORM\Entity(repositoryClass: WalletStatementRepository::class)] class WalletStatement { @@ -23,33 +31,44 @@ class WalletStatement #[ORM\JoinColumn(nullable: false)] private ?Transaction $transaction = null; + /** + * An `incoming` direction Statement means the Transaction saved money into the wallet.\ + * An `outgoing` direction Statement means the Transaction spent money from the wallet. + */ #[ORM\Column(enumType: StatementDirection::class)] private ?StatementDirection $direction = null; + /** + * For incoming Statements, the balance tells the available money.\ + * i.e: money not used yet by outgoing Statements. + */ #[ORM\Embedded(class: Money::class)] private ?Money $balance = null; /** - * The Statements from which the money in the Transaction was drawed out of. - * - * @var Collection + * A collection of Financements for outgoing Statements that were made using money from this Statement. + * + * Only not empty for incoming Statements. + * + * @var Collection */ - #[ORM\ManyToMany(targetEntity: WalletStatement::class, mappedBy: 'financesTo')] - private Collection $financedBy; + #[ORM\OneToMany(mappedBy: 'origin', targetEntity: WalletFinancement::class)] + private Collection $financesTo; /** - * The Statements that drawed money out of this Statement for their Transactions.\ - * Inverse of `financedBy`. - * - * @var Collection + * A collection of Financements from incoming Statements that provided money for this Statement. + * + * Only not empty for outgoing Statements. + * + * @var Collection */ - #[ORM\ManyToMany(targetEntity: WalletStatement::class, inversedBy: 'financedBy')] - private Collection $financesTo; + #[ORM\OneToMany(mappedBy: 'target', targetEntity: WalletFinancement::class)] + private Collection $financedBy; public function __construct() { - $this->financedBy = new ArrayCollection(); $this->financesTo = new ArrayCollection(); + $this->financedBy = new ArrayCollection(); } public function getId(): ?int @@ -94,52 +113,61 @@ class WalletStatement } /** - * @return Collection - */ - public function getFinancedBy(): Collection - { - return $this->financedBy; - } - - public function addFinancedBy(WalletStatement $financedBy): static - { - if (!$this->financedBy->contains($financedBy)) { - $this->financedBy->add($financedBy); - $financedBy->addFinanceTo($this); - } - - return $this; - } - - public function removeFinancedBy(WalletStatement $financedBy): static - { - if ($this->financedBy->removeElement($financedBy)) { - $financedBy->removeFinanceTo($this); - } - - return $this; - } - - /** - * @return Collection + * @return Collection */ public function getFinancesTo(): Collection { return $this->financesTo; } - public function addFinanceTo(WalletStatement $finance): static + public function addFinancesTo(WalletFinancement $financesTo): static { - if (!$this->financesTo->contains($finance)) { - $this->financesTo->add($finance); + if (!$this->financesTo->contains($financesTo)) { + $this->financesTo->add($financesTo); + $financesTo->setOrigin($this); } return $this; } - public function removeFinanceTo(WalletStatement $finance): static + public function removeFinancesTo(WalletFinancement $financesTo): static { - $this->financesTo->removeElement($finance); + if ($this->financesTo->removeElement($financesTo)) { + // set the owning side to null (unless already changed) + if ($financesTo->getOrigin() === $this) { + $financesTo->setOrigin(null); + } + } + + return $this; + } + + /** + * @return Collection + */ + public function getFinancedBy(): Collection + { + return $this->financedBy; + } + + public function addFinancedBy(WalletFinancement $financedBy): static + { + if (!$this->financedBy->contains($financedBy)) { + $this->financedBy->add($financedBy); + $financedBy->setTarget($this); + } + + return $this; + } + + public function removeFinancedBy(WalletFinancement $financedBy): static + { + if ($this->financedBy->removeElement($financedBy)) { + // set the owning side to null (unless already changed) + if ($financedBy->getTarget() === $this) { + $financedBy->setTarget(null); + } + } return $this; } diff --git a/src/Repository/WalletFinancementRepository.php b/src/Repository/WalletFinancementRepository.php new file mode 100644 index 0000000..7c2729e --- /dev/null +++ b/src/Repository/WalletFinancementRepository.php @@ -0,0 +1,43 @@ + + */ +class WalletFinancementRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, WalletFinancement::class); + } + +// /** +// * @return WalletFinancement[] Returns an array of WalletFinancement objects +// */ +// public function findByExampleField($value): array +// { +// return $this->createQueryBuilder('w') +// ->andWhere('w.exampleField = :val') +// ->setParameter('val', $value) +// ->orderBy('w.id', 'ASC') +// ->setMaxResults(10) +// ->getQuery() +// ->getResult() +// ; +// } + +// public function findOneBySomeField($value): ?WalletFinancement +// { +// return $this->createQueryBuilder('w') +// ->andWhere('w.exampleField = :val') +// ->setParameter('val', $value) +// ->getQuery() +// ->getOneOrNullResult() +// ; +// } +}