

<?php
set_time_limit(0);
	require "commonincludefiles.php";
	require "data/sendingsms_studentpresant.php";
	error_reporting(1);

	if ($_SESSION['VedantnrCollegeId']!="" && isset($_SESSION['VedantnrCollegeId'])) {
		$message = $_REQUEST['message']; 
		$mobilestudents = $_REQUEST['mobilestudents']; 
		$mobileparents = $_REQUEST['mobileparents']; 
		$msgprefix	= $_REQUEST['msgprefix']; 
		$msgsuffix	= $_REQUEST['msgsuffix']; 
		$message = json_decode(stripslashes($message));
		$mobilestudents = json_decode(stripslashes($mobilestudents));
		$mobileparents = json_decode(stripslashes($mobileparents));
		
		$objMaster = new TransactionMgmt();
		$objDetail = new TransactionDetailMgmt();
		$avialabelbal = getBalance($SmsUserName,$SmsPassword,$SMSGetbalance);
		
		for($i=0; $i<count($message); $i++)
		{
			$msg = $msgprefix." ".trim($message[$i]).$msgsuffix;
			if($mobilestudents[$i] != "")
			{
				$mobileno_students = $mobilestudents[$i];
			}
			if($mobileparents[$i] != "")
			{
				$mobileno_parents = $mobileparents[$i];
			}
					
			/* START DATA SAVE PROCESS */
			try{
				$objMaster->user_message 	 = $msg;	
				$objMaster->user_id              = $_SESSION['aUserId'];
				$objMaster->user_lastbal 	 = $avialabelbal;
				$objMaster->user_usedbal	 = "";
				$objMaster->user_availbal 	 = "";
				$objMaster->add();	
				
				if($mobileno_students != ""){
					$response=sendSms($SmsUserName,$SmsPassword,$SmsSendapi,$mobileno_students,$msg);					
					$status=explode(" | ",$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=explode(" | ",$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 */
                }
		echo '{"success":"true","msg":"Message Sent to selected student."}';
	}
?>