<?php
  /**
   * phpaga
   *
   * Summary graph
   *
   * View a summary chart
   *
   * @author Florian Lanthaler <florian@phpaga.net>
   * @version $Id: summary_graph.php 870 2007-10-01 09:05:24Z florian $
   *
   * Copyright (c) 2005, Florian Lanthaler <florian@phpaga.net>
   *
   * All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions are
   * met:
   *
   *    * Redistributions of source code must retain the above copyright
   *      notice, this list of conditions and the following disclaimer.
   *
   *    * Redistributions in binary form must reproduce the above copyright
   *      notice, this list of conditions and the following disclaimer in
   *      the documentation and/or other materials provided with the
   *      distribution.
   *
   *    * Neither the name of Florian Lanthaler nor the names of his
   *      contributors may be used to endorse or promote products derived
   *      from this software without specific prior written permission.
   *
   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   */

include_once ("./config.php");
include_once(PHPAGA_LIBPATH.'graphs.php');

$summary = array();
$data = array();
$t = array();

$graphWidth = PHPAGA_GRAPH_WIDTH;
$graphHeight = PHPAGA_GRAPH_HEIGHT;

$summary = phpaga_getSummary();
if (PhPagaError::isError($summary)) {
    $summary->printMessage();
    exit;
}

if (!isset($_REQUEST["type"])) {
    phpaga_error(_('Summmary type not set.'));
    exit;
}

switch ($_REQUEST["type"]) {

    case "prj":

        foreach ($summary["projects"]["rows"] as $t)
            if (isset($t["sum_duration"]) && is_numeric($t["sum_duration"]) && ($t["sum_duration"] > 0))
                $data[$t["prj_title"]] = $t["sum_duration"] / 60.0;
        break;

    case "pe":

        foreach ($summary["persons"]["rows"] as $t)
            if (isset($t["sum_duration"]) && is_numeric($t["sum_duration"]) && ($t["sum_duration"] > 0))
                $data[$t["pe_person"]] = $t["sum_duration"] / 60.0;
        break;

    case "cu":

        if (!isset($_REQUEST["curr_id"]) || !is_numeric($_REQUEST["curr_id"])) {
            phpaga_error(_("Illegal currency"));
            exit;
        }

        if (!isset($summary["customers"][$_REQUEST["curr_id"]])) {
            phpaga_error(_("Unable to fetch data for currency"));
            exit;
        }

        foreach ($summary["customers"][$_REQUEST["curr_id"]]["rows"] as $t)
            if (isset($t["sum_amount"]) && is_numeric($t["sum_amount"]) && ($t["sum_amount"] > 0))
                $data[$t["cpn_name"]] = $t["sum_amount"];

        break;

    default:
        phpaga_error(_("Illegal type"));
        exit;
}

$graph = new PhPagaGraphPieChart();
$graph->data['summary'] = new ezcGraphArrayDataSet($data);
$graph->renderToOutput($graphWidth, $graphHeight);

?>