<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use App\Repository\BikeLogRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* normalizationContext={"groups"={"bike_log:read"}},
* denormalizationContext={"groups"={"bike_log:write"}},
* itemOperations={
* "get"={"security"="is_granted('ROLE_USER')"}
* },
* collectionOperations={
* "get"={"security"="is_granted('ROLE_USER')"}
* }
* )
* @ORM\Entity(repositoryClass=BikeLogRepository::class)
* @ApiFilter(SearchFilter::class, properties={"user.id": "exact", "company.id": "exact"})
* @ApiFilter(DateFilter::class, properties={"logDate", "dateStart", "dateEnd"})
* @ApiFilter(OrderFilter::class)
*/
class BikeLog
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"bike_log:read"})
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=false)
* @Groups({"bike_log:read", "bike_log:write"})
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity=Company::class)
* @Groups({"bike_log:read", "bike_log:write"})
*/
private $company;
/**
* Distance en mètres (avec décimales)
* @ORM\Column(type="decimal", precision=10, scale=2, options={"default": "0"})
* @Groups({"bike_log:read", "bike_log:write"})
*/
private $distanceAmount = 0;
/**
* Date du log (jour normalisé à minuit)
* @ORM\Column(type="date")
* @Groups({"bike_log:read", "bike_log:write"})
*/
private $logDate;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Groups({"bike_log:read", "bike_log:write"})
*/
private $dateStart;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Groups({"bike_log:read", "bike_log:write"})
*/
private $dateEnd;
/**
* @ORM\Column(type="string", length=50, nullable=true)
* @Groups({"bike_log:read", "bike_log:write"})
*/
private $source;
/**
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
* @Groups({"bike_log:read"})
*/
private $previousDistanceAmount;
/**
* @ORM\Column(type="integer", options={"default": "0"})
* @Groups({"bike_log:read"})
*/
private $regularizationCount = 0;
/**
* @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
* @Gedmo\Timestampable(on="create")
* @Groups({"bike_log:read"})
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Gedmo\Timestampable(on="update")
* @Groups({"bike_log:read"})
*/
private $updatedAt;
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getDistanceAmount(): float
{
return (float) $this->distanceAmount;
}
public function setDistanceAmount(float $distanceAmount): self
{
$this->distanceAmount = $distanceAmount;
return $this;
}
/**
* Retourne la distance en kilomètres
*/
public function getDistanceInKm(): float
{
return $this->distanceAmount / 1000;
}
public function getLogDate(): ?\DateTimeInterface
{
return $this->logDate;
}
public function setLogDate(\DateTimeInterface $logDate): self
{
$this->logDate = $logDate;
return $this;
}
public function getDateStart(): ?\DateTimeInterface
{
return $this->dateStart;
}
public function setDateStart(?\DateTimeInterface $dateStart): self
{
$this->dateStart = $dateStart;
return $this;
}
public function getDateEnd(): ?\DateTimeInterface
{
return $this->dateEnd;
}
public function setDateEnd(?\DateTimeInterface $dateEnd): self
{
$this->dateEnd = $dateEnd;
return $this;
}
public function getSource(): ?string
{
return $this->source;
}
public function setSource(?string $source): self
{
$this->source = $source;
return $this;
}
public function getPreviousDistanceAmount(): ?float
{
return $this->previousDistanceAmount ? (float) $this->previousDistanceAmount : null;
}
public function setPreviousDistanceAmount(?float $previousDistanceAmount): self
{
$this->previousDistanceAmount = $previousDistanceAmount;
return $this;
}
public function getRegularizationCount(): int
{
return $this->regularizationCount;
}
public function incrementRegularizationCount(): self
{
$this->regularizationCount++;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
}