<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_APPLY_LEAVE';
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");

include_once($path_to_root . "/includes/db/module_settings_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();
}


if (!@$_GET['popup'])
	page(_($help_context = "Apply Leave"));

//----------------------------------------------------------------------------------------
simple_page_mode(true);
if (isset($_GET['employee_code']))
{
	$_POST['employee_code'] = $_GET['employee_code'];
}

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

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
{
    $can_process = can_process_leave();
    
    if($can_process)
    {
        $data["leave_entitlement_id"] = $_POST["leave_entitlement_id"];
        
        $leave_entitlement = get_leave_entitlement($data["leave_entitlement_id"]);
        $uninformed_leave_type = $leave_entitlement['uninformed_leave_type'];
        $data["uninformed_leave_type"] = $uninformed_leave_type;
        
        $data["employee_code"] = $_POST["employee_code"];
        $data["from_date"] = date2sql($_POST["from_date"]);
        $data["leave_from_time_code"] = $_POST["leave_from_time_code"];
        $data["to_date"] = date2sql($_POST["to_date"]); 
        $data["leave_to_time_code"] = $_POST["leave_to_time_code"];

        $data["comment"] = $_POST["comment"];
        $data["is_revoke"] = $_POST["is_revoke"];

        $add_leave_msg = add_leave($data, $Mode, true);
        if($add_leave_msg['passed'] == true){
            notify_leave_approver($add_leave_msg['message']);
            display_notification(_("Leave record has been added."));
        }
        else {
            display_error($add_leave_msg['message'] );
        }
    }    
        

}


if ($Mode == 'Delete')
{
	delete_leave($selected_id);
	display_notification(_("Leave has been deleted."));
	$Mode = 'RESET';
}

function can_process_leave()
{
    if(!($_POST['leave_entitlement_id'] > 0)){
        display_error( _("Please select a leave type."));
        set_focus('leave_entitlement_id');
        return false;
    }
            
    if (!is_date($_POST['from_date'])) {
		display_error(_("The entered date for From Date is invalid."));
		set_focus('from_date');
		return false;
	}
    
    if(!isset($_POST['leave_from_time_code']) || $_POST['leave_from_time_code'] == -1){
        display_error( _("Please select from leave time."));
        set_focus('leave_from_time_code');
        return false;
    }
    
    if (!is_date($_POST['to_date'])) {
		display_error(_("The entered date for To Date is invalid."));
		set_focus('to_date');
		return false;
	}
    
    if(!isset($_POST['leave_to_time_code']) || $_POST['leave_to_time_code'] == -1){
        display_error( _("Please select to leave time."));
        set_focus('leave_to_time_code');
        return false;
    }
    
    if (date1_greater_date2($_POST['from_date'], $_POST['to_date']))
    {
        display_error( _("Leave To Date can not be later than leave From Date"));
        set_focus('to_date');
        return false;
    }
    
        
    $outside = is_outside_payroll_entries_allowed_date_range($_POST['from_date']);
    if($outside["success"] !== true)
   	{
            display_error( _("From Date: ".$outside['message']));
            set_focus('from_date');
            return false;
   	}
    
    
    return true;
}


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

function clear_data()
{
	unset($_POST['long_description']);
}



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

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

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


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

br(2);


if (list_updated('employee_code')) {
	$employee_code = get_post('employee_code');
        clear_data();
	$Ajax->activate('Entitlements');
	$Ajax->activate('applied_leaves');
        $Ajax->activate('controls');
        
}

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

display_heading("Leave Entitlements");

