'!NULL'], 1, 0, ['key' => 'invoice_number', 'order' => 'DESC']); $last = $invoices[0]->invoice_number ?? null; $year = date("Y"); if ($last && preg_match('/^RN(\d+)-C(\d+)$/', $last, $m)) { $num = ($m[1] == $year) ? $m[2] + 1 : 1; } else { $num = 1; } return sprintf("RN%s-C%06d", $year, $num); } public function getProperty($name) { if (!$this->id) return null; switch ($name) { case 'positions': return ManualInvoicepositionModel::search(['manualinvoice_id' => $this->id]); case 'creator': return $this->create_by ? new User($this->create_by) : null; case 'editor': return $this->edit_by ? new User($this->edit_by) : null; default: $classname = ucfirst($name); $idfield = $name . '_id'; return (property_exists($this, $idfield) && class_exists($classname)) ? new $classname($this->$idfield) : null; } } }