- Mengubah logika pengambilan nilai filter pencarian dari `request` menjadi format JSON terstruktur. - Menambahkan validasi untuk memastikan hanya filter yang memiliki nilai diterapkan. - Memperbaiki logika filter di frontend dengan menggabungkan parameter pencarian dan filter lainnya sebelum mengirimkannya. - Mengganti metode `setRequestParams` di frontend menjadi `search` untuk pengiriman parameter pencarian yang lebih terstruktur. Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
157 lines
5.1 KiB
PHP
157 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\Webstatement\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\File;
|
|
use Carbon\Carbon;
|
|
use Modules\Webstatement\Jobs\GenerateBiayaKartuCsvJob;
|
|
use Modules\Webstatement\Models\KartuSyncLog;
|
|
|
|
class SyncLogsController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
return view('webstatement::sync-logs.index');
|
|
}
|
|
|
|
/**
|
|
* Provide data for datatables - sync logs
|
|
*/
|
|
public function dataForDatatables(Request $request)
|
|
{
|
|
// Retrieve data from the database
|
|
$query = KartuSyncLog::query();
|
|
|
|
// Apply search filter if provided
|
|
if ($request->has('search') && !empty($request->get('search'))) {
|
|
$search = json_decode($request->get('search'));
|
|
if (isset($search->search)) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('periode', 'LIKE', "%$search->search%")
|
|
->orWhere('sync_notes', 'LIKE', "%$search->search%")
|
|
->orWhere('csv_notes', 'LIKE', "%$search->search%")
|
|
->orWhere('ftp_notes', 'LIKE', "%$search->search%")
|
|
->orWhere('file_name', 'LIKE', "%$search->search%");
|
|
});
|
|
}
|
|
|
|
// Apply filter for sync status if provided
|
|
if (isset($search->is_sync) && $search->is_sync !== '') {
|
|
$query->where('is_sync', $search->is_sync == '1');
|
|
}
|
|
|
|
// Apply filter for CSV status if provided
|
|
if (isset($search->is_csv) && $search->is_csv !== '') {
|
|
$query->where('is_csv', $search->is_csv == '1');
|
|
}
|
|
|
|
// Apply filter for FTP status if provided
|
|
if (isset($search->is_ftp) && $search->is_ftp !== '') {
|
|
$query->where('is_ftp', $search->is_ftp == '1');
|
|
}
|
|
}
|
|
|
|
// Apply sorting if provided
|
|
if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) {
|
|
$order = $request->get('sortOrder');
|
|
$column = $request->get('sortField');
|
|
$query->orderBy($column, $order);
|
|
} else {
|
|
// Default sort by created_at descending
|
|
$query->orderBy('created_at', 'desc');
|
|
}
|
|
|
|
// Get the total count of records
|
|
$totalRecords = $query->count();
|
|
|
|
// Apply pagination if provided
|
|
if ($request->has('page') && $request->has('size')) {
|
|
$page = $request->get('page');
|
|
$size = $request->get('size');
|
|
$offset = ($page - 1) * $size;
|
|
|
|
$query->skip($offset)->take($size);
|
|
}
|
|
|
|
// Get the filtered count of records
|
|
$filteredRecords = $query->count();
|
|
|
|
// Get the data for the current page
|
|
$data = $query->get();
|
|
|
|
// Calculate the page count
|
|
$pageCount = ceil($filteredRecords / ($request->get('size') ?: 1));
|
|
|
|
// Calculate the current page number
|
|
$currentPage = $request->get('page') ?: 1;
|
|
|
|
// Return the response data as a JSON object
|
|
return response()->json([
|
|
'draw' => $request->get('draw'),
|
|
'recordsTotal' => $totalRecords,
|
|
'recordsFiltered' => $filteredRecords,
|
|
'pageCount' => $pageCount,
|
|
'page' => $currentPage,
|
|
'totalCount' => $totalRecords,
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Format file size to human readable format
|
|
*/
|
|
private function formatSize($bytes)
|
|
{
|
|
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
|
|
$bytes = max($bytes, 0);
|
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
|
|
$pow = min($pow, count($units) - 1);
|
|
|
|
$bytes /= pow(1024, $pow);
|
|
|
|
return round($bytes, 2) . ' ' . $units[$pow];
|
|
}
|
|
|
|
public function show($id){
|
|
$syncLog = KartuSyncLog::find($id);
|
|
$csvSize = $this->formatSize(File::size($syncLog->csv_path));
|
|
$ftpSize = $this->formatSize(File::size($syncLog->ftp_path));
|
|
|
|
return response()->json($syncLog, 200, ['Content-Type' => 'application/json'], JSON_PRETTY_PRINT);
|
|
}
|
|
|
|
|
|
/**
|
|
* Download file
|
|
*/
|
|
public function downloadFile($id)
|
|
{
|
|
$log = KartuSyncLog::findOrFail($id);
|
|
|
|
// Periksa apakah file sudah dibuat
|
|
if (!$log->is_csv || !$log->file_name) {
|
|
return redirect()->back()->with('error', 'File CSV belum dibuat');
|
|
}
|
|
|
|
// Periksa apakah file ada di lokasi yang ditunjukkan
|
|
if ($log->file_path && File::exists($log->file_path)) {
|
|
return response()->download($log->file_path, $log->file_name);
|
|
}
|
|
|
|
// Jika tidak ada di path spesifik, coba cari di direktori umum
|
|
$path = storage_path('app/biaya_kartu/' . $log->file_name);
|
|
if (File::exists($path)) {
|
|
return response()->download($path, $log->file_name);
|
|
}
|
|
|
|
// Jika masih tidak ditemukan, beri pesan error
|
|
return redirect()->back()->with('error', 'File tidak ditemukan di server');
|
|
}
|
|
}
|