prepare("SELECT * FROM company_settings WHERE id = 1"); $stmt->execute(); $company = $stmt->fetch(); // 2. Determine Fiscal Year Range $fiscal_end_raw = $company['fiscal_year_end'] ?? '12-31'; $fiscal_month = (int)date('m', strtotime($fiscal_end_raw)); $fiscal_day = (int)date('d', strtotime($fiscal_end_raw)); // If fiscal end is 2024-03-31, and selected year is 2024: // Start: 2023-04-01, End: 2024-03-31 $end_date = sprintf('%d-%02d-%02d', $selected_year, $fiscal_month, $fiscal_day); $start_date = date('Y-m-d', strtotime($end_date . ' -1 year +1 day')); // 3. Fetch Projects active in this period (with labour or expenses) $projects_query = " SELECT DISTINCT p.* FROM projects p LEFT JOIN labour_entries le ON p.id = le.project_id AND le.entry_date BETWEEN ? AND ? LEFT JOIN expenses e ON p.id = e.project_id AND e.entry_date BETWEEN ? AND ? WHERE (le.id IS NOT NULL OR e.id IS NOT NULL) ORDER BY p.name ASC "; $stmt = db()->prepare($projects_query); $stmt->execute([$start_date, $end_date, $start_date, $end_date]); $active_projects = $stmt->fetchAll(); // 4. Helper for Labour Data function getLabourForProject($projectId, $start, $end) { $stmt = db()->prepare(" SELECT le.*, e.name as employee_name, lt.name as labour_type FROM labour_entries le JOIN employees e ON le.employee_id = e.id LEFT JOIN labour_types lt ON le.labour_type_id = lt.id WHERE le.project_id = ? AND le.entry_date BETWEEN ? AND ? ORDER BY le.entry_date ASC "); $stmt->execute([$projectId, $start, $end]); return $stmt->fetchAll(); } // 5. Helper for Expense Data function getExpensesForProject($projectId, $start, $end) { $stmt = db()->prepare(" SELECT e.*, s.name as supplier_name, et.name as expense_type FROM expenses e JOIN suppliers s ON e.supplier_id = s.id LEFT JOIN expense_types et ON e.expense_type_id = et.id WHERE e.project_id = ? AND e.entry_date BETWEEN ? AND ? ORDER BY e.entry_date ASC "); $stmt->execute([$projectId, $start, $end]); return $stmt->fetchAll(); } // 6. Helper for Summary Calendar (Labour Hours) $summary_labour_query = " SELECT p.id as project_id, p.name as project_name, DATE_FORMAT(le.entry_date, '%Y-%m') as month, SUM(le.hours) as total_hours, SUM(le.hours * IFNULL(le.hourly_rate, 0)) as total_cost FROM labour_entries le JOIN projects p ON le.project_id = p.id WHERE le.entry_date BETWEEN ? AND ? GROUP BY p.id, p.name, month "; $stmt = db()->prepare($summary_labour_query); $stmt->execute([$start_date, $end_date]); $summary_labour_data = $stmt->fetchAll(); // 7. Helper for Summary Calendar (Expenses) $summary_expense_query = " SELECT p.id as project_id, p.name as project_name, DATE_FORMAT(e.entry_date, '%Y-%m') as month, SUM(e.amount) as total_amount FROM expenses e JOIN projects p ON e.project_id = p.id WHERE e.entry_date BETWEEN ? AND ? GROUP BY p.id, p.name, month "; $stmt = db()->prepare($summary_expense_query); $stmt->execute([$start_date, $end_date]); $summary_expense_data = $stmt->fetchAll(); // Organize Summary Data $months = []; try { $current_dt = new DateTime($start_date); $current_dt->modify('first day of this month'); $end_dt = new DateTime($end_date); $end_dt->modify('first day of this month'); while ($current_dt <= $end_dt) { $months[] = $current_dt->format('Y-m'); $current_dt->modify('+1 month'); if (count($months) > 13) break; // Safety for roughly 1 year } } catch (Exception $e) { // Fallback if DateTime fails $months = [date('Y-m', strtotime($start_date))]; } $labour_matrix = []; $project_names = []; foreach ($summary_labour_data as $row) { $pid = $row['project_id']; $project_names[$pid] = $row['project_name']; $labour_matrix[$pid][$row['month']] = ['hours' => $row['total_hours'], 'cost' => $row['total_cost']]; } $expense_matrix = []; foreach ($summary_expense_data as $row) { $pid = $row['project_id']; $project_names[$pid] = $row['project_name']; $expense_matrix[$pid][$row['month']] = $row['total_amount']; } $pageTitle = "SRED Claim Report - " . $selected_year; ?>
Range: = date('M d, Y', strtotime($start_date)) ?> to = date('M d, Y', strtotime($end_date)) ?>
= htmlspecialchars($company['address_1'] ?? '') ?>
= htmlspecialchars($company['address_2']) ?>
= htmlspecialchars($company['city'] ?? '') ?>, = htmlspecialchars($company['province'] ?? '') ?> = htmlspecialchars($company['postal_code'] ?? '') ?>
Phone: = htmlspecialchars($company['phone'] ?? '') ?> | Email: = htmlspecialchars($company['email'] ?? '') ?>
Website: = htmlspecialchars($company['website'] ?? '') ?>
Business Number: = htmlspecialchars($company['business_number']) ?>
= nl2br(htmlspecialchars($project['description'] ?? 'No project description available.')) ?>
| Date | Employee | Activity Type | Hours | Rate | Total Wage |
|---|---|---|---|---|---|
| No labour entries recorded. | |||||
| = $l['entry_date'] ?> | = htmlspecialchars($l['employee_name']) ?> | = htmlspecialchars($l['labour_type'] ?? 'N/A') ?> | = number_format((float)$l['hours'], 2) ?> | = $l['hourly_rate'] ? '$'.number_format((float)$l['hourly_rate'], 2) : '-' ?> | = $cost > 0 ? '$'.number_format($cost, 2) : '-' ?> |
| Project Totals: | = number_format($total_proj_hours, 2) ?> h | $= number_format($total_proj_labour_cost, 2) ?> | |||
| Date | Supplier | Expense Type | Notes | Allocation | Amount |
|---|---|---|---|---|---|
| No expenses recorded. | |||||
| = $e['entry_date'] ?> | = htmlspecialchars($e['supplier_name']) ?> | = htmlspecialchars($e['expense_type'] ?? 'N/A') ?> | = htmlspecialchars($e['notes'] ?? '') ?> | = number_format((float)$e['allocation_percent'], 0) ?>% | $= number_format((float)$e['amount'], 2) ?> |
| Total Expenses: | $= number_format($total_proj_expenses, 2) ?> | ||||
| Project Name | = date('M y', strtotime($m)) ?> | Total $ |
|---|---|---|
| = htmlspecialchars($pname) ?> | = $h > 0 ? number_format($h, 1) : '-' ?> | $= number_format($row_cost, 0) ?> |
| Monthly Totals | $d) $m_hours += (float)($d[$m]['hours'] ?? 0); ?>= $m_hours > 0 ? number_format($m_hours, 1) : '-' ?> | $= number_format($grand_total_cost, 2) ?> |
All amounts are calculated using the recorded hourly rate at the time of labour entry.
| Project Name | = date('M y', strtotime($m)) ?> | Total $ |
|---|---|---|
| = htmlspecialchars($pname) ?> | = $a > 0 ? '$'.number_format($a, 0) : '-' ?> | $= number_format($row_amount, 0) ?> |
| Monthly Totals | $d) $m_amt += (float)($d[$m] ?? 0); ?>= $m_amt > 0 ? '$'.number_format($m_amt, 0) : '-' ?> | $= number_format($grand_total_expense, 2) ?> |
Includes all Labour Wages and Project Expenses for Fiscal Year = $selected_year ?>