diff --git a/app/Http/Controllers/App/InventoryController.php b/app/Http/Controllers/App/InventoryController.php index 92f7948..07955d9 100644 --- a/app/Http/Controllers/App/InventoryController.php +++ b/app/Http/Controllers/App/InventoryController.php @@ -23,7 +23,7 @@ public function __construct( public function index(Request $request) { - $products = Inventory::with(['category', 'price']) + $products = Inventory::with(['category', 'price'])->withCount('serials') ->where('is_active', true); @@ -46,7 +46,7 @@ public function index(Request $request) public function show(Inventory $inventario) { return ApiResponse::OK->response([ - 'model' => $inventario->load(['category', 'price']) + 'model' => $inventario->load(['category', 'price'])->loadCount('serials') ]); } diff --git a/app/Http/Controllers/App/InventorySerialController.php b/app/Http/Controllers/App/InventorySerialController.php index 1cb91d8..b61a161 100644 --- a/app/Http/Controllers/App/InventorySerialController.php +++ b/app/Http/Controllers/App/InventorySerialController.php @@ -32,7 +32,7 @@ public function index(Inventory $inventario, Request $request) $query->where('serial_number', 'like', "%{$request->q}%"); } - $serials = $query->orderBy('serial_number', 'ASC') + $serials = $query->orderBy('serial_number', 'ASC')->with('saleDetail.sale') ->paginate(config('app.pagination')); return ApiResponse::OK->response([ @@ -136,36 +136,4 @@ public function destroy(Inventory $inventario, InventorySerial $serial) 'inventory' => $inventario->fresh(), ]); } - - /** - * Importar mĂșltiples seriales - */ - public function bulkStore(Inventory $inventario, Request $request) - { - $request->validate([ - 'serial_numbers' => ['required', 'array', 'min:1'], - 'serial_numbers.*' => ['required', 'string', 'unique:inventory_serials,serial_number'], - ]); - - $created = []; - - foreach ($request->serial_numbers as $serialNumber) { - $serial = InventorySerial::create([ - 'inventory_id' => $inventario->id, - 'serial_number' => $serialNumber, - 'status' => 'disponible', - ]); - - $created[] = $serial; - } - - // Sincronizar stock - $inventario->syncStock(); - - return ApiResponse::CREATED->response([ - 'serials' => $created, - 'count' => count($created), - 'inventory' => $inventario->fresh(), - ]); - } } diff --git a/app/Http/Controllers/App/SaleController.php b/app/Http/Controllers/App/SaleController.php index 5cbfa37..8d92b24 100644 --- a/app/Http/Controllers/App/SaleController.php +++ b/app/Http/Controllers/App/SaleController.php @@ -17,7 +17,7 @@ public function __construct( public function index(Request $request) { - $sales = Sale::with(['details.inventory', 'user', 'client']) + $sales = Sale::with(['details.inventory', 'details.serials', 'user', 'client']) ->orderBy('created_at', 'desc'); if ($request->has('q') && $request->q) { diff --git a/app/Models/Inventory.php b/app/Models/Inventory.php index 58aac4e..697b911 100644 --- a/app/Models/Inventory.php +++ b/app/Models/Inventory.php @@ -28,6 +28,8 @@ class Inventory extends Model 'is_active' => 'boolean', ]; + protected $appends = ['has_serials']; + public function category() { return $this->belongsTo(Category::class); @@ -67,4 +69,9 @@ public function syncStock(): void { $this->update(['stock' => $this->getAvailableStockAttribute()]); } + + public function getHasSerialsAttribute(): bool + { + return isset($this->attributes['serials_count']) && $this->attributes['serials_count'] > 0; + } } diff --git a/routes/api.php b/routes/api.php index 0a8f3a8..38b367d 100644 --- a/routes/api.php +++ b/routes/api.php @@ -35,9 +35,7 @@ Route::get('inventario/template/download', [InventoryController::class, 'downloadTemplate']); // NĂșmeros de serie - Route::resource('inventario.serials', InventorySerialController::class) - ->except(['create', 'edit']); - Route::post('inventario/{inventario}/serials/bulk', [InventorySerialController::class, 'bulkStore']); + Route::resource('inventario.serials', InventorySerialController::class); //CATEGORIAS Route::resource('categorias', CategoryController::class);