<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_FIXED_DEPOSITS';
$path_to_root = "..";
include($path_to_root . "/includes/session.inc");

$js = "";
if (user_use_date_picker())
	$js .= get_js_date_picker();
page(_($help_context = "Fixed Deposits"), 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/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");
include_once($path_to_root . "/finance/includes/db/fixed_deposits_db.inc");


simple_page_mode(true);

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

$fixed_deposit_id = -1;

if (isset($_GET['FDid']))
{
    $_POST['FDid'] = $_GET['FDid'];
}
if (isset($_POST['FDid']) && $_POST['FDid'] > 0)
{
    $fixed_deposit_id = $_POST['FDid'];
}


//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();
}


function check_data($selected_id)
{           
    if(!(strlen(get_post('name')) > 0)){
        display_error( _("Name can not be empty."));
        set_focus('name');
        return false;
    }

    if(!(get_post('bank_id') > 0)){
        display_error( _("Please select a Bank."));
        set_focus('bank_id');
        return false;
    }
    
    if(!(get_post('branch_id') > 0)){
        display_error( _("Please select a Bank Branch."));
        set_focus('branch_id');
        return false;
    }

    if (!check_num('amount', 0001))
    {
        display_error(_("Amount entered has to be numeric and possitive."));
        set_focus('amount');
        return false;
    } 
    
    if(!is_date($_POST['start_date']))
    {
        display_error(_("Fix Deposit Start date is not a valid date"));
        set_focus('start_date');
        return false;
    }
    
    if(!is_date($_POST['end_date']))
    {
        display_error(_("Fix Deposit end date is not a valid date"));
        set_focus('end_date');
        return false;
    }
    
    if(date1_greater_date2($_POST['start_date'], $_POST['end_date']))
    {
        display_error("Fix deposit start date has to be greater than end date.");
        set_focus("end_date");
        return false;
        
    }
         
    return true;    
}


function can_mature_fixed_deposit($selected_id)
{
    $fixed_deposit = get_fixed_deposit($selected_id);
    if($fixed_deposit['matured'] == 1){
        display_error( _("The original fix deposit is already matured. It can not be extended."));
        set_focus('loan_amount');
        return false;
    }
        
    if(!(get_post('extend_bank_id') > 0)){
        display_error( _("Please select a Bank."));
        set_focus('extend_bank_id');
        return false;
    }

    if(!(get_post('extend_branch_id') > 0)){
        display_error( _("Please select a Bank Branch."));
        set_focus('extend_branch_id');
        return false;
    }

    if (!check_num('extend_amount', 0001))
    {
        display_error(_("Bank charge entered is not numeric or positive."));
        set_focus('extend_amount');
        return false;
    } 
    
    if(!is_date($_POST['extend_start_date']))
    {
        display_error(_("Fix Deposit Start date is not a valid date"));
        set_focus('extend_start_date');
        return false;
    }
    
    if(!is_date($_POST['extend_end_date']))
    {
        display_error(_("Fix Deposit end date is not a valid date"));
        set_focus('extend_end_date');
        return false;
    }
    
    if(date1_greater_date2($_POST['extend_start_date'], $_POST['extend_end_date']))
    {
        display_error("Fix deposit start date cannot be greater than end date.");
        set_focus("end_date");
        return false;        
    }
    
    $matured_FD = get_matured_fixed_deposit($selected_id);
    $original_fixed_deposit_id = $matured_FD['original_fixed_deposit_id'];
    
    if($original_fixed_deposit_id > 0){
        display_error("This fix deposit is already extended to fix deposit". $matured_FD['id']);
        return false;
    }
         
    return true;    
}
  

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


