<?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 843 2007-08-30 17:16:32Z florian $
 *
 * 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.
 */

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

phpaga_user_pageperm(PHPAGA_PERM_VIEW_FINANCE);

$status = '';
$bpc_arr = array ();
$bpcamount_arr = array ();
$billedPerCompany = array();
$currency = PHPAGA_DEFAULTCURRENCY;
$year = date ('Y');

if (isset($_REQUEST['year']) && is_numeric($_REQUEST['year']))
    $year = $_REQUEST['year'];

if (isset ($_REQUEST['currency']) && is_numeric($_REQUEST['currency']))
    $currency = $_REQUEST['currency'];

$billedPerCompany = phpaga_finances_billedPerCompany($currency, $year);
if (PhPagaError::isError($billedPerCompany)) {
    $billedPerCompany->printMessage();
    exit;
}

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

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

foreach ($billedPerCompany as $tmp_stats) {
    $bpc_arr[] = trim(substr($tmp_stats['cpn_name'], 0, PHPAGA_GRAPHSHOWCPNNAMELENGHT));
    $bpcamount_arr[] = $tmp_stats['amount_src'];

    $data[trim(substr($tmp_stats['cpn_name'], 0, PHPAGA_GRAPHSHOWCPNNAMELENGHT))] = $tmp_stats['amount_src'];
}

$graph = new PhPagaGraphPieChart();

$graph->title = _('Amounts invoiced per customer');
$graph->data['bpc'] = new ezcGraphArrayDataSet($data);
$graph->legend = false;

$graph->renderToOutput(PHPAGA_FINGRAPH_WIDTH, PHPAGA_FINGRAPH_HEIGHT);

?>