<?php
{
	$emp_id = 6;
	$getjoiningdate = $db->get_col ( "employees", array ("e_id" => $emp_id ), 'j_date' );
	
	$get_month = $getjoiningdate [0]; // get the name of month in alphbet according
	                                // to date
	                                // ---------------------------------------------days
	                                // calculation
	$previous_month = date ( "Y-m-d", strtotime ( "-1 Months" ) ); // get the
	                                                               // name of month
	                                                               // in alphbet
	if ($get_month <= $previous_month) {
		$j_date = $getjoiningdate [0];
		$joining_month = date ( "F", strtotime ( $j_date ) );
		$explodedate = explode ( "-", $j_date );
		$j_date = $explodedate [2] . ' ' . $joining_month . ',' . $explodedate [0];
	} else {
		$j_date = '';
	}
	$previous_month = date ( "F", strtotime ( "-1 Months" ) ); // get the name of
	                                                           // month in alphbet
	$numeric_previousmonth = date ( "m", strtotime ( $previous_month ) ); // get
	                                                                      // the
	                                                                      // month
	                                                                      // in
	                                                                      // numeric
	                                                                      // calculate
	                                                                      // the
	                                                                      // number
	                                                                      // of
	                                                                      // days
	                                                                      // in
	                                                                      // given
	                                                                      // month
	$num_day = cal_days_in_month ( CAL_GREGORIAN, $numeric_previousmonth, date ( y ) ); // get
	                                                                                    // the
	                                                                                    // total
	                                                                                    // days
	                                                                                    // in
	                                                                                    // given
	                                                                                    // date
	$startdate = date ( 'Y' ) . '-' . $numeric_previousmonth . '-' . '01';
	$enddate = date ( 'Y' ) . '-' . $numeric_previousmonth . '-' . $num_day;
	$totaldays = $db->run ( "SELECT * FROM `attendence` where `e_id`='$emp_id' and date between '$startdate' and '$enddate';" )->fetchAll ();
	
	/*
	 * echo '<pre>'; print_r($totaldays); echo '</pre>';
	 */
	if ($totaldays) {
		$total_attendence = count ( $totaldays );
		$not_marked = $num_day - $total_attendence;
		
		$weekoff = $db->get_col ( "office", '', 'w_day' );
		$getday = unserialize ( $weekoff [0] );
		$getmonth_small = substr ( $previous_month, 0, 3 );
		$finalmonth = $feature->textstyler ( $getmonth_small, 3 );
		
		foreach ( $getday as $key => $day ) {
			$concat [] = $key;
		}
		/*
		 * calculation of present and absent
		 */
		
		$present = array ();
		$present1 = array ();
		$absent = array ();
		$absent1 = array ();
		$weekoffattendence = array ();
		foreach ( $totaldays as $row ) {
			if ($row ['firsthalf'] == 0.00 && $row ['secondhalf'] == 0.00) {
				$present [] = 1;
			}
			if ($row ['firsthalf'] == 0.00 || $row ['secondhalf'] == 0.00) {
				$present1 [] = 0.5;
			}
			if ($row ['firsthalf'] == 0.50 && $row ['secondhalf'] == 0.50) {
				$absent [] = 1;
			}
			if ($row ['firsthalf'] == 0.50 || $row ['secondhalf'] == 0.50) {
				$absent1 [] = 0.5;
			}
			$checkweekoff = date ( "l", strtotime ( $row ['date'] ) );
			if (! in_array ( $feature->textstyler ( $checkweekoff, 2 ), $concat )) {
				$weekoffattendence [] = $row ['date'];
			}
		}
		$presentcount = count ( $present );
		$presentcount1 = (count ( $present1 ) - $presentcount) * 0.5;
		$absentcount = count ( $absent );
		$absentcount1 = (count ( $absent1 ) - $absentcount) * 0.5;
		$presentday = $presentcount + $presentcount1;
		
		$absentday = $absentcount + $absentcount1;
		$extrawork = count ( $weekoffattendence );
		if (! in_array ( "sunday", $concat )) {
			$totalday .= "sunday,";
			$remainday [] = $calculateday->daysBetween ( $finalmonth . ' ' . '01' . ' ' . date ( 'Y' ), $finalmonth . ' ' . $num_day . ' ' . date ( 'Y' ), "sunday" );
		}
		if (! in_array ( "monday", $concat )) {
			$totalday .= "monday,";
			$remainday [] = $calculateday->daysBetween ( $finalmonth . ' ' . '01' . ' ' . date ( 'Y' ), $finalmonth . ' ' . $num_day . ' ' . date ( 'Y' ), "monday" );
		}
		if (! in_array ( "tuesday", $concat )) {
			$totalday .= "tuesday,";
			$remainday [] = $calculateday->daysBetween ( $finalmonth . ' ' . '01' . ' ' . date ( 'Y' ), $finalmonth . ' ' . $num_day . ' ' . date ( 'Y' ), "tuesday" );
		}
		if (! in_array ( "wednesday", $concat )) {
			$totalday .= "wednesday,";
			$remainday [] = $calculateday->daysBetween ( $finalmonth . ' ' . '01' . ' ' . date ( 'Y' ), $finalmonth . ' ' . $num_day . ' ' . date ( 'Y' ), "wednesday" );
		}
		if (! in_array ( "thursday", $concat )) {
			$totalday .= "thursday,";
			$remainday [] = $calculateday->daysBetween ( $finalmonth . ' ' . '01' . ' ' . date ( 'Y' ), $finalmonth . ' ' . $num_day . ' ' . date ( 'Y' ), "thursday" );
		}
		if (! in_array ( "friday", $concat )) {
			$totalday .= "friday,";
			$remainday [] = $calculateday->daysBetween ( $finalmonth . ' ' . '01' . ' ' . date ( 'Y' ), $finalmonth . ' ' . $num_day . ' ' . date ( 'Y' ), "friday" );
		}
		if (! in_array ( "saturday", $concat )) {
			$totalday .= "saturday,";
			$remainday [] = $calculateday->daysBetween ( $finalmonth . ' ' . '01' . ' ' . date ( 'Y' ), $finalmonth . ' ' . $num_day . ' ' . date ( 'Y' ), "saturday" );
		}
		$week_off = array_sum ( $remainday );
		
		// ----------------------------------------------Get The Department
		$dep_id = $db->get_col ( "employees", array ('e_id' => $emp_id ), 'department' );
		$dep_name = $db->get_col ( "departments", array ("id" => $dep_id [0] ), 'name' );
		// ----------------------------------------------Get The Employee Code
		$empcode = $db->get_col ( "employees", array ("e_id" => $emp_id ), 'e_code' );
		// ************************************************check the user is
		// already exist in salaryslipdata table
		$checkuser = $db->exists ( "payslip", array ("empname" => $emp_id ) );
		if ($checkuser) {
			$button = '<a href=' . $link->link ( "viewslip_table", Admin, "?empid=" . $emp_id ) . ' target="_blank" class="btn btn-small btn-info">View Previous Slip</a>';
			echo $data = $num_day . '////' . $presentday . '////' . $week_off . '////' . $absentday . '////' . $extrawork . '////' . $dep_name [0] . '////' . $empcode [0] . '////' . $j_date . '////' . $not_marked . '////' . $button;
		} else {
			echo $data = $num_day . '////' . $presentday . '////' . $week_off . '////' . $absentday . '////' . $extrawork . '////' . $dep_name [0] . '////' . $empcode [0] . '////' . $j_date . '////' . $not_marked;
		}
	}
}
?>
<?php

echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
$start_date = 08 - 01 - 2014;
$end_date = 08 - 31 - 2014;
$weekDay = "sunday";
echo $finalmonth = 'AUG';
echo $num_day = "31";
echo $calculateday->daysBetween ( $finalmonth . ' ' . '01' . ' ' . date ( 'Y' ), $finalmonth . ' ' . $num_day . ' ' . date ( 'Y' ), "saturday" );
?>