<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_STOCK_COUNT_INQ';
$path_to_root = "../..";
include($path_to_root . "/includes/db_pager.inc");
include($path_to_root . "/includes/session.inc");
include($path_to_root . "/includes/ui/ui_lists.inc");
include($path_to_root . "/includes/ui/ui_input.inc");

//include($path_to_root . "/purchasing/includes/purchasing_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/inventory/includes/db/stock_count_db.inc");
include_once($path_to_root . "/inventory/includes/db/stock_count_inquiry_db.inc");

        
$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 = "Search Stock Count List"), false, false, "", $js);


if (get_post('SearchOrders')) 
{
	$Ajax->activate('orders_tbl');
} elseif (get_post('_job_id_changed')) 
{
	$disable = get_post('job_id') !== '';

	$Ajax->addDisable(true, 'OrdersAfterDate', $disable);
	$Ajax->addDisable(true, 'OrdersToDate', $disable);
	$Ajax->addDisable(true, 'sales_type', $disable);
	$Ajax->addDisable(true, '_SelectStockFromList_edit', $disable);
	$Ajax->addDisable(true, 'SelectStockFromList', $disable);

	if ($disable) {
		$Ajax->addFocus(true, 'job_id');
	} else
		$Ajax->addFocus(true, 'OrdersAfterDate');

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


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

start_form();

start_table(TABLESTYLE_NOBORDER);
start_row();

locations_list_cells(_("Location:"), 'Location', null, true, true);
date_cells(_("from:"), 'OrdersAfterDate', '', null, -60);
date_cells(_("to:"), 'OrdersToDate');
submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');

end_row();
end_table();

start_table(TABLESTYLE_NOBORDER);

end_table(1);
//---------------------------------------------------------------------------------------------
function view_link($row)
{
	return get_trans_view_str(ST_STOCK_COUNT, $row["stock_count_no"]);
}

function count_link($row) 
{  
    
    if($row['status_id'] == SC_COUNTING){  
        return pager_link( _("Count"), "/inventory/enter_counted_data.php?stock_count_no= ". $row['stock_count_no']); 
    }
    else{
        return null;
    }

}

function complete_link($row)
{
 
    if($row['status_id'] == SC_COUNTED || $row['status_id'] == SC_COMPLETING){
            return pager_link( _("Complete"), "/inventory/complete_stock_count.php?CompleteStockCountNo=" . $row["stock_count_no"]);
    }
    else{
        return "&nbsp&nbsp";
    }
    
}

function edit_link($row) 
{
    if($row['status_id'] == SC_NEW){
        return pager_link( _("Edit"), "/inventory/stock_count.php?ModifyStockCountNo=" . $row["stock_count_no"]);
    }
    else{
        return null;
    }
}

function process_link($row) 
{    
    if($row['status_id'] == SC_COMPLETED || $row['status_id'] == SC_PROCESSING){
        return pager_link( _("Process"), "/inventory/process_stock_count.php?StockCountNo=" . $row["stock_count_no"]);
    }
    else{
        return "&nbsp&nbsp";
    }
}

function show_status_name($row) 
{
    global $stock_counting_statuses;
    return array_selector(null, $row['status_id'], $stock_counting_statuses, 
		array( 
			'select_submit'=> false, 
			'disabled' => true )
		 );
}

function show_location($row){
    return $row['loc_code'] . ' - '. $row['location_name'];
}

$_SESSION['location'] = $_POST['Location'];

$sql = get_sql_for_stock_count_search();

$result = db_query($sql,"No orders were returned");

/*show a$myrow table of the orders returned by the sql */

    $cols = array(
                _("Stock Count No")=> array('align'=>'center', 'ord'=>'', 'fun'=>'view_link' ),
                _("Reference") , 
                _("Location")=> array('fun'=>'show_location', 'align'=>'left'), 
                _("Date") => array('type' =>  'date', 'ord'=>'' ),
                _("Status")  => array('fun'=>'show_status_name'),
                _("Edit")  => array('insert'=>true, 'fun'=>'edit_link'),
                _("Count")  => array('insert'=>true, 'fun'=>'count_link'),
                _("Process")  => array('insert'=>true, 'fun'=>'process_link')

);


if (get_post('StockLocation') != $all_items) {
	$cols[_("Location")] = 'skip';
}

$table =& new_db_pager('orders_tbl', $sql, $cols);

$table->width = "60%";

display_db_pager($table);

end_form();
end_page();
?>
