0 && TenantScopeService::hasGlobalAccess($tenantUserId)) { unset($options['tenantUserId'], $options['tenantIds']); } } return DepartmentRepository::listPaged($options); } public static function listByTenantIds(array $tenantIds): array { return DepartmentRepository::listByTenantIds($tenantIds); } public static function listByIds(array $departmentIds): array { return DepartmentRepository::listByIds($departmentIds); } public static function listForUserAssignments(array $tenantIds, array $selectedDepartmentIds): array { $departments = $tenantIds ? self::listByTenantIds($tenantIds) : self::list(); $extraDepartments = $selectedDepartmentIds ? self::listByIds($selectedDepartmentIds) : []; if (!$extraDepartments) { return $departments; } $departmentMap = []; foreach ($departments as $department) { if (isset($department['id'])) { $departmentMap[(int) $department['id']] = $department; } } foreach ($extraDepartments as $department) { if (isset($department['id'])) { $departmentMap[(int) $department['id']] = $department; } } return array_values($departmentMap); } public static function findByUuid(string $uuid): ?array { return DepartmentRepository::findByUuid($uuid); } public static function findById(int $id): ?array { return DepartmentRepository::find($id); } public static function createFromAdmin(array $input, int $currentUserId = 0): array { $form = self::sanitizeBase($input); $errors = self::validateBase($form); $warnings = self::warningsForCode($form, 0); if ($errors) { return ['ok' => false, 'errors' => $errors, 'warnings' => $warnings, 'form' => $form]; } $createdId = DepartmentRepository::create([ 'description' => $form['description'], 'code' => $form['code'] ?: null, 'cost_center' => $form['cost_center'] ?: null, 'active' => $form['active'], 'created_by' => $currentUserId > 0 ? $currentUserId : null, ]); if (!$createdId) { return ['ok' => false, 'errors' => [t('Department can not be created')], 'form' => $form]; } $createdDepartment = DepartmentRepository::find((int) $createdId); $uuid = $createdDepartment['uuid'] ?? null; if (!empty($input['is_default']) && $createdId) { SettingService::setDefaultDepartmentId((int) $createdId); } return ['ok' => true, 'form' => $form, 'warnings' => $warnings, 'uuid' => $uuid, 'id' => (int) $createdId]; } public static function updateFromAdmin(int $departmentId, array $input, int $currentUserId = 0): array { $form = self::sanitizeBase($input); $errors = self::validateBase($form); $warnings = self::warningsForCode($form, $departmentId); if ($errors) { return ['ok' => false, 'errors' => $errors, 'warnings' => $warnings, 'form' => $form]; } $updated = DepartmentRepository::update($departmentId, [ 'description' => $form['description'], 'code' => $form['code'] ?: null, 'cost_center' => $form['cost_center'] ?: null, 'active' => $form['active'], 'modified_by' => $currentUserId > 0 ? $currentUserId : null, ]); if (!$updated) { return ['ok' => false, 'errors' => [t('Department can not be updated')], 'form' => $form]; } return ['ok' => true, 'form' => $form, 'warnings' => $warnings]; } public static function syncTenants(int $departmentId, array $tenantIds): int { $updated = TenantDepartmentRepository::replaceForDepartment($departmentId, $tenantIds); if (!$updated) { return -1; } return UserDepartmentRepository::removeInvalidForDepartment($departmentId); } public static function deleteByUuid(string $uuid): array { $uuid = trim($uuid); if ($uuid === '') { return ['ok' => false, 'status' => 404, 'error' => 'not_found']; } $department = DepartmentRepository::findByUuid($uuid); if (!$department || !isset($department['id'])) { return ['ok' => false, 'status' => 404, 'error' => 'not_found']; } $deleted = DepartmentRepository::delete((int) $department['id']); if (!$deleted) { return ['ok' => false, 'status' => 500, 'error' => 'delete_failed']; } return ['ok' => true, 'department' => $department]; } private static function sanitizeBase(array $input): array { return [ 'description' => trim((string) ($input['description'] ?? '')), 'code' => trim((string) ($input['code'] ?? '')), 'cost_center' => trim((string) ($input['cost_center'] ?? '')), 'active' => self::normalizeActive($input['active'] ?? 1), ]; } private static function validateBase(array $form): array { $errors = []; if ($form['description'] === '') { $errors[] = t('Description cannot be empty'); } return $errors; } private static function warningsForCode(array $form, int $excludeId): array { $warnings = []; $code = $form['code'] ?? ''; if ($code !== '' && DepartmentRepository::existsByCode($code, $excludeId)) { $warnings[] = t('Department code already exists'); } return $warnings; } private static function normalizeActive($value): int { $value = strtolower(trim((string) $value)); if (in_array($value, ['0', 'false', 'inactive'], true)) { return 0; } return 1; } }