Merge branch 'main' of https://git.probindo.com/Probindo/database-pertani-web
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web\Produktivitas;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Models\Pertanian\Petani;
|
||||
use App\Models\Pertanian\AksesPasar;
|
||||
use App\Models\Pertanian\HargaJualPanen;
|
||||
|
||||
class AksesPasarController extends Controller
|
||||
{
|
||||
public function index($petaniId)
|
||||
{
|
||||
$petani = Petani::findOrFail($petaniId);
|
||||
|
||||
$aksesPasar = AksesPasar::where('petani_id', $petaniId)->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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user