<?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_invoice_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 = "POS Invoices and POS Credit Notes Inquiry"), false, false, "", $js);

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

if (isset($_GET['project_id']))
{
	$_POST['project_id'] = $_GET['project_id'];
}
if (isset($_GET['from_date']))
{
	$_POST['from_date'] = $_GET['from_date'];
}
if (isset($_GET['to_date']))
{
	$_POST['to_date'] = $_GET['to_date'];
}

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

function _entry_type_name($row) {
    return $row['entry_type_name'];   
}

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

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

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

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

function customer_name($row){
    return get_customer_formatted_name($row['debtor_no']);
}

function receiving_location($row){
    return $row['location_name'];
}

function pos_invoice_total($row){
    return $row['total'];
}

function _pos_invoice_paid_amount($row){
    return $row['paid_amount'];
}

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

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

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_INVOICE) {
            $name_value = "ModifyInvoice=".$row['id'];
            return pager_link( _("Edit"), "/sales/pos_invoice.php?$name_value", ICON_EDIT);
        } else {
            $name_value = "ModifyCreditNote=".$row['id'];
            return pager_link( _("Edit"), "/sales/pos_credit_note.php?$name_value", ICON_EDIT);
        }
    } else {
        return null;
    }
}

function edit_link_2($row) {
    global $systypes_array;
    
    if ($row['approval_status'] == AS_APPROVED) {
        if ($row['type'] == ST_POS_INVOICE) {
            $tp_name = $systypes_array[ST_POS_CUSTOMER_PAYMENT];
            $name_value = "inv_trans_no=".$row['trans_no'];
            
            return pager_link( _("$tp_name"), "/sales/pos_customer_payments.php?$name_value", ICON_DOC); 
        } elseif ($row['type'] == ST_POS_CREDIT_NOTE) {
            $tp_name = $systypes_array[ST_POS_CUSTOMER_RETURN];
            $name_value = "crd_trans_no=".$row['trans_no'];
            
            return pager_link( _("$tp_name"), "/sales/pos_customer_returns.php?$name_value", ICON_DOC); 
        } 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_types_array = array(ST_POS_INVOICE => $systypes_array[ST_POS_INVOICE], ST_POS_CREDIT_NOTE => $systypes_array[ST_POS_CREDIT_NOTE]);
            custom_values_list_cells("Tran Type:", 'tran_type', $pos_types_array, null, '', true);
            value_list_cells('Entry Type:', 'entry_type_id', 'UTILITY_BILL_ENTRY_TYPES', null, '', true);
            ref_cells("Reference:", 'reference', '',null, '', true);
            date_cells("From Date:", 'from_date', '', null, -30);
            date_cells("To Date:", 'to_date', '', null, +30);
        end_row();
    end_table();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            locations_list_cells(_("Location:"), 'loc_code', null, true, true);
            project_list_cells(_("Project:"), 'project_id', null, _('Select a project'), true);
            
            $values_array[-1] = "";
            $values_array[AS_NEW] = $document_approval_statuses[AS_NEW];
            $values_array[AS_APPROVED] = $document_approval_statuses[AS_APPROVED];
            $values_array[AS_PAID] = $document_approval_statuses[AS_PAID];
            custom_values_list_cells("Status:", "trans_status", $values_array, null, false, true);
            submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
        end_row();    
    end_table();
    
$sql = get_sql_for_pos_invoices_inquiry($_POST['trans_no'], $_POST['reference'], $_POST['project_id'], $_POST['from_date'], $_POST['to_date'], $_POST['loc_code'], 
                                        $_POST['trans_status'], $_POST['entry_type_id'], $_POST['tran_type']);  
        
	$cols = array(  _("Trans #") => array('fun'=>'_trans_no', 'align'=>'center'), 
                        _("Tran Type")=> array('fun'=>'_type'),  
                        _("Entry Type")=> array('fun'=>'_entry_type_name'),  
                        _("Ref") => array('fun'=>'reference', 'align'=>'left'),  
                        _("Date") => array('fun'=>'tran_date', 'align'=>'center'),
                        _("Customer") => array('fun'=>'customer_name'),  
                        _("Invoice Total") => array('fun'=>'pos_invoice_total', 'align'=>'right'),   
                        _("Paid Amount") => array('fun'=>'_pos_invoice_paid_amount', 'align'=>'right'),   
                        _("Status") => array('fun'=>'approval_status', 'align'=>'center'));

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

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

    $table->width = "98%";

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


end_form(2);
end_page();

?>