Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 163 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| BinController | |
0.00% |
0 / 163 |
|
0.00% |
0 / 10 |
870 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getBinDetails | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| updateNotes | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| generateName | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| generateAllNames | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| updateItemCount | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| updateEstimatedValue | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| recordAudit | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
30 | |||
| getBinsNeedingAudit | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| searchByNotes | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\Backstock\Controllers; |
| 4 | |
| 5 | |
| 6 | use BuyerKiosk\Backstock\BinNamingService; |
| 7 | /** |
| 8 | * BinController |
| 9 | * |
| 10 | * Handles API routes for bin enhancements: notes, naming, audits. |
| 11 | */ |
| 12 | class BinController extends \BuyerKiosk\Core\Controllers\BaseController |
| 13 | { |
| 14 | /** |
| 15 | * @var \Store Store object |
| 16 | */ |
| 17 | private $store; |
| 18 | |
| 19 | /** |
| 20 | * Constructor |
| 21 | * |
| 22 | * @param \Slim\Slim $app Slim application instance |
| 23 | * @param \Store $store Store object |
| 24 | */ |
| 25 | public function __construct($app, \Store $store) |
| 26 | { |
| 27 | parent::__construct($app); |
| 28 | $this->store = $store; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Get bin details with enhanced fields |
| 33 | * |
| 34 | * GET /api/:typeNum/backstock/bins/:binId/details |
| 35 | */ |
| 36 | public function getBinDetails($binId) |
| 37 | { |
| 38 | try { |
| 39 | $service = new BinNamingService($this->store); |
| 40 | |
| 41 | $bin = $service->getBinDetails((int)$binId); |
| 42 | |
| 43 | if (!$bin) { |
| 44 | echo json_encode([ |
| 45 | 'error' => true, |
| 46 | 'message' => 'Bin not found' |
| 47 | ]); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | echo json_encode([ |
| 52 | 'success' => true, |
| 53 | 'bin' => $bin |
| 54 | ]); |
| 55 | |
| 56 | } catch (\Exception $e) { |
| 57 | echo json_encode([ |
| 58 | 'error' => true, |
| 59 | 'message' => 'Failed to retrieve bin details: ' . $e->getMessage() |
| 60 | ]); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Update bin notes |
| 66 | * |
| 67 | * PUT /api/:typeNum/backstock/bins/:binId/notes |
| 68 | */ |
| 69 | public function updateNotes($binId) |
| 70 | { |
| 71 | $app = $this->_app; |
| 72 | |
| 73 | try { |
| 74 | $service = new BinNamingService($this->store); |
| 75 | |
| 76 | $data = json_decode($app->request->getBody(), true); |
| 77 | |
| 78 | if (!isset($data['notes'])) { |
| 79 | echo json_encode([ |
| 80 | 'error' => true, |
| 81 | 'message' => 'Notes field is required' |
| 82 | ]); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | $success = $service->updateNotes((int)$binId, $data['notes']); |
| 87 | |
| 88 | echo json_encode([ |
| 89 | 'success' => $success |
| 90 | ]); |
| 91 | |
| 92 | } catch (\Exception $e) { |
| 93 | echo json_encode([ |
| 94 | 'error' => true, |
| 95 | 'message' => 'Failed to update notes: ' . $e->getMessage() |
| 96 | ]); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Generate name for a specific bin |
| 102 | * |
| 103 | * POST /api/:typeNum/backstock/bins/:binId/generate-name |
| 104 | */ |
| 105 | public function generateName($binId) |
| 106 | { |
| 107 | try { |
| 108 | $service = new BinNamingService($this->store); |
| 109 | |
| 110 | $name = $service->generateName((int)$binId); |
| 111 | |
| 112 | if ($name) { |
| 113 | $service->updateGeneratedName((int)$binId, $name); |
| 114 | echo json_encode([ |
| 115 | 'success' => true, |
| 116 | 'generatedName' => $name |
| 117 | ]); |
| 118 | } else { |
| 119 | echo json_encode([ |
| 120 | 'error' => true, |
| 121 | 'message' => 'Failed to generate name' |
| 122 | ]); |
| 123 | } |
| 124 | |
| 125 | } catch (\Exception $e) { |
| 126 | echo json_encode([ |
| 127 | 'error' => true, |
| 128 | 'message' => 'Failed to generate name: ' . $e->getMessage() |
| 129 | ]); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Generate names for all bins missing generated names |
| 135 | * |
| 136 | * POST /api/:typeNum/backstock/bins/generate-all-names |
| 137 | */ |
| 138 | public function generateAllNames() |
| 139 | { |
| 140 | try { |
| 141 | $service = new BinNamingService($this->store); |
| 142 | |
| 143 | $count = $service->generateAllNames(); |
| 144 | |
| 145 | echo json_encode([ |
| 146 | 'success' => true, |
| 147 | 'updatedCount' => $count |
| 148 | ]); |
| 149 | |
| 150 | } catch (\Exception $e) { |
| 151 | echo json_encode([ |
| 152 | 'error' => true, |
| 153 | 'message' => 'Failed to generate names: ' . $e->getMessage() |
| 154 | ]); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Update bin item count |
| 160 | * |
| 161 | * PUT /api/:typeNum/backstock/bins/:binId/item-count |
| 162 | */ |
| 163 | public function updateItemCount($binId) |
| 164 | { |
| 165 | $app = $this->_app; |
| 166 | |
| 167 | try { |
| 168 | $service = new BinNamingService($this->store); |
| 169 | |
| 170 | $data = json_decode($app->request->getBody(), true); |
| 171 | |
| 172 | if (!isset($data['itemCount'])) { |
| 173 | echo json_encode([ |
| 174 | 'error' => true, |
| 175 | 'message' => 'itemCount is required' |
| 176 | ]); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | $success = $service->updateItemCount((int)$binId, (int)$data['itemCount']); |
| 181 | |
| 182 | echo json_encode([ |
| 183 | 'success' => $success |
| 184 | ]); |
| 185 | |
| 186 | } catch (\Exception $e) { |
| 187 | echo json_encode([ |
| 188 | 'error' => true, |
| 189 | 'message' => 'Failed to update item count: ' . $e->getMessage() |
| 190 | ]); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Update bin estimated value |
| 196 | * |
| 197 | * PUT /api/:typeNum/backstock/bins/:binId/estimated-value |
| 198 | */ |
| 199 | public function updateEstimatedValue($binId) |
| 200 | { |
| 201 | $app = $this->_app; |
| 202 | |
| 203 | try { |
| 204 | $service = new BinNamingService($this->store); |
| 205 | |
| 206 | $data = json_decode($app->request->getBody(), true); |
| 207 | |
| 208 | if (!isset($data['estimatedValue'])) { |
| 209 | echo json_encode([ |
| 210 | 'error' => true, |
| 211 | 'message' => 'estimatedValue is required' |
| 212 | ]); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | $success = $service->updateEstimatedValue((int)$binId, (float)$data['estimatedValue']); |
| 217 | |
| 218 | echo json_encode([ |
| 219 | 'success' => $success |
| 220 | ]); |
| 221 | |
| 222 | } catch (\Exception $e) { |
| 223 | echo json_encode([ |
| 224 | 'error' => true, |
| 225 | 'message' => 'Failed to update estimated value: ' . $e->getMessage() |
| 226 | ]); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Record bin audit |
| 232 | * |
| 233 | * POST /api/:typeNum/backstock/bins/:binId/audit |
| 234 | */ |
| 235 | public function recordAudit($binId) |
| 236 | { |
| 237 | $app = $this->_app; |
| 238 | |
| 239 | try { |
| 240 | $service = new BinNamingService($this->store); |
| 241 | |
| 242 | $data = json_decode($app->request->getBody(), true); |
| 243 | |
| 244 | $employeeId = $data['employeeId'] ?? ($app->user->id ?? 0); |
| 245 | |
| 246 | $success = $service->recordAudit( |
| 247 | (int)$binId, |
| 248 | (int)$employeeId, |
| 249 | isset($data['itemCount']) ? (int)$data['itemCount'] : null, |
| 250 | isset($data['estimatedValue']) ? (float)$data['estimatedValue'] : null, |
| 251 | $data['notes'] ?? null |
| 252 | ); |
| 253 | |
| 254 | if ($success) { |
| 255 | $bin = $service->getBinDetails((int)$binId); |
| 256 | echo json_encode([ |
| 257 | 'success' => true, |
| 258 | 'bin' => $bin |
| 259 | ]); |
| 260 | } else { |
| 261 | echo json_encode([ |
| 262 | 'error' => true, |
| 263 | 'message' => 'Failed to record audit' |
| 264 | ]); |
| 265 | } |
| 266 | |
| 267 | } catch (\Exception $e) { |
| 268 | echo json_encode([ |
| 269 | 'error' => true, |
| 270 | 'message' => 'Failed to record audit: ' . $e->getMessage() |
| 271 | ]); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Get bins needing audit |
| 277 | * |
| 278 | * GET /api/:typeNum/backstock/bins/needs-audit |
| 279 | */ |
| 280 | public function getBinsNeedingAudit() |
| 281 | { |
| 282 | $app = $this->_app; |
| 283 | $days = $app->request->get('days') ?: 90; |
| 284 | |
| 285 | try { |
| 286 | $service = new BinNamingService($this->store); |
| 287 | |
| 288 | $bins = $service->getBinsNeedingAudit((int)$days); |
| 289 | |
| 290 | echo json_encode([ |
| 291 | 'success' => true, |
| 292 | 'bins' => $bins, |
| 293 | 'count' => count($bins), |
| 294 | 'thresholdDays' => (int)$days |
| 295 | ]); |
| 296 | |
| 297 | } catch (\Exception $e) { |
| 298 | echo json_encode([ |
| 299 | 'error' => true, |
| 300 | 'message' => 'Failed to retrieve bins: ' . $e->getMessage() |
| 301 | ]); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Search bins by notes |
| 307 | * |
| 308 | * GET /api/:typeNum/backstock/bins/search-notes |
| 309 | */ |
| 310 | public function searchByNotes() |
| 311 | { |
| 312 | $app = $this->_app; |
| 313 | $query = $app->request->get('q'); |
| 314 | |
| 315 | if (empty($query)) { |
| 316 | echo json_encode([ |
| 317 | 'error' => true, |
| 318 | 'message' => 'Search query (q) is required' |
| 319 | ]); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | try { |
| 324 | $service = new BinNamingService($this->store); |
| 325 | |
| 326 | $bins = $service->searchByNotes($query); |
| 327 | |
| 328 | echo json_encode([ |
| 329 | 'success' => true, |
| 330 | 'bins' => $bins, |
| 331 | 'count' => count($bins), |
| 332 | 'query' => $query |
| 333 | ]); |
| 334 | |
| 335 | } catch (\Exception $e) { |
| 336 | echo json_encode([ |
| 337 | 'error' => true, |
| 338 | 'message' => 'Failed to search bins: ' . $e->getMessage() |
| 339 | ]); |
| 340 | } |
| 341 | } |
| 342 | } |