<?php
include '../connect/cms-config.php' ;
include '../requires/function.php' ;


$staffs_q = $mysqli->query("SELECT staff_id, staff_star, staff_point_achievement, staff_point, staff_achievement FROM staff
                            WHERE deleted_at IS NULL AND ( staff_date_resigned >= '".date("Y-m-d",time())."' OR staff_date_resigned = '0000-00-00' OR staff_date_resigned IS NULL )") ;

if ( $staffs_q->num_rows > 0 ){

	$yesterday_day = date( 'Y-m-d', strtotime( '-1 days' ) ) ;
	$year_end      = date( 'Y', strtotime( '-1 days' ) ) ;
	$is_year_end   = ( date( 'm-d', strtotime( '-1 days' ) ) == '12-31' ? true : false ) ;
	$is_year_end   = true ;

	while ( $staff = $staffs_q->fetch_assoc() ){

		$staff_id 				 = $staff['staff_id'] ;
		$staff_star 			 = $staff['staff_star'] ;
		$staff_point_achievement = $staff['staff_point_achievement'] ;
		$staff_achievement       = $staff['staff_achievement'] ;
		$staff_point 			 = $staff['staff_point'] ;

		// $mysqli->query( "INSERT INTO staff_monthly_achievement
		// 				 ( reported_at, staff_id, staff_point_achievement, staff_star, staff_achievement ) VALUES
		// 				 ( '".$yesterday_day."', '".$staff_id."', '".$staff_point_achievement."', '".$staff_star."', '".$staff_achievement."' )" ) ;

		// $staff_point_achievement = ( $staff_point_achievement <= 0 ? $staff_point_achievement : 0 ) ;

		// $mysqli->query( "UPDATE staff SET
		// 				 staff_star  			 = '0',
		// 				 staff_point_achievement = '".$staff_point_achievement."'
		// 				 WHERE staff_id 		 = '".$staff_id."'" ) ;

		echo '----' ;
		echo "\n" ;
		echo $staff_id ;
		echo "\n" ;


		if ( $is_year_end ){
			$mysqli->query( "INSERT INTO staff_point_movement_cutoff
							 ( `staff_id`, `cutoff_year`, `amount` ) VALUES
							 ( '".$staff_id."', '".$year_end."', '".$staff_point."' )" ) ;

			$last_cutoff_id  	 = $mysqli->insert_id ;

			if ( $staff_point < 0 || $staff_point > 0 ){
				$point_amount    = -( $staff_point ) ;

				echo $staff_point ;
				echo "\n" ;
				echo $point_amount ;
				echo "\n" ;
				echo "\n" ;
				echo "\n" ;
				echo "\n" ;

				pointMovement( 'staff_point_movement_cutoff', $last_cutoff_id, 'reset', 'normal', $staff_id, $point_amount, 'Year end reset point to zero' ) ;
			}
		}

	}
}
?>