<?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			
			employee_name,
			employee_phone
		FROM
			cms_employeemaster
		WHERE
			employee_active = 'Yes'";
			
	$SQL_CONDITION = "";

	if($message_to == "BirthDay")
	{
		$SQL_CONDITION .= " AND month( employee_birthdate ) = $curmonth AND DAYOFMONTH( employee_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)){
							
				$employee_name = $RECORD_STUDENT["employee_name"];																			
                                
				if($employee_name != ""){					
          $str_temp = str_replace("<StudentName>",$employee_name,$str_msg);
					$temparray["message"] = $str_temp;					
					$temparray["employee_name"]  = $RECORD_STUDENT["employee_name"];
					$temparray["employee_phone"]  = $RECORD_STUDENT["employee_phone"];
					$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]["employee_name"].", ".trim($StudentData[$i]["message"])." ".$msgsuffix;
		if($StudentData[$i]["employee_phone"] != "")
		{
			$employee_phone = $StudentData[$i]["employee_phone"];
		}
				
		/* 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($employee_phone != ""){
				$response=sendSms($SmsUserName,$SmsPassword,$SmsSendapi,$employee_phone,$msg);
				$status=split(" | ",$response);					
				if(trim($status[0]) == "success")
				{
					$objDetail->trans_id 		     = $objMaster->trans_id;
					$objDetail->sms_mobileno	   = $employee_phone;	
          $objDetail->sms_status       = $status[0];                                              
					$objDetail->sms_apimessageid = 0;
					$objDetail->sms_credit	   	 = 1;
					$objDetail->add();
					$avialabelbal--;
				}
			}
			$employee_phone = "";
		}
		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*/	
?>