256 lines
8.7 KiB
PHP
256 lines
8.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\Lpj\Http\Controllers;
|
|
|
|
use Throwable;
|
|
use Illuminate\Http\Request;
|
|
use Modules\Lpj\Models\KJPP;
|
|
use Illuminate\Http\Response;
|
|
use Modules\Lpj\Models\Branch;
|
|
use Modules\Location\Models\City;
|
|
use Modules\Lpj\Models\IjinUsaha;
|
|
use Modules\Lpj\Exports\KJPPExport;
|
|
use App\Http\Controllers\Controller;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use Modules\Location\Models\Village;
|
|
use Modules\Lpj\Models\JenisJaminan;
|
|
use Modules\Location\Models\District;
|
|
use Modules\Location\Models\Province;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\Lpj\Http\Requests\KJPPRequest;
|
|
|
|
class KJPPController extends Controller
|
|
{
|
|
public $user;
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
return view('lpj::kjpp.index');
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
$branch = Branch::all();
|
|
$ijin_usaha = IjinUsaha::all();
|
|
$jenis_aset = JenisJaminan::all();
|
|
$provinces = Province::all();
|
|
|
|
// Generate KJPP Number
|
|
$lastKjpp = KJPP::orderBy('code', 'desc')->first();
|
|
$nextNumber = $lastKjpp ? intval(substr($lastKjpp->code, 1, 6)) + 1 : 1;
|
|
$kjppNumber = 'K' . str_pad($nextNumber, 6, '0', STR_PAD_LEFT);
|
|
|
|
// Combine KJPP number with branch code
|
|
$fullKjppNumber = $kjppNumber;
|
|
|
|
return view('lpj::kjpp.create', compact('branch', 'ijin_usaha', 'jenis_aset', 'provinces', 'fullKjppNumber'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(KJPPRequest $request)
|
|
{
|
|
$validated = $request->validated();
|
|
|
|
if ($validated) {
|
|
$file = $request->file('attachment');
|
|
$filename = $file ? time() . '.' . $file->getClientOriginalExtension() : 'default.pdf';
|
|
|
|
if ($file) {
|
|
// Simpan file yang diunggah
|
|
$file->storeAs('public/uploads_pdf', $filename);
|
|
} else {
|
|
// Salin file default ke lokasi yang diinginkan
|
|
Storage::copy('public/test/default.pdf', 'public/uploads_pdf/' . $filename);
|
|
}
|
|
|
|
$validated['ijin_usaha_id'] = json_encode($request->input('ijin_usaha_id'));
|
|
$validated['jenis_aset_id'] = json_encode($request->input('jenis_aset_id'));
|
|
|
|
// Tambahkan nama file ke data yang divalidasi
|
|
$validated['attachment'] = $filename;
|
|
|
|
// Simpan data ke database
|
|
KJPP::create($validated);
|
|
|
|
return redirect()
|
|
->route('basicdata.kjpp.index')
|
|
->with('success', 'KJPP created successfully');
|
|
} else {
|
|
return redirect()
|
|
->route('basicdata.kjpp.create')
|
|
->with('error', 'Validation failed');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$kjpp = KJPP::find($id);
|
|
$ijin_usaha = IjinUsaha::where('code', $kjpp->nomor_ijin_usaha)->get();
|
|
$ijin_usahas = IjinUsaha::all();
|
|
$jenis_jaminan = JenisJaminan::all();
|
|
$branches = Branch::where('name', $kjpp->jenis_kantor)->get();
|
|
$provinces = Province::where('code', $kjpp->province_code)->get();
|
|
$cities = City::where('code', $kjpp->city_code)->get();
|
|
$districts = District::where('code', $kjpp->district_code)->get();
|
|
$villages = Village::where('code', $kjpp->village_code)->get();
|
|
// dd($branches);
|
|
return view('lpj::kjpp.show', compact('jenis_jaminan', 'ijin_usahas', 'ijin_usaha', 'branches', 'kjpp', 'provinces', 'cities', 'districts', 'villages'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$kjpp = KJPP::find($id);
|
|
$branch = Branch::all();
|
|
$ijin_usaha = IjinUsaha::all();
|
|
$jenis_aset = JenisJaminan::all();
|
|
$provinces = Province::all();
|
|
$cities = City::where('province_code', $kjpp->province_code)->get();
|
|
$districts = District::where('city_code', $kjpp->city_code)->get();
|
|
$villages = Village::where('district_code', $kjpp->district_code)->get();
|
|
|
|
return view('lpj::kjpp.create', compact('kjpp', 'branch', 'ijin_usaha', 'jenis_aset', 'provinces', 'cities', 'districts', 'villages'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(KJPPRequest $request, $id)
|
|
{
|
|
$validated = $request->validated();
|
|
|
|
if ($validated) {
|
|
$file = $request->file('attachment');
|
|
$filename = $file ? time() . '.' . $file->getClientOriginalExtension() : null;
|
|
|
|
if ($file !== null) {
|
|
// Jika ada file dari database maka hapus file yang lama ke file yang baru
|
|
$kjpp = KJPP::find($id);
|
|
// Jika filenya ada default.pdf jangan dihapus
|
|
if ($kjpp->attachment !== 'default.pdf') {
|
|
Storage::delete('public/uploads_pdf/' . $kjpp->attachment);
|
|
}
|
|
// Simpan file yang diunggah
|
|
$file->storeAs('public/uploads_pdf', $filename);
|
|
$validated['attachment'] = $filename;
|
|
} else {
|
|
// Jika tidak ada file yang diunggah, gunakan file yang sudah ada atau file default
|
|
$kjpp = KJPP::find($id);
|
|
$validated['attachment'] = $kjpp->attachment ?? 'default.pdf';
|
|
}
|
|
|
|
// Perbarui data di database
|
|
KJPP::where('id', $id)->update($validated);
|
|
|
|
return redirect()
|
|
->route('basicdata.kjpp.index')
|
|
->with('success', 'KJPP updated successfully');
|
|
} else {
|
|
return redirect()
|
|
->route('basicdata.kjpp.edit', $id)
|
|
->with('error', 'Validation failed');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$kjpp = KJPP::find($id);
|
|
|
|
// Jangan hapus file default.pdf
|
|
if ($kjpp->attachment && $kjpp->attachment !== 'default.pdf') {
|
|
Storage::delete('public/uploads_pdf/' . $kjpp->attachment);
|
|
}
|
|
|
|
// Hapus data dari database
|
|
$kjpp->delete();
|
|
|
|
echo json_encode(['success' => true, 'message' => 'KJPP deleted successfully']);
|
|
} catch (Throwable $e) {
|
|
echo json_encode(['success' => false, 'message' => 'Failed to delete branch: ' . $e]);
|
|
}
|
|
}
|
|
|
|
public function dataForDatatables(Request $request)
|
|
{
|
|
if (is_null($this->user) || !$this->user->can('kjpp.view')) {
|
|
//abort(403, 'Sorry! You are not allowed to view users.');
|
|
}
|
|
|
|
// Retrieve data from the database
|
|
$query = KJPP::query();
|
|
|
|
// Apply search filter if provided
|
|
if ($request->has('search') && !empty($request->get('search'))) {
|
|
$search = $request->get('search');
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('code', 'LIKE', "%$search%");
|
|
$q->orWhere('name', 'LIKE', "%$search%");
|
|
$q->orWhere('jenis_kantor', 'LIKE', "%$search%");
|
|
});
|
|
}
|
|
|
|
// Apply sorting if provided
|
|
if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) {
|
|
$order = $request->get('sortOrder');
|
|
$column = $request->get('sortField');
|
|
$query->orderBy($column, $order);
|
|
}
|
|
|
|
// 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; // Calculate the offset
|
|
|
|
$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($totalRecords / $request->get('size'));
|
|
|
|
// Calculate the current page number
|
|
$currentPage = 0 + 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,
|
|
]);
|
|
}
|
|
|
|
public function export()
|
|
{
|
|
return Excel::download(new KJPPExport, 'kjpp.xlsx');
|
|
}
|
|
}
|