<?php
	require "commonincludefiles.php";
	require "cronjob-sms-function.php";
		
 	$arr = array();
 	$total = 0;
	$str_msg                = "Wishing You a Very Happy Birthday! May God bless You.";		
	$msgprefix 		= "Dear ";
	$msgsuffix 		= "From Image ETI.";
	$message_to             = "BirthDay";				
	$bday 			= date("Y-m-d");
	$curmonth 		= date("m");
	$curdate		= date("d");	 
        
        $year_id=0;
        $objYearMgmt = new YearMgmt();
        $year_id = $objYearMgmt->get_year_id();     
	
	$SQL_STUDENT = "SELECT			
			CONCAT(student_surname,' ',student_firstname,'. ',SUBSTR(student_lastname,1,1),'.') AS student_name,
			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 = "";

	if($message_to == "BirthDay")
	{
		$SQL_CONDITION .= " AND month( student_birthdate ) = $curmonth AND DAYOFMONTH( student_birthdate ) = $curdate";	
		
		$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_name = $RECORD_STUDENT["student_name"];																			
                                
				if($student_name != ""){					
                                        $str_temp = str_replace("<StudentName>",$student_name,$str_msg);
					$temparray["message"] = $str_temp;					
					$temparray["student_name"]  = $RECORD_STUDENT["student_name"];
					$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."".$StudentData[$i]["student_name"].", ".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($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($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*/	
?>