102 lines
3.1 KiB
PHP
102 lines
3.1 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();
|
|
$payslips = Payslip::where('employee_id', $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,
|
|
'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,
|
|
]);
|
|
}
|
|
}
|