<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_MANUF_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 . "/manufacturing/includes/manufacturing_ui.inc");
include_once($path_to_root . "/manufacturing/includes/db/manufacturing_trans_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 = "Manufacturing Transactions Inquiry"), isset($_GET['supplier_id']), false, "", $js);
}


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

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


start_table(TABLESTYLE_NOBORDER);
start_row();
three_value_list_cells(_("Work Order"),'w_no',null,'Confirmed','1','Shedular','2','All','3');
manufacturing_transactions_list_cell("type", null, true);

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["row_id"]);
}

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

function gl_view($row)
{
	return get_gl_view_str($row["type"], $row["row_id"]);
}

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 fmt_debit($row)
{
	$value = $row["TotalAmount"];
	return $value>0 ? price_format($value) : '';

}

function fmt_credit($row)
{
	$value = -$row["TotalAmount"];
	return $value>0 ? price_format($value) : '';
}

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['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_manufacturing_transaction_inquiry($_POST['location'], $_POST['from_date'], $_POST['to_date'], $_POST['type'],$_POST['w_no']);

$cols = array(
			_("Type") => array('fun'=>'systype_name', 'ord'=>''), 
			_("#") => array('fun'=>'trans_view', 'ord'=>''), 
			_("Reference"), 
                        _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'desc'), 
			_("Location"),
			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);
}
?>
