lundi 12 octobre 2020

Laravel 5.2 - SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;

I got this error when i run edit method.

Laravel 5.2 - SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '0' at line 1 (SQL: select * from q_alat offset 0)

here is the controller:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Input;
use DB;
use Redirect;
use Auth;
use View;
use App\Alat;
use App\Jenis_alat;
use App\Plant;
use App\Departement;
use App\Workcenter;
use App\Cadangan_alat;
use App\Alat_baru;
use App\Request_nonaktif;
use App\Permohonan;
use App\file_standard;
use App\kategori;
use Response;

class Crudcontroller extends Controller
{
    public function index(){
        $data = DB::table('q_alat')->get();
        $jenis_alat = Jenis_alat::get();
        $departement = Departement::get();
        $plant = Plant::get();
        $workcenter = Workcenter::get();
        $kategori = kategori::get();
        $kalibrator = Alat::where('kategori','=','standard')->get();
        return view('akun/admin_0/alat/index')
        ->with('jenis_alat',$jenis_alat)
        ->with('q_alat',$data)
        ->with('departement',$departement)
        ->with('plant',$plant)
        ->with('workcenter',$workcenter)
        ->with('kategori',$kategori)
        ->with('kalibrator',$kalibrator)
        ->with('sort','1');
    }

public function search(Request $request){

    $query = $request->get('q');
    $hasil =DB::table('q_alat')->where('no_inventaris', 'LIKE', '%' . $query . '%')->paginate(10);

    return view('akun/admin_0/alat/result', compact('hasil', 'query'))
    ->with('sort','1')
    ->with('edit','');

}

public function page(){
    $page = session('alatPage');
    if ($page > 0) {
        
    }else{
        $page = 25;
    }
    $data = DB::table('q_alat')->paginate($page);
    $jenis_alat = Jenis_alat::get();
    $departement = Departement::get();
    $plant = Plant::get();
    $kategori = kategori::get();
    $workcenter = Workcenter::get();
    $kalibrator = Alat::where('kategori','=','standard')->get();
    return view('akun/admin_0/alat/index')
    ->with('jenis_alat',$jenis_alat)
    ->with('q_alat',$data)
    ->with('departement',$departement)
    ->with('plant',$plant)
    ->with('workcenter',$workcenter)
    ->with('kategori',$kategori)
    ->with('kalibrator',$kalibrator)
    ->with('page',$page)
    ->with('sort','1')
    ->with('edit','');
}

public function pilihPage(Request $request){
    session(['alatPage' => $request->input_page]);
    return redirect('/alat');
}

public function store(Request $request){
    $alat = new Alat;

    //buat variable dari inputan
    $get_jenis_alat = $request->input_jenis_alat;
    $get_no_urut = $request->input_no_urut;
    $get_departement = $request->input_departement;
    $get_plant = $request->input_plant;
    $get_tahun = $request->input_tahun;
    $get_workcenter = $request->input_workcenter;
    $get_kategori = $request->input_kategori;
    $get_merk = $request->input_merk;
    $get_no_seri = $request->input_no_seri;
    $get_no_asset = $request->input_no_asset;
    $get_tipe_alat = $request->input_tipe_alat;
    $get_durasi = $request->input_durasi;
    $get_tanggal_awal = $request->input_tanggal_awal;
    $get_jangkauan_pemakaian = $request->input_jangkauan_pemakaian;
    $get_kapasitas = $request->input_kapasitas;
    $get_resolusi = $request->input_resolusi;
    $get_kalibrator = $request->input_kalibrator;

    //buat kode alat
    $jenis = Jenis_alat::where('jenis_alat','=',$get_jenis_alat)->get();
    foreach ($jenis as $jenis1) {
        $kode_alat = $jenis1->kode_alat;    
    }

    //buat id departement
    $sql_departement =  Departement::where('nama_departement','=',$get_departement)->get();
    foreach ($sql_departement as $row) {
        $dept = $row->id_departement;
    }

    //buat id plant dan buat kode lokasi
    $sql_plant = Plant::where('nama_plant','=',$get_plant)->get();
    foreach ($sql_plant as $row) {
        $plant =  $row->id_plant;
    }
    if ($dept<10) {
        $dept1 = "0".$dept;
    }else{
        $dept1 = $dept;
    }
    $kode_lokasi = $dept1 ."". $plant ;

    //ambil tahun
    $tahun =$get_tahun;
    $tahun1 = substr($tahun, -2);

    //buat id workcenter
    $sql_get_workcenter = Workcenter::where('nama_workcenter','=',$get_workcenter)->get();
    foreach ($sql_get_workcenter as $row) {
        $get_workcenter = $row->id_workcenter;
    }

    //buat no urut
    $q_max = Alat::orderby('no_urut','desc')->where('kode_alat','=',$kode_alat)->where('kode_lokasi','=',$kode_lokasi)->first();
    // foreach ($q_max as $row) {
    //  $max1 = $row->no_urut;
    // }
    if ($q_max != '') {
        $max1 = $q_max->no_urut;
        $max2 = $max1 + 1;  
    }else{
        $max2 = 1;
    }
    $no_urut= $get_no_urut;
    if ($no_urut!="") {
        if ($no_urut < 10) {
            $no_urut1 = "00".$no_urut;
        }elseif ($no_urut < 100) {
            $no_urut1 = "0".$no_urut;
        }else{
            $no_urut1 = $no_urut;
        }   
    }else{
        if ($max2 < 10) {
            $no_urut1 = "00".$max2;
        }elseif ($max2 < 100) {
            $no_urut1 = "0".$max2;
        }else{
            $no_urut1 = $max2;
        }   
    }

    //cek duplikasi data dari inputan merk,tipe,nomor seri,nomor asset
    $cekDuplikat = Alat::where('merk_alat','=',$get_merk)->where('tipe_alat','=',$get_tipe_alat)->where('no_seri','=',$get_no_seri)->where('no_asset','=',$get_no_asset)->count();
    if ($cekDuplikat > 0) {
        return Redirect::to('/alat')->with('message','Merk alat, tipe alat, nomor seri, dan nomor asset sudah digunakan, silahkan masukan data lainnya!')->with('sort','1');
        //echo "<script>alert('Merk alat, tipe alat, nomor seri, dan nomor asset sudah digunakan, silahkan masukan data lainnya!');</script>";
    }else{

    //cek no urut
        if ($no_urut != "") {
            $cek_no_urut = Alat::where('no_urut','=',$no_urut)->where('kode_alat','=',$kode_alat)->where('kode_lokasi','=',$kode_lokasi)->count();
            if ($cek_no_urut > 0) {
                return Redirect::to('/alat')->with('message','No urut sudah digunakan, silahkan masukan nomor urut lainnya atau kosongkan!')->with('sort','1');
                //echo "<script>alert('No urut sudah digunakan, silahkan masukan nomor urut lainnya atau kosongkan!');</script>";
            }else{
            //generate no inventaris
                $table="tbl_alat";
                if ($get_kategori== "standard") {
                    $inventaris=$no_urut1."/".$kode_alat."-SK"."/".$kode_lokasi."/".$tahun1;
                }else{
                    $inventaris=$no_urut1."/".$kode_alat."/".$kode_lokasi."/".$tahun1;  
                }

            //insert
                $alat->no_inventaris = $inventaris;
                $alat->no_urut = $no_urut1;
                $alat->kode_alat = $kode_alat;
                $alat->kode_lokasi=$kode_lokasi;
                $alat->tahun = $tahun;
                $alat->merk_alat= $get_merk;
                $alat->no_seri= $get_no_seri;
                $alat->no_asset= $get_no_asset;
                $alat->kategori= $get_kategori;
                $alat->tipe_alat= $get_tipe_alat;
                $alat->id_workcenter= $get_workcenter;
                $alat->durasi= $get_durasi;
                $alat->tanggal_awal= $get_tanggal_awal;
                $alat->jangkauan_pemakaian= $get_jangkauan_pemakaian;
                $alat->kapasitas= $get_kapasitas;
                $alat->resolusi= $get_resolusi;
                $alat->kalibrator= $get_kalibrator;

                $alat->save();

                return Redirect::to('/alat')->with('message','Data alat berhasil diinput')->with('sort','1');
            }
        }else{
        //generate no inventaris
            $table="tbl_alat";
            if ($get_kategori== "standard") {
                $inventaris=$no_urut1."/".$kode_alat."-SK"."/".$kode_lokasi."/".$tahun1;
            }else{
                $inventaris=$no_urut1."/".$kode_alat."/".$kode_lokasi."/".$tahun1;  
            }

            //insert
            $alat->no_inventaris = $inventaris;
            $alat->no_urut = $no_urut1;
            $alat->kode_alat = $kode_alat;
            $alat->kode_lokasi=$kode_lokasi;
            $alat->tahun = $tahun;
            $alat->merk_alat= $get_merk;
            $alat->no_seri= $get_no_seri;
            $alat->no_asset= $get_no_asset;
            $alat->kategori= $get_kategori;
            $alat->tipe_alat= $get_tipe_alat;
            $alat->id_workcenter= $get_workcenter;
            $alat->durasi= $get_durasi;
            $alat->tanggal_awal= $get_tanggal_awal;
            $alat->jangkauan_pemakaian= $get_jangkauan_pemakaian;
            $alat->kapasitas= $get_kapasitas;
            $alat->resolusi= $get_resolusi;
            $alat->kalibrator= $get_kalibrator;

            $alat->save();

            return Redirect::to('/alat')->with('message','Data alat berhasil diinput')->with('sort','1');
        }
    }
}

public function edit($no1,$no2,$no3,$no4){
    $page = session('alatPage');
    $no_inventaris = $no1."/".$no2."/".$no3."/".$no4;
    $data = DB::table('q_alat')->paginate($page);
    $jenis_alat = Jenis_alat::get();
    $departement = Departement::get();
    $plant = Plant::get();
    $kategori = kategori::get();
    $workcenter = Workcenter::get();
    $kalibrator = Alat::where('kategori','=','standard')->get();
    $edit = DB::table('q_alat')->where('no_inventaris','=',$no_inventaris)->first();
    $get_kode = $edit->kode_lokasi;
    $id_plant = substr($get_kode, 2);
    $id_departement = substr($get_kode, 0,2);
    $edit_plant = Plant::where('id_plant','=',$id_plant)->first();
    $edit_departement = Departement::where('id_departement','=',$id_departement)->first();
    return view('/akun/admin_0/alat/index')
    ->with('jenis_alat',$jenis_alat)
    ->with('q_alat',$data)
    ->with('departement',$departement)
    ->with('plant',$plant)
    ->with('workcenter',$workcenter)
    ->with('kategori',$kategori)
    ->with('kalibrator',$kalibrator)
    ->with('edit_plant',$edit_plant)
    ->with('edit_departement',$edit_departement)
    ->with('edit',$edit)
    ->with('page',$page)
    ->with('sort','1');
    

    // $data = DB::table('q_alat')->paginate($page);
    // $jenis_alat = Jenis_alat::get();
    // $departement = Departement::get();
    // $plant = Plant::get();
    // $workcenter = Workcenter::get();
    // $kalibrator = Alat::where('kategori','=','standard')->get();
    // return view('akun/admin_0/alat/index')
    // ->with('jenis_alat',$jenis_alat)
    // ->with('q_alat',$data)
    // ->with('departement',$departement)
    // ->with('plant',$plant)
    // ->with('workcenter',$workcenter)
    // ->with('kalibrator',$kalibrator)
    // ->with('page',$page)
    // ->with('sort','1')
    // ->with('edits','');
}

public function update(Request $request){
    $alat = new Alat;

    //buat variable dari inputan
    $get_no_inventaris = $request->input_inventaris;
    $get_jenis_alat = $request->input_jenis_alat;
    $get_no_urut = $request->input_no_urut;
    $get_departement = $request->input_departement;
    $get_plant = $request->input_plant;
    $get_tahun = $request->input_tahun;
    $get_workcenter = $request->input_workcenter;
    $get_kategori = $request->input_kategori;
    $get_merk = $request->input_merk;
    $get_no_seri = $request->input_no_seri;
    $get_no_asset = $request->input_no_asset;
    $get_tipe_alat = $request->input_tipe_alat;
    $get_durasi = $request->input_durasi;
    $get_tanggal_awal = $request->input_tanggal_awal;
    $get_jangkauan_pemakaian = $request->input_jangkauan_pemakaian;
    $get_kapasitas = $request->input_kapasitas;
    $get_resolusi = $request->input_resolusi;
    $get_kalibrator = $request->input_kalibrator;

    //buat kode alat
    $jenis = Jenis_alat::where('jenis_alat','=',$get_jenis_alat)->get();
    foreach ($jenis as $jenis1) {
        $kode_alat = $jenis1->kode_alat;    
    }

    //buat id departement
    $sql_departement =  Departement::where('nama_departement','=',$get_departement)->get();
    foreach ($sql_departement as $row) {
        $dept = $row->id_departement;
    }

    //buat id plant dan buat kode lokasi
    $sql_plant = Plant::where('nama_plant','=',$get_plant)->get();
    foreach ($sql_plant as $row) {
        $plant =  $row->id_plant;
    }
    if ($dept<10) {
        $dept1 = "0".$dept;
    }else{
        $dept1 = $dept;
    }
    $kode_lokasi = $dept1 ."". $plant ;

    //ambil tahun
    $tahun =$get_tahun;
    $tahun1 = substr($tahun, -2);

    //buat id workcenter
    $sql_get_workcenter = Workcenter::where('nama_workcenter','=',$get_workcenter)->get();
    foreach ($sql_get_workcenter as $row) {
        $get_workcenter = $row->id_workcenter;
    }

    //buat no urut
    $q_max = Alat::orderby('no_urut','desc')->where('kode_alat','=',$kode_alat)->where('kode_lokasi','=',$kode_lokasi)->first();
    // foreach ($q_max as $row) {
    //  $max1 = $row->no_urut;
    // }
    if ($q_max != '') {
        $max1 = $q_max->no_urut;
        $max2 = $max1 + 1;  
    }else{
        $max2 = 1;
    }
    $no_urut= $get_no_urut;
    if ($no_urut!="") {
        if ($no_urut < 10) {
            $no_urut1 = "00".$no_urut;
        }elseif ($no_urut < 100) {
            $no_urut1 = "0".$no_urut;
        }else{
            $no_urut1 = $no_urut;
        }   
    }else{
        if ($max2 < 10) {
            $no_urut1 = "00".$max2;
        }elseif ($max2 < 100) {
            $no_urut1 = "0".$max2;
        }else{
            $no_urut1 = $max2;
        }   
    }

    $get_no_urut2 = Alat::where('no_inventaris','=',$get_no_inventaris)->first();
    $get_no_urut3 = $get_no_urut2->no_urut;

    //cek no urut
    if ($no_urut != "") {
        $cek_no_urut = Alat::where('no_urut','=',$no_urut)->where('no_urut','!=',$get_no_urut3)->where('kode_alat','=',$kode_alat)->where('kode_lokasi','=',$kode_lokasi)->count();
        if ($cek_no_urut > 0) {
            return Redirect('/alat')->with('sort','1');
            echo "<script>alert('No urut sudah digunakan, silahkan masukan nomor urut lainnya atau kosongkan!');</script>";
        }else{
            //generate no inventaris
            $table="tbl_alat";
            if ($get_kategori== "standard") {
                $inventaris=$no_urut1."/".$kode_alat."-SK"."/".$kode_lokasi."/".$tahun1;
            }else{
                $inventaris=$no_urut1."/".$kode_alat."/".$kode_lokasi."/".$tahun1;  
            }

            //update
            Alat::find($get_no_inventaris)->update([
                'no_inventaris'=> $inventaris,
                'no_urut'=> $no_urut1,
                'kode_alat'=> $kode_alat,
                'kode_lokasi'=>$kode_lokasi,
                'tahun'=> $tahun,
                'merk_alat'=> $get_merk,
                'no_seri'=> $get_no_seri,
                'no_asset'=> $get_no_asset,
                'kategori'=> $get_kategori,
                'tipe_alat'=> $get_tipe_alat,
                'id_workcenter'=> $get_workcenter,
                'durasi'=> $get_durasi,
                'tanggal_awal'=> $get_tanggal_awal,
                'jangkauan_pemakaian'=> $get_jangkauan_pemakaian,
                'kapasitas'=> $get_kapasitas,
                'resolusi'=> $get_resolusi,
                'kalibrator'=> $get_kalibrator
            ]);
            return Redirect::to('/alat')->with('message','Data berhasil diupdate')->with('sort','1');
        }
    }else{
        //generate no inventaris
        $table="tbl_alat";
        if ($get_kategori== "standard") {
            $inventaris=$no_urut1."/".$kode_alat."-SK"."/".$kode_lokasi."/".$tahun1;
        }else{
            $inventaris=$no_urut1."/".$kode_alat."/".$kode_lokasi."/".$tahun1;  
        }

            //insert
        Alat::find($get_no_inventaris)->update([
            'no_inventaris'=> $inventaris,
            'no_urut'=> $no_urut1,
            'kode_alat'=> $kode_alat,
            'kode_lokasi'=>$kode_lokasi,
            'tahun'=> $tahun,
            'merk_alat'=> $get_merk,
            'no_seri'=> $get_no_seri,
            'no_asset'=> $get_no_asset,
            'kategori'=> $get_kategori,
            'tipe_alat'=> $get_tipe_alat,
            'id_workcenter'=> $get_workcenter,
            'durasi'=> $get_durasi,
            'tanggal_awal'=> $get_tanggal_awal,
            'jangkauan_pemakaian'=> $get_jangkauan_pemakaian,
            'kapasitas'=> $get_kapasitas,
            'resolusi'=> $get_resolusi,
            'kalibrator'=> $get_kalibrator
        ]);
        return Redirect::to('/alat')->with('message','Data berhasil diupdate')->with('sort','1');
    }

}

public function delete($no1,$no2,$no3,$no4){
    $id = $no1."/".$no2."/".$no3."/".$no4;
    $get_alat = Alat::where('no_inventaris','=',$id)->first();
    Cadangan_alat::insert([
        'no_inventaris' => $id,
        'no_urut' => $get_alat->no_urut,
        'kode_alat' => $get_alat->kode_alat,
        'kode_lokasi' => $get_alat->kode_lokasi,
        'tahun' => $get_alat->tahun,
        'tipe_alat' => $get_alat->tipe_alat,
        'merk_alat' => $get_alat->merk_alat,
        'no_seri' => $get_alat->no_seri,
        'no_asset' => $get_alat->no_asset,
        'status' => $get_alat->status,
        'kategori' => $get_alat->kategori,
        'id_workcenter' => $get_alat->id_workcenter,
        'durasi' => $get_alat->durasi,
        'tanggal_awal' => $get_alat->tanggal_awal,
        'jangkauan_pemakaian' => $get_alat->jangkauan_pemakaian,
        'kapasitas' => $get_alat->kapasitas,
        'resolusi' => $get_alat->resolusi,
        'kalibrator' => $get_alat->kalibrator
    ]); 

    Alat::find($id)->delete();

    $data = DB::table('q_alat')->get();
    $jenis_alat = Jenis_alat::get();
    $departement = Departement::get();
    $plant = Plant::get();
    $workcenter = Workcenter::get();
    $kategori = kategori::get();
    $kalibrator = Alat::where('kategori','=','standard')->get();
    return Redirect('/alat')
    ->with('jenis_alat',$jenis_alat)
    ->with('q_alat',$data)
    ->with('departement',$departement)
    ->with('plant',$plant)
    ->with('workcenter',$workcenter)
    ->with('kategori',$kategori)
    ->with('kalibrator',$kalibrator)
    ->with('message','Data berhasil dihapus')
    ->with('sort','1'); 
}

public function sort($field,$by){
    if ($by == "asc") {
        $sort = "0";
    }else{
        $sort = "1";
    }
    $data = DB::table('q_alat')->orderBy($field,$by)->get();
    $jenis_alat = Jenis_alat::get();
    $departement = Departement::get();
    $plant = Plant::get();
    $workcenter = Workcenter::get();
    $kategori = kategori::get();
    $kalibrator = Alat::where('kategori','=','standard')->get();
    return view('akun/admin_0/alat/index')
    ->with('jenis_alat',$jenis_alat)
    ->with('q_alat',$data)
    ->with('departement',$departement)
    ->with('plant',$plant)
    ->with('workcenter',$workcenter)
    ->with('kategori',$kategori)
    ->with('kalibrator',$kalibrator)
    ->with('sort',$sort);
}

public function page_sort($field,$by){
    if ($by == "asc") {
        $sort = "0";
    }else{
        $sort = "1";
    }
    $page = session('alatPage');
    $data = DB::table('q_alat')->orderBy($field,$by)->paginate($page);
    $jenis_alat = Jenis_alat::get();
    $departement = Departement::get();
    $plant = Plant::get();
    $workcenter = Workcenter::get();
    $kategori = kategori::get();
    $kalibrator = Alat::where('kategori','=','standard')->get();
    return view('akun/admin_0/alat/index')
    ->with('jenis_alat',$jenis_alat)
    ->with('q_alat',$data)
    ->with('departement',$departement)
    ->with('plant',$plant)
    ->with('kategori',$kategori)
    ->with('workcenter',$workcenter)
    ->with('kalibrator',$kalibrator)
    ->with('page',$page)
    ->with('sort',$sort)
    ->with('edit','');
}

public function nonaktif(){
    $page = session('nonaktifPage');
    if ($page > 0) {
        
    }else{
        $page = 25;
    }
    $nonaktif = Cadangan_alat::select('no_inventaris','no_urut','kode_alat','kode_lokasi','tahun','tipe_alat','merk_alat','no_seri','no_asset','status','kategori','nama_workcenter','durasi','tanggal_awal','jangkauan_pemakaian','kapasitas','resolusi','kalibrator')->join('tbl_workcenter','tbl_alat_nonaktif.id_workcenter','=','tbl_workcenter.id_workcenter')->get();
    return view('akun/admin_0/cadangan')->with('alat_nonaktif',$nonaktif)->with('page',$page)->with('sort','1');
}
public function nonaktif_page(Request $request){
    session(['nonaktifPage' => $request->input_page]);
    return redirect('/alat_nonaktif');
}

public function sort_nonaktif($field,$by){
    if ($by == "asc") {
        $sort = "0";
    }else{
        $sort = "1";
    }
    $nonaktif = Cadangan_alat::select('no_inventaris','no_urut','kode_alat','kode_lokasi','tahun','tipe_alat','merk_alat','no_seri','no_asset','status','kategori','nama_workcenter','durasi','tanggal_awal','jangkauan_pemakaian','kapasitas','resolusi','kalibrator')->join('tbl_workcenter','tbl_alat_nonaktif.id_workcenter','=','tbl_workcenter.id_workcenter')->orderBy($field,$by)->get();
    return view('akun/admin_0/cadangan')->with('alat_nonaktif',$nonaktif)->with('sort',$sort);
}

public function delete_nonaktif($id1,$id2,$id3,$id4){
    $id = $id1."/".$id2."/".$id3."/".$id4;
    Cadangan_alat::find($id)->delete();
    $nonaktif = Cadangan_alat::select('no_inventaris','no_urut','kode_alat','kode_lokasi','tahun','tipe_alat','merk_alat','no_seri','no_asset','status','kategori','nama_workcenter','durasi','tanggal_awal','jangkauan_pemakaian','kapasitas','resolusi','kalibrator')->join('tbl_workcenter','tbl_alat_nonaktif.id_workcenter','=','tbl_workcenter.id_workcenter')->get();
    return Redirect('alat_nonaktif');
}

public function kembalikan($id1,$id2,$id3,$id4){
    $id = $id1."/".$id2."/".$id3."/".$id4;
    $get_alat = Cadangan_alat::where('no_inventaris','=',$id)->first();
    Alat::insert([
        'no_inventaris' => $id,
        'no_urut' => $get_alat->no_urut,
        'kode_alat' => $get_alat->kode_alat,
        'kode_lokasi' => $get_alat->kode_lokasi,
        'tahun' => $get_alat->tahun,
        'tipe_alat' => $get_alat->tipe_alat,
        'merk_alat' => $get_alat->merk_alat,
        'no_seri' => $get_alat->no_seri,
        'no_asset' => $get_alat->no_asset,
        'status' => $get_alat->status,
        'kategori' => $get_alat->kategori,
        'id_workcenter' => $get_alat->id_workcenter,
        'durasi' => $get_alat->durasi,
        'tanggal_awal' => $get_alat->tanggal_awal,
        'jangkauan_pemakaian' => $get_alat->jangkauan_pemakaian,
        'kapasitas' => $get_alat->kapasitas,
        'resolusi' => $get_alat->resolusi,
        'kalibrator' => $get_alat->kalibrator
    ]);

    Cadangan_alat::find($id)->delete();
    return Redirect('alat_nonaktif');
}

public function alat_baru(){
    $jenis_alat = Jenis_alat::get();
    $departement = Departement::get();
    $plant = Plant::get();
    $workcenter = Workcenter::get();
    $kalibrator = Alat::where('kategori','=','standard')->get();
    $data = DB::table('q_alat_baru')->get();
    return view('akun/admin_0/alat_baru')
    ->with('alat',$data)
    ->with('jenis_alat',$jenis_alat)
    ->with('departement',$departement)
    ->with('plant',$plant)
    ->with('workcenter',$workcenter)
    ->with('kalibrator',$kalibrator)
    ->with('cek_edit','0')
    ->with('tampil','0');
}

public function alat_baru_page(){
    $page = session('alatBaruPage');
    if ($page > 0) {
        
    }else{
        $page = 15;
    }
    $jenis_alat = Jenis_alat::get();
    $departement = Departement::get();
    $plant = Plant::get();
    $workcenter = Workcenter::get();
    $kalibrator = Alat::where('kategori','=','standard')->get();
    $data = DB::table('q_alat_baru')->paginate($page);
    return view('akun/admin_0/alat_baru')
    ->with('alat',$data)
    ->with('jenis_alat',$jenis_alat)
    ->with('departement',$departement)
    ->with('plant',$plant)
    ->with('workcenter',$workcenter)
    ->with('kalibrator',$kalibrator)
    ->with('cek_edit','0')
    ->with('tampil','0');
}

public function input_alat_baru($id){
    $jenis_alat = Jenis_alat::get();
    $departement = Departement::get();
    $plant = Plant::get();
    $workcenter = Workcenter::get();
    $kalibrator = Alat::where('kategori','=','standard')->get();
    $data = DB::table('q_alat_baru')->get();
    $edit = DB::table('q_alat_baru')->where('id_alat_baru','=',$id)->first();
    $get_kode = $edit->kode_lokasi;
    $id_plant = substr($get_kode, 2);
    $id_departement = substr($get_kode, 0,2);
    $edit_plant = Plant::where('id_plant','=',$id_plant)->first();
    $edit_departement = Departement::where('id_departement','=',$id_departement)->first();
    return view('akun/admin_0/alat_baru')
    ->with('alat',$data)
    ->with('jenis_alat',$jenis_alat)
    ->with('departement',$departement)
    ->with('plant',$plant)
    ->with('workcenter',$workcenter)
    ->with('kalibrator',$kalibrator)
    ->with('edit',$edit)
    ->with('cek_edit','1')
    ->with('edit_plant',$edit_plant)
    ->with('edit_departement',$edit_departement)
    ->with('tampil','1');
}

public function input_alat_baru_input(Request $request,$id){
    $get_jenis_alat = $request->input_jenis_alat;
    $get_no_urut = $request->input_no_urut;
    $get_departement = $request->input_departement;
    $get_plant = $request->input_plant;
    $get_tahun = $request->input_tahun;
    $get_workcenter = $request->input_workcenter;
    $get_kategori = $request->input_kategori;
    $get_merk = $request->input_merk;
    $get_no_seri = $request->input_no_seri;
    $get_no_asset = $request->input_no_asset;
    $get_tipe_alat = $request->input_tipe_alat;
    $get_durasi = $request->input_durasi;
    $get_tanggal_awal = $request->input_tanggal_awal;
    $get_jangkauan_pemakaian = $request->input_jangkauan_pemakaian;
    $get_kapasitas = $request->input_kapasitas;
    $get_resolusi = $request->input_resolusi;
    $get_kalibrator = $request->input_kalibrator;

    //buat kode alat
    $jenis = Jenis_alat::where('jenis_alat','=',$get_jenis_alat)->get();
    foreach ($jenis as $jenis1) {
        $kode_alat = $jenis1->kode_alat;    
    }

    //buat id departement
    $sql_departement =  Departement::where('nama_departement','=',$get_departement)->get();
    foreach ($sql_departement as $row) {
        $dept = $row->id_departement;
    }

    //buat id plant dan buat kode lokasi
    $sql_plant = Plant::where('nama_plant','=',$get_plant)->get();
    foreach ($sql_plant as $row) {
        $plant =  $row->id_plant;
    }
    if ($dept<10) {
        $dept1 = "0".$dept;
    }else{
        $dept1 = $dept;
    }
    $kode_lokasi = $dept1 ."". $plant ;

    //ambil tahun
    $tahun =$get_tahun;
    $tahun1 = substr($tahun, -2);

    //buat id workcenter
    $sql_get_workcenter = Workcenter::where('nama_workcenter','=',$get_workcenter)->get();
    foreach ($sql_get_workcenter as $row) {
        $get_workcenter = $row->id_workcenter;
    }

    //buat no urut
    $q_max = Alat::orderby('no_urut','desc')->where('kode_alat','=',$kode_alat)->where('kode_lokasi','=',$kode_lokasi)->first();
    // foreach ($q_max as $row) {
    //  $max1 = $row->no_urut;
    // }
    if ($q_max != '') {
        $max1 = $q_max->no_urut;
        $max2 = $max1 + 1;  
    }else{
        $max2 = 1;
    }
    $no_urut= $get_no_urut;
    if ($no_urut!="") {
        if ($no_urut < 10) {
            $no_urut1 = "00".$no_urut;
        }elseif ($no_urut < 100) {
            $no_urut1 = "0".$no_urut;
        }else{
            $no_urut1 = $no_urut;
        }   
    }else{
        if ($max2 < 10) {
            $no_urut1 = "00".$max2;
        }elseif ($max2 < 100) {
            $no_urut1 = "0".$max2;
        }else{
            $no_urut1 = $max2;
        }   
    }

    //cek no urut
    if ($no_urut != "") {
        $cek_no_urut = Alat::where('no_urut','=',$no_urut)->where('no_urut','!=',$get_no_urut3)->where('kode_alat','=',$kode_alat)->where('kode_lokasi','=',$kode_lokasi)->count();
        if ($cek_no_urut > 0) {
            return Redirect('/alat')->with('sort','1');
            echo "<script>alert('No urut sudah digunakan, silahkan masukan nomor urut lainnya atau kosongkan!');</script>";
        }else{
            //generate no inventaris
            $table="tbl_alat";
            if ($get_kategori== "standard") {
                $inventaris=$no_urut1."/".$kode_alat."-SK"."/".$kode_lokasi."/".$tahun1;
            }else{
                $inventaris=$no_urut1."/".$kode_alat."/".$kode_lokasi."/".$tahun1;  
            }
        }
    }else{
        //generate no inventaris
        $table="tbl_alat";
        if ($get_kategori== "standard") {
            $inventaris=$no_urut1."/".$kode_alat."-SK"."/".$kode_lokasi."/".$tahun1;
        }else{
            $inventaris=$no_urut1."/".$kode_alat."/".$kode_lokasi."/".$tahun1;  
        }
    }


    Alat::insert([
        'no_inventaris'=> $inventaris,
        'no_urut'=> $no_urut1,
        'kode_alat'=> $kode_alat,
        'kode_lokasi'=>$kode_lokasi,
        'tahun'=> $tahun,
        'merk_alat'=> $get_merk,
        'no_seri'=> $get_no_seri,
        'no_asset'=> $get_no_asset,
        'kategori'=> $get_kategori,
        'tipe_alat'=> $get_tipe_alat,
        'id_workcenter'=> $get_workcenter,
        'durasi'=> $get_durasi,
        'tanggal_awal'=> $get_tanggal_awal,
        'jangkauan_pemakaian'=> $get_jangkauan_pemakaian,
        'kapasitas'=> $get_kapasitas,
        'resolusi'=> $get_resolusi,
        'kalibrator'=> $get_kalibrator
    ]);

    $edit = DB::table('q_alat_baru')->where('id_alat_baru','=',$id)->first();
    $get_kode = $edit->kode_lokasi;

    Alat_baru::find($id)->delete();


    $jenis_alat = Jenis_alat::get();
    $departement = Departement::get();
    $plant = Plant::get();
    $workcenter = Workcenter::get();
    $kalibrator = Alat::where('kategori','=','standard')->get();
    $data = DB::table('q_alat_baru')->get();
    $id_plant = substr($get_kode, 2);
    $id_departement = substr($get_kode, 0,2);
    $edit_plant = Plant::where('id_plant','=',$id_plant)->first();
    $edit_departement = Departement::where('id_departement','=',$id_departement)->first();
    return view('akun/admin_0/alat_baru')
    ->with('alat',$data)
    ->with('jenis_alat',$jenis_alat)
    ->with('departement',$departement)
    ->with('plant',$plant)
    ->with('workcenter',$workcenter)
    ->with('kalibrator',$kalibrator)
    ->with('edit',$edit)
    ->with('cek_edit','0')
    ->with('edit_plant',$edit_plant)
    ->with('edit_departement',$edit_departement)
    ->with('tampil','0')
    ->with('message','alat berhasil diinput');
}

}

here is the route:

Route::post('alat/{id1}/{id2}/{id3}/{id4}/edit', 'Crudcontroller@edit')->middleware('admin_petugas');

please help me, thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire