<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_WORK_ORDER_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_inquiry_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 - approved and not closed
	$outstanding_only = 1;
	page(_($help_context = "Search Outstanding Work Orders"), false, false, "", $js);
}
else
{
	$outstanding_only = 0;
	page(_($help_context = "Search Work Orders"), 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, 'WorkOrderType', $disable);
	$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();
    ref_cells(_("Work Order#:"), 'OrderNumber','',null,'', true);
    ref_cells(_("Reference:"), 'OrderReference','',null,'', true);
   
    project_list_cells("Project", "ProjectId", null, '');
//    locations_list_cells(_("Location:"), 'StockLocation', null, true);
end_row();
end_table();

start_table(TABLESTYLE_NOBORDER);
start_row();
    check_cells( _("Only Overdue:"), 'OverdueOnly', null);

    if ($outstanding_only==0){
        check_cells( _("Only Open:"), 'OpenOnly', null);
        three_value_list_cells(_("Work Order Type"),'WorkOrderType',null,'Confirmed','1','Shedular','2','All','3');
    }
    else{
//        $_POST['OpenOnly'] = 0;
        $_POST['WorkOrderType'] = 1;
    }
    
    stock_manufactured_items_list_cells(_("for item:"), 'SelectedStockItem', null, true);

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

//-----------------------------------------------------------------------------
function check_overdue($row)
{
	return (!$row["closed"] 
		&& date_diff2(Today(), sql2date($row["required_by"]), "d") > 0);
}

function view_link($dummy, $order_no)
{
	return get_trans_view_str(ST_WORKORDER, $order_no);
}

function view_stock($row)
{
	return view_stock_status($row["stock_id"], $row["description"], false);
}

function wo_type_name($dummy, $type)
{
	global $wo_types_array;
	
	return $wo_types_array[$type];
}

function status_link($row)
{
    global $approval_statuses;
    
    if($row['id'] < 0){
        return "Draft";
    }
    
    if($row['closed']){
        return '<i>'._('Closed').'</i>' ;
    }
    
    if($row["released"] == 0){
        if($row['display_status'] != ""){
            return $row['display_status'];
        }
        else{
            return $approval_statuses[$row['approval_status']];
        }
    }
    
    if($row["released"]){
        return "Released";
    }
		
    return "Confimed";
}

function edit_link($row)
{
	return  $row['closed'] ? '' :
		pager_link(_("Edit"),
			"/manufacturing/work_order_entry.php?trans_no=" . $row["id"], ICON_EDIT);
}


function approve_link($row)
{
	if($row["closed"]){
        return null;
    }
    
    if($row["released"]==0 && $row['id'] > 0){
        if($row['approval_status'] == AS_APPROVED){
            return pager_link(_("Reopen"), "/manufacturing/approve_work_orders.php?trans_no=" . $row["id"], null);
        }
        elseif($row['approval_status'] == AS_REJECTED){
            return null;
        }
        else{
            return pager_link(_("Approve"), "/manufacturing/approve_work_orders.php?trans_no=" . $row["id"], null);
        }
    }

}


function release_link($row)
{
	if($row["closed"]){
        return null;
    }

    if($row['id'] < 0){
        return null;
    }

    
    if($row["released"] == 1){
        return null;
    }
    
    if($row["released"]==0 && $row["approval_status"] == AS_APPROVED){
        return pager_link(_('Release'), "/manufacturing/work_order_release.php?trans_no=" . $row["id"]);
    }
    else{
        return null;
    }
    
}

function purchase_link($row)
{
	if($row["closed"]){
        return null;
    }

    if($row['id'] < 0){
        return null;
    }
    
    if($row["approval_status"] == AS_APPROVED){
        if($row['closed_for_prs']){
            return pager_link(_('---'), "/manufacturing/work_order_pr.php?trans_no=" . $row["id"]);
        }
        else{
            return pager_link(_('Purchase'), "/manufacturing/work_order_pr.php?trans_no=" . $row["id"]);
        }
    }
    else{
        return null;
    }
}


function issue_link($row)
{
	if($row["closed"]){
        return null; 
    }
    
    if($row["released"] == 1){ 
        if($row['closed_for_issues']){
            return pager_link(_('---'), "/manufacturing/work_order_issue.php?trans_no=" .$row["id"]);
        }
        else{
            return pager_link(_('Issue'), "/manufacturing/work_order_issue.php?trans_no=" .$row["id"]);
        }
    }
    else{
        return null;
    }
}

function produce_link($row)
{
    if($row["closed"]){
        return null; 
    }
    
    if(!$row["released"]){
        return null;
    }
    else{
        return pager_link(_('Produce'), "/manufacturing/work_order_add_finished.php?trans_no=" .$row["id"]);
    }

    
}



function booking_link($row)
{
	if($row["closed"]){
        return null; 
    }
    
	if(!$row["released"]){
        return null;
    }
    else{
        return pager_link(_('Booking'), "/manufacturing/work_order_production_booking.php?trans_no=" .$row["id"]);
    }
    
}


function costs_link($row)
{
/*
	return $row["closed"] || !$row["released"] ? '' :
		pager_link(_('Costs'),
			"/gl/gl_bank.php?NewPayment=1&PayType=" 
			.PT_WORKORDER. "&PayPerson=" .$row["id"]);
*/			
	return $row["closed"] || !$row["released"] ? '' :
		pager_link(_('Costs'),
			"/manufacturing/work_order_costs.php?trans_no=" .$row["id"]);
}

function view_gl_link($row)
{
	if ($row['closed'] == 0){
        return '';
    }
    else{
        return get_gl_view_str(ST_WORKORDER, $row['id']);
    }
}


function copy_link($row)
{
	return  pager_link(_("Copy"), "/manufacturing/copy_work_order.php?trans_no=" . $row["id"], null);
}



function dec_amount($row, $amount)
{
	return number_format2($amount, $row['decimals']);
}

$sql = get_sql_for_work_orders($outstanding_only, $all_items);

$cols = array(
	_("#") => array('fun'=>'view_link'), 
	_("Ref"),
	//_("Type") => array('fun'=>'wo_type_name'),
	_("Project"), 
	_("Item") => array('fun'=>'view_stock'),
	_("Reqd") => array('fun'=>'dec_amount', 'align'=>'right'),
	_("Manufd") => array('fun'=>'dec_amount', 'align'=>'right'),
	_("Start Date") => array('name'=>'date_', 'type'=>'date', 'ord'=>'desc'), 
	_("End Date") => array('type'=>'date', 'ord'=>''),
	_("Status") => array('insert'=>true, 'fun'=> 'status_link'),
    _("Edit") => array('insert'=>true, 'fun'=> 'edit_link'),
	_("Approve") => array('insert'=>true, 'fun'=> 'approve_link', 'align'=>'center'),
    _("Purchase") => array('insert'=>true, 'fun'=> 'purchase_link', 'align'=>'center'),
    _("Release") => array('insert'=>true, 'fun'=> 'release_link', 'align'=>'center'),
    _("Issue") => array('insert'=>true, 'fun'=> 'issue_link', 'align'=>'center'),
    _("Booking") => array('insert'=>true, 'fun'=> 'booking_link', 'align'=>'center'),
	_("Produce") => array('insert'=>true, 'fun'=> 'produce_link', 'align'=>'center'),
//	_("Costs") => array('insert'=>true, 'fun'=> 'costs_link'),
	_("GL") => array('insert'=>true, 'fun'=> 'view_gl_link'),
	_("Copy") => array('insert'=>true, 'fun'=> 'copy_link')
);

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

$table->width = "95%";

display_db_pager($table);

end_form();
end_page();
?>
