<?php
 namespace App\ReportsData\EntityFields; class InvoiceRowsGroup extends \App\ReportsData\EntityFields\GeneralAbstract { protected $pkColumnName = 'id'; protected function getDbTableName() { global $DBTRd_clientInvoiceRows; return $DBTRd_clientInvoiceRows; } protected function populateDefaultValues() :void { if (!$this->id) { $this->values = []; $this->recordExists = false; return ; } if (!is_string($this->id)) { throw new \Exception('The invoice rows ids must be set as string separated by `|`.'); } $ids = explodeClean('|', $this->id); $db = \DB::getInstance(); $selectRowsSQL = "SELECT * FROM `{$this->dbTableName}` WHERE `{$this->pkColumnName}` IN (" . implode(', ', array_fill(0, count($ids), '?')) . ")"; $selectRowsEx = $db->Execute($selectRowsSQL, $ids); if ($db->ErrorNo()) { throw new \DBException('The RD record could not be selected: '.$db->ErrorMsg()); } $entityInfo = $selectRowsEx->GetAll(); if (!$entityInfo or count($entityInfo)!=count($ids)) { $this->values = []; $this->recordExists = false; return ; } $this->recordExists = true; $invoiceRows = []; foreach($entityInfo as $row) { if ($row['extraInfo']) { $row['extraInfo'] = json_decode($row['extraInfo'], true); } else { $row['extraInfo'] = []; } $invoiceRows[$row['id']] = $row; } ksort($invoiceRows); $this->values = [ 'id'=>$this->id, 'rowsDetails' => $invoiceRows ]; } protected function getCustomValues($fieldName) { switch ($fieldName) { case 'idService': if ($this->recordExists) { $firstRow = reset($this->values['rowsDetails']); $idService = $firstRow['idService']??0; $this->addCachedValue($fieldName, $idService); return $idService; } else { return 0; } case 'service': return new Service($this->getValue('idService')); default: return parent::getCustomValues($fieldName); } } } 