<?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($from, $to, $project_id = 0, $group = '') {

    $where = '';
    if ($project_id)
        $where .= " AND c.project=" . db_escape($project_id);
    if (!empty($group))
        $where .= " AND c.cont_group=" . db_escape($group) ;
    if (is_date($from) && is_date($to))
        $where .= " AND t.date between '" . date2sql($from) . "' and '" . date2sql($to) . "'";


    $sql = "SELECT t.type,t.id,t.ref, g.name as group_name,d.proj_name,t.date,"
            . " b.bank_account_name, c.amount FROM " . TB_PREF . "contribution_items c "
            . " right join " . TB_PREF . "person_contributions t on t.id=c.contribution_id "
            . " right join " . TB_PREF . "per_groups g on g.id=c.cont_group "
            . " right join " . TB_PREF . "projects d on d.proj_id=c.project "
            . " right join " . TB_PREF . "bank_accounts b on t.bank=b.id WHERE !t.inactive  " . $where
            . " ";
//    display_warning($sql);
    return db_query($sql, "No transactions were returned");
}

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

function print_member_transactions() {
    global $path_to_root, $systypes_array;
    
    $group = $_POST['PARAM_0'];
    $from = $_POST['PARAM_1'];
    $to = $_POST['PARAM_2'];
    $dimension = $_POST['PARAM_3'];
    $orientation = $_POST['PARAM_4'];
    $destination = $_POST['PARAM_5'];

    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');
    if ($group== ALL_TEXT)
        $grp_name = _('All');
    else
        $grp_name = get_group_name($group);
    $dec = user_price_dec();


    $cols = array(0, 95, 170, 220, 350, 420, 490, 550);

    $headers = array(_('Trans Type'), _('Group'), _('ref'), _('Project'), _('Date'), _('Bank'),
        _('Amount'));


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


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

    

    $params = array(0 => '',
        1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
        2 => array('text' => _('Project'), 'from' => $project_name, 'to' => ''),
        3 => array('text' => _('Member Group'), 'from' => $grp_name, 'to' => ''));

    $rep = new FrontReport(_('Group Contribution Report'), "GroupContributionReport", user_pagesize(), 9, $orientation);
    if ($orientation == 'L')
        recalculate_cols($cols);
    $rep->Font();
    $rep->Info($params, $cols, $headers, $aligns);
    $rep->NewPage();

    $grandtotal = 0;
    $result = db_query(get_groups($group,'','',TRUE), " member groups could not be retrieved");

    while ($myrow = db_fetch($result)) {
        $res = get_transactions($from, $to, $dimension, $myrow['id']);

        if (db_num_rows($res) == 0) {
            continue;
        }
        $rep->NewLine(1, 2);
        $total = 0;
        while ($trans = db_fetch($res)) {

            $rep->NewLine(1, 2);
            $rep->TextCol(0, 1, $systypes_array[$trans['type']]);
            $rep->TextCol(1, 2, $trans['group_name']);
            $rep->TextCol(2, 3, $trans['ref'], true);
            $rep->TextCol(3, 4, $trans['proj_name'], true);
            $rep->DateCol(4, 5, $trans['date'], true);
            $rep->TextCol(5, 6, $trans['bank_account_name'], true);
            $rep->AmountCol(6, 7, $trans['amount'], $dec);

            $total += $trans['amount'];
            $grandtotal += $trans['amount'];
        }
        $rep->Line($rep->row - 8);
        $rep->NewLine(2);
        $rep->TextCol(0, 3, _('Total'));
        for ($i = 0; $i < 2; $i++) {
            $rep->TextCol($i + 3, $i + 4, '');
        }
        $rep->AmountCol(6, 7, $total, $dec);
        $rep->Line($rep->row - 4);
        $rep->NewLine(2);
    }
    $rep->fontSize += 2;
    $rep->TextCol(0, 3, _('Grand Total'));
    $rep->fontSize -= 2;
    for ($i = 0; $i < 2; $i++)
        $rep->TextCol($i + 3, $i + 4, '', $dec);
    $rep->AmountCol(6, 7, $grandtotal, $dec);
    $rep->Line($rep->row - 4);
    $rep->NewLine();
    $rep->End();
}
