-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from seiyanz16/main
Enhance Internship Management with Role-based Company Filtering, PDF Export, and Date Validation
- Loading branch information
Showing
16 changed files
with
555 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
// use ZipArchive; | ||
use mikehaertl\pdftk\Pdf; | ||
use Carbon\Carbon; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Http\Request; | ||
|
||
class ExportController extends Controller | ||
{ | ||
public function exportJournal(Request $request, $id) | ||
{ | ||
$user = auth()->user(); | ||
$courseName = $user->courses->first()?->name; | ||
$courseLevel = $courseName ? explode(' ', $courseName)[0] : 'N/A'; | ||
|
||
$formattedDateOfBirth = $user->date_of_birth | ||
? Carbon::parse($user->date_of_birth)->translatedFormat('j F Y') | ||
: 'N/A'; | ||
|
||
$gender = match ($user->gender) { | ||
'male' => 'Laki-Laki', | ||
'female' => 'Perempuan' | ||
}; | ||
|
||
$companyId = $id; | ||
$company = $user->companies()->find($companyId); | ||
if (!$company) { | ||
return response()->json(['error' => 'Company not found!'], 404); | ||
} | ||
$companyName = $company?->name ?? 'N/A'; | ||
|
||
$presences = $user->presences() | ||
->where('company_id', $companyId) | ||
->whereDate('date', '<=', Carbon::now()) | ||
->orderBy('date', 'asc') | ||
->get() | ||
->filter(fn($presence) => !is_null($presence->check_in)) | ||
->values(); | ||
|
||
if ($presences->isEmpty()) { | ||
return response()->json(['error' => 'No presences found!'], 404); | ||
} | ||
|
||
$journals = $user->journals() | ||
->where('company_id', $companyId) | ||
->whereDate('date', '<=', Carbon::now()) | ||
->orderBy('date', 'asc') | ||
->get() | ||
->filter(fn($journal) => !is_null($journal->work_type)) | ||
->values(); | ||
|
||
if ($journals->isEmpty()) { | ||
return response()->json(['error' => 'No journals found!'], 404); | ||
} | ||
|
||
$formFields = [ | ||
'namasiswa' => $user->name, | ||
'kelas' => $courseLevel, | ||
'alamat_siswa' => $user->address, | ||
'jurusan' => $user->departments->first()?->description, | ||
'instansi_nama' => $companyName, | ||
'kelasjurusan' => $courseName, | ||
'ttl' => $formattedDateOfBirth, | ||
'gender' => $gender, | ||
]; | ||
|
||
foreach ($presences as $index => $presence) { | ||
$i = $index + 1; | ||
$formFields["tanggal$i"] = Carbon::parse($presence->date)->translatedFormat('l, j F Y'); | ||
$formFields["checkin$i"] = $presence->check_in; | ||
$formFields["checkout$i"] = $presence->check_out ?? 'N/A'; | ||
} | ||
|
||
foreach ($journals as $index => $journal) { | ||
$i = $index + 1; | ||
$formFields["no$i"] = $i; | ||
$formFields["bidang$i"] = $journal->work_type; | ||
$formFields["uraian$i"] = $journal->description; | ||
$formFields["tgl_jurnal$i"] = Carbon::parse($journal->date)->translatedFormat('l, j F Y'); | ||
} | ||
|
||
$templatePath = ''; | ||
if (count($presences) <= 93 || count($journals) <= 93) { | ||
$templatePath = public_path('template-pdf/template_3_month.pdf'); | ||
} else { | ||
$templatePath = public_path('template-pdf/template_6_month.pdf'); | ||
} | ||
|
||
if (!file_exists($templatePath)) { | ||
return response()->json(['error' => 'PDF template not found!'], 404); | ||
} | ||
|
||
$fileName = $user->id . time() . '.pdf'; | ||
$tempPath = storage_path('storage/journals/' . $fileName); | ||
$outputPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $fileName; | ||
$pdf = new Pdf($templatePath); | ||
$pdf->fillForm($formFields)->flatten()->saveAs($outputPath); | ||
|
||
return response()->download($outputPath, $fileName)->deleteFileAfterSend(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ class Score extends Model | |
'company_id', | ||
'name', | ||
'score', | ||
'type', | ||
]; | ||
|
||
protected $appends = [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.