<?php
namespace App\Entity\Participation;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\Api\Participation\Step\GetParticipationStepsByUser;
use App\Entity\Details\Participation\DetailsParticipationStep;
use App\Entity\Ean\Participation\EanParticipation;
use App\Entity\Ean\Participation\EanStep;
use App\Repository\Participation\StepRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\ExclusionPolicy;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
/**
* @ORM\Entity(repositoryClass=StepRepository::class)
* @ExclusionPolicy("all")
* @ApiResource(
* messenger=true,
* shortName="participation-step",
* attributes={
* "force_eager"=false,
* "normalization_context"={
* "groups"={
* "ParticipationStep:output",
* "ParticipationStep:io",
* },
* "enable_max_depth"=true
* },
* "denormalization_context"={
* "groups"={
* "ParticipationStep:input",
* "ParticipationStep:io",
* },
* "enable_max_depth"=true
* }
* },
* collectionOperations={
* "get",
* "post",
* "get-by-user"={
* "method"="GET",
* "path"="/participation-steps/user",
* "access_control"="is_granted('ROLE_USER')",
* "validation_groups"={"get_participation-steps_by_user"},
* "controller"=GetParticipationStepsByUser::class,
* "defaults"={"_api_receive"=false},
* },
* },
* itemOperations={
* "get",
* "put"={
* "validation_groups"={"Default", "Edit"}
* },
* },
* )
*/
class Step extends Participation
{
/**
* @ORM\Column(name="code", type="string", nullable=true)
* @SerializedName("code")
* @Groups({
* "ParticipationStep:io",
* })
*/
private ?string $code = null;
/**
* @var Collection<EanParticipation>|EanParticipation[]
* @ORM\OneToMany(targetEntity=EanStep::class, mappedBy="participation")
* @SerializedName("ean_steps")
* @Groups({
* "ParticipationStep:io",
* "get_steps_by_user_department",
* })
*/
private $eanParticipations;
/**
* @ORM\Column(name="amount", type="float")
* @SerializedName("amount")
* @Groups({
* "ParticipationStep:io",
* "get_steps_by_user_department",
* })
*/
private ?float $amount = null;
/**
* @ORM\OneToOne(targetEntity=DetailsParticipationStep::class, mappedBy="participation", cascade={"persist", "remove"})
* @SerializedName("details_participation_step")
* @Groups({
* "ParticipationStep:io",
* "get_steps_by_user_department",
* })
*/
private ?DetailsParticipationStep $details = null;
/**
* @ORM\Column(type="boolean", options={"default": 0})
*/
private bool $mailSuccess = false;
public function __construct()
{
parent::__construct();
$this->eanParticipations = new ArrayCollection();
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(?string $code)
{
$this->code = $code;
return $this;
}
/**
* @param array $filter
* @return int
*/
public function getEanParticipationsQuantityByStatus(array $filter = []): int
{
$eanParts = $this->eanParticipations ?? [];
if(!$eanParts instanceof Collection) {
$eanParts = new ArrayCollection($eanParts);
}
if( !empty($filter) ) {
$eanParts = $eanParts->filter(function(EanParticipation $eanParticipation) use ($filter) {
return in_array($eanParticipation->getBarcodeStatus(), $filter);
});
}
$quantity = 0;
foreach ($eanParts as $eanParticipation) {
$quantity += ((int)$eanParticipation->getQuantity() * (int)$eanParticipation->getEan()->getQuantity());
}
return $quantity;
}
/**
* @return Collection|EanStep[]
*/
public function getEanParticipations(): ?Collection
{
return $this->eanParticipations;
}
public function addEanParticipations(EanStep $eanStep): self
{
if (!$this->eanParticipations->contains($eanStep)) {
$this->eanParticipations[] = $eanStep;
$eanStep->setParticipation($this);
}
return $this;
}
public function removeEanParticipations(EanStep $eanStep): self
{
if ($this->eanParticipations->removeElement($eanStep)) {
// set the owning side to null (unless already changed)
if ($eanStep->getParticipation() === $this) {
$eanStep->setParticipation(null);
}
}
return $this;
}
public function getDetails(): ?DetailsParticipationStep
{
return $this->details;
}
public function setDetails(?DetailsParticipationStep $details): self
{
$this->details = $details;
return $this;
}
/**
* @return float|null
*/
public function getAmount() : ?float
{
return $this->amount;
}
/**
* @param float|null $amount
*
* @return Step
*/
public function setAmount(?float $amount) : Step
{
$this->amount = $amount;
return $this;
}
/**
* @Serializer\VirtualProperty
* @Serializer\Groups({"shannon"})
* @Serializer\SerializedName("participation_id")
* @return string
*/
public function getParticipationId(): string
{
return $this->id;
}
/**
* Get opt-in RGPD.
*
* @Serializer\VirtualProperty
* @Serializer\Groups({"shannon"})
* @Serializer\SerializedName("optin_rgpd")
* @return int
*/
public function intOptInRGPD(): int
{
return true;
}
/**
* @Serializer\VirtualProperty
* @Serializer\Groups({"shannon"})
* @Serializer\SerializedName("action")
* @return string
*/
public function getAction(): string
{
return 'create';
}
/**
* @Serializer\VirtualProperty
* @Serializer\Groups({"shannon"})
* @Serializer\SerializedName("channel")
* @return string
*/
public function getChannel(): string
{
return 'FW';
}
/**
* @Serializer\VirtualProperty
* @Serializer\Groups({"shannon"})
* @Serializer\SerializedName("date_participation")
* @return string
*/
public function getDateParticipation(): string
{
return $this->getCreatedAt()->format('Y-m-d H:i:s');
}
public function getApiCode()
{
return $this->getValidationExport() instanceof ValidationExport ? $this->getValidationExport()->getAPIParticpationId() : null;
}
public function getMailSuccess(): bool
{
return (bool) $this->mailSuccess;
}
public function setMailSuccess(bool $mailSuccess): self
{
$this->mailSuccess = $mailSuccess;
return $this;
}
}