This commit is contained in:
2026-07-06 15:45:56 +07:00
6 changed files with 424 additions and 3 deletions
@@ -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();
}
}
}
+62
View File
@@ -0,0 +1,62 @@
<?php
namespace App\Models\Pertanian;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\User;
use App\Models\Pertanian\Petani;
class AksesPasar extends Model
{
use SoftDeletes;
protected $table = 'akses_pasar';
protected $fillable = [
'uid',
'petani_id',
'skor_pengalaman',
'saluran_terdahulu',
'saluran_terdahulu_lainnya',
'alasan_berhenti',
'praktik_fermentasi',
'proses_fermentasi',
'model_fermentasi_memungkinkan',
'skor_kesadaran_premium',
'sumber_info_premium',
'waktu_mulai_wawancara',
'waktu_selesai_wawancara',
'skor_kepercayaan_responden',
'kehadiran_orang_lain',
'skor_pengaruh_orang_lain',
'created_by',
'updated_by',
'deleted_by',
];
protected $casts = [
'saluran_terdahulu' => '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');
}
}
+45
View File
@@ -0,0 +1,45 @@
<?php
namespace App\Models\Pertanian;
use Illuminate\Database\Eloquent\Model;
use App\Models\User;
use App\Models\Pertanian\Petani;
class HargaJualPanen extends Model
{
protected $table = 'harga_jual_panen';
protected $fillable = [
'uid',
'petani_id',
'bentuk_produk',
'harga_panen_terakhir',
'harga_tahun_lalu',
'harga_dua_tahun_lalu',
'created_by',
'updated_by',
'deleted_by',
];
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');
}
}