Merge branch 'main' of https://git.probindo.com/Probindo/database-pertani-web
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Models\Map\PanenTanaman;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Exception;
|
||||
|
||||
class PanenTanamanController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$panenTanaman = PanenTanaman::latest()->get();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Daftar data panen tanaman berhasil diambil.',
|
||||
'data' => $panenTanaman
|
||||
], 200);
|
||||
} catch (Exception $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Terjadi kesalahan saat mengambil data.',
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'panen_id' => 'required|integer',
|
||||
'tanaman_id' => 'required|integer',
|
||||
'commodity_name' => 'required|string|max:255',
|
||||
'sync_status' => 'nullable|string|max:50',
|
||||
'weight_kg' => 'required|numeric|min:0',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Validasi gagal.',
|
||||
'errors' => $validator->errors()
|
||||
], 422);
|
||||
}
|
||||
|
||||
try {
|
||||
$panenTanaman = PanenTanaman::create($request->all());
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Data panen tanaman berhasil disimpan.',
|
||||
'data' => $panenTanaman
|
||||
], 201);
|
||||
} catch (Exception $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Gagal menyimpan data.',
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(PanenTanaman $panenTanaman)
|
||||
{
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Detail data panen tanaman ditemukan.',
|
||||
'data' => $panenTanaman
|
||||
], 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, PanenTanaman $panenTanaman)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'panen_id' => 'sometimes|required|integer',
|
||||
'tanaman_id' => 'sometimes|required|integer',
|
||||
'commodity_name' => 'sometimes|required|string|max:255',
|
||||
'sync_status' => 'nullable|string|max:50',
|
||||
'weight_kg' => 'sometimes|required|numeric|min:0',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Validasi gagal.',
|
||||
'errors' => $validator->errors()
|
||||
], 422);
|
||||
}
|
||||
|
||||
try {
|
||||
$panenTanaman->update($request->all());
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Data panen tanaman berhasil diperbarui.',
|
||||
'data' => $panenTanaman
|
||||
], 200);
|
||||
} catch (Exception $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Gagal memperbarui data.',
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(PanenTanaman $panenTanaman)
|
||||
{
|
||||
try {
|
||||
$panenTanaman->delete(); // Karena pakai SoftDeletes, data tidak terhapus permanen di DB
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Data panen tanaman berhasil dihapus (soft delete).'
|
||||
], 200);
|
||||
} catch (Exception $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Gagal menghapus data.',
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Produktivitas;
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
use App\Models\Map\Panen;
|
||||
|
||||
@@ -29,33 +30,61 @@ class PanenController extends Controller
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
if (!Gate::any(['petani', 'fasilitator','super_admin'])) {
|
||||
if (!Gate::any(['petani', 'fasilitator', 'super_admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'uid' => ['required', 'string'],
|
||||
'title' => ['required', 'string'],
|
||||
'land_id' => ['required', 'integer'],
|
||||
'plant_id' => ['required', 'integer'],
|
||||
'commodity_name' => ['required', 'string', 'max:255'],
|
||||
'weight_kg' => ['required', 'numeric'],
|
||||
'farmer_id' => ['required', 'integer'],
|
||||
'land_description' => ['nullable', 'string'],
|
||||
'harvest_date' => ['required', 'date'],
|
||||
'commodities' => ['required', 'array', 'min:1'],
|
||||
'commodities.*.plant_id' => ['nullable', 'integer'],
|
||||
'commodities.*.commodity_name' => ['required', 'string'],
|
||||
'commodities.*.weight_kg' => ['required', 'numeric'],
|
||||
'notes' => ['nullable', 'string'],
|
||||
]);
|
||||
|
||||
$panen = Panen::create([
|
||||
'land_id' => $validated['land_id'],
|
||||
'plant_id' => $validated['plant_id'],
|
||||
'farmer_id' => Auth::user()->profile->id,
|
||||
'commodity_name' => $validated['commodity_name'],
|
||||
'weight_kg' => $validated['weight_kg'],
|
||||
'harvest_date' => $validated['harvest_date'],
|
||||
'notes' => $validated['notes'] ?? null,
|
||||
'sync_status' => 'synced'
|
||||
]);
|
||||
try {
|
||||
$panen = DB::transaction(function () use ($validated) {
|
||||
|
||||
$panenData = collect($validated)->except('commodities')->toArray();
|
||||
$panen = Panen::create($panenData);
|
||||
|
||||
return response()->json($panen, 201);
|
||||
foreach ($validated['commodities'] as $commodity) {
|
||||
\App\Models\Map\PanenTanaman::create([
|
||||
'panen_id' => $panen->id,
|
||||
'tanaman_id' => $commodity['plant_id'] ?? null,
|
||||
'commodity_name' => $commodity['commodity_name'],
|
||||
'weight_kg' => $commodity['weight_kg'],
|
||||
'sync_status' => 'synced',
|
||||
]);
|
||||
}
|
||||
|
||||
return $panen;
|
||||
});
|
||||
$panen->load('panenTanaman');
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Data panen dan detail tanaman berhasil disimpan.',
|
||||
'data' => $panen
|
||||
], 201);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Gagal menyimpan data ke database.',
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function show(string $id): JsonResponse
|
||||
{
|
||||
if (!Gate::any(['petani', 'fasilitator','super_admin'])) {
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Produktivitas;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
use App\Models\Map\Panen;
|
||||
|
||||
class PanenController01 extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
if (!Gate::any(['petani', 'fasilitator','super_admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$user = Auth::user();
|
||||
$profile = $user->profile;
|
||||
|
||||
$profileId = $user->role === 'petani' ? $profile->id : $request->id;
|
||||
$listPanen = Panen::when($profileId, fn($q,$v) => $q->where('farmer_id',$user->id))->get();
|
||||
|
||||
return response()->json($listPanen);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
if (!Gate::any(['petani', 'fasilitator','super_admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'land_id' => ['required', 'integer'],
|
||||
'plant_id' => ['required', 'integer'],
|
||||
'commodity_name' => ['required', 'string', 'max:255'],
|
||||
'weight_kg' => ['required', 'numeric'],
|
||||
'harvest_date' => ['required', 'date'],
|
||||
'notes' => ['nullable', 'string'],
|
||||
]);
|
||||
|
||||
$panen = Panen::create([
|
||||
'land_id' => $validated['land_id'],
|
||||
'plant_id' => $validated['plant_id'],
|
||||
'farmer_id' => Auth::user()->profile->id,
|
||||
'commodity_name' => $validated['commodity_name'],
|
||||
'weight_kg' => $validated['weight_kg'],
|
||||
'harvest_date' => $validated['harvest_date'],
|
||||
'notes' => $validated['notes'] ?? null,
|
||||
'sync_status' => 'synced'
|
||||
]);
|
||||
|
||||
return response()->json($panen, 201);
|
||||
}
|
||||
|
||||
public function show(string $id): JsonResponse
|
||||
{
|
||||
if (!Gate::any(['petani', 'fasilitator','super_admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
$panen = Panen::findOrFail($id);
|
||||
return response()->json($panen);
|
||||
}
|
||||
|
||||
public function update(Request $request, string $id): JsonResponse
|
||||
{
|
||||
if (!Gate::any(['petani', 'fasilitator','super_admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
$panen = Panen::findOrFail($id);
|
||||
|
||||
$validated = $request->validate([
|
||||
'commodity_name' => ['string', 'max:255'],
|
||||
'weight_kg' => ['numeric'],
|
||||
'harvest_date' => ['date'],
|
||||
'notes' => ['nullable', 'string'],
|
||||
]);
|
||||
|
||||
$panen->update($validated);
|
||||
return response()->json($panen);
|
||||
}
|
||||
|
||||
public function destroy(string $id): JsonResponse
|
||||
{
|
||||
Gate::authorize(['petani', 'fasilitator']);
|
||||
Panen::findOrFail($id)->delete();
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
|
||||
/**
|
||||
* Digunakan untuk sinkronisasi massal dari data lokal (offline)
|
||||
*/
|
||||
public function batchUpsert(Request $request): JsonResponse
|
||||
{
|
||||
if (Gate::authorize(['petani', 'fasilitator','super_admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
$profileId = Auth::user()->profile->id;
|
||||
|
||||
$validated = $request->validate([
|
||||
'panens' => ['required', 'array'],
|
||||
'panens.*.id' => ['nullable', 'integer'],
|
||||
'panens.*.land_id' => ['required', 'integer'],
|
||||
'panens.*.plant_id' => ['required', 'integer'],
|
||||
'panens.*.commodity_name' => ['required', 'string'],
|
||||
'panens.*.weight_kg' => ['required', 'numeric'],
|
||||
'panens.*.harvest_date' => ['required', 'date'],
|
||||
]);
|
||||
|
||||
$dataUpsert = [];
|
||||
foreach ($validated['panens'] as $item) {
|
||||
$dataUpsert[] = [
|
||||
'id' => $item['id'] ?? null,
|
||||
'land_id' => $item['land_id'],
|
||||
'plant_id' => $item['plant_id'],
|
||||
'farmer_id' => $profileId,
|
||||
'commodity_name' => $item['commodity_name'],
|
||||
'weight_kg' => $item['weight_kg'],
|
||||
'harvest_date' => $item['harvest_date'],
|
||||
'sync_status' => 'synced',
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
];
|
||||
}
|
||||
|
||||
// Melakukan upsert berdasarkan ID
|
||||
Panen::upsert($dataUpsert, ['id'], [
|
||||
'weight_kg', 'commodity_name', 'harvest_date', 'sync_status', 'updated_at'
|
||||
]);
|
||||
|
||||
return response()->json(['status' => 'success'], 200);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user