prepare($sql); $sth->execute([ ":company_id" => $company_id, ":id" => $id ]); $table_log = json_decode($sth->fetchColumn() ?: '[]', true); if (!is_array($table_log)) $table_log = []; $table_log[] = $logging; $params = [ ":company_id" => $company_id, ":warehouse" => $data["warehouse"] ?? "", ":zone" => $data["zone"] ?? "", ":aisle_from" => $data["aisle_from"] ?? "", ":aisle_to" => $data["aisle_to"] ?? "", ":rack_from" => $data["rack_from"] ?? "", ":rack_to" => $data["rack_to"] ?? "", ":description" => $data["description"] ?? "", ":status" => (int)($data["status"] ?? 1), ":log" => json_encode($table_log, JSON_UNESCAPED_UNICODE), ]; if ($id > 0) { $sql = "UPDATE md_storage SET warehouse = :warehouse, zone = :zone, aisle_from = :aisle_from, aisle_to = :aisle_to, rack_from = :rack_from, rack_to = :rack_to, description = :description, status = :status, `log` = :log WHERE id = :id AND company_id = :company_id"; $params[":id"] = $id; $sth = $pdo->prepare($sql); $sth->execute($params); $storage_id = $id; } else { $sql = "INSERT INTO md_storage (company_id, warehouse, zone, aisle_from, aisle_to, rack_from, rack_to, description, status, `log`) VALUES (:company_id, :warehouse, :zone, :aisle_from, :aisle_to, :rack_from, :rack_to, :description, :status, :log)"; $sth = $pdo->prepare($sql); $sth->execute($params); $storage_id = (int) $pdo->lastInsertId(); } // Sync md_rack rows — same $pdo, inside the same transaction $wm = new WarehouseManager($pdo, $company_id); $wm->syncRacks($storage_id, [ 'warehouse' => $data['warehouse'], 'zone' => $data['zone'], 'aisle_from' => $data['aisle_from'], 'aisle_to' => $data['aisle_to'], 'rack_from' => $data['rack_from'], 'rack_to' => $data['rack_to'], ]); }); $answer["success"] = 1; } catch (PDOException $e) { $answer["success"] = 0; $answer["message"] = "Database error, please try again"; http_response_code(500); } catch (Exception $e) { $answer["success"] = 0; $answer["message"] = $e->getMessage(); http_response_code(400); } exit(json_encode($answer)); ?>