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

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

$js = "";
if (user_use_date_picker())
	$js .= get_js_date_picker();
page(_($help_context = "Credit Facilities"), 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/credit_facilities_db.inc");
include_once($path_to_root . "/gl/includes/db/gl_db_trans.inc");

        
        
simple_page_mode(true);

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


$credit_facility_id = -1;
//if(isset($_GET['ModifyMRNumber']))
//{
//	$_POST['ModifyMRNumber'] = $_GET['ModifyMRNumber'];
//}
if(isset($_POST['credit_facility_id'])){
    $credit_facility_id = $_POST['credit_facility_id'];
}

function clear_data()
{
    unset($_POST['name']);	
    unset($_POST['description']);
    unset($_POST['bank_id']);  
    unset($_POST['bank_branch_id']);
    unset($_POST['start_date']);
    unset($_POST['end_date']);
    unset($_POST['amount']);      
}

function can_process_credit_facility($credit_facility_id)
{ 
    $input_error = 0;
    
    if (strlen($_POST['name']) == 0) 
    {
        $input_error = 1;
        display_error(_('Enter a name.'));
        set_focus('name');
        return false;
    }     
    
    if (strlen($_POST['description']) == 0) 
    {
        $input_error = 1;
        display_error( _('Enter a description.'));
        set_focus('description');
        return false;
    } 
   
    if(!(input_num('amount') > 0))
    {
        display_error( _("Amount must be numeric and positive."));
        set_focus('amount');
        return false;
    }
    
    if(!($credit_facility_id > 0)){
        if (check_num('bank_id', 0) == FALSE){
            display_error( _("Please select the Bank."));
            set_focus('bank_id');
            return false;
        }        

        if(!(input_num('bank_branch_id') > 0)){
            display_error(_("Please select a Bank Branch."));
            set_focus('bank_branch_id');
            return false;
        }  
    }  
    
    if (is_date($_POST['start_date']) == false)
    {
        $input_error = 1;
        display_error( _("Incorrect start date"));
        set_focus('start_date');
        return false;
    }
    
    if (is_date($_POST['end_date']) == false)
    {
        $input_error = 1;
        display_error(_("Incorrect end date"));
        set_focus('end_date');
        return false;
    }
    
    if (date1_greater_date2($_POST['start_date'], $_POST['end_date']))
    {
        $input_error = 1;
        display_error( _("start date can not be greater than end date"));
        set_focus('end_date');
        return false;
    }       
    
    return true;    
}


function can_process_credit_facility_sublimit($credit_facility_id, $selected_id)
{
    if(!(strlen($_POST['sublimit_name']) > 1)){
        display_error( _("Please enter sublimit name."));
        set_focus('sublimit_bank_account_id');
        return false;
    }
    
    if (check_num('sublimit_amount', 0) == FALSE){
        display_error( _("Amount must be numeric and can not be a negative value."));
        set_focus('sublimit_amount');
        return false;
    }  
    
    if(!($_POST['sublimit_bank_account_id'] > 0)){
        display_error( _("Please select a bank account for Sublimit."));
        set_focus('sublimit_bank_account_id');
        return false;
    }
    
    if(check_bank_account_already_selected_for_credit_facility($credit_facility_id, $_POST['sublimit_bank_account_id']))
    {
        display_error( _("Selected bank account has been already selected for this credit facility."));
        set_focus('sublimit_bank_account_id');
        return false;
    }
    
    return true;    
}


function display_credit_facility_header($credit_facility_id)
{   
    //table_section(1);     
     start_table(TABLESTYLE2);  
   // table_section_title(_("Credit Facilities"));
    if ($credit_facility_id > 0){
        $myrow = get_credit_facility($credit_facility_id);

        $_POST['id'] = $myrow["id"];
        $_POST['name'] = $myrow["name"];
        $_POST['description'] = $myrow["description"];
        $_POST['bank_id'] = $myrow["bank_id"];
        $_POST['bank_branch_id'] = $myrow["branch_id"];
        $_POST['bank_account_id'] = $myrow["bank_account_id"];
        $_POST['start_date'] = sql2date($myrow["start_date"]);             
        $_POST['end_date'] = sql2date($myrow["end_date"]);            
        $_POST['amount'] =  $myrow['amount'];   
        $_POST['comment'] = $myrow["comment"]; 
                             
        start_table(TABLESTYLE_NOBORDER);

        text_row("Name: ", 'name', null, 41, 200);        
        textarea_row(_('Description :'), 'description', null, 25, 1);  

        if($credit_facility_id > 0){

//            $bank_name = get_bank_name($_POST['credit_facility_id']);
//
            label_row("Bank Name :", $myrow['bank_name']);
            hidden('bank_id', $_POST['bank_id']);

            label_row("Bank Branch Name :", $myrow['branch_name']);
            hidden('bank_branch_id', $_POST['bank_branch_id']);
            
//            value_list_row(_("Bank Name :"), 'bank_id', "BANKS", null, 'Select a Bank', true);        
//            bank_branch_list_row(_("Bank Branch Name :"), 'bank_branch_id', $_POST['bank_id'], null, 'Select a Bank Branch');
//            bank_accounts_list_row("Bank Account :", "bank_account_id", null, false, 'Select a Bank Account');
//            label_row("Bank Account :", $bank_name['bank_account_name']);
//            hidden('bank_account_id', $_POST['bank_account_id']);                
        }
                   
        date_row("Start Date :", 'start_date');
        date_row("End Date :", 'end_date');
        amount_row("Amount :", 'amount');               
        textarea_row(_('Comment :'), 'comment', null, 25, 3);            
                       
        end_table(1);
    
    } 
    else{                 
//        $_POST['id'] = -1;
//        $_POST['name'] = '';
//        $_POST['description'] = '';
//        $_POST['bank_id'] = -1;
//        $_POST['bank_branch_id'] = -1;
//        $_POST['bank_account_id'] = 0;
//        $_POST['start_date'] = Today();             
//        $_POST['end_date'] = Today();            
//        $_POST['amount'] =  0.00;   
//        $_POST['comment'] = ''; 
        
        text_row("Name : ", 'name', null, 25, 50);        
        textarea_row(_('Description :'), 'description', null, 25, 1);          
        value_list_row(_("Bank Name :"), 'bank_id', "BANKS", null, 'Select a Bank', true);        
        bank_branch_list_row(_("Bank Branch Name :"), 'bank_branch_id', $_POST['bank_id'], null, 'Select a Bank Branch');
//        bank_accounts_list_row("Bank Account :", 'bank_account_id', null, false, 'Select a bank account');
//        hidden('bank_account_id', $_POST['bank_account_id']);   
        
        date_row("Start Date :", 'start_date');
        date_row("End Date :", 'end_date');
        amount_row("Amount :", 'amount');           
        textarea_row(_('Comment :'), 'comment', null, 25, 3);

        end_table(1);
        end_outer_table(1);
    }
    
    
    
    if($credit_facility_id > 0){
        submit_center_first('SAVE_CREDIT_FACILITY', _("Save"), '', true);  
        br(2);
    }
      
}  


function display_credit_facility_sublimits($credit_facility_id)
{

    
    div_start('sublimits');

    start_table(TABLESTYLE, "width=60%");

    $th = array("Id", "Name",_("Limit"), "Account", "Comment", "Inactive", "Balance", "");
    table_header($th);

    $k=0; //row colour counter

    $q = get_credit_facility_sublimits($credit_facility_id);
    
    while ($sub_limit = db_fetch($q))
    {
        alt_table_row_color($k);
        label_cell($sub_limit["id"]);
        label_cell($sub_limit["name"]);
        amount_decimal_cell($sub_limit["amount"]);
        label_cell($sub_limit["bank_account_number"]." - ".$sub_limit["bank_account_name"]);
        label_cell($sub_limit["comment"]);
        label_cell(get_yes_no_name($sub_limit["inactive"]), "align=center");
        
//        $bfw = get_gl_balance_from_to("01/01/2001", Today(), $sub_limit["account_code"]);
        $bfw = get_bank_account_unreconciled_balance($sub_limit["account_code"]);
        amount_decimal_cell($sub_limit["amount"] - $bfw);
        
        edit_button_cell("Edit".$sub_limit["id"], _("Edit"));

    }
    
    end_table();
}

function display_credit_facility_sublimit_edit_controls($selected_id)
{   
    //table_section(1);     
    start_table(TABLESTYLE2);  
   // table_section_title(_("Credit Facilities"));
    if ($selected_id > 0){
        $myrow = get_credit_facility_sub_limit($selected_id);

        $_POST['sublimit_id'] = $myrow["id"];
        $_POST['sublimit_name'] = $myrow["name"];
        $_POST['sublimit_amount'] = $myrow["amount"];  
        $_POST['sublimit_bank_account_id'] = $myrow["bank_account_id"];  
        $_POST['sublimit_comment'] = $myrow["comment"]; 
        $_POST['sublimit_inactive'] = $myrow["inactive"]; 
                             
        text_row("Name: ", 'sublimit_name', null, 41, 200);   
        amount_row("Amount :", 'sublimit_amount');     
//        bank_accounts_list_row("Bank Account :", 'sublimit_bank_account_id', null, false,'');
        label_row("Bank :", $myrow["bank_account_name"]." - ".$myrow["bank_name"]);
        hidden('sublimit_bank_account_id', $_POST['sublimit_bank_account_id']);
        textarea_row(_('Comment :'), 'sublimit_comment', null, 25, 3);            
    } 
    else{                 
        text_row("Name : ", 'sublimit_name', null, 25, 50);        
        amount_row("Amount :", 'sublimit_amount');           
        bank_accounts_list_row("Bank Account :", 'sublimit_bank_account_id', null, false,'');
        textarea_row(_('Comment :'), 'sublimit_comment', null, 25, 3);

//        end_outer_table(1);
    }
    
    end_table(1);
    
    hidden('selected_id', $selected_id);

    if ($selected_id > 0){
        submit_center_first('UPDATE_SUBLIMIT', _("Update Sublimit"), '', true);            
        submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel'); 
    }
    else{
        submit_center_first('ADD_SUBLIMIT', _("Add Sublimit"), '', true);  
    }  
}

function save_credit_facility($credit_facility_id)
{
    if(can_process_credit_facility($credit_facility_id) == true){
        if($credit_facility_id > 0){
            update_credit_facility($credit_facility_id, $_POST['name'] , $_POST['description'], 
                                $_POST['start_date'], $_POST['end_date'], 
                                input_num('amount'), $_POST['comment']);        

            return $credit_facility_id;
        }
        else{
            $credit_facility_id = add_credit_facility($_POST['name'] , $_POST['description'], $_POST['bank_id'], 
                                $_POST['bank_branch_id'], $_POST['start_date'], $_POST['end_date'], 
                                input_num('amount'), $_POST['comment']);        

            return $credit_facility_id;
            
        }

//        clear_data();
    }
    
    return -999;
}


function update_cf_sublimit($credit_facility_id, $selected_id)
{
    
    if(!can_process_credit_facility_sublimit($credit_facility_id, $selected_id)){
        return -999;
    }
        
    $returned_cf_id = save_credit_facility($credit_facility_id);
    if(!($returned_cf_id > 0)){
        return -999;
    }
        
    update_credit_facility_sublimit($selected_id, $_POST['sublimit_name'], input_num('sublimit_amount'), 
                                    $_POST['sublimit_bank_account_id'], $_POST['sublimit_comment']);        

    return $returned_cf_id;
    
}

function add_cf_sublimit($credit_facility_id)
{
    if(!can_process_credit_facility_sublimit($credit_facility_id, -1)){
        return -999;
    }
        
    $returned_cf_id = save_credit_facility($credit_facility_id);
    if(!($returned_cf_id > 0)){
        return -999;
    }
        
    add_credit_facility_sublimit($returned_cf_id, $_POST['sublimit_name'], input_num('sublimit_amount'), 
                                    $_POST['sublimit_bank_account_id'], $_POST['sublimit_comment']);        

    return $returned_cf_id;
    
}

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

if(isset($_POST['SAVE_CREDIT_FACILITY']))
{    
    if($credit_facility_id > 0){
        save_credit_facility($credit_facility_id);    
        display_notification(_('Credit Facility has been Updated')); 
    }
    else{
        save_credit_facility($credit_facility_id);
        display_notification(_('New Credit Facility has been added'));  
    }
}    
    
//--------------------------------------------------------------------------------------------
    
if(isset($_POST['UPDATE_SUBLIMIT']))
{    
    
    if(update_cf_sublimit($credit_facility_id, $selected_id) > 0){
        display_notification(_('Credit Facility Sublimit been updated.')); 
        $selected_id = -1;
        $Mode = 'RESET';
    }
    
    
}      
    
if(isset($_POST['ADD_SUBLIMIT']))
{    
    $returned_cf_id = add_cf_sublimit($credit_facility_id);
    if($returned_cf_id > 0){
        $credit_facility_id = $returned_cf_id;
        $_POST['credit_facility_id'] = $credit_facility_id;
        display_notification(_('Credit Facility Sublimit been added.')); 
        $Mode = 'RESET';
    }

} 

//-----------------------------------------------------------------------------------------
    
    if(isset($_POST['cancel'])){      
        $selected_id = -1;
    }
//-----------------------------------------------------------------------------------------
    
if ($Mode == 'Delete')
{
	//$credit_facility_id = get_post('credit_id');
//	   $recs = delete_credit_facilities($credit_facility_id);
//           
//           display_notification(_('Selected Credit Facility has been deleted'));           
//	
//	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$sav = get_post('show_inactive');
//	unset($_POST);
	$_POST['show_inactive'] = $sav;
}

if (get_post('cancel')) {

    clear_data();
	set_focus('serial');
	$Ajax->activate('_page_body');
}

if(list_updated('credit_facility_id')){
    $selected_id = -1;
}
//---------------------------------------------------------------------------------------------



global $Ajax;

$Ajax->activate('credit_facility_id');

start_form();

br();

start_table(TABLESTYLE_NOBORDER);

credit_facility_list_row("Credit Facility :", 'credit_facility_id', null, 'New Credit Facility', true);


end_table();

br();

$Ajax->activate('details');
div_start('details');

//$credit_facility_id = get_post('credit_facility_id');
//display_credit_facilities($credit_facility_id);

display_credit_facility_header($credit_facility_id);

display_credit_facility_sublimits($credit_facility_id);

br();

display_credit_facility_sublimit_edit_controls($selected_id);

br();
//    tabbed_content_end();
div_end();

hidden('popup', @$_REQUEST['popup']);

end_form();

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

end_page(@$_REQUEST['popup']);


?>

