<?php
if(!(is_readable(realpath(dirname(__FILE__)).'/mysqlroutines.inc'))) {
        die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/mysqlroutines.inc');
} else {
        include_once(realpath(dirname(__FILE__)).'/mysqlroutines.inc');
}

function db_getExcludeBranchesToImport($cn)
{
        $query  = "SELECT DISTINCT br.idbranch FROM branches as br
                        INNER JOIN pollingobjects as po ON po.skbranch = br.skbranch
                        INNER JOIN useranswers as ua ON po.idpollingobject = ua.idpollingobject AND ua.idpollingobjectstatus IN (0,2) 
                        WHERE br.ismain !=1 AND br.idbranch NOT IN (
                                SELECT DISTINCT br.idbranch FROM branches as br
                                INNER JOIN pollingobjects as po ON po.skbranch = br.skbranch
                                INNER JOIN useranswers as ua ON po.idpollingobject = ua.idpollingobject AND ua.idpollingobjectstatus NOT IN (0,2));";
        $arr = db_runQueryAndReturnArray($cn, $query, 'db_getExcludeBranchesToImport');
        $resarr = array();
        foreach ($arr as $key => $value){
                array_push($resarr, $value["idbranch"]);
        }
        return $resarr;
}

?>