<?php
/**
 * phpaga
 *
 * financial stats
 *
 * View finance statistics: amount of money billed and amount of money
 * received per month
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: finances_bprpm_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);

$paidPerMonth = array();
$billedPerMonth = array();
$missingPerMonth = array();

$months = array();
$currInfo = array();
$year = date ("Y");

$currency = PHPAGA_DEFAULTCURRENCY;

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

$fy_dates = phpaga_finances_getfiscalyeardates($year);
$t_month = date("m", strtotime($fy_dates["start"]));

$oldlocale = setlocale(LC_TIME, 0);
setlocale(LC_TIME, PHPAGA_LANGNAME);

for ($i = 1; $i <= 12; $i++) {
    $months[$i] = $t_month - 1;
    $cal_monthNamesShort[$i] = strftime ("%b", mktime (0, 0, 0, $t_month, 1, 2001));
    $t_month++;
}

setlocale(LC_TIME, $oldlocale);

if (isset ($_GET["currency"]) && is_numeric ($_GET["currency"]))
    $currency = $_GET["currency"];

$currInfo = phpaga_currencies_getCurrencyInfo($currency);

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

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

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

for ($i = 1; $i <= 12; $i++) {

    $idx = $cal_monthNamesShort[$i];

    $months_billed[$idx] = (float)0;
    $months_paid[$idx] = (float)0;
    $months_missing[$idx] = (float)0;
}

foreach ($billedPerMonth as $t)
    $months_billed[$cal_monthNamesShort[$t["month"]]] = (float)$t["amount"];

foreach ($paidPerMonth as $t)
    $months_paid[$cal_monthNamesShort[$t["month"]]] = (float)$t["amount"];

foreach ($missingPerMonth as $t)
    $months_missing[$cal_monthNamesShort[$t["month"]]] = (float)$t["amount"];

$graph = new PhPagaGraphBarChart();
$graph->title = sprintf(_("Invoiced / Received / Missing (FY %s)"), $year);
$graph->legend->position = ezcGraph::BOTTOM;
$graph->xAxis = new ezcGraphChartElementLabeledAxis();
$graph->yAxis->label = $currInfo["curr_name"];
$graph->xAxis->axisLabelRenderer->showZeroValue = true;

$graph->data[_("Invoiced")] = new ezcGraphArrayDataSet($months_billed);
$graph->data[_("Received")] = new ezcGraphArrayDataSet($months_paid);
$graph->data[_("Missing")] = new ezcGraphArrayDataSet($months_missing);

$graph->xAxis->labelCount = max(count($graph->data[_("Invoiced")]),
                                count($graph->data[_("Received")]),
                                count($graph->data[_("Missing")]));

$graph->renderToOutput(PHPAGA_FINGRAPH_WIDTH, PHPAGA_FINGRAPH_HEIGHT);

?>