<?php

error_reporting(E_ERROR);
require_once 'class/webpage.php';
require_once 'class/sql.php';
require_once 'PEAR.php';
require_once 'class/graph.php';
WEBPAGE::START();
$_LABELS = WEBPAGE::getCacheData(sprintf(WEBPAGE::_APP_LABELS_FILE, $_GET['lang']));
$mrow = WEBPAGE::$dbh->getAll('select c.gender, zp.short_name as branch, count(c.id) as hits from tblClients as c, tblZones as z, tblZones as zp 
    							where c.zone_id > 0 and z.id = c.zone_id and zp.id = z.parent_id group by c.gender, zp.id');
foreach ($mrow as $key => $val) {
    $data[$_LABELS[sprintf('tblClients.gender.%s', $val['gender'])]]['TOTAL'] += $val['hits'];
    $data[$_LABELS[sprintf('tblClients.gender.%s', $val['gender'])]][$val['branch']] = $val['hits'];
}
$graph = new Graph('', $_LABELS['tblUsers.zone_id'], '%', $data);
Graph::printStdCols($graph);
$top = 5;
$mrow1 = WEBPAGE::$dbh->getAll("SELECT bt.type, count( b.id ) AS hits, pz.short_name AS zone\r\n\t\t\t\t\t\t\t\tFROM tblBusiness AS b, tblBusinessTypes AS bt, tblZones AS z, tblClients AS c, tblZones AS pz\r\n\t\t\t\t\t\t\t\tWHERE bt.id = b.type_id\r\n\t\t\t\t\t\t\t\tAND FIND_IN_SET( c.id, b.client_list )\r\n\t\t\t\t\t\t\t\tAND z.id = c.zone_id\r\n\t\t\t\t\t\t\t\tAND pz.id = z.parent_id\r\n\t\t\t\t\t\t\t\tGROUP BY bt.id, pz.id");
$mrow1 = is_array($mrow1) ? $mrow1 : array();
$data1['TOTAL'] = array();
foreach ($mrow1 as $i => $row) {
    $data1[$row['zone']][$row['type']] = $row['hits'];
    $data1['TOTAL'][$row['type']] += $row['hits'];
}
$types['OTROS'] = '';
foreach ($data1 as $z => $row) {
    arsort($row);
    $c = $top;
    foreach ($row as $t => $h) {
        if ($c > 0) {
            $data2[$z][$t] = $h;
            $zones[$z] = '';
            $types[$t] = '';
        } else {
            $data2[$z]['OTROS'] += $h;
        }
        $c--;
    }
}
foreach ($zones as $i => $val_i) {
    foreach ($types as $j => $val_j) {
        $data3[$j][$i] = $data2[$i][$j] ? $data2[$i][$j] : 0;
    }
}
$grafico = new Graph('', '', '%', $data3);
Graph::printStdCols($grafico);
exit;