115 lines
3.4 KiB
PHP
115 lines
3.4 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 App\Models\Warehouse;
|
|
use App\Services\InventoryMovementService;
|
|
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('warehouse_id')) {
|
|
$query->where('warehouse_id', $request->warehouse_id);
|
|
} elseif ($request->boolean('main_warehouse')) {
|
|
$mainWarehouse = Warehouse::where('is_main', true)->first();
|
|
if ($mainWarehouse) {
|
|
$query->where(function ($q) use ($mainWarehouse) {
|
|
$q->where('warehouse_id', $mainWarehouse->id)
|
|
->orWhereNull('warehouse_id');
|
|
});
|
|
}
|
|
}
|
|
|
|
if ($request->has('q')) {
|
|
$query->where('serial_number', 'like', "%{$request->q}%");
|
|
}
|
|
|
|
$serials = $query->orderBy('serial_number', 'ASC')->with(['saleDetail.sale', 'warehouse'])
|
|
->paginate(config('app.pagination'));
|
|
|
|
return ApiResponse::OK->response([
|
|
'serials' => $serials,
|
|
'inventory' => $inventario->load('category'),
|
|
]);
|
|
}
|
|
|
|
public function search(Request $request)
|
|
{
|
|
$serialNumber = $request->input('serial_number');
|
|
|
|
$serial = InventorySerial::with(['inventory.price', 'inventory.category'])
|
|
->where('serial_number', $serialNumber)
|
|
->where('status', 'disponible')
|
|
->first();
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'data' => ['serial' => $serial]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 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'),
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* 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(),
|
|
]);
|
|
}
|
|
}
|