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

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

$last_month            = date( "Y-m", strtotime( "-1 month" ) ) ;
$last_monthday  	   = $last_month . '-01' ;



// get setting point
$array_setting_point   = [ 'monthly-compliment' => 0, 'monthly-task' => 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 ( 'monthly-compliment', 'monthly-task' ) 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 ( 'monthly-compliment', 'monthly-task' ) 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 ;
	}
}


// - compliment
//   ·一个月内被加了最少五次分，并且同时没有被扣过任何的分数，就能拿到额外的30分（每个月30分）
//   ·每个月一号计算上个月的数据（auto）
$adjustments 		    = [] ;
$select_adjustment      = $mysqli->query( "SELECT a.staff_id, b.adjustment_type, count(b.adjustment_type) as total_adjustment_type FROM staff_adjustment_point a
										   LEFT JOIN staff_adjustment b ON ( a.adjustment_id = b.adjustment_id )
										   WHERE a.deleted_at IS NULL AND b.deleted_at IS NULL AND b.created_at LIKE '%".$last_month."%'
										   GROUP BY a.staff_id, b.adjustment_type" ) ;
if ( $select_adjustment->num_rows > 0 ){
	while ( $row_adjustment  = $select_adjustment->fetch_assoc() ){
		$adjustments[$row_adjustment['staff_id']][$row_adjustment['adjustment_type']] = $row_adjustment['total_adjustment_type'] ;
	}
}




// - task
//   ·一个月内最少完成三个task（在deadline之前），就能获得50分（每个月50分）
//   ·每个月一号计算上个月的数据（auto）
$tasks 					= [] ;
$select_task 		    = $mysqli->query( "SELECT a.task_id, a.assigned_by, ( SELECT GROUP_CONCAT( DISTINCT(staff_id) ) FROM task_joinstaff WHERE task_id = a.task_id ) as executed_by FROM task a
										   WHERE a.deleted_at IS NULL AND a.status = 'approved' AND a.is_late = 'no' AND a.updated_at LIKE '%".$last_month."%'" ) ; 
if ( $select_task->num_rows > 0 ){
	while ( $row_task = $select_task->fetch_assoc() ){
		$executed_by  = $row_task['executed_by'] ;
		$executeds    = ( $executed_by != null ? explode( ',', $executed_by ) : [] ) ;

		if ( $row_task['assigned_by'] > 0 ){
			if ( !in_array( $row_task['assigned_by'], $executeds ) ){
				$tasks[$row_task['assigned_by']][] = $row_task['task_id'] ;
			}
		}

		foreach ( $executeds as $kexecuted => $vexecuted ){
			$tasks[$vexecuted][] = $row_task['task_id'] ;
		}
		
	}
}



// - 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 LIKE '%".$last_month."%'
// 										   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]['monthly-compliment']   == null ){
		$get_adjustment = $adjustments[$staff_id] ;
		if ( $get_adjustment != null ){
			if ( $get_adjustment['minus'] == null && $get_adjustment['plus'] != null ){
				if ( $get_adjustment['plus'] >= 5 ){
				
					$point_value = $array_setting_point['monthly-compliment'] ;

					$remark      = 'System auto given monthly compliment 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-compliment', '".$remark."', '".$last_monthday."', '1', '".$point_value."' )" ) ;

					$report_id   = $mysqli->insert_id ;

					pointMovement( 'staff_point_monthly_report', $report_id, 'monthly-compliment', 'normal', $staff_id, 0, $remark ) ;

				}
			}
		}
	}


	// task given
	if ( $array_record[$staff_id]['monthly-task']   == 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 ) ;
			
			}
		}
	}

}



?>