get('month_id'); if ($monthId) { $month = $user->months()->findOrFail($monthId); } else { $month = $user->getCurrentMonth(); if (!$month) { $month = $user->months()->latest()->first(); } } if (!$month) { return redirect()->route('dashboard')->with('info', 'No hay meses disponibles.'); } $report = CommissionCalculator::calculateForMonth($user, $month); // Cargar ventas y gastos detalles $dailySales = $month->dailySales() ->orderBy('date', 'asc') ->get(); $expenses = $month->expenses() ->orderBy('date', 'desc') ->get(); $months = $user->months() ->orderBy('year', 'desc') ->orderByRaw("FIELD(name, 'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre') DESC") ->get(); return view('reports.monthly', compact('report', 'month', 'dailySales', 'expenses', 'months')); } /** * Mostrar reporte quincenal */ public function biweekly(Request $request) { $user = Auth::user(); $monthId = $request->get('month_id'); $biweekly = $request->get('biweekly', 1); if ($monthId) { $month = $user->months()->findOrFail($monthId); } else { $month = $user->getCurrentMonth(); if (!$month) { $month = $user->months()->latest()->first(); } } if (!$month) { return redirect()->route('dashboard')->with('info', 'No hay meses disponibles.'); } $report = CommissionCalculator::calculateBiweekly($user, $month, $biweekly); // Obtener ventas del mes $dailySales = $month->dailySales() ->orderBy('date', 'asc') ->get(); // Obtener gastos según la quincena seleccionada $expenses = $month->expenses() ->where(function($query) use ($biweekly) { if ($biweekly === 1) { $query->where('expense_type', 'q1') ->orWhere('expense_type', 'mensual'); } else { $query->where('expense_type', 'q2') ->orWhere('expense_type', 'mensual'); } }) ->orderBy('date', 'desc') ->get(); $months = $user->months() ->orderBy('year', 'desc') ->orderByRaw("FIELD(name, 'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre') DESC") ->get(); return view('reports.biweekly', compact('report', 'month', 'dailySales', 'expenses', 'months', 'biweekly')); } /** * Resumen anual */ public function yearly(Request $request) { $user = Auth::user(); $year = $request->get('year', now()->year); $report = CommissionCalculator::calculateYearly($user, $year); // Obtener meses del año $months = $user->months() ->where('year', $year) ->orderByRaw("FIELD(name, 'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre')") ->get(); // Años disponibles $years = $user->months() ->select('year') ->distinct() ->orderBy('year', 'desc') ->pluck('year'); return view('reports.yearly', compact('report', 'year', 'months', 'years')); } /** * Obtener número del mes por nombre */ private static function getMonthNumber(string $monthName): int { $months = [ 'Enero' => 1, 'Febrero' => 2, 'Marzo' => 3, 'Abril' => 4, 'Mayo' => 5, 'Junio' => 6, 'Julio' => 7, 'Agosto' => 8, 'Septiembre' => 9, 'Octubre' => 10, 'Noviembre' => 11, 'Diciembre' => 12 ]; return $months[$monthName] ?? 1; } }