src/Entity/Import/DataImport.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Import;
  3. use App\Entity\Client\Client;
  4. use App\Entity\Extension\BlameableTrait;
  5. use App\Entity\Extension\ImportableInterface;
  6. use App\Entity\Extension\TimestampableTrait;
  7. use App\Entity\File;
  8. use App\Entity\Gauge\Gauge;
  9. use App\Entity\Gauge\GaugeReading;
  10. use App\Entity\Gauge\Invoice;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use App\Repository\DataImportRepository;
  15. /**
  16. * @ORM\Entity(repositoryClass=DataImportRepository::class)
  17. * @ORM\Table(name="data_import")
  18. */
  19. class DataImport
  20. {
  21. use BlameableTrait;
  22. use TimestampableTrait;
  23. const TYPE_INVOICES = 'invoices';
  24. const TYPE_READINGS = 'readings';
  25. const TYPE_GAUGES = 'gauges';
  26. const TYPE_GAUGES_EDIT = 'gauges_edit';
  27. /**
  28. * @var null|int
  29. *
  30. * @ORM\Id()
  31. * @ORM\GeneratedValue()
  32. * @ORM\Column(type="integer")
  33. */
  34. private ?int $id = null;
  35. /**
  36. * @var Collection|DataImportItem[]
  37. *
  38. * @ORM\OneToMany(targetEntity="App\Entity\Import\DataImportItem", mappedBy="import", cascade={"all"})
  39. */
  40. private $items;
  41. /**
  42. * @var File|null
  43. * @ORM\ManyToOne(targetEntity="App\Entity\File", inversedBy="invoices")
  44. * @ORM\JoinColumn(nullable=true)
  45. */
  46. private ?File $file = null;
  47. /**
  48. * @var Client|null
  49. * @ORM\ManyToOne(targetEntity="App\Entity\Client\Client", inversedBy="files")
  50. * @ORM\JoinColumn(name="client_id", nullable=true)
  51. */
  52. private ?Client $client = null;
  53. /**
  54. * @var string
  55. *
  56. * @ORM\Column(type="string", nullable=false)
  57. */
  58. private string $type = self::TYPE_READINGS;
  59. /**
  60. * @var string|null
  61. *
  62. * @ORM\Column(type="string", nullable=true)
  63. */
  64. private ?string $statusMessage = null;
  65. /**
  66. * @var array
  67. *
  68. * @ORM\Column(type="json", nullable=false)
  69. */
  70. private array $reportInfo = [];
  71. /**
  72. * @var Collection|Invoice[]
  73. *
  74. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\Invoice", mappedBy="import")
  75. */
  76. private $invoices;
  77. /**
  78. * @var Collection|GaugeReading[]
  79. *
  80. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugeReading", mappedBy="import")
  81. */
  82. private $readings;
  83. /**
  84. * @var Collection|Gauge[]
  85. *
  86. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\Gauge", mappedBy="import")
  87. */
  88. private $gauges;
  89. public function __construct()
  90. {
  91. $this->invoices = new ArrayCollection();
  92. }
  93. public function getId(): ?int
  94. {
  95. return $this->id;
  96. }
  97. public function setId(?int $id): DataImport
  98. {
  99. $this->id = $id;
  100. return $this;
  101. }
  102. /**
  103. * @return DataImportItem[]|Collection
  104. */
  105. public function getItems()
  106. {
  107. return $this->items;
  108. }
  109. /**
  110. * @param DataImportItem[]|Collection $items
  111. * @return DataImport
  112. */
  113. public function setItems($items)
  114. {
  115. $this->items = $items;
  116. return $this;
  117. }
  118. public function getFile(): ?File
  119. {
  120. return $this->file;
  121. }
  122. public function setFile(?File $file): DataImport
  123. {
  124. $this->file = $file;
  125. return $this;
  126. }
  127. public function getClient(): ?Client
  128. {
  129. return $this->client;
  130. }
  131. public function setClient(?Client $client): DataImport
  132. {
  133. $this->client = $client;
  134. return $this;
  135. }
  136. public function addItem(ImportableInterface $item): DataImport
  137. {
  138. if ($item->getId() !== null) {
  139. $this->items->add(
  140. (new DataImportItem())
  141. ->setImport($this)
  142. ->setItemId($item->getId())
  143. ->setItemClass(get_class($item))
  144. );
  145. }
  146. return $this;
  147. }
  148. public function getType(): string
  149. {
  150. return $this->type;
  151. }
  152. public function setType(string $type): self
  153. {
  154. $this->type = $type;
  155. return $this;
  156. }
  157. public function getReportInfo(): array
  158. {
  159. return $this->reportInfo;
  160. }
  161. public function setReportInfo(array $reportInfo): self
  162. {
  163. $this->reportInfo = $reportInfo;
  164. return $this;
  165. }
  166. public function getSuccessMessages():array
  167. {
  168. $info = $this->reportInfo;
  169. if (array_key_exists('messages', $info) && array_key_exists('success', $info['messages'])) {
  170. return $info['messages']['success'];
  171. }
  172. return [];
  173. }
  174. public function getWarningMessages():array
  175. {
  176. $info = $this->reportInfo;
  177. if (array_key_exists('messages', $info) && array_key_exists('warning', $info['messages'])) {
  178. return $info['messages']['warning'];
  179. }
  180. return [];
  181. }
  182. public function getErrorMessages():array
  183. {
  184. $info = $this->reportInfo;
  185. if (array_key_exists('messages', $info) && array_key_exists('error', $info['messages'])) {
  186. return $info['messages']['error'];
  187. }
  188. return [];
  189. }
  190. public function getErrorRows(): array
  191. {
  192. $info = $this->reportInfo;
  193. if (array_key_exists('errorRows', $info)) {
  194. return $info['errorRows'];
  195. }
  196. return [];
  197. }
  198. public function isInvoiceType():bool
  199. {
  200. return $this->type === self::TYPE_INVOICES;
  201. }
  202. public function isReadingsType():bool
  203. {
  204. return $this->type === self::TYPE_READINGS;
  205. }
  206. public function getStatusMessage(): ?string
  207. {
  208. return $this->statusMessage;
  209. }
  210. public function setStatusMessage(?string $statusMessage): DataImport
  211. {
  212. $this->statusMessage = $statusMessage;
  213. return $this;
  214. }
  215. /**
  216. * @return Invoice[]|ArrayCollection|Collection
  217. */
  218. public function getInvoices()
  219. {
  220. return $this->invoices;
  221. }
  222. /**
  223. * @param Invoice[]|ArrayCollection|Collection $invoices
  224. * @return DataImport
  225. */
  226. public function setInvoices($invoices)
  227. {
  228. $this->invoices = $invoices;
  229. return $this;
  230. }
  231. /**
  232. * @return GaugeReading[]|Collection
  233. */
  234. public function getReadings()
  235. {
  236. return $this->readings;
  237. }
  238. /**
  239. * @param GaugeReading[]|Collection $readings
  240. * @return DataImport
  241. */
  242. public function setReadings($readings)
  243. {
  244. $this->readings = $readings;
  245. return $this;
  246. }
  247. /**
  248. * @return Collection|Gauge[]
  249. */
  250. public function getGauges()
  251. {
  252. return $this->gauges;
  253. }
  254. /**
  255. * @param Collection|Gauge[] $gauges
  256. * @return DataImport
  257. */
  258. public function setGauges($gauges): DataImport
  259. {
  260. $this->gauges = $gauges;
  261. return $this;
  262. }
  263. /**
  264. * @param array $warningData
  265. * @return $this
  266. */
  267. public function addWarning(array $warningData)
  268. {
  269. $this->reportInfo['messages']['warning'][] = $warningData;
  270. return $this;
  271. }
  272. public function getBackupRows(): array
  273. {
  274. $info = $this->reportInfo;
  275. if (array_key_exists('backupRows', $info)) {
  276. return $info['backupRows'];
  277. }
  278. return [];
  279. }
  280. }