<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_SALES_ORDER_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 . "/payroll/includes/db/salary_advances_db.inc"); 
include_once($path_to_root . "/payroll/includes/db/employees_db.inc"); 
include_once($path_to_root . "/sales/includes/sales_ui.inc");

$js = "";
$js .= get_js_open_window(800, 500);
$js .= get_js_date_picker();

page(_($help_context = $type_name = $systypes_array[ST_SALARY_ADVANCE]."s Inquiry"), false, false, "", $js);

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

function _trans_no($row){ 
    return get_payroll_trans_view_str(ST_SALARY_ADVANCE, $row['id']);
}

function _type($row) {
    return $row['type_name'];   
}

function _employee($row){
    return get_employee_formatted_long_name($row['employee_code']);
}

function _due_date($row){
    return sql2date($row['due_date']);
}

function _salary_advance($row){
    return $row['salary_advance'];
}

function _status($row){
    global $payroll_approval_statuses;
    return $payroll_approval_statuses[$row['status_code']];
}

function _comment($row){
    return $row['comments'];
}

function _edit_link($row){
    if (in_array($row['status_code'], array('A'))) {
        $name_value = "sad_trans_no=".$row['id'];
        return pager_link( _("Pay"), "/payroll/salary_advance_payments.php?$name_value", ICON_DOC); 
    } elseif (in_array($row['status_code'], array('N', 'U'))) {
        $name_value = "employee_code=".$row['employee_code']."&selected_id=".$row['id'];
        return pager_link( _("Edit"), "/payroll/salary_advances.php?$name_value", ICON_EDIT); 
    } else {
        return null;
    }
}

global $payroll_approval_statuses;

start_form();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            ref_cells(text_bold("Trans No #:"), 'trans_no', '',null, '', true);
            value_list_cells("Type:", "type", "SALARY_ADVANCES", null, '', true);
            label_cells(_("Employee:"), emoplyees_list('employee_code', null, '', true));
            date_cells("Due Date:", 'from_date', '', null, -30);
            date_cells("Due Date:", 'to_date', '', null, +0);
            custom_values_list_cells("Status:", "trans_status", $payroll_approval_statuses, null, '', true);
            submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
        end_row();
    end_table(1);
    
$sql = get_sql_for_salary_advances_inquiry($_POST['type'], $_POST['trans_no'], $_POST['employee_code'], $_POST['from_date'], $_POST['to_date'], $_POST['trans_status']);  
        
	$cols = array(  _("Trans #") => array('fun'=>'_trans_no', 'align'=>'center'), 
                        _("Type")=> array('fun'=>'_type'),  
                        _("Employee") => array('fun'=>'_employee', 'align'=>'left'),  
                        _("Due Date") => array('fun'=>'_due_date', 'align'=>'center'),
                        _("Salary Advance") => array('fun'=>'_salary_advance', 'align'=>'right'),   
                        _("Status") => array('fun'=>'_status', 'align'=>'center'),   
                        _("Comment") => array('fun'=>'_comment', 'align'=>'center'));

        array_append($cols, array(
            array('insert'=>true, 'fun'=>'_edit_link', 'align'=>'center')));

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

    $table->width = "75%";

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


end_form(2);
end_page();

?>