sigd/app/Http/Controllers/LembarPengesahanController.php

373 lines
14 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Services\DashboardAdaptationService;
use App\Services\DashboardMitigationService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Carbon\Carbon;
use App\Models\LembarPengesahan;
use App\Models\KegiatanAdaptasi;
use App\Models\KegiatanMitigasi;
class LembarPengesahanController extends Controller
{
protected $title = 'Lembar Pengesahan';
protected $template = 'modules.pengesahan';
protected $route = 'modules.pengesahan';
public function __construct(DashboardAdaptationService $adaptationService,DashboardMitigationService $mitigationService)
{
$this->adaptationService = $adaptationService;
$this->mitigationService = $mitigationService;
}
/**
* Display a listing of the resource.
*/
public function index()
{
$data['route'] = $this->route;
$data['title'] = $this->title;
return view($this->template.'.index',$data);
}
public function grid()
{
if(auth()->user()->ms_group_id == 1){
$data = LembarPengesahan::with(['agency'])->where('tahun',date('Y'))->get();
}else{
$data = LembarPengesahan::with(['agency'])->where('tahun',date('Y'))->where('agency_id',auth()->user()->ms_agency_id)->get();
}
$_data = [];
foreach ($data as $key => $row) {
if($row->status == 0 || $row->status == 2){
$btn = '<a href="' . url('pengesahan/update/').'/'.encode_id($row->LembarPengesahanId). '" class="btn btn-sm w-100 mb-1 btn-primary">Edit</a>';
$btn .= '<a href="#" data-href="' . url('pengesahan/delete/').'/'.encode_id($row->LembarPengesahanId). '" class="btn btn-sm w-100 mb-1 btn-danger remove_data">Hapus</a>';
}
if(auth()->user()->ms_group_id == 1){
if($row->status == 0){
$btn .= '<a href="#" data-secure="'.encode_id($row->LembarPengesahanId).'" class="btn btn-sm w-100 mb-1 btn-success upload">Upload Berkas</a>';
$btn .= '<a href="#" data-href="' . url('pengesahan/tolak-verifikasi/').'/'.encode_id($row->LembarPengesahanId). '" class="btn btn-sm w-100 mb-1 btn-warning tolak-verifikasi">Tolak Verifikasi</a>';
}
}
$download = '<a class="btn btn-primary btn-sm w-100" href="'.asset('uploads/'.$row->file).'">Download File</a>';
$datanya = '';
if($row->type == 'inventory'){
$sektor = json_decode($row->sektor);
foreach($sektor as $datasek){
$datanya .= '<span class="badge bg-danger w-100">'.$datasek.'</span><br>';
}
}elseif($row->type == 'adaptasi'){
$form = json_decode($row->form_id);
foreach($form as $datasek){
$datanya .= '<span class="badge bg-danger w-100">'.KegiatanAdaptasi::where('id',$datasek)->value('nama_kegiatan').'</span><br>';
}
}elseif($row->type == 'mitigasi'){
$form = json_decode($row->form_id);
foreach($form as $datasek){
$datanya .= '<span class="badge bg-danger w-100">'.KegiatanMitigasi::where('id',$datasek)->value('nama_kegiatan').'</span><br>';
}
}
if ($row->status == 1) {
$status = '<span class="badge bg-success">Sudah Verifikasi</span>';
} elseif ($row->status == 2) {
$status = '<span class="badge bg-danger">Verifikasi Ditolak</span>';
} else {
$status = '<span class="badge bg-danger">Belum Di Verifikasi</span>';
}
$_data[] = [
'no' => $key+1,
'name' => $row->name,
'type' => \Str::title($row->type),
'agency' => $row->agency->name,
'file' => $download,
'data' => @$datanya,
'status' => $status,
'action' => @$btn,
];
}
return response()->json($_data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$year = (int) request()->input('adaptationYear', date('Y'));
$adaptasi = $this->adaptationService->getDashboardData($year);
$mitigasi = $this->mitigationService->getDashboardData($year);
$data['route'] = $this->route;
$data['title'] = $this->title;
$data['kegiatanAdaptasi'] = $adaptasi['tableData'];
$data['kegiatanMitigasi'] = $mitigasi['tableData'];
return view($this->template.'.form',$data);
}
public function update($id = null)
{
$year = (int) request()->input('adaptationYear', date('Y'));
$adaptasi = $this->adaptationService->getDashboardData($year);
$mitigasi = $this->mitigationService->getDashboardData($year);
$keyId = decode_id($id);
$data['title'] = $this->title;
$data['route'] = $this->route;
$data['keyId'] = $id;
$data['kegiatanAdaptasi'] = $adaptasi['tableData'];
$data['kegiatanMitigasi'] = $mitigasi['tableData'];
$data['item'] = LembarPengesahan::find($keyId);
return view($this->template.'.form',$data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$request->validate([
// 'file' => 'required',
// 'type' => 'required',
]);
try {
if(@$request->secure_id){
$ag = LembarPengesahan::find(decode_id(@$request->secure_id));
$ag->keterangan = $request->keterangan;
// if($request->type == 'inventory'){
// $ag->sektor = json_encode($request->sektor);
// }elseif($request->type == 'adaptasi'){
// $ag->form_id = json_encode($request->kegiatan_adaptasi);
// }elseif($request->type == 'mitigasi'){
// $ag->form_id = json_encode($request->kegiatan_mitigasi);
// }
if (@$request->hasFile('file')) {
$file = $request->file('file');
$destinationPath = public_path('uploads/pengesahan');
$current = Carbon::now()->format('Y/m/d');
$path = $destinationPath . '/' . $current;
$fileExtension = $file->getClientOriginalExtension();
$fileSize = $file->getSize();
if($fileExtension != 'pdf'){
return redirect()->back()->with([
'message' => 'Maaf File Harus Berupa PDF!',
'type' => "error"
]);
}
$newFilename = session('id').'_'.uniqid('file_') . '.' . $fileExtension;
if (!File::exists($path)) {
File::isDirectory($path) or File::makeDirectory($path, 0777, true, true);
}
$filePathTim = 'pengesahan/' . $current . '/' . $newFilename;
$uploaded = $file->move($path, $newFilename);
$ag->file = $filePathTim;
}
if(auth()->user()->ms_group_id != 1){
$ag->status = 0;
}
$ag->save();
}else{
$ag = new LembarPengesahan;
$ag->user_id = auth()->user()->id;
$ag->agency_id = auth()->user()->ms_agency_id;
$ag->tahun = date('Y');
// $ag->type = $request->type;
// $ag->keterangan = $request->keterangan;
// if($request->type == 'inventory'){
// $ag->sektor = json_encode($request->sektor);
// }elseif($request->type == 'adaptasi'){
// $ag->form_id = json_encode($request->kegiatan_adaptasi);
// }elseif($request->type == 'mitigasi'){
// $ag->form_id = json_encode($request->kegiatan_mitigasi);
// }
if (@$request->hasFile('file')) {
$file = $request->file('file');
$destinationPath = public_path('uploads/pengesahan');
$current = Carbon::now()->format('Y/m/d');
$path = $destinationPath . '/' . $current;
$fileExtension = $file->getClientOriginalExtension();
$fileSize = $file->getSize();
if($fileExtension != 'pdf'){
return redirect()->back()->with([
'message' => 'Maaf File Harus Berupa PDF!',
'type' => "error"
]);
}
$newFilename = session('id').'_'.uniqid('file_') . '.' . $fileExtension;
if (!File::exists($path)) {
File::isDirectory($path) or File::makeDirectory($path, 0777, true, true);
}
$filePathTim = 'pengesahan/' . $current . '/' . $newFilename;
$uploaded = $file->move($path, $newFilename);
}
$ag->file = $filePathTim;
$ag->status = 0;
$ag->save();
}
return redirect()->route($this->route.'.index')->with('success', 'Lembar Pengesahan berhasil ditambahkan.');
} catch (\Exception $e) {
return back()->withErrors(['error' => 'Lembar Pengesahan gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]);
}
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
$data['ar'] = LembarPengesahan::find($id);
$data['route'] = $this->route;
$data['title'] = $this->title;
return view($this->template.'.form', $data);
}
public function verifikasi($id)
{
$ag = LembarPengesahan::find(decode_id(@$id));
$ag->status = 1;
$ag->save();
return redirect()->route($this->route.'.index')->with('success', 'Lembar Pengesahan berhasil diupdate.');
}
public function tolakverifikasi($id)
{
$ag = LembarPengesahan::find(decode_id(@$id));
$ag->status = 2;
$ag->save();
return redirect()->route($this->route.'.index')->with('success', 'Lembar Pengesahan berhasil diupdate.');
}
public function delete($id)
{
$ag = LembarPengesahan::find(decode_id(@$id));
$ag->delete();
return redirect()->route($this->route.'.index')->with('success', 'Lembar Pengesahan berhasil diupdate.');
}
/**
* Update the specified resource in storage.
*/
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
function generate(Request $request) {
// dd($request->all());
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor(asset('assets/dokumen_pengesahan.docx'));
$templateProcessor->setValue('tahun_dokumen', date('Y'));
$templateProcessor->setValue('tahun_data', date('Y')-1);
$templateProcessor->setValue('pengampu_data', $request->nama_instansi);
$templateProcessor->setValue('sektor', \Str::title($request->type));
$templateProcessor->setValue('jenis_data', $request->jenis_data);
if($request->type == 'mitigasi'){
$templateProcessor->setValue('sumber_data', sumberData($request->kegiatan_mitigasi,$request->type));
}elseif($request->type == 'adaptasi'){
$templateProcessor->setValue('sumber_data', sumberData($request->kegiatan_adaptasi,$request->type));
}elseif($request->type == 'inventory'){
$templateProcessor->setValue('sumber_data', sumberData($request->sektor,$request->type));
}
$templateProcessor->setValue('kepala_pengampu_data', $request->kepala_pengampu_data);
$templateProcessor->setValue('nama_instansi', $request->nama_instansi);
// Simpan hasil sementara
// $tempDocx = public_path('123.docx');
// Buat file temporary
$tempFile = tempnam(sys_get_temp_dir(), 'dokumen_') . '.docx';
$templateProcessor->saveAs($tempFile);
// Nama file saat didownload
$downloadName = 'dokumen_pengesahan_' . date('YmdHis') . '.docx';
return response()
->download($tempFile, $downloadName)
->deleteFileAfterSend(true);
}
function upload(Request $request){
if (@$request->hasFile('file')) {
$file = $request->file('file');
$destinationPath = public_path('uploads/pengesahan');
$current = Carbon::now()->format('Y/m/d');
$path = $destinationPath . '/' . $current;
$fileExtension = $file->getClientOriginalExtension();
$fileSize = $file->getSize();
if($fileExtension != 'pdf'){
return redirect()->back()->with([
'message' => 'Maaf File Harus Berupa PDF!',
'type' => "error"
]);
}
$newFilename = session('id').'_'.uniqid('file_') . '.' . $fileExtension;
if (!File::exists($path)) {
File::isDirectory($path) or File::makeDirectory($path, 0777, true, true);
}
$filePathTim = 'pengesahan/' . $current . '/' . $newFilename;
$uploaded = $file->move($path, $newFilename);
$ag = LembarPengesahan::find(decode_id(@$request->secure_id));
$ag->file = $filePathTim;
// if(auth()->user()->ms_group_id != 1){
$ag->status = 1;
// }
$ag->save();
return redirect()->route($this->route.'.index')->with('success', 'Lembar Pengesahan berhasil diverifikasi.');
}else{
return back()->withErrors(['error' => 'Lembar Pengesahan gagal disimpan. Silakan coba lagi']);
}
}
}