Initial commit: Sistema de comisiones y gastos personales
This commit is contained in:
163
app/Http/Controllers/MonthController.php
Executable file
163
app/Http/Controllers/MonthController.php
Executable file
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Month;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class MonthController extends Controller
|
||||
{
|
||||
/**
|
||||
* Listar todos los meses
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
$months = $user->months()
|
||||
->orderBy('year', 'desc')
|
||||
->orderByRaw("FIELD(name, 'Diciembre', 'Noviembre', 'Octubre', 'Septiembre', 'Agosto', 'Julio', 'Junio', 'Mayo', 'Abril', 'Marzo', 'Febrero', 'Enero')")
|
||||
->paginate(12);
|
||||
|
||||
return view('months.index', compact('months'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Mostrar formulario para crear mes
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('months.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Guardar nuevo mes
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => ['required', 'string', 'max:50'],
|
||||
'year' => ['required', 'integer', 'min:2020', 'max:2100'],
|
||||
]);
|
||||
|
||||
// Verificar que no exista ya el mes para el usuario
|
||||
$exists = $user->months()
|
||||
->where('name', $validated['name'])
|
||||
->where('year', $validated['year'])
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
return back()->withErrors([
|
||||
'name' => 'Ya existe un mes con ese nombre y año.',
|
||||
])->withInput();
|
||||
}
|
||||
|
||||
$user->months()->create([
|
||||
'name' => $validated['name'],
|
||||
'year' => $validated['year'],
|
||||
'status' => 'open',
|
||||
]);
|
||||
|
||||
return redirect()->route('months.index')
|
||||
->with('success', 'Mes creado correctamente.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Mostrar detalles del mes
|
||||
*/
|
||||
public function show(Month $month)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($month->user_id !== $user->id) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$month->load(['dailySales', 'expenses']);
|
||||
|
||||
return view('months.show', compact('month'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Mostrar formulario de edición
|
||||
*/
|
||||
public function edit(Month $month)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($month->user_id !== $user->id) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
return view('months.edit', compact('month'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Actualizar mes
|
||||
*/
|
||||
public function update(Request $request, Month $month)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($month->user_id !== $user->id) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => ['required', 'string', 'max:50'],
|
||||
'year' => ['required', 'integer', 'min:2020', 'max:2100'],
|
||||
'status' => ['required', 'in:open,closed,paid'],
|
||||
]);
|
||||
|
||||
$month->update($validated);
|
||||
|
||||
return redirect()->route('months.show', $month->id)
|
||||
->with('success', 'Mes actualizado correctamente.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Cerrar mes
|
||||
*/
|
||||
public function close(Month $month)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($month->user_id !== $user->id) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
if ($month->status !== 'open') {
|
||||
return back()->with('error', 'Solo se pueden cerrar meses abiertos.');
|
||||
}
|
||||
|
||||
$month->update(['status' => 'closed']);
|
||||
|
||||
return redirect()->route('months.show', $month->id)
|
||||
->with('success', 'Mes cerrado correctamente.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Eliminar mes
|
||||
*/
|
||||
public function destroy(Month $month)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($month->user_id !== $user->id) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
// Verificar que no tenga ventas o gastos asociados
|
||||
if ($month->dailySales()->count() > 0 || $month->expenses()->count() > 0) {
|
||||
return back()->with('error', 'No puedes eliminar un mes que tiene ventas o gastos asociados.');
|
||||
}
|
||||
|
||||
$month->delete();
|
||||
|
||||
return redirect()->route('months.index')
|
||||
->with('success', 'Mes eliminado correctamente.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user