tempData['data'])) { if ($row === null) { $row = $this->tempData['data']; } else { $row = array_merge($this->tempData['data'], $this->transformDataToArray($row, 'insert')); } $this->escape = $this->tempData['escape'] ?? []; $this->tempData = []; } return parent::insert($this->withDefaultSchoolYear($row), $returnID); } public function insertBatch(?array $set = null, ?bool $escape = null, int $batchSize = 100, bool $testing = false) { if (is_array($set)) { foreach ($set as $index => $row) { $set[$index] = $this->withDefaultSchoolYear($row); } } return parent::insertBatch($set, $escape, $batchSize, $testing); } protected function withDefaultSchoolYear($row) { if (! is_array($row)) { return $row; } if ($this->hasNonEmptySchoolYear($row)) { return $row; } $schoolYear = $this->deriveSchoolYearForRow($row) ?? $this->currentSchoolYearName(); if ($schoolYear !== '') { $row['school_year'] = $schoolYear; } return $row; } protected function deriveSchoolYearForRow(array $row): ?string { foreach (['school_year_from', 'school_year_to', 'previous_school_year'] as $field) { $value = trim((string)($row[$field] ?? '')); if ($this->isValidSchoolYear($value)) { return $value; } } foreach (['source_school_year_id', 'target_school_year_id', 'school_year_id'] as $field) { $id = (int)($row[$field] ?? 0); if ($id <= 0) { continue; } $year = $this->schoolYearNameById($id); if ($year !== null) { return $year; } } $batchId = (int)($row['closing_batch_id'] ?? 0); if ($batchId > 0) { return $this->schoolYearNameByClosingBatchId($batchId); } return null; } private function hasNonEmptySchoolYear(array $row): bool { return array_key_exists('school_year', $row) && trim((string)$row['school_year']) !== ''; } private function currentSchoolYearName(): string { try { if ($this->db->tableExists('school_years')) { $row = $this->db->table('school_years') ->select('name') ->where('status', 'active') ->orderBy('id', 'DESC') ->get(1) ->getRowArray(); $name = trim((string)($row['name'] ?? '')); if ($this->isValidSchoolYear($name)) { return $name; } } if ($this->db->tableExists('configuration')) { foreach (['school_year', 'current_school_year'] as $key) { $row = $this->db->table('configuration') ->select('config_value') ->where('config_key', $key) ->orderBy('id', 'DESC') ->get(1) ->getRowArray(); $value = trim((string)($row['config_value'] ?? '')); if ($this->isValidSchoolYear($value)) { return $value; } } } } catch (\Throwable) { return $this->calendarYearFallback(); } return $this->calendarYearFallback(); } private function schoolYearNameById(int $id): ?string { try { if (! $this->db->tableExists('school_years')) { return null; } $row = $this->db->table('school_years') ->select('name') ->where('id', $id) ->get(1) ->getRowArray(); $name = trim((string)($row['name'] ?? '')); return $this->isValidSchoolYear($name) ? $name : null; } catch (\Throwable) { return null; } } private function schoolYearNameByClosingBatchId(int $id): ?string { try { if (! $this->db->tableExists('school_year_closing_batches')) { return null; } $row = $this->db->table('school_year_closing_batches') ->select('school_year') ->where('id', $id) ->get(1) ->getRowArray(); $year = trim((string)($row['school_year'] ?? '')); return $this->isValidSchoolYear($year) ? $year : null; } catch (\Throwable) { return null; } } private function calendarYearFallback(): string { $year = (int) date('Y'); return $year . '-' . ($year + 1); } private function isValidSchoolYear(string $value): bool { return preg_match('/^\d{4}-\d{4}$/', $value) === 1; } }