src/Entity/PointsCatalog/CatalogPointMovement.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Entity\PointsCatalog;
  3. use App\Annotation\SiteAware;
  4. use App\Entity\AbstractBase;
  5. use App\Entity\Garages\Garage;
  6. use App\Entity\Interfaces\SiteInterface;
  7. use App\Entity\MiniAbstractBase;
  8. use App\Entity\Traits\DescriptionTrait;
  9. use App\Entity\Traits\GarageTrait;
  10. use App\Entity\Traits\SiteTrait;
  11. use App\Entity\User;
  12. use App\Exception\PointsLimitException;
  13. use App\Repository\PointsCatalog\CatalogPointMovementRepository;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Gedmo\Mapping\Annotation as Gedmo;
  16. /**
  17.  * @ORM\Table(name="vulco_points_catalog_point_movement", indexes={@ORM\Index(name="catalog_point_movement_site_idx", columns={"site"})})
  18.  *
  19.  * @ORM\Entity(repositoryClass=CatalogPointMovementRepository::class)
  20.  *
  21.  * @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
  22.  *
  23.  * @SiteAware(siteFieldName="site")
  24.  */
  25. class CatalogPointMovement extends AbstractBase implements SiteInterface
  26. {
  27.     use DescriptionTrait;
  28.     use GarageTrait;
  29.     use SiteTrait;
  30.     public const TYPE_POSITIVE 'positive';
  31.     public const TYPE_NEGATIVE 'negative';
  32.     public const TYPE_EXPIRED 'expired';
  33.     /**
  34.      * @ORM\Column(type="string", length=255, nullable=true)
  35.      */
  36.     private ?string $description null;
  37.     /**
  38.      * @ORM\Column(type="date", nullable=false)
  39.      */
  40.     private \DateTimeInterface $date;
  41.     /**
  42.      * @ORM\Column(type="date", nullable=true)
  43.      */
  44.     private ?\DateTimeInterface $expiredAt null;
  45.     /**
  46.      * @ORM\Column(type="float", nullable=false)
  47.      */
  48.     private float $points;
  49.     /**
  50.      * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="pointMovements")
  51.      *
  52.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  53.      */
  54.     private User $user;
  55.     /**
  56.      * @ORM\ManyToOne(targetEntity="App\Entity\Garages\Garage")
  57.      *
  58.      * @ORM\JoinColumn(name="garage_id", referencedColumnName="id")
  59.      */
  60.     private Garage $garage;
  61.     /**
  62.      * @ORM\ManyToOne(targetEntity="App\Entity\PointsCatalog\CatalogConversionFormula")
  63.      *
  64.      * @ORM\JoinColumn(name="conversion_formula_id", referencedColumnName="id")
  65.      */
  66.     private CatalogConversionFormula $conversionFormula;
  67.     /**
  68.      * @ORM\ManyToOne(targetEntity="App\Entity\PointsCatalog\CatalogGiftProvider")
  69.      *
  70.      * @ORM\JoinColumn(name="provider_id", referencedColumnName="id")
  71.      */
  72.     private ?CatalogGiftProvider $provider null;
  73.     /**
  74.      * @ORM\Column(type="boolean", nullable=false, options={"default": 0})
  75.      */
  76.     private bool $returnMovement false;
  77.     public function getDate(): \DateTimeInterface
  78.     {
  79.         return $this->date;
  80.     }
  81.     public function getDateString(): string
  82.     {
  83.         return $this->getDateAsString($this->getDate());
  84.     }
  85.     public function setDate(\DateTimeInterface $date): self
  86.     {
  87.         $this->date $date;
  88.         return $this;
  89.     }
  90.     public function getExpiredAt(): ?\DateTimeInterface
  91.     {
  92.         return $this->expiredAt;
  93.     }
  94.     public function setExpiredAt(?\DateTimeInterface $date): self
  95.     {
  96.         $this->expiredAt $date;
  97.         return $this;
  98.     }
  99.     public function getPoints(): float
  100.     {
  101.         return $this->points;
  102.     }
  103.     /**
  104.      * @throws \RuntimeException
  105.      * @throws PointsLimitException
  106.      */
  107.     public function setPoints(float $pointsbool $isReturnMovement): self
  108.     {
  109.         if (!$this->getDate() instanceof \DateTimeInterface) {
  110.             throw new \RuntimeException('Invalid date');
  111.         }
  112.         // Si son puntos positivos (los que gasta son negativos) y no es una devolucion
  113.         // (IMP: EL LIMITE DE PUNTOS SE REFIERE A LOS PUNTOS QUE PUEDE CONSEGUIR, NO AL BALANCE)
  114.         // => comprobamos que no sobrepase el limite de puntos que puede conseguir en un año natural
  115.         if (!$isReturnMovement && ($points 0.00001)) {
  116.             // https://www.php.net/manual/en/language.types.float.php
  117.             $earnedGaragePoints $this->garage->getOwner()->getGarageEarnedPointsWithinCalendarYear($this->garage$this->date);
  118.             $pointsSum $earnedGaragePoints $points;
  119.             if ($pointsSum $this->garage->getPointsLimit()) {
  120.                 // we assign it only the points to reach the limit.
  121.                 $this->points $this->garage->getPointsLimit() - $earnedGaragePoints;
  122.                 throw new PointsLimitException();
  123.             }
  124.         }
  125.         $this->points $points;
  126.         return $this;
  127.     }
  128.     // only for fixtures purpouses
  129.     public function setFakedPoints(float $points): self
  130.     {
  131.         $this->points $points;
  132.         return $this;
  133.     }
  134.     public function getUser(): User
  135.     {
  136.         return $this->user;
  137.     }
  138.     public function setUser(User $user): self
  139.     {
  140.         $this->user $user;
  141.         return $this;
  142.     }
  143.     public function getConversionFormula(): CatalogConversionFormula
  144.     {
  145.         return $this->conversionFormula;
  146.     }
  147.     public function setConversionFormula(CatalogConversionFormula $conversionFormula): self
  148.     {
  149.         $this->conversionFormula $conversionFormula;
  150.         return $this;
  151.     }
  152.     public function getProvider(): ?CatalogGiftProvider
  153.     {
  154.         return $this->provider;
  155.     }
  156.     public function setProvider(?CatalogGiftProvider $provider): self
  157.     {
  158.         $this->provider $provider;
  159.         return $this;
  160.     }
  161.     public function isReturnMovement(): bool
  162.     {
  163.         return $this->returnMovement;
  164.     }
  165.     public function setReturnMovement(bool $returnMovement): self
  166.     {
  167.         $this->returnMovement $returnMovement;
  168.         return $this;
  169.     }
  170.     public function type(): string
  171.     {
  172.         if (!$this->expiredAt) {
  173.             // https://www.php.net/manual/en/language.types.float.php
  174.             return $this->points 0.00001 self::TYPE_POSITIVE self::TYPE_NEGATIVE;
  175.         }
  176.         return self::TYPE_EXPIRED;
  177.     }
  178.     public function __toString(): string
  179.     {
  180.         return $this->id $this->getId().' # '.$this->getDateAsString($this->getDate()).' # '.$this->getPoints() : MiniAbstractBase::DEFAULT_EMPTY_STRING;
  181.     }
  182. }