103 lines
4.0 KiB
PHP
103 lines
4.0 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Validation\ValidationException;
|
||
use App\Models\StockRoom;
|
||
use App\Models\StockLine;
|
||
use App\Models\StockRack;
|
||
use App\Models\StockShelf;
|
||
use App\Models\StockPosition;
|
||
use App\Models\StockSection;
|
||
|
||
class StorageController extends Controller
|
||
{
|
||
/**
|
||
* Handle initial bulk‐creation of a new storage layout.
|
||
*
|
||
* Expected JSON payload:
|
||
* {
|
||
* "room_symbol": "RM1",
|
||
* "room_name": "Main Storeroom",
|
||
* "num_lines": 5, // minimum 1
|
||
* "racks_per_line": 5, // minimum 1
|
||
* "shelves_per_rack": 5, // minimum 1
|
||
* "positions_per_shelf": 4, // minimum 1
|
||
* "sections_per_position": 1 // minimum 1
|
||
* }
|
||
*/
|
||
public function setup(Request $request)
|
||
{
|
||
$data = $request->validate([
|
||
'room_symbol' => 'required|string|max:50',
|
||
'room_name' => 'required|string|max:100',
|
||
'num_lines' => 'nullable|integer|min:1',
|
||
'racks_per_line' => 'nullable|integer|min:1',
|
||
'shelves_per_rack' => 'nullable|integer|min:1',
|
||
'positions_per_shelf' => 'nullable|integer|min:1',
|
||
'sections_per_position'=> 'nullable|integer|min:1',
|
||
]);
|
||
|
||
// Apply defaults if any count is missing or invalid:
|
||
$numLines = $data['num_lines'] ?? 1;
|
||
$racksPerLine = $data['racks_per_line'] ?? 1;
|
||
$shelvesPerRack= $data['shelves_per_rack'] ?? 1;
|
||
$positionsPerShelf = $data['positions_per_shelf'] ?? 1;
|
||
$sectionsPerPosition= $data['sections_per_position'] ?? 1;
|
||
|
||
// 1) Create the Room
|
||
$room = StockRoom::create([
|
||
'room_symbol' => $data['room_symbol'],
|
||
'room_name' => $data['room_name'],
|
||
]);
|
||
|
||
// 2) Create N lines
|
||
for ($i = 1; $i <= $numLines; $i++) {
|
||
$line = $room->lines()->create([
|
||
'line_symbol' => $room->room_symbol . '_L' . $i,
|
||
'line_name' => $room->room_name . ' Line ' . $i,
|
||
]);
|
||
|
||
// 3) For each line, create M racks
|
||
for ($j = 1; $j <= $racksPerLine; $j++) {
|
||
$rack = $line->racks()->create([
|
||
'rack_symbol' => $line->line_symbol . '_R' . $j,
|
||
'rack_name' => $line->line_name . ' Rack ' . $j,
|
||
]);
|
||
|
||
// 4) For each rack, create P shelves
|
||
for ($k = 1; $k <= $shelvesPerRack; $k++) {
|
||
$shelf = $rack->shelves()->create([
|
||
'shelf_symbol' => $rack->rack_symbol . '_S' . $k,
|
||
'shelf_name' => $rack->rack_name . ' Shelf ' . $k,
|
||
]);
|
||
|
||
// 5) For each shelf, create Q positions
|
||
for ($m = 1; $m <= $positionsPerShelf; $m++) {
|
||
$position = $shelf->positions()->create([
|
||
'position_symbol' => $shelf->shelf_symbol . '_P' . $m,
|
||
'position_name' => $shelf->shelf_name . ' Position ' . $m,
|
||
]);
|
||
|
||
// 6) For each position, create R sections
|
||
for ($n = 1; $n <= $sectionsPerPosition; $n++) {
|
||
$position->sections()->create([
|
||
'section_symbol' => $position->position_symbol . '_SEC' . $n,
|
||
'section_name' => $position->position_name . ' Section ' . $n,
|
||
'capacity' => 0,
|
||
'retrievable' => true,
|
||
]);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return response()->json([
|
||
'message' => 'Storage layout created successfully.',
|
||
'room_id' => $room->room_id,
|
||
], 201);
|
||
}
|
||
}
|