<?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\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use App\Ship\ApiPlatform\Filter\SimpleSearchFilter;
use App\Containers\ServiceSection\TravelContainer\Data\Repositories\ViolationRepository;
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\TravelContainer\Entities\TravelEntity;
use App\Containers\ServiceSection\IndexContainer\Entities\TranslationEntity;
/**
* Нарушения
*/
#[ApiResource(
routePrefix: '/service',
normalizationContext: [
'groups' => ['violation:read', 'travel:read', 'read'],
'enable_max_depth' => true
],
denormalizationContext: [
'groups' => ['violation:create', 'violation:update']
],
operations: [
new Get(
uriTemplate: '/violations/{id}', requirements: ['id' => '\d+'],
security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_USER_CLIENT') or is_granted('ROLE_READONLY')"
),
new Patch(
uriTemplate: '/violations/{id}', requirements: ['id' => '\d+'],
security: "is_granted('ROLE_ADMIN')"
),
// new Put(
// uriTemplate: '/violations/{id}', requirements: ['id' => '\d+'],
// security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_USER_CLIENT')"
// ),
new Delete(
uriTemplate: '/violations/{id}', requirements: ['id' => '\d+'],
security: "is_granted('ROLE_ADMIN')"
),
// new Post(
// uriTemplate: '/violations',
// security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_USER_CLIENT')"
// ),
new GetCollection(
uriTemplate: '/violations',
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',
'travel' => 'exact'
])]
#[ApiFilter(NumericFilter::class, properties: ['id'])]
#[ApiFilter(RangeFilter::class, properties: [
'id',
'travel.id'
])]
#[ApiFilter(DateFilter::class, properties: [
'startDate',
'endDate',
'createdAt',
'updatedAt'
])]
#[ApiFilter(OrderFilter::class)]
#[ApiFilter(filterClass: SimpleSearchFilter::class, properties: ['externalCode'])]
#[ORM\Entity(repositoryClass: ViolationRepository::class)]
#[ORM\Table(name: '`service_violations`')]
#[ORM\Index(columns: ['ext_start_timestamp', 'ext_end_timestamp'], name: 'service_violations_custom_idx')]
#[Gedmo\TranslationEntity(class: TranslationEntity::class)]
#[UniqueEntity(fields: ['externalCode'])]
class ViolationEntity
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
#[Groups(['violation:read'])]
private ?int $id = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true, unique: true)]
#[Assert\Length(min: 0, max: 255, groups: ['violation:update', 'violation:create'])]
#[Groups(['violation:read', 'violation:update', 'violation: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: ['violation:update', 'violation:create'])]
#[Groups(['violation:read', 'violation:update', 'violation: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: ['violation:update', 'violation:create'])]
#[Groups(['violation:read', 'violation:update', 'violation: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(['violation:read'])]
#[MaxDepth(1)]
private ClientEntity $client;
// Договор
#[ORM\ManyToOne(targetEntity: ContractEntity::class)]
#[ORM\JoinColumn(name: 'contract_id', referencedColumnName: 'id', nullable: false)]
#[Groups(['violation:read'])]
#[MaxDepth(1)]
private ContractEntity $contract;
// Заявка
#[ORM\ManyToOne(targetEntity: ApplicationEntity::class)]
#[ORM\JoinColumn(name: 'application_id', referencedColumnName: 'id', nullable: false)]
#[Assert\NotNull(groups: ['violation:update', 'violation:create'])]
#[Assert\NotBlank(groups: ['violation:update', 'violation:create'])]
#[Groups(['violation:read', 'violation:update', 'violation:create'])]
#[MaxDepth(1)]
private ApplicationEntity $application;
// Автомобиль
#[ORM\ManyToOne(targetEntity: CarEntity::class)]
#[ORM\JoinColumn(name: 'car_id', referencedColumnName: 'id', nullable: false)]
#[Groups(['violation:read'])]
#[MaxDepth(1)]
private CarEntity $car;
// Поездка
#[ORM\ManyToOne(targetEntity: TravelEntity::class)]
#[ORM\JoinColumn(name: 'travel_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
#[Groups(['violation:read'])]
#[MaxDepth(1)]
private TravelEntity $travel;
// Нарушение
#[ORM\Column(type: Types::STRING, length: 150, nullable: false)]
#[Assert\Length(min: 0, max: 150, groups: ['violation:update', 'violation:create'])]
#[Gedmo\Translatable]
#[Groups(['travel:read', 'violation:read', 'violation:update', 'violation:create'])]
private string $title;
// Дата начала
#[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', 'violation:read', 'violation:update', 'violation:create'])]
private ?\DateTimeInterface $startDate = null;
// Адрес начала
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
#[Assert\Length(min: 0, max: 255, groups: ['violation:update', 'violation:create'])]
#[Gedmo\Translatable]
#[Groups(['travel:read', 'violation:read', 'violation:update', 'violation:create'])]
private ?string $startAddress = null;
// GPS координаты начала
#[ORM\Column(type: Types::JSON, nullable: true)]
#[Groups(['travel:read', 'violation:read'])]
private $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', 'violation:read', 'violation:update', 'violation:create'])]
private ?\DateTimeInterface $endDate;
// Адрес окончания
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
#[Assert\Length(min: 0, max: 255, groups: ['violation:update', 'violation:create'])]
#[Gedmo\Translatable]
#[Groups(['travel:read', 'violation:read', 'violation:update', 'violation:create'])]
private ?string $endAddress = null;
// GPS координаты окончания
#[ORM\Column(type: Types::JSON, nullable: true)]
#[Groups(['travel:read', 'violation:read'])]
private $endGPS = null;
// Значение
#[ORM\Column(type: Types::STRING, length: 50, nullable: true)]
#[Assert\Length(min: 0, max: 50, groups: ['violation:update', 'violation:create'])]
#[Gedmo\Translatable]
#[Groups(['travel:read', 'violation:read', 'violation:update', 'violation:create'])]
private ?string $value = null;
// GPS координаты точки значения
#[ORM\Column(type: Types::JSON, nullable: true)]
#[Groups(['travel:read'])]
private ?string $valueGPS = null;
#[Gedmo\Timestampable(on: 'create')]
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
#[Groups(['violation:read'])]
protected $createdAt;
#[Gedmo\Timestampable(on: 'update')]
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
#[Groups(['violation: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 getTravel(): ?TravelEntity
{
return $this->travel;
}
public function setTravel($travel): ?self
{
$this->travel = $travel;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): ?self
{
$this->title = $title;
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()
{
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()
{
return json_decode($this->endGPS ?? '[]', true);
}
public function setEndGPS(mixed $value): ?self
{
$this->endGPS = json_encode($value);
return $this;
}
public function getValue(): ?string
{
return $this->value;
}
public function setValue(?string $value): ?self
{
$this->value = $value;
return $this;
}
public function getValueGPS()
{
return json_decode($this->valueGPS ?? '[]', true);
}
public function setValueGPS(mixed $value): ?self
{
$this->valueGPS = 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;
}
}