<?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_CUSTPAYMREP';
// ----------------------------------------------------------------
// $ Revision:	2.0 $
// Creator:	Joe Hunt
// date_:	2005-05-19
// Title:	Aged 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");

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

print_aged_customer_analysis();

function get_invoices($supplier_id,$start, $to) {
    $fromdate = date2sql($start);
    $todate = date2sql($to);

    $cust_filter = '';
    if ($supplier_id != ALL_TEXT)
            $cust_filter = " AND trans.supplier_id=".db_escape($supplier_id) ." ";
    
    $sql = "SELECT type, reference, tran_date,trans.supplier_id,s.supp_name,
		(ov_amount + ov_gst + ov_discount) AS amount
		FROM " . TB_PREF . "supp_trans trans
                LEFT JOIN ".TB_PREF."suppliers s ON trans.supplier_id=s.supplier_id

		WHERE (trans.type=".ST_SUPPINVOICE." OR trans.type=".ST_SUPPCREDIT.")
			$cust_filter
			AND tran_date between '$fromdate' and '$todate' 
                        AND (ov_amount + ov_gst + ov_discount) <> 0 ";
    
    $sql .= " ORDER BY tran_date,trans_no";
    return db_query($sql, "The supplier transactions could not be retrieved");
}

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

function print_aged_customer_analysis() {
    global $path_to_root, $systypes_array, $SysPrefs;

    $start = $_POST['PARAM_0'];
    $to = $_POST['PARAM_1'];
    $fromsupp = $_POST['PARAM_2'];
    $orientation = $_POST['PARAM_3'];
    $destination = $_POST['PARAM_4'];
    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 ($fromsupp == ALL_TEXT)
        $from = _('All');
    else
        $from = get_supplier_name($fromsupp);
    
    $dec = user_price_dec();
    $cols = array(0, 100, 150, 200, 400, 450, 550);
    $headers = array(_('Type'),_('Ref'),_('Date'),_('Supplier'),_('Currency'),_('Amount'));

    $aligns = array('left', 'left', 'left', 'left', 'left', 'right');
    $params = array(
        0 => '',
        1 => array('text' => _('Date'), 'from' => $start, 'to' => $to),
        2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''));

    
    $rep = new FrontReport(_('purchases By Dates Report'), "purchasesbydates", user_pagesize(), 9, $orientation);
    if ($orientation == 'L')
        recalculate_cols($cols);

    $rep->Font();
    $rep->Info($params, $cols, $headers, $aligns);
    $rep->NewPage();

    $res = get_invoices($fromsupp, $start,$to);

    $total = 0;
    while ($trans = db_fetch($res)) {
        $sql = "SELECT supplier_id, supp_name, curr_code FROM " . TB_PREF . "suppliers";
        $sql .= " WHERE supplier_id=" . db_escape($trans['supplier_id']);
        $result = db_query($sql, "The Suppliers could not be retrieved");
        $myrow = db_fetch($result);
        $rep->NewLine(1, 2);
        $rep->TextCol(0, 1, $systypes_array[$trans['type']], -2);
        $rep->TextCol(1, 2, $trans['reference'], -2);
        $rep->DateCol(2, 3, $trans['tran_date'], true, -2);
        $rep->TextCol(3, 4, $myrow['supp_name'], -2);
        $rep->TextCol(4, 5, $myrow['curr_code'], -2);

        $rep->AmountCol(5, 6, $trans['amount'], $dec);
        $total += $trans['amount'];
    }
    $rep->Line($rep->row - 8);
    $rep->NewLine(2);

    $rep->fontSize += 2;
    $rep->TextCol(0, 1, _('Grand Total'));
    $rep->fontSize -= 2;
    $rep->AmountCol(5, 2, $total, $dec);

    $rep->Line($rep->row - 8);

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