<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_MANUF_LABOUR_BOOKING_INQ';
$path_to_root = "../..";
include($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 . "/manufacturing/includes/manufacturing_ui.inc");
include_once($path_to_root . "/manufacturing/includes/db/work_order_booking_db.inc");

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
if (isset($_GET['outstanding_only']) && ($_GET['outstanding_only'] == true))
{
// curently outstanding simply means not closed
	$outstanding_only = 1;
	page(_($help_context = "Search Outstanding Work Orders"), false, false, "", $js);
}
else
{
	$outstanding_only = 0;
	page(_($help_context = "Search Work Centre Shedules"), false, false, "", $js);
}
//-----------------------------------------------------------------------------------
// Ajax updates
//
if (get_post('SearchOrders')) 
{
	$Ajax->activate('orders_tbl');
} elseif (get_post('_OrderNumber_changed')) 
{
	$disable = get_post('OrderNumber') !== '';

	$Ajax->addDisable(true, 'StockLocation', $disable);
	$Ajax->addDisable(true, 'OverdueOnly', $disable);
	$Ajax->addDisable(true, 'OpenOnly', $disable);
	$Ajax->addDisable(true, 'SelectedStockItem', $disable);

	if ($disable) {
		set_focus('OrderNumber');
	} else
		set_focus('StockLocation');

	$Ajax->activate('orders_tbl');
}

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

if (isset($_GET["stock_id"]))
	$_POST['SelectedStockItem'] = $_GET["stock_id"];

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

start_form(false, false, $_SERVER['PHP_SELF'] ."?outstanding_only=$outstanding_only");

start_table(TABLESTYLE_NOBORDER);
start_row();
three_value_list_cells(_("Work Order Type"),'w_no',null,'Confirmed','1','Shedular','2','All','3');
ref_cells(_("Work Order #:"), 'wo', '',null, '', false);
employees_simple_list_cells(_("Employee:"), 'employee_code', null,'');
workcenter_list_cells(_("Work Centre:"), 'work_centre', null, '');
end_row();

start_row();
workorder_process_work_type_list_cells("Work Type",'work_type',null,true);

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


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

//function view_link($dummy, $order_no)
//{
//	return get_trans_view_str(ST_WORKORDER, $order_no);
//}
//
//function view_end_date($row)
//{
//    $seconds1=strtotime($row['work_start_date_time']);
//    $seconds2=$row['duration'];
//    $seconds_all=$seconds1+$seconds2;
//    $w = date('Y-m-d H:i:s', $seconds_all);
//    $w=substr($w,0,16);
//    return $w;      
//    
//}
//
//function view_duraion($row)
//{
//        
//    $hoursminsandsecs = gmdate('H:i:s',$row['duration']);
//    return $hoursminsandsecs=substr($hoursminsandsecs,0,5);
//}
//
//function view_remaining($row)
//{
//        
//    $hoursminsandsecs = gmdate('H:i:s',$row['remaining_duration']);
//    return $hoursminsandsecs=substr($hoursminsandsecs,0,5);
//}
//
//function edit_link($row)
//{
//	return  $row['closed'] ? '<i>'._('Closed').'</i>' :
//		pager_link(_("Edit"),
//			"/manufacturing/work_order_entry.php?trans_no=" . $row["id"], ICON_EDIT);
//}
//
//function view_start_date($row)
//{
//    $hoursminsandsecs = $row['work_start_date_time'];
//    return $hoursminsandsecs=substr($hoursminsandsecs,0,16);
//    
//}

function work_type_view($row)
{
    global $workorder_process_work_types;
    return $workorder_process_work_types[$row['work_type']];
   
    
}
function sqltodatestart($row)
{
    $datetime=$row['start_date_time'];
    $time=substr($row['start_date_time'],10);
    
    $date=sql2date($row['start_date_time']);
    return $date." ".$time;
   
    
}

function sqltodate_end($row)
{
    
    $datetime=$row['end_date_time'];
    $time=substr($row['end_date_time'],10);
    
    $date=sql2date($row['end_date_time']);
    return $date." ".$time;
   
    
}

function view_employee($row)
{
    
    $name=$row['employee_code']." - ".$row['initials']." ".$row['last_name']." (".$row["first_name"].")";
    
    return $name;
   
    
}

$from_date=date2sql($_POST['from_date']);
$to_date=date2sql($_POST['to_date']);
$sql = get_sql_for_order_booking_inquiries_labor($_POST['w_no'],$_POST['wo'],$_POST['employee_code'],$_POST['work_centre'],$_POST['work_type'],$from_date,$to_date);

$cols = array(
	_("Work Order No")=> array('align' => 'center'), 
        _("Work Centre")=> array('align' => 'center'),
        _("Process")=> array('align' => 'center'),
        _("Work Type")=> array('fun'=>'work_type_view','align' => 'center'),	
	_("Employee")=> array('fun'=>'view_employee'),
	_("Start Date Time")=> array('fun'=>'sqltodatestart','align' => 'center'),
        _("End Date Time")=> array('fun'=>'sqltodate_end','align' => 'center')      
	
);

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

$table->width = "90%";

display_db_pager($table);

end_form();
end_page();
?>
