$sql.="(select call_center_queue_uuid from v_call_center_queues where replace(queue_name, ' ', '-') = t.queue_name and domain_uuid = t.domain_uuid) as queue_uuid, ";
$sql.="(select call_center_agent_uuid from v_call_center_agents where agent_name = t.agent_name and domain_uuid = t.domain_uuid) as agent_uuid ";
$sql.="from v_call_center_tiers as t, v_domains as d ";
$sql.="where t.domain_uuid = d.domain_uuid ";
$sql.="and (t.call_center_queue_uuid is null or t.call_center_agent_uuid is null) ";
//update all callcenter dialplans to have the @domain in the queue name
$sql="UPDATE \n";
$sql.=" v_dialplans d SET dialplan_xml = REPLACE( \n";
$sql.=" dialplan_xml, \n";
$sql.=" (SELECT call_center_queue_uuid::text FROM v_call_center_queues c WHERE c.dialplan_uuid = d.dialplan_uuid ), \n";
$sql.=" (SELECT call_center_queue_uuid::text FROM v_call_center_queues c WHERE c.dialplan_uuid = d.dialplan_uuid ) || '@' || (SELECT domain_name FROM v_domains vd WHERE vd.domain_uuid = d.domain_uuid) \n";
$sql.=" ) \n";
$sql.="WHERE dialplan_uuid IN (SELECT dialplan_uuid FROM v_call_center_queues) \n";
$sql.="AND dialplan_xml NOT LIKE '%<action application=\"callcenter\" data=%@%\"/>%' \n";