# End of Year Processing Guide

## Overview

This system now includes comprehensive end-of-year processing functionality to manage students and attendance records when transitioning between school years.

## Features

### 1. School Year Management
- Create and manage multiple school years
- Set active school year
- Edit and delete school years (cannot delete active school year)

### 2. End-of-Year Processing
When processing the end of a school year, the system will:

1. **Archive Attendance Records**
   - All attendance records from the old school year are moved to `archived_attendance` table
   - Original attendance records are deleted from the active `attendance` table

2. **Archive Students**
   - All student records from the old school year are copied to `archived_students` table
   - Original student records are marked as 'archived' status

3. **Promote Students**
   - Students are automatically promoted to the next grade level
   - Grade levels are incremented (e.g., Grade 1 → Grade 2, Grade 2 → Grade 3, etc.)
   - Students are assigned to the new school year

4. **Graduate Students**
   - Students in Grade 6 (or highest grade) are marked as 'graduated' instead of being promoted
   - Graduated students remain in the system but are no longer active

5. **Activate New School Year**
   - The new school year is automatically set as active
   - The old school year is deactivated

## Database Changes

The following tables are automatically created/updated:

### New Tables
- `school_years` - Stores school year information
- `archived_students` - Stores archived student records
- `archived_attendance` - Stores archived attendance records

### Updated Tables
- `students` - Added `school_year` and `status` columns
- `attendance` - Added `school_year` column

## How to Use

### Accessing School Year Management
1. Log in as Administrator (not available for advisers)
2. Click on "School Years" in the sidebar navigation
3. You'll see two sections:
   - **School Year Management** - Create and manage school years
   - **End of Year Processing** - Process the end of a school year

### Creating a New School Year
1. Click "Add School Year" button
2. Fill in the form:
   - **School Year**: Format like "2024-25" or "2024-2025"
   - **Start Date**: When the school year begins
   - **End Date**: When the school year ends
   - **Set as Active**: Check this to make it the active school year
3. Click "Save"

### Processing End of Year
**⚠️ WARNING: This action cannot be undone!**

1. Ensure you have created the new school year first
2. In the "End of Year Processing" section:
   - Select the **Current School Year** (the one ending)
   - Select the **New School Year** (the one starting)
3. Click "Process End of Year"
4. Confirm the action (you'll be asked twice for safety)
5. The system will:
   - Archive all attendance and students from the old year
   - Promote students to the next grade
   - Mark graduating students
   - Activate the new school year

## What Happens to Students

### Active Students
- Students are automatically assigned to the active school year when created
- Only active students are shown in the student list by default

### After End-of-Year Processing
- **Promoted Students**: Moved to the new school year with incremented grade level
- **Graduated Students**: Marked as 'graduated' status (Grade 6 or highest grade)
- **Archived Students**: Original records are archived but remain in the system for historical reference

## Student Status Values

- `active` - Currently enrolled students
- `archived` - Students from previous school years
- `graduated` - Students who have completed the highest grade

## Attendance Records

- Active attendance records are automatically tagged with the current school year
- When processing end of year, attendance records are archived
- You can still access archived attendance data if needed

## Best Practices

1. **Backup First**: Always backup your database before processing end of year
2. **Create New Year First**: Create the new school year before processing end of year
3. **Verify Data**: After processing, verify that students were promoted correctly
4. **Review Graduates**: Check the graduated students list to ensure accuracy
5. **Test First**: Consider testing with a small subset of data first

## API Endpoints

### School Years API (`api/school_years.php`)
- `GET` - List all school years
- `POST` - Create new school year
- `PUT` - Update school year
- `DELETE` - Delete school year (cannot delete active)

### End of Year API (`api/end_of_year.php`)
- `POST` with action `process_end_of_year` - Complete end-of-year processing
- `POST` with action `archive_students` - Archive students only
- `POST` with action `archive_attendance` - Archive attendance only
- `POST` with action `promote_students` - Promote students only

## Notes

- The system automatically detects grade levels from the course field (e.g., "Grade 1", "Grade 2")
- If a student's course doesn't match the grade pattern, they won't be automatically promoted
- You can manually update student records if needed after processing
- Archived data is preserved for historical reporting and records

## Troubleshooting

### Students not showing up
- Check if they have the correct school year assigned
- Verify their status is 'active'
- Ensure the active school year is set correctly

### Promotion not working
- Verify course names follow the pattern "Grade X" (e.g., "Grade 1", "Grade 2")
- Check that both old and new school years are selected
- Review the processing results message for details

### Cannot delete school year
- You cannot delete the active school year
- First, set another school year as active, then delete the old one

