Files
Ferme/src/Entity/Reception.php
T
tristan 1ce6357c1d Finalisation réception marchandise, ajout de composant UI et ajout de fixtures (!7)
| Numéro du ticket | Titre du ticket |
|------------------|-----------------|
|                  |                 |

## Description de la PR

## Modification du .env

## Check list

- [x] Pas de régression
- [ ] TU/TI/TF rédigée
- [x] TU/TI/TF OK
- [x] CHANGELOG modifié

Reviewed-on: https://gitea.malio.fr/MALIO-DEV/Ferme/pulls/7
Co-authored-by: tristan <tristan@yuno.malio.fr>
Co-committed-by: tristan <tristan@yuno.malio.fr>
2026-01-30 14:10:40 +00:00

473 lines
12 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Metadata\ApiProperty;
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\ReceptionReceiptProvider;
use App\State\ReceptionWeighingProvider;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Event\PostPersistEventArgs;
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,
),
new Get(
uriTemplate: '/receptions/{id}/receipt',
requirements: ['id' => '\d+'],
openapi: new OpenApiOperation(
summary: 'Render a reception receipt',
description: 'Returns a PDF receipt for the reception.',
),
output: false,
provider: ReceptionReceiptProvider::class,
),
],
security: "is_granted('ROLE_USER')",
)]
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(length: 20, unique: true, nullable: true)]
#[Groups(['reception:read'])]
private ?string $identificationNumber = 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\Column(length: 255, nullable: true)]
#[Groups(['reception:read', 'reception:write'])]
private ?string $merchandiseDetail = null;
#[ORM\OneToMany(targetEntity: Weight::class, mappedBy: 'reception', cascade: ['persist', 'remove'], orphanRemoval: true)]
#[Groups(['reception:read'])]
private Collection $weights;
#[ORM\ManyToOne(inversedBy: 'receptions')]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['reception:read', 'reception:write'])]
#[ApiProperty(readableLink: true)]
private ?ReceptionType $receptionType = null;
#[ORM\ManyToOne(inversedBy: 'receptions')]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['reception:read', 'reception:write'])]
#[ApiProperty(readableLink: true)]
private ?MerchandiseType $merchandiseType = null;
/**
* @var Collection<int, Building>
*/
#[ORM\ManyToMany(targetEntity: Building::class, inversedBy: 'receptions')]
#[ORM\JoinTable(name: 'reception_building')]
#[Groups(['reception:read', 'reception:write'])]
#[ApiProperty(readableLink: true)]
private Collection $buildings;
/**
* @var Collection<int, ReceptionPelletBuilding>
*/
#[ORM\OneToMany(targetEntity: ReceptionPelletBuilding::class, mappedBy: 'reception', cascade: ['persist', 'remove'], orphanRemoval: true)]
#[Groups(['reception:read'])]
private Collection $pelletBuildings;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['reception:read', 'reception:write'])]
#[ApiProperty(readableLink: true)]
private ?User $user = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['reception:read', 'reception:write'])]
#[ApiProperty(readableLink: true)]
private ?Supplier $supplier = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['reception:read', 'reception:write'])]
#[ApiProperty(readableLink: true)]
private ?Address $address = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['reception:read', 'reception:write'])]
#[ApiProperty(readableLink: true)]
private ?Truck $truck = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['reception:read', 'reception:write'])]
#[ApiProperty(readableLink: true)]
private ?Carrier $carrier = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['reception:read', 'reception:write'])]
#[ApiProperty(readableLink: true)]
private ?Driver $driver = null;
public function __construct(
?DateTimeImmutable $receptionDate = null,
) {
$this->receptionDate = $receptionDate;
$this->weights = new ArrayCollection();
$this->buildings = new ArrayCollection();
$this->pelletBuildings = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
#[Groups(['reception:read'])]
public function getLicensePlate(): ?string
{
return $this->licensePlate;
}
public function getIdentificationNumber(): ?string
{
return $this->identificationNumber;
}
public function setIdentificationNumber(?string $identificationNumber): self
{
$this->identificationNumber = $identificationNumber;
return $this;
}
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;
}
public function getMerchandiseDetail(): ?string
{
return $this->merchandiseDetail;
}
public function setMerchandiseDetail(?string $merchandiseDetail): self
{
$this->merchandiseDetail = $merchandiseDetail;
return $this;
}
/**
* @return Collection<int, Weight>
*/
public function getWeights(): Collection
{
return $this->weights;
}
public function getReceptionType(): ?ReceptionType
{
return $this->receptionType;
}
public function setReceptionType(?ReceptionType $receptionType): self
{
$this->receptionType = $receptionType;
return $this;
}
public function getMerchandiseType(): ?MerchandiseType
{
return $this->merchandiseType;
}
public function setMerchandiseType(?MerchandiseType $merchandiseType): self
{
$this->merchandiseType = $merchandiseType;
return $this;
}
/**
* @return Collection<int, Building>
*/
public function getBuildings(): Collection
{
return $this->buildings;
}
public function addBuilding(Building $building): self
{
if (!$this->buildings->contains($building)) {
$this->buildings->add($building);
}
return $this;
}
public function removeBuilding(Building $building): self
{
$this->buildings->removeElement($building);
return $this;
}
/**
* @return Collection<int, ReceptionPelletBuilding>
*/
public function getPelletBuildings(): Collection
{
return $this->pelletBuildings;
}
public function addPelletBuilding(ReceptionPelletBuilding $pelletBuilding): self
{
if (!$this->pelletBuildings->contains($pelletBuilding)) {
$this->pelletBuildings->add($pelletBuilding);
$pelletBuilding->setReception($this);
}
return $this;
}
public function removePelletBuilding(ReceptionPelletBuilding $pelletBuilding): self
{
if ($this->pelletBuildings->removeElement($pelletBuilding)) {
if ($pelletBuilding->getReception() === $this) {
$pelletBuilding->setReception(null);
}
}
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getSupplier(): ?Supplier
{
return $this->supplier;
}
public function setSupplier(?Supplier $supplier): self
{
$this->supplier = $supplier;
return $this;
}
public function getAddress(): ?Address
{
return $this->address;
}
public function setAddress(?Address $address): self
{
$this->address = $address;
return $this;
}
public function getTruck(): ?Truck
{
return $this->truck;
}
public function setTruck(?Truck $truck): self
{
$this->truck = $truck;
return $this;
}
public function getCarrier(): ?Carrier
{
return $this->carrier;
}
public function setCarrier(?Carrier $carrier): self
{
$this->carrier = $carrier;
return $this;
}
public function getDriver(): ?Driver
{
return $this->driver;
}
public function setDriver(?Driver $driver): self
{
$this->driver = $driver;
return $this;
}
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();
}
}
#[ORM\PostPersist]
public function initializeIdentificationNumber(PostPersistEventArgs $args): void
{
if (null !== $this->identificationNumber) {
return;
}
if (null === $this->id) {
return;
}
$number = sprintf('P-BR-%04d', $this->id);
$this->identificationNumber = $number;
$args->getObjectManager()
->getConnection()
->executeStatement(
'UPDATE reception SET identification_number = :number WHERE id = :id',
[
'number' => $number,
'id' => $this->id,
]
)
;
}
}