new functionality: Add project coding configuration feature for projects
This commit is contained in:
223
app/Http/Controllers/ProjectSettingsController.php
Normal file
223
app/Http/Controllers/ProjectSettingsController.php
Normal file
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\ProjectCodingConfig;
|
||||
use App\Models\ProjectDocumentStatus;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class ProjectSettingsController extends Controller
|
||||
{
|
||||
public function index(Project $project)
|
||||
{
|
||||
$this->authorize('update', $project);
|
||||
|
||||
//$project->load(['codingConfig', 'documentStatuses']);
|
||||
|
||||
return view('project-settings.index', compact('project'));
|
||||
}
|
||||
|
||||
public function updateCoding(Request $request, Project $project)
|
||||
{
|
||||
$this->authorize('update', $project);
|
||||
|
||||
$validated = $request->validate([
|
||||
'format' => 'required|string|max:255',
|
||||
'year_format' => 'required|in:Y,y,Yy,yy,Y-m,Y/m,y-m,y/m',
|
||||
'separator' => 'required|string|max:5',
|
||||
'sequence_length' => 'required|integer|min:1|max:10',
|
||||
'auto_generate' => 'boolean',
|
||||
'elements' => 'nullable|array',
|
||||
'reset_sequence' => 'boolean',
|
||||
]);
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$codingConfig = $project->codingConfig ?: new ProjectCodingConfig();
|
||||
$codingConfig->project_id = $project->id;
|
||||
|
||||
$codingConfig->fill([
|
||||
'format' => $validated['format'],
|
||||
'year_format' => $validated['year_format'],
|
||||
'separator' => $validated['separator'],
|
||||
'sequence_length' => $validated['sequence_length'],
|
||||
'auto_generate' => $validated['auto_generate'] ?? false,
|
||||
'elements' => $validated['elements'] ?? [],
|
||||
]);
|
||||
|
||||
if ($request->boolean('reset_sequence')) {
|
||||
$codingConfig->next_sequence = 1;
|
||||
}
|
||||
|
||||
$codingConfig->save();
|
||||
|
||||
DB::commit();
|
||||
|
||||
return redirect()->route('project-settings.index', $project)
|
||||
->with('success', 'Configuración de codificación actualizada correctamente.');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return redirect()->back()
|
||||
->with('error', 'Error al actualizar la configuración: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function storeStatus(Request $request, Project $project)
|
||||
{
|
||||
$this->authorize('update', $project);
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:100',
|
||||
'color' => 'required|string|max:7',
|
||||
'text_color' => 'nullable|string|max:7',
|
||||
'description' => 'nullable|string|max:500',
|
||||
'allow_upload' => 'boolean',
|
||||
'allow_edit' => 'boolean',
|
||||
'allow_delete' => 'boolean',
|
||||
'requires_approval' => 'boolean',
|
||||
'is_default' => 'boolean',
|
||||
]);
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
// Si se marca como default, quitar el default de otros estados
|
||||
if ($validated['is_default'] ?? false) {
|
||||
$project->documentStatuses()->update(['is_default' => false]);
|
||||
}
|
||||
|
||||
$status = new ProjectDocumentStatus($validated);
|
||||
$status->project_id = $project->id;
|
||||
$status->slug = Str::slug($validated['name']);
|
||||
|
||||
// Verificar que el slug sea único
|
||||
$counter = 1;
|
||||
$originalSlug = $status->slug;
|
||||
while ($project->documentStatuses()->where('slug', $status->slug)->exists()) {
|
||||
$status->slug = $originalSlug . '-' . $counter;
|
||||
$counter++;
|
||||
}
|
||||
|
||||
$status->save();
|
||||
|
||||
DB::commit();
|
||||
|
||||
return redirect()->route('project-settings.index', $project)
|
||||
->with('success', 'Estado creado correctamente.');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return redirect()->back()
|
||||
->with('error', 'Error al crear el estado: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function updateStatus(Request $request, Project $project, ProjectDocumentStatus $status)
|
||||
{
|
||||
$this->authorize('update', $project);
|
||||
|
||||
if ($status->project_id !== $project->id) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:100',
|
||||
'color' => 'required|string|max:7',
|
||||
'text_color' => 'nullable|string|max:7',
|
||||
'description' => 'nullable|string|max:500',
|
||||
'allow_upload' => 'boolean',
|
||||
'allow_edit' => 'boolean',
|
||||
'allow_delete' => 'boolean',
|
||||
'requires_approval' => 'boolean',
|
||||
'is_default' => 'boolean',
|
||||
]);
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
// Si se marca como default, quitar el default de otros estados
|
||||
if ($validated['is_default'] ?? false) {
|
||||
$project->documentStatuses()
|
||||
->where('id', '!=', $status->id)
|
||||
->update(['is_default' => false]);
|
||||
}
|
||||
|
||||
$status->update($validated);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return redirect()->route('project-settings.index', $project)
|
||||
->with('success', 'Estado actualizado correctamente.');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return redirect()->back()
|
||||
->with('error', 'Error al actualizar el estado: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function destroyStatus(Project $project, ProjectDocumentStatus $status)
|
||||
{
|
||||
$this->authorize('update', $project);
|
||||
|
||||
if ($status->project_id !== $project->id) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
// Verificar que no haya documentos con este estado
|
||||
if ($status->documents()->exists()) {
|
||||
return redirect()->back()
|
||||
->with('error', 'No se puede eliminar el estado porque hay documentos asociados.');
|
||||
}
|
||||
|
||||
// Si es el estado por defecto, establecer otro como default
|
||||
if ($status->is_default) {
|
||||
$newDefault = $project->documentStatuses()
|
||||
->where('id', '!=', $status->id)
|
||||
->first();
|
||||
|
||||
if ($newDefault) {
|
||||
$newDefault->update(['is_default' => true]);
|
||||
}
|
||||
}
|
||||
|
||||
$status->delete();
|
||||
|
||||
return redirect()->route('project-settings.index', $project)
|
||||
->with('success', 'Estado eliminado correctamente.');
|
||||
}
|
||||
|
||||
public function reorderStatuses(Request $request, Project $project)
|
||||
{
|
||||
$this->authorize('update', $project);
|
||||
|
||||
$request->validate([
|
||||
'order' => 'required|array',
|
||||
'order.*' => 'exists:project_document_statuses,id',
|
||||
]);
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
foreach ($request->order as $index => $statusId) {
|
||||
$status = ProjectDocumentStatus::find($statusId);
|
||||
if ($status && $status->project_id === $project->id) {
|
||||
$status->update(['order' => $index + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json(['success' => true]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return response()->json(['error' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user