<?php
namespace App\Containers\ServiceSection\TravelContainer\Entities;
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\Put;
// use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Doctrine\Orm\Filter\NumericFilter;
use ApiPlatform\Doctrine\Orm\Filter\RangeFilter;
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
// use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\DBAL\Types\Types;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use Symfony\Component\Serializer\Annotation\Context;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use App\Ship\ApiPlatform\Filter\SimpleSearchFilter;
use App\Containers\ServiceSection\TravelContainer\Data\Repositories\TravelRepository;
use App\Containers\CrmSection\ClientContainer\Entities\ClientEntity;
use App\Containers\CrmSection\CarContainer\Entities\CarEntity;
use App\Containers\CrmSection\ContractContainer\Entities\ContractEntity;
use App\Containers\CrmSection\EquipmentContainer\Entities\ApplicationEntity;
use App\Containers\ServiceSection\IndexContainer\Entities\TranslationEntity;
use App\Containers\ServiceSection\TravelContainer\UI\ApiPlatform\Controllers\GetMapByTravelController;
/**
* История поездок
*/
#[ApiResource(
routePrefix: '/service',
normalizationContext: [
'groups' => ['travel:read', 'read'],
'enable_max_depth' => true
],
denormalizationContext: [
'groups' => ['travel:create', 'travel:update']
],
operations: [
new Get(
uriTemplate: '/travels/{id}', requirements: ['id' => '\d+'],
security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_USER_CLIENT') or is_granted('ROLE_READONLY')"
),
new Get(
uriTemplate: '/travels/{id}/map', requirements: ['id' => '\d+'],
controller: GetMapByTravelController::class,
openapiContext: [
'parameters' => [
[
'in' => 'path',
'name' => 'id',
'required' => true,
'schema' => [
'type' => 'string'
],
'description' => 'TravelEntity identifier'
]
],
'responses' => [
'200' => [
'description' => 'Service_Travels resource',
'content' => [
'application/ld+json' => [
'schema' => [
'type' => 'object',
'properties' => [
'@context' => [
'type' => 'string',
'readOnly' => true
],
'@id' => [
'type' => 'string',
'readOnly' => true
],
'@type' => [
'type' => 'string',
'readOnly' => true
],
'id' => [
'type' => 'integer',
'readOnly' => true
],
'contentUrl' => [
'type' => 'string',
'readOnly' => true
]
]
]
],
'application/json' => [
'schema' => [
'type' => 'object',
'properties' => [
'@context' => [
'type' => 'string',
'readOnly' => true
],
'@id' => [
'type' => 'string',
'readOnly' => true
],
'@type' => [
'type' => 'string',
'readOnly' => true
],
'id' => [
'type' => 'integer',
'readOnly' => true
],
'contentUrl' => [
'type' => 'string',
'readOnly' => true
]
]
]
],
'text/html' => [
'schema' => [
'type' => 'object',
'properties' => [
'@context' => [
'type' => 'string',
'readOnly' => true
],
'@id' => [
'type' => 'string',
'readOnly' => true
],
'@type' => [
'type' => 'string',
'readOnly' => true
],
'id' => [
'type' => 'integer',
'readOnly' => true
],
'contentUrl' => [
'type' => 'string',
'readOnly' => true
]
]
]
]
]
],
'404' => ['description' => 'Resource not found']
]
],
security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_USER_CLIENT') or is_granted('ROLE_READONLY')"
),
new Patch(
uriTemplate: '/travels/{id}', requirements: ['id' => '\d+'],
security: "is_granted('ROLE_ADMIN')"
),
// new Put(
// uriTemplate: '/travels/{id}', requirements: ['id' => '\d+'],
// security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_USER_CLIENT')"
// ),
new Delete(
uriTemplate: '/travels/{id}', requirements: ['id' => '\d+'],
security: "is_granted('ROLE_ADMIN')"
),
// new Post(
// uriTemplate: '/travels',
// security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_USER_CLIENT')"
// ),
new GetCollection(
uriTemplate: '/travels',
security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_USER_CLIENT') or is_granted('ROLE_READONLY')"
),
],
shortName: 'Service_Travels'
)]
#[ApiFilter(SearchFilter::class, properties: [
'id' => 'exact',
'externalCode' => 'partial',
'application' => 'exact',
'application.carNumber' => 'partial',
'client' => 'exact',
'contract' => 'exact',
'contract.carNumber' => 'partial',
'car' => 'exact',
'car.carNumber' => 'partial'
])]
#[ApiFilter(NumericFilter::class, properties: ['id', 'rating'])]
#[ApiFilter(RangeFilter::class, properties: [
'id',
'rating',
'mileage',
'correctMileage',
'cityMileage',
'finalMileage',
'avgSpeed',
'maxSpeed'
])]
#[ApiFilter(DateFilter::class, properties: [
'startDate',
'endDate',
'createdAt',
'updatedAt'
])]
#[ApiFilter(OrderFilter::class)]
#[ApiFilter(filterClass: SimpleSearchFilter::class, properties: ['externalCode'])]
#[ORM\Entity(repositoryClass: TravelRepository::class)]
#[ORM\Table(name: '`service_travels`')]
#[ORM\Index(columns: ['ext_start_timestamp', 'ext_end_timestamp', 'application_id'], name: 'service_travels_custom_idx')]
#[Gedmo\TranslationEntity(class: TranslationEntity::class)]
#[UniqueEntity(fields: ['externalCode'])]
class TravelEntity
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
#[Groups(['travel:read'])]
private ?int $id = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true, unique: true)]
#[Assert\Length(min: 0, max: 255, groups: ['travel:update', 'travel:create'])]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
#[ApiProperty(security: "is_granted('ROLE_ADMIN')")]
private ?string $externalCode = null;
#[ORM\Column(type: Types::STRING, length: 20, nullable: false)]
#[Assert\Length(min: 0, max: 20, groups: ['travel:update', 'travel:create'])]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
#[ApiProperty(security: "is_granted('ROLE_ADMIN')")]
private string $extStartTimestamp;
#[ORM\Column(type: Types::STRING, length: 20, nullable: false)]
#[Assert\Length(min: 0, max: 20, groups: ['travel:update', 'travel:create'])]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
#[ApiProperty(security: "is_granted('ROLE_ADMIN')")]
private string $extEndTimestamp;
// Клиент
#[ORM\ManyToOne(targetEntity: ClientEntity::class)]
#[ORM\JoinColumn(name: 'client_id', referencedColumnName: 'id', nullable: false)]
#[Groups(['travel:read'])]
#[MaxDepth(1)]
private ClientEntity $client;
// Договор
#[ORM\ManyToOne(targetEntity: ContractEntity::class)]
#[ORM\JoinColumn(name: 'contract_id', referencedColumnName: 'id', nullable: false)]
#[Groups(['travel:read'])]
#[MaxDepth(1)]
private ContractEntity $contract;
// Заявка
#[ORM\ManyToOne(targetEntity: ApplicationEntity::class)]
#[ORM\JoinColumn(name: 'application_id', referencedColumnName: 'id', nullable: false)]
#[Assert\NotNull(groups: ['travel:update', 'travel:create'])]
#[Assert\NotBlank(groups: ['travel:update', 'travel:create'])]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
#[MaxDepth(1)]
private ApplicationEntity $application;
// Автомобиль
#[ORM\ManyToOne(targetEntity: CarEntity::class)]
#[ORM\JoinColumn(name: 'car_id', referencedColumnName: 'id', nullable: false)]
#[Groups(['travel:read'])]
#[MaxDepth(1)]
private CarEntity $car;
// Оценка
#[ORM\Column(type: Types::FLOAT, scale: 2, options: ['default' => 0.00])]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
private ?float $rating = 0.00;
// Дата начала
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
#[Context(
normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d H:i:s'],
denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d H:i:s']
)]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
private ?\DateTimeInterface $startDate = null;
// Адрес начала
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
#[Assert\Length(min: 0, max: 255, groups: ['travel:update', 'travel:create'])]
#[Gedmo\Translatable]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
private ?string $startAddress = null;
// GPS координаты начала
#[ORM\Column(type: Types::JSON, nullable: true)]
#[Groups(['travel:read'])]
private ?string $startGPS = null;
// Дата окончания
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
#[Context(
normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d H:i:s'],
denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d H:i:s']
)]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
private ?\DateTimeInterface $endDate = null;
// Адрес окончания
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
#[Assert\Length(min: 0, max: 255, groups: ['travel:update', 'travel:create'])]
#[Gedmo\Translatable]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
private ?string $endAddress = null;
// GPS координаты окончания
#[ORM\Column(type: Types::JSON, nullable: true)]
#[Groups(['travel:read'])]
private ?string $endGPS = null;
// Продолжительность
#[ORM\Column(type: Types::DATEINTERVAL, nullable: true)]
#[Context(
normalizationContext: [DateIntervalNormalizer::FORMAT_KEY => '%H:%I:%S'],
denormalizationContext: [DateIntervalNormalizer::FORMAT_KEY => '%H:%I:%S']
)]
#[Groups(['travel:read'])]
private ?\DateInterval $duration = null;
// Пробег (километры)
#[ORM\Column(type: Types::FLOAT, scale: 4, options: ['default' => 0.0000])]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
private ?float $mileage = 0.0000;
// Скорректированный пробег (километры)
#[ORM\Column(type: Types::FLOAT, scale: 4, options: ['default' => 0.0000])]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
private ?float $correctMileage = 0.0000;
// Городской пробег (километры)
#[ORM\Column(type: Types::FLOAT, scale: 4, options: ['default' => 0.0000])]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
private ?float $cityMileage = 0.0000;
// Загородный пробег (километры)
#[ORM\Column(type: Types::FLOAT, scale: 4, options: ['default' => 0.0000])]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
private ?float $countryMileage = 0.0000;
// Конечный пробег (километры)
#[ORM\Column(type: Types::FLOAT, scale: 4, options: ['default' => 0.0000])]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
private ?float $finalMileage = 0.0000;
// Средняя скорость (км/ч)
#[ORM\Column(type: Types::INTEGER, options: ['default' => 0])]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
private ?int $avgSpeed = 0;
// Максимальная скорость (км/ч)
#[ORM\Column(type: Types::INTEGER, options: ['default' => 0])]
#[Groups(['travel:read', 'travel:update', 'travel:create'])]
private ?int $maxSpeed = 0;
// GPS координаты точки максимальной скорости
#[ORM\Column(type: Types::JSON, nullable: true)]
#[Groups(['travel:read'])]
private ?string $maxSpeedGPS = null;
// Нарушения
#[Groups(['travel:read'])]
public ?array $violations = null;
#[Gedmo\Timestampable(on: 'create')]
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
#[Groups(['travel:read'])]
protected $createdAt;
#[Gedmo\Timestampable(on: 'update')]
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
#[Groups(['travel:read'])]
protected $updatedAt;
/**
* Used locale to override Translation listener`s locale
* this is not a mapped field of entity metadata, just a simple property
*/
#[Gedmo\Locale]
private $locale;
public function getId(): ?int
{
return $this->id;
}
public function getExternalCode(): ?string
{
return $this->externalCode;
}
public function setExternalCode($code): ?self
{
$this->externalCode = $code;
return $this;
}
public function getExtStartTimestamp(): ?string
{
return $this->extStartTimestamp;
}
public function setExtStartTimestamp($value): ?self
{
$this->extStartTimestamp = $value;
return $this;
}
public function getExtEndTimestamp(): ?string
{
return $this->extEndTimestamp;
}
public function setExtEndTimestamp($value): ?self
{
$this->extEndTimestamp = $value;
return $this;
}
public function getClient(): ?ClientEntity
{
return $this->client;
}
public function setClient($client): ?self
{
$this->client = $client;
return $this;
}
public function getContract(): ?ContractEntity
{
return $this->contract;
}
public function setContract($contract): ?self
{
$this->contract = $contract;
return $this;
}
public function getApplication(): ?ApplicationEntity
{
return $this->application;
}
public function setApplication($application): ?self
{
$this->application = $application;
return $this;
}
public function getCar(): ?CarEntity
{
return $this->car;
}
public function setCar($car): ?self
{
$this->car = $car;
return $this;
}
public function getRating(): ?float
{
return $this->rating;
}
public function setRating(?float $value): ?self
{
$this->rating = ($value ? round($value, 2) : 0.00);
return $this;
}
public function setStartDate(?\DateTimeImmutable $date): ?self
{
$this->startDate = $date;
return $this;
}
public function getStartDate(): ?\DateTimeInterface
{
return $this->startDate;
}
public function getStartAddress(): ?string
{
return $this->startAddress;
}
public function setStartAddress($address): ?self
{
$this->startAddress = $address;
return $this;
}
public function getStartGPS(): ?array
{
return json_decode($this->startGPS ?? '[]', true);
}
public function setStartGPS(mixed $value): ?self
{
$this->startGPS = json_encode($value);
return $this;
}
public function setEndDate(?\DateTimeImmutable $date): ?self
{
$this->endDate = $date;
return $this;
}
public function getEndDate(): ?\DateTimeInterface
{
return $this->endDate;
}
public function getEndAddress(): ?string
{
return $this->endAddress;
}
public function setEndAddress($address): ?self
{
$this->endAddress = $address;
return $this;
}
public function getEndGPS(): ?array
{
return json_decode($this->endGPS ?? '[]', true);
}
public function setEndGPS(mixed $value): ?self
{
$this->endGPS = json_encode($value);
return $this;
}
public function getMileage(): ?float
{
return $this->mileage;
}
public function setMileage(?float $value): ?self
{
$this->mileage = ($value ? round($value, 4) : 0.0000);
return $this;
}
public function getCorrectMileage(): ?float
{
return $this->correctMileage;
}
public function setCorrectMileage(?float $value): ?self
{
$this->correctMileage = ($value ? round($value, 4) : 0.0000);
return $this;
}
public function getCityMileage(): ?float
{
return $this->cityMileage;
}
public function setCityMileage(?float $value): ?self
{
$this->cityMileage = ($value ? round($value, 4) : 0.0000);
return $this;
}
public function getCountryMileage(): ?float
{
return $this->countryMileage;
}
public function setCountryMileage(?float $value): ?self
{
$this->countryMileage = ($value ? round($value, 4) : 0.0000);
return $this;
}
public function getFinalMileage(): ?float
{
return $this->finalMileage;
}
public function setFinalMileage(?float $value): ?self
{
$this->finalMileage = ($value ? round($value, 4) : 0.0000);
return $this;
}
public function setDuration(?\DateInterval $value): ?self
{
$this->duration = $value;
return $this;
}
public function getDuration(): ?\DateInterval
{
return $this->duration;
}
public function setAvgSpeed(?int $value): ?self
{
$this->avgSpeed = $value;
return $this;
}
public function getAvgSpeed(): ?int
{
return $this->avgSpeed;
}
public function setMaxSpeed(?int $value): ?self
{
$this->maxSpeed = $value;
return $this;
}
public function getMaxSpeed(): ?int
{
return $this->maxSpeed;
}
public function getMaxSpeedGPS(): ?array
{
return json_decode($this->maxSpeedGPS ?? '[]', true);
}
public function setMaxSpeedGPS(mixed $value): ?self
{
$this->maxSpeedGPS = json_encode($value);
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
}