<?php
/**
 * GET /api/lugares/listar.php
 * List purchase locations (excluding branch names)
 */
require_once '../../config/database.php';
require_once '../../includes/helpers.php';

$pdo = getConnection();

try {
    // Get branch names to exclude
    $branches = [];
    $resBranch = $pdo->query("SELECT nombre FROM sucursales");
    while ($r = $resBranch->fetch()) {
        $branches[] = strtoupper(trim($r['nombre']));
    }

    $result = $pdo->query("SELECT id, nombre FROM lugarcompra ORDER BY nombre ASC");
    $lugares = [];
    while ($r = $result->fetch()) {
        if (!in_array(strtoupper(trim($r['nombre'])), $branches)) {
            $lugares[] = $r;
        }
    }

    json_success($lugares);
} catch (Exception $e) {
    json_error('Error al listar lugares: ' . $e->getMessage());
}
