Untitled
unknown
plain_text
7 months ago
12 kB
1
Indexable
Never
<?php namespace App\Http\Controllers\Api; use App\Models\AbsenceEmploye; use App\Http\Controllers\Controller; use App\Http\Requests\StoreAbsenceEmployeRequest; use App\Http\Requests\UpdateAbsenceEmployeRequest; use App\Models\AbsenceActivity; use App\Models\Activities; use App\Models\Employee; use App\Models\Field; use App\Models\FieldAbsence; use App\Models\FieldOwner; use App\Models\GroupAbsence; use App\Models\Plant; use App\Models\PlantsAbsence; use App\Models\Wages; use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Str; class AbsenceEmployeApiController extends Controller { public function getByAbsenceId($id) { $data = AbsenceEmploye::with([ 'getEmploye', 'fieldAbsences.field', 'groupAbsences', 'groupAbsences.absenceActivities', 'groupAbsences.plantsAbsences', 'groupAbsences.plantsAbsences.plant', ])->findOrFail($id); $groupData = $data->groupAbsences->map(function ($group) { $plantsData = $group->plantsAbsences->map(function ($plantsAbsence) { return [ 'plants_id' => $plantsAbsence->plants_id, 'plant_name' => $plantsAbsence->plant->name, ]; }); $activityData = $group->absenceActivities->map(function ($activityAbsence) { return [ 'activity_id' => $activityAbsence->id, 'activities' => $activityAbsence->activities, ]; }); return [ 'name_lahan' => $group->name, 'activity' => $activityData->toArray(), 'plants' => $plantsData->toArray(), ]; }); $fieldData = $data->fieldAbsences->map(function ($field) { return [ 'field_id' => $field->id, 'name' => $field->field->name, ]; }); $response = [ 'employee' => $data->getEmploye, 'start_work' => $data->start_work, 'end_work' => $data->end_work, 'note' => $data->note, 'date' => $data->date, 'employees_id' => $data->employees_id, 'hok' => $data->hok, 'wages' => $data->wages, 'group' => $groupData->toArray(), 'field' => $fieldData->toArray(), ]; return response()->json(['data' => $response], 200); } public function getAll() { try { $data = Employee::get(); foreach ($data as $employee) { $employee->date = Carbon::parse($employee->date)->translatedFormat('d F Y'); } return response()->json(['data' => $data], 200); } catch (\Exception $e) { return response()->json(['error' => 'Failed to retrieve data', 'message' => $e->getMessage()], 500); } } public function getById($date) { try { $absences = AbsenceEmploye::whereDate('created_at', $date)->get(); $data = []; foreach ($absences as $absence) { $absenceData = AbsenceEmploye::join('employees', 'employees.id', '=', 'absence_employes.employees_id') ->where('absence_employes.employees_id', $absence->employees_id) ->select('employees.name', 'absence_employes.*') ->orderBy('absence_employes.created_at', 'desc') ->get(); $data = array_merge($data, $absenceData->toArray()); } if (empty($data)) { return response()->json(['message' => 'No records found for the specified date'], 404); } foreach ($data as $absence) { $absence['date'] = Carbon::parse($absence['date'])->translatedFormat('d F Y'); unset($absence['activity_1'], $absence['activity_2'], $absence['activity_3'], $absence['author_id'], $absence['fields_id']); } return response()->json(['data' => $data], 200); } catch (\Exception $e) { return response()->json(['error' => 'Failed to retrieve data', 'message' => $e->getMessage()], 500); } } public function update_absen(Request $request, $id) { try { $validator = Validator::make($request->all(), [ 'date' => 'required|date', 'employees_id' => 'required|exists:employees,id', 'hok' => 'required', 'wages' => 'required|integer', ]); if ($validator->fails()) { return response()->json(['errors' => $validator->errors()], 400); } $absenceEmploye = AbsenceEmploye::find($id); if (!$absenceEmploye) { return response()->json(['error' => 'Record not found'], 404); } $absenceEmploye->update($request->all()); return response()->json(['message' => 'Record updated successfully', 'data' => $absenceEmploye], 200); } catch (\Exception $e) { return response()->json(['error' => 'Failed to retrieve data', 'message' => $e->getMessage()], 500); } } public function delete($id) { try { $absenceEmploye = AbsenceEmploye::find($id); if (!$absenceEmploye) { return response()->json(['error' => 'Record not found'], 404); } $absenceEmploye->fieldAbsences()->delete(); $absenceEmploye->delete(); return response()->json(['message' => 'Record deleted successfully'], 200); } catch (\Exception $e) { return response()->json(['error' => 'Error deleting record'], 500); } } public function store_absen(Request $request) { try { // return $request; $validator = Validator::make($request->all(), [ 'date' => 'required|date', 'hok' => 'required', 'start_work' => 'required|date_format:H:i', 'end_work' => 'required|date_format:H:i|after:start_work', 'wages' => 'required|numeric', 'note' => 'required', 'author_id' => 'required|exists:users,id', // Adjust 'users' to your actual users table 'name' => 'required|string', // 'data' => 'required|array', 'name_lahan' => 'required|array', // 'name_lahan' => 'required|integer', 'label' => 'required|array', 'label.*' => 'required|integer', 'activities' => 'required|array', 'activities.*' => 'required|string', 'plants' => 'required|array', 'plants.*' => 'required|string', ]); if (is_array($request->name_lahan) && in_array(null, $request->name_lahan, true)) { return redirect()->back()->with('error', json_encode(['message' => 'Harap isi form Pilih Lahan'])); } if ($request['hok'] === "Pilih HOK") { return redirect()->back()->with('error', json_encode(['message' => 'Harap isi form HOK'])); } if ($validator->fails()) { $errors = $validator->errors(); if ($errors->has('plants') && $validator->errors()->count() === 1) { return redirect()->back()->with('error', json_encode(['message' => 'Harap isi form jenis tanaman'])); } return redirect()->back()->with('error', json_encode(['message' => 'Harap isi form absen dengan lengkap'])); } $wages = (int) str_replace('.', '', $request['wages']); $data = [ 'date' => $request['date'], 'start_work' => $request['start_work'], 'end_work' => $request['end_work'], 'hok' => $request['hok'], 'wages' => $wages, 'note' => $request['note'], 'author_id' => $request['author_id'] ]; $employee = Employee::firstOrCreate( ['name' => $request['name']], ['name' => $request['name'], 'gender' => 'male', 'address' => null, 'author_id' => $request['author_id']] ); $data['employees_id'] = $employee->id; $data['fields_id'] = 1; $absenceEmploye = AbsenceEmploye::create($data); $name_lahans = []; foreach ($request['name_lahan'] as $key) { $name_lahans[] = Field::where('id', $key)->get(); } $form = [ 'name_lahan' => $name_lahans, 'label' => $request['label'], 'activities' => $request['activities'], 'plants' => $request['plants'], ]; foreach ($form['label'] as $index => $a) { $label = $form['label'][$index]; $plant = $form['plants'][$index]; $name_lahan = $form['name_lahan'][$index]; $names = $name_lahan->pluck('name')->toArray(); foreach ($names as $name) { $field = Field::firstOrCreate( ['name' => $name], ['name' => $name] ); FieldOwner::firstOrCreate( ['fields_id' => $field->id], ['user_id' => $request['author_id']] ); $slug = Str::slug($plant); $plantModel = Plant::firstOrCreate( ['name' => $plant], ['slug' => $slug] ); $plantModel->update(['last_update' => Carbon::now()->format('Y-m-d')]); $group = GroupAbsence::firstOrCreate(['name' => 'Data #' . $label, 'absence_employes_id' => $absenceEmploye->id]); $fieldAbsence = FieldAbsence::create([ 'absence_employes_id' => $absenceEmploye->id, 'employees_id' => $employee->id, 'fields_id' => $field->id, 'group_absences_id' => $group->id, ]); $activity = Activities::firstOrCreate(['activities' => $form['activities'][$index]]); $absence_id = AbsenceActivity::create([ 'absence_employes_id' => $absenceEmploye->id, 'activities' => $form['activities'][$index], 'group_absences_id' => $group->id ]); $field_id = PlantsAbsence::create([ 'absence_employes_id' => $absenceEmploye->id, 'plants_id' => $plantModel->id, 'group_absences_id' => $group->id ]); } } return redirect()->back()->with('success', json_encode(['message' => 'Absen berhasil'])); } catch (\Exception $e) { return redirect()->back()->with('error', json_encode(['message' => 'Absen gagal'])); } } }
Leave a Comment