feat(webstatement): update print statement functionalities

- Menambahkan kolom `remarks` pada tabel print_statement_logs untuk menyimpan catatan tambahan.
- Mengubah validasi periode pada `PrintStatementRequest` untuk mencegah request duplikasi periode.
- Memperbaiki tampilan di `statements.index` dan `statements.show` agar lebih responsif dan informatif.
- Mengubah logika download statement untuk mendukung file range periode dalam format zip.
- Menambahkan logika cek file statement berdasarkan ketersediaan file di storage SFTP.
- Menghapus file legacy `create.blade.php` yang tidak lagi digunakan.
- Menyesuaikan ikon menu dari `calendar` ke `printer` agar lebih relevan.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
Daeng Deni Mardaeni
2025-05-13 14:01:41 +07:00
parent 823ccf0fe7
commit 8e5c2ce79e
8 changed files with 485 additions and 133 deletions

View File

@@ -5,9 +5,11 @@
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Modules\Webstatement\Http\Requests\PrintStatementRequest;
use Modules\Webstatement\Models\PrintStatementLog;
use Modules\Basicdata\Models\Branch;
use phpseclib3\Net\SFTP;
class PrintStatementController extends Controller
{
@@ -49,7 +51,7 @@
// Process statement availability check (this would be implemented based on your system)
$this->checkStatementAvailability($statement);
return redirect()->route('webstatement.statements.show', $statement->id)
return redirect()->route('statements.index')
->with('success', 'Statement request has been created successfully.');
}
@@ -72,9 +74,9 @@
return back()->with('error', 'Statement is not available for download.');
}
if ($statement->authorization_status !== 'approved') {
/* if ($statement->authorization_status !== 'approved') {
return back()->with('error', 'Statement download requires authorization.');
}
}*/
// Update download status
$statement->update([
@@ -84,10 +86,75 @@
]);
// Generate or fetch the statement file (implementation depends on your system)
$filePath = $this->generateStatementFile($statement);
$disk = Storage::disk('sftpStatement');
$filePath = "{$statement->period_from}/Print/{$statement->branch_code}/{$statement->account_number}.pdf";
// Return file download response
return response()->download($filePath, $this->generateFileName($statement));
if ($statement->is_period_range && $statement->period_to) {
$periodFrom = \Carbon\Carbon::createFromFormat('Ym', $statement->period_from);
$periodTo = \Carbon\Carbon::createFromFormat('Ym', $statement->period_to);
// Loop through each month in the range
$missingPeriods = [];
$availablePeriods = [];
for ($period = clone $periodFrom; $period->lte($periodTo); $period->addMonth()) {
$periodFormatted = $period->format('Ym');
$periodPath = $periodFormatted . "/Print/{$statement->branch_code}/{$statement->account_number}.pdf";
if ($disk->exists($periodPath)) {
$availablePeriods[] = $periodFormatted;
} else {
$missingPeriods[] = $periodFormatted;
}
}
// If any period is missing, the statement is not available
if (count($availablePeriods) > 0) {
// Create a temporary zip file
$zipFileName = "{$statement->account_number}_{$statement->period_from}_to_{$statement->period_to}.zip";
$zipFilePath = storage_path("app/temp/{$zipFileName}");
// Ensure the temp directory exists
if (!file_exists(storage_path('app/temp'))) {
mkdir(storage_path('app/temp'), 0755, true);
}
// Create a new zip archive
$zip = new \ZipArchive();
if ($zip->open($zipFilePath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) === TRUE) {
// Add each available statement to the zip
foreach ($availablePeriods as $period) {
$filePath = "{$period}/Print/{$statement->branch_code}/{$statement->account_number}.pdf";
$localFilePath = storage_path("app/temp/{$statement->account_number}_{$period}.pdf");
// Download the file from SFTP to local storage temporarily
file_put_contents($localFilePath, $disk->get($filePath));
// Add the file to the zip
$zip->addFile($localFilePath, "{$statement->account_number}_{$period}.pdf");
}
$zip->close();
// Clean up temporary files
foreach ($availablePeriods as $period) {
$localFilePath = storage_path("app/temp/{$statement->account_number}_{$period}.pdf");
if (file_exists($localFilePath)) {
unlink($localFilePath);
}
}
// Return the zip file for download
return response()->download($zipFilePath, $zipFileName)->deleteFileAfterSend(true);
} else {
return back()->with('error', 'Failed to create zip archive.');
}
} else {
return back()->with('error', 'No statements available for download.');
}
} else if($disk->exists($filePath)) {
return $disk->download($filePath, "{$statement->account_number}_{$statement->period_from}.pdf");
}
}
/**
@@ -111,8 +178,9 @@
$statusText = $request->authorization_status === 'approved' ? 'approved' : 'rejected';
return redirect()->route('webstatement.statements.show', $statement->id)
return redirect()->route('statements.show', $statement->id)
->with('success', "Statement request has been {$statusText} successfully.");
}
/**
@@ -123,13 +191,61 @@
{
// This would be implemented based on your system's logic
// For example, checking an API or database for statement availability
$disk = Storage::disk('sftpStatement');
// Placeholder implementation - set to available for demo
//format folder /periode/Print/branch_code/account_number.pdf
$filePath = "{$statement->period_from}/Print/{$statement->branch_code}/{$statement->account_number}.pdf";
// Check if the statement exists in the storage
if ($statement->is_period_range && $statement->period_to) {
$periodFrom = \Carbon\Carbon::createFromFormat('Ym', $statement->period_from);
$periodTo = \Carbon\Carbon::createFromFormat('Ym', $statement->period_to);
// Loop through each month in the range
$missingPeriods = [];
$availablePeriods = [];
for ($period = clone $periodFrom; $period->lte($periodTo); $period->addMonth()) {
$periodFormatted = $period->format('Ym');
$periodPath = $periodFormatted . "/Print/{$statement->branch_code}/{$statement->account_number}.pdf";
if ($disk->exists($periodPath)) {
$availablePeriods[] = $periodFormatted;
} else {
$missingPeriods[] = $periodFormatted;
}
}
// If any period is missing, the statement is not available
if (count($missingPeriods) > 0) {
$notes = "Missing periods: " . implode(', ', $missingPeriods);
$statement->update([
'is_available' => false,
'remarks' => $notes,
'updated_by' => Auth::id()
]);
return;
} else {
// All periods are available
$statement->update([
'is_available' => true,
'updated_by' => Auth::id()
]);
}
} else if($disk->exists($filePath)) {
$statement->update([
'is_available' => true,
'updated_by' => Auth::id()
]);
return;
}
$statement->update([
'is_available' => false,
'updated_by' => Auth::id()
]);
return;
}
/**
* Generate or fetch the statement file.
@@ -213,6 +329,7 @@
'account' => 'account_number',
'period' => 'period_from',
'status' => 'authorization_status',
'remarks' =>'remarks',
// Add more mappings as needed
];
@@ -254,10 +371,11 @@
'authorization_status' => $item->authorization_status,
'is_available' => $item->is_available,
'is_downloaded' => $item->is_downloaded,
'created_at' => $item->created_at->format('Y-m-d H:i:s'),
'created_at' => dateFormat($item->created_at,1,1),
'created_by' => $item->user->name ?? 'N/A',
'authorized_by' => $item->authorizer ? $item->authorizer->name : null,
'authorized_at' => $item->authorized_at ? $item->authorized_at->format('Y-m-d H:i:s') : null,
'remarks' => $item->remarks,
];
});
@@ -278,4 +396,13 @@
'data' => $data,
]);
}
public function destroy(PrintStatementLog $statement){
// Delete the statement
$statement->delete();
return response()->json([
'message' => 'Statement deleted successfully.',
]);
}
}

View File

@@ -3,6 +3,8 @@
namespace Modules\Webstatement\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Modules\Webstatement\Models\PrintStatementLog as Statement;
class PrintStatementRequest extends FormRequest
{
@@ -27,11 +29,38 @@
'branch_code' => ['required', 'string', 'exists:branches,code'],
'account_number' => ['required', 'string'],
'is_period_range' => ['sometimes', 'boolean'],
'period_from' => ['required', 'string', 'regex:/^\d{6}$/'], // YYYYMM format
'period_from' => [
'required',
'string',
'regex:/^\d{6}$/', // YYYYMM format
// Prevent duplicate requests with same account number and period
function ($attribute, $value, $fail) {
$query = Statement::where('account_number', $this->input('account_number'))
->where('authorization_status', '!=', 'rejected')
->where('period_from', $value);
// If this is an update request, exclude the current record
if ($this->route('statement')) {
$query->where('id', '!=', $this->route('statement'));
}
// If period_to is provided, check for overlapping periods
if ($this->input('period_to')) {
$query->where(function ($q) use ($value) {
$q->where('period_from', '<=', $this->input('period_to'))
->where('period_to', '>=', $value);
});
}
if ($query->exists()) {
$fail('A statement request with this account number and period already exists.');
}
}
],
];
// If it's a period range, require period_to
if ($this->input('is_period_range')) {
if ($this->input('period_to')) {
$rules['period_to'] = [
'required',
'string',
@@ -71,10 +100,24 @@
protected function prepareForValidation()
: void
{
// Convert is_period_range to boolean if it exists
if ($this->has('is_period_range')) {
if($this->has('period_from')){
//conver to YYYYMM format
$this->merge([
'is_period_range' => filter_var($this->is_period_range, FILTER_VALIDATE_BOOLEAN),
'period_from' => substr($this->period_from, 0, 4).substr($this->period_from, 5, 2),
]);
}
if($this->has('period_to')){
//conver to YYYYMM format
$this->merge([
'period_to' => substr($this->period_to, 0, 4).substr($this->period_to, 5, 2),
]);
}
// Convert is_period_range to boolean if it exists
if ($this->has('period_to')) {
$this->merge([
'is_period_range' => true,
]);
}
}

View File

@@ -30,6 +30,7 @@
'deleted_by',
'authorized_by',
'authorized_at',
'remarks',
];
protected $casts = [

View File

@@ -24,6 +24,7 @@
$table->string('ip_address')->nullable()->comment('IP address of requester');
$table->string('user_agent')->nullable()->comment('User agent of requester');
$table->timestamp('downloaded_at')->nullable()->comment('When the statement was downloaded');
$table->string('remarks')->nullable()->comment('Remarks for the statement');
$table->enum('authorization_status', ['pending', 'approved', 'rejected'])->default('pending');

View File

@@ -25,7 +25,7 @@
{
"title": "Print Statement",
"path": "statements",
"icon": "ki-filled ki-calendar text-lg text-primary",
"icon": "ki-filled ki-printer text-lg text-primary",
"classes": "",
"attributes": [],
"permission": "",

View File

@@ -1,38 +0,0 @@
@extends('layouts.main')
@section('breadcrumbs')
@endsection
@section('content')
@endsection
@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function() {
// Validate that end date is after start date
const startDateInput = document.getElementById('start_date');
const endDateInput = document.getElementById('end_date');
function validateDates() {
const startDate = new Date(startDateInput.value);
const endDate = new Date(endDateInput.value);
if (startDate > endDate) {
endDateInput.setCustomValidity('End date must be after start date');
} else {
endDateInput.setCustomValidity('');
}
}
startDateInput.addEventListener('change', validateDates);
endDateInput.addEventListener('change', validateDates);
// Set max date for date inputs to today
const today = new Date().toISOString().split('T')[0];
startDateInput.setAttribute('max', today);
endDateInput.setAttribute('max', today);
});
</script>
@endpush

View File

@@ -5,15 +5,10 @@
@endsection
@section('content')
<div class="grid">
<div class="card">
<div class="grid grid-cols-8 gap-5">
<div class="col-span-2 card">
<div class="card-header">
<h3 class="card-title">Request Print Stetement</h3>
<div class="card-toolbar">
<a href="{{ route('statements.index') }}" class="btn btn-sm btn-light">
<i class="ki-outline ki-arrow-left fs-2"></i> Back
</a>
</div>
</div>
<div class="card-body">
<form action="{{ isset($statement) ? route('statements.update', $statement->id) : route('statements.store') }}" method="POST">
@@ -22,7 +17,7 @@
@method('PUT')
@endif
<div class="grid grid-cols-1 md:grid-cols-2 gap-5">
<div class="grid grid-cols-1 gap-5">
<div class="form-group">
<label class="form-label required" for="branch_code">Branch</label>
<select class="select tomselect @error('branch_code') is-invalid @enderror" id="branch_code" name="branch_code" required>
@@ -81,9 +76,7 @@
</form>
</div>
</div>
</div>
<div class="grid mt-5">
<div class="col-span-6">
<div class="card card-grid min-w-full" data-datatable="false" data-datatable-page-size="10" data-datatable-state-save="false" id="statement-table" data-api-url="{{ route('statements.datatables') }}">
<div class="card-header py-5 flex-wrap">
<h3 class="card-title">
@@ -129,8 +122,8 @@
<span class="sort"> <span class="sort-label"> Available </span>
<span class="sort-icon"> </span> </span>
</th>
<th class="min-w-[150px]" data-datatable-column="is_downloaded">
<span class="sort"> <span class="sort-label"> Downloaded </span>
<th class="min-w-[150px]" data-datatable-column="remarks">
<span class="sort"> <span class="sort-label"> Notes </span>
<span class="sort-icon"> </span> </span>
</th>
<th class="min-w-[180px]" data-datatable-column="created_at">
@@ -156,6 +149,7 @@
</div>
</div>
</div>
</div>
@endsection
@push('scripts')
@@ -177,7 +171,7 @@
}
});
$.ajax(`webstatement/statements/${data}`, {
$.ajax(`statements/${data}`, {
type: 'DELETE'
}).then((response) => {
swal.fire('Deleted!', 'Statement request has been deleted.', 'success').then(() => {
@@ -222,6 +216,24 @@
},
period: {
title: 'Period',
render: (item, data) => {
const monthNames = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
];
const formatPeriod = (period) => {
if (!period) return '';
const year = period.substring(0, 4);
const month = parseInt(period.substring(4, 6));
return `${monthNames[month - 1]} ${year}`;
};
const fromPeriod = formatPeriod(data.period_from);
const toPeriod = data.period_to ? ` - ${formatPeriod(data.period_to)}` : '';
return fromPeriod + toPeriod;
},
},
authorization_status: {
title: 'Status',
@@ -247,20 +259,13 @@
return `<span class="${statusClass}">${statusText}</span>`;
},
},
is_downloaded: {
title: 'Downloaded',
render: (item, data) => {
let statusClass = data.is_downloaded ? 'badge badge-light-success' : 'badge badge-light-danger';
let statusText = data.is_downloaded ? 'Yes' : 'No';
let downloadInfo = data.is_downloaded && data.downloaded_at ?
`<div class="text-muted text-xs mt-1">${new Date(data.downloaded_at).toLocaleString()}</div>` : '';
return `<span class="${statusClass}">${statusText}</span>${downloadInfo}`;
},
remarks : {
title: 'Notes',
},
created_at: {
title: 'Created At',
render: (item, data) => {
return data.created_at ? new Date(data.created_at).toLocaleString() : '';
return data.created_at ?? '';
},
},
actions: {
@@ -271,17 +276,11 @@
<i class="ki-outline ki-eye"></i>
</a>`;
// Only show edit button if status is pending
if (data.authorization_status === 'pending') {
buttons += `<a class="btn btn-sm btn-icon btn-clear btn-primary" href="statements/${data.id}/edit">
<i class="ki-outline ki-notepad-edit"></i>
</a>`;
}
// Show download button if statement is approved and available but not downloaded
if (data.authorization_status === 'approved' && data.is_available && !data.is_downloaded) {
//if (data.authorization_status === 'approved' && data.is_available && !data.is_downloaded) {
if (data.is_available) {
buttons += `<a class="btn btn-sm btn-icon btn-clear btn-success" href="statements/${data.id}/download">
<i class="ki-outline ki-document-download"></i>
<i class="ki-outline ki-cloud-download"></i>
</a>`;
}

View File

@@ -0,0 +1,219 @@
@extends('layouts.main')
@section('content')
<div class="card">
<div class="card-header">
<h3 class="card-title">Statement Request Details</h3>
<div class="card-toolbar">
<a href="{{ route('statements.index') }}" class="btn btn-sm btn-info me-2">
<i class="ki-duotone ki-arrow-left fs-2"></i>Back to List
</a>
@if($statement->is_available && $statement->authorization_status === 'approved')
<a href="{{ route('statements.download', $statement->id) }}" class="btn btn-sm btn-primary">
<i class="ki-duotone ki-document fs-2"></i>Download Statement
</a>
@endif
</div>
</div>
<div class="card-body">
@if(session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
@if(session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
@endif
<div class="grid grid-cols-2 gap-5 g-5">
<!-- Left Column - Statement Information -->
<div class="card card-flush h-xl-100 shadow-sm">
<div class="card-header">
<div class="card-title">
<h2>Statement Information</h2>
</div>
</div>
<div class="card-body py-5">
<div class="d-flex flex-column gap-5">
<div class="d-flex flex-column">
<div class="text-gray-500 fw-semibold">Branch</div>
<div class="fw-bold fs-5">{{ $statement->branch->name ?? 'N/A' }} ({{ $statement->branch_code }})</div>
</div>
<div class="d-flex flex-column">
<div class="text-gray-500 fw-semibold">Account Number</div>
<div class="fw-bold fs-5">{{ $statement->account_number }}</div>
</div>
<div class="d-flex flex-column">
<div class="text-gray-500 fw-semibold">Period</div>
<div class="fw-bold fs-5">
@php
// Convert format YYYYMM to Month Year
$fromYear = substr($statement->period_from, 0, 4);
$fromMonth = substr($statement->period_from, 4, 2);
$fromMonthName = date('F', mktime(0, 0, 0, $fromMonth, 1));
$periodText = $fromMonthName . ' ' . $fromYear;
if($statement->is_period_range && $statement->period_to) {
$toYear = substr($statement->period_to, 0, 4);
$toMonth = substr($statement->period_to, 4, 2);
$toMonthName = date('F', mktime(0, 0, 0, $toMonth, 1));
$periodText .= ' - ' . $toMonthName . ' ' . $toYear;
}
@endphp
{{ $periodText }}
</div>
</div>
<div class="d-flex flex-column">
<div class="text-gray-500 fw-semibold">Status</div>
<div>
@if($statement->authorization_status === 'pending')
<span class="badge badge-warning">Pending Authorization</span>
@elseif($statement->authorization_status === 'approved')
<span class="badge badge-success">Approved</span>
@elseif($statement->authorization_status === 'rejected')
<span class="badge badge-danger">Rejected</span>
@endif
</div>
</div>
<div class="d-flex flex-column">
<div class="text-gray-500 fw-semibold">Availability</div>
<div>
@if($statement->is_available)
<span class="badge badge-success">Available</span>
@else
<span class="badge badge-danger">Not Available</span>
@endif
</div>
</div>
<div class="d-flex flex-column">
<div class="text-gray-500 fw-semibold">Downloaded</div>
<div>
@if($statement->is_downloaded)
<span class="badge badge-success">Yes</span>
<div class="text-muted mt-1">
Downloaded at: {{ $statement->downloaded_at ? $statement->downloaded_at->format('d M Y H:i:s') : 'N/A' }}
</div>
@else
<span class="badge badge-light-primary">No</span>
@endif
</div>
</div>
</div>
</div>
</div>
<!-- Right Column - Request Information -->
<div class="card card-flush h-xl-100 shadow-sm">
<div class="card-header">
<div class="card-title">
<h2>Request Information</h2>
</div>
</div>
<div class="card-body py-5">
<div class="d-flex flex-column gap-5">
<div class="d-flex flex-column">
<div class="text-gray-500 fw-semibold">Requested By</div>
<div class="fw-bold fs-5">{{ $statement->user->name ?? 'N/A' }}</div>
</div>
<div class="d-flex flex-column">
<div class="text-gray-500 fw-semibold">Requested At</div>
<div class="fw-bold fs-5">{{ dateFormat($statement->created_at,1,1) }}</div>
</div>
<div class="d-flex flex-column">
<div class="text-gray-500 fw-semibold">IP Address</div>
<div class="fw-bold fs-5">{{ $statement->ip_address }}</div>
</div>
<div class="d-flex flex-column">
<div class="text-gray-500 fw-semibold">User Agent</div>
<div class="text-muted small">{{ $statement->user_agent }}</div>
</div>
@if($statement->authorization_status !== 'pending')
<div class="d-flex flex-column">
<div class="text-gray-500 fw-semibold">Authorized By</div>
<div class="fw-bold fs-5">{{ $statement->authorizer->name ?? 'N/A' }}</div>
</div>
<div class="d-flex flex-column">
<div class="text-gray-500 fw-semibold">Authorized At</div>
<div class="fw-bold fs-5">{{ $statement->authorized_at ? $statement->authorized_at->format('d M Y H:i:s') : 'N/A' }}</div>
</div>
@if($statement->remarks)
<div class="d-flex flex-column">
<div class="text-gray-500 fw-semibold">Remarks</div>
<div class="fw-bold fs-5">{{ $statement->remarks }}</div>
</div>
@endif
@endif
</div>
</div>
</div>
</div>
@if($statement->authorization_status === 'pending' && auth()->user()->can('authorize_statements'))
<div class="card shadow-sm mt-7">
<div class="card-header">
<h3 class="card-title">Authorization</h3>
</div>
<div class="card-body">
<form action="{{ route('webstatement.statements.authorize', $statement->id) }}" method="POST">
@csrf
<div class="mb-5">
<label class="form-label required">Authorization Decision</label>
<div class="d-flex">
<div class="form-check form-check-custom form-check-solid me-5">
<input class="form-check-input" type="radio" name="authorization_status" value="approved" id="status_approved" required/>
<label class="form-check-label" for="status_approved">
Approve
</label>
</div>
<div class="form-check form-check-custom form-check-solid">
<input class="form-check-input" type="radio" name="authorization_status" value="rejected" id="status_rejected" required/>
<label class="form-check-label" for="status_rejected">
Reject
</label>
</div>
</div>
</div>
<div class="mb-5">
<label class="form-label">Remarks</label>
<textarea class="form-control" name="remarks" rows="3" placeholder="Enter any remarks or reasons for your decision"></textarea>
</div>
<div class="text-end">
<button type="submit" class="btn btn-primary">Submit Authorization</button>
</div>
</form>
</div>
</div>
@endif
</div>
</div>
@endsection
@push('scripts')
<script>
// Any additional JavaScript for this page
document.addEventListener('DOMContentLoaded', function () {
// Initialize any components if needed
});
</script>
@endpush