src/Entity/Participation/Step.php line 62

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Participation;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Controller\Api\Participation\Step\GetParticipationStepsByUser;
  5. use App\Entity\Details\Participation\DetailsParticipationStep;
  6. use App\Entity\Ean\Participation\EanParticipation;
  7. use App\Entity\Ean\Participation\EanStep;
  8. use App\Repository\Participation\StepRepository;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use JMS\Serializer\Annotation as Serializer;
  13. use JMS\Serializer\Annotation\ExclusionPolicy;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. use Symfony\Component\Serializer\Annotation\SerializedName;
  16. /**
  17.  * @ORM\Entity(repositoryClass=StepRepository::class)
  18.  * @ExclusionPolicy("all")
  19.  * @ApiResource(
  20.  *     messenger=true,
  21.  *     shortName="participation-step",
  22.  *     attributes={
  23.  *          "force_eager"=false,
  24.  *          "normalization_context"={
  25.  *              "groups"={
  26.  *                  "ParticipationStep:output",
  27.  *                  "ParticipationStep:io",
  28.  *               },
  29.  *              "enable_max_depth"=true
  30.  *          },
  31.  *          "denormalization_context"={
  32.  *              "groups"={
  33.  *                  "ParticipationStep:input",
  34.  *                  "ParticipationStep:io",
  35.  *              },
  36.  *              "enable_max_depth"=true
  37.  *          }
  38.  *      },
  39.  *     collectionOperations={
  40.  *          "get",
  41.  *          "post",
  42.  *          "get-by-user"={
  43.  *              "method"="GET",
  44.  *              "path"="/participation-steps/user",
  45.  *              "access_control"="is_granted('ROLE_USER')",
  46.  *              "validation_groups"={"get_participation-steps_by_user"},
  47.  *              "controller"=GetParticipationStepsByUser::class,
  48.  *              "defaults"={"_api_receive"=false},
  49.  *          },
  50.  *     },
  51.  *     itemOperations={
  52.  *          "get",
  53.  *          "put"={
  54.  *              "validation_groups"={"Default", "Edit"}
  55.  *          },
  56.  *     },
  57.  * )
  58.  */
  59. class Step extends Participation
  60. {
  61.     /**
  62.      * @ORM\Column(name="code", type="string", nullable=true)
  63.      * @SerializedName("code")
  64.      * @Groups({
  65.      *     "ParticipationStep:io",
  66.      * })
  67.      */
  68.     private ?string $code null;
  69.     /**
  70.      * @var Collection<EanParticipation>|EanParticipation[]
  71.      * @ORM\OneToMany(targetEntity=EanStep::class, mappedBy="participation")
  72.      * @SerializedName("ean_steps")
  73.      * @Groups({
  74.      *     "ParticipationStep:io",
  75.      *     "get_steps_by_user_department",
  76.      * })
  77.      */
  78.     private $eanParticipations;
  79.     /**
  80.      * @ORM\Column(name="amount", type="float")
  81.      * @SerializedName("amount")
  82.      * @Groups({
  83.      *     "ParticipationStep:io",
  84.      *     "get_steps_by_user_department",
  85.      * })
  86.      */
  87.     private ?float $amount null;
  88.     /**
  89.      * @ORM\OneToOne(targetEntity=DetailsParticipationStep::class, mappedBy="participation", cascade={"persist", "remove"})
  90.      * @SerializedName("details_participation_step")
  91.      * @Groups({
  92.      *     "ParticipationStep:io",
  93.      *     "get_steps_by_user_department",
  94.      * })
  95.      */
  96.     private ?DetailsParticipationStep $details null;
  97.     /**
  98.      * @ORM\Column(type="boolean", options={"default": 0})
  99.      */
  100.     private bool $mailSuccess false;
  101.     public function __construct()
  102.     {
  103.         parent::__construct();
  104.         $this->eanParticipations = new ArrayCollection();
  105.     }
  106.     public function getCode(): ?string
  107.     {
  108.         return $this->code;
  109.     }
  110.     public function setCode(?string $code)
  111.     {
  112.         $this->code $code;
  113.         return $this;
  114.     }
  115.     /**
  116.      * @param array $filter
  117.      * @return int
  118.      */
  119.     public function getEanParticipationsQuantityByStatus(array $filter = []): int
  120.     {
  121.         $eanParts $this->eanParticipations ?? [];
  122.         if(!$eanParts instanceof Collection) {
  123.             $eanParts = new ArrayCollection($eanParts);
  124.         }
  125.         if( !empty($filter) ) {
  126.             $eanParts =  $eanParts->filter(function(EanParticipation $eanParticipation) use ($filter) {
  127.                 return in_array($eanParticipation->getBarcodeStatus(), $filter);
  128.             });
  129.         }
  130.         $quantity 0;
  131.         foreach ($eanParts as $eanParticipation) {
  132.             $quantity += ((int)$eanParticipation->getQuantity() * (int)$eanParticipation->getEan()->getQuantity());
  133.         }
  134.         return $quantity;
  135.     }
  136.     /**
  137.      * @return Collection|EanStep[]
  138.      */
  139.     public function getEanParticipations(): ?Collection
  140.     {
  141.         return $this->eanParticipations;
  142.     }
  143.     public function addEanParticipations(EanStep $eanStep): self
  144.     {
  145.         if (!$this->eanParticipations->contains($eanStep)) {
  146.             $this->eanParticipations[] = $eanStep;
  147.             $eanStep->setParticipation($this);
  148.         }
  149.         return $this;
  150.     }
  151.     public function removeEanParticipations(EanStep $eanStep): self
  152.     {
  153.         if ($this->eanParticipations->removeElement($eanStep)) {
  154.             // set the owning side to null (unless already changed)
  155.             if ($eanStep->getParticipation() === $this) {
  156.                 $eanStep->setParticipation(null);
  157.             }
  158.         }
  159.         return $this;
  160.     }
  161.     public function getDetails(): ?DetailsParticipationStep
  162.     {
  163.         return $this->details;
  164.     }
  165.     public function setDetails(?DetailsParticipationStep $details): self
  166.     {
  167.         $this->details $details;
  168.         return $this;
  169.     }
  170.     /**
  171.      * @return float|null
  172.      */
  173.     public function getAmount() : ?float
  174.     {
  175.         return $this->amount;
  176.     }
  177.     /**
  178.      * @param float|null $amount
  179.      *
  180.      * @return Step
  181.      */
  182.     public function setAmount(?float $amount) : Step
  183.     {
  184.         $this->amount $amount;
  185.         return $this;
  186.     }
  187.     /**
  188.      * @Serializer\VirtualProperty
  189.      * @Serializer\Groups({"shannon"})
  190.      * @Serializer\SerializedName("participation_id")
  191.      * @return string
  192.      */
  193.     public function getParticipationId(): string
  194.     {
  195.         return $this->id;
  196.     }
  197.     /**
  198.      * Get opt-in RGPD.
  199.      *
  200.      * @Serializer\VirtualProperty
  201.      * @Serializer\Groups({"shannon"})
  202.      * @Serializer\SerializedName("optin_rgpd")
  203.      * @return int
  204.      */
  205.     public function intOptInRGPD(): int
  206.     {
  207.         return true;
  208.     }
  209.     /**
  210.      * @Serializer\VirtualProperty
  211.      * @Serializer\Groups({"shannon"})
  212.      * @Serializer\SerializedName("action")
  213.      * @return string
  214.      */
  215.     public function getAction(): string
  216.     {
  217.         return 'create';
  218.     }
  219.     /**
  220.      * @Serializer\VirtualProperty
  221.      * @Serializer\Groups({"shannon"})
  222.      * @Serializer\SerializedName("channel")
  223.      * @return string
  224.      */
  225.     public function getChannel(): string
  226.     {
  227.         return 'FW';
  228.     }
  229.     /**
  230.      * @Serializer\VirtualProperty
  231.      * @Serializer\Groups({"shannon"})
  232.      * @Serializer\SerializedName("date_participation")
  233.      * @return string
  234.      */
  235.     public function getDateParticipation(): string
  236.     {
  237.         return $this->getCreatedAt()->format('Y-m-d H:i:s');
  238.     }
  239.     public function getApiCode()
  240.     {
  241.         return $this->getValidationExport() instanceof ValidationExport $this->getValidationExport()->getAPIParticpationId() : null;
  242.     }
  243.     public function getMailSuccess(): bool
  244.     {
  245.         return (bool) $this->mailSuccess;
  246.     }
  247.     public function setMailSuccess(bool $mailSuccess): self
  248.     {
  249.         $this->mailSuccess $mailSuccess;
  250.         return $this;
  251.     }
  252. }