217 lines
6.7 KiB
PHP
217 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\StoreEmployeeRequest;
|
|
use App\Models\Employee;
|
|
use App\Models\Project;
|
|
use App\Models\Currency;
|
|
use App\Helpers\CurrencyHelper;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class EmployeeController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of employees with filters.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorize('viewAny', Employee::class);
|
|
|
|
$query = Employee::with(['project', 'currency']);
|
|
|
|
// Filter by project
|
|
if ($request->filled('project_id')) {
|
|
$query->where('project_id', $request->project_id);
|
|
}
|
|
|
|
// Filter by status (active/inactive)
|
|
if ($request->filled('status')) {
|
|
$query->where('status', $request->status);
|
|
}
|
|
|
|
// Filter by contract type
|
|
if ($request->filled('contract_type')) {
|
|
$query->where('contract_type', $request->contract_type);
|
|
}
|
|
|
|
// Filter by search
|
|
if ($request->filled('search')) {
|
|
$search = $request->search;
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('first_name', 'like', "%{$search}%")
|
|
->orWhere('last_name', 'like', "%{$search}%")
|
|
->orWhere('national_code', 'like', "%{$search}%")
|
|
->orWhere('position', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
$query->orderBy('first_name')->orderBy('last_name');
|
|
|
|
$employees = $query->paginate(16)->appends($request->query());
|
|
|
|
$projects = Project::whereIn('status', ['planning', 'active'])->get();
|
|
$contractTypes = ['full_time', 'part_time', 'contractor', 'intern', 'consultant'];
|
|
|
|
return view('employees.index', compact('employees', 'projects', 'contractTypes'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new employee.
|
|
*/
|
|
public function create()
|
|
{
|
|
$this->authorize('create', Employee::class);
|
|
|
|
$employee = null;
|
|
$projects = Project::whereIn('status', ['planning', 'active'])->get();
|
|
$currencies = Currency::where('is_active', true)->get();
|
|
$contractTypes = ['full_time', 'part_time', 'contractor', 'intern', 'consultant'];
|
|
|
|
return view('employees.form', compact('employee', 'projects', 'currencies', 'contractTypes'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created employee in storage.
|
|
*/
|
|
public function store(StoreEmployeeRequest $request)
|
|
{
|
|
$this->authorize('create', Employee::class);
|
|
|
|
$data = $request->validated();
|
|
$data['tenant_id'] = $this->getTenantId();
|
|
|
|
// Convert jalali date if needed
|
|
if (!empty($data['hire_date'])) {
|
|
$data['hire_date'] = \App\Helpers\CalendarHelper::parseDate($data['hire_date']);
|
|
}
|
|
|
|
$employee = Employee::create($data);
|
|
|
|
return redirect()
|
|
->route('employees.show', $employee)
|
|
->with('success', __('employee.created_successfully'));
|
|
}
|
|
|
|
/**
|
|
* Display the specified employee with work logs and financials.
|
|
*/
|
|
public function show(Employee $employee)
|
|
{
|
|
$this->authorize('view', $employee);
|
|
|
|
$employee->load([
|
|
'project',
|
|
'currency',
|
|
'workLogs.project',
|
|
'workLogs.task',
|
|
'financials.currency',
|
|
]);
|
|
|
|
return view('employees.show', compact('employee'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified employee.
|
|
*/
|
|
public function edit(Employee $employee)
|
|
{
|
|
$this->authorize('update', $employee);
|
|
|
|
$projects = Project::whereIn('status', ['planning', 'active'])->get();
|
|
$currencies = Currency::where('is_active', true)->get();
|
|
$contractTypes = ['full_time', 'part_time', 'contractor', 'intern', 'consultant'];
|
|
|
|
return view('employees.form', compact('employee', 'projects', 'currencies', 'contractTypes'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified employee in storage.
|
|
*/
|
|
public function update(StoreEmployeeRequest $request, Employee $employee)
|
|
{
|
|
$this->authorize('update', $employee);
|
|
|
|
$data = $request->validated();
|
|
|
|
// Convert jalali date if needed
|
|
if (!empty($data['hire_date'])) {
|
|
$data['hire_date'] = \App\Helpers\CalendarHelper::parseDate($data['hire_date']);
|
|
}
|
|
|
|
$employee->update($data);
|
|
|
|
return redirect()
|
|
->route('employees.show', $employee)
|
|
->with('success', __('employee.updated_successfully'));
|
|
}
|
|
|
|
/**
|
|
* Remove the specified employee from storage.
|
|
*/
|
|
public function destroy(Employee $employee)
|
|
{
|
|
$this->authorize('delete', $employee);
|
|
|
|
$employee->delete();
|
|
|
|
return redirect()
|
|
->route('employees.index')
|
|
->with('success', __('employee.deleted_successfully'));
|
|
}
|
|
|
|
/**
|
|
* Display work logs for the specified employee.
|
|
*/
|
|
public function workLogs(Request $request, Employee $employee)
|
|
{
|
|
$this->authorize('view', $employee);
|
|
|
|
$query = $employee->workLogs()->with(['project', 'task']);
|
|
|
|
if ($request->filled('from_date') && $request->filled('to_date')) {
|
|
$query->dateRange($request->from_date, $request->to_date);
|
|
}
|
|
|
|
if ($request->filled('project_id')) {
|
|
$query->where('project_id', $request->project_id);
|
|
}
|
|
|
|
$totalHours = (clone $query)->sum('hours_worked');
|
|
$overtimeHours = (clone $query)->overtime()->sum('overtime_hours');
|
|
$workLogs = $query->orderBy('date', 'desc')->paginate(20);
|
|
|
|
$projects = Project::whereIn('status', ['planning', 'active'])->get();
|
|
|
|
return view('employees.work-logs', compact('employee', 'workLogs', 'totalHours', 'overtimeHours', 'projects'));
|
|
}
|
|
|
|
/**
|
|
* Display financial records for the specified employee.
|
|
*/
|
|
public function financials(Request $request, Employee $employee)
|
|
{
|
|
$this->authorize('viewFinancials', $employee);
|
|
|
|
$query = $employee->financials()->with('currency');
|
|
|
|
if ($request->filled('type')) {
|
|
$query->byType($request->type);
|
|
}
|
|
|
|
if ($request->filled('is_paid') && $request->is_paid !== '') {
|
|
$query->where('is_paid', $request->boolean('is_paid'));
|
|
}
|
|
|
|
$financials = $query->orderBy('effective_date', 'desc')->paginate(20);
|
|
|
|
$totalPaid = $employee->financials()->where('is_paid', true)->sum('amount');
|
|
$totalUnpaid = $employee->financials()->where('is_paid', false)->sum('amount');
|
|
|
|
$financialTypes = ['salary', 'bonus', 'deduction', 'overtime', 'advance', 'reimbursement'];
|
|
|
|
return view('employees.financials', compact('employee', 'financials', 'totalPaid', 'totalUnpaid', 'financialTypes'));
|
|
}
|
|
}
|