<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PETTY_CASH_ITEM_RECEIPTS_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 . "/sales/includes/sales_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/inventory/includes/db/material_requisitions_db.inc");
include_once($path_to_root . "/inventory/includes/db/petty_cash_item_receive_inquiry_db.inc");


//if (!@$_GET['popup'])
//{
//	$js = "";
//	if ($SysPrefs->use_popup_windows)
//		$js .= get_js_open_window(900, 600);
//	if (user_use_date_picker())
//		$js .= get_js_date_picker();
//	page("Manage Material Requisitions", false, false, "", $js);
//}

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(1000, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();

$outstanding = 0;
$help_context = "Petty Cash Item Receipts Inquiry";

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

if($outstanding == 1){
    $help_context = "Outstanding Petty Cash Item Receipts Inquiry";
} 

page($help_context, false, false, "", $js);



if (isset($_GET['Approves']))
{
	$_SESSION['current_user_for_mr'] = $_SESSION['wa_current_user']->user;
}

if (isset($_GET['project_id']))
{
	$_POST['project_id'] = $_GET['project_id'];
}
if (isset($_GET['from_date']))
{
	$_POST['from_date'] = $_GET['from_date'];
}
if (isset($_GET['to_date']))
{
	$_POST['to_date'] = $_GET['to_date'];
}


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

if (isset($_POST['SelectStockFromList']) && ($_POST['SelectStockFromList'] != "") &&
	($_POST['SelectStockFromList'] != ALL_TEXT))
{
 	$selected_stock_item = $_POST['SelectStockFromList'];
}
else
{
	unset($selected_stock_item);
}
//---------------------------------------------------------------------------------------------
//	Query format functions
//
function check_overdue($row)
{
	global $trans_type;
	if ($trans_type == ST_SALESQUOTE)
		return (date1_greater_date2(Today(), sql2date($row['delivery_date'])));
//	else
//		return (date1_greater_date2(Today(), sql2date($row['mtr_required_date'])));
}

function _type($row)
{
	global $systypes_array;
	return $systypes_array[$row['type']];
}

function _view_link($row)
{
	return get_trans_view_str($row['type'], $row['trans_no']);
}

function _location($row)
{
	return $row['location_name'];
}

function _project($row)
{
	return $row['project_description'];
}

function _tran_date($row)
{
  return sql2date($row['tran_date']);
}

function _item($row)
{
  return $row['stock_id']." - ".$row['item_description'];
}

function _unit($row)
{
  return $row['tran_unit_name'];
}

function _qty_received($row)
{
  return $row['tran_qty'];
}

function _qty_issued($row)
{
  return $row['issued_qty'];
}

function _originator($row)
{
  return $row['real_name'];
}

function _comments($row)
{
  return $row['comment'];
}



function _dispatch_link($row)
{
//    if($row['tran_qty'] * $row['conversion_factor'] > $row['issued_qty'])
//    {
//        return pager_link( _("Issue"),"/inventory/issue_petty_cash_items.php?stype=".$row['source_type']."&sdid=".$row['source_detail_id']);
//    }
////    elseif($row['approval_status'] == AS_COMPLETED)
////    {
////        return pager_link( _("---"),"/inventory/issue_materials.php?mr_no=" .$row['mr_no']);
////    }
//    else 
//    {
//        return null;
//    }
    
    $source_type = $row["source_type"];
    $source_trans_no = $row["source_trans_no"];
    $source_detail_id = $row["source_detail_id"];
    $link = get_petty_cash_item_receive_str($source_type, $source_trans_no, $source_detail_id, "Receive items");
    
    return $link;
  		
}


if (get_post('SearchOrders')) 
{
	$Ajax->activate('orders_tbl');
}


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

start_table(TABLESTYLE_NOBORDER);
    start_row();
        ref_cells(_("MR #:"), 'mr_no', '',null, '', false);
//        ref_cells(_("MR Ref"), 'mr_reference', '',null, '', false);
        
        $_POST['mr_reference'] = '';
        hidden('mr_reference', $_POST['mr_reference']);

        date_cells(_("From:"), 'from_date', '', null, -15);
        date_cells(_("To:"), 'to_date', '', null, +15);
    end_row();
end_table();

start_table(TABLESTYLE_NOBORDER, "width=50%");
    start_row();
        project_list_cells("Project", 'project_id',null, '');
        stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true, false); 
        
    end_row();
end_table();

start_table(TABLESTYLE_NOBORDER);
    start_row();
        system_users_list_cells("Originator", 'originator',null, '');
        locations_list_cells(_("Issuing Location:"), 'issuing_location', null, true, true);
        
        hidden("outstanding", $outstanding);

        submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
    end_row();

end_table(1);
//---------------------------------------------------------------------------------------------
//	Orders inquiry table
//
$sql = get_sql_for_petty_cash_item_receipts_inquiry(@$_POST['from_date'], @$_POST['to_date'], 
//            @$_POST['created_on_from'], @$_POST['created_on_to'],  
            @$_POST['issuing_location'],
            @$_POST['mr_no'], @$_POST['mr_reference'],
            @$_POST['project_id'],@$_POST['originator'],
            @$selected_stock_item, @$outstanding );


	$cols = array(
		_("Type") => array('fun'=>'_type'), 
		_("Trans. #") => array('fun'=>'_view_link', 'align' => 'center'), 
//		_("Ref"),
                _("Location") => array('fun'=>'_location'), 
                _("Project") => array('fun'=>'_project'),     
                _("Date") => array('fun'=>'_tran_date', 'align' => 'center'),
                _("Item") => array('fun'=>'_item'),
                _("Unit") => array('fun'=>'_unit'),
                _("Qty") => array('fun'=>'_qty_received', 'align' => 'right'),
//                _("Qty. Issued") => array('fun'=>'_qty_issued', 'align' => 'right'),
		_("Originator") => array('fun'=>'_originator'),
		_("Comments") => array('fun'=>'_comments'));

        if($outstanding == 1){
            array_append($cols, array(
                array('insert'=>true, 'fun'=>'_dispatch_link'), ));
        }


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

$table->width = "95%";

display_db_pager($table);
submit_center('Update', _("Update"), true, '', null);

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