<?php
namespace App\Entity\Gauge;
use App\Entity\Extension\BlameableTrait;
use App\Entity\Extension\ImportableInterface;
use App\Entity\Extension\TimestampableTrait;
use App\Entity\File;
use App\Entity\FileCategory;
use App\Entity\Import\DataImport;
use App\Utils\GaugeType;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\Gauge\InvoiceRepository;
use App\Validator as AcmeAssert;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=InvoiceRepository::class)
* @ORM\Table(name="invoices", indexes={@ORM\Index(name="date_idx", columns={"date_from", "date_to"})})
*/
class Invoice implements ImportableInterface
{
/**
* @var int|null
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @var Gauge $gauge
* @ORM\ManyToOne(targetEntity="App\Entity\Gauge\Gauge", inversedBy="invoices")
* @ORM\JoinColumn(name="gauge_id", referencedColumnName="id", nullable=false)
*/
private Gauge $gauge;
/**
* @var bool
* @ORM\Column(name="credit_note", type="boolean", nullable=false, options={"default": 0})
*/
private bool $creditNote = false;
/**
* @var string
* @ORM\Column(type="string")
*/
private string $invoiceNumber;
/**
* @var \DateTime
* @AcmeAssert\DateTimeRange()
* @ORM\Column(name="date_from", type="datetime", nullable=false)
*/
protected \DateTime $dateFrom;
/**
* @var \DateTime
* @AcmeAssert\DateTimeRange()
* @ORM\Column(name="date_to", type="datetime", nullable=false)
*/
protected \DateTime $dateTo;
/**
* @ORM\Column(type="float", options={"default" : 0})
*/
private float $priceWithoutVat;
/**
* @ORM\Column(type="float")
*/
private float $price;
/**
* @var File|null
* @ORM\ManyToOne(targetEntity="App\Entity\File", inversedBy="invoices")
* @ORM\JoinColumn(nullable=true)
*/
private ?File $file = null;
/**
* @var string|null
* @ORM\Column(type="text", nullable=true)
*/
private ?string $note = null;
/**
* @var DataImport|null
* @ORM\ManyToOne(targetEntity="App\Entity\Import\DataImport", inversedBy="invoices")
* @ORM\JoinColumn(nullable=true)
*/
private ?DataImport $import = null;
/**
* @var Collection|InvoiceValue[]
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\InvoiceValue", mappedBy="invoice", cascade={"all"})
*/
private $values;
/**
* record_id in Revisio source database, also referencing an entry in RevisioRecord
* @var ?RevisioRecord
* @ORM\OneToOne(targetEntity="App\Entity\Gauge\RevisioRecord", cascade={"all"})
* @ORM\JoinColumn(name="revisio_record_id", referencedColumnName="record_id", nullable=true)
*/
private ?RevisioRecord $revisioRecord = null;
use BlameableTrait;
use TimestampableTrait;
public function __construct()
{
$this->values = new ArrayCollection();
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @param int|null $id
* @return Invoice
*/
public function setId(?int $id): Invoice
{
$this->id = $id;
return $this;
}
/**
* @return bool
*/
public function isCreditNote(): bool
{
return $this->creditNote;
}
/**
* @param bool $creditNote
* @return self
*/
public function setCreditNote(bool $creditNote): self
{
$this->creditNote = $creditNote;
return $this;
}
/**
* @return Gauge
*/
public function getGauge(): Gauge
{
return $this->gauge;
}
/**
* @param Gauge $gauge
* @return Invoice
*/
public function setGauge(Gauge $gauge): Invoice
{
$this->gauge = $gauge;
return $this;
}
/**
* @return string
*/
public function getInvoiceNumber(): string
{
return $this->invoiceNumber;
}
/**
* @param string $invoiceNumber
* @return Invoice
*/
public function setInvoiceNumber(string $invoiceNumber): Invoice
{
$this->invoiceNumber = $invoiceNumber;
return $this;
}
/**
* @return \DateTime
*/
public function getDateFrom(): \DateTime
{
return $this->dateFrom;
}
/**
* @param \DateTime $dateFrom
* @return Invoice
*/
public function setDateFrom(\DateTime $dateFrom): Invoice
{
$this->dateFrom = $dateFrom;
return $this;
}
/**
* @return \DateTime
*/
public function getDateTo(): \DateTime
{
return $this->dateTo;
}
/**
* @param \DateTime $dateTo
* @return Invoice
*/
public function setDateTo(\DateTime $dateTo): Invoice
{
$this->dateTo = $dateTo;
return $this;
}
/**
* @return InvoiceValue[]|Collection
*/
public function getValues()
{
return $this->values;
}
/**
* @param InvoiceValue[]|Collection $values
* @return Invoice
*/
public function setValues($values)
{
$this->values = $values;
return $this;
}
/**
* @param InvoiceValue $value
* @return $this
*/
public function addValue(InvoiceValue $value): self
{
if (!$this->values->contains($value)) {
$this->values->add($value);
$value->setInvoice($this);
}
return $this;
}
/**
* @param int $segment
* @return float|null
*/
public function getSegmentValue(int $segment) : ?float
{
foreach ($this->getValues() as $invValue) {
if ($invValue->getSegment() === $segment) {
return $invValue->getValue();
}
}
return null;
}
/**
* @param int $segment
* @return InvoiceValue|null
*/
public function getValueItemBySegment(int $segment) : ?InvoiceValue
{
foreach ($this->getValues() as $invValue) {
if ($invValue->getSegment() === $segment) {
return $invValue;
}
}
return null;
}
/**
* @return float
*/
public function getPrice(): float
{
return $this->price;
}
/**
* @param float $price
* @return Invoice
*/
public function setPrice(float $price): Invoice
{
$this->price = $price;
return $this;
}
/**
* @return File|null
*/
public function getFile(): ?File
{
return $this->file;
}
/**
* @param File|null $file
* @return Invoice
*/
public function setFile(?File $file): Invoice
{
$this->file = $file;
if ($file !== null) {
$file->addGauge($this->getGauge());
}
return $this;
}
/**
* @return string
*/
public function getFileCategoryMachineName(): string
{
$catNamesByType = [
GaugeType::TYPE_ELECTRICITY_METER => FileCategory::INVOICES_ELECTRICITY,
GaugeType::TYPE_HEAT_METER => FileCategory::INVOICES_HEAT,
GaugeType::TYPE_GASOMETER => FileCategory::INVOICES_GAS,
GaugeType::TYPE_WATER_METER => FileCategory::INVOICES_WATER,
];
if (array_key_exists($this->getGauge()->getType(), $catNamesByType)) {
return $catNamesByType[$this->getGauge()->getType()];
}
return FileCategory::INVOICES_OTHER;
}
public function getNote(): ?string
{
return $this->note;
}
public function setNote(?string $note): Invoice
{
$this->note = $note;
return $this;
}
public function getPriceWithoutVat(): float
{
return $this->priceWithoutVat;
}
public function setPriceWithoutVat(float $priceWithoutVat): Invoice
{
$this->priceWithoutVat = $priceWithoutVat;
return $this;
}
public function getImport(): ?DataImport
{
return $this->import;
}
public function setImport(?DataImport $import): Invoice
{
$this->import = $import;
return $this;
}
/**
* @return RevisioRecord|null
*/
public function getRevisioRecord(): ?RevisioRecord
{
return $this->revisioRecord;
}
/**
* @param RevisioRecord|null $revisioRecord
* @return $this
*/
public function setRevisioRecord(?RevisioRecord $revisioRecord): Invoice
{
$this->revisioRecord = $revisioRecord;
return $this;
}
}