172 lines
4.9 KiB
PHP
172 lines
4.9 KiB
PHP
<?php namespace App\Http\Controllers\App;
|
|
/**
|
|
* @copyright (c) 2025 Notsoweb Software (https://notsoweb.com) - All Rights Reserved
|
|
*/
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Inventory;
|
|
use App\Models\InventorySerial;
|
|
use Illuminate\Http\Request;
|
|
use Notsoweb\ApiResponse\Enums\ApiResponse;
|
|
|
|
/**
|
|
* Controlador para gestión de números de serie
|
|
*
|
|
* @author Moisés Cortés C. <moises.cortes@notsoweb.com>
|
|
* @version 1.0.0
|
|
*/
|
|
class InventorySerialController extends Controller
|
|
{
|
|
/**
|
|
* Listar seriales de un producto
|
|
*/
|
|
public function index(Inventory $inventario, Request $request)
|
|
{
|
|
$query = $inventario->serials();
|
|
|
|
if ($request->has('status')) {
|
|
$query->where('status', $request->status);
|
|
}
|
|
|
|
if ($request->has('q')) {
|
|
$query->where('serial_number', 'like', "%{$request->q}%");
|
|
}
|
|
|
|
$serials = $query->orderBy('serial_number', 'ASC')
|
|
->paginate(config('app.pagination'));
|
|
|
|
return ApiResponse::OK->response([
|
|
'serials' => $serials,
|
|
'inventory' => $inventario->load('category'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Mostrar un serial específico
|
|
*/
|
|
public function show(Inventory $inventario, InventorySerial $serial)
|
|
{
|
|
// Verificar que el serial pertenece al inventario
|
|
if ($serial->inventory_id !== $inventario->id) {
|
|
return ApiResponse::NOT_FOUND->response([
|
|
'message' => 'Serial no encontrado para este inventario'
|
|
]);
|
|
}
|
|
|
|
return ApiResponse::OK->response([
|
|
'serial' => $serial->load('saleDetail'),
|
|
'inventory' => $inventario->load('category'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Crear un nuevo serial
|
|
*/
|
|
public function store(Inventory $inventario, Request $request)
|
|
{
|
|
$request->validate([
|
|
'serial_number' => ['required', 'string', 'unique:inventory_serials,serial_number'],
|
|
'notes' => ['nullable', 'string'],
|
|
]);
|
|
|
|
$serial = InventorySerial::create([
|
|
'inventory_id' => $inventario->id,
|
|
'serial_number' => $request->serial_number,
|
|
'status' => 'disponible',
|
|
'notes' => $request->notes,
|
|
]);
|
|
|
|
// Sincronizar stock
|
|
$inventario->syncStock();
|
|
|
|
return ApiResponse::CREATED->response([
|
|
'serial' => $serial,
|
|
'inventory' => $inventario->fresh(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Actualizar un serial
|
|
*/
|
|
public function update(Inventory $inventario , InventorySerial $serial, Request $request)
|
|
{
|
|
// Verificar que el serial pertenece al inventario
|
|
if ($serial->inventory_id !== $inventario->id) {
|
|
return ApiResponse::NOT_FOUND->response([
|
|
'message' => 'Serial no encontrado para este inventario'
|
|
]);
|
|
}
|
|
|
|
$request->validate([
|
|
'serial_number' => ['sometimes', 'string', 'unique:inventory_serials,serial_number,' . $serial->id],
|
|
'status' => ['sometimes', 'in:disponible,vendido'],
|
|
'notes' => ['nullable', 'string'],
|
|
]);
|
|
|
|
$serial->update($request->only(['serial_number', 'status', 'notes']));
|
|
|
|
// Sincronizar stock del inventario
|
|
$inventario->syncStock();
|
|
|
|
return ApiResponse::OK->response([
|
|
'serial' => $serial->fresh(),
|
|
'inventory' => $inventario->fresh(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Eliminar un serial
|
|
*/
|
|
public function destroy(Inventory $inventario, InventorySerial $serial)
|
|
{
|
|
// Verificar que el serial pertenece al inventario
|
|
if ($serial->inventory_id !== $inventario->id) {
|
|
return ApiResponse::NOT_FOUND->response([
|
|
'message' => 'Serial no encontrado para este inventario'
|
|
]);
|
|
}
|
|
|
|
$serial->delete();
|
|
|
|
// Sincronizar stock
|
|
$inventario->syncStock();
|
|
|
|
return ApiResponse::OK->response([
|
|
'message' => 'Serial eliminado exitosamente',
|
|
'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(),
|
|
]);
|
|
}
|
|
}
|