<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_LOANS';
if (!@$_GET['popup'])
	$path_to_root = "../..";
else	
	$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/payroll/includes/payroll_db.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 = "Employee Loans"));

//----------------------------------------------------------------------------------------
simple_page_mode(true);

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


function check_data($selected_id)
{	
    if (!check_num('loan_amount', 0))
    {
        display_error( _("Loan amount entered was not numeric or positive."));
        set_focus('loan_amount');
        return false;
    }
    
    if (!check_num('installment_amount', 0))
    {
        display_error( _("Installment amount entered was not numeric or possitive."));
        set_focus('installment_amount');
        return false;
    }
    
    return true;
    
}
//--------------------------------------------------------------------------------------------------

function can_commit($selected_id)
{
            if(!check_data)
                return false;
            
            if ($_POST['employee_code'] == "" || !isset($_POST['employee_code']))
   	{
            display_error( _("There is no employee selected."));
            set_focus('employee_code');
            return false;
   	}
   	
        if (!check_num('leave_entitlement', 0))
   	{
            display_error( _("The leave entitlement entered was not numeric."));
            set_focus('leave_entitlement');
            return false;
   	}
        
        if(!isset($_POST['leave_type_id']))
        {
            return false;
        }
        
        if (get_existing_leave_entitlement($_POST['employee_code'], $_POST['leave_type_id'],
                date2sql($_POST['from_date']), date2sql($_POST['to_date']),$selected_id))
        {
            display_error( _("There is a period overlapping definition for this leave type."));
            set_focus('from_date');
            return false;
        }
            
        
        return true;
            
}

if (($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') && check_data($selected_id))
{

    if ($Mode == 'ADD_ITEM') 
    {
            add_loan($_POST['employee_code'], $_POST['loan_description'], 
                                    input_num('loan_amount'), input_num('installment_amount'), 
                                    input_num('interest'), date2sql($_POST['start_date']) , 
                                    $_POST['inactive'], $_POST['completed']);


            display_notification(_("Loan has been added."));
    } 
    if ($Mode == 'UPDATE_ITEM') 
    {
            update_loan_definition($selected_id, $_POST['employee_code'], $_POST['loan_description'],
                                    input_num('loan_amount'), input_num('installment_amount'), 
                                    input_num('interest'), date2sql($_POST['start_date']) , 
                                    $_POST['inactive'], $_POST['completed']);

            $selected_id = -1;
            display_notification(_("Leave entitlement has been updated."));
    }
    
    $Mode = 'RESET';

}

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

if ($Mode == 'Delete')
{
	return;
        
            delete_loan($selected_id);
	display_notification(_("The leave entitlement has been sucessfully deleted."));
	$Mode = 'RESET';
}

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

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

if (list_updated('employee_code')) 
	$Ajax->activate('price_table');
//--------------------------------------------------------------------------------------------------

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


        emoplyees_list_cells(_("Select an Employee:"), 'employee_code', null,
	  false, true, check_value('show_inactive'));

br(2);	

    $result = get_active_loans($_POST['employee_code']);
    
    div_start('price_table');
    
    if (db_num_rows($result) == 0)
    {
    	display_note(_("There are no loans obtained by the selected employee"));
    }
    else
    {
        start_table(TABLESTYLE, "width=65%");

		$th = array(_("id"), _("Description"), _("Total Loan"),
                    _("Instalment"), _("Interest"),_("Start Date"), 
                    _("Total paid"), _("Total Interest"),
                    _("On Hold"), _("Completed"), "", "");

        table_header($th);

        $k = $j = 0; //row colour counter

        while ($myrow = db_fetch($result))
        {
			alt_table_row_color($k);

            label_cell($myrow["id"]);
            label_cell($myrow["loan_description"]);
            label_cell($myrow["loan_amount"], "align=right");
            label_cell($myrow["installment_amount"], "align=right");
            label_cell($myrow["interest"], "align=right");
            label_cell(sql2date($myrow["start_date"]), "align=center");
            label_cell($myrow["total_paid"], "align=right");
            label_cell($myrow["total_interest"], "align=right");
            
            if($myrow["inactive"] == 1)
                label_cell("Yes", "align=center");
            else {
                label_cell("No", "align=center");
            }
            
            if($myrow["completed"] == 1)
                label_cell("Yes", "align=center");
            else {
                label_cell("No", "align=center");
            }
            
		 	edit_button_cell("Edit".$myrow['id'], _("Edit"));
		 	delete_button_cell("Delete".$myrow['id'], _("Delete"));
            end_row();

            $j++;
            If ($j == 12)
            {
            	$j = 1;
        		table_header($th);
            } //end of page full new headings
        } //end of while loop

        end_table();
    }
    div_end();

//-----------------------------------------------------------------------------------------------
    $_POST['id'] = 0;
    $_POST['loan_description'] = "";
    $_POST['loan_amount'] = 0;
    $_POST['installment_amount'] = 0;
    $_POST['interest'] = 0;
    $_POST['start_date'] = "";
    $_POST['inactive'] = 0;
    $_POST['completed'] = 0;
    
$dec2 = 6;
if ($Mode =='Edit')
{
    $myrow = get_loan_details($selected_id);

    $_POST['id'] = $myrow["id"];
    $_POST['loan_description'] = $myrow["loan_description"];
    $_POST['loan_amount'] = $myrow["loan_amount"];
    $_POST['installment_amount'] = $myrow["installment_amount"];
    $_POST['interest'] = $myrow["interest"];
    $_POST['start_date'] = sql2date($myrow["start_date"]);
    $_POST['inactive'] = $myrow["inactive"];
    $_POST['completed'] = $myrow["completed"];
    
    hidden('id', $_POST['id']); 
}

br();

hidden('selected_id', $selected_id);

start_table(TABLESTYLE2);

text_row("Description", "loan_description", $_POST['loan_description'], 50,200);
amount_row(_("total Loan:"),'loan_amount', $_POST['loan_amount'],null,null,2);
amount_row(_("Installment Amount:"),'installment_amount', $_POST['installment_amount'],null,null,2);
amount_row(_("Interest:"),'interest', $_POST['interest'],null,null,2);
date_row("Loan Start Date:", "start_date", $_POST['start_date']);
yesno_list_row("On Hold:", 'inactive', $_POST['inactive'] );
yesno_list_row("Completed:", 'completed', $_POST['completed'] );

end_table(1);

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

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