44 lines
1.4 KiB
PHP
Executable File
44 lines
1.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\DailySale;
|
|
use App\Models\Month;
|
|
use App\Services\CommissionCalculator;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
/**
|
|
* Mostrar dashboard del usuario
|
|
*/
|
|
public function index()
|
|
{
|
|
$user = Auth::user();
|
|
|
|
// Obtener mes actual o último mes
|
|
$currentMonth = $user->getCurrentMonth();
|
|
|
|
// Si no hay mes abierto, buscar el último mes
|
|
if (!$currentMonth) {
|
|
$currentMonth = $user->months()
|
|
->orderBy('year', 'desc')
|
|
->orderByRaw("FIELD(name, 'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre') DESC")
|
|
->first();
|
|
}
|
|
|
|
$data = null;
|
|
if ($currentMonth) {
|
|
$data = CommissionCalculator::calculateForMonth($user, $currentMonth);
|
|
}
|
|
|
|
// Últimos meses del usuario
|
|
$recentMonths = $user->months()
|
|
->orderBy('year', 'desc')
|
|
->orderByRaw("FIELD(name, 'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre') DESC")
|
|
->limit(6)
|
|
->get();
|
|
|
|
return view('dashboard.index', compact('currentMonth', 'data', 'recentMonths'));
|
|
}
|
|
} |