$le_result = get_leave_entitlements($_POST['employee_code']);
    
    div_start('Entitlements');
    
    if (db_num_rows($le_result) == 0)
    {
    	display_note(_("No leave entitlements defined for the selected employee"));
    }
    else
    {
        start_table(TABLESTYLE, "width=65%");

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

        table_header($th);

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

        while ($le_row = db_fetch($le_result))
        {
			alt_table_row_color($k);

            label_cell($le_row["leave_type_name"]);
            label_cell(sql2date($le_row["from_date"]), "align=center");
            label_cell(sql2date($le_row["to_date"]), "align=center");
            //label_cell($le_row["leave_entitlement"], "align=right");
            
            $leave_entitlement = $le_row["leave_entitlement"];
            //label_cell($leave_entitlement, "align=center");
            qty_cell($leave_entitlement, false, 1);
            
            
//            $monthly_limit = '';
//            if($le_row["monthly_limit"] > 0){
//                $monthly_limit = $le_row["monthly_limit"];
//                qty_cell($monthly_limit, false, 1);
//            }
//            else{
//                label_cell("");
//            }
            
            //qty_cell($le_row["leave_used"], false, 1);
            
            $leave_entitlement_balances = get_leave_entitlement_balances($le_row['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");
            //qty_cell($total_nopay_days, false, 1);
            
            qty_cell($le_row["leave_payroll_deducted"], false, 1);
            label_cell($le_row["comment"]);

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

echo "<br/>";
//-----------------------------------------------------------------------------------------------


display_heading("Applied Leave");

$al_result = get_leave_records($_POST['employee_code']);

div_start('applied_leaves');

if (db_num_rows($al_result) == 0)
{
    display_note(_("There is no applied leave for slected employee"));
}
else
{
    start_table(TABLESTYLE, "width=80%");

            $th = array(_("Leave Type"), _("From date"), _("From Session"),
                        _("To Date"),_("To Session"),_("Leave Days"),_("Nopay Days"),
                     _("Is Revoke"),_("Comment"), _("Status"), "");

    table_header($th);

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

    while ($al_row = db_fetch($al_result))
    {
                    alt_table_row_color($k);

        label_cell($al_row["leave_type_name"]);
        label_cell(sql2date($al_row["from_date"]), "align=center");
        label_cell($al_row["from_time_name"]);
        label_cell(sql2date($al_row["to_date"]), "align=center");
        label_cell($al_row["to_time_name"]);
        qty_cell($al_row["leave_days"], false, 1);
        qty_cell($al_row["nopay_days"], false, 1);

        if($al_row["is_revoke"] == 1)
            label_cell("Yes");
        else {
            label_cell("No");
        }
        label_cell($al_row["comment"]);
        label_cell($al_row["leave_status_name"]);
        
//                edit_button_cell("Edit".$myrow['id'], _("Edit"));
        
        if($al_row['status_code'] == 'N'){
            edit_button_cell("Delete".$al_row['id'], _("Delete"));
        }
        else{
            label_cell("");
        }

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

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


br();

hidden('selected_id', $selected_id);

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

$_POST["from_date"] = "";
$_POST["leave_from_time_code"] = "";
$_POST["to_date"] = "";
$_POST["leave_to_time_code"] = "";
$_POST['is_revoke'] = 0;

div_start('controls');

start_table(TABLESTYLE2);

//unset($_POST['leave_type_id2']);
assigned_leave_list_row("Leave Type:", 'leave_entitlement_id', $_POST['employee_code'], null, "Select a leave type");

date_row("From Date:", 'from_date');
value_list_code_row("From Leave Time:", 'leave_from_time_code', "HOLIDAY_TIME", null, "Select a leave time");
date_row("To Date:", 'to_date');
value_list_code_row("To Leave Time:", 'leave_to_time_code', "HOLIDAY_TIME", null, "Select to leave time");

if(isset($_SESSION["no_of_days_leave"])){
    label_row("No. of Days Leave:", $_SESSION["no_of_days_leave"]);
}
//yesno_list_row("Is Revoke:",'is_revoke');
hidden('is_revoke', $_POST['is_revoke']);
textarea_row(_("Comment:"), 'comment', null, 20, 3);

end_table(1);
div_end();

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

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