<?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/company_loans_inquiry_db.inc");


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


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

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

function display_ba_name($row)
{
    return $row["bank_account_number"];
}


function _format_loan_capital($row)
{
    return number_format($row['loan_amount'],2);
}

function _format_loan_interest($row)
{
    return number_format($row['interest'],2);
}

function format_installment($row)
{
    return number_format($row['installment_amount'],2);
}


function format_capital_paid($row)
{
    return number_format($row['total_captal_paid'],2);
}


function format_interest_paid($row)
{
    return number_format($row['total_interest_paid'],2);
}


function format_installment_interest($row)
{
    return number_format($row['installment_interest'],2);
}

function display_guarantor_name($row)
{
    if($row["payable"]){
        return $row["customer_name"];
    }
    else{
        return $row["supplier_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_last_paid_date($row)
{
    if(substr($row['last_payment_date'], 0, 4) == "0000"){
        return null;
    }
    
    return sql2date($row['last_payment_date']);
}


function display_on_hold_name($row)
{
    return get_yes_no_name($row['inactive']);
}


function display_original_loan_id($row)
{
    if($row['original_loan_id'] > 0){
        return get_finance_trans_view_str(ST_COMPANY_LOAN_INSTALLMENTS, $row['original_loan_id']);
    }else{
        return "";
    }
   
}


function display_completed_name($row)
{
    return get_yes_no_name($row['completed']);
}

function edit_link($row) 
{

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


function installments_link($row) 
{

    if($row['completed'] == 1 || $row['inactive'] == 1){
        return null;
    }
    
    $modify = "id";
    return pager_link( _("Installments"), "/finance/company_loan_installments.php?$modify=" . $row['id'], ICON_EDIT);
    
}

function repayments_link($row) 
{

    if($row['completed'] == 1 || $row['inactive'] == 1){
        return null;
    }
    
    $modify = "id";
    return pager_link( _("Repayments"), "/finance/company_loan_repayments.php?$modify=" . $row['id'], ICON_DOC);
    
}

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



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


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

start_table(TABLESTYLE_NOBORDER);
start_row();

text_cells(_("Loan Id:"), 'loan_id', '',8, '', false);
ref_cells(_("Loan Reference:"), 'loan_name', '',null, '', false);
date_cells(_("From:"), 'from_date', '', null, 0, 0, -2);
date_cells(_("To:"), 'to_date');
value_list_cells("Bank", 'bank_id', 'BANKS', null, '');


submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');


end_row();

end_table(1);
//---------------------------------------------------------------------------------------------
//	Orders inquiry table

$loan_id = "";
if(isset($_POST['loan_id'])){
    $loan_id = $_POST['loan_id'];
}

$loan_name = "";
if(isset($_POST['loan_name'])){
    $loan_name = $_POST['loan_name'];
}

$from_date = "";
if(isset($_POST['from_date'])){
    $from_date = $_POST['from_date'];
}

$to_date = "";
if(isset($_POST['to_date'])){
    $to_date = $_POST['to_date'];
}

$bank_id = "";
if(isset($_POST['bank_id'])){
    $bank_id = $_POST['bank_id'];
}


$sql = get_sql_for_company_loans_inquiry($loan_id, $loan_name,
                                        $from_date, $to_date, $bank_id);
   
	$cols = array(
		_("#")=> array('fun'=>'view_link', 'align' => 'center') , 
		_("Reference"),
                _("Description"), 
                _("Type"), 
                _("Loan Capital") => array('fun'=>'_format_loan_capital', 'align' => 'right'), 
                _("Loan Interest") => array('fun'=>'_format_loan_interest', 'align' => 'right'), 
                _("Instl. Capital") => array('fun'=>'format_installment', 'align' => 'right'), 
                _("Instl. Interest") => array('fun'=>'format_installment_interest', 'align' => 'right'),
                _("CF / Bank") => array('fun'=>'display_cf_bank'),  
                _("Bank Account") => array('fun'=>'display_ba_name'),
                _("Start Date") => array('fun'=>'convert_start_date', 'align' => 'center'),
//                _("Capital Paid") => array('fun'=>'format_capital_paid', 'align' => 'right'), 
//                _("Interest Paid") => array('fun'=>'format_interest_paid', 'align' => 'right'), 
//                _("Interest") => array('fun'=>'format_interest', 'align' => 'right'),    

                _("Last Paid On") => array('fun'=>'convert_last_paid_date', 'align' => 'center'), 
                _("Ori. Loan ID") => array('fun'=>'display_original_loan_id', 'align' => 'center'),     
                _("On Hold") => array('fun'=>'display_on_hold_name', 'align' => 'center'),                   
                _("Completed") => array('fun'=>'display_completed_name', 'align' => 'center'), 
		);
    
    array_append($cols, array(
        array('insert'=>true, 'fun'=>'edit_link'),
        array('insert'=>true, 'fun'=>'installments_link'),
        array('insert'=>true, 'fun'=>'repayments_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();
}


?>
