185 lines
6.9 KiB
PHP
185 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 $service;
|
|
|
|
public function __construct(LivestockManureService $service)
|
|
{
|
|
$this->service = $service;
|
|
}
|
|
|
|
public static function middleware(): array
|
|
{
|
|
return [
|
|
//new Middleware('permission:/agriculture/kotoran_ternak'),
|
|
];
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
if ($request->ajax()) {
|
|
$data = $this->service->getAll();
|
|
|
|
$result = datatables()::of($data)
|
|
->addColumn('no_baris', function ($lm) {
|
|
return $lm->row_num;
|
|
})
|
|
->addColumn('status', function ($lm) {
|
|
if ($lm->active_status == 1) {
|
|
return '<span class="badge badge-success">Aktif</span>';
|
|
} else {
|
|
return '<span class="badge badge-danger">Tidak Aktif</span>';
|
|
}
|
|
})
|
|
->editColumn('ef_direct_n2o_n', function ($lm) {
|
|
if ($lm->ef_direct_n2o_n === 0) {
|
|
return 0; // Show 0 as is
|
|
}
|
|
|
|
$output = getFormattedValue($lm->ef_direct_n2o_n, 2);
|
|
return $output;
|
|
})
|
|
->editColumn('ef_evaporation_n', function ($lm) {
|
|
if ($lm->ef_evaporation_n === 0) {
|
|
return 0; // Show 0 as is
|
|
}
|
|
|
|
$output = getFormattedValue($lm->ef_evaporation_n);
|
|
return $output;
|
|
})
|
|
->addColumn('action', function ($lm) {
|
|
$btn = '<a href="' . route('livestockManure.edit', $lm->id) . '" class="btn btn-primary">Edit</a>';
|
|
$btn .= ' <form action="' . route('livestockManure.destroy', $lm->id) . '" method="POST" style="display: inline;" class="delete-form">';
|
|
$btn .= csrf_field();
|
|
$btn .= method_field('DELETE');
|
|
$btn .= '<button type="button" class="btn btn-danger delete-button">Hapus</button>';
|
|
$btn .= '</form>';
|
|
|
|
if ($lm->active_status == 0) {
|
|
$btn .= ' <form action="' . route('livestockManure.setAktif', $lm->id) . '" method="POST" style="display: inline;" class="set-aktif-form">';
|
|
$btn .= csrf_field();
|
|
$btn .= method_field('PUT');
|
|
$btn .= '<button type="button" class="btn btn-info text-white set-aktif-button">Aktifkan</button>';
|
|
$btn .= '</form>';
|
|
}
|
|
|
|
return $btn;
|
|
})
|
|
->rawColumns(['no_baris', 'status', 'action'])
|
|
->make(true);
|
|
|
|
return $result;
|
|
}
|
|
|
|
return view('form.livestock-manure.index');
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('form.livestock-manure.create');
|
|
}
|
|
|
|
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('livestockManure.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)
|
|
{
|
|
$lm = $this->service->find($id);
|
|
return view('form.livestock-manure.edit', compact('lm'));
|
|
}
|
|
|
|
public function update(Request $request, LivestockManure $kotoran_ternak)
|
|
{
|
|
$request->validate([
|
|
// 'code' => [
|
|
// 'required',
|
|
// 'string',
|
|
// 'max:255',
|
|
// new UniqueInSchema('activity', 'livestock_manure', 'code', $lm->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('livestockManure.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('livestockManure.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('livestockManure.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()]);
|
|
}
|
|
}
|
|
}
|