@@ -17,22 +17,25 @@ class DashboardController extends Controller
1717 public function index ()
1818 {
1919 $ user = Auth::user ();
20- $ productos = BlProducto::with (['color ' , 'empaques.movimientos ' ])
20+ $ productos = BlProducto::with ('color ' )
21+ ->withSum (['empaques as stock_total ' => function ($ q ) {
22+ $ q ->where ('estado ' , 'disponible ' );
23+ }], 'cantidad_por_empaque ' )
2124 ->get ()
25+ ->filter (function ($ producto ) {
26+ return $ producto ->stock_total > 0 ; // ✅ filtrar en memoria
27+ })
28+ ->take (10 )
2229 ->map (function ($ producto ) {
2330 return [
2431 'id ' => $ producto ->id ,
2532 'tipo_producto ' => $ producto ->tipo_producto ,
2633 'tamanio ' => $ producto ->tamanio ,
2734 'color_nombre ' => $ producto ->color ->codigo ,
2835 'descripcion ' => $ producto ->descripcion ,
29- 'stock_total ' => $ producto ->empaques ->sum (function ($ empaque ) {
30- return $ empaque ->movimientos ->sum ('cantidad ' ) * $ empaque ->cantidad_por_empaque ;
31- }),
36+ 'stock_total ' => $ producto ->stock_total ?? 0 ,
3237 ];
33- })
34- ->where ('stock_total ' , '> ' , 1 )
35- ->take (10 );
38+ });
3639 // dd($productos->pluck('descripcion'));
3740 $ rankingProductos = BLPedido::with (['items.empaque.producto ' ])
3841 ->get ()
@@ -63,7 +66,7 @@ public function index()
6366
6467 $ pedidosEspera = BLPedido::get ()
6568 ->where ('estado ' , 'pendiente ' );
66- $ movimientos = BlMovimiento::with (['empaque.producto ' ])->orderByDesc ('created_at ' )->take (6 )->get ();
69+ $ movimientos = BlMovimiento::with (['movible ' ])->orderByDesc ('created_at ' )->take (6 )->get ();
6770 // dd($movimientos);
6871 $ produccion = $ this ->produccionSemanal ();
6972
@@ -78,49 +81,49 @@ public function index()
7881 ]);
7982 }
8083
81- public function produccionSemanal ()
82- {
83- $ dias = [
84- 'Monday ' => 'Lun ' ,
85- 'Tuesday ' => 'Mar ' ,
86- 'Wednesday ' => 'Mié ' ,
87- 'Thursday ' => 'Jue ' ,
88- 'Friday ' => 'Vie ' ,
89- 'Saturday ' => 'Sáb ' ,
90- 'Sunday ' => 'Dom ' ,
91- ];
92-
93- // Rango de la semana actual (Lunes a Domingo)
94- $ inicioSemana = \Carbon \Carbon::now ('America/Bogota ' )->startOfWeek ();
95- $ finSemana = \Carbon \Carbon::now ('America/Bogota ' )->endOfWeek ();
96-
97- // Obtener registros agrupados por fecha usando updated_at
98- $ registros = BLPedidoItem::where ('estado ' , 'completado ' )
99- ->whereBetween ('updated_at ' , [$ inicioSemana , $ finSemana ])
100- ->get ()
101- ->groupBy (function ($ item ) {
102- return \Carbon \Carbon::parse ($ item ->updated_at )
103- ->setTimezone ('America/Bogota ' )
104- ->format ('Y-m-d ' );
105- });
106-
107- // Crear la estructura de la semana completa
108- $ data = collect ();
109- for ($ fecha = $ inicioSemana ->copy (); $ fecha <= $ finSemana ; $ fecha ->addDay ()) {
110- $ fechaStr = $ fecha ->format ('Y-m-d ' );
111- $ grupo = $ registros ->get ($ fechaStr , collect ());
112-
113- $ data ->push ([
114- 'fecha ' => $ fechaStr ,
115- 'dia ' => $ dias [$ fecha ->format ('l ' )],
116- 'produccion ' => $ grupo ->sum ('cantidad_empaques ' ),
117- 'timestamps ' => $ grupo ->pluck ('updated_at ' ),
118- ]);
84+ public function produccionSemanal ()
85+ {
86+ $ dias = [
87+ 'Monday ' => 'Lun ' ,
88+ 'Tuesday ' => 'Mar ' ,
89+ 'Wednesday ' => 'Mié ' ,
90+ 'Thursday ' => 'Jue ' ,
91+ 'Friday ' => 'Vie ' ,
92+ 'Saturday ' => 'Sáb ' ,
93+ 'Sunday ' => 'Dom ' ,
94+ ];
95+
96+ // Rango de la semana actual (Lunes a Domingo)
97+ $ inicioSemana = \Carbon \Carbon::now ('America/Bogota ' )->startOfWeek ();
98+ $ finSemana = \Carbon \Carbon::now ('America/Bogota ' )->endOfWeek ();
99+
100+ // Obtener registros agrupados por fecha usando updated_at
101+ $ registros = BLPedidoItem::where ('estado ' , 'completado ' )
102+ ->whereBetween ('updated_at ' , [$ inicioSemana , $ finSemana ])
103+ ->get ()
104+ ->groupBy (function ($ item ) {
105+ return \Carbon \Carbon::parse ($ item ->updated_at )
106+ ->setTimezone ('America/Bogota ' )
107+ ->format ('Y-m-d ' );
108+ });
109+
110+ // Crear la estructura de la semana completa
111+ $ data = collect ();
112+ for ($ fecha = $ inicioSemana ->copy (); $ fecha <= $ finSemana ; $ fecha ->addDay ()) {
113+ $ fechaStr = $ fecha ->format ('Y-m-d ' );
114+ $ grupo = $ registros ->get ($ fechaStr , collect ());
115+
116+ $ data ->push ([
117+ 'fecha ' => $ fechaStr ,
118+ 'dia ' => $ dias [$ fecha ->format ('l ' )],
119+ 'produccion ' => $ grupo ->sum ('cantidad_empaques ' ),
120+ 'timestamps ' => $ grupo ->pluck ('updated_at ' ),
121+ ]);
122+ }
123+
124+ return $ data ->values ();
119125 }
120126
121- return $ data ->values ();
122- }
123-
124127
125128
126129
0 commit comments