Skip to content

Commit 24ef54b

Browse files
authored
Merge pull request #59 from chechojgb/buttonsLovers
refactor: correccion tablas y metodo store
2 parents b01901f + d906d61 commit 24ef54b

File tree

7 files changed

+234
-123
lines changed

7 files changed

+234
-123
lines changed

app/Http/Controllers/BLMarcacionController.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\Models\BLCliente;
6+
use App\Models\BLMarcacion;
67
use App\Models\User;
78
use Illuminate\Http\Request;
89
use Inertia\Inertia;
@@ -14,11 +15,61 @@ public function index()
1415
{
1516
$user = Auth::user();
1617
$orderCustomer = BLCliente::with('pedidos.items.empaque.producto')->get();
17-
$buttonUser = User::where('proyecto', 'Button LoversM')->get();
18+
$buttonUser = User::whereIn('proyecto', ['Button LoversM', 'Button LoversMN'])->get();
1819
return Inertia::render('BLMarcacion', [
1920
'user' => $user,
2021
'orderCustomer' => $orderCustomer,
2122
'buttonUser' => $buttonUser
2223
]);
2324
}
25+
26+
public function store(Request $request)
27+
{
28+
$validated = $request->validate([
29+
'marcaciones' => 'required|array|min:1',
30+
'marcaciones.*.pedido_item_id' => 'required|exists:bl_pedido_items,id',
31+
'marcaciones.*.user_id' => 'required|exists:users,id',
32+
'marcaciones.*.cantidad' => 'required|integer|min:1',
33+
'marcaciones.*.fecha' => 'required|date',
34+
'marcaciones.*.pedido_id' => 'required|exists:bl_pedidos,id',
35+
'marcaciones.*.precio_unitario' => 'required|numeric|min:0',
36+
]);
37+
38+
foreach ($validated['marcaciones'] as $data) {
39+
// calcular el costo_total de esta marcación
40+
$data['costo_total'] = $data['cantidad'] * $data['precio_unitario'];
41+
// guardar en BD
42+
BLMarcacion::create($data);
43+
}
44+
45+
return redirect()->back()->with('success', 'Marcaciones registradas correctamente.');
46+
}
47+
48+
public function update(Request $request, $id)
49+
{
50+
$registro = BLMarcacion::findOrFail($id);
51+
52+
$request->validate([
53+
'cantidad' => 'required|integer|min:1',
54+
]);
55+
56+
$registro->update([
57+
'cantidad' => $request->cantidad,
58+
]);
59+
60+
return response()->json([
61+
'message' => 'Cantidad actualizada correctamente',
62+
'data' => $registro
63+
]);
64+
}
65+
66+
public function destroy($id)
67+
{
68+
$registro = BLMarcacion::findOrFail($id);
69+
$registro->delete();
70+
71+
return response()->json([
72+
'message' => 'Asignación eliminada correctamente'
73+
]);
74+
}
2475
}

app/Http/Controllers/MarcacionController.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

app/Models/BLMarcacion.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class BLMarcacion extends Model
8+
{
9+
protected $table = 'bl_marcacion';
10+
11+
protected $fillable = [
12+
'pedido_id',
13+
'pedido_item_id',
14+
'user_id',
15+
'cantidad',
16+
'fecha',
17+
];
18+
19+
protected $casts = [
20+
'items_marcados' => 'array', // para que se convierta automáticamente a array
21+
'fecha' => 'date',
22+
];
23+
24+
// Relaciones opcionales
25+
public function cliente()
26+
{
27+
return $this->belongsTo(BLCliente::class, 'cliente_id');
28+
}
29+
30+
public function pedido()
31+
{
32+
return $this->belongsTo(BLPedido::class, 'pedido_id');
33+
}
34+
35+
public function trabajador()
36+
{
37+
return $this->belongsTo(User::class, 'trabajador_id');
38+
}
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('bl_pedido_items', function (Blueprint $table) {
15+
$table->string('estado', 20)->default('pendiente')->after('cantidad');
16+
// $table->index('estado'); // lo pones solo si lo vas a consultar MUCHO
17+
});
18+
}
19+
20+
public function down(): void
21+
{
22+
Schema::table('bl_pedido_items', function (Blueprint $table) {
23+
$table->dropColumn('estado');
24+
// $table->dropIndex(['estado']); // si agregaste el índice
25+
});
26+
}
27+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('bl_marcacion', function (Blueprint $table) {
15+
$table->decimal('precio_unitario', 10, 2)->nullable()->after('cantidad');
16+
$table->decimal('costo_total', 10, 2)->nullable()->after('precio_unitario');
17+
});
18+
}
19+
20+
public function down(): void
21+
{
22+
Schema::table('bl_marcacion', function (Blueprint $table) {
23+
$table->dropColumn(['precio_unitario', 'costo_total']);
24+
});
25+
}
26+
27+
};

0 commit comments

Comments
 (0)