Files
usher-manage-stack/app/Http/Controllers/AdminMemberController.php
Gbanyan 642b879dd4 Add membership fee system with disability discount and fix document permissions
Features:
- Implement two fee types: entrance fee and annual fee (both NT$1,000)
- Add 50% discount for disability certificate holders
- Add disability certificate upload in member profile
- Integrate disability verification into cashier approval workflow
- Add membership fee settings in system admin

Document permissions:
- Fix hard-coded role logic in Document model
- Use permission-based authorization instead of role checks

Additional features:
- Add announcements, general ledger, and trial balance modules
- Add income management and accounting entries
- Add comprehensive test suite with factories
- Update UI translations to Traditional Chinese

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-01 09:56:01 +08:00

397 lines
14 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Member;
use App\Support\AuditLogger;
use Spatie\Permission\Models\Role;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
class AdminMemberController extends Controller
{
public function index(Request $request)
{
$query = Member::query()->with('user');
// Text search (name, email, phone, national ID)
if ($search = $request->string('search')->toString()) {
$query->where(function ($q) use ($search) {
$q->where('full_name', 'like', "%{$search}%")
->orWhere('email', 'like', "%{$search}%")
->orWhere('phone', 'like', "%{$search}%");
// Search by national ID hash if provided
if (!empty($search)) {
$q->orWhere('national_id_hash', hash('sha256', $search));
}
});
}
// Membership status filter
if ($status = $request->string('status')->toString()) {
if ($status === 'active') {
$query->whereDate('membership_expires_at', '>=', now()->toDateString());
} elseif ($status === 'expired') {
$query->where(function ($q) {
$q->whereNull('membership_expires_at')
->orWhereDate('membership_expires_at', '<', now()->toDateString());
});
} elseif ($status === 'expiring_soon') {
$query->whereBetween('membership_expires_at', [
now()->toDateString(),
now()->addDays(30)->toDateString()
]);
}
}
// Date range filters
if ($startedFrom = $request->string('started_from')->toString()) {
$query->whereDate('membership_started_at', '>=', $startedFrom);
}
if ($startedTo = $request->string('started_to')->toString()) {
$query->whereDate('membership_started_at', '<=', $startedTo);
}
// Payment status filter
if ($paymentStatus = $request->string('payment_status')->toString()) {
if ($paymentStatus === 'has_payments') {
$query->whereHas('payments');
} elseif ($paymentStatus === 'no_payments') {
$query->whereDoesntHave('payments');
}
}
$members = $query->orderBy('full_name')->paginate(15)->withQueryString();
return view('admin.members.index', [
'members' => $members,
'filters' => $request->only(['search', 'status', 'started_from', 'started_to', 'payment_status']),
]);
}
public function show(Member $member)
{
$member->load('user.roles', 'payments');
$roles = Role::orderBy('name')->get();
return view('admin.members.show', [
'member' => $member,
'roles' => $roles,
]);
}
public function create()
{
return view('admin.members.create');
}
public function store(Request $request)
{
$validated = $request->validate([
'full_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'max:255', 'unique:users,email'],
'national_id' => ['nullable', 'string', 'max:50'],
'phone' => ['nullable', 'string', 'max:50'],
'address_line_1' => ['nullable', 'string', 'max:255'],
'address_line_2' => ['nullable', 'string', 'max:255'],
'city' => ['nullable', 'string', 'max:120'],
'postal_code' => ['nullable', 'string', 'max:20'],
'emergency_contact_name' => ['nullable', 'string', 'max:255'],
'emergency_contact_phone' => ['nullable', 'string', 'max:50'],
'membership_started_at' => ['nullable', 'date'],
'membership_expires_at' => ['nullable', 'date', 'after_or_equal:membership_started_at'],
]);
// Create user account
$user = \App\Models\User::create([
'name' => $validated['full_name'],
'email' => $validated['email'],
'password' => \Illuminate\Support\Str::random(32),
]);
// Create member record
$member = Member::create(array_merge($validated, [
'user_id' => $user->id,
]));
// Send activation email
$token = \Illuminate\Support\Facades\Password::createToken($user);
\Illuminate\Support\Facades\Mail::to($user)->queue(new \App\Mail\MemberActivationMail($user, $token));
// Log the action
AuditLogger::log('member.created', $member, $validated);
AuditLogger::log('user.activation_link_sent', $user, ['email' => $user->email]);
return redirect()
->route('admin.members.show', $member)
->with('status', __('Member created successfully. Activation email has been sent.'));
}
public function edit(Member $member)
{
return view('admin.members.edit', [
'member' => $member,
]);
}
public function update(Request $request, Member $member)
{
$validated = $request->validate([
'full_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'max:255'],
'national_id' => ['nullable', 'string', 'max:50'],
'phone' => ['nullable', 'string', 'max:50'],
'address_line_1' => ['nullable', 'string', 'max:255'],
'address_line_2' => ['nullable', 'string', 'max:255'],
'city' => ['nullable', 'string', 'max:120'],
'postal_code' => ['nullable', 'string', 'max:20'],
'emergency_contact_name' => ['nullable', 'string', 'max:255'],
'emergency_contact_phone' => ['nullable', 'string', 'max:50'],
'membership_started_at' => ['nullable', 'date'],
'membership_expires_at' => ['nullable', 'date', 'after_or_equal:membership_started_at'],
]);
$member->update($validated);
AuditLogger::log('member.updated', $member, $validated);
return redirect()
->route('admin.members.show', $member)
->with('status', __('Member updated successfully.'));
}
public function importForm()
{
return view('admin.members.import');
}
public function import(Request $request)
{
$validated = $request->validate([
'file' => ['required', 'file', 'mimes:csv,txt'],
]);
$path = $validated['file']->store('imports');
$fullPath = storage_path('app/'.$path);
Artisan::call('members:import', ['path' => $fullPath]);
$output = Artisan::output();
AuditLogger::log('members.imported', null, [
'path' => $fullPath,
'output' => $output,
]);
return redirect()
->route('admin.members.index')
->with('status', __('Import completed.')."\n".$output);
}
public function updateRoles(Request $request, Member $member)
{
$user = $member->user;
if (! $user) {
abort(400, 'Member is not linked to a user.');
}
$validated = $request->validate([
'roles' => ['nullable', 'array'],
'roles.*' => ['exists:roles,name'],
]);
$roleNames = $validated['roles'] ?? [];
$user->syncRoles($roleNames);
AuditLogger::log('member.roles_updated', $member, ['roles' => $roleNames]);
return redirect()->route('admin.members.show', $member)->with('status', __('Roles updated.'));
}
/**
* Show membership activation form
*/
public function showActivate(Member $member)
{
// Check if user has permission
if (!auth()->user()->can('activate_memberships') && !auth()->user()->hasRole('admin')) {
abort(403, 'You do not have permission to activate memberships.');
}
// Check if member has fully approved payment
$approvedPayment = $member->payments()
->where('status', \App\Models\MembershipPayment::STATUS_APPROVED_CHAIR)
->latest()
->first();
if (!$approvedPayment && !auth()->user()->hasRole('admin')) {
return redirect()->route('admin.members.show', $member)
->with('error', __('Member must have an approved payment before activation.'));
}
return view('admin.members.activate', compact('member', 'approvedPayment'));
}
/**
* Activate membership
*/
public function activate(Request $request, Member $member)
{
// Check if user has permission
if (!auth()->user()->can('activate_memberships') && !auth()->user()->hasRole('admin')) {
abort(403, 'You do not have permission to activate memberships.');
}
$validated = $request->validate([
'membership_started_at' => ['required', 'date'],
'membership_expires_at' => ['required', 'date', 'after:membership_started_at'],
'membership_type' => ['required', 'in:regular,honorary,lifetime,student'],
]);
// Update member
$member->update([
'membership_started_at' => $validated['membership_started_at'],
'membership_expires_at' => $validated['membership_expires_at'],
'membership_type' => $validated['membership_type'],
'membership_status' => Member::STATUS_ACTIVE,
]);
AuditLogger::log('member.activated', $member, [
'started_at' => $validated['membership_started_at'],
'expires_at' => $validated['membership_expires_at'],
'type' => $validated['membership_type'],
'activated_by' => auth()->id(),
]);
// Send activation confirmation email
\Illuminate\Support\Facades\Mail::to($member->email)
->queue(new \App\Mail\MembershipActivatedMail($member));
return redirect()->route('admin.members.show', $member)
->with('status', __('Membership activated successfully! Member has been notified.'));
}
public function export(Request $request): StreamedResponse
{
$query = Member::query()->with('user');
if ($search = $request->string('search')->toString()) {
$query->where(function ($q) use ($search) {
$q->where('full_name', 'like', "%{$search}%")
->orWhere('email', 'like', "%{$search}%");
});
}
if ($status = $request->string('status')->toString()) {
if ($status === 'active') {
$query->whereDate('membership_expires_at', '>=', now()->toDateString());
} elseif ($status === 'expired') {
$query->where(function ($q) {
$q->whereNull('membership_expires_at')
->orWhereDate('membership_expires_at', '<', now()->toDateString());
});
}
}
$headers = [
'ID',
'Full Name',
'Email',
'Phone',
'Address Line 1',
'Address Line 2',
'City',
'Postal Code',
'Emergency Contact Name',
'Emergency Contact Phone',
'Membership Start',
'Membership Expiry',
];
$response = new StreamedResponse(function () use ($query, $headers) {
$handle = fopen('php://output', 'w');
fputcsv($handle, $headers);
$query->chunk(500, function ($members) use ($handle) {
foreach ($members as $member) {
fputcsv($handle, [
$member->id,
$member->full_name,
$member->email,
$member->phone,
$member->address_line_1,
$member->address_line_2,
$member->city,
$member->postal_code,
$member->emergency_contact_name,
$member->emergency_contact_phone,
optional($member->membership_started_at)->toDateString(),
optional($member->membership_expires_at)->toDateString(),
]);
}
});
fclose($handle);
});
$filename = 'members-export-'.now()->format('Ymd_His').'.csv';
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set('Content-Disposition', "attachment; filename=\"{$filename}\"");
return $response;
}
public function batchDestroy(Request $request)
{
$validated = $request->validate([
'ids' => ['required', 'array'],
'ids.*' => ['exists:members,id'],
]);
$count = Member::whereIn('id', $validated['ids'])->delete();
AuditLogger::log('members.batch_deleted', null, ['ids' => $validated['ids'], 'count' => $count]);
return back()->with('status', __(':count members deleted successfully.', ['count' => $count]));
}
public function batchUpdateStatus(Request $request)
{
$validated = $request->validate([
'ids' => ['required', 'array'],
'ids.*' => ['exists:members,id'],
'status' => ['required', 'in:pending,active,expired,suspended'],
]);
$count = Member::whereIn('id', $validated['ids'])->update(['membership_status' => $validated['status']]);
AuditLogger::log('members.batch_status_updated', null, [
'ids' => $validated['ids'],
'status' => $validated['status'],
'count' => $count
]);
return back()->with('status', __(':count members updated successfully.', ['count' => $count]));
}
/**
* View member's disability certificate
*/
public function viewDisabilityCertificate(Member $member)
{
if (!$member->disability_certificate_path) {
abort(404, '找不到身心障礙手冊');
}
if (!\Illuminate\Support\Facades\Storage::disk('private')->exists($member->disability_certificate_path)) {
abort(404, '檔案不存在');
}
return \Illuminate\Support\Facades\Storage::disk('private')->response($member->disability_certificate_path);
}
}