<?php
	set_time_limit(0);
	require "commonincludefiles.php";
    require "cronjob-sms-function.php";
		
 	$arr = array();
 	$total = 0;       
	$str_msg            = "<StudentName> has attend <PresentLecture> lecture out of <TotalLecture> lecture to college today. <CurrentDate>";
        $msgprefix          = "Dear Parents,";
	$msgsuffix          = "From Image ETI.";
	$message_to         = "present_day";
	$present_day        = date("Y-m-d");
        
        
        
        
        $year_id=0;
        $objYearMgmt = new YearMgmt();
        $year_id = $objYearMgmt->get_year_id();       
					
	$SQL_STUDENT = "
		SELECT
			sm.student_id,
                        CONCAT(student_surname,' ',student_firstname,'. ',SUBSTR(student_lastname,1,1),'.') AS student_name,
			sd.cur_branchid,
			sd.cur_semesterid,
			sd.batch_id,
			sd.cur_divison,						
			student_mobilestudents,
			student_mobileparents
		FROM
			cms_studentmaster sm,
			cms_studentdetail sd,
			cms_branchmaster bm,
			cms_semestermaster sem
		WHERE
			sm.student_id = sd.student_id AND
			sd.cur_branchid = bm.branch_id AND
			sm.student_active = 1 AND			
			sd.cur_semesterid = sem.semester_id AND
			sd.year_id = ".$year_id;
			
	$SQL_CONDITION = "";
		
	$SQL_STUDENT .= $SQL_CONDITION;
			
	$RESULT_STUDENT = dbQuery($SQL_STUDENT);
	$total = dbRows($RESULT_STUDENT);
	$StudentData = array();	
	$i=0;
        
	if($total > 0)
	{
		while($RECORD_STUDENT = dbArray($RESULT_STUDENT)){
			
			$student_id         = $RECORD_STUDENT["student_id"];
			$student_name       = $RECORD_STUDENT["student_name"];
			$branch_id          = $RECORD_STUDENT["cur_branchid"];
			$semester_id        = $RECORD_STUDENT["cur_semesterid"];									
			$batch_id           = $RECORD_STUDENT["batch_id"];
			$div_id             = $RECORD_STUDENT["cur_divison"];
							
			if($message_to == "present_day"){								
                            $str_temp = str_replace("<StudentName>",$student_name,$str_msg);
                            $str_temp = getPresentData($str_temp,$branch_id,$semester_id,$div_id,$student_id,$present_day,$batch_id,$year_id);
                            
                            if($str_temp != "skip student"){		
                                $temparray["message"] = $str_temp;				
                                $temparray["student_mobilestudents"]  = $RECORD_STUDENT["student_mobilestudents"];
                                $temparray["student_mobileparents"]  = $RECORD_STUDENT["student_mobileparents"];
                                $StudentData[$i] = $temparray;   
                                $i++;
                            }                            
			}						
		}
	}
        
        /* CODE TO SEND SMS AND MAKE ENTERY IN TRANSACTION MASTER AND DETAIL TABLE START*/
        
	$objMaster = new TransactionMgmt();
	$objDetail = new TransactionDetailMgmt();
	$avialabelbal = getBalance($SmsUserName,$SmsPassword,$SMSGetbalance);
	
	for($i=0; $i<count($StudentData); $i++) {
        //for($i=0; $i<1; $i++) {            

			$msg = $msgprefix." ".trim($StudentData[$i]["message"])." ".$msgsuffix;
			if($StudentData[$i]["student_mobilestudents"] != "")
			{
				$mobileno_students = $StudentData[$i]["student_mobilestudents"];
			}
			if($StudentData[$i]["student_mobileparents"] != "")
			{
				$mobileno_parents = $StudentData[$i]["student_mobileparents"];
			}
					
			/* START DATA SAVE PROCESS */
			try{
				$objMaster->user_message 	 = $msg;	
				$objMaster->user_id		 = "1";
				$objMaster->user_lastbal 	 = $avialabelbal;
				$objMaster->user_usedbal	 = "";
				$objMaster->user_availbal 	 = "";
				$objMaster->add();	
				
                                
                                if($PresentSMSS==1) {
                                    if($mobileno_students != ""){
					$response=sendSms($SmsUserName,$SmsPassword,$SmsSendapi,$mobileno_students,$msg);
					$status=split(" | ",$response);					
					if(trim($status[0]) == "success"){
						$objDetail->trans_id 		 = $objMaster->trans_id;
						$objDetail->sms_mobileno	 = $mobileno_students;	
                                                $objDetail->sms_status           = $status[0];                                              
						$objDetail->sms_apimessageid     = 0;
						$objDetail->sms_credit	   	 = 1;
						$objDetail->add();
						$avialabelbal--;
					}
					
                                    }
                                }
				
				if($PresentSMSP==1) {
                                    if($mobileno_parents != ""){
                                            $response=sendSms($SmsUserName,$SmsPassword,$SmsSendapi,$mobileno_parents,$msg);
                                            $status=split(" | ",$response);					
                                            if(trim($status[0]) == "success"){
                                                    $objDetail->trans_id 		 = $objMaster->trans_id;
                                                    $objDetail->sms_mobileno	 = $mobileno_parents;							   
                                                    $objDetail->sms_status           = $status[0];                                                                                               
                                                    $objDetail->sms_apimessageid     = 0;
                                                    $objDetail->sms_credit	   	 = 1;
                                                    $objDetail->add();
                                                    $avialabelbal--;
                                            }

                                    }
                                }
				$mobileno_students = "";
				$mobileno_parents = "";
			}
			catch(Exception $e){
  				echo '{"success":"false","msg":"'.$e->getMessage().'"}';
				exit();	
 			}						
			/* END DATA SAVE PROCESS */
	}	
        
        /* CODE TO SEND SMS AND MAKE ENTERY IN TRANSACTION MASTER AND DETAIL TABLE END*/	        
?>