<?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/loans_inquiry_db.inc");
include_once($path_to_root . "/finance/includes/db/fixed_deposits_inquiry_db.inc");
include_once($path_to_root . "/finance/includes/db/company_loans_db.inc");
include_once($path_to_root . "/finance/includes/db/credit_facilities_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 = "Fix Deposit Inquiry"), false, false, "", $js);


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


function original_fixed_deposit_id($row)
{
    return get_finance_trans_view_str(ST_FIXED_DEPOSIT, $row['original_fixed_deposit_id']); 
}

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

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


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


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

function security_type($row)
{
    global $fixed_deposit_security_type;
    
    if($row['security_type_id'] > 0){
        return $fixed_deposit_security_type[$row['security_type_id']];
    }
    else{
        return null;
    }
}


function security_name($row)
{
    switch ($row['security_type_id']){
        case ST_COMPANY_LOANS:
            $cl = get_company_loan($row['security_id']);
            return $cl['description'];
        case ST_CREDIT_FACILITY:
            $cf = get_credit_facility($row['security_id']);
            return $cf['description'];
        default:
            break;
    }
}


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


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


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


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



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


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

function edit_link($row) 
{    
    if($row['matured'] == 1){
        return null;
    }else{
        $modify = "FDid";
        return pager_link( _("Edit"), "/finance/fixed_deposits.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_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();
    date_cells(_("From:"), 'from_date', '', null, -90);
    date_cells(_("To:"), 'to_date', '', null, +90);
//end_row();
//
////start_row();
////    value_list_cells("Bank", 'bank_id', 'BANKS', null, 'Select a Bank', true);
////    bank_branch_list_cells("Bank Branch:", 'branch_id', $_POST['bank_id'], null, 'Select a Bank Branch', true);
////end_row();
//
////start_table(TABLESTYLE_NOBORDER);
//start_row();

    bank_accounts_list_cells("Bank Account:", 'bank_account_id', null, true, 'Select a Bank Account');
//end_row();

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

end_row();

end_table(1);
//---------------------------------------------------------------------------------------------
//	Orders inquiry table
//
$sql = get_sql_for_fixed_deposits_inquiry(get_post('from_date'), get_post('to_date'), get_post('bank_account_id'));


	$cols = array(
		_("#")=> array('fun'=>'view_link', 'align' => 'center') , 
                _("Name")=> array('fun'=>'name') , 
                _("Description")=> array('fun'=>'description') , 
                _("Bank") => array('fun'=>'bank_name'), 
                _("Branch") => array('fun'=>'branch_name'), 
                _("Bank Account") => array('fun'=>'bank_account_name'),
                _("Security Type") => array('fun'=>'security_type'), 
                _("Security") => array('fun'=>'security_name'), 
                _("Amount") => array('fun'=>'amount', 'align'=>'right'),  
                _("Start Date") => array('fun'=>'start_date', 'align' => 'center'),
                _("End Date") => array('fun'=>'end_date', 'align' => 'center'), 
                _("Interest") => array('fun'=>'interest', 'align' => 'right'),
                _("Original FD") => array('fun'=>'original_fixed_deposit_id', 'align'=>'center')
         
		);
    
    array_append($cols, array(
        array('insert'=>true, 'fun'=>'edit_link'),
//        array('insert'=>true, 'fun'=>'installments_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();
}
?>
