<?php
namespace App\Entity\Import;
use App\Entity\Client\Client;
use App\Entity\Extension\BlameableTrait;
use App\Entity\Extension\ImportableInterface;
use App\Entity\Extension\TimestampableTrait;
use App\Entity\File;
use App\Entity\Gauge\Gauge;
use App\Entity\Gauge\GaugeReading;
use App\Entity\Gauge\Invoice;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\DataImportRepository;
/**
* @ORM\Entity(repositoryClass=DataImportRepository::class)
* @ORM\Table(name="data_import")
*/
class DataImport
{
use BlameableTrait;
use TimestampableTrait;
const TYPE_INVOICES = 'invoices';
const TYPE_READINGS = 'readings';
const TYPE_GAUGES = 'gauges';
const TYPE_GAUGES_EDIT = 'gauges_edit';
/**
* @var null|int
*
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @var Collection|DataImportItem[]
*
* @ORM\OneToMany(targetEntity="App\Entity\Import\DataImportItem", mappedBy="import", cascade={"all"})
*/
private $items;
/**
* @var File|null
* @ORM\ManyToOne(targetEntity="App\Entity\File", inversedBy="invoices")
* @ORM\JoinColumn(nullable=true)
*/
private ?File $file = null;
/**
* @var Client|null
* @ORM\ManyToOne(targetEntity="App\Entity\Client\Client", inversedBy="files")
* @ORM\JoinColumn(name="client_id", nullable=true)
*/
private ?Client $client = null;
/**
* @var string
*
* @ORM\Column(type="string", nullable=false)
*/
private string $type = self::TYPE_READINGS;
/**
* @var string|null
*
* @ORM\Column(type="string", nullable=true)
*/
private ?string $statusMessage = null;
/**
* @var array
*
* @ORM\Column(type="json", nullable=false)
*/
private array $reportInfo = [];
/**
* @var Collection|Invoice[]
*
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\Invoice", mappedBy="import")
*/
private $invoices;
/**
* @var Collection|GaugeReading[]
*
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugeReading", mappedBy="import")
*/
private $readings;
/**
* @var Collection|Gauge[]
*
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\Gauge", mappedBy="import")
*/
private $gauges;
public function __construct()
{
$this->invoices = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function setId(?int $id): DataImport
{
$this->id = $id;
return $this;
}
/**
* @return DataImportItem[]|Collection
*/
public function getItems()
{
return $this->items;
}
/**
* @param DataImportItem[]|Collection $items
* @return DataImport
*/
public function setItems($items)
{
$this->items = $items;
return $this;
}
public function getFile(): ?File
{
return $this->file;
}
public function setFile(?File $file): DataImport
{
$this->file = $file;
return $this;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(?Client $client): DataImport
{
$this->client = $client;
return $this;
}
public function addItem(ImportableInterface $item): DataImport
{
if ($item->getId() !== null) {
$this->items->add(
(new DataImportItem())
->setImport($this)
->setItemId($item->getId())
->setItemClass(get_class($item))
);
}
return $this;
}
public function getType(): string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getReportInfo(): array
{
return $this->reportInfo;
}
public function setReportInfo(array $reportInfo): self
{
$this->reportInfo = $reportInfo;
return $this;
}
public function getSuccessMessages():array
{
$info = $this->reportInfo;
if (array_key_exists('messages', $info) && array_key_exists('success', $info['messages'])) {
return $info['messages']['success'];
}
return [];
}
public function getWarningMessages():array
{
$info = $this->reportInfo;
if (array_key_exists('messages', $info) && array_key_exists('warning', $info['messages'])) {
return $info['messages']['warning'];
}
return [];
}
public function getErrorMessages():array
{
$info = $this->reportInfo;
if (array_key_exists('messages', $info) && array_key_exists('error', $info['messages'])) {
return $info['messages']['error'];
}
return [];
}
public function getErrorRows(): array
{
$info = $this->reportInfo;
if (array_key_exists('errorRows', $info)) {
return $info['errorRows'];
}
return [];
}
public function isInvoiceType():bool
{
return $this->type === self::TYPE_INVOICES;
}
public function isReadingsType():bool
{
return $this->type === self::TYPE_READINGS;
}
public function getStatusMessage(): ?string
{
return $this->statusMessage;
}
public function setStatusMessage(?string $statusMessage): DataImport
{
$this->statusMessage = $statusMessage;
return $this;
}
/**
* @return Invoice[]|ArrayCollection|Collection
*/
public function getInvoices()
{
return $this->invoices;
}
/**
* @param Invoice[]|ArrayCollection|Collection $invoices
* @return DataImport
*/
public function setInvoices($invoices)
{
$this->invoices = $invoices;
return $this;
}
/**
* @return GaugeReading[]|Collection
*/
public function getReadings()
{
return $this->readings;
}
/**
* @param GaugeReading[]|Collection $readings
* @return DataImport
*/
public function setReadings($readings)
{
$this->readings = $readings;
return $this;
}
/**
* @return Collection|Gauge[]
*/
public function getGauges()
{
return $this->gauges;
}
/**
* @param Collection|Gauge[] $gauges
* @return DataImport
*/
public function setGauges($gauges): DataImport
{
$this->gauges = $gauges;
return $this;
}
/**
* @param array $warningData
* @return $this
*/
public function addWarning(array $warningData)
{
$this->reportInfo['messages']['warning'][] = $warningData;
return $this;
}
public function getBackupRows(): array
{
$info = $this->reportInfo;
if (array_key_exists('backupRows', $info)) {
return $info['backupRows'];
}
return [];
}
}