<?php
/****************************************
/*  Author 	: Kvvaradha
/*  Module 	: Extended HRM
/*  E-mail 	: admin@kvcodes.com
/*  Version : 1.0
/*  Http 	: www.kvcodes.com
*****************************************/

$page_security = 'SA_OPEN';
$path_to_root="../../..";
include($path_to_root . "/includes/session.inc");
include($path_to_root . "/modules/ExtendedHRM/includes/Payroll.inc" );
include($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/db_pager.inc");

$js = get_js_date_picker();	
page(_("Daily Attendance Inquiry"), false, false, "", $js);
 check_db_has_employees(sprintf(_("There is no employee in this system. Kindly Open '%s' to update it"), "<a href='".$path_to_root."/modules/ExtendedHRM/manage/employees.php'>"._("Add And Manage Employees")."</a>"));
 
 simple_page_mode(true);
//----------------------------------------------------------------------------------------
	$new_item = get_post('selected_id')=='' || get_post('cancel') ;
	$month = get_post('month','');
	$year = get_post('year','');
	if (isset($_GET['selected_id'])){
		$_POST['selected_id'] = $_GET['selected_id'];
	}
	$selected_id = get_post('selected_id');
	 if (list_updated('selected_id')) {
		$_POST['empl_id'] = $selected_id = get_post('selected_id');
	    $Ajax->activate('details');
	}
	if (isset($_GET['month'])){
		$_POST['month'] = $_GET['month'];
	}
	if (isset($_GET['year'])){
		$_POST['year'] = $_GET['year'];
	}
	
	if (list_updated('month')) {
		$month = get_post('month');   
		$Ajax->activate('details');
	}

//$month = date("m");
//----------------------------------------------------------------------------------------

	start_form(true);
		start_table(TABLESTYLE_NOBORDER);
			echo '<tr>';

				date_cells(_("Date") . ":", 'attendance_date', null, null, 0,0,0, null, true);
			 	department_list_cells(_("Select a Department")." :", 'selected_id', null,	_("All Departments"), true, check_value('show_inactive'));
				empl_workcenter_list_cells(_("Work Center"), 'work_centre', null, true, true);

				/*kv_fiscalyears_list_cells(_("Fiscal Year").":", 'year', null, true);
			 	kv_current_fiscal_months_list_cell(_("Months"), "month", null, true);
			 	department_list_cells(_("Select a Department")." :", 'selected_id', null,	_("All Departments"), true, check_value('show_inactive'));
				
				if ($dim >= 1){
					dimensions_list_cells(_("Dimension")." 1", 'dimension_id', null, true, " ", false, 1, true);
					//if ($dim > 1)
						//dimensions_list_cells(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2, true);
					//else
						//hidden('dimension2_id', 0);
				}
				if ($dim < 1)
					hidden('dimension_id', 0);
				//if ($dim < 2)
					//hidden('dimension2_id', 0);*/
			
				$new_item = get_post('selected_id')=='';
		 	echo '</tr>';
	 	end_table(1);

	 	if (list_updated('selected_id') || list_updated('work_centre')) {
			//$Ajax->activate('month');
			//$Ajax->activate('attendance_date');
			$Ajax->activate('details');
			//$Ajax->activate('selected_id');		
			//set_focus('month');
		}
	/*	if($month==null){			 
			$month = $_POST['month'];
		}
		if($year==null){			 
			$year = $_POST['year'];
		}
		//echo $month;
*/
		//$hrmsetup = get_hrm_option();
		//$months_with_years_list = kv_get_months_with_years_in_fiscal_year($year);
 		//$ext_year = date("Y", strtotime($months_with_years_list[(int)get_post('month')]));
		//$total_days =  date("t", strtotime($ext_year."-".$month."-01"));
		div_start('details');
		
			$selected_empl = kv_get_employees_list_based_on_dept($selected_id, false, get_post('work_centre'));
			//$selected_empl_attend_details=kv_get_attend_details($selected_id);
			$present = $absent = $leave = $onduty = 0 ;
			if(db_num_rows($selected_empl) > 0 ) {
				start_table(TABLESTYLE2 , "width='50%'");	 

				echo "<tr><td rowspan=2 class='tableheader'>" . _("Empl ID") . "</td><td rowspan=2 class='tableheader'>" . _("Empl Name") . "</td><td rowspan=2 class='tableheader'>" . _("Attendance") . "</td>					
					</tr><tr>";
				//$weekly_off = $hrmsetup['weekly_off'];
				while($row = db_fetch($selected_empl)){											
					
					$sql = "SELECT * FROM ".TB_PREF."kv_empl_attendance WHERE empl_id=".db_escape($row['empl_id'])." AND a_date = '".date2sql(get_post('attendance_date'))."' GROUP BY empl_id ORDER BY empl_id ASC";
					$res = db_query($sql, _("Can't get attendance"));
					while($row2 = db_fetch_assoc($res)){
						$attendance_code  = $row2['code'];
						if($row2['code'] == 'A')
							$absent++;
						elseif($row2['code'] == 'P')
							$present++;
						elseif($row2['code'] == 'AL' || $row2['code'] == 'CL' || $row2['code'] == 'SL' || $row2['code'] == 'ML' || $row2['code'] == 'L')
							$leave++;
						elseif($row2['code'] == 'OD')
							$onduty++;
					}	
					echo '<tr> <td>'.$row['empl_id'].'</td><td>'.$row['empl_name'].'</td><td>'.(isset($attendance_code) ? $attendance_code : '' ).'</td></tr>';
				}							
			end_table();	
			}
			br(2);
			start_table(TABLESTYLE2, "width='30%'");
				label_row(_("Presents"), $present);
				label_row(_("Absents"), $absent);
				label_row(_("Leave"), $leave);
				label_row(_("On duty"), $onduty);
			end_table();	
		div_end();
	end_form();?>
	<style>
	#details table.tablestyle { min-width:1330px; } 
	#details table.tablestyle td { text-align:center; border: 1px solid #cccccc; padding: 1px;  } 
	#details table.tablestyle td span {font-size:10px;display:block;  } 
	table tr td {   line-height: 1.5em;}
	</style><?php 
end_page(); ?>