diff --git a/app/Http/Controllers/Web/Produktivitas/AksesPasarController.php b/app/Http/Controllers/Web/Produktivitas/AksesPasarController.php new file mode 100644 index 0000000..14c21a1 --- /dev/null +++ b/app/Http/Controllers/Web/Produktivitas/AksesPasarController.php @@ -0,0 +1,110 @@ +first(); + $hargaJualPanen = HargaJualPanen::where('petani_id', $petaniId)->get(); + + return view('produktivitas.petani.akses-pasar.index', compact('petani', 'aksesPasar', 'hargaJualPanen')); + } + + public function store(Request $request) + { + $request->validate([ + 'petani_id' => 'required|exists:petani,id', + ]); + + DB::beginTransaction(); + + try { + $aksesPasarData = $request->except(['_token', 'harga_jual']); + $aksesPasarData['uid'] = Str::uuid(); + $aksesPasarData['created_by'] = Auth::id(); + + AksesPasar::create($aksesPasarData); + + if ($request->has('harga_jual')) { + foreach ($request->harga_jual as $harga) { + if (!empty($harga['bentuk_produk'])) { + HargaJualPanen::create([ + 'uid' => Str::uuid(), + 'petani_id' => $request->petani_id, + 'bentuk_produk' => $harga['bentuk_produk'], + 'harga_panen_terakhir' => $harga['harga_panen_terakhir'], + 'harga_tahun_lalu' => $harga['harga_tahun_lalu'], + 'harga_dua_tahun_lalu' => $harga['harga_dua_tahun_lalu'], + 'created_by' => Auth::id(), + ]); + } + } + } + + DB::commit(); + + return redirect()->back()->with('success', 'Data Rantai Nilai dan Akses Pasar berhasil disimpan.'); + + } catch (\Exception $e) { + DB::rollBack(); + return redirect()->back()->withErrors(['error' => 'Gagal menyimpan data: ' . $e->getMessage()])->withInput(); + } + } + + public function update(Request $request, $id) + { + $request->validate([ + 'petani_id' => 'required|exists:petani,id', + ]); + + DB::beginTransaction(); + + try { + $aksesPasar = AksesPasar::findOrFail($id); + $updateData = $request->except(['_token', '_method', 'harga_jual']); + $updateData['updated_by'] = Auth::id(); + + $aksesPasar->update($updateData); + + HargaJualPanen::where('petani_id', $request->petani_id)->delete(); + + if ($request->has('harga_jual')) { + foreach ($request->harga_jual as $harga) { + if (!empty($harga['bentuk_produk'])) { + HargaJualPanen::create([ + 'uid' => Str::uuid(), + 'petani_id' => $request->petani_id, + 'bentuk_produk' => $harga['bentuk_produk'], + 'harga_panen_terakhir' => $harga['harga_panen_terakhir'], + 'harga_tahun_lalu' => $harga['harga_tahun_lalu'], + 'harga_dua_tahun_lalu' => $harga['harga_dua_tahun_lalu'], + 'created_by' => $aksesPasar->created_by, + 'updated_by' => Auth::id(), + ]); + } + } + } + + DB::commit(); + + return redirect()->back()->with('success', 'Data Rantai Nilai dan Akses Pasar berhasil diperbarui.'); + + } catch (\Exception $e) { + DB::rollBack(); + return redirect()->back()->withErrors(['error' => 'Gagal memperbarui data: ' . $e->getMessage()])->withInput(); + } + } +} \ No newline at end of file diff --git a/app/Models/Pertanian/AksesPasar.php b/app/Models/Pertanian/AksesPasar.php new file mode 100644 index 0000000..eedc554 --- /dev/null +++ b/app/Models/Pertanian/AksesPasar.php @@ -0,0 +1,62 @@ + 'array', + ]; + + + public function petani() + { + return $this->belongsTo(Petani::class, 'petani_id'); + } + + public function creator() + { + return $this->belongsTo(User::class, 'created_by'); + } + + public function updater() + { + return $this->belongsTo(User::class, 'updated_by'); + } + + public function deleter() + { + return $this->belongsTo(User::class, 'deleted_by'); + } +} \ No newline at end of file diff --git a/app/Models/Pertanian/HargaJualPanen.php b/app/Models/Pertanian/HargaJualPanen.php new file mode 100644 index 0000000..d74370d --- /dev/null +++ b/app/Models/Pertanian/HargaJualPanen.php @@ -0,0 +1,45 @@ +belongsTo(Petani::class, 'petani_id'); + } + + public function creator() + { + return $this->belongsTo(User::class, 'created_by'); + } + + public function updater() + { + return $this->belongsTo(User::class, 'updated_by'); + } + + public function deleter() + { + return $this->belongsTo(User::class, 'deleted_by'); + } +} \ No newline at end of file diff --git a/resources/views/produktivitas/petani/akses-pasar/index.blade.php b/resources/views/produktivitas/petani/akses-pasar/index.blade.php new file mode 100644 index 0000000..0f3184e --- /dev/null +++ b/resources/views/produktivitas/petani/akses-pasar/index.blade.php @@ -0,0 +1,205 @@ +@extends('layout.main') + +@section('content') +