<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_VEHICLE_REQUEST_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/sales_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/finance/includes/db/bonds_inquiry_db.inc");


//include_once($path_to_root . "/includes/ui/ui_view.inc");
$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();
page(_($help_context = "Bond Inquiry"), false, false, "", $js);

function view_link($row)
{
	return get_finance_trans_view_str(ST_BONDS, $row['id']);
}


function display_payable_name($row)
{
    return get_yes_no_name($row["payable"]);
}


function display_expired_name($row)
{
    return get_yes_no_name($row["expired"]);
}

function display_guarantor_name($row)
{
    if($row["payable"]){
        return $row["customer_name"];
    }
    else{
        return $row["supplier_name"];
    }
}

function display_cf_bank($row)
{
    if($row["credit_facility_id"] > 0){
        return $row["credit_facility_name"];
    }
    else{
        return $row["bank_name"];
    }
}


function approvel_status_link($row)
{
    if($row['closed'] == 1){
        return "Closed";
    }
    else{
        global $document_approval_statuses;
        return $document_approval_statuses[$row['approval_status']];
    }

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


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

function convert_to_date($row)
{
  return substr($row['to_date_time'], 0, 16);
}

function edit_link($row) 
{

    if($row['expired'] == 1){
        return null;
    }
    
    $modify = "id";
    return pager_link( _("Edit"), "/finance/bonds.php?$modify=" . $row['id'], ICON_EDIT);
    
}

function display_original_bond_id($row)
{
    if($row['original_bond_id'] > 0){
        return get_finance_trans_view_str(ST_BONDS, $row['original_bond_id']);
    }
    else{
        return "";
    }
}

function format_value($row)
{

    return number_format($row['value'],2);

}

if (get_post('SearchOrders')) 
{
	$Ajax->activate('orders_tbl');
}


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

start_table(TABLESTYLE_NOBORDER);
start_row();

text_cells(_("Bond Id:"), 'bond_id', '',8, '', false);
ref_cells(_("Bond Name:"), 'bond_name', '',null, '', false);
date_cells(_("From:"), 'from_date', '', null, -90);
date_cells(_("To:"), 'to_date', '', null, +90);

end_row();

start_table(TABLESTYLE_NOBORDER);
start_row();
project_list_cells("Project :", 'project_id', null, '');
value_list_cells("Bank", 'bank_id', 'BANKS', null, '');

submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
//hidden('order_view_mode', $_POST['order_view_mode']);
//hidden('type', $trans_type);

end_row();

end_table(1);
//---------------------------------------------------------------------------------------------
//	Orders inquiry table
//
$sql = get_sql_for_bond_inquiry(input_num('bond_id'),get_post('bond_name'),
                                        get_post('from_date'),get_post('to_date'),
                                        get_post('project_id'),get_post('bank_id'));


	$cols = array(
		_("#")=> array('fun'=>'view_link', 'align' => 'center') , 
		_("Name"), 
        _("Description"), 
        _("Project"),
        _("Type"),
        _("Value") => array('fun'=>'format_value', 'align' => 'right'), 
        _("Start Date") => array('fun'=>'convert_start_date', 'align' => 'center'), 
        _("Expiry Date") => array('fun'=>'convert_expiry_date', 'align' => 'center'), 
        _("Payable") => array('fun'=>'display_payable_name', 'align' => 'center'),
        _("Guarantor / Beneficiary") => array('fun'=>'display_guarantor_name'),            
        _("CF / Bank") => array('fun'=>'display_cf_bank', 'align' => 'center'), 
        _("Expired") => array('fun'=>'display_expired_name', 'align' => 'center'), 
        _("Ori. Bond Id") => array('fun'=>'display_original_bond_id', 'align' => 'center'), 
		);
    
    array_append($cols, array(
        array('insert'=>true, 'fun'=>'edit_link'), 
		));


$table =& new_db_pager('orders_tbl', $sql, $cols);
//$table->set_marker('check_overdue', _("Marked items are overdue."));

$table->width = "95%";

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

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