Files
sidata/app/Http/Controllers/LaporanFileController.php
T

210 lines
7.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\LaporanFile;
use App\Models\User;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\File;
class LaporanFileController extends Controller
{
public function __construct(){
$this->siteTitle = "Laporan File";
parent::__construct();
$this->setIconTitle("fa fa-reply");
}
public function index()
{
return view('laporan_file.index');
}
public function create()
{
$this->setSubtitle("Tambah Data");
return view('laporan_file.create');
}
public function store(Request $request)
{
try{
$date = date("Ymd");
DB::beginTransaction();
$req = $request->all();
$LaporanExists = LaporanFile::where("nama_laporan",$req["nama_laporan"])->count();
if ($LaporanExists > 0)
{
return redirect(URL::to("/laporan-file"))
->with('error','Data nama laporan '.$req["nama_laporan"].' sudah ada!');
}
$this->validate($request, [
'file' => 'required|mimes:pdf,xlsx,xls,png,gif,jpg,jpeg',
]);
$data["tahun"] = $req["ta"];
$data["kode_rekening"] = $req["opt_kode_rekening"];
$data["kode_skpd"] = Auth::user()->nrk;
$data["nama_skpd"] = Auth::user()->name;
$data["nama_laporan"] = $req["nama_laporan"];
$file = $request->file('file');
if ($file){
$fileName = $file->getClientOriginalName();
$ext = $file->getClientOriginalExtension();
$NewFileName = $req["nama_laporan"].".".$ext;
$tujuan_upload = config('constants.upload_path_laporan');
File::isDirectory($tujuan_upload) or File::makeDirectory($tujuan_upload, 0777, true, true);
$file->move($tujuan_upload, $NewFileName);
$data["nama_file"] = $NewFileName;
}
LaporanFile::create($data);
DB::commit();
return redirect(URL::to("/laporan-file"))->with('success','Laporan berhasil dibuat!');
}catch(\Exception $e){
DB::rollBack();
return redirect(URL::to("/laporan-file"))->with('error',$e->getMessage());
}
}
public function edit(LaporanFile $laporanFile)
{
$this->setSubtitle("Ubah Data");
return view('laporan_file.edit',compact('laporanFile'));
}
public function update(Request $request, LaporanFile $laporanFile)
{
try{
DB::beginTransaction();
$req = $request->all();
$LaporanExists = LaporanFile::where("nama_laporan",$req["nama_laporan"])->where("id","<>",$laporanFile->id)->count();
if ($LaporanExists > 0)
{
return redirect(URL::to("/laporan-file"))
->with('error','Data nama laporan '.$req["nama_laporan"].' sudah ada!');
}
$this->validate($request, [
'file' => 'required|mimes:pdf,xlsx,xls,png,gif,jpg,jpeg',
]);
$data["tahun"] = $req["ta"];
$data["kode_rekening"] = $req["opt_kode_rekening"];
$data["kode_skpd"] = Auth::user()->nrk;
$data["nama_skpd"] = Auth::user()->name;
$data["nama_laporan"] = $req["nama_laporan"];
$file = $request->file('file');
$tujuan_upload = config('constants.upload_path_laporan');
if ($file){
$fileName = $file->getClientOriginalName();
$ext = $file->getClientOriginalExtension();
$NewFileName = $req["nama_laporan"].".".$ext;
File::isDirectory($tujuan_upload) or File::makeDirectory($tujuan_upload, 0777, true, true);
$file->move($tujuan_upload, $NewFileName);
$data["nama_file"] = $NewFileName;
}else{
$arrName = explode(".",$laporanFile->nama_file);
$ext = end($arrName);
rename($tujuan_upload.$laporanFile->nama_file, $tujuan_upload.$req["nama_laporan"].".".$ext);
$data["nama_file"] = $req["nama_laporan"].".".$ext;
}
$laporanFile->update($data);
DB::commit();
return redirect(URL::to("/laporan-file"))->with('success','Laporan berhasil diubah.');
}catch(Throwable $e){
DB::rollBack();
return redirect(URL::to("/laporan-file"))->with('error','Laporan gagal diubah!');
}
}
public function datatable(Request $request)
{
$post = $request->all();
$offset = $post["start"];
$limit = $post["length"];
$kode_rekening = isset($post["kode_rekening"])?$post["kode_rekening"]:"";
$ta = $post["ta"];
$src = isset($post["search"]["value"]) ? strtolower($post["search"]["value"]):"";
$data = LaporanFile::where("tahun",$ta);
if (getRole()=="skpd"){
$data->where("kode_skpd",Auth::user()->nrk);
}
if($kode_rekening){
$data->where("kode_rekening", $kode_rekening);
}
if (!empty($src))
{
$src = strtolower($src);
//$data->whereRaw("( LOWER(simtrad4_uraian) like '%".$src."%' OR LOWER(simtrad4_sp2d_bun) like '%".$src."%' OR LOWER(rkud_no_sts) like '%".$src."%')");
}
$dataFilter = $data->count();
$dataTotal = LaporanFile::where("tahun",$ta);
if (getRole()=="skpd"){
$dataTotal->where("kode_skpd",Auth::user()->nrk);
}
return response()->json([
"recordsTotal" => $dataTotal->count(),
"recordsFiltered" => $dataFilter,
"data"=> $data->orderBy("created_at","ASC")->skip($offset)->take($limit)->get()
]);
}
public function destroy(LaporanFile $laporanFile)
{
try{
DB::beginTransaction();
$alamatFile = config('constants.upload_path_laporan').$laporanFile->nama_file;
if (file_exists($alamatFile)){
unlink($alamatFile);
}
$laporanFile->delete();
DB::commit();
return redirect(URL::to("/laporan-file"))->with('success','Data Laporan berhasil dihapus!');
}catch(Throwable $e){
DB::rollBack();
return redirect(URL::to("/laporan-file"))->with('error','Data Laporan gagal dihapus.');
}
}
public function show(LaporanFile $laporanFile)
{
$this->setSubtitle("Detail Data");
return view('laporan_file.show',compact("laporanFile"));
}
public function download($kode)
{
$laporanFile = LaporanFile::where("id", $kode)->firstorNew();
$headers = [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment;filename='.$laporanFile->nama_file
];
if ($laporanFile){
return response()->download(config('constants.upload_path_laporan').$laporanFile->nama_file);
}else{
echo "File tidak ditemukan!";
}
}
}