function display_fixed_deposit_edit_controls($selected_id)
{   
    if($selected_id > 0)
    {
        $myrow = get_fixed_deposit($selected_id);
        
        $_POST['id'] = $myrow["id"];
        $_POST['name'] = $myrow["name"];
        $_POST['description'] = $myrow["description"];
        $_POST['bank_id'] = $myrow["bank_id"];
        $_POST['branch_id'] = $myrow["branch_id"];
        $_POST['bank_account_id'] = $myrow["bank_account_id"];
        $_POST['amount'] = $myrow["amount"];
        $_POST['start_date'] = sql2date($myrow["start_date"]);
        $_POST['end_date'] = sql2date($myrow["end_date"]);
        $_POST['interest'] = $myrow["interest"]; 
        
        if(isset($_POST['security_type_id'])){
            if($_POST['security_type_id'] > 0){
                
            }
            else{
                $_POST['security_type_id'] = 0;
            }            
        }else{
            $_POST['security_type_id'] = $myrow["security_type_id"];   
        }
        
        
        if(isset($_POST['extend_security_type_id'])){
            if($_POST['extend_security_type_id'] > 0){
                $_POST['extend_security_type_id'] = $_POST['extend_security_type_id'];
            }
            else{
                $_POST['extend_security_type_id'] = 0;
            }                
        }
        else{
            $_POST['extend_security_type_id'] = $myrow["security_type_id"];   
        }        
                                
        $_POST['security_id'] = $myrow["security_id"];
        $_POST['lien_amount'] = $myrow["lien_amount"];
        
    }
    
    div_start('bond_edit_controls');

    start_table(TABLESTYLE2);
    start_row();   
   

    text_row(_("Reference :"), 'name', null, 25, 50);   
    text_row("Description :", "description", null, 50,200);
    value_list_row(_("Bank Name :"), 'bank_id', "BANKS", null, 'Select a Bank', true);     
    bank_branch_list_row("Bank Branch :", 'branch_id', $_POST['bank_id'], null, 'Select a Bank Branch', false);
    bank_accounts_list_row("Bank Account :", 'bank_account_id');
    amount_row("Amount :", "amount");                  
    date_row("Start Date :", 'start_date');
    date_row("End Date :", 'end_date');
    amount_row("Interest :", 'interest');
    
   
    fixed_deposit_security_type_list_row("Security type :", 'security_type_id', null, '', true);
      
    if($_POST['security_type_id'] == ST_COMPANY_LOANS){
        company_loans_list_row("Company Loan :", "security_id");
    }
    elseif($_POST['security_type_id'] == ST_CREDIT_FACILITY){
        credit_facilities_list_row("Credit Facility :", 'security_id');
    }
    else{
        
    }
    
    amount_row("Lien Amount :", "lien_amount");
    
    yesno_list_row("Matured:", 'matured', null, 'Yes', 'No', false);
    
    end_table(1);

    hidden('selected_id', $selected_id); 

    submit_add_or_update_center($selected_id == -1, '', 'both');

}


function display_fixed_deposit_extend_controls()
{
    start_table(TABLESTYLE2);
    
    text_row(_("Name :"), 'extend_name', get_post('name'), 25, 50);   
    text_row("Description :", "extend_description", get_post('description'), 50,200);
    value_list_row(_("Bank Name :"), 'extend_bank_id', "BANKS", get_post('bank_id'), 'Select a Bank', true);  
    bank_branch_list_row("Bank Branch :", 'extend_branch_id', $_POST['extend_bank_id'], get_post('branch_id'), 'Select a Bank Branch', false);
    bank_accounts_list_row("Bank Account :", 'extend_bank_account_id', get_post('bank_account_id'));
    amount_row(_("Amount :"), 'extend_amount', get_post('amount'), null, null, 2);   
    
    $_POST['extend_start_date'] = $_POST['start_date'];
    $_POST['extend_end_date'] = $_POST['end_date'];
    date_row("Start Date :", 'extend_start_date');
    date_row("End Date :", 'extend_end_date');
    amount_row(_("Interest :"), 'extend_interest', get_post('interest'), null, null, 2);        
    fixed_deposit_security_type_list_row("Security type :", 'extend_security_type_id', null, '', true);
    
    
    if($_POST['extend_security_type_id'] == ST_COMPANY_LOANS){
        company_loans_list_row("Company Loans :", "extend_security_id", get_post('loan_id'));
    }elseif($_POST['extend_security_type_id'] == ST_CREDIT_FACILITY){
        credit_facilities_list_row("Credit Facility :", 'extend_security_id', get_post('credit_facility_id'));
    }
    else{
        
    }
    
    $_POST['extend_lien_amount'] = $_POST['lien_amount'];
    amount_row("Lien Amount :", "extend_lien_amount");
    
    end_table(1);
    
    submit_center_first('EXTEND_FIX_DEPOSIT', _("Extend Fixed Deposit"), '', 'default');
    
}
//--------------------------------------------------------------------------------------------------


