<?php
// default config setting
$boolean_ssl_lock = true ;

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

$current_month         = date( "m", time() ) ;
$date_from             = date( "Y-m-01", strtotime( "-3 months" ) ) ;
$date_to               = date( "Y-m-t", strtotime( "-1 months" ) ) ;
$last_month 		   = date( "Y-m", strtotime( $date_to ) ) ;
$last_monthday  	   = $last_month . '-01' ;

// running season report, calculate the report base on month
// 1 ( 10,11,12 )
// 4 ( 1,2,3 )
// 7 ( 4,5,6 )
// 10 ( 7,8,9 )
if ( $current_month == 1 || $current_month == 4 || $current_month == 7 || $current_month == 10 || $current_month == 2 ){


	// get setting point
	$array_setting_point   = [ 'seasonly-improvement' => 0, 'seasonly-summary' => 0 ] ;
	$select_setting_point  = $mysqli->query( "SELECT point_type, point_value FROM setting_point
											  WHERE deleted_at IS NULL AND point_from = 'staff_point_monthly_report' AND point_type IN ( 'seasonly-improvement', 'seasonly-summary' ) AND difficulty = 'normal' " ) ;
	if ( $select_setting_point->num_rows > 0 ){
		while ( $row_setting_point = $select_setting_point->fetch_assoc() ){
			$array_setting_point[$row_setting_point['point_type']] = $row_setting_point['point_value'] ;
		}
	}


	// check record
	$array_record 		   = [] ;
	$select_record         = $mysqli->query( "SELECT staff_id, type FROM staff_point_monthly_report
											  WHERE deleted_at IS NULL AND type IN ( 'seasonly-improvement', 'seasonly-summary' ) AND given_date LIKE '%".$last_month."%'" ) ;

	if ( $select_record->num_rows > 0 ){
		while ( $row_record = $select_record->fetch_assoc() ){
			$array_record[$row_record['staff_id']][$row_record['type']] = $row_record['staff_id'] ;
		}
	}



	// get all staff
	$staffs 			    = [] ;
	$staffs_q 				=   $mysqli->query("SELECT staff_id, staff_idno, staff_name 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 ){
		while ( $row_staff  = $staffs_q->fetch_assoc() ){
			$staffs[]  	    = $row_staff ;
		}
	}

	// - improvement
	//   ·给出有用的suggestion就能获得额外的60分（每三个月60分）
	//   ·每个月一号计算上个月的数据（auto）
	$suggestions 		    = [] ;
	$select_suggestion      = $mysqli->query( "SELECT staff_id, count(suggestion_id) as total_suggestion FROM suggestion
											   WHERE deleted_at IS NULL AND type = 'improvement-plan' AND status = 'confirmed' AND updated_at BETWEEN '".$date_from." 00:00:00' AND '".$date_to." 23:59:59'
											   GROUP BY staff_id" ) ;
	if ( $select_suggestion->num_rows > 0 ){
		while ( $row_suggestion  = $select_suggestion->fetch_assoc() ){
			$suggestions[$row_suggestion['staff_id']] = $row_suggestion['total_suggestion'] ;
		}
	}





 
	// start running report
	foreach ( $staffs as $kstaff => $vstaff ){

		$staff_id  = $vstaff['staff_id'] ;


		// adjustment given (compliment)
		if ( $array_record[$staff_id]['seasonly-improvement']   == null ){
			$get_suggestion = $suggestions[$staff_id] ;
			if ( $get_suggestion != null ){
				if ( $get_suggestion >= 1 ){
				
					$point_value = $array_setting_point['seasonly-improvement'] ;

					$remark      = 'System auto given seasonly improvement point ( '.$point_value.' points )' ;

					$mysqli->query( "INSERT INTO staff_point_monthly_report
									 ( staff_id, type, remark, given_date, times, given_point ) VALUES
									 ( '".$staff_id."', 'seasonly-improvement', '".$remark."', '".$last_monthday."', '1', '".$point_value."' )" ) ;

					$report_id   = $mysqli->insert_id ;

					pointMovement( 'staff_point_monthly_report', $report_id, 'seasonly-improvement', 'normal', $staff_id, 0, $remark ) ;

				}
			}
		}


		// task given
		// if ( $array_record[$staff_id]['seasonly-summary']   == null ){
		// 	$get_task       = $tasks[$staff_id] ;
		// 	if ( $get_task != null ){

		// 		if ( count($get_task) >= 3 ){
				
		// 			$point_value = $array_setting_point['monthly-task'] ;

		// 			$remark      = 'System auto given monthly task point ( '.$point_value.' points )' ;

		// 			$mysqli->query( "INSERT INTO staff_point_monthly_report
		// 							 ( staff_id, type, remark, given_date, times, given_point ) VALUES
		// 							 ( '".$staff_id."', 'monthly-task', '".$remark."', '".$last_monthday."', '1', '".$point_value."' )" ) ;

		// 			$report_id   = $mysqli->insert_id ;

		// 			pointMovement( 'staff_point_monthly_report', $report_id, 'monthly-task', 'normal', $staff_id, 0, $remark ) ;
				
		// 		}
		// 	}
		// }

	}

}

?>