124 lines
3.8 KiB
PHP
124 lines
3.8 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Api;
|
||
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Models\StockPosition;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\Validator;
|
||
|
||
|
||
class StockPositionController extends Controller
|
||
{
|
||
/**
|
||
* Update the capacity of a given StockPosition.
|
||
*
|
||
* Request payload:
|
||
* {
|
||
* "capacity": <integer> // new capacity for the position
|
||
* }
|
||
*
|
||
* - Validates that capacity ≥ 0.
|
||
* - Ensures the new capacity is not less than the sum of existing section capacities.
|
||
* - Returns JSON { success: true } on success,
|
||
* or { success: false, error: "<code>", message: "<details>" } on failure.
|
||
*/
|
||
public function update(Request $request, $id)
|
||
{
|
||
// 1) Find the position
|
||
$position = StockPosition::find($id);
|
||
if (!$position) {
|
||
return response()
|
||
->json(['success' => false, 'error' => 'not_found', 'message' => 'Position not found.'], 404);
|
||
}
|
||
|
||
// 2) Validate incoming payload
|
||
$validator = Validator::make($request->all(), [
|
||
'capacity' => 'required|integer|min:0',
|
||
]);
|
||
|
||
if ($validator->fails()) {
|
||
return response()
|
||
->json([
|
||
'success' => false,
|
||
'error' => 'validation_failed',
|
||
'message' => $validator->errors()->first(),
|
||
], 422);
|
||
}
|
||
|
||
$newCapacity = $request->input('capacity');
|
||
|
||
// 3) Check that new capacity ≥ sum of all existing (non-deleted) sections' capacities
|
||
// (we assume “sections” relationship returns the live StockSection models)
|
||
$currentSectionsSum = $position
|
||
->sections()
|
||
->sum('capacity');
|
||
|
||
if ($newCapacity < $currentSectionsSum) {
|
||
return response()
|
||
->json([
|
||
'success' => false,
|
||
'error' => 'insufficient_capacity',
|
||
'message' => "New capacity ({$newCapacity}) is less than total of existing section capacities ({$currentSectionsSum}).",
|
||
], 422);
|
||
}
|
||
|
||
// 4) All good → update and save
|
||
$position->capacity = $newCapacity;
|
||
$position->save();
|
||
|
||
return response()->json(['success' => true], 200);
|
||
}
|
||
|
||
|
||
public function getPosition(Request $request, int $id)
|
||
{
|
||
// 1) Eager‐load the real relationships
|
||
$position = StockPosition::with([
|
||
'sections.entries.physicalItem',
|
||
'shelf.rack.line.room',
|
||
])->findOrFail($id);
|
||
|
||
// 2) Compute the storage address string
|
||
$position->storage_address = $position->storageAddress();
|
||
|
||
// 3) Return the model itself (now including a top‐level "storage_address" field)
|
||
return response()->json($position);
|
||
}
|
||
|
||
|
||
public function store(Request $request)
|
||
{
|
||
$data = $request->validate([
|
||
'shelf_id' => 'required|exists:stock_shelf,shelf_id',
|
||
'position_symbol' => 'required|string|max:50',
|
||
'position_name' => 'required|string|max:100',
|
||
'capacity' => 'required|integer|min:0',
|
||
]);
|
||
|
||
$pos = StockPosition::create($data);
|
||
|
||
return response()->json($pos, 201);
|
||
}
|
||
|
||
public function updateBasic(Request $request, StockPosition $pos)
|
||
{
|
||
$data = $request->validate([
|
||
'position_symbol' => 'sometimes|required|string|max:50',
|
||
'position_name' => 'sometimes|required|string|max:100',
|
||
'capacity' => 'sometimes|required|integer|min:0',
|
||
]);
|
||
|
||
$pos->update($data);
|
||
|
||
return response()->json($pos);
|
||
}
|
||
|
||
public function destroy(StockPosition $pos)
|
||
{
|
||
$pos->delete();
|
||
return response()->json(null, 204);
|
||
}
|
||
}
|