sigd/app/Http/Controllers/Activity/LivestockManureController.php

183 lines
6.9 KiB
PHP

<?php
namespace App\Http\Controllers\Activity;
use App\Http\Controllers\Controller;
use App\Models\LivestockManure;
use App\Models\ReferenceAr;
use App\Rules\UniqueInSchema;
use App\Services\Activity\LivestockManureService;
use Illuminate\Http\Request;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;
class LivestockManureController implements HasMiddleware
{
protected $title = 'Jenis Pengelolaan Kotoran Ternak';
protected $template = 'modules.form.livestock-manure';
protected $route = 'modules.pertanian.kotoran-ternak';
protected $service;
public function __construct(LivestockManureService $service)
{
$this->service = $service;
}
public static function middleware(): array
{
return [
//new Middleware('permission:/agriculture/kotoran_ternak'),
];
}
public function grid()
{
$_data = [];
$data = $this->service->getAll();
foreach ($data as $key => $row) {
$btn = '<a href="' . route($this->route.'.edit', $row->id) . '" class="btn btn-sm btn-primary mb-1 w-100">Edit</a>';
$btn .= ' <form action="' . route($this->route.'.destroy', $row->id) . '" method="POST" style="display: inline;" class="delete-form">';
$btn .= csrf_field();
$btn .= method_field('DELETE');
$btn .= '<button type="button" class="btn btn-sm btn-danger mb-1 w-100 delete-button">Hapus</button>';
$btn .= '</form>';
if ($row->active_status == 0) {
$btn .= ' <form action="' . route($this->route.'.setAktif', $row->id) . '" method="POST" style="display: inline;" class="set-aktif-form">';
$btn .= csrf_field();
$btn .= method_field('PUT');
$btn .= '<button type="button" class="btn btn-sm btn-info mb-1 w-100 text-white set-aktif-button">Aktifkan</button>';
$btn .= '</form>';
}
if ($row->active_status == 1) {
$status = '<span class="badge bg-success">Aktif</span>';
} else {
$status = '<span class="badge bg-danger">Tidak Aktif</span>';
}
$_data[] = [
'no' => $key+1,
'no_baris' => @$row->row_num,
'ef_direct_n2o_n' => $row->ef_direct_n2o_n === 0 ? 0 : getFormattedValue($row->ef_direct_n2o_n, 2),
'ef_evaporation_n' => $row->ef_evaporation_n === 0 ? 0 : getFormattedValue($row->ef_evaporation_n),
'name' => $row->name,
'description' => $row->description,
'status' => $status,
'action' => @$btn,
];
}
return response()->json($_data);
}
public function index(Request $request)
{
$data['route'] = $this->route;
$data['title'] = $this->title;
return view($this->template.'.index',$data);
}
public function create()
{
$data['route'] = $this->route;
$data['title'] = $this->title;
return view($this->template.'.create',$data);
}
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'ef_direct_n2o_n' => 'nullable',
'ef_evaporation_n' => 'nullable'
]);
try {
$nextRowNum = LivestockManure::max('row_num') + 1;
$formattedEf1 = getOriginalValue($request->input('ef_direct_n2o_n'));
$formattedEf2 = getOriginalValue($request->input('ef_evaporation_n'));
$data = array_merge($request->all(), [
'row_num' => $nextRowNum,
'active_status' => 0,
'ef_direct_n2o_n' => $formattedEf1,
'ef_evaporation_n' => $formattedEf2,
]);
$this->service->create($data);
return redirect()->route($this->route.'.index')->with('success', 'Jenis Pengelolaan Kotoran Ternak berhasil ditambahkan.');
} catch (\Exception $e) {
return back()->withErrors(['error' => 'Jenis Pengelolaan Kotoran Ternak gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]);
}
}
public function edit($id)
{
$data['lm'] = $this->service->find($id);
$data['route'] = $this->route;
$data['title'] = $this->title;
return view($this->template.'.edit', $data);
}
public function update(Request $request, LivestockManure $kotoran_ternak)
{
$request->validate([
// 'code' => [
// 'required',
// 'string',
// 'max:255',
// new UniqueInSchema('activity', 'livestock_manure', 'code', $row->id),
// ],
'name' => 'required|string|max:255',
'ef_direct_n2o_n' => 'nullable',
'ef_evaporation_n' => 'nullable',
'row_num' => 'required|numeric',
]);
try {
$formattedEf1 = getOriginalValue($request->input('ef_direct_n2o_n'));
$formattedEf2 = getOriginalValue($request->input('ef_evaporation_n'));
$data = array_merge($request->all(), [
'ef_direct_n2o_n' => $formattedEf1,
'ef_evaporation_n' => $formattedEf2,
]);
$this->service->update($kotoran_ternak, $data);
return redirect()->route($this->route.'.index')->with('success', 'Jenis Pengelolaan Kotoran Ternak berhasil diperbarui.');
} catch (\Exception $e) {
return back()->withErrors(['error' => 'Jenis Pengelolaan Kotoran Ternak gagal diperbarui. Silakan coba lagi. Error: ' . $e->getMessage()]);
}
}
public function destroy(LivestockManure $kotoran_ternak)
{
try {
$this->service->delete($kotoran_ternak);
$records = LivestockManure::orderBy('row_num')->get();
foreach ($records as $index => $record) {
$record->row_num = $index + 1;
$record->save();
}
return redirect()->route($this->route.'.index')->with('success', 'Jenis Pengelolaan Kotoran Ternak berhasil dihapus.');
} catch (\Exception $e) {
return back()->withErrors(['error' => 'Jenis Pengelolaan Kotoran Ternak gagal dihapus. Silakan coba lagi. Error: ' . $e->getMessage()]);
}
}
public function setAktif($id)
{
try {
$this->service->setAktif($id);
return redirect()->route($this->route.'.index')->with('success', 'Jenis Pengelolaan Kotoran Ternak berhasil diaktifkan.');
} catch (\Exception $e) {
return back()->withErrors(['error' => 'Gagal mengaktifkan Jenis Pengelolaan Kotoran Ternak. Silakan coba lagi. Error: ' . $e->getMessage()]);
}
}
}