loadLangs(array('kundenkarte@kundenkarte')); $action = GETPOST('action', 'aZ09'); $response = array('success' => false, 'error' => ''); // Berechtigungsprüfung if (!$user->hasRight('kundenkarte', 'read')) { $response['error'] = $langs->trans('ErrorPermissionDenied'); echo json_encode($response); exit; } $accessory = new AnlageAccessory($db); switch ($action) { case 'list': // Alle Zubehörteile einer Anlage laden $anlageId = GETPOSTINT('fk_anlage'); if ($anlageId > 0) { $accessories = $accessory->fetchAllByAnlage($anlageId); $result = array(); foreach ($accessories as $acc) { $result[] = array( 'id' => $acc->id, 'fk_product' => $acc->fk_product, 'product_ref' => $acc->product_ref, 'product_label' => $acc->product_label, 'product_price' => $acc->product_price, 'qty' => $acc->qty, 'note' => $acc->note, ); } $response['success'] = true; $response['accessories'] = $result; } else { $response['error'] = 'Missing fk_anlage'; } break; case 'add': // Zubehör hinzufügen if (!$user->hasRight('kundenkarte', 'write')) { $response['error'] = $langs->trans('ErrorPermissionDenied'); break; } $accessory->fk_anlage = GETPOSTINT('fk_anlage'); $accessory->fk_product = GETPOSTINT('fk_product'); $accessory->qty = GETPOSTINT('qty') > 0 ? GETPOSTINT('qty') : 1; $accessory->note = GETPOST('note', 'alphanohtml'); $result = $accessory->create($user); if ($result > 0) { $response['success'] = true; $response['id'] = $result; } else { $response['error'] = $accessory->error ?: 'Fehler beim Speichern'; } break; case 'update': // Zubehör aktualisieren (Menge, Notiz) if (!$user->hasRight('kundenkarte', 'write')) { $response['error'] = $langs->trans('ErrorPermissionDenied'); break; } $id = GETPOSTINT('id'); if ($id > 0 && $accessory->fetch($id) > 0) { $accessory->qty = GETPOSTINT('qty') > 0 ? GETPOSTINT('qty') : $accessory->qty; $accessory->note = GETPOST('note', 'alphanohtml'); $result = $accessory->update($user); if ($result > 0) { $response['success'] = true; } else { $response['error'] = 'Fehler beim Speichern'; } } else { $response['error'] = $langs->trans('ErrorRecordNotFound'); } break; case 'delete': // Zubehör löschen if (!$user->hasRight('kundenkarte', 'delete')) { $response['error'] = $langs->trans('ErrorPermissionDenied'); break; } $id = GETPOSTINT('id'); if ($id > 0 && $accessory->fetch($id) > 0) { $result = $accessory->delete($user); if ($result > 0) { $response['success'] = true; } else { $response['error'] = 'Fehler beim Löschen'; } } else { $response['error'] = $langs->trans('ErrorRecordNotFound'); } break; case 'order': // Lieferantenbestellung aus Zubehör erstellen if (!$user->hasRight('kundenkarte', 'write')) { $response['error'] = $langs->trans('ErrorPermissionDenied'); break; } $anlageId = GETPOSTINT('fk_anlage'); $supplierId = GETPOSTINT('supplier_id'); $idsRaw = GETPOST('ids', 'array'); if ($anlageId > 0 && $supplierId > 0 && !empty($idsRaw)) { $ids = array_map('intval', $idsRaw); $result = $accessory->generateSupplierOrder($user, $supplierId, $anlageId, $ids); if ($result > 0) { $response['success'] = true; $response['order_id'] = $result; } else { $response['error'] = $accessory->error ?: 'Fehler beim Erstellen der Bestellung'; } } else { $response['error'] = 'Fehlende Parameter (Anlage, Lieferant, IDs)'; } break; default: $response['error'] = 'Unknown action'; } echo json_encode($response);