partial update create kpi and progress bar
This commit is contained in:
237
app/Http/Controllers/KPI/TargetsController.php
Normal file
237
app/Http/Controllers/KPI/TargetsController.php
Normal file
@@ -0,0 +1,237 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\KPI;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\KPI\StoreKpiTargetRequest;
|
||||
use App\Http\Requests\KPI\UpdateKpiTargetRequest;
|
||||
use App\Models\KpiTarget;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class TargetsController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of KPI targets
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$targets = KpiTarget::with(['user', 'user.role'])
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate(15);
|
||||
|
||||
// Get mechanics using role_id 3 (mechanic) with dealer relationship
|
||||
$mechanics = User::with('dealer')
|
||||
->where('role_id', 3)
|
||||
->orderBy('name', 'asc')
|
||||
->limit(50)
|
||||
->get();
|
||||
|
||||
// If no mechanics found, get all users as fallback
|
||||
if ($mechanics->isEmpty()) {
|
||||
$mechanics = User::with('dealer')
|
||||
->orderBy('name', 'asc')
|
||||
->limit(50)
|
||||
->get();
|
||||
}
|
||||
|
||||
return view('kpi.targets.index', compact('targets', 'mechanics'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new KPI target
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
// Get mechanics using role_id 3 (mechanic) with dealer relationship
|
||||
$mechanics = User::with('dealer')
|
||||
->where('role_id', 3)
|
||||
->orderBy('name', 'asc')
|
||||
->limit(50)
|
||||
->get();
|
||||
|
||||
// Debug: Log the mechanics found
|
||||
Log::info('Mechanics found for KPI target creation:', [
|
||||
'count' => $mechanics->count(),
|
||||
'mechanics' => $mechanics->pluck('name', 'id')->toArray()
|
||||
]);
|
||||
|
||||
// If no mechanics found, get all users as fallback
|
||||
if ($mechanics->isEmpty()) {
|
||||
$mechanics = User::with('dealer')
|
||||
->orderBy('name', 'asc')
|
||||
->limit(50)
|
||||
->get();
|
||||
|
||||
Log::warning('No mechanics found, using all users as fallback', [
|
||||
'count' => $mechanics->count()
|
||||
]);
|
||||
}
|
||||
|
||||
return view('kpi.targets.create', compact('mechanics'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created KPI target
|
||||
*/
|
||||
public function store(StoreKpiTargetRequest $request)
|
||||
{
|
||||
try {
|
||||
// Log the validated data
|
||||
Log::info('Creating KPI target with data:', $request->validated());
|
||||
|
||||
// Check if user already has an active target and deactivate it
|
||||
$existingTarget = KpiTarget::where('user_id', $request->user_id)
|
||||
->where('is_active', true)
|
||||
->first();
|
||||
|
||||
if ($existingTarget) {
|
||||
Log::info('Deactivating existing active KPI target', [
|
||||
'user_id' => $request->user_id,
|
||||
'existing_target_id' => $existingTarget->id
|
||||
]);
|
||||
|
||||
// Deactivate the existing target
|
||||
$existingTarget->update(['is_active' => false]);
|
||||
}
|
||||
|
||||
$target = KpiTarget::create($request->validated());
|
||||
|
||||
Log::info('KPI target created successfully', [
|
||||
'target_id' => $target->id,
|
||||
'user_id' => $target->user_id
|
||||
]);
|
||||
|
||||
return redirect()->route('kpi.targets.index')
|
||||
->with('success', 'Target KPI berhasil ditambahkan');
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to create KPI target', [
|
||||
'error' => $e->getMessage(),
|
||||
'data' => $request->validated()
|
||||
]);
|
||||
|
||||
return redirect()->back()
|
||||
->withInput()
|
||||
->with('error', 'Gagal menambahkan target KPI: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified KPI target
|
||||
*/
|
||||
public function show(KpiTarget $target)
|
||||
{
|
||||
$target->load(['user.dealer', 'achievements']);
|
||||
|
||||
return view('kpi.targets.show', compact('target'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified KPI target
|
||||
*/
|
||||
public function edit(KpiTarget $target)
|
||||
{
|
||||
// Debug: Check if target is loaded correctly
|
||||
if (!$target) {
|
||||
abort(404, 'Target KPI tidak ditemukan');
|
||||
}
|
||||
|
||||
// Load target with user relationship
|
||||
$target->load('user');
|
||||
|
||||
// Get mechanics using role_id 3 (mechanic) with dealer relationship
|
||||
$mechanics = User::with('dealer')
|
||||
->where('role_id', 3)
|
||||
->orderBy('name', 'asc')
|
||||
->limit(50)
|
||||
->get();
|
||||
|
||||
// If no mechanics found, get all users as fallback
|
||||
if ($mechanics->isEmpty()) {
|
||||
$mechanics = User::with('dealer')
|
||||
->orderBy('name', 'asc')
|
||||
->limit(50)
|
||||
->get();
|
||||
}
|
||||
|
||||
// Ensure data types are correct for comparison
|
||||
$target->user_id = (int)$target->user_id;
|
||||
|
||||
return view('kpi.targets.edit', compact('target', 'mechanics'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified KPI target
|
||||
*/
|
||||
public function update(UpdateKpiTargetRequest $request, KpiTarget $target)
|
||||
{
|
||||
try {
|
||||
$target->update($request->validated());
|
||||
|
||||
return redirect()->route('kpi.targets.index')
|
||||
->with('success', 'Target KPI berhasil diperbarui');
|
||||
} catch (\Exception $e) {
|
||||
return redirect()->back()
|
||||
->withInput()
|
||||
->with('error', 'Gagal memperbarui target KPI: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified KPI target
|
||||
*/
|
||||
public function destroy(KpiTarget $target)
|
||||
{
|
||||
try {
|
||||
$target->delete();
|
||||
|
||||
return redirect()->route('kpi.targets.index')
|
||||
->with('success', 'Target KPI berhasil dihapus');
|
||||
} catch (\Exception $e) {
|
||||
return redirect()->back()
|
||||
->with('error', 'Gagal menghapus target KPI: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle active status of KPI target
|
||||
*/
|
||||
public function toggleStatus(KpiTarget $target)
|
||||
{
|
||||
try {
|
||||
$target->update(['is_active' => !$target->is_active]);
|
||||
|
||||
$status = $target->is_active ? 'diaktifkan' : 'dinonaktifkan';
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => "Target KPI berhasil {$status}",
|
||||
'is_active' => $target->is_active
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Gagal mengubah status target KPI'
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get KPI targets for specific user
|
||||
*/
|
||||
public function getUserTargets(User $user)
|
||||
{
|
||||
$targets = $user->kpiTargets()
|
||||
->with('achievements')
|
||||
->orderBy('year', 'desc')
|
||||
->orderBy('month', 'desc')
|
||||
->get();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => $targets
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,28 @@ class TransactionController extends Controller
|
||||
->where('active', true)
|
||||
->get();
|
||||
|
||||
return view('transaction.index', compact('now', 'wash_work', 'work_works', 'user_sas', 'count_transaction_users', 'count_transaction_dealers', 'mechanic', 'products'));
|
||||
// Get KPI data for current user using KPI service
|
||||
$kpiService = app(\App\Services\KpiService::class);
|
||||
|
||||
// Auto-calculate current month KPI achievement to ensure data is up-to-date
|
||||
$kpiService->calculateKpiAchievement(Auth::user());
|
||||
|
||||
$kpiSummary = $kpiService->getKpiSummary(Auth::user());
|
||||
|
||||
// Get current month period name
|
||||
$currentMonthName = now()->translatedFormat('F Y');
|
||||
|
||||
$kpiData = [
|
||||
'target' => $kpiSummary['current_target'] ? $kpiSummary['current_target']->target_value : 0,
|
||||
'actual' => $kpiSummary['current_achievement'] ? $kpiSummary['current_achievement']->actual_value : 0,
|
||||
'percentage' => $kpiSummary['current_percentage'],
|
||||
'status' => $kpiSummary['current_achievement'] ? $kpiSummary['current_achievement']->status : 'pending',
|
||||
'status_color' => $kpiSummary['current_achievement'] ? $kpiSummary['current_achievement']->status_color : 'secondary',
|
||||
'period' => $currentMonthName,
|
||||
'has_target' => $kpiSummary['current_target'] ? true : false
|
||||
];
|
||||
|
||||
return view('transaction.index', compact('now', 'wash_work', 'work_works', 'user_sas', 'count_transaction_users', 'count_transaction_dealers', 'mechanic', 'products', 'kpiData'));
|
||||
}
|
||||
|
||||
public function workcategory($category_id)
|
||||
|
||||
59
app/Http/Requests/KPI/StoreKpiTargetRequest.php
Normal file
59
app/Http/Requests/KPI/StoreKpiTargetRequest.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\KPI;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class StoreKpiTargetRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'user_id' => 'required|exists:users,id',
|
||||
'target_value' => 'required|integer|min:1',
|
||||
'description' => 'nullable|string|max:1000',
|
||||
'is_active' => 'boolean'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom messages for validator errors.
|
||||
*/
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'user_id.required' => 'Mekanik harus dipilih',
|
||||
'user_id.exists' => 'Mekanik yang dipilih tidak valid',
|
||||
'target_value.required' => 'Target nilai harus diisi',
|
||||
'target_value.integer' => 'Target nilai harus berupa angka',
|
||||
'target_value.min' => 'Target nilai minimal 1',
|
||||
'description.max' => 'Deskripsi maksimal 1000 karakter',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the data for validation.
|
||||
*/
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$this->merge([
|
||||
'is_active' => $this->boolean('is_active', true)
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
59
app/Http/Requests/KPI/UpdateKpiTargetRequest.php
Normal file
59
app/Http/Requests/KPI/UpdateKpiTargetRequest.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\KPI;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class UpdateKpiTargetRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'user_id' => 'required|exists:users,id',
|
||||
'target_value' => 'required|integer|min:1',
|
||||
'description' => 'nullable|string|max:1000',
|
||||
'is_active' => 'boolean'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom messages for validator errors.
|
||||
*/
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'user_id.required' => 'Mekanik harus dipilih',
|
||||
'user_id.exists' => 'Mekanik yang dipilih tidak valid',
|
||||
'target_value.required' => 'Target nilai harus diisi',
|
||||
'target_value.integer' => 'Target nilai harus berupa angka',
|
||||
'target_value.min' => 'Target nilai minimal 1',
|
||||
'description.max' => 'Deskripsi maksimal 1000 karakter',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the data for validation.
|
||||
*/
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$this->merge([
|
||||
'is_active' => $this->boolean('is_active', true)
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user