108 lines
3.7 KiB
PHP
108 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Employee;
|
|
use App\Models\Payslip;
|
|
use App\Services\EmployeeService;
|
|
use Illuminate\Http\Request;
|
|
use Exception;
|
|
|
|
class PayslipApiController extends Controller
|
|
{
|
|
protected EmployeeService $employeeService;
|
|
|
|
public function __construct(EmployeeService $employeeService)
|
|
{
|
|
$this->employeeService = $employeeService;
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
try {
|
|
$employee = $this->employeeService->resolveEmployee($request->user());
|
|
} catch (Exception $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'data' => [],
|
|
'message' => 'Employee profile not found',
|
|
'errors' => null,
|
|
], 403);
|
|
}
|
|
|
|
$user = $request->user();
|
|
$employeeId = $employee ? $employee->id : $user->id;
|
|
|
|
$payslips = Payslip::whereIn('employee_id', [$employeeId, $user->id])
|
|
->orderBy('created_at', 'desc')
|
|
->get()
|
|
->map(function ($p) {
|
|
$downloadUrl = url("/api/payslips/{$p->id}/pdf");
|
|
return [
|
|
'id' => $p->id,
|
|
'employee_id' => $p->employee_id,
|
|
'payslip_number' => $p->payslip_number ?? ('PAY-' . str_pad($p->id, 5, '0', STR_PAD_LEFT)),
|
|
'pay_period_start' => $p->salary_month ? $p->salary_month : ($p->created_at ? $p->created_at->format('M 01, Y') : date('M 01, Y')),
|
|
'pay_period_end' => $p->salary_month ? $p->salary_month : ($p->created_at ? $p->created_at->format('M t, Y') : date('M t, Y')),
|
|
'pay_date' => $p->created_at ? $p->created_at->format('M d, Y') : date('M d, Y'),
|
|
'period' => $p->salary_month ?? 'Current Period',
|
|
'basic_salary' => (float)($p->basic_salary ?? 0),
|
|
'allowances' => (float)($p->allowance ?? 0),
|
|
'deductions' => (float)($p->commission ?? 0),
|
|
'net_pay' => (float)($p->net_payble ?? $p->basic_salary ?? 0),
|
|
'pdf_url' => $downloadUrl,
|
|
'download_url' => $downloadUrl,
|
|
'created_at' => $p->created_at ? $p->created_at->format('Y-m-d') : date('Y-m-d'),
|
|
];
|
|
});
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $payslips,
|
|
'message' => null,
|
|
'errors' => null,
|
|
]);
|
|
}
|
|
|
|
public function showPdf(Request $request, $id)
|
|
{
|
|
try {
|
|
$employee = $this->employeeService->resolveEmployee($request->user());
|
|
} catch (Exception $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'data' => null,
|
|
'message' => 'Employee profile not found',
|
|
'errors' => null,
|
|
], 403);
|
|
}
|
|
|
|
$payslip = Payslip::where('id', $id)
|
|
->whereIn('employee_id', [$employee->id, $employee->user_id])
|
|
->first();
|
|
|
|
if (!$payslip) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'data' => null,
|
|
'message' => 'Payslip record not found or access denied',
|
|
'errors' => null,
|
|
], 404);
|
|
}
|
|
|
|
$url = url("/payslip/{$id}/download");
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Payslip document generated',
|
|
'data' => [
|
|
'id' => $payslip->id,
|
|
'pdf_url' => $url,
|
|
'download_url' => $url,
|
|
],
|
|
'errors' => null,
|
|
]);
|
|
}
|
|
}
|