<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_ATTENDANCE_VIEW';
$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 . "/includes/ui/ui_input.inc");
include_once($path_to_root . "/includes/ui/ui_lists.inc");
include_once($path_to_root . "/includes/ui/ui_lists_custom.inc");
include_once($path_to_root . "/payroll/includes/db/payroll_inquiry_db.inc");
include_once($path_to_root . "/payroll/includes/attendance_calculation.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 = "Attendance Inquiry"), false, false, "", $js);
}
if (isset($_GET['order_number']))
{
	$order_number = $_GET['order_number'];
}

//-----------------------------------------------------------------------------------
// Ajax updates
//
if (get_post('SearchOrders')) 
{
	$Ajax->activate('orders_tbl');
} elseif (get_post('_order_number_changed')) 
{
	$disable = get_post('order_number') !== '';

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

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

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

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

start_table(TABLESTYLE_NOBORDER);
start_row();

date_cells(_("from:"), 'from_date', '', null, -30);
date_cells(_("to:"), 'to_date');

emoplyees_list_cells(_(" Employee:"), 'employee_code', null,
	  "", false, check_value('show_inactive'));
yesno_list_cells("OT Only", "ot_only", 0, "Yes", "No");

value_list_code_cells("Status Code:", "status_code", "ATTENDANCE_STATUS", false, "");

submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');

end_row();
end_table(1);
//---------------------------------------------------------------------------------------------
if (isset($_POST['order_number']))
{
	$order_number = $_POST['order_number'];
}

if (isset($_POST['SelectStockFromList']) &&	($_POST['SelectStockFromList'] != "") &&
	($_POST['SelectStockFromList'] != ALL_TEXT))
{
 	$selected_stock_item = $_POST['SelectStockFromList'];
}
else
{
	unset($selected_stock_item);
}

//---------------------------------------------------------------------------------------------
function trans_view($row)
{
	$employee_code = $row["employee_code"];;
        $date_s = strtotime($row["date"]);
        $att_id = $row['id'];

        $link = $date_s."~".$att_id."~".$employee_code;


        return get_trans_view_str(ST_ATTENDANCE, $link, $row['date']);

}


function split_shift($row)
{
    if($row['split_shift'] == 1){
        return "Yes";
    }
    else{
        return "No";
    }
  
}

function hide_blanks1($row)
{
    if($row['in2'] == "00:00"){
        return "";
    }
    else{
        return $row['in2'];
    }
}

function hide_blanks2($row)
{
    if($row['out2'] == "00:00"){
        return "";
    }
    else{
        return $row['out2'];
    }
}

function _rate_table($row)
{

        return $row['table_name'];
    
}

function _status($row)
{

        return $row['status_name'];
    
}

function hide_blanks_ot($row)
{
    if($row['ot_time'] == "00:00"){
        return "";
    }
    else{
        return $row['ot_time'];
    }
}

function duration1_hm($row)
{    
    return get_duration($row['counted_duration1']); 
}

function duration2_hm($row)
{    
    $duration = get_duration($row['counted_duration2']); 
    if($duration == "00:00"){
        return "";
    }
    else{
        return $duration;
    }
}


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

$from_date = "";
if(isset($_POST['from_date'])){
    $from_date = $_POST['from_date'];
}
$to_date = "";
if(isset($_POST['to_date'])){
    $to_date = $_POST['to_date'];
}
$employee_code = "";
if(isset($_POST['employee_code'])){
    $employee_code = $_POST['employee_code'];
}
$ot_only = 0;
if(isset($_POST['ot_only'])){
    $ot_only = $_POST['ot_only'];
}

$status_code = 0;
if(isset($_POST['status_code'])){
    $status_code = $_POST['status_code'];
}

$sql = get_sql_for_attendance_inquiry($from_date, $to_date, $employee_code, $ot_only, $status_code);

$cols = array(
		_("Date") => array('fun'=>'trans_view'), 
		_("Employee"), 
		_("Shift") => array('align'=>'center'),
		_("In1") => array('align'=>'center'), 
                _("Out1") => array('align'=>'center'),
		_("Duration1") => array('fun' => 'duration1_hm', 'align'=>'center'), 
//                _("Split Shift") => array('fun'=>'split_shift'), 
//                _("In2") => array('fun'=>'hide_blanks1', 'align'=>'center'),
//                _("Out2") => array('fun'=>'hide_blanks2', 'align'=>'center'),
//		_("Duration2") => array('fun' => 'duration2_hm', 'align'=>'center'), 
                _("OT Time") => array('fun' => 'hide_blanks_ot', 'align'=>'center'), 
                _("Rate Table") => array('fun' => '_rate_table', 'align'=>'center'), 
		_("Status") => array('fun' => '_status', 'align'=>'center'),

);

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

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

$table->width = "80%";

display_db_pager($table);

br();

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