<?php namespace App\Http\Controllers; use App\Models\Bom; use App\Models\Bahan; use App\Models\Produk; use App\Models\bomsementara; use Illuminate\Http\Request; use App\Models\Manufacturingorder; use Illuminate\Support\Facades\DB; use App\Models\manufacturingorderisi; class ManufacturingorderController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $manufacturingorders = Manufacturingorder::all(); $produks = Produk::all(); return view('dashboards.manufacturingorder.index', [ 'manufacturingorders' => $manufacturingorders, 'produks' => $produks, ]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { $code = 'MOR' . date('Y') . rand(1111, 9999); $produks = Produk::all(); $status = "MAT"; return view('dashboards.manufacturingorder.create', [ 'code' => $code, 'produks' => $produks, 'status' => $status, ]); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $request->validate([ 'kodemo' => 'required', 'kodeprodu' => 'required', 'banyakproduksi' => 'required', 'tglproduksi' => 'required', 'tglselesaiproduksi' => 'required', ]); Manufacturingorder::create($request->all()); return redirect()->route('adminIndexManufacturingorder') ->with('success', 'Data Telah Ditambahkan. Silahkan buka isi dari MO'); } public function storeisi(Request $request) { $request->validate([ 'idbom' => 'required', 'idmonya' => 'required', ]); $materialnya = bomsementara::get()->where('idbomnya', $request->idbom); $banyakproduksimo = manufacturingorder::select('banyakproduksi')->where('idmo', $request->idmonya)->get(); // dd((int)$banyakproduksimo[0]->banyakproduksi); //dibuat karena matrialnya index tidak dimulai dari nol foreach ($materialnya as $value) { $materialbaru[] = [ 'idbomsementara' => $value->idbomsementara, 'idbomnya' => $value->idbomnya, 'kodebah' => $value->kodebah, 'banyakqty' => $value->banyakqty * (int)$banyakproduksimo[0]->banyakproduksi, 'hargaakhir' => $value->hargaakhir, ]; } $isimaterialnya = manufacturingorderisi::where('idmonya', $request->idmonya)->delete(); // buat cari bahan apa yg dipake di bom isi $kodbahan = DB::table('bomsementaras') ->select('kodebah') ->orderBy('idbomnya', 'asc') //berarti kriteria wajib urut ->where('idbomnya', $request->idbom) ->get(); //nanti berupa objek //setelahnya ambil data dari bahan sesuai bahan yg dipake di bom isi $bahann = array(); for ($i = 0; $i < $kodbahan->count(); $i++) { $bahann[] = Bahan::select('kodebahan', 'stockbahan')->where('kodebahan', $kodbahan[$i]->kodebah)->get(); } //dicek stock nya $statbol = 0; for ($i = 0; $i < $kodbahan->count(); $i++) { if ($materialbaru[$i]['banyakqty'] <= $bahann[$i][0]->stockbahan) { $statusbahan[] = "Bahan Aman"; $statbol += 0; } else { $statusbahan[] = "Bahan Kurang"; $statbol += 1; } } // dd($statbol); for ($i = 0; $i < $materialnya->count(); $i++) { manufacturingorderisi::create([ 'idmonya' => $request->idmonya, 'idbomnya' => $materialbaru[$i]['idbomnya'], 'kodebah' => $materialbaru[$i]['kodebah'], 'banyakqty' => $materialbaru[$i]['banyakqty'], 'statusbahan' => $statusbahan[$i], 'statbol' => $statbol, ]); } return redirect()->back() ->with('success', 'Data Telah Ditambahkan. Silahkan Isi Bahan Baku Yang Dibutuhkan Di Isi Bom'); } /** * Display the specified resource. * * @param \App\Models\Produk $Produk * @return \Illuminate\Http\Response */ public function show(Manufacturingorder $Manufacturingorder) { // } public function isi(Manufacturingorder $datamanufacturingorder) { // dd($databom->idbom); $boms = bom::all(); $manufacturingorderisi = manufacturingorderisi::get()->where('idmonya', $datamanufacturingorder->idmo); $bahanbakus = Bahan::all(); //dibuat karena manufacturingisi index tidak dimulai dari nol dan kita cuma butuh banyak qty foreach ($manufacturingorderisi as $value) { $quantiti[] = [ 'banyakqty' => $value->banyakqty ]; } // buat cari bahan apa yg dipake di manufacturingisi $kodbahan = DB::table('manufacturingorderisis') ->select('kodebah') ->orderBy('idmonya', 'asc') //berarti kriteria wajib urut ->where('idmonya', $datamanufacturingorder->idmo) ->get(); //nanti berupa objek //setelahnya ambil data dari tabel bahan untuk ambil bahan yg dipake di materialorder $bahann = array(); for ($i = 0; $i < $kodbahan->count(); $i++) { $bahann[] = Bahan::select('kodebahan', 'stockbahan')->where('kodebahan', $kodbahan[$i]->kodebah)->get(); } //buat cek banyak nya bahan aman atau tidaknya.. kalau bahan aman semua berarti statol harus 0 $statbol = 0; for ($i = 0; $i < $kodbahan->count(); $i++) { if ($quantiti[$i]['banyakqty'] <= $bahann[$i][0]->stockbahan) { // Bahan Aman $statbol += 1; } else { // Bahan tidak aman $statbol += 0; } } return view('dashboards.manufacturingorder.isi', [ 'datamanufacturingorder' => $datamanufacturingorder, 'manufacturingorderisi' => $manufacturingorderisi, 'bahanbakus' => $bahanbakus, 'boms' => $boms, 'statbol' => $statbol, ]); //SAYANG UNTUK DIHAPUS // try { // $buton = $manufacturingorderisi[0]->statbol; // } catch (\Exception $e) { // $buton = 2; // } // try{ // // if ($manufacturingorderisi[0]->statbol->isEmpty()){ // // $buton = 2; // // } else { // $buton = $manufacturingorderisi[0]->statbol; // // } // } catch (\Exception $e) { // } // $buton = 2; // $dummy = collect(collect([ // "idmoisi" => 1, // "idmonya" => 1, // "idbomnya" => 1, // "kodebah" => "MT2410", // "banyakqty" => 2.0, // "statbol" => 2, // "statusbahan" => "Bahan Kurang", // "created_at" => "2023-01-11 04:50:18", // "updated_at" => "2023-01-11 04:50:18"] // )); // $dummy = collect(collect([ // ["statbol" => 2],["statbol" => 7]] // )); // dd($dummy[0]->statbol); // $harga = bomsementara::get()->where('idbomnya', $databom->idbom); // $hargaakhir = manufacturingorderisi::where('idmonya',$datamanufacturingorder->idmo)->sum('hargaakhir'); // $hargaproduk = Produk::select('hargaproduk')->where('kodeproduk', $databom->kodeprod)->get(); // dd($manufacturingorderisi[0]->statbol); // dd($manufacturingorderisi); // if ($manufacturingorderisi->isEmpty() === true) { // return view ('dashboards.manufacturingorder.isi', [ // 'datamanufacturingorder'=>$datamanufacturingorder, // 'manufacturingorderisi' => $dummy, // 'bahanbakus' => $bahanbakus, // 'boms' => $boms, // 'ada' => false, // // 'hargaakhir' => $hargaakhir, // // 'hargaproduk' => $hargaproduk, // ]); // }else{ // // dd('sampai sini'); // return view ('dashboards.manufacturingorder.isi', [ // 'datamanufacturingorder'=>$datamanufacturingorder, // 'manufacturingorderisi' => $manufacturingorderisi, // 'bahanbakus' => $bahanbakus, // 'boms' => $boms, // 'ada' => true, // // 'hargaakhir' => $hargaakhir, // // 'hargaproduk' => $hargaproduk, // ]); // } } /** * Show the form for editing the specified resource. * * @param \App\Models\produk $produk * @return \Illuminate\Http\Response */ public function edit(Manufacturingorder $datamanufacturingorder) { $produks = Produk::all(); return view('dashboards.manufacturingorder.edit', [ 'datamanufacturingorder' => $datamanufacturingorder, 'produks' => $produks ]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Models\Produk $Produk * @return \Illuminate\Http\Response */ public function update(Request $request, Manufacturingorder $datamanufacturingorder) { $request->validate([ 'kodemo' => 'required', 'kodeprodu' => 'required', 'banyakproduksi' => 'required', 'tglproduksi' => 'required', 'tglselesaiproduksi' => 'required', ]); $datamanufacturingorder->update($request->all()); return redirect()->route('adminIndexManufacturingorder') ->with('success', 'Data Telah Diperbaharui.'); } /** * Remove the specified resource from storage. * * @param \App\Models\Produk $Produk * @return \Illuminate\Http\Response */ public function destroy(Manufacturingorder $datamanufacturingorder) { $datamanufacturingorder->delete(); return redirect()->route('adminIndexManufacturingorder') ->with('success', 'Data Telah Dihapus.'); } // public function destroyisi(bomsementara $dataisibom) // { // $dataisibom->delete(); // return redirect()->back() // ->with('success','Data Telah Dihapus.'); // } public function produce(Request $request, Manufacturingorder $datamo) { $request->validate([ 'idmonya' => 'required', ]); Manufacturingorder::where('idmo', $request->idmonya)->update([ 'status' => 'PRODUCE' ]); return redirect()->route('adminIndexManufacturingorder') ->with('success', 'Data Telah Diperbaharui.'); } public function mad(Request $request, Manufacturingorder $datamo) { $request->validate([ 'idmonya' => 'required', ]); $tanggalselesai = date('d-m-Y'); // dd($tanggalselesai); Manufacturingorder::where('idmo', $request->idmonya)->update([ 'tglselesaiproduksi' => $tanggalselesai, 'status' => 'MaD' ]); $manufacturingorderisi = manufacturingorderisi::get()->where('idmonya', $request->idmonya); //buat ngurang bahan foreach ($manufacturingorderisi as $bahanygdipake) { $bahannya = Bahan::select('stockbahan')->where('kodebahan', $bahanygdipake->kodebah)->get(); $hitung = $bahannya[0]->stockbahan - $bahanygdipake->banyakqty; Bahan::where('kodebahan', $bahanygdipake->kodebah)->update([ 'stockbahan' => $hitung, ]); } //buat tambah produk $kodeproduknyamo = manufacturingorder::select('kodeprodu')->where('idmo', $request->idmonya)->get(); $banyakproduksimo = manufacturingorder::select('banyakproduksi')->where('idmo', $request->idmonya)->get(); $oldstock = Produk::select('stockproduk')->where('kodeproduk', $kodeproduknyamo[0]->kodeprodu)->get(); $perhitungan = $oldstock[0]->stockproduk + $banyakproduksimo[0]->banyakproduksi; // dd($perhitungan); // banyakproduksi Produk::where('kodeproduk', $kodeproduknyamo[0]->kodeprodu)->update([ 'stockproduk' => $perhitungan, ]); return redirect()->route('adminIndexManufacturingorder') ->with('success', 'Data Telah Diperbaharui.'); } }