mejoras en la gestión de proyectos y documentos: se añaden nuevos campos y validaciones para optimizar la organización y el seguimiento de los mismos.
This commit is contained in:
53
app/Http/Controllers/CompanyContactController.php
Normal file
53
app/Http/Controllers/CompanyContactController.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Company;
|
||||
use App\Models\User;
|
||||
|
||||
class CompanyContactController extends Controller
|
||||
{
|
||||
public function store(Request $request, Company $company)
|
||||
{
|
||||
$request->validate([
|
||||
'user_id' => 'required|exists:users,id',
|
||||
'position' => 'nullable|string|max:100'
|
||||
]);
|
||||
|
||||
// Evitar duplicados
|
||||
if (!$company->contacts()->where('user_id', $request->user_id)->exists()) {
|
||||
$company->contacts()->attach($request->user_id, [
|
||||
'position' => $request->position
|
||||
]);
|
||||
|
||||
return redirect()->back()
|
||||
->with('success', 'Contacto agregado exitosamente');
|
||||
}
|
||||
|
||||
return redirect()->back()
|
||||
->with('error', 'Este contacto ya está asociado a la empresa');
|
||||
}
|
||||
|
||||
public function update(Request $request, Company $company, User $contact)
|
||||
{
|
||||
$request->validate([
|
||||
'position' => 'nullable|string|max:100'
|
||||
]);
|
||||
|
||||
$company->contacts()->updateExistingPivot($contact->id, [
|
||||
'position' => $request->position
|
||||
]);
|
||||
|
||||
return redirect()->back()
|
||||
->with('success', 'Cargo del contacto actualizado');
|
||||
}
|
||||
|
||||
public function destroy(Company $company, User $contact)
|
||||
{
|
||||
$company->contacts()->detach($contact->id);
|
||||
|
||||
return redirect()->back()
|
||||
->with('success', 'Contacto eliminado de la empresa');
|
||||
}
|
||||
}
|
||||
143
app/Http/Controllers/CompanyController.php
Normal file
143
app/Http/Controllers/CompanyController.php
Normal file
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Company;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CompanyController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$companies = Company::orderBy('name')->paginate(10);
|
||||
return view('companies.index', compact('companies'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('companies.form');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'commercial_name' => 'required|string|max:255',
|
||||
'status' => 'required|in:active,closed',
|
||||
'address' => 'nullable|string|max:255',
|
||||
'postal_code' => 'nullable|string|max:10',
|
||||
'city' => 'nullable|string|max:100',
|
||||
'country' => 'nullable|string|max:100',
|
||||
'phone' => 'nullable|string|max:20',
|
||||
'email' => 'nullable|email|max:255',
|
||||
'cif' => 'nullable|string|max:20',
|
||||
'logo' => 'nullable|image|max:2048|mimes:jpg,jpeg,png,gif',
|
||||
]);
|
||||
|
||||
// Manejar la carga del logo
|
||||
if ($request->hasFile('logo')) {
|
||||
$validated['logo'] = $request->file('logo')->store('companies/logos', 'public');
|
||||
}
|
||||
|
||||
Company::create($validated);
|
||||
|
||||
return redirect()->route('companies.index')
|
||||
->with('success', 'Empresa creada exitosamente.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Company $company)
|
||||
{
|
||||
$company->load(['contacts' => function($query) {
|
||||
$query->withPivot('position');
|
||||
}]);
|
||||
|
||||
$contacts = $company->contacts()->paginate(5);
|
||||
$projects = $company->contacts()->paginate(5);//$company->projects()->paginate(5);
|
||||
$availableUsers = User::whereDoesntHave('companies', function($query) use ($company) {
|
||||
$query->where('company_id', $company->id);
|
||||
})->get();
|
||||
|
||||
return view('companies.show', compact('company', 'contacts', 'projects', 'availableUsers'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(Company $company)
|
||||
{
|
||||
|
||||
return view('companies.form', compact('company'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Company $company)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'commercial_name' => 'required|string|max:255',
|
||||
'status' => 'required|in:active,closed',
|
||||
'address' => 'nullable|string|max:255',
|
||||
'postal_code' => 'nullable|string|max:10',
|
||||
'city' => 'nullable|string|max:100',
|
||||
'country' => 'nullable|string|max:100',
|
||||
'phone' => 'nullable|string|max:20',
|
||||
'email' => 'nullable|email|max:255',
|
||||
'cif' => 'nullable|string|max:20',
|
||||
'logo' => 'nullable|image|max:2048|mimes:jpg,jpeg,png,gif',
|
||||
]);
|
||||
|
||||
// Manejar la actualización del logo
|
||||
if ($request->hasFile('logo')) {
|
||||
// Eliminar el logo anterior si existe
|
||||
if ($company->logo && Storage::disk('public')->exists($company->logo)) {
|
||||
Storage::disk('public')->delete($company->logo);
|
||||
}
|
||||
$validated['logo'] = $request->file('logo')->store('companies/logos', 'public');
|
||||
} elseif ($request->has('remove_logo')) {
|
||||
// Eliminar el logo si se seleccionó la opción de eliminar
|
||||
if ($company->logo && Storage::disk('public')->exists($company->logo)) {
|
||||
Storage::disk('public')->delete($company->logo);
|
||||
}
|
||||
$validated['logo'] = null;
|
||||
} else {
|
||||
// Mantener el logo existente
|
||||
$validated['logo'] = $company->logo;
|
||||
}
|
||||
|
||||
$company->update($validated);
|
||||
|
||||
return redirect()->route('companies.index')
|
||||
->with('success', 'Empresa actualizada exitosamente.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(Company $company)
|
||||
{
|
||||
// Eliminar el logo si existe
|
||||
if ($company->logo && Storage::disk('public')->exists($company->logo)) {
|
||||
Storage::disk('public')->delete($company->logo);
|
||||
}
|
||||
|
||||
$company->delete();
|
||||
|
||||
return redirect()->route('companies.index')
|
||||
->with('success', 'Empresa eliminada exitosamente.');
|
||||
}
|
||||
}
|
||||
@@ -19,8 +19,8 @@ class ProjectController extends Controller
|
||||
public function index()
|
||||
{
|
||||
$projects = auth()->user()->hasRole('admin')
|
||||
? Project::get() // Todos los proyectos para admin
|
||||
: auth()->user()->projects()->latest()->get(); // Solo proyectos asignados
|
||||
? Project::get() // Todos los proyectos para admin
|
||||
: auth()->user()->projects()->latest()->get(); // Solo proyectos asignados
|
||||
|
||||
/*
|
||||
$projects = Project::whereHas('users', function($query) {
|
||||
@@ -43,6 +43,7 @@ class ProjectController extends Controller
|
||||
'project' => $project,
|
||||
'categories' => Category::orderBy('name')->get(),
|
||||
'users' => User::where('id', '!=', auth()->id())->get(),
|
||||
'companies' => \App\Models\Company::all(), // Pass companies to the view
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -65,9 +66,8 @@ class ProjectController extends Controller
|
||||
'start_date' => 'nullable|date',
|
||||
'deadline' => 'nullable|date|after:start_date',
|
||||
'categories' => 'nullable|array|exists:categories,id',
|
||||
//'categories' => 'required|array',
|
||||
//'categories.*' => 'exists:categories,id',
|
||||
'project_image_path' => 'nullable|string',
|
||||
'company_id' => 'required|exists:companies,id', // Validate company_id
|
||||
]);
|
||||
|
||||
|
||||
@@ -82,8 +82,8 @@ class ProjectController extends Controller
|
||||
$tempPath = $request->project_image_path;
|
||||
$newPath = 'images/projects/' . basename($tempPath);
|
||||
|
||||
Storage::move($tempPath, $newPath); // Mover el archivo
|
||||
$validated['project_image_path'] = $newPath; // Actualizar path
|
||||
Storage::move($tempPath, $newPath); // Mover el archivo
|
||||
$validated['project_image_path'] = $newPath; // Actualizar path
|
||||
}
|
||||
|
||||
// Crear el proyecto con todos los datos validados
|
||||
@@ -111,6 +111,7 @@ class ProjectController extends Controller
|
||||
'project' => $project,
|
||||
'categories' => Category::orderBy('name')->get(),
|
||||
'users' => User::where('id', '!=', auth()->id())->get(),
|
||||
'companies' => \App\Models\Company::all(), // Pass companies to the view
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -135,8 +136,31 @@ class ProjectController extends Controller
|
||||
public function update(Request $request, Project $project)
|
||||
{
|
||||
$this->authorize('update', $project);
|
||||
// Lógica de actualización
|
||||
$project->update($request->all());
|
||||
|
||||
$validated = $request->validate([
|
||||
'reference' => 'required|string|max:255',
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'status' => 'required|in:Activo,Inactivo',
|
||||
'address' => 'nullable|string|max:255',
|
||||
'province' => 'nullable|string|max:100',
|
||||
'country' => 'nullable|string|size:2',
|
||||
'postal_code' => 'nullable|string|max:10',
|
||||
'latitude' => 'required|numeric|between:-90,90',
|
||||
'longitude' => 'required|numeric|between:-180,180',
|
||||
'start_date' => 'nullable|date',
|
||||
'deadline' => 'nullable|date|after:start_date',
|
||||
'categories' => 'nullable|array|exists:categories,id',
|
||||
'project_image_path' => 'nullable|string',
|
||||
'company_id' => 'required|exists:companies,id', // Validate company_id
|
||||
]);
|
||||
|
||||
$project->update($validated);
|
||||
|
||||
if ($request->has('categories')) {
|
||||
$project->categories()->sync($request->categories);
|
||||
}
|
||||
|
||||
return redirect()->route('projects.show', $project)->with('success', 'Project updated successfully.');
|
||||
}
|
||||
|
||||
|
||||
@@ -65,6 +65,8 @@ class UserController extends Controller
|
||||
'email' => 'required|email|unique:users',
|
||||
'phone' => 'nullable|string|max:20',
|
||||
'address' => 'nullable|string|max:255',
|
||||
'user_type' => 'required|integer|in:0,1,2',
|
||||
'company_id' => 'nullable|exists:companies,id', // Si se usa una relación con empresas
|
||||
'profile_photo_path' => 'nullable|string' // Ruta de la imagen subida por Livewire
|
||||
]);
|
||||
|
||||
@@ -80,7 +82,9 @@ class UserController extends Controller
|
||||
'address' => $validated['address'],
|
||||
'access_start' => $validated['start_date'],
|
||||
'access_end' => $validated['end_date'],
|
||||
'is_active' => true,
|
||||
'is_active' => $validated['is_active'] ?? false, // Por defecto, inactivo
|
||||
'user_type' => $validated['user_type'] ?? 0, // 0: Usuario, 1: Administrador, 2: Super Admin
|
||||
'company_id' => $validated['company_id'] ?? null, // Si se usa una relación con empresas
|
||||
'profile_photo_path' => $validated['profile_photo_path'] ?? null
|
||||
]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user