Files
Ferme/src/Entity/Shipment.php
Matteo b1c3952d09
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
[#271]Créer une nouvelle expédition (étape 1) (!12)
| Numéro du ticket | Titre du ticket |
|------------------|-----------------|
|          #271        |         Créer une nouvelle expédition (étape 1)        |

## 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é

Co-authored-by: kevin <kevin@yuno.malio.fr>
Reviewed-on: #12
Reviewed-by: Autin <tristan@yuno.malio.fr>
Co-authored-by: Matteo <matteo@yuno.malio.fr>
Co-committed-by: Matteo <matteo@yuno.malio.fr>
2026-02-12 07:31:40 +00:00

367 lines
9.5 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\ShipmentReceiptProvider;
use App\State\ShipmentWeighingProvider;
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: 'shipment')]
#[ApiResource(
operations: [
new Get(
requirements: ['id' => '\d+'],
normalizationContext: ['groups' => ['shipment:read']],
),
new GetCollection(
normalizationContext: ['groups' => ['shipment:read']],
),
new Post(
normalizationContext: ['groups' => ['shipment:read']],
denormalizationContext: ['groups' => ['shipment:write']],
),
new Patch(
requirements: ['id' => '\d+'],
normalizationContext: ['groups' => ['shipment:read']],
denormalizationContext: ['groups' => ['shipment:write']],
),
new Get(
uriTemplate: '/shipments/weigh',
openapi: new OpenApiOperation(
summary: 'Fetch the current weight reading',
description: 'Queries the pont-bascule and returns the weight data.',
),
normalizationContext: ['groups' => ['shipment:weigh:read']],
output: PontBasculeReading::class,
provider: ShipmentWeighingProvider::class,
),
new Get(
uriTemplate: '/shipments/{id}/receipt',
requirements: ['id' => '\d+'],
openapi: new OpenApiOperation(
summary: 'Render a shipment receipt',
description: 'Returns a PDF receipt for the shipment.',
),
output: false,
provider: ShipmentReceiptProvider::class,
),
],
security: "is_granted('ROLE_USER')",
)]
class Shipment
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['shipment:read'])]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups(['shipment:read', 'shipment:write'])]
private ?string $licencePlate = null;
#[ORM\Column(length: 20, unique: true, nullable: true)]
#[Groups(['shipment:read'])]
private ?string $identificationNumber = null;
#[ORM\Column(options: ['default' => 0])]
#[Groups(['shipment:read', 'shipment:write'])]
private int $currentStep = 0;
#[ORM\Column]
#[Groups(['shipment:read', 'shipment:write'])]
private bool $isValid = false;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['shipment:read', 'shipment:write'])]
#[ApiProperty(readableLink: true)]
private ?User $user = null;
#[ORM\Column(name: 'shipment_date', type: 'datetime_immutable')]
#[Groups(['shipment:read', 'shipment:write'])]
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
private ?DateTimeImmutable $shipmentDate = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['shipment:read', 'shipment:write'])]
#[ApiProperty(readableLink: true)]
private ?Carrier $carrier = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['shipment:read', 'shipment:write'])]
#[ApiProperty(readableLink: true)]
private ?Truck $truck = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['shipment:read', 'shipment:write'])]
#[ApiProperty(readableLink: true)]
private ?Customer $customer = null;
/**
* @var Collection<int, BovinShipment>
*/
#[ORM\OneToMany(
targetEntity: BovinShipment::class,
mappedBy: 'shipment',
cascade: ['persist', 'remove'],
orphanRemoval: true
)]
#[Groups(['shipment:read', 'shipment:write'])]
private Collection $bovinShipments;
/**
* @var Collection<int, Weight>
*/
#[ORM\OneToMany(targetEntity: Weight::class, mappedBy: 'shipment', cascade: ['persist', 'remove'], orphanRemoval: true)]
#[Groups(['shipment:read'])]
private Collection $weights;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['shipment:read', 'shipment:write'])]
#[ApiProperty(readableLink: true)]
private ?Driver $driver = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['shipment:read', 'shipment:write'])]
#[ApiProperty(readableLink: true)]
private ?Address $address = null;
public function __construct()
{
$this->bovinShipments = new ArrayCollection();
$this->weights = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLicencePlate(): ?string
{
return $this->licencePlate;
}
public function setLicencePlate(?string $licencePlate): void
{
$this->licencePlate = $licencePlate;
}
public function getIdentificationNumber(): ?string
{
return $this->identificationNumber;
}
public function setIdentificationNumber(?string $identificationNumber): void
{
$this->identificationNumber = $identificationNumber;
}
public function getCurrentStep(): int
{
return $this->currentStep;
}
public function setCurrentStep(int $currentStep): void
{
$this->currentStep = $currentStep;
}
public function getIsValid(): ?bool
{
return $this->isValid;
}
#[Groups(['shipment:read'])]
public function isValid(): bool
{
return $this->isValid;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): void
{
$this->user = $user;
}
public function setIsValid(?bool $isValid): void
{
$this->isValid = $isValid;
}
public function getShipmentDate(): ?DateTimeImmutable
{
return $this->shipmentDate;
}
public function setShipmentDate(?DateTimeImmutable $shipmentDate): void
{
$this->shipmentDate = $shipmentDate;
}
public function getCarrier(): ?Carrier
{
return $this->carrier;
}
public function setCarrier(?Carrier $carrier): void
{
$this->carrier = $carrier;
}
public function getTruck(): ?Truck
{
return $this->truck;
}
public function setTruck(?Truck $truck): void
{
$this->truck = $truck;
}
public function getCustomer(): ?Customer
{
return $this->customer;
}
public function setCustomer(?Customer $customer): void
{
$this->customer = $customer;
}
public function getBovinShipments(): Collection
{
return $this->bovinShipments;
}
public function setBovinShipments(Collection $bovinShipments): void
{
$this->bovinShipments = $bovinShipments;
}
public function addBovinShipment(BovinShipment $bovinShipment): self
{
if (!$this->bovinShipments->contains($bovinShipment)) {
$this->bovinShipments->add($bovinShipment);
$bovinShipment->setShipment($this);
}
return $this;
}
public function removeBovinShipment(BovinShipment $bovinShipment): self
{
if ($this->bovinShipments->removeElement($bovinShipment)) {
if ($bovinShipment->getShipment() === $this) {
$bovinShipment->setShipment(null);
}
}
return $this;
}
/**
* @return Collection<int, Weight>
*/
public function getWeights(): Collection
{
return $this->weights;
}
public function addWeight(Weight $weight): void
{
if (!$this->weights->contains($weight)) {
$this->weights->add($weight);
$weight->setShipment($this);
}
}
public function removeWeight(Weight $weight): void
{
if ($this->weights->removeElement($weight)) {
if ($weight->getShipment() === $this) {
$weight->setShipment(null);
}
}
}
#[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 shipment SET identification_number = :number WHERE id = :id',
[
'number' => $number,
'id' => $this->id,
]
)
;
}
public function getDriver(): ?Driver
{
return $this->driver;
}
public function setDriver(?Driver $driver): static
{
$this->driver = $driver;
return $this;
}
public function getAddress(): ?Address
{
return $this->address;
}
public function setAddress(?Address $address): static
{
$this->address = $address;
return $this;
}
}