<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_BONDS';
$path_to_root = "..";
include($path_to_root . "/includes/session.inc");

$js = "";
if (user_use_date_picker())
	$js .= get_js_date_picker();
page(_($help_context = "Bonds"), false, false, "", $js);

include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/finance/includes/ui/bonds_ui.inc");
include_once($path_to_root . "/finance/includes/db/bonds_db.inc");
include_once($path_to_root . "/finance/includes/db/credit_facilities_db.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/session.inc");

include_once($path_to_root . "/gl/includes/db/gl_db_bank_accounts.inc");



simple_page_mode(true);

//----------------------------------------------------------------------------------------
$bond_id = -1;

if (isset($_GET['id']))
{
	$_POST['bond_id'] = $_GET['id'];
}
if (isset($_POST['bond_id']) && $_POST['bond_id'] > 0)
{
	$bond_id = $_POST['bond_id'];
}
if ($bond_id > 0)
{
	$selected_id = $bond_id;
}


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

if (isset($_GET['AddedID'])) 
{
	//return;
        
    $trans_no = $_GET['AddedID'];
//	$trans_type = ST_MATERIAL_REQ;

    $msg = get_session_variable('bond_messages');
    clear_session_variable('bond_messages');
        
	display_notification($msg);
	
//    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this Material Requisition")));
//    
//    hyperlink_params("$path_to_root/inventory/material_requisitions.php", _("Edit this Material Requisition"), "ModifyMRNumber=$trans_no");
//        
////    br();
//
////	display_note(print_document_link($trans_no, _("&Print this Material Requisition"), true, $trans_type), 0, 0);
//
//	hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &another Material Requisition"));
//
////	hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
//        
//    clear_session_variable('mr_messages');

	display_footer_exit();
}



if ($Mode=='ADD_ITEM' && check_bond_data($selected_id))
{
//    if(get_post('payable') == 1){
//        $customer_id = get_post('customer_id');
//        if($_POST['credit_facility_id'] > 0){
//            $credit_facility_id = $_POST['credit_facility_id'];
//        }
//        else{
//            $credit_facility_id = 0; 
//        }
//        
//        $bank_account_id = get_post('bank_account_id');
//        $bank_account = get_bank_account($bank_account_id);
//        $bank_id = $bank_account['bank_id'];
//        $branch_id = $bank_account['branch_id'];
//        
//        $supplier_id = 0;
//    }
//    else{
//        $credit_facility_id = 0;
//        $bank_account_id = 0;
//        $bank_id = $_POST['bank_id'];
//        $branch_id = $_POST['branch_id'];
//        
//        $customer_id = 0;
//        $supplier_id = get_post('supplier_id');
//    }
//
//    $ab_msg = add_bond($_POST['bond_name'], $_POST['bond_description'], $credit_facility_id, 
//                $_POST['bond_type'], $_POST['payable'], $customer_id, $supplier_id, 
//                $bank_id, $branch_id, $bank_account_id, 
//                input_num('bond_value'), input_num('bank_charge'), 
//                $_POST['start_date'], $_POST['expiry_date'], 
//                $_POST['project_id'], $_POST['comment'], $_POST['expired']);          
//
//    if($ab_msg['success'] !== true){
//        display_error($ab_msg['message']);
//    }
//    else{
//        $selected_id = -1;
//        $Mode = 'RESET';
//
//        set_session_variable('bond_messages', $ab_msg['message']);
//        meta_forward($_SERVER['PHP_SELF'], "AddedID=".$ab_msg['trans_no']);
//    }
    
    handle_new_bond($Mode, $selected_id);
}
    
if ($Mode=='UPDATE_ITEM' && check_bond_data($selected_id))
{
//    if(get_post('payable') == 1){
//        $customer_id = get_post('customer_id');
//        $supplier_id = 0;
//    }
//    else{
//        $customer_id = 0;
//        $supplier_id = get_post('supplier_id');
//    }
//    $ub_msg = update_bond($selected_id, $_POST['bond_name'], $_POST['bond_description'],                    
//                $_POST['start_date'], $_POST['expiry_date'], 
//                $customer_id, $supplier_id, $_POST['project_id'],
//                $_POST['comment'], $_POST['expired']);          
//
//    if($ub_msg['success'] !== true){
//        display_error($ub_msg['message']);
//    }
//    else{
//        $selected_id = -1;
//        $Mode = 'RESET';
//
//        set_session_variable('bond_messages', $ub_msg['message']);
//        meta_forward($_SERVER['PHP_SELF'], "AddedID=".$ub_msg['trans_no']);
//    }

    handle_update_bond($Mode, $selected_id);
}

if(isset($_POST['EXTEND_BOND']) && can_extend_bond($selected_id))
{
//    $eb_msg = extend_bond($selected_id, $_POST['extend_bond_name'], $_POST['extend_bond_description'], 
//                input_num('extend_bond_value'), input_num('extend_bank_charge'),
//                $_POST['extend_start_date'], $_POST['extend_expiry_date'], $_POST['extend_comment']);  
//    
//    if($eb_msg['success'] !== true){
//        display_error($eb_msg['message']);
//    }
//    else{
//        $selected_id = -1;
//        $Mode = 'RESET';
//
//        set_session_variable('bond_messages', $eb_msg['message']);
//        meta_forward($_SERVER['PHP_SELF'], "AddedID=".$eb_msg['trans_no']);
//    }
    
    handle_extend_bond($Mode, $selected_id);
}
//--------------------------------------------------------------------------------------------------


if ($Mode == 'RESET')
{
	$selected_id = -1;
        $bond_id =  -1;
        $_POST['bond_id'] = -1;
}


//--------------------------------------------------------------------------------------------------

global $Ajax;

start_form();

//start_table(TABLESTYLE2);
//company_loans_list_row("Company Loan :", 'compay_loan_id');
//end_table();

    
if (isset($_POST['_selected_id_update']) )
{
    $selected_id = $_POST['selected_id'];
    $Ajax->activate('_page_body');
}

if (list_updated('credit_facility_id')){
    $Ajax->activate('price_table');
}
    
$Ajax->activate('bond_edit_controls');

//display_bonds();

display_bond_edit_controls($selected_id);

br(2);

if($bond_id > 0){
    display_bond_extend_edit_controls();
}

br(2);

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


?>
