<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_INVENTORY_TRANS_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 . "/inventory/includes/inventory_transactions_ui.inc");
include_once($path_to_root . "/inventory/includes/db/inventory_transactions_db.inc");
include_once($path_to_root . "/reporting/includes/reporting.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 = "Inventory Transactions Inquiry"), isset($_GET['supplier_id']), false, "", $js);
}
//if (isset($_GET['Location'])){
//	$_POST['Location'] = $_GET['Location'];
//}
//if (isset($_GET['FromDate'])){
//	$_POST['TransAfterDate'] = $_GET['FromDate'];
//}
//if (isset($_GET['ToDate'])){
//	$_POST['TransToDate'] = $_GET['ToDate'];
//}

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

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


start_table(TABLESTYLE_NOBORDER);
start_row();

inventory_transactions_list_cell("type", null, '');

date_cells(_("From:"), 'from_date', '', null, -30);
date_cells(_("To:"), 'to_date');

locations_list_cells('Location:', 'location', null, "All Locations");

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

end_row();
end_table();

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



if(get_post('RefreshInquiry'))
{
	$Ajax->activate('totals_tbl');
}

//------------------------------------------------------------------------------------------------
function systype_name($dummy, $type)
{
	global $systypes_array;
	return $systypes_array[$type];
}

function trans_view($trans)
{
	return get_trans_view_str($trans["type"], $trans["id"]);
}

function due_date($row)
{
	return ($row["type"]== ST_SUPPINVOICE) || ($row["type"]== ST_SUPPCREDIT) ? $row["due_date"] : '';
}

function gl_view($row)
{
	if($row["type"]== ST_INVADJUST){
            return get_gl_view_str($row["type"], $row["id"]);
        }
        else{
            return null;
        }
}

function credit_link($row)
{
	if (@$_GET['popup'])
		return '';
	return $row['type'] == ST_SUPPINVOICE && $row["TotalAmount"] - $row["Allocated"] > 0 ?
		pager_link(_("Credit This"),
			"/purchasing/supplier_credit.php?New=1&invoice_no=".
			$row['trans_no'], ICON_CREDIT)
			: '';
}

function from_location($row)
{
    if(strlen($row["from_location"]) > 0){
        $value = $row["from_location"]." - ".$row["from_location_name"];
        return $value;
    }
    else{
        return null;
    }

}

function to_location($row)
{
	if($row['type'] == ST_INVADJUST){
                return "";
    }
    else{
        if(strlen($row["to_location"]) > 0){
            $value = $row["to_location"]." - ".$row["to_location_name"];
            return $value;
        }
        else{
            return null;
        }
    }
}

function prt_link($row)
{
//  	if ($row['type'] == ST_MATERIAL_ISSUE || $row['type'] == ST_MATERIAL_RETURN || $row['type'] == ST_PROD_ADD || $row['type'] == ST_PROD_RETURN) 
//                return print_document_link($row['row_id'], _("&Print ".$row['type']), true, $row['type'], ICON_PRINT);
//        elseif($row['type'] == ST_WORKORDER)
                return print_document_link($row['id'], _("&Print ".$row['type']), true, $row['type'], ICON_PRINT);
}

function check_overdue($row)
{
	return $row['OverDue'] == 1
		&& (abs($row["TotalAmount"]) - $row["Allocated"] != 0);
}
//------------------------------------------------------------------------------------------------

$sql = get_sql_for_inventory_transactions_inquiry($_POST['location'], $_POST['from_date'], $_POST['to_date'], $_POST['type']);

$cols = array(
			_("Type") => array('fun'=>'systype_name', 'ord'=>''), 
			_("#") => array('fun'=>'trans_view', 'ord'=>''), 
			_("Reference"), 
                        _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'desc'), 
			_("From Location") => array('fun'=>'from_location', 'ord'=>''), 
                        _("To Location") => array('fun'=>'to_location', 'ord'=>''), 
			array('insert'=>true, 'fun'=>'gl_view'),
			array('insert'=>true, 'fun'=>'prt_link')
			);

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


/*show a table of the transactions returned by the sql */
$table =& new_db_pager('trans_tbl', $sql, $cols);
//$table->set_marker('check_overdue', _("Marked items are overdue."));

$table->width = "60%";

display_db_pager($table);

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