<?php
/** ensure this file is being included by a parent file */
defined( '_VALID_' ) or die( 'Direct Access to this location is not allowed.' );
$today = date("Y-m-d");
//ส่วนหัว
$message_work = "";
$list='1';
$sql = "select * from person_position order by position_code";
$dbquery = mysqli_query($connect,$sql);
While ($result = mysqli_fetch_array($dbquery)){
$position_ar[$result['position_code']]=$result['position_name'];
}
//ส่วนแสดงหลัก
$sql_person = "select * from person_main where status='0'";
$dbquery_person=mysqli_query($connect,$sql_person);
While ($result_person = mysqli_fetch_array($dbquery_person)){
$person_id = $result_person['person_id'];
$sql_work = "select * from work_main where work_date='$today' and person_id='$person_id' ";
$dbquery_work = mysqli_query($connect,$sql_work);
$result_work = mysqli_fetch_array($dbquery_work);
$work_ar[$person_id]=$result_work['work'];
}
$N=1;
$sql = "select * from person_main where status='0' order by department,position_code,person_order";
$dbquery = mysqli_query($connect,$sql);
While ($result = mysqli_fetch_array($dbquery)){
$id = $result['id'];
$person_id = $result['person_id'];
$prename=$result['prename'];
$name= $result['name'];
$surname = $result['surname'];
$position_code= $result['position_code'];
$department= $result['department'];
//check การลา
$sql_la="select * from la_main where (la_start<='$today' and '$today'<=la_finish) and person_id='$person_id' ";
$dbquery_la = mysqli_query($connect,$sql_la);
if($dbquery_la){
$la_num=mysqli_num_rows($dbquery_la);
if($la_num>=1){
$result_la = mysqli_fetch_array($dbquery_la);
if($result_la['la_type']==1){
$color="#ffdfe8";
}
else if($result_la['la_type']==2){
$color="#ffffdf";
}
else if($result_la['la_type']==3){
$color="#ffdfff";
}
else if($result_la['la_type']==4){
$color="#dff2ff";
}
$sql_cancel="select * from la_cancel where (cancel_la_start<='$today' and '$today'<=cancel_la_finish) and person_id='$person_id' ";
$dbquery_cancel = mysqli_query($connect,$sql_cancel);
if($dbquery_cancel){
$la_num_cancel=mysqli_num_rows($dbquery_cancel);
if($la_num_cancel>=1){
$color=$color2;
}
}
}
}
$check_index2="";
$check_index3="";
$check_index4="";
$check_index5="";
$check_index6="";
$check_index7="";
if(!isset($_GET['index'])){
$_GET['index']="";
}
//if($_GET['index']==2){
//$check_index1="checked";
//}
if($work_ar[$person_id]==2){
$check_index2="ไปราชการ 1";
$color="#dfffff";
}
else if($work_ar[$person_id]==3){
$check_index3="ลาป่วย";
$color="#ffdfe8";
}
else if($work_ar[$person_id]==4){
$check_index4="ลากิจ";
$color="#FFFF00";
}
else if($work_ar[$person_id]==5){
$check_index5="ลาพักผ่อน";
$color="#0099FF";
}
else if($work_ar[$person_id]==6){
$check_index6="ลาคลอด";
$color="#FF00FF";
}
else if($work_ar[$person_id]==7){
$check_index7="ลาอื่นๆ";
}
//เช็คการไปราชการลา la_type 1="ลาป่วย" 2="ลากิจ" 3="ลาคลอด" 4="ลาพักผ่อน"
if($check_index3!="ลาป่วย" && $check_index4!="ลากิจ" && $check_index5!="ลาพักผ่อน" && $check_index6!="ลาคลอด" && $check_index7!="ลาอื่นๆ"){
//echo "ที่หนึ่งด้านบน เช็คว่าไม่ว่าง";
$sql_la="SELECT
la_main.id AS lamid ,
la_main.la_type AS la_type
FROM
la_main
WHERE
la_main.person_id = $person_id
and '$today' BETWEEN la_main.la_start AND la_main.la_finish AND
la_main.commander_grant = 1 ";
$dbquery_la = mysqli_query($connect,$sql_la);
While ($result_la = mysqli_fetch_array($dbquery_la))
{
//echo "ที่สอง เช็คการลา";
if(isset($result_la['lamid'])){
$la_type=$result_la['la_type'];
//เช็คประเภทการลา
$la_type = $result_la['la_type'];
if($la_type==1){
$check_index3="ลาป่วย";
$color="#ffdfe8";
}
else if($la_type==2){
$check_index4="ลากิจ";
$color="#ffffdf";
}
else if($la_type==3){
$check_index6="ลาคลอด";
$color="#ffdfff";
}
else if($la_type==4){
$check_index5="ลาพักผ่อน";
$color="#dff2ff";
}else{
$check_index7="ลาอื่นๆ";
}
$sql_lac="SELECT
la_cancel.id AS lacid ,
la_cancel.la_type AS la_ctype
FROM
la_cancel
WHERE
la_cancel.person_id = $person_id
and '$today' BETWEEN la_cancel.cancel_la_start AND la_cancel.cancel_la_finish AND
la_cancel.commander_grant = '1' and la_cancel.la_type='$la_type' ";
$dbquery_lac = mysqli_query($connect,$sql_lac);
While ($result_lac = mysqli_fetch_array($dbquery_lac))
{
//echo "ที่สาม เช็คยกเลิกการลา";
if(isset($result_lac['lacid'])){
$la_ctype = $result_lac['la_ctype'];
if($la_ctype==1){
$check_index3="";
}
else if($la_ctype==2){
$check_index4="";
}
else if($la_ctype==3){
$check_index6="";
}
else if($la_ctype==4){
$check_index5="";
}else{
$check_index7="";
}
}
}
}
}
}
//echo "ประเภทลา:".$la_type;
//echo "ประเภทยกเลิกลา:".$la_ctype;
//เช็คการไปราชการ
if($check_index2!="ไปราชการ"){
$sql_permis="SELECT permission_main.id,permission_main.ref_id,permission_main.subject,permission_main.place
FROM
permission_main
INNER JOIN permission_date ON permission_main.ref_id = permission_date.ref_id
WHERE
permission_date.date = '$today'
and permission_date.person_id='$person_id'
and permission_main.grant_x='1' ";
$dbquery_permis = mysqli_query($connect,$sql_permis);
While ($result_permis = mysqli_fetch_array($dbquery_permis))
{
if(isset($result_permis['id'])){
$place = "(".$result_permis['place'].")";
//$place= substr($place_item,0,25);
$check_index2="ไปราชการ";
$color="#dfffff";
}
}
//เพิ่มเงื่อนไขกรณีมีผู้ขอราชการไปพร้อมด้วย
$sql_follow="SELECT permission_main.id,permission_main.person_id
FROM
permission_main
INNER JOIN permission_follower ON permission_follower.ref_id = permission_main.ref_id
INNER JOIN permission_date ON permission_date.ref_id = permission_main.ref_id
WHERE
permission_date.date = '$today'
and permission_follower.person_id='$person_id'
and permission_main.grant_x='1' ";
$dbquery_follow = mysqli_query($connect,$sql_follow);
While ($result_follow = mysqli_fetch_array($dbquery_follow))
{
if(isset($result_follow['id'])){
$sql_follow_main = "select * from person_main where person_id=$result_follow[person_id] ";
$dbquery_follow_main = mysqli_query($connect,$sql_follow_main);
$result_follow_main = mysqli_fetch_array($dbquery_follow_main) ;
//$check_index2="ไปราชการกับ/$result_follow_main[name]";
$check_index2="ไปกับ/$result_follow_main[name]";
$place = "";
$color="#dfffff";
}
}
}
//แสดงผล
if($check_index2!='' or $check_index3!='' or $check_index4!='' or $check_index5!='' or $check_index6!='' or $check_index7!=''){
//$message_work .= $list.".".$prename."".$name." ".$surname." ";
$message_work .= $list.".".$name." ".$surname ;
// if(isset($position_ar[$position_code])){
// $message_work .= $position_ar[$position_code]."\n";
// }
if ($check_index2 != ''){ $message_work .= " ".$check_index2."".$place."\n" ; }
if ($check_index3 != ''){ $message_work .= " ".$check_index3."\n"; }
if ($check_index4 != ''){ $message_work .= " ".$check_index4."\n"; }
if ($check_index5 != ''){ $message_work .= " ".$check_index5."\n"; }
if ($check_index6 != ''){ $message_work .= " ".$check_index6."\n"; }
if ($check_index7 != ''){ $message_work .= " ".$check_index7."\n"; }
$list++;
}
$N++;
}
$today=date("Y-m-d");
$sql_del = "delete FROM line_notify WHERE date_line!='$today' AND module_line='work' ";
$dbquery_del = mysqli_query($connect,$sql_del);
$sql="select * from line_group where active_line='1' and module_line='work' ";
$dbquery = mysqli_query($connect,$sql);
$access_token_work = '';
while ( $result = mysqli_fetch_array($dbquery) ){
$subject = $result['module_linedesc'];
$access_token_work .= $comma_work.$result['token_line'];
$sticker_package_id = $result['sticker_package_id'];
$sticker_id = $result['sticker_id'];
$comma_work=",";
}
$line_api = 'https://notify-api.line.me/api/notify';
$str = "$subject\n$message_work"; //ข้อความที่ต้องการส่ง สูงสุด 1000 ตัวอักษร
$image_thumbnail_url = ''; // ขนาดสูงสุด 240×240px JPEG
$image_fullsize_url = ''; // ขนาดสูงสุด 1024×1024px JPEG
$message_data = array(
'message' => $str,
'imageThumbnail' => $image_thumbnail_url,
'imageFullsize' => $image_fullsize_url,
'stickerPackageId' => $sticker_package_id,
'stickerId' => $sticker_id
);
$sql = "SELECT * FROM line_notify WHERE date_line='$today' AND module_line='work' ";
$query = mysqli_query($connect,$sql);
$num_rows = mysqli_num_rows($query);
if ($num_rows == '0'){
$sql = "INSERT INTO line_notify (date_line, module_line, token_line) VALUE ('$today', 'work', '$access_token_work') ";
$query = mysqli_query($connect,$sql);
$access_token_work_array = explode("," , $access_token_work);
for ($i_work=0; $i_work<=count($access_token_work_array); $i_work++){
$access_token_work = $access_token_work_array[$i_work];
switch ($i_work){
case '1' :
$result = send_notify_work1 ($line_api, $access_token_work, $message_data);
break;
case '2' :
$result = send_notify_work2 ($line_api, $access_token_work, $message_data);
break;
case '3' :
$result = send_notify_work3 ($line_api, $access_token_work, $message_data);
break;
case '4' :
$result = send_notify_work4 ($line_api, $access_token_work, $message_data);
break;
default :
$result = send_notify_work ($line_api, $access_token_work, $message_data);
break;
}
print_r($result);
//print_r($message_data);
}
//echo "/".$subject." ";
}
function send_notify_work ($line_api, $access_token_work, $message_data){
$headers = array('Method: POST', 'Content-type: multipart/form-data', 'Authorization: Bearer '. $access_token_work );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $line_api);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
// Check Error
if(curl_error($ch)){
$return_array = array( 'status' => '000: send fail', 'message' => curl_error($ch) );
}else{
$return_array = json_decode($result, true);
}
curl_close($ch);
//return $return_array;
}
function send_notify_work1 ($line_api, $access_token_work, $message_data){
$headers = array('Method: POST', 'Content-type: multipart/form-data', 'Authorization: Bearer '. $access_token_work );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $line_api);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
// Check Error
if(curl_error($ch)){
$return_array = array( 'status' => '000: send fail', 'message' => curl_error($ch) );
}else{
$return_array = json_decode($result, true);
}
curl_close($ch);
//return $return_array;
}
function send_notify_work2 ($line_api, $access_token_work, $message_data){
$headers = array('Method: POST', 'Content-type: multipart/form-data', 'Authorization: Bearer '. $access_token_work );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $line_api);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
// Check Error
if(curl_error($ch)){
$return_array = array( 'status' => '000: send fail', 'message' => curl_error($ch) );
}else{
$return_array = json_decode($result, true);
}
curl_close($ch);
//return $return_array;
}
function send_notify_work3 ($line_api, $access_token_work, $message_data){
$headers = array('Method: POST', 'Content-type: multipart/form-data', 'Authorization: Bearer '. $access_token_work );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $line_api);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
// Check Error
if(curl_error($ch)){
$return_array = array( 'status' => '000: send fail', 'message' => curl_error($ch) );
}else{
$return_array = json_decode($result, true);
}
curl_close($ch);
//return $return_array;
}
function send_notify_work4 ($line_api, $access_token_work, $message_data){
$headers = array('Method: POST', 'Content-type: multipart/form-data', 'Authorization: Bearer '. $access_token_work );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $line_api);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
// Check Error
if(curl_error($ch)){
$return_array = array( 'status' => '000: send fail', 'message' => curl_error($ch) );
}else{
$return_array = json_decode($result, true);
}
curl_close($ch);
//return $return_array;
}
?>