src/Entity/ServiceUsage.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Billing\Model\BillableItemInterface;
  4. use App\Repository\ServiceUsageRepository;
  5. use Doctrine\DBAL\Types\Types;
  6. use Doctrine\ORM\Mapping as ORM;
  7. #[ORM\Entity(repositoryClassServiceUsageRepository::class)]
  8. class ServiceUsage implements BillableItemInterface
  9. {
  10.     const STATUS_UNBILLED 'UNBILLED';
  11.     const STATUS_BILLED 'BILLED';
  12.     const STATUS_CANCELLED 'CANCELLED';
  13.     #[ORM\Id]
  14.     #[ORM\GeneratedValue]
  15.     #[ORM\Column]
  16.     private ?int $id null;
  17.     #[ORM\ManyToOne]
  18.     #[ORM\JoinColumn(nullablefalse)]
  19.     private ?Service $service null;
  20.     #[ORM\Column(typeTypes::FLOAT)]
  21.     private ?float $quantity 1.0;
  22.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  23.     private ?\DateTimeInterface $date null;
  24.     #[ORM\Column(length50)]
  25.     private ?string $status self::STATUS_UNBILLED;
  26.     #[ORM\ManyToOne(inversedBy'serviceUsages')]
  27.     private ?User $user null;
  28.     #[ORM\ManyToOne(inversedBy'serviceUsages')]
  29.     private ?Organization $organization null;
  30.     #[ORM\Column(typeTypes::FLOATnullabletrue)]
  31.     private ?float $unitPrice null;
  32.     #[ORM\Column(typeTypes::FLOATnullabletrue)]
  33.     private ?float $exchangeRate null;
  34.     #[ORM\Column(typeTypes::FLOATnullabletrue)]
  35.     private ?float $vatRate null;
  36.     #[ORM\Column(length255nullabletrue)]
  37.     private ?string $description null;
  38.     public function __construct()
  39.     {
  40.         $this->date = new \DateTime();
  41.     }
  42.     public function getId(): ?int
  43.     {
  44.         return $this->id;
  45.     }
  46.     public function getService(): ?Service
  47.     {
  48.         return $this->service;
  49.     }
  50.     public function setService(?Service $service): static
  51.     {
  52.         $this->service $service;
  53.         return $this;
  54.     }
  55.     public function getQuantity(): ?float
  56.     {
  57.         return $this->quantity;
  58.     }
  59.     public function setQuantity(float $quantity): static
  60.     {
  61.         $this->quantity $quantity;
  62.         return $this;
  63.     }
  64.     public function getDate(): ?\DateTimeInterface
  65.     {
  66.         return $this->date;
  67.     }
  68.     public function setDate(\DateTimeInterface $date): static
  69.     {
  70.         $this->date $date;
  71.         return $this;
  72.     }
  73.     public function getStatus(): ?string
  74.     {
  75.         return $this->status;
  76.     }
  77.     public function setStatus(string $status): static
  78.     {
  79.         $this->status $status;
  80.         return $this;
  81.     }
  82.     public function getUser(): ?User
  83.     {
  84.         return $this->user;
  85.     }
  86.     public function setUser(?User $user): static
  87.     {
  88.         $this->user $user;
  89.         return $this;
  90.     }
  91.     public function getOrganization(): ?Organization
  92.     {
  93.         return $this->organization;
  94.     }
  95.     public function setOrganization(?Organization $organization): static
  96.     {
  97.         $this->organization $organization;
  98.         return $this;
  99.     }
  100.     public function getUnitPrice(): ?float
  101.     {
  102.         return $this->unitPrice;
  103.     }
  104.     public function setUnitPrice(?float $unitPrice): static
  105.     {
  106.         $this->unitPrice $unitPrice;
  107.         return $this;
  108.     }
  109.     public function getExchangeRate(): ?float
  110.     {
  111.         return $this->exchangeRate;
  112.     }
  113.     public function setExchangeRate(?float $exchangeRate): static
  114.     {
  115.         $this->exchangeRate $exchangeRate;
  116.         return $this;
  117.     }
  118.     public function getVatRate(): ?float
  119.     {
  120.         return $this->vatRate;
  121.     }
  122.     public function setVatRate(?float $vatRate): static
  123.     {
  124.         $this->vatRate $vatRate;
  125.         return $this;
  126.     }
  127.     public function getDescription(): ?string
  128.     {
  129.         return $this->description;
  130.     }
  131.     public function setDescription(?string $description): static
  132.     {
  133.         $this->description $description;
  134.         return $this;
  135.     }
  136.     // BillableItemInterface
  137.     public function getBillableDescription(): string
  138.     {
  139.         $desc $this->description;
  140.         if (!$desc) {
  141.             $desc $this->service->getNom() . ' (x' $this->quantity ')';
  142.         }
  143.         if ($this->user) {
  144.             $nomComplet $this->user->getPrenom() . ' ' $this->user->getNom();
  145.             // Append only if not already in description (case insensitive check to be safe)
  146.             if (stripos($desc$nomComplet) === false) {
  147.                 $desc .= ' - ' $nomComplet;
  148.             }
  149.         }
  150.         return $desc;
  151.     }
  152.     public function getBillablePrice(): float
  153.     {
  154.         // Base price calculation (from fixed unitPrice or service default)
  155.         $price $this->unitPrice !== null $this->unitPrice $this->service->getPrixUnitaire();
  156.         // Apply exchange rate if present (assuming unitPrice is in foreign currency)
  157.         if ($this->exchangeRate !== null) {
  158.             $price $price $this->exchangeRate;
  159.         }
  160.         return $price $this->quantity;
  161.     }
  162.     public function getBillableVatRate(): float
  163.     {
  164.         // Use frozen VAT rate if available, otherwise Service default
  165.         return $this->vatRate !== null $this->vatRate $this->service->getTva();
  166.     }
  167. }