141 lines
4.3 KiB
PHP
141 lines
4.3 KiB
PHP
<?php namespace App\Http\Controllers\App;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Bundle;
|
|
use App\Services\BundleService;
|
|
use App\Http\Requests\App\BundleStoreRequest;
|
|
use App\Http\Requests\App\BundleUpdateRequest;
|
|
use Illuminate\Http\Request;
|
|
use Notsoweb\ApiResponse\Enums\ApiResponse;
|
|
|
|
class BundleController extends Controller
|
|
{
|
|
public function __construct(
|
|
protected BundleService $bundleService
|
|
) {}
|
|
|
|
/**
|
|
* Listar todos los bundles activos
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$bundles = Bundle::with(['items.inventory.price', 'price'])
|
|
->when($request->has('q'), function ($query) use ($request) {
|
|
$query->where(function ($q) use ($request) {
|
|
$q->where('name', 'like', "%{$request->q}%")
|
|
->orWhere('sku', 'like', "%{$request->q}%")
|
|
->orWhere('barcode', $request->q);
|
|
});
|
|
})
|
|
->orderBy('name')
|
|
->paginate(config('app.pagination', 15));
|
|
|
|
return ApiResponse::OK->response([
|
|
'bundles' => $bundles,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Ver detalle de un bundle
|
|
*/
|
|
public function show(Bundle $bundle)
|
|
{
|
|
$bundle->load(['items.inventory.price', 'price']);
|
|
|
|
return ApiResponse::OK->response([
|
|
'model' => $bundle,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Crear un nuevo bundle
|
|
*/
|
|
public function store(BundleStoreRequest $request)
|
|
{
|
|
try {
|
|
$bundle = $this->bundleService->createBundle($request->validated());
|
|
|
|
return ApiResponse::CREATED->response([
|
|
'model' => $bundle,
|
|
'message' => 'Bundle creado exitosamente',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return ApiResponse::INTERNAL_ERROR->response([
|
|
'message' => 'Error al crear el bundle: ' . $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Actualizar un bundle existente
|
|
*/
|
|
public function update(BundleUpdateRequest $request, Bundle $bundle)
|
|
{
|
|
try {
|
|
$updatedBundle = $this->bundleService->updateBundle($bundle, $request->validated());
|
|
|
|
return ApiResponse::OK->response([
|
|
'model' => $updatedBundle,
|
|
'message' => 'Bundle actualizado exitosamente',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return ApiResponse::INTERNAL_ERROR->response([
|
|
'message' => 'Error al actualizar el bundle: ' . $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Eliminar (soft delete) un bundle
|
|
*/
|
|
public function destroy(Bundle $bundle)
|
|
{
|
|
try {
|
|
$this->bundleService->deleteBundle($bundle);
|
|
|
|
return ApiResponse::OK->response([
|
|
'message' => 'Bundle eliminado exitosamente',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return ApiResponse::INTERNAL_ERROR->response([
|
|
'message' => 'Error al eliminar el bundle: ' . $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Verificar stock disponible de un bundle
|
|
*/
|
|
public function checkStock(Request $request, Bundle $bundle)
|
|
{
|
|
$quantity = $request->input('quantity', 1);
|
|
$warehouseId = $request->input('warehouse_id');
|
|
|
|
$bundle->load(['items.inventory']);
|
|
|
|
$availableStock = $warehouseId
|
|
? $bundle->stockInWarehouse($warehouseId)
|
|
: $bundle->available_stock;
|
|
|
|
$hasStock = $bundle->hasStock($quantity, $warehouseId);
|
|
|
|
return ApiResponse::OK->response([
|
|
'bundle_id' => $bundle->id,
|
|
'bundle_name' => $bundle->name,
|
|
'quantity_requested' => $quantity,
|
|
'available_stock' => $availableStock,
|
|
'has_stock' => $hasStock,
|
|
'components_stock' => $bundle->items->map(function ($item) use ($warehouseId) {
|
|
return [
|
|
'inventory_id' => $item->inventory_id,
|
|
'product_name' => $item->inventory->name,
|
|
'required_quantity' => $item->quantity,
|
|
'available_stock' => $warehouseId
|
|
? $item->inventory->stockInWarehouse($warehouseId)
|
|
: $item->inventory->stock,
|
|
];
|
|
}),
|
|
]);
|
|
}
|
|
}
|