<?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' ;




$service_module        = '' ;
$services              = $mysqli->query( "SELECT a.module FROM app_service a
                                          WHERE a.deleted_at IS NULL AND a.type = 'service' AND a.module IN ( 'Hr', 'HrBranch', 'HrManual' ) AND on_off = 'on' 
                                          LIMIT 1" ) ;
   
if ( $services->num_rows > 0 ){
	$row_service       = $services->fetch_assoc() ;
	$service_module    = $row_service['module'] ;
}


	


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



switch ( $service_module ){

	case 'Hr'       :
	case 'HrBranch' :


		// get working day
		$list_workday          = [] ;
		$select_attendancelist = $mysqli->query("SELECT staff_id, list_type_remark, COUNT( list_type_remark ) as count_list_type_remark, SUM( list_work_day ) as sum_list_work_day, SUM( list_leave_day ) as sum_list_leave_day FROM staff_attendance_list
		                                         WHERE deleted_at IS NULL AND list_date LIKE '".$last_month."%'
		                                         GROUP BY staff_id, list_type_remark") ;

		if ( $select_attendancelist->num_rows > 0 ){
		    while ( $row_attendancelist = $select_attendancelist->fetch_assoc() ){
		        $list_workday[$row_attendancelist['staff_id']][$row_attendancelist['list_type_remark']] = $row_attendancelist ;
		    }
		}

		foreach ( $list_workday as $kwork => $vwork ){
			$staff_id        = $kwork ;

			$total_realwork  = floor( ( $vwork['WD'] != null ? $vwork['WD']['sum_list_work_day'] : 0 ) + 0 ) ;
			$total_workday   = 0 ;
			foreach ( [ 'WD', 'PT', 'AL', 'MC', 'UL', 'AS' ] as $ktype => $vtype ){
				if ( $vwork[$vtype] != null ){
					$total_workday += $vwork[$vtype]['count_list_type_remark'] ;
				}
			}


			if ( $array_record[$staff_id]['daily-attendance']   == null ){
				if ( $total_realwork > 0 ){
					$point_value = $array_setting_point['daily'] ;
					$point_total = ( $total_realwork * $point_value ) ;
					$point_total_minus1day = ( $point_total - $point_value ) ;

					$remark      = 'System auto given daily attendance point ( '.$total_realwork.' days * '.$point_value.' points = '.$point_total.' points )' ;

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

					$report_id   = $mysqli->insert_id ;

					pointMovement( 'staff_point_monthly_report', $report_id, 'daily', 'normal', $staff_id, $point_total_minus1day, $remark ) ;
				}
			}


			if ( $array_record[$staff_id]['monthly-attendance'] == null ){
				if ( $total_realwork > 0 && $total_realwork == $total_workday ){
					$point_value = $array_setting_point['monthly'] ;

					$remark      = 'System auto given monthly attendance point ( '.$total_workday.' days, '.$point_value.' points )' ;

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

					$report_id   = $mysqli->insert_id ;

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

		}

	break ;

	case 'HrManual' :

		$staff_ids     = [] ;
		$select_manual = $mysqli->query( "SELECT staff_id FROM staff_attendance_manual
										  WHERE deleted_at IS NULL AND attendance_date LIKE '%".$last_month."%'" ) ;
		if ( $select_manual->num_rows > 0 ){
			while ( $row_manual      = $select_manual->fetch_assoc() ){
				$staff_ids[]         = $row_manual['staff_id'] ;
			}
		}

		if ( count($staff_ids) ){
			foreach ( $staff_ids as $kstaff => $vstaff ){
				$staff_id            = $vstaff ;

				if ( $array_record[$staff_id]['monthly-attendance']   == null ){
					$point_value     = $array_setting_point['monthly'] ;

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

					$report_id   = $mysqli->insert_id ;

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

			}
		}

	break ;

}

?>