Mini Kabibi Habibi

Current Path : C:/xampp/htdocs/cid/system/
Upload File :
Current File : C:/xampp/htdocs/cid/system/generate_monthly_report.php

<?php
session_start();
@include 'include/config.php';
require_once 'vendor/autoload.php';

use PhpOffice\PhpWord\TemplateProcessor;

if (!isset($_SESSION['username_school']) || empty($_SESSION['username_school'])) {
    header('Location: login.php');
    exit();
}

$username = $_SESSION['username_school'];

// ✅ GET month and year from POST form
$month = isset($_POST['month']) ? (int)$_POST['month'] : date('m');
$year = isset($_POST['year']) ? (int)$_POST['year'] : date('Y');

$fullname = 'N/A';
$section = 'N/A';
$designation = 'N/A';
$supervisor = 'N/A';
$activities = [];

// Get user info
$user_stmt = $conn->prepare("SELECT fullname, section, designation, immediate_supervisor FROM information WHERE username = ?");
$user_stmt->bind_param("s", $username);
$user_stmt->execute();
$user_result = $user_stmt->get_result();
if ($user_result->num_rows > 0) {
    $row = $user_result->fetch_assoc();
    $fullname = $row['fullname'];
    $section = $row['section'];
    $designation = $row['designation'];
    $supervisor = $row['immediate_supervisor'];
}

// Get selected activity IDs if provided
$selected_ids = isset($_POST['selected_ids']) ? $_POST['selected_ids'] : [];

if (!empty($selected_ids)) {
    // Query only selected entries
    $placeholders = implode(',', array_fill(0, count($selected_ids), '?'));
    $types = str_repeat('i', count($selected_ids)); // all IDs are integers
    $params = array_map('intval', $selected_ids);

    $query = "SELECT date, activities, other_activity FROM schedule WHERE id IN ($placeholders) AND username = ? AND MONTH(date) = ? AND YEAR(date) = ?";
    $types .= "sii"; // add username, month, year types
    $params = array_merge($params, [$username, $month, $year]);

    $stmt = $conn->prepare($query);
    $stmt->bind_param($types, ...$params);
} else {
    // Query all for the selected month/year
    $stmt = $conn->prepare("SELECT date, activities, other_activity FROM schedule WHERE username = ? AND MONTH(date) = ? AND YEAR(date) = ?");
    $stmt->bind_param("sii", $username, $month, $year);
}

$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $activity = $row['activities'];
        if ($activity === 'Others' && !empty($row['other_activity'])) {
            $activity = "Other Activity: " . $row['other_activity'];
        }
        $activities[] = [
            'date' => date('F d, Y', strtotime($row['date'])),
            'activity' => $activity
        ];
    }
} else {
    $activities[] = [
        'date' => 'N/A',
        'activity' => 'No activities recorded.'
    ];
}

// Load template
$templatePath = __DIR__ . '/templates/monthly_accomplishment_report.docx';
if (!file_exists($templatePath)) {
    die("Template not found at $templatePath");
}

$templateProcessor = new TemplateProcessor($templatePath);

// Fill in basic info
$templateProcessor->setValue('fullname', strtoupper($fullname));
$templateProcessor->setValue('designation', $designation);
$templateProcessor->setValue('section', $section);
$templateProcessor->setValue('month', date('F', mktime(0, 0, 0, $month, 10)));
$templateProcessor->setValue('year', $year);
$templateProcessor->setValue('immediate_supervisor', strtoupper($supervisor));

$currentDate = date('F d, Y');
$templateProcessor->setValue('employee_date', $currentDate);
$templateProcessor->setValue('supervisor_date', $currentDate);

// Fill activity rows
$templateProcessor->cloneRowAndSetValues('date', $activities);

// Save file and trigger download
$filename = 'monthly_report_' . $username . '_' . $month . '_' . $year . '.docx';
$outputPath = __DIR__ . '/generated_reports/' . $filename;
$templateProcessor->saveAs($outputPath);

header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . filesize($outputPath));
readfile($outputPath);
exit();
?>