mirror of
https://github.com/marcogll/AnchorOS.git
synced 2026-03-15 16:24:30 +00:00
- Add KiosksManagement component with full CRUD for kiosks - Add ScheduleManagement for staff schedules with break reminders - Update booking flow to allow artist selection by customers - Add staff_services API for assigning services to artists - Update staff management UI with service assignment dialog - Add auto-break reminder when schedule >= 8 hours - Update availability API to filter artists by service - Add kiosk management to Aperture dashboard - Clean up ralphy artifacts and logs
113 lines
4.5 KiB
TypeScript
113 lines
4.5 KiB
TypeScript
/**
|
|
* @description Retrieves payroll calculations for staff including base salary, commissions, tips, and hours worked
|
|
* @param {NextRequest} request - HTTP request with query parameters: staff_id, period_start (default 2026-01-01), period_end (default 2026-01-31), action (optional 'calculate')
|
|
* @returns {NextResponse} JSON with success status and payroll data including earnings breakdown
|
|
* @example GET /api/aperture/payroll?staff_id=...&period_start=2026-01-01&period_end=2026-01-31&action=calculate
|
|
* @audit BUSINESS RULE: Calculates payroll based on completed bookings within the specified period
|
|
* @audit BUSINESS RULE: Commission is 10% of service revenue, tips are 5% of service revenue
|
|
* @audit SECURITY: Requires authenticated admin/manager role via middleware
|
|
* @audit Validate: Ensures staff member exists and has completed bookings in the period
|
|
* @audit PERFORMANCE: Computes hours worked from booking start/end times
|
|
* @audit AUDIT: Payroll calculations logged for financial compliance and transparency
|
|
*/
|
|
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { supabaseAdmin } from '@/lib/supabase/admin'
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const { searchParams } = new URL(request.url)
|
|
const staffId = searchParams.get('staff_id')
|
|
const periodStart = searchParams.get('period_start') || '2026-01-01'
|
|
const periodEnd = searchParams.get('period_end') || '2026-01-31'
|
|
const action = searchParams.get('action')
|
|
|
|
if (action === 'calculate' && staffId) {
|
|
// Get staff details
|
|
const { data: staff, error: staffError } = await supabaseAdmin
|
|
.from('staff')
|
|
.select('id, display_name, role')
|
|
.eq('id', staffId)
|
|
.single()
|
|
|
|
if (staffError || !staff) {
|
|
console.log('Staff lookup error:', staffError)
|
|
return NextResponse.json(
|
|
{ error: 'Staff member not found', debug: { staffId, error: staffError?.message } },
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
// Set default base salary (since column doesn't exist yet)
|
|
;(staff as any).base_salary = 8000 // Default salary
|
|
|
|
// Calculate service commissions from completed bookings
|
|
const { data: bookings } = await supabaseAdmin
|
|
.from('bookings')
|
|
.select('total_amount, start_time_utc, end_time_utc')
|
|
.eq('staff_id', staffId)
|
|
.eq('status', 'completed')
|
|
.gte('end_time_utc', `${periodStart}T00:00:00Z`)
|
|
.lte('end_time_utc', `${periodEnd}T23:59:59Z`)
|
|
|
|
// Simple commission calculation (10% of service revenue)
|
|
const serviceRevenue = bookings?.reduce((sum: number, b: any) => sum + b.total_amount, 0) || 0
|
|
const serviceCommissions = serviceRevenue * 0.1
|
|
|
|
// Calculate hours worked from bookings
|
|
const hoursWorked = bookings?.reduce((total: number, booking: any) => {
|
|
const start = new Date(booking.start_time_utc)
|
|
const end = new Date(booking.end_time_utc)
|
|
const hours = (end.getTime() - start.getTime()) / (1000 * 60 * 60)
|
|
return total + hours
|
|
}, 0) || 0
|
|
|
|
// Get tips (simplified - assume some percentage of revenue)
|
|
const totalTips = serviceRevenue * 0.05
|
|
|
|
const baseSalary = (staff as any).base_salary || 0
|
|
const totalEarnings = baseSalary + serviceCommissions + totalTips
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
staff,
|
|
payroll: {
|
|
base_salary: baseSalary,
|
|
service_commissions: serviceCommissions,
|
|
total_tips: totalTips,
|
|
total_earnings: totalEarnings,
|
|
hours_worked: hoursWorked
|
|
}
|
|
})
|
|
}
|
|
|
|
// Default response - list all staff payroll summaries
|
|
const { data: allStaff } = await supabaseAdmin
|
|
.from('staff')
|
|
.select('id, display_name, role, base_salary')
|
|
.eq('is_active', true)
|
|
|
|
const payrollSummaries = allStaff?.map(staff => ({
|
|
id: `summary-${staff.id}`,
|
|
staff_id: staff.id,
|
|
staff_name: staff.display_name,
|
|
role: staff.role,
|
|
base_salary: staff.base_salary || 0,
|
|
period_start: periodStart,
|
|
period_end: periodEnd,
|
|
status: 'ready_for_calculation'
|
|
})) || []
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: 'Payroll summaries ready - use action=calculate with staff_id for detailed calculations',
|
|
payroll_summaries: payrollSummaries
|
|
})
|
|
} catch (error) {
|
|
console.error('Payroll API error:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Internal server error' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
} |