<?php
namespace App\Entity\Participation;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\Api\Participation\Odr\GetParticipationOdrsByUser;
use App\Controller\Api\Participation\Odr\GetParticipationOdrsByUserByOperation;
use App\Controller\Api\Participation\Odr\GetParticipationOdrsById;
use App\Controller\Api\Participation\Odr\GetReParticipationOdrsById;
use App\Controller\Api\Participation\Odr\CreateParticipationOdrByUser;
use App\Controller\Api\Participation\Odr\ConfirmationParticipationOdrByUser;
use App\Controller\Api\Participation\Odr\GetNbParticipationByOperationOdrId;
use App\Entity\Details\Participation\DetailsParticipationOdr;
use App\Entity\Ean\Participation\EanParticipation;
use App\Entity\Ean\Participation\EanOdr;
use App\Repository\Participation\OdrRepository;
use App\Entity\OperationCode\OdrCode;
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;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=OdrRepository::class)
* @ExclusionPolicy("all")
* @ApiResource(
* messenger=true,
* shortName="participation-odr",
* attributes={
* "force_eager"=false,
* "normalization_context"={
* "groups"={
* "ParticipationOdr:output",
* "ParticipationOdr:io",
* "get_participation_odrs_by_user",
* "get_participation_odrs_by_id",
* },
* "enable_max_depth"=true
* },
* "denormalization_context"={
* "groups"={
* "ParticipationOdr:input",
* "ParticipationOdr:io",
* },
* "enable_max_depth"=true
* }
* },
* collectionOperations={
* "get",
* "post",
* "get-by-user"={
* "method"="GET",
* "path"="/participation-odrs/user",
* "access_control"="is_granted('ROLE_USER')",
* "validation_groups"={"get_participation_odrs_by_user"},
* "controller"=GetParticipationOdrsByUser::class,
* "defaults"={"_api_receive"=false},
* },
* "get-by-user-by-operation"={
* "method"="GET",
* "path"="/participation-odrs/user/operation/{operationId}",
* "access_control"="is_granted('ROLE_USER')",
* "validation_groups"={"get_participation_odrs_by_user"},
* "controller"=GetParticipationOdrsByUserByOperation::class,
* "defaults"={"_api_receive"=false},
* },
* "create-participation-odr-post"={
* "method"="POST",
* "path"="/participation-odrs/create-participation",
* "access_control"="is_granted('ROLE_USER')",
* "controller"=CreateParticipationOdrByUser::class,
* "defaults"={"_api_receive"=false},
* },
* "confirmation-participation-odr-post"={
* "method"="POST",
* "path"="/participation-odrs/smp/{participationId}/{userId}",
* "access_control"="is_granted('ROLE_USER')",
* "controller"=ConfirmationParticipationOdrByUser::class,
* "defaults"={"_api_receive"=false},
* },
* },
* itemOperations={
* "get"={
* "method"="GET",
* "path"="/participation-odrs/{id}",
* "access_control"="is_granted('ROLE_USER')",
* "validation_groups"={"get_participation_odrs_by_id"},
* "controller"=GetParticipationOdrsById::class,
* "defaults"={"_api_receive"=false},
* },
* "get-reparticipation"={
* "method"="GET",
* "path"="/participation-odrs/reparticipation/{id}",
* "access_control"="is_granted('ROLE_USER')",
* "validation_groups"={"get_reparticipation_odrs_by_id"},
* "controller"=GetReParticipationOdrsById::class,
* "defaults"={"_api_receive"=false},
* },
* "put"={
* "validation_groups"={"Default", "Edit"}
* },
* "public-get-nbr-participation-odr"={
* "method"="GET",
* "access_control"="is_granted('ROLE_PUBLIC')",
* "path"="/public/get-nb-participation-odr/{id}",
* "controller"=GetNbParticipationByOperationOdrId::class,
* "defaults"={"_api_receive"=false},
* }
* },
* )
*/
class Odr extends Participation
{
const CHANNEL_CASH_BACK = "cash-back";
const PAYMENT_TYPE_IBAN = "transfer";
const PAYMENT_TYPE_PAYPAL = "paypal";
/**
* @ORM\OneToMany(targetEntity=OdrCode::class, mappedBy="participation")
* @SerializedName("operation_codes")
* @Groups({
* "OdrCode:output",
* })
*/
protected Collection $operationCodes;
/**
* @ORM\Column(name="code", type="string", nullable=true)
* @SerializedName("code")
* @Groups({
* "ParticipationOdr:io",
* "get_odrs_by_user_department",
* "get_participation_odrs_by_user",
* "get_participation_odrs_by_id",
* "get_reparticipation_odrs_by_id",
* })
*/
private ?string $code = null;
/**
* @var Collection<EanParticipation>|EanParticipation[]
* @ORM\OneToMany(targetEntity=EanOdr::class, mappedBy="participation")
* @SerializedName("ean_odrs")
* @Groups({
* "ParticipationOdr:io",
* "get_odrs_by_user_department",
* "get_participation_odrs_by_user",
* "get_participation_odrs_by_id",
* "get_reparticipation_odrs_by_id",
* })
*/
private $eanParticipations;
/**
* @ORM\Column(name="amount", type="float")
* @SerializedName("amount")
* @Groups({
* "ParticipationOdr:io",
* "get_odrs_by_user_department",
* "get_participation_odrs_by_user",
* "get_participation_odrs_by_id",
* "get_reparticipation_odrs_by_id",
* })
*/
private ?float $amount = null;
/**
* @ORM\Column(type="string", nullable=true)
* @SerializedName("date_of_purchase")
* @Groups({
* "ParticipationOdr:io",
* "get_odrs_by_user_department",
* "get_participation_odrs_by_user",
* "get_participation_odrs_by_id",
* "get_reparticipation_odrs_by_id",
* })
*/
private ?string $dateOfPurchase = null;
/**
* @ORM\OneToOne(targetEntity=DetailsParticipationOdr::class, mappedBy="participation", cascade={"persist", "remove"})
* @SerializedName("details_participation_odr")
* @Groups({
* "ParticipationOdr:io",
* "get_odrs_by_user_department",
* "get_participation_odrs_by_user",
* "get_participation_odrs_by_id",
* "get_reparticipation_odrs_by_id",
* })
*/
private ?DetailsParticipationOdr $details = null;
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
* @SerializedName("iban")
* @Groups({
* "ParticipationOdr:io",
* "get_odrs_by_user_department",
* "get_participation_odrs_by_user",
* "get_participation_odrs_by_id",
* "get_reparticipation_odrs_by_id",
* })
*/
private ?string $iban = null;
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
* @SerializedName("bic")
* @Groups({
* "ParticipationOdr:io",
* "get_odrs_by_user_department",
* "get_participation_odrs_by_user",
* "get_participation_odrs_by_id",
* "get_reparticipation_odrs_by_id",
* })
* @Assert\Bic()
*/
private ?string $bic = null;
/**
* @ORM\Column(type="string", length=180, nullable=true)
* @SerializedName("paypal")
* @Groups({
* "ParticipationOdr:io",
* "get_participation_odrs_by_user",
* "get_participation_odrs_by_id",
* "get_reparticipation_odrs_by_id",
* })
*/
private ?string $paypal = null;
/**
* @return string|null
*/
public function getPaypal(): ?string
{
return $this->paypal;
}
/**
* @param string|null $paypal
* @return Odr
*/
public function setPaypal(?string $paypal): Odr
{
$this->paypal = $paypal;
return $this;
}
/**
* @var string|null
* @ORM\Column(type="string", nullable=true)
* @SerializedName("id_billy")
* @Groups({
* "ParticipationOdr:io",
* "get_odrs_by_user_department",
* "get_participation_odrs_by_user",
* "get_participation_odrs_by_id",
* "get_reparticipation_odrs_by_id",
* })
*/
private ?string $idBilly = null;
/**
* @ORM\Column(type="boolean", options={"default": 0})
*/
private bool $mailSuccess = false;
public function __construct()
{
parent::__construct();
$this->eanParticipations = new ArrayCollection();
$this->operationCodes = 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|EanOdr[]
*/
public function getEanParticipations(): ?Collection
{
return $this->eanParticipations;
}
public function addEanParticipations(EanOdr $eanOdr): self
{
if (!$this->eanParticipations->contains($eanOdr)) {
$this->eanParticipations[] = $eanOdr;
$eanOdr->setParticipation($this);
}
return $this;
}
public function removeEanParticipations(EanOdr $eanOdr): self
{
if ($this->eanParticipations->removeElement($eanOdr)) {
// set the owning side to null (unless already changed)
if ($eanOdr->getParticipation() === $this) {
$eanOdr->setParticipation(null);
}
}
return $this;
}
public function getDetails(): ?DetailsParticipationOdr
{
return $this->details;
}
public function setDetails(?DetailsParticipationOdr $details): self
{
$this->details = $details;
return $this;
}
/**
* @return float|null
*/
public function getAmount() : ?float
{
return $this->amount;
}
/**
* @param float|null $amount
*
* @return Odr
*/
public function setAmount(?float $amount) : Odr
{
$this->amount = $amount;
return $this;
}
/**
* @return string
*/
public function getIban(): ?string
{
return $this->iban;
}
/**
* @param string|null $iban
* @return Odr
*/
public function setIban(?string $iban): Odr
{
$this->iban = $iban;
return $this;
}
/**
* @return string
*/
public function getBic(): ?string
{
return $this->bic;
}
/**
* @param string|null $bic
* @return Odr
*/
public function setBic(?string $bic): Odr
{
$this->bic = $bic;
return $this;
}
/**
* @return string
*/
public function getIdBilly(): ?string
{
return $this->idBilly;
}
/**
* @param string|null $idBilly
* @return Odr
*/
public function setIdBilly(?string $idBilly): Odr
{
$this->idBilly = $idBilly;
return $this;
}
public function getApiCode(): ?string
{
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;
}
public function getDateOfPurchase(): ?string
{
return $this->dateOfPurchase;
}
public function setDateOfPurchase(?string $dateOfPurchase): self
{
$this->dateOfPurchase = $dateOfPurchase;
return $this;
}
public function getOperationCodes(): Collection
{
return $this->operationCodes;
}
public function addOperationCode(OdrCode $operationCode): self
{
if (!$this->operationCodes->contains($operationCode)) {
$this->operationCodes[] = $operationCode;
}
return $this;
}
public function removeOperationCode(OdrCode $operationCode): self
{
$this->operationCodes->removeElement($operationCode);
return $this;
}
}