186 lines
4.9 KiB
PHP
186 lines
4.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Entity;
|
|
|
|
use ApiPlatform\Metadata\ApiResource;
|
|
use ApiPlatform\Metadata\Get;
|
|
use ApiPlatform\Metadata\GetCollection;
|
|
use ApiPlatform\Metadata\Patch;
|
|
use ApiPlatform\Metadata\Post;
|
|
use ApiPlatform\OpenApi\Model\Operation as OpenApiOperation;
|
|
use App\Dto\PontBasculeReading;
|
|
use App\State\ReceptionWeighingProvider;
|
|
use DateTimeImmutable;
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
use Doctrine\Common\Collections\Collection;
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use Symfony\Component\Serializer\Attribute\Context;
|
|
use Symfony\Component\Serializer\Attribute\Groups;
|
|
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
|
|
|
#[ORM\Entity]
|
|
#[ORM\HasLifecycleCallbacks]
|
|
#[ORM\Table(name: 'reception')]
|
|
#[ApiResource(
|
|
operations: [
|
|
new Get(
|
|
requirements: ['id' => '\d+'],
|
|
normalizationContext: ['groups' => ['reception:read']],
|
|
),
|
|
new GetCollection(
|
|
normalizationContext: ['groups' => ['reception:read']],
|
|
),
|
|
new Post(
|
|
normalizationContext: ['groups' => ['reception:read']],
|
|
denormalizationContext: ['groups' => ['reception:write']],
|
|
),
|
|
new Patch(
|
|
requirements: ['id' => '\d+'],
|
|
normalizationContext: ['groups' => ['reception:read']],
|
|
denormalizationContext: ['groups' => ['reception:write']],
|
|
),
|
|
new Get(
|
|
uriTemplate: '/receptions/weigh',
|
|
openapi: new OpenApiOperation(
|
|
summary: 'Fetch the current weight reading',
|
|
description: 'Queries the pont-bascule and returns the weight data.',
|
|
),
|
|
normalizationContext: ['groups' => ['reception:weigh:read']],
|
|
output: PontBasculeReading::class,
|
|
provider: ReceptionWeighingProvider::class,
|
|
),
|
|
],
|
|
)]
|
|
class Reception
|
|
{
|
|
#[ORM\Id]
|
|
#[ORM\GeneratedValue]
|
|
#[ORM\Column]
|
|
#[Groups(['reception:read'])]
|
|
private ?int $id = null;
|
|
|
|
#[ORM\Column(length: 20, nullable: true)]
|
|
#[Groups(['reception:read', 'reception:write'])]
|
|
private ?string $licensePlate = null;
|
|
|
|
#[ORM\Column(options: ['default' => 0])]
|
|
#[Groups(['reception:read', 'reception:write'])]
|
|
private int $currentStep = 0;
|
|
|
|
#[ORM\Column(options: ['default' => false])]
|
|
#[Groups(['reception:read', 'reception:write'])]
|
|
private bool $isValid = false;
|
|
|
|
#[ORM\Column(name: 'date_reception', type: 'datetime_immutable')]
|
|
#[Groups(['reception:read', 'reception:write'])]
|
|
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
|
|
private ?DateTimeImmutable $receptionDate = null;
|
|
|
|
#[ORM\OneToMany(targetEntity: Weight::class, mappedBy: 'reception', cascade: ['persist', 'remove'], orphanRemoval: true)]
|
|
#[Groups(['reception:read'])]
|
|
private Collection $weights;
|
|
|
|
public function __construct(
|
|
?DateTimeImmutable $receptionDate = null,
|
|
) {
|
|
$this->receptionDate = $receptionDate;
|
|
$this->weights = new ArrayCollection();
|
|
}
|
|
|
|
public function getId(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
#[Groups(['reception:read'])]
|
|
public function getLicensePlate(): ?string
|
|
{
|
|
return $this->licensePlate;
|
|
}
|
|
|
|
public function setLicensePlate(?string $licensePlate): self
|
|
{
|
|
$this->licensePlate = $licensePlate;
|
|
|
|
return $this;
|
|
}
|
|
|
|
#[Groups(['reception:read'])]
|
|
public function getCurrentStep(): int
|
|
{
|
|
return $this->currentStep;
|
|
}
|
|
|
|
public function setCurrentStep(int $currentStep): self
|
|
{
|
|
$this->currentStep = $currentStep;
|
|
|
|
return $this;
|
|
}
|
|
|
|
#[Groups(['reception:read'])]
|
|
public function isValid(): bool
|
|
{
|
|
return $this->isValid;
|
|
}
|
|
|
|
public function setIsValid(bool $isValid): self
|
|
{
|
|
$this->isValid = $isValid;
|
|
|
|
return $this;
|
|
}
|
|
|
|
#[Groups(['reception:read'])]
|
|
public function getReceptionDate(): ?DateTimeImmutable
|
|
{
|
|
return $this->receptionDate;
|
|
}
|
|
|
|
public function setReceptionDate(?DateTimeImmutable $receptionDate): self
|
|
{
|
|
$this->receptionDate = $receptionDate;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, Weight>
|
|
*/
|
|
public function getWeights(): Collection
|
|
{
|
|
return $this->weights;
|
|
}
|
|
|
|
public function addWeight(Weight $weight): self
|
|
{
|
|
if (!$this->weights->contains($weight)) {
|
|
$this->weights->add($weight);
|
|
$weight->setReception($this);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removeWeight(Weight $weight): self
|
|
{
|
|
if ($this->weights->removeElement($weight)) {
|
|
if ($weight->getReception() === $this) {
|
|
$weight->setReception(null);
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
#[ORM\PrePersist]
|
|
public function initializeReceptionDate(): void
|
|
{
|
|
if (null === $this->receptionDate) {
|
|
$this->receptionDate = new DateTimeImmutable();
|
|
}
|
|
}
|
|
}
|