385 lines
10 KiB
PHP
385 lines
10 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Entity;
|
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
|
|
use ApiPlatform\Metadata\ApiFilter;
|
|
use ApiPlatform\Metadata\ApiProperty;
|
|
use ApiPlatform\Metadata\ApiResource;
|
|
use ApiPlatform\Metadata\Delete;
|
|
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')]
|
|
#[ApiFilter(BooleanFilter::class, properties: ['isValid'])]
|
|
#[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 Delete(
|
|
requirements: ['id' => '\d+'],
|
|
),
|
|
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 $licensePlate = 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(
|
|
normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d H:i'],
|
|
denormalizationContext: [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;
|
|
|
|
#[ORM\ManyToOne(inversedBy: 'shipments')]
|
|
#[ORM\JoinColumn(nullable: true)]
|
|
#[Groups(['shipment:read', 'shipment:write'])]
|
|
#[ApiProperty(readableLink: true)]
|
|
private ?ShipmentType $shipmentType = null;
|
|
|
|
#[ORM\Column]
|
|
#[Groups(['shipment:read', 'shipment:write'])]
|
|
private int $nbBovinSend = 0;
|
|
|
|
/**
|
|
* @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->weights = new ArrayCollection();
|
|
}
|
|
|
|
public function getId(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getLicensePlate(): ?string
|
|
{
|
|
return $this->licensePlate;
|
|
}
|
|
|
|
public function setLicensePlate(?string $licensePlate): void
|
|
{
|
|
$this->licensePlate = $licensePlate;
|
|
}
|
|
|
|
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 getShipmentType(): ?ShipmentType
|
|
{
|
|
return $this->shipmentType;
|
|
}
|
|
|
|
public function setShipmentType(?ShipmentType $shipmentType): void
|
|
{
|
|
$this->shipmentType = $shipmentType;
|
|
}
|
|
|
|
public function getNbBovinSend(): int
|
|
{
|
|
return $this->nbBovinSend;
|
|
}
|
|
|
|
public function setNbBovinSend(int $nbBovinSend): void
|
|
{
|
|
$this->nbBovinSend = $nbBovinSend;
|
|
}
|
|
|
|
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
|
|
{
|
|
if (null !== $shipmentDate && null !== $this->shipmentDate) {
|
|
$shipmentDate = $shipmentDate->setTime(
|
|
(int) $this->shipmentDate->format('H'),
|
|
(int) $this->shipmentDate->format('i'),
|
|
(int) $this->shipmentDate->format('s'),
|
|
);
|
|
}
|
|
$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;
|
|
}
|
|
|
|
/**
|
|
* @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\PrePersist]
|
|
public function initializeShipmentDate(): void
|
|
{
|
|
$now = new DateTimeImmutable();
|
|
if (null === $this->shipmentDate) {
|
|
$this->shipmentDate = $now;
|
|
} else {
|
|
$this->shipmentDate = $this->shipmentDate->setTime(
|
|
(int) $now->format('H'),
|
|
(int) $now->format('i'),
|
|
(int) $now->format('s'),
|
|
);
|
|
}
|
|
}
|
|
|
|
#[ORM\PostPersist]
|
|
public function initializeIdentificationNumber(PostPersistEventArgs $args): void
|
|
{
|
|
if (null !== $this->identificationNumber) {
|
|
return;
|
|
}
|
|
|
|
if (null === $this->id) {
|
|
return;
|
|
}
|
|
|
|
$number = sprintf('P-BL-%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;
|
|
}
|
|
}
|