<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_CUST_TRAN_DETAIL_INQ';
$path_to_root = "../..";
include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/banking.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
//include_once($path_to_root . "/loyalty/includes/loyalty_db.inc");

include_once($path_to_root . "/includes/ui.inc");

if (!@$_GET['popup'])
{
	$js = "";
	if ($SysPrefs->use_popup_windows)
		$js .= get_js_open_window(900, 500);
	if (user_use_date_picker())
		$js .= get_js_date_picker();
	page(_($help_context = "Customer Transaction Details"), isset($_GET['customer_id']), false, "", $js);
}

if (isset($_GET['customer_id']))
{
	$_POST['customer_id'] = $_GET['customer_id'];
}

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

if (!@$_GET['popup'])
	start_form();

//if (!isset($_POST['customer_id']))
//	$_POST['customer_id'] = get_global_customer();

start_table(TABLESTYLE_NOBORDER);
start_row();

if (!@$_GET['popup'])
	//customer_list_cells(_("Select a customer: "), 'customer_id', null, true, false, false, !@$_GET['popup']);

//date_cells(_("From:"), 'TransAfterDate', '', null, -30);
//date_cells(_("To:"), 'TransToDate', '', null, 1);

if (!isset($_POST['filterType']))
	$_POST['filterType'] = 0;

//cust_allocations_list_cells(null, 'filterType', $_POST['filterType'], true);

customer_list_cells(_("Select a Customer:"), 'customer_id',null,"All customers");

date_cells(_("From:"), 'AfterDate', '', null, -30);
date_cells(_("To:"), 'BeforeDate');
stock_categories_list_cells("Category:", 'category_id', null,"");
end_row();

start_row();
stock_items_list_cells(_("Item:"), 'stock_id', null, "");

submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), 'default');
end_row();
end_table();

//set_global_customer($_POST['customer_id']);

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

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

div_start('totals_tbl');
//if ($_POST['customer_id'] != "" && $_POST['customer_id'] != ALL_TEXT)
//{
//	$customer_record = get_customer_details($_POST['customer_id'], $_POST['TransToDate']);
//    display_customer_summary($customer_record);
//    echo "<br>";
//}
div_end();

if(get_post('RefreshInquiry'))
{
	$Ajax->activate('totals_tbl');
}
//------------------------------------------------------------------------------------------------
function trans_view($trans)
{
	return get_trans_view_str(ST_SALESINVOICE, $trans["trans_no"]);
}


function format_date($row)
{
	return	sql2date($row["tran_date"]) ;
}


function prt_link($row)
{
  	if ($row['type'] == ST_CUSTPAYMENT || $row['type'] == ST_BANKDEPOSIT) 
		return print_document_link($row['trans_no']."-".$row['type'], _("Print Receipt"), true, ST_CUSTPAYMENT, ICON_PRINT);
  	elseif ($row['type'] == ST_BANKPAYMENT) // bank payment printout not defined yet.
		return '';
 	else	
 		return print_document_link($row['trans_no']."-".$row['type'], _("Print"), true, $row['type'], ICON_PRINT);
}

//------------------------------------------------------------------------------------------------
$sql = get_sql_for_customer_transaction_details($_POST['customer_id'],
	$_POST['BeforeDate'], $_POST['AfterDate'],$_POST['category_id'], $_POST['stock_id']);

//------------------------------------------------------------------------------------------------
db_query("set @bal:=0");

$cols = array(
	_("Customer No") => array('name'=>'debtor_no', 'ord'=>''),
        _("Customer Name") => array('name'=>'debtor_name', 'ord'=>''),
	_("#") => array('fun'=>'trans_view', 'ord'=>''),
	_("Reference") => array('name'=>'reference'),
	_("Date") => array('type'=>'tran_date', 'fun'=>'format_date', 'ord'=>''),
	_("Category") => array('name'=>'category_name', 'ord'=>''),
	_("Itemcode") => array('name'=>'stock_id', 'ord'=>''),
        _("Item Description") => array('name'=>'item_description', 'ord'=>''),
	_("Price") => array('name'=>'unit_price', 'align'=>'right', 'type'=>'amount'),
	_("Qty") => array('name'=>'quantity', 'align'=>'right', 'type'=>'qty'),
        _("Discount %") => array('name'=>'discount_percent', 'align'=>'right', 'type'=>'amount')

//		array('insert'=>true, 'fun'=>'gl_view'),
//		array('insert'=>true, 'fun'=>'credit_link'),
//		array('insert'=>true, 'fun'=>'edit_link'),
//		array('insert'=>true, 'fun'=>'prt_link')
	);


$table =& new_db_pager('trans_tbl', $sql, $cols);
//$table->set_marker('check_overdue', _("Marked items are overdue."));

$table->width = "85%";

display_db_pager($table);

if (!@$_GET['popup'])
{
	end_form();
	end_page(@$_GET['popup'], false, false);
}
?>
