<?php

/* * ********************************************************************
  Copyright (C) FrontAccounting, LLC.
  Released under the terms of the GNU General Public License, GPL,
  as published by the Free Software Foundation, either version 3
  of the License, or (at your option) any later version.
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 * ********************************************************************* */
//$page_security = 'SA_MMBRTR';

// ----------------------------------------------------------------
// $ Revision:	2.0 $
// Creator:	Joe Hunt
// date_:	2005-05-19
// Title:	Customer Balances
// ----------------------------------------------------------------
$path_to_root = "..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/gl/includes/gl_db.inc");
include_once($path_to_root . "/sales/includes/db/customers_db.inc");
include_once($path_to_root . '/modules/kanban/includes/kanban_db.inc');
include_once($path_to_root . '/modules/kanban/includes/kanban_ui.inc');

//----------------------------------------------------------------------------------------------------

print_member_transactions();

function get_transactions($project_id) {

    $where = '';
    if ($project_id)
        $where .= " AND c.project=" . db_escape($project_id);
    $sql = "SELECT d.proj_name, SUM(c.amount) as amount, d.proj_amt FROM " . TB_PREF . "contribution_items c "
            . " right join " . TB_PREF . "person_contributions t on t.id=c.contribution_id "
            . " right join " . TB_PREF . "projects d on d.proj_id=c.project "
            . "  WHERE !t.inactive  " . $where
            . " and type=" . ST_MEMBER_CONT
            . " group by c.project order by c.project ";
    
    return db_query($sql, "No transactions were returned");
}

//----------------------------------------------------------------------------------------------------

function print_member_transactions() {
    global $path_to_root, $systypes_array;

    $dimension = $_POST['PARAM_0'];
    $orientation = $_POST['PARAM_1'];
    $destination = $_POST['PARAM_2'];

    if ($destination)
        include_once($path_to_root . "/reporting/includes/excel_report.inc");
    else
        include_once($path_to_root . "/reporting/includes/pdf_report.inc");

    $orientation = ($orientation ? 'L' : 'P');

    $dec = user_price_dec();


    $cols = array(0, 100, 200, 300, 400, 515);

    $headers = array(_('Project'), _('Target'), _('Attained'), _('Remainder'), _('Attained %'));


    $aligns = array('left', 'right', 'right', 'right', 'right');


    $project_name = '';
    if ($dimension) {
        $project_name = get_project_name($dimension);
    }

    

    $params = array(0 => '',
        1 => array('text' => _('Project'), 'from' => $project_name, 'to' => ''));

    $rep = new FrontReport(_('Projects Summary'), "ProjectsSummary", user_pagesize(), 9, $orientation);
    if ($orientation == 'L')
        recalculate_cols($cols);
    $rep->Font();
    $rep->Info($params, $cols, $headers, $aligns);
    $rep->NewPage();

    $grandtotal = 0;

    $res = get_transactions($dimension);
    $rep->NewLine(1, 2);
    $total = 0;
    while ($trans = db_fetch($res)) {
        $remainder = ($trans['proj_amt'] - $trans['amount']);
        $attained_percentage = $trans['amount']/max($trans['proj_amt'],1) * 100;
        $rep->NewLine(1, 2);
        $rep->TextCol(0, 1, $trans['proj_name']);
        $rep->AmountCol(1, 2, $trans['proj_amt'], $dec);
        $rep->AmountCol(2, 3, $trans['amount'], $dec);
        $rep->AmountCol(3, 4, max($remainder,0), $dec);
        $rep->TextCol(4, 5, $attained_percentage . '%');

    }
    
    $rep->NewLine();
    $rep->End();
}
