<?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 . "/sales/includes/db/pos_customer_payments_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();

global $systypes_array;

page(_($help_context = $systypes_array[ST_POS_CUSTOMER_PAYMENT]."s and ".$systypes_array[ST_POS_CUSTOMER_RETURN]."s Inquiry"), false, false, "", $js);

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

function _trans_no($row) { 
    return get_customer_trans_view_str($row['type'], $row['trans_no']);
}

function _type($row) {
    global $systypes_array;
    return $systypes_array[$row['type']];
}

function _reference($row) {
    return $row['reference'];
}

function _tran_date($row) {
    return sql2date($row['tran_date']);
}

function _float($row) {
    return $row['float_name'];
}

function _amount($row) {
    return $row['amount'];
}

function _approval_status($row) {
    global $document_approval_statuses;
    return $document_approval_statuses[$row['approval_status']];
}

function _source_type($row) {
    global $systypes_array;
    return $systypes_array[$row['source_type']];
}

function _source_trans_no($row) {
    return get_customer_trans_view_str($row['source_type'], $row['source_trans_no']);
}

function edit_link($row){
    if (in_array($row['approval_status'], array(AS_NEW, AS_WITH_THE_APPROVER, AS_AWAITING_APPROVAL, AS_BACK_TO_ORIGINATOR))) {
        if ($row['type'] == ST_POS_CUSTOMER_PAYMENT) {
            $name_value = "Modify_pos_cus_pay_id=".$row['id']; 
            return pager_link( _("Edit"), "/sales/pos_customer_payments.php?$name_value", ICON_EDIT);
        } elseif ($row['type'] == ST_POS_CUSTOMER_RETURN) {
            $name_value = "Modify_pos_cst_rtn_id=".$row['id']; 
            return pager_link( _("Edit"), "/sales/pos_customer_returns.php?$name_value", ICON_EDIT);
        } else {
            return null;
        }
        
    } else {
        return null;
    }
}

global $systypes_array, $document_approval_statuses;

start_form();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            ref_cells(text_bold("Trans No #:"), 'trans_no', '',null, '', true);
            $pos_cus_types_array = array(ST_POS_CUSTOMER_PAYMENT => $systypes_array[ST_POS_CUSTOMER_PAYMENT], 
                                         ST_POS_CUSTOMER_RETURN => $systypes_array[ST_POS_CUSTOMER_RETURN]);
            custom_values_list_cells("Tran Type:", 'tran_type', $pos_cus_types_array, null, '', true);
            ref_cells("Reference:", 'reference', '',null, '', true);
            date_cells("From Date:", 'from_date', '', null, -30);
            date_cells("To Date:", 'to_date', '', null, +30);
            petty_cash_floats_list_cells("Float:", 'float_id', null, '', true);
        end_row();
    end_table();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            $values_array[-1] = "";
            $values_array[AS_NEW] = $document_approval_statuses[AS_NEW];
            $values_array[AS_PROCESSED] = $document_approval_statuses[AS_PROCESSED];
            $values_array[AS_REOPENED] = $document_approval_statuses[AS_REOPENED];
            custom_values_list_cells("Status:", "trans_status", $values_array, null, false, true);
            
            $pos_types_array = array(ST_POS_INVOICE => $systypes_array[ST_POS_INVOICE]);
            custom_values_list_cells("Source Tran Type:", 'source_type', $pos_types_array, null, '', true);
            ref_cells(text_bold("Source Trans No #:"), 'source_trans_no', '',null, '', true);
            submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');
        end_row();    
    end_table();
    
$sql = get_sql_for_pos_customer_payments_inquiry($_POST['trans_no'], $_POST['tran_type'], $_POST['reference'], $_POST['from_date'], $_POST['to_date'], $_POST['float_id'], 
                                                 $_POST['trans_status'], $_POST['source_type'], $_POST['source_trans_no']);  
        
	$cols = array(  _("Trans #") => array('fun'=>'_trans_no', 'align'=>'center'), 
                        _("Tran Type")=> array('fun'=>'_type'),  
                        _("Reference") => array('fun'=>'_reference', 'align'=>'left'),  
                        _("Tran Date") => array('fun'=>'_tran_date', 'align'=>'center'),
                        _("Float") => array('fun'=>'_float'),  
                        _("Amount") => array('fun'=>'_amount', 'align'=>'right'),   
                        _("Status") => array('fun'=>'_approval_status', 'align'=>'center'), 
                        _("Source Type") => array('fun'=>'_source_type'), 
                        _("Source Trans #") => array('fun'=>'_source_trans_no', 'align'=>'center'));

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

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

    $table->width = "90%";

    display_db_pager($table);

end_form(2);
end_page();

?>