<?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 = "Customer Invoices 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 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 tran_date($row){
    return sql2date($row['tran_date']);
}

function approval_status($row){
    global $approval_statuses;
    return $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'];
        } else {
            return null;
        }
        
        return pager_link( _("Edit"), "/sales/pos_invoice.php?$name_value", ICON_EDIT);
    } else {
        return null;
    }
}

function edit_link_2($row){
    if ($row['approval_status'] == AS_APPROVED) {
        if ($row['type'] == ST_POS_INVOICE) {
            $name_value = "dii_trans_no=".$row['trans_no'];
            
            return pager_link( _("Edit"), "/sales/pos_invoice.php?$name_value", ICON_CREDIT);
        } else {
            return null;
        }
        
    } else {
        return null;
    }
}

global $systypes_array, $approval_statuses;

start_form();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            ref_cells(text_bold("Trans No #:"), 'trans_no', '',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] = "ALL";
            $values_array[AS_NEW] = $approval_statuses[AS_NEW];
            $values_array[AS_APPROVED] = $approval_statuses[AS_APPROVED];
            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']);  
        
	$cols = array(  _("Trans #") => array('fun'=>'_trans_no', 'align'=>'center'), 
                        _("Ref") => array('fun'=>'reference', 'align'=>'left'),  
                        _("Date") => array('fun'=>'tran_date', 'align'=>'center'),
                        _("Customer") => array('fun'=>'customer_name'),  
                        _("Amount") => array('fun'=>'pos_invoice_total', 'align'=>'right'),   
                        _("Status") => array('fun'=>'approval_status', 'align'=>'center'));
//array(3) (
//  [type] => (string) total
//  [head] => (string) Amount
//  [name] => (string) debtor_no
//)
	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 = "96%";

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


end_form(3);
end_page();

?>