Anons79 Mini Shell

Directory : /home/aplikasiposinfo/.trash/app/Http/Controllers/
Upload File :
Current File : /home/aplikasiposinfo/.trash/app/Http/Controllers/PermintaanIzinController.php

<?php

namespace App\Http\Controllers;

use App\Helpers\Helper;
use App\Jobs\NotifikasiWa;
use App\Models\Absensi;
use App\Models\Departemen;
use App\Models\Izin;
use App\Models\Perusahaan;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class PermintaanIzinController extends BaseController
{
    public $namaMenu = 'izin';
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        $this->cekAkses();
        $datas = Izin::with('karyawan')->where('status','pending')->has('karyawan')->filtersInput([
            'keterangan',
            'status',
            [
                'karyawan'=>[
                    'nama',
                    'nik'
                ],
            ]
        ],'search');
        
        $perusahaans = Perusahaan::all();
        $departemens = Departemen::all();
        $user = auth()->user()->load(['perusahaans','departemen']);
        $departemen_ids = $user->departemen->pluck('id')->toArray();
        if (count($departemen_ids) > 0) {
            $datas = $datas->whereHas('karyawan', function ($q) use ($departemen_ids) {
                $q->whereHas("departemen", function ($qd) use ($departemen_ids) {
                    $qd->whereIn('id', $departemen_ids);
                });
            });

            $departemens = Departemen::whereIn('id',$departemen_ids)->get();
        }

        // if ($user->perusahaan_id != null) {
        //     $datas = $datas->whereHas('karyawan', function ($q) use ($user) {
        //         $q->where('perusahaan_id', $user->perusahaan_id);
        //     });
        // }

        if (count($user->perusahaans) > 0) {
            $datas = $datas->whereHas('karyawan', function ($q) use ($user) {
                $q->whereHas('perusahaans',function($w)use($user){
                    $w->whereIn('perusahaan_id', $user->perusahaans->pluck('id')->toArray());
                });
            });
            $perusahaans = $user->perusahaans;
        }

        if (request()->has('perusahaan_id') && (int) request()->perusahaan_id != 0) {
            $datas = $datas->whereHas('karyawan', function ($q) {
                $q->whereHas('perusahaans',function($w){
                    $w->where('perusahaan_id',request()->perusahaan_id);
                });
            });
        }

        if (request()->has('departemen_id') && (int) request()->departemen_id != 0) {
            $datas = $datas->whereHas('karyawan', function ($q) {
                $q->whereHas("departemen", function ($qd) {
                    $qd->where('id', request()->departemen_id);
                });
            });
        }

        if (request()->has('periode_awal') && request()->periode_awal != "") {
            $datas = $datas->whereDate('tanggal_izin','>=', date("Y-m-d",strtotime(request()->periode_awal)));
        }

        if (request()->has('periode_akhir') && request()->periode_akhir != "") {
            $datas = $datas->whereDate('tanggal_izin','<=', date("Y-m-d",strtotime(request()->periode_akhir)));
        }


        $datas = $datas->orderBy('created_at','DESC')->simplePaginate(10)->appends(request()->query());

        return view('permintaan-izin.index', compact('datas','perusahaans','departemens'),['akses'=>$this->akses]);
    }

   

    /**
     * Remove the specified resource from storage.
     * 
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        DB::beginTransaction();
        try {
            $data = Izin::findOrFail($id);
            $data->delete();
            Helper::addUserLog(auth()->user()->nama." menghapus permintaan izin : ".$data->tanggal_izin.",".$data->karyawan->nama);
            DB::commit();
        } catch (\Throwable $th) {
            DB::rollBack();
            Log::error($th);
            return redirect()->back()->withErrors([
                'error'=> 'Data gagal dihapus'.((env('APP_DEBUG') == true)?' : ' . $th->getMessage():'')
            ]);
        }
        return redirect()->route('permintaan-izin.index')->with('success', 'Data berhasil dihapus');
    }

    public function lampiran($id) {
        $data = Izin::findOrFail($id);
        return response()->file(storage_path('app/'.$data->lampiran));
    }

    public function status($id,Request $request)
    {
        DB::beginTransaction();
        try {
            $data = Izin::findOrFail($id);
            $data->update([
                'status'=>$request->status,
                'verifikator_id'=>auth()->user()->id,
                'tanggapan_verifikator'=>$request->tanggapan_verifikator,
            ]);
            $karyawan = $data->karyawan;
            if($request->status == "accept"){
                $absensi = Absensi::where([
                    'karyawan_id'=>$data->karyawan_id,
                    'tanggal_absen'=>$data->tanggal_izin,
                   
                ])->first();
                if(!$absensi){
                    $absensi = Absensi::create([
                        'karyawan_id'=>$data->karyawan_id,
                        'tanggal_absen'=>$data->tanggal_izin,
                        'status_absen'=>$data->jenis_izin
                       
                    ]);                    
                }

                $data->update([
                    'absensi_id'=>$absensi->id
                ]);

                if ($karyawan->no_hp != null && env('APP_ENV') == 'production') {
                    dispatch(new NotifikasiWa($karyawan->no_hp, env("APP_NAME") . " : Permintaan Izin *$data->jenis_izin_label* anda untuk tanggal *" . date("d-m-Y", strtotime($data->tanggal_izin)) ."* telah *Disetujui* dengan tanggapan *".$request->tanggapan_verifikator."*. informasi izin bisa langsung menghubungi nomor +6281345927722 untuk penjelasan lebih lanjut"));
                    
                }
            } else {
                if ($karyawan->no_hp != null && env('APP_ENV') == 'production') {
                    dispatch(new NotifikasiWa($karyawan->no_hp, env("APP_NAME") . " : Permintaan Izin *$data->jenis_izin_label* anda untuk tanggal *" . date("d-m-Y", strtotime($data->tanggal_izin)) ."* telah *Ditolak* dengan tanggapan *".$request->tanggapan_verifikator."*. informasi izin bisa langsung menghubungi nomor +6281345927722 untuk penjelasan lebih lanjut"));
                    
                }
            }
            
           
            Helper::addUserLog(auth()->user()->nama." mengubah status permintaan izin : ".$data->tanggal_izin.",".$data->karyawan->nama.",".$request->status);
            DB::commit();
        } catch (\Throwable $th) {
            DB::rollBack();
            Log::error($th);
            return response()->json([
                'status'=>'error',
                'message'=> 'Data gagal diubah'.((env('APP_DEBUG') == true)?' : ' . $th->getMessage():'')
            ]);
        }
        return response()->json([
            'status'=>'success',
            'message'=> 'Data berhasil diubah'
        ]);
    }
}

Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]