<?php
/**
 * phpaga
 *
 * financial stats
 *
 * View finance statistics: amount of money billed per customer
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: finances_bpc_graph.php 634 2005-09-13 23:11:31Z worf $
 *
 * Copyright (c) 2002, 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.
 */

$within = defined("PHPAGA_FROM_WITHIN") && PHPAGA_FROM_WITHIN;

if (!$within) {
    include_once("./config.php");
    $width = PHPAGA_FINGRAPH_WIDTH;
    $height = PHPAGA_FINGRAPH_HEIGHT;
} else {
    $width = 3 * PHPAGA_FINGRAPH_WIDTH;
    $height = 3 * PHPAGA_FINGRAPH_HEIGHT;
}

include_once(PHPAGA_LIBPATH."jpgraph_wrapper.php");

phpaga_user_pageperm(PHPAGA_PERM_VIEW_FINANCE);

$status = "";
$bpc_arr = array ();
$bpcamount_arr = array ();
$billedPerCompany = array();

if (isset($_REQUEST["year"]) && is_numeric($_REQUEST["year"])) {
    $year = $_REQUEST["year"];
} else {
    $year = date ("Y");
}

if (isset ($_REQUEST["currency"]) && is_numeric($_REQUEST["currency"])) {
    $currency = $_REQUEST["currency"];
} else {
    $currency = PHPAGA_DEFAULTCURRENCY;
}

$result =
    phpaga_finances_billedPerCompany($billedPerCompany, $currency, $year);

if ($result != PHPAGA_ERR_NONE)
    phpaga_error($result);
else
{

    if (!is_array($billedPerCompany) || (count($billedPerCompany) < 1))
    {

        $billedPerCompany = array(
            array(
                "cpn_name" =>  "",
                "cpn_id" =>  "",
                "amount" => 1,
                "curr_name" =>  "",
                "curr_decimals" =>  0,
                "amount_src" => 1));
    }

    /* populate arrays */
    foreach (array_reverse($billedPerCompany) as $tmp_stats)
    {
        $bpc_arr[] = trim(substr($tmp_stats["cpn_name"], 0,
                                 PHPAGA_GRAPHSHOWCPNNAMELENGHT));

        $bpcamount_arr[] = $tmp_stats["amount_src"];
    }

    $graph = new PieGraph($width, $height);

    $graph->SetFrame(false);

    $title = new Text("   "._("Amounts invoiced per customer")."   ", 0, 0);
    $title->SetFont(FF_FONT1);
    $title->SetColor("black");
    $graph->AddText($title);

    $graph->legend->setColor("#444444", "white");
    $graph->legend->Pos(0.01, 0.01);
    $graph->legend->SetShadow(false);
    $graph->legend->SetFillColor("#ffffff");

    $p1 = new PiePlot(array_reverse($bpcamount_arr));
    $p1->SetTheme('earth');
    $p1->SetCenter(0.28);
    $p1->SetLegends(array_reverse($bpc_arr));
    $graph->Add($p1);

    if (!$within)
        $graph->Stroke();
    else
        $handle = $graph->Stroke(_IMG_HANDLER);

}

?>