download and print pdf

parent 8a9ff11e
......@@ -43,6 +43,12 @@
</div>
<div class="actions">
<button class="ui teal button" type="button" id="btn-vm-download" disabled>
<i class="download icon"></i> Download
</button>
<button class="ui blue button" type="button" id="btn-vm-print" disabled>
<i class="print icon"></i> Print
</button>
<button class="ui grey button" onclick="$('#modalViewTuntutan').modal('hide');">
<i class="times icon"></i> Close
</button>
......@@ -56,6 +62,7 @@
'January','February','March','April','May','June',
'July','August','September','October','November','December'
];
var VCM_CLAIM_TITLE = 'Overtime Claim Form';
function vcmPad(n) { return String(n).padStart(2, '0'); }
function vcmEsc(s) {
......@@ -71,7 +78,8 @@ function vcmFmtDate(str) {
}
function viewTuntutan(id) {
$('#vm-claim-content').html(
$('#btn-vm-download, #btn-vm-print').prop('disabled', true);
$('#vm-claim-content').removeAttr('data-claim-id data-claim-number').html(
'<div class="vm-loading"><i class="spinner loading icon"></i> Loading data…</div>'
);
$('#modalViewTuntutan').modal({ blurring: true, closable: true }).modal('show');
......@@ -81,7 +89,12 @@ function viewTuntutan(id) {
method : 'GET',
success: function (res) {
if (res.success) {
$('#vm-claim-content').html(vcmBuildHtml(res.data.main, res.data.details));
$('#vm-claim-content')
.attr('data-claim-id', res.data.main.id)
.attr('data-claim-number', res.data.main.ocm_apply_no)
.html(vcmBuildHtml(res.data.main, res.data.details));
var canExport = Number(res.data.main.ocm_status) === 11;
$('#btn-vm-download, #btn-vm-print').prop('disabled', !canExport);
} else {
$('#vm-claim-content').html(
'<div class="ui negative message"><p>'
......@@ -98,6 +111,29 @@ function viewTuntutan(id) {
});
}
function vcmDownload() {
var id = $('#vm-claim-content').data('claim-id');
if (!id) return;
window.location.href = "{{ url('overtime/claim') }}/" + id + "/pdf";
}
function vcmPrint() {
var id = $('#vm-claim-content').data('claim-id');
var claimNumber = $('#vm-claim-content').data('claim-number');
if (!id || !claimNumber) return;
var printWindow = window.open(
"{{ url('overtime/claim') }}/" + id + "/pdf/" + encodeURIComponent(claimNumber) + "?inline=1",
'_blank'
);
if (!printWindow) {
alert('Unable to open the print window. Please allow pop-ups and try again.');
return;
}
}
$(document).on('click', '#btn-vm-download', vcmDownload);
$(document).on('click', '#btn-vm-print', vcmPrint);
function vcmBuildHtml(main, details) {
var badge = '<span class="ui label" style="font-size:.78rem;">' + vcmEsc(main.ocm_status_label || '-') + '</span>';
......
......@@ -31,7 +31,7 @@
? ($monthNames[(int) $main['ocm_month'] - 1] ?? '-') . ' ' . $main['ocm_year']
: '-';
$badge = '<span class="ui label" style="font-size:.78rem;">' . $escape($main['ocm_status_label'] ?? '-') . '</span>';
$badge = '<span class="ui label vm-status-badge">' . $escape($main['ocm_status_label'] ?? '-') . '</span>';
$grandTotalTotal = 0;
$grandTotalEffective = 0;
?>
......@@ -95,12 +95,12 @@
<td>
@php $isNoProject = (!empty($detail['project_id']) && $detail['project_id'] == 110) || (!empty($detail['fk_lkp_project']) && $detail['fk_lkp_project'] == 110); @endphp
@if($isNoProject)
<span class="ui tiny blue label">Auto</span>
<span class="ui tiny blue label vm-review-badge">Auto</span>
@else
@if (!empty($detail['ocd_pm_reviewed']))
<span class="ui green label">Reviewed</span>
<span class="ui green label vm-review-badge">Reviewed</span>
@else
<span class="ui orange label">Pending</span>
<span class="ui orange label vm-review-badge">Pending</span>
@endif
@endif
</td>
......
......@@ -21,6 +21,7 @@ function () {
Route::post('claim/store', ['uses' => 'OvertimeClaimController@storeOvertimeClaim', 'as' => 'overtime.storeovertimeclaim']);
Route::get('claim/existing-months', ['uses' => 'OvertimeClaimController@getExistingMonths', 'as' => 'overtime.existingmonths']);
Route::get('claim/{id}/view', ['uses' => 'OvertimeClaimController@viewOvertimeClaim', 'as' => 'overtime.viewovertimeclaim']);
Route::get('claim/{id}/pdf/{filename?}', ['uses' => 'OvertimeClaimController@downloadOvertimeClaim', 'as' => 'overtime.downloadovertimeclaim']);
Route::get('claim/{id}/edit', ['uses' => 'OvertimeClaimController@editOvertimeClaim', 'as' => 'overtime.editovertimeclaim']);
Route::post('claim/{id}/detail/add', ['uses' => 'OvertimeClaimController@addOvertimeClaimDetail', 'as' => 'overtime.addovertimeclaim']);
Route::post('claim/detail/{id}/update', ['uses' => 'OvertimeClaimController@updateOvertimeClaimDetail', 'as' => 'overtime.updateovertimeclaimdetail']);
......
......@@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
use Barryvdh\DomPDF\Facade\Pdf;
use Portal\Overtime\Data\Repo\OvertimeClaimRepo;
use Portal\Overtime\Model\OvertimeClaimMain;
......@@ -200,6 +201,102 @@ public function viewOvertimeClaim(Request $request, int $id): JsonResponse
]);
}
public function downloadOvertimeClaim(Request $request, int $id, ?string $filename = null)
{
$main = $this->repos->findOvertimeClaim($id);
$staffInfoId = DB::table('staff_info')->where('fk_users', Auth::id())->value('id');
abort_unless((int) $main->fk_lkp_staff_info === (int) $staffInfoId, 403);
abort_unless((int) $main->ocm_status === 11, 404);
$details = $this->repos->findOvertimeClaimDetails($id);
$projectNames = DB::table('lkp_project')
->whereIn('id', $details->pluck('fk_lkp_project')->filter()->unique())
->pluck('p_project_description', 'id');
$statusLabel = DB::table('lkp_status')
->where('module', 'ot')
->where('ref_id', $main->ocm_status)
->value('description');
$staffInfo = DB::table('staff_info')->where('id', $main->fk_lkp_staff_info)->first();
$departmentId = $staffInfo->fk_lkp_department ?? $main->ocm_division;
$mainData = [
'ocm_apply_no' => $main->ocm_apply_no,
'ocm_staff_name' => $main->ocm_staff_name,
'ocm_division_label' => $departmentId
? (DB::table('lkp_department')->where('id', $departmentId)->value('description')
?: DB::table('lkp_department')->where('id', $departmentId)->value('name') ?: '-')
: '-',
'ocm_status_label' => $statusLabel ?: '-',
'ocm_month' => $main->ocm_month,
'ocm_year' => $main->ocm_year,
'ocm_submit_date' => $main->ocm_submit_date,
'ocm_approved_by_name' => $main->ocm_approved_by
? (DB::table('staff_info')->where('fk_users', $main->ocm_approved_by)->value('fullname')
?: DB::table('users')->where('id', $main->ocm_approved_by)->value('name') ?: '-')
: '-',
'ocm_approved_date' => $main->ocm_approved_date,
];
$detailData = $details->map(fn ($detail) => [
'ocd_date' => $detail->ocd_date,
'ocd_day_type' => $detail->ocd_day_type,
'ocd_office_hour' => $detail->ocd_office_hour,
'ocd_overtime_from' => $detail->ocd_overtime_from,
'ocd_overtime_to' => $detail->ocd_overtime_to,
'ocd_total_hours' => $detail->ocd_total_hours,
'ocd_effective_hours' => $detail->ocd_effective_hours,
'ocd_tasks' => $detail->ocd_tasks,
'fk_lkp_project' => $detail->fk_lkp_project,
'project_name' => $projectNames[$detail->fk_lkp_project] ?? '',
'ocd_pm_reviewed' => $detail->ocd_pm_reviewed,
])->all();
$claimContent = view('overtime::overtime.overtimeclaim.view_modal_content', [
'main' => $mainData,
'details' => $detailData,
])->render();
$pdfStyles = <<<'CSS'
@page { size: A4 landscape; margin: 10mm; }
body { font-family: DejaVu Sans, sans-serif; color: #222; margin: 0; font-size: 9px; }
.vm-summary { background: #ddeeff; border-radius: 6px; padding: 8px 11px; margin-bottom: 10px; line-height: 1.75; }
.vm-summary .apply-no { font-size: 12px; font-weight: 700; color: #1a56a0; }
.vm-summary .meta-line { font-size: 9px; color: #333; }
.vm-tbl { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: 8px; }
.vm-tbl th, .vm-tbl td { border: 1px solid #cfd8e3; padding: 5px 4px; min-width: 0 !important; vertical-align: middle; text-align: center; word-wrap: break-word; }
.vm-tbl thead tr.grp th { background: #2185d0; color: #fff; font-weight: 600; }
.vm-tbl thead tr.grp th.vm-light { background: #5aace8; }
.vm-tbl thead tr.grp th.vm-plain { background: #e8f0fa; color: #444; }
.vm-tbl tbody td.vm-shade { background: #f0f7ff; }
.vm-tbl tbody td.vm-left { text-align: left; }
.vm-tbl tfoot td { background: #ddeeff; font-weight: 700; color: #1a56a0; border-top: 2px solid #2185d0; }
.vm-tbl tfoot .lbl { text-align: right; font-size: 8px; }
.vm-empty { text-align: center; color: #bbb; font-style: italic; padding: 16px; }
.ui.label { display: inline-block; padding: 3px 6px; border-radius: 3px; background: #e8e8e8; font-size: 8px; }
.vm-status-badge, .vm-review-badge { vertical-align: middle; line-height: 1.2; white-space: nowrap; }
.vm-status-badge { margin-left: 2px; }
.ui.tiny.label, .ui.blue.label { background: #2185d0; color: #fff; }
.ui.green.label { background: #21ba45; color: #fff; }
.ui.orange.label { background: #f2711c; color: #fff; }
CSS;
$pdf = Pdf::loadHTML('<!doctype html><html><head><meta charset="utf-8"><style>'
. $pdfStyles . '</style></head><body>' . $claimContent . '</body></html>')
->setPaper('a4', 'landscape');
$filename = $main->ocm_apply_no . '.pdf';
if ($request->boolean('inline')) {
return response($pdf->output(), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="' . $filename . '"',
]);
}
return response($pdf->output(), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
]);
}
// EDIT
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment