<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_LEAVE_ENTITLEMENTS';
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");
include_once($path_to_root . "/payroll/includes/leave_calculation.inc");

$js = '';
$js .= get_js_open_window(900, 500);
$js .= get_js_date_picker();

page(_($help_context = "Leave Entitlements"));

simple_page_mode(true);

global $Ajax;

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

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

function check_data($selected_id)
{
   	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) || ! (input_num('leave_entitlement') > 0))
   	{
            display_error( _("The leave entitlement entered was not numeric or not possitive."));
            set_focus('leave_entitlement');
            return false;
   	}
        
    if (!check_num('monthly_limit', 0) || ! (input_num('monthly_limit') > 0))
   	{
            display_error( _("The monthly limit entered was not numeric or not possitive."));
            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_leave_entitlement($_POST['employee_code'], $_POST['leave_type_id'], 
                                    date2sql($_POST['from_date']), date2sql($_POST['to_date']) , 
                                    $_POST['leave_entitlement'], $_POST['monthly_limit'], 
                                    $_POST['uninformed_leave_type'], $_POST['comment']);


            display_notification(_("Leave entitlement has been added."));
    } 
    if ($Mode == 'UPDATE_ITEM') 
    {
            update_leave_entitlement($selected_id, $_POST['employee_code'],
                                    date2sql($_POST['from_date']), date2sql($_POST['to_date']) , 
                                    $_POST['leave_entitlement'], $_POST['monthly_limit'],
                                    $_POST['uninformed_leave_type'], 
                                    $_POST['comment'], $_POST['is_closed']);

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

}

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

if ($Mode == 'Delete')
{
    if(check_leave_entitlement_used($selected_id))
   	{
            display_error( _("There are leaves applied for this leave entitlement. Not possible to delete."));
            //set_focus('leave_entitlement');
            return false;
   	}
    
	delete_leave_entitlement($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');
}

start_form();

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

    $result = get_leave_entitlements($_POST['employee_code']);
    
    div_start('price_table');
    
    if (db_num_rows($result) == 0)
    {
    	display_note(_("There is no leave entitlements for selected employee."));
    }
    else
    {
        start_table(TABLESTYLE, "width=90%");

		$th = array(_("Leave Type"), _("From date"), _("To Date"),
			_("Entitlement"), _("Monthly Limit"), _("Applied"), _("Balance"), _("Approved"), _("Nopay"),
                         _("Payroll Deducted"), _("Uninfomed..."),_("Comment"), "", "");

        table_header($th);

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

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

            label_cell($myrow["leave_type_name"], "align=center");
            label_cell($myrow["from_date"],  "align=center");
            label_cell($myrow["to_date"], "align=center");
            
            $leave_entitlement = $myrow["leave_entitlement"];
            label_cell($leave_entitlement, "align=center");
            
            $monthly_limit = '';
            if($myrow["monthly_limit"] > 0){
                $monthly_limit = $myrow["monthly_limit"];
            }
            label_cell($monthly_limit, "align=center");
            
            
            //$total_nopay_days = get_previous_nopay_days($myrow['id'], $_POST['employee_code']);
            $leave_entitlement_balances = get_leave_entitlement_balances($myrow['id'], $_POST['employee_code']);
            $total_nopay_days = $leave_entitlement_balances['nopay_days'];
            $total_applied = $leave_entitlement_balances['applied'];
            $approved = $leave_entitlement_balances['approved'];
            
            label_cell(number_format($total_applied,1), "align=center");
            label_cell(number_format($leave_entitlement - $total_applied,1), "align=center");
            label_cell(number_format($approved,1), "align=center");
            label_cell(number_format($total_nopay_days,1), "align=center");
            
            label_cell($myrow["leave_payroll_deducted"], "align=center");
            
            if($myrow["uninformed_leave_type"] == 1){
                $uninformed_leave_type = "Yes";
            }
            else{
                $uninformed_leave_type = "No";
            }
            label_cell($uninformed_leave_type, "align=center");
            label_cell($myrow["comment"]);
		 	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();

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

$dec2 = 6;
if ($Mode =='Edit')
{
    $myrow = get_leave_entitlement($selected_id);

    $_POST['leave_type_id'] = $myrow["leave_type_id"];
    $_POST['leave_type_name'] = $myrow["leave_type_name"];
    $_POST['from_date'] = sql2date($myrow["from_date"]);
    $_POST['to_date'] = sql2date($myrow["to_date"]);
    $_POST['leave_entitlement'] = $myrow["leave_entitlement"];
    $_POST['monthly_limit'] = $myrow["monthly_limit"];
    $_POST['leave_used'] = $myrow["leave_used"];
    $_POST['leave_scheduled'] = $myrow["leave_scheduled"];
    $_POST['leave_payroll_deducted'] = $myrow["leave_payroll_deducted"];
    $_POST['uninformed_leave_type'] = $myrow["uninformed_leave_type"];
    $_POST['comment'] = $myrow["comment"];
    
    hidden('leave_type_id', $_POST['leave_type_id']);
    hidden('leave_type_name', $_POST['leave_type_name']);
    
}

br();

hidden('selected_id', $selected_id);

if (@$_GET['popup'])
{
	hidden('_tabs_sel', get_post('_tabs_sel'));
	hidden('popup', @$_GET['popup']);
}

    start_table(TABLESTYLE);

        if($selected_id>0)
        {
            if(isset($_POST['leave_type_name']))
            {
                label_row(_("Leave type:"), $_POST['leave_type_name']);
            }
            hidden('leave_type_id', $selected_id);
        }
        else 
        {
            value_list_row("Leave Type:", 'leave_type_id', "LEAVE_TYPES");
        }

        date_row("From Date:", 'from_date');
        date_row("To Date:", 'to_date');

        if(isset($_POST['leave_entitlement'])){
            amount_row(_("Entitlement:"),'leave_entitlement', $_POST['leave_entitlement'],null,null,1);
        }
        else{
            amount_row(_("Entitlement:"), 'leave_entitlement', null,null,null,1);
        }

        amount_row(_("Monthly Limit:"), 'monthly_limit', null,null,null,1);

        yesno_list_row("Uninformed Absent Leave Type:", 'uninformed_leave_type',0);

        textarea_row(_("Comment:"), 'comment', null, 20, 3);

        yesno_list_row("Closed:", 'is_closed',0);

    end_table(1);

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

end_form(2);
end_page(@$_GET['popup'], false, false);

?>