src/Entity/Operation/Operation.php line 77

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Operation;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiProperty;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  7. use App\Entity\Cart\OperationCart;
  8. use App\Entity\Company\Company;
  9. use App\Entity\Eligibility;
  10. use App\Entity\Participation\Participation;
  11. use App\Entity\Recipe\Ingredient;
  12. use App\Entity\Segmentation\Department;
  13. use App\Entity\State;
  14. use App\Entity\User\User;
  15. use App\Repository\Operation\OperationRepository;
  16. use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
  17. use Doctrine\Common\Collections\ArrayCollection;
  18. use Doctrine\Common\Collections\Collection;
  19. use Doctrine\ORM\Mapping as ORM;
  20. use Gedmo\Timestampable\Traits\TimestampableEntity;
  21. use Ramsey\Uuid\Doctrine\UuidGenerator;
  22. use Symfony\Component\Serializer\Annotation\Groups;
  23. use Symfony\Component\Serializer\Annotation\SerializedName;
  24. use Symfony\Component\Validator\Constraints as Assert;
  25. /**
  26.  * @ORM\Entity(repositoryClass=OperationRepository::class)
  27.  *
  28.  * @ApiResource(
  29.  *     attributes={
  30.  *          "normalization_context"={"groups"={
  31.  *              "Operation:output",
  32.  *              "Operation:io",
  33.  *              "ParticipationStep:io",
  34.  *              "ParticipationOdr:io",
  35.  *              "get_participation_odrs_by_user",
  36.  *          }},
  37.  *          "denormalization_context"={"groups"={
  38.  *              "Operation:input",
  39.  *              "Operation:io",
  40.  *              "ParticipationStep:io",
  41.  *              "ParticipationOdr:io",
  42.  *              "get_participation_odrs_by_user",
  43.  *          }}
  44.  *      },
  45.  *     collectionOperations={
  46.  *          "post"={
  47.  *              "method"="POST",
  48.  *              "access_control"="is_granted('ROLE_ADMIN_SOGEC')",
  49.  *          },
  50.  *     },
  51.  *     itemOperations={
  52.  *          "get"={
  53.  *              "method"="GET",
  54.  *              "access_control"="is_granted('ROLE_USER')",
  55.  *          },
  56.  *      }
  57.  * )
  58.  *
  59.  * @ORM\InheritanceType("SINGLE_TABLE")
  60.  * @ORM\DiscriminatorColumn(name="discr", type="string")
  61.  * @ORM\DiscriminatorMap({
  62.  *     "coupon" = "Coupon",
  63.  *     "step" = "Step",
  64.  *     "game" = "Game",
  65.  *     "refund" = "Refund",
  66.  *     "puntos" = "Puntos",
  67.  *     "draw" = "Draw",
  68.  *     "odr" = "Odr",
  69.  *     "sponsorship" = "Sponsorship",
  70.  *     "multi_offer" = "MultiOffer"
  71.  * })
  72.  *
  73.  */
  74. abstract class Operation
  75. {
  76.     /**
  77.      * Hook timestampable behavior
  78.      * updates createdAt, updatedAt fields
  79.      */
  80.     use TimestampableEntity;
  81.     public const ARRAY_OPERATION_TYPE = [
  82.         MultiOffer::class,
  83.         Coupon::class,
  84.         Odr::class,
  85.         Step::class,
  86.         Refund::class,
  87.         Game::class,
  88.         Draw::class,
  89.         Puntos::class,
  90.         Sponsorship::class,
  91.     ];
  92.     public const ARRAY_OFFER_MULTICANAL = [
  93.         MultiOffer::class,
  94.         Coupon::class,
  95.         Odr::class,
  96.     ];
  97.     /**
  98.      * @ORM\Id
  99.      * @ORM\Column(type="uuid", unique=true)
  100.      * @ORM\GeneratedValue(strategy="CUSTOM")
  101.      * @ORM\CustomIdGenerator(class=UuidGenerator::class)
  102.      * @SerializedName("id")
  103.      * @ApiProperty(identifier=true)
  104.      * @Assert\Uuid()
  105.      * @Groups({
  106.      *     "Operation:output",
  107.      *     "OperationCoupon:output",
  108.      *     "OperationGame:output",
  109.      *     "OperationRefund:output",
  110.      *     "OperationDraw:output",
  111.      *     "OperationOdr:output",
  112.      *     "OperationMultiOffer:output",
  113.      *     "ParticipationStep:io",
  114.      *     "ParticipationMulti:io",
  115.      *     "get_user_operation_carts",
  116.      *     "DetailsStep:io",
  117.      *     "get_all_public_steps",
  118.      *     "get_all_public_puntos",
  119.      *     "get_all_public_sponsorship",
  120.      *     "get_participation_odrs_by_user",
  121.      *     "Recipe:io",
  122.      * })
  123.      */
  124.     private string $id;
  125.     /**
  126.      * This is the sogec code of the operation ( ex : AD34 or XX00 )
  127.      * @var string|null
  128.      *
  129.      * @ORM\Column(name="code", type="string", nullable=true)
  130.      * @SerializedName("code")
  131.      * @Groups({
  132.      *     "Operation:io",
  133.      *     "OperationStep:io",
  134.      *     "OperationCoupon:io",
  135.      *     "OperationGame:io",
  136.      *     "OperationOdr:io",
  137.      *     "OperationMultiOffer:io",
  138.      *     "OperationRefund:io",
  139.      *     "OperationPuntos:io",
  140.      *     "OperationSponsorship:io",
  141.      *     "OperationDraw:output",
  142.      *     "get_all_public_coupons",
  143.      *     "get_coupons_by_user_department",
  144.      *     "get_user_operation_carts",
  145.      *     "get_participation_odrs_by_user",
  146.      *     "Recipe:io",
  147.      * })
  148.      */
  149.     private ?string $code;
  150.     /**
  151.      * This a code to call api ( may the uuid of the offer )
  152.      * @var string|null
  153.      *
  154.      * @ORM\Column(name="operation_code", type="string", nullable=true)
  155.      * @SerializedName("operation_code")
  156.      * @Groups({
  157.      *     "Operation:io",
  158.      *     "OperationCoupon:io",
  159.      *     "OperationStep:io",
  160.      *     "OperationGame:io",
  161.      *     "OperationOdr:io",
  162.      *     "OperationMultiOffer:io",
  163.      *     "OperationRefund:io",
  164.      *     "OperationPuntos:io",
  165.      *     "OperationSponsorship:io",
  166.      *     "OperationDraw:output",
  167.      *     "get_all_public_coupons",
  168.      *     "get_coupons_by_user_department",
  169.      *     "Recipe:io",
  170.      * })
  171.      */
  172.     private ?string $operationCode;
  173.     /**
  174.      * @var string
  175.      *
  176.      * @ORM\Column(name="title", type="string", nullable=true)
  177.      * @SerializedName("title")
  178.      * @Assert\NotBlank(
  179.      *      groups={
  180.      *          "Coupon:input",
  181.      *     }
  182.      * )
  183.      * @Groups({
  184.      *     "Operation:io",
  185.      *     "OperationCoupon:io",
  186.      *     "OperationStep:io",
  187.      *     "OperationGame:io",
  188.      *     "OperationOdr:io",
  189.      *     "OperationMultiOffer:io",
  190.      *     "OperationRefund:io",
  191.      *     "OperationPuntos:io",
  192.      *     "OperationSponsorship:io",
  193.      *     "OperationDraw:output",
  194.      *     "get_all_public_coupons",
  195.      *     "get_coupons_by_user_department",
  196.      *     "OperationStep:io",
  197.      *     "get_user_operation_carts",
  198.      *     "get_participation_odrs_by_user",
  199.      *     "get_participation_multi_odrs_by_user",
  200.      *     "Recipe:io",
  201.      * })
  202.      */
  203.     private string $title;
  204.     /**
  205.      * @ORM\ManyToMany(targetEntity="App\Entity\Segmentation\Department", inversedBy="coupons")
  206.      * @SerializedName("departments")
  207.      * @Groups({
  208.      *     "OperationCoupon:output",
  209.      *     "get_coupons_by_user_department",
  210.      * })
  211.      */
  212.     public Collection $departments;
  213.     /**
  214.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="operations")
  215.      * @ORM\JoinColumn(nullable=false)
  216.      * @SerializedName("owner")
  217.      */
  218.     private $owner;
  219.     /**
  220.      * @ORM\ManyToOne(targetEntity=State::class, inversedBy="operations")
  221.      * @ORM\JoinColumn(nullable=true)
  222.      * @SerializedName("state")
  223.      *
  224.      * @ApiFilter(SearchFilter::class, properties={"state.label": "partial"})
  225.      */
  226.     private $state;
  227.     /**
  228.      * @ORM\ManyToMany(targetEntity=Participation::class, mappedBy="operations")
  229.      */
  230.     private $participations;
  231.     /**
  232.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="operations")
  233.      * @ORM\JoinColumn(nullable=false)
  234.      */
  235.     private $company;
  236.     /**
  237.      * @ORM\OneToMany(targetEntity=OperationCart::class, mappedBy="operation")
  238.      */
  239.     private Collection $carts;
  240.     /**
  241.      * @ORM\ManyToMany(targetEntity=Eligibility::class, mappedBy="operation")
  242.      * @SerializedName("eligibilities")
  243.      * @Groups({
  244.      *     "Operation:io",
  245.      *     "OperationCoupon:io",
  246.      *     "get_all_public_coupons",
  247.      *     "get_coupons_by_user_department",
  248.      *     "Recipe:io",
  249.      *     "OperationOdr:io",
  250.      * })
  251.      */
  252.     private Collection $eligibilities;
  253.     /**
  254.      * @ORM\Column(type="float", nullable=true)
  255.      * @Groups({
  256.      *     "OperationMultiOffer:io",
  257.      *     "get_participation_multi_odrs_by_user",
  258.      *     "OperationOdr:io",
  259.      *     "get_participation_odrs_by_user",
  260.      *     "Recipe:io",
  261.      * })
  262.      */
  263.     private ?float $amount;
  264.     /**
  265.      * @ORM\ManyToMany(targetEntity=Ingredient::class, mappedBy="offers")
  266.      */
  267.     private $ingredients;
  268.     /**
  269.      * @SerializedName("nb_participations")
  270.      * @Groups({
  271.      *     "OperationMultiOffer:io",
  272.      *     "OperationOdr:io",
  273.      *     "OperationCoupon:io",
  274.      * })
  275.      */
  276.     private int $nbParticipations 0;
  277.     public function __construct()
  278.     {
  279.         $this->departments = new ArrayCollection();
  280.         $this->participations = new ArrayCollection();
  281.         $this->carts = new ArrayCollection();
  282.         $this->eligibilities = new ArrayCollection();
  283.         $this->ingredients = new ArrayCollection();
  284.     }
  285.     /**
  286.      * @param string $id
  287.      * @return Operation
  288.      */
  289.     public function setId(string $id): Operation
  290.     {
  291.         $this->id $id;
  292.         return $this;
  293.     }
  294.     /**
  295.      * @return string
  296.      */
  297.     public function getId(): string
  298.     {
  299.         return $this->id;
  300.     }
  301.     /**
  302.      * @return string|null
  303.      */
  304.     public function getOperationCode(): ?string
  305.     {
  306.         return $this->operationCode;
  307.     }
  308.     /**
  309.      * @param string|null $operationCode
  310.      * @return Operation
  311.      */
  312.     public function setOperationCode(?string $operationCode): Operation
  313.     {
  314.         $this->operationCode $operationCode;
  315.         return $this;
  316.     }
  317.     /**
  318.      * @return string|null
  319.      */
  320.     public function getCode(): ?string
  321.     {
  322.         return $this->code;
  323.     }
  324.     /**
  325.      * @param string|null $code
  326.      *
  327.      * @return Operation
  328.      */
  329.     public function setCode(?string $code): Operation
  330.     {
  331.         $this->code $code;
  332.         return $this;
  333.     }
  334.     /**
  335.      * @return string|null
  336.      */
  337.     public function getTitle(): ?string
  338.     {
  339.         return $this->title;
  340.     }
  341.     /**
  342.      * @param string $title
  343.      *
  344.      * @return Operation
  345.      */
  346.     public function setTitle(string $title): Operation
  347.     {
  348.         $this->title $title;
  349.         return $this;
  350.     }
  351.     /**
  352.      * @return Collection|null
  353.      */
  354.     public function getDepartments(): ?Collection
  355.     {
  356.         return $this->departments;
  357.     }
  358.     /**
  359.      * @param Department $department
  360.      *
  361.      * @return $this
  362.      */
  363.     public function addDepartment(Department $department): Operation
  364.     {
  365.         if ($department !== null && $this->departments->contains($department)) {
  366.             return $this;
  367.         }
  368.         $this->departments[] = $department;
  369.         return $this;
  370.     }
  371.     /**
  372.      * @param Department $department
  373.      *
  374.      * @return Coupon
  375.      */
  376.     public function removeDepartment(Department $department): Operation
  377.     {
  378.         $this->departments->removeElement($department);
  379.         return $this;
  380.     }
  381.     /**
  382.      * @return Coupon
  383.      */
  384.     public function removeAllDepartment(): Operation
  385.     {
  386.         $this->departments = new ArrayCollection();
  387.         return $this;
  388.     }
  389.     /**
  390.      * @return mixed
  391.      */
  392.     public function getOwner()
  393.     {
  394.         return $this->owner;
  395.     }
  396.     /**
  397.      * @param mixed $owner
  398.      *
  399.      * @return Operation
  400.      */
  401.     public function setOwner($owner)
  402.     {
  403.         $this->owner $owner;
  404.         return $this;
  405.     }
  406.     /**
  407.      * @return mixed
  408.      */
  409.     public function getState()
  410.     {
  411.         return $this->state;
  412.     }
  413.     /**
  414.      * @param mixed $state
  415.      *
  416.      * @return Operation
  417.      */
  418.     public function setState($state)
  419.     {
  420.         $this->state $state;
  421.         return $this;
  422.     }
  423.     /**
  424.      * @return Collection|Participation[]
  425.      */
  426.     public function getParticipations(): Collection
  427.     {
  428.         return $this->participations;
  429.     }
  430.     /**
  431.      * @return Collection|OperationCart[]
  432.      */
  433.     public function getCarts(): Collection
  434.     {
  435.         return $this->carts;
  436.     }
  437.     public function addCart(OperationCart $cart): self
  438.     {
  439.         if (!$this->carts->contains($cart)) {
  440.             $this->carts[] = $cart;
  441.             $cart->setOperation($this);
  442.         }
  443.         return $this;
  444.     }
  445.     public function removeCart(OperationCart $cart): self
  446.     {
  447.         if ($this->carts->removeElement($cart)) {
  448.             // set the owning side to null (unless already changed)
  449.             if ($cart->getOperation() === $this) {
  450.                 $cart->setOperation(null);
  451.             }
  452.         }
  453.         return $this;
  454.     }
  455.     /**
  456.      * @return Collection|Eligibility[]
  457.      */
  458.     public function getEligibilities(): Collection
  459.     {
  460.         return $this->eligibilities;
  461.     }
  462.     public function addEligibility(Eligibility $eligibility): self
  463.     {
  464.         if (!$this->eligibilities->contains($eligibility)) {
  465.             $this->eligibilities[] = $eligibility;
  466.             $eligibility->addOperation($this);
  467.         }
  468.         return $this;
  469.     }
  470.     public function removeEligibility(Eligibility $eligibility): self
  471.     {
  472.         if ($this->eligibilities->removeElement($eligibility)) {
  473.             $eligibility->removeOperation($this);
  474.         }
  475.         return $this;
  476.     }
  477.     public function addParticipation(Participation $participation): self
  478.     {
  479.         if (!$this->participations->contains($participation)) {
  480.             $this->participations[] = $participation;
  481.             $participation->addOperation($this);
  482.         }
  483.         return $this;
  484.     }
  485.     public function removeParticipation(Participation $participation): self
  486.     {
  487.         if ($this->participations->removeElement($participation)) {
  488.             $participation->removeOperation($this);
  489.         }
  490.         return $this;
  491.     }
  492.     public function getCompany(): ?Company
  493.     {
  494.         return $this->company;
  495.     }
  496.     public function setCompany(?Company $company): self
  497.     {
  498.         $this->company $company;
  499.         return $this;
  500.     }
  501.     public function getAmount(): ?float
  502.     {
  503.         return $this->amount;
  504.     }
  505.     public function setAmount(float $amount): self
  506.     {
  507.         $this->amount $amount;
  508.         return $this;
  509.     }
  510.     /**
  511.      * @return Collection<int, Ingredient>
  512.      */
  513.     public function getIngredients(): Collection
  514.     {
  515.         return $this->ingredients;
  516.     }
  517.     public function addIngredient(Ingredient $ingredient): self
  518.     {
  519.         if (!$this->ingredients->contains($ingredient)) {
  520.             $this->ingredients[] = $ingredient;
  521.             $ingredient->addOffer($this);
  522.         }
  523.         return $this;
  524.     }
  525.     public function removeIngredient(Ingredient $ingredient): self
  526.     {
  527.         if ($this->ingredients->removeElement($ingredient)) {
  528.             $ingredient->removeOffer($this);
  529.         }
  530.         return $this;
  531.     }
  532.     public function clearIngredients(): self
  533.     {
  534.         foreach ($this->ingredients as $ingredient) {
  535.             $ingredient->removeOffer($this);
  536.         }
  537.         $this->ingredients = new ArrayCollection();
  538.         return $this;
  539.     }
  540.     public function getNbParticipations(): int
  541.     {
  542.         return $this->nbParticipations;
  543.     }
  544.     public function setNbParticipations(int $nbParticipations)
  545.     {
  546.         $this->nbParticipations $nbParticipations;
  547.     }
  548. }