<?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 677 2006-03-21 19:14:13Z 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);

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

$months = array();
$currInfo = array();

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

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

$t_month = $first_month;

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

for ($i = 1; $i <= 12; $i++)
{
    $months[$i - 1] = $t_month - 1;

    $months_billed[$i - 1] = null;
    $months_paid[$i - 1] = null;

    $cal_monthNamesShort[$i - 1] =
        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"];
else
    $currency = PHPAGA_DEFAULTCURRENCY;

phpaga_currencies_getCurrencyInfo($currInfo, $currency);

$result = phpaga_finances_billedPerMonth($billedPerMonth, $currency, $year);

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

    $result = phpaga_finances_paidPerMonth($paidPerMonth, $currency, $year);

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

        $idx = null;

        foreach ($billedPerMonth as $tmp_stats) {

            $idx = $tmp_stats["month"] - PHPAGA_FIN_FISCYEAR_MONTH;

            if ($idx < 0)
                $idx += 12;

            $months_billed[$idx] = $tmp_stats["amount"];
        }

        $idx = null;

        foreach ($paidPerMonth as $tmp_stats) {

            $idx = $tmp_stats["month"] - PHPAGA_FIN_FISCYEAR_MONTH;

            if ($idx < 0)
                $idx += 12;

            $months_paid[$idx] = $tmp_stats["amount"];
        }

        $graph = new JpGraph ($width, $height);
        $graph->SetScale ("textlin");

        $graph->SetFrame(false);

        $graph->img->SetMargin(80, 10, 30, 50);
        $title =
            new Text(sprintf(_("Invoiced / Received (FY %s)"), $year), 0.25, 0);

        $title->SetFont (FF_FONT1);

        $title->SetColor ("black");
        $graph->AddText ($title);

        $p1 = new BarPlot ($months_billed);
        $p1->SetLegend(_("Invoiced"));
        $p1->SetFillGradient("#ffaaaa", "#ff4444", GRAD_HOR);

        $p2 = new BarPlot ($months_paid);
        $p2->SetLegend(_("Received"));
        $p2->SetFillGradient("#aaffaa", "#44ff44", GRAD_HOR);

        $gbplot = new GroupBarPlot(array($p1, $p2));

        $graph->legend->setColor("#444444", "white");
        $graph->legend->setFillColor("white");
        $graph->legend->SetShadow(false);
        $graph->legend->setLayout(LEGEND_HOR);
        $graph->legend->pos(0.5, 0.98, "center", "bottom");

        $graph->xaxis->SetTickLabels ($cal_monthNamesShort);
        $graph->yaxis->title->Set($currInfo["curr_name"]);
        $graph->yaxis->SetLabelFormatCallback("phpaga_formatint");
        $graph->yaxis->SetTitleMargin(65);
        $graph->Add ($gbplot);

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

?>