if ($Mode=='ADD_ITEM' && check_data($selected_id))
{
    if(isset($_POST['security_id']) && $_POST['security_id'] > 0){
        $security_id = $_POST['security_id'];
    }
    else{
        $security_id = 0;
    }
   
    $fd_msg = add_fixed_deposit($_POST['name'], $_POST['description'], $_POST['bank_id'], 
                                $_POST['branch_id'], $_POST['bank_account_id'], input_num('amount'), 
                                $_POST['start_date'], $_POST['end_date'], input_num('interest'),
                                $_POST['security_type_id'], $security_id, input_num('lien_amount'), $_POST['matured']);          

    if($fd_msg['success'] !== true){
        display_error($fd_msg['message']);
    }
    else{
        $selected_id = -1;
        $Mode = 'RESET';

        set_session_variable('bond_messages', $fd_msg['message']);
        meta_forward($_SERVER['PHP_SELF'], "AddedID=".$fd_msg['trans_no']);
    }
    
}
    
if ($Mode=='UPDATE_ITEM' && check_data($selected_id))
{    
    if(isset($_POST['security_id']) && $_POST['security_id'] > 0){
        $security_id = $_POST['security_id'];
    }
    else{
        $security_id = 0;
    }
       
    $ufd_msg = update_fixed_deposit($selected_id, $_POST['name'], $_POST['description'], $_POST['bank_id'], 
                                $_POST['branch_id'], $_POST['bank_account_id'], input_num('amount'), 
                                $_POST['start_date'], $_POST['end_date'], input_num('interest'),
                                $_POST['security_type_id'], $security_id, input_num('lien_amount'),
                                $_POST['matured']);          

    if($ufd_msg['success'] !== true){
        display_error($ufd_msg['message']);
    }
    else{
        $selected_id = -1;
        $Mode = 'RESET';

        set_session_variable('bond_messages', $ufd_msg['message']);
//        meta_forward($_SERVER['PHP_SELF'], "AddedID=".$ufd_msg['trans_no']);
    }

}

if(isset($_POST['EXTEND_FIX_DEPOSIT']) && can_mature_fixed_deposit($selected_id))
{
    if(isset($_POST['extend_security_id']) && $_POST['extend_security_id'] > 0){
        $extend_security_id = $_POST['extend_security_id'];
    }
    else{
        $extend_security_id = 0;
    }
    
    $fd_msg = extend_fixed_deposit($selected_id, $_POST['extend_name'], $_POST['extend_description'], $_POST['extend_bank_id'],
                                    $_POST['extend_branch_id'], $_POST['extend_bank_account_id'], 
                                    input_num('extend_amount'), $_POST['extend_start_date'] , $_POST['extend_end_date'], input_num('extend_interest'),
                                    $_POST['extend_security_type_id'], $extend_security_id, input_num('extend_lien_amount'));  

            
    if($fd_msg['success'] !== true){
        display_error($fd_msg['message']);
    }
    else{
        $selected_id = -1;
        $Mode = 'RESET';

        set_session_variable('bond_messages', $fd_msg['message']);
//        meta_forward($_SERVER['PHP_SELF'], "AddedID=".$fd_msg['trans_no']);
    }
}

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


if ($Mode == 'RESET')
{
	$selected_id = -1;
}

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

global $Ajax;

start_form();

    
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_fixed_deposit_edit_controls($fixed_deposit_id);
hidden('FDid',  $fixed_deposit_id);

br(2);

if($fixed_deposit_id > 0){
    display_fixed_deposit_extend_controls();
}

br(2);

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


?>
