<?php
/****************************************
/*  Author 	: Kvvaradha
/*  Module 	: Extended HRM
/*  E-mail 	: admin@kvcodes.com
/*  Version : 1.0
/*  Http 	: www.kvcodes.com
*****************************************/
$page_security = 'HR_EMPLOYEE_SETUP';
$path_to_root="../../..";
include($path_to_root . "/includes/session.inc");
add_access_extensions();
include($path_to_root . "/includes/ui.inc");
include($path_to_root . "/modules/ExtendedHRM/includes/Payroll.inc" );

page(_("Attendance Settings"));

CheckEmptyResult('kv_empl_departments', sprintf(_("There is no departments in this system. Kindly Open '%s' to update it"), "<a href='".$path_to_root."/modules/ExtendedHRM/manage/department.php'>"._("Departments")."</a>"));

if(isset($_GET['Updated']) && $_GET['Updated'] == 'Yes'){
	display_notification(_("Attendance Settings Updated"));
}//else
	//display_warning(_("Payroll Begin and End day can be set only once. So careful with the settings."));
simple_page_mode(true);
if(isset($_GET['dept_id']) && $_GET['dept_id'] > 0 && !isset($_POST['dept_id']))
	$_POST['dept_id'] = $_GET['dept_id'];

function can_process() {	

	 /*
	if (!check_num('expd_percentage_amt'))	{
		display_error(_("Maximum EMI Limit should be a positive number"));
		set_focus('login_tout');
		$input_error = 1;
	}
	if (!check_num('ot_factor'))	{
		display_error(_("OT Multiplication Factor should be a positive number"));
		set_focus('ot_factor');
		$input_error = 1;
	}*/
	
	return true;	
}
if (isset($_POST['addupdate'])&& can_process()) {
	if($_POST['dept_id'] > 0 ){
		$options = array( 'early_coming_punch', 'Halfday_workduration', 'absent_workduration' , 'mark_half_day_late', 'mark_half_day_early_go', 'absent_workduration_both', 'consecutive_days_absent', 'late_going_punch');
		$option_times = array('grace_in_time', 'grace_out_time', 'Halfday_workduration_min', 'absent_workduration_min', 'mark_half_day_late_min', 'mark_half_day_early_go_min');
		foreach($option_times as $opt_time){
			$BeginTime = date('H:i:s', strtotime($_POST[$opt_time.'_hour'].':'.str_pad($_POST[$opt_time.'_min'], 2, '0', STR_PAD_LEFT).':00'));
			$_POST[$opt_time] = $BeginTime;
		}
		$process = true;
		if (strtotime($_POST['mark_half_day_late_min']) < strtotime($_POST['grace_in_time'])){
			display_error(_("Your grace In time is greater than this time."));
			set_focus('mark_half_day_late_hour');
			$process =  false;
		}
		if($process) {
			foreach ($_POST as $key => $value) {
				if((in_array($key, $options) || in_array($key, $option_times) ) && $key != 'debt_id'){
					Update('kv_empl_attendance_settings', array('option_name' => $key, 'dept_id' => $_POST['dept_id']) , array('option_value' => $value) );
				}
			}
			meta_forward($_SERVER['PHP_SELF'], "dept_id=".$_POST['dept_id']."&Updated=Yes");	
		}
	} else	
		display_warning(_("No Department selected"));
}

	start_form();
		// start_table(TABLESTYLE_NOBORDER);
		// 	department_list_cells(_("Select a Department")." :", 'dept_id', null, _("Select Department"), true, check_value('show_inactive'), false, true);
		// end_table();

		start_table(TABLESTYLE, "width='90%'");

		$th = array (_('Department'), _("Consider Early Punch In"), _("Consider late Punch Out"), _('Grace Time(Late In)'), _('Grace Time(Early Out)'), _("Calculate Half Day"), _("Calculate Absent"), _("Mark Half Day if Late"), _("Mark Halfday IF Early Going"),_('Absent Workduration both'),_('Consecutive Days Absent'), '','');
		inactive_control_column($th);
		table_header($th);
		$k = 0;
		$get_dept_attendance = get_dept_attendance_settings();

		foreach($get_dept_attendance as $dept => $value){
			start_row();

			label_cell($value['name']);
			label_cell(isset($value['early_coming_punch']) ? ($value['early_coming_punch'] == 1 ? 'Yes' : 'No') : '' );
			label_cell(isset($value['late_going_punch']) ? ($value['late_going_punch'] == 1 ? 'Yes' : 'No') : '' );
			label_cell(isset($value['grace_in_time']) ? $value['grace_in_time']  : '' );
			label_cell(isset($value['grace_out_time']) ? $value['grace_out_time'] : '' );
			label_cell(isset($value['Halfday_workduration']) ? ($value['Halfday_workduration'] == 1 ? 'Yes'.'('.$value['Halfday_workduration_min'].')' : 'No') : '' );
			label_cell(isset($value['absent_workduration']) ? ($value['absent_workduration'] == 1 ? 'Yes'.'('.$value['absent_workduration_min'].')' : 'No') : '' );
			label_cell(isset($value['mark_half_day_late']) ? ($value['mark_half_day_late'] == 1 ? 'Yes'.'('.$value['mark_half_day_late_min'].')' : 'No') : '' );
			label_cell(isset($value['mark_half_day_early_go']) ? ($value['mark_half_day_early_go'] == 1 ? 'Yes'.'('.$value['mark_half_day_early_go_min'].')' : 'No') : '');
			label_cell(isset($value['absent_workduration_both']) ? ($value['absent_workduration_both'] == 1 ? 'Yes'.'('.$value['mark_half_day_early_go_min'].')' : 'No') : '');
			label_cell(isset($value['consecutive_days_absent']) ? ($value['consecutive_days_absent'] == 1 ? 'Yes'.'('.$value['mark_half_day_early_go_min'].')' : 'No') : '');

			edit_button_cell("Edit".$dept, _("Edit"));
		 	delete_button_cell("Delete".$dept, _("Delete"));
			end_row();

			//{"1":{"dept_id":"1","early_coming_punch":"1","Halfday_workduration":"1","absent_workduration":"1","partial_halfday_workduration":"1","partial_absent_workduration":"1","mark_half_day_late":"1","mark_half_day_early_go":"1","absent_workduration_both":"1","late_going_punch":"1","grace_in_time":"02:00:00","grace_out_time":"02:00:00","Halfday_workduration_min":"04:00:00","absent_workduration_min":"02:00:00","partial_halfday_workduration_min":"04:00:00","partial_absent_workduration_min":"02:00:00","mark_half_day_late_min":"04:00:00","mark_half_day_early_go_min":"04:00:00"},"2":
		}
		end_table();
		br(2);
	div_start('Department_Details');
		if ($selected_id != -1)	{

 	if ($Mode == 'Edit') {
 		//display_error($Mode.'-'.$selected_id);
		$_POST['grace_in_time'] = $_POST['grace_out_time'] = $_POST['Halfday_workduration_min'] = $_POST['absent_workduration_min'] =  $_POST['mark_half_day_late_min'] = $_POST['mark_half_day_early_go_min'] = '';

		if($selected_id> 0){
			$all_settings =  GetAll('kv_empl_attendance_settings', array('dept_id' => $selected_id));

			foreach($all_settings as $settings){
				//if(isset($_POST['BeginDay']) && $settings['option_name'] != $_POST['BeginDay'])
					$_POST[$settings['option_name']] = $settings['option_value'];
			}
			$Ajax->activate('Department_Details');
		}

		hidden('dept_id', $selected_id);
		start_outer_table(TABLESTYLE2);

			table_section(1);
			//table_section_title(_("Grace Times"));
				//text_row(_("Grace Time for Late Coming"), 'grace_in_time', null, 10, 10);	
				department_list_row( _("Department").":", 'dept_id', null, false,false, false,false, true,true);
				TimeDropDown_row(_('Grace Time for Late Coming Punch'), 'grace_in_time', $_POST['grace_in_time'], false, true);			

				check_row(_("Consider Early Coming Punch")." :", 'early_coming_punch');
				table_section_title(_(" "));
				check_row(_("Calculate Halfday if work duration is less than"), 'Halfday_workduration');
				check_row(_("Calculate Absent if work duration is less than"), 'absent_workduration');
				table_section_title(_(" "));
				//check_row(_("On Partial day Calculation Half Day if work duration is less than  "), 'partial_halfday_workduration');
				//check_row(_("On Partial day Calculation Absent if work duration is less than"), 'partial_absent_workduration');
				
				check_row(_("Mark Halfday if late by"), 'mark_half_day_late');
				check_row(_("Mark Halfday if Early Going by "), 'mark_half_day_early_go');

				check_row(_("Mark Weekly Off and Holiday as Absent, if both Sufix, Prefix is Absent"), 'absent_workduration_both');						
				check_row(_("Mark Weekly Off and Holiday as Absent, if both consecutive days Sufix, Prefix is Absent"), 'consecutive_days_absent');						
			table_section(2);
				//text_row(_("Grace Time for Early Going"), 'grace_out_time', null, 10, 10);
				TimeDropDown_row(_('Grace Time for Early Going Punch'), 'grace_out_time', $_POST['grace_out_time'], false,  true);	
				check_row(_("Consider Late Going Punch")." :", 'late_going_punch');	
				table_section_title(_(" "));
				TimeDropDown_row(_(' '), 'Halfday_workduration_min', $_POST['Halfday_workduration_min'], false,  true);	
				//text_row_ex(_(" "), 'Halfday_workduration_min', 10, 10, '', null, null, " Mins");
				TimeDropDown_row(_(' '), 'absent_workduration_min', $_POST['absent_workduration_min'], false,  true);	
				table_section_title(_(" "));
				//TimeDropDown_row(_(' '), 'partial_halfday_workduration_min', $_POST['partial_halfday_workduration_min'], false, false, true);	
				//TimeDropDown_row(_(' '), 'partial_absent_workduration_min', $_POST['partial_absent_workduration_min'], false, false, true);	

				TimeDropDown_row(_(' '), 'mark_half_day_late_min', $_POST['mark_half_day_late_min'], false,  true);	
				TimeDropDown_row(_(' '), 'mark_half_day_early_go_min', $_POST['mark_half_day_early_go_min'], false, true);	
		end_outer_table(1);
		br();
		submit_center('addupdate', _("Submit"), true, '', 'default');
	}
}
div_end();
	end_form();  
end_page(); 


function get_dept_attendance_settings() {
	$sql = " SELECT dept.*, settings.*, dept.id as dep_id FROM ".TB_PREF."kv_empl_departments AS dept LEFT JOIN ".TB_PREF."kv_empl_attendance_settings AS settings ON dept.id = settings.dept_id ORDER BY settings.dept_id ";

	$res = db_query($sql, _("Can't get attendance Settings"));
	$final = [];
	$vj= $this_dept_id = 0 ;
	while($row = db_fetch($res)){
		// if($row['dept_id'] != null){
		// 	$final[$row['dept_id']][$row['option_name']] = $row['option_value'];
		// 	$final[$row['dept_id']]['name'] = $row['description'];
		// }
		// else{
			$final[$row['dep_id']][$row['option_name']] = $row['option_value'];
			$final[$row['dep_id']]['name'] = $row['description'];
		// }
		
	}
	return $final;
}
?>
<style>select { width: auto !important; }</style>
