<?php 

include("parent/callMe.php");

include('Sessioncheck.php');

  



$m->where("branch_id", $_SESSION['branch']);

$smsSerivce=$m->get("edu_branch_config");



$paidByFee;

if ($brid[0]['paid_by']!=NULL) {

	$paidByFee=$brid[0]['paid_by'];

} else {

	$paidByFee=$_REQUEST['PiadBy'];

}


if(($_REQUEST['action']=='PayChallan'))

{


	$m->where ("challan_id", $_REQUEST['challan_id']);   
   $challanData=$m->get("eduportal_monthly_challan");

   $m->where ("challan_id", $_REQUEST['challan_id']);
	$challanDetailsData = $m->get("eduportal_monthly_challan_details");

	$paid_date=date("Y-m-d",strtotime($_REQUEST['paid_date']));



	// echo "<pre>"; print_r($_REQUEST); echo "</pre>";

	

	$collected_amount=intVal($_REQUEST['totalAmount']-$_REQUEST['fine']);



	$data = array(

	'collected_amount' => $collected_amount,

	'collected_fine' => $_REQUEST['fine'],

	'paid_status' => 1,

	'tutionfee_received' => $challanData[0]['tutionfee'],
	
	'transportfee_received' => $challanData[0]['transportfee'],
	
	'misc_fee_recevied' => $challanData[0]['misc_fee'],

	'paid_fee_user_id' => $_SESSION['user_id'],

	'paying_time' => date("h:i:s a"),

	'paid_by' => $paidByFee,

	'remaining_amount' => $_REQUEST['totalAmount']-$_REQUEST['totalAmount'],

	'paying_date'=>date('Y-m-d',strtotime($paid_date))

	);



	$m->where ("challan_id", $_REQUEST['challan_id']);

	$result = $m->update('eduportal_monthly_challan',$data);


	foreach ($challanDetailsData as $challanDetails) {

	$data = array(

	'date' => date("Y-m-d"),

	'fee_received' => $challanDetails['fee_amount'],

	'remaning' => ($challanDetails['fee_amount'])-($challanDetails['fee_amount'])

	);		          
	$m->where ("feetype_id", $challanDetails['feetype_id']);
	$m->where ("challan_id", $_REQUEST['challan_id']);
	$m->update('eduportal_monthly_challan_details',$data);


	}



	$m->where ("branch_id", $_SESSION['branch']);

	$m->where("student_id", $_REQUEST['student']);

   $student = $m->get("edu_student_details");



   $ClassSec=getClassName($m,$student[0]['class_id']); 





  

   $totalAmount=$_REQUEST['totalAmount'];

   $std_name=$_REQUEST['std_name'];

   $number=$_REQUEST['std_no'];



   // echo "Data added";

   $paidAmount=$collected_amount+$_REQUEST['fine'];

   $balanc=$totalAmount-($collected_amount+$_REQUEST['fine']);

		





		// now sending fee confirmation msg to parents if the sms status is YES



      $branch=$_SESSION['branch'];

		$monthId=date("m");

		$sessionID=date("Y");



		$qr="SELECT SUM(add_sms) as TaditionalSms FROM eduportal_branches_SMScredit_limit WHERE month_id='$monthId'  and branch_id='$branch' AND session_id=".$sessionID;



		$AdSMS = $m->query($qr);



	   $total_sms_monthly_used=getCurrentMonthFeeSms($m)+getCurrentMonthResultSms($m)+getCurrentMonthOtherSms($m)+getCurrentMonthAttendanceSms($m)+0;

		$sms_credit=$brid[0]['monthly_credit']+$AdSMS[0]['TaditionalSms']-$total_sms_monthly_used;

 



   if($brid[0]['sms_service']=="0" /*&& $sms_credit > 0*/) {

   



   if($sms_credit > 0) {

		

		if($_REQUEST['fee_recv_sms']=="1" || $_REQUEST['fee_recv_sms']==1 && $totalAmount>0){





		   $message =trim("Dear Parents, ".$std_name." (Class: ".$ClassSec.") has submitted the fee of (".$_REQUEST['month_name'].") Total:".$totalAmount." Paid:".$paidAmount." Balance:".$balanc." From:".$_SESSION['branch_name']);



		// sms sending function is made at the bottom 

		 $result=sendSMSFuction($number,$message,$_SESSION['brand']);

	 

		  $data = array('msg_sender' => $_SESSION['brand'],'msg_from' => $number,'msg_details' => $message,'msg_status' => "Sent",'branch_id' => $_SESSION['branch'],'fee_msg' => 1);

			$m->insert ('edu_messages_details', $data);





		}// ending fee recv if

	  



   

    // now adding user audit detail





		$username=$_SESSION['username'];

		$monthName=$_REQUEST['month_name'];



		$action="Pay Full Fee";

			$details="Paid Fee (".$std_name.") Amount ".$totalAmount." and Paid Date: ".date('d-m-Y', strtotime($_REQUEST['paid_date']))." Month = ".$monthName;







			date_default_timezone_set("Asia/Karachi");







		$data = array(



		'user_id' => $_SESSION['user_id'],



		'module_id' => 14,



		'action' => $action,



		'details' => $details,



		'date' => date("Y-m-d"),



		'month_id' => date('m'),



		'time' => date("h:i:sa"),



		'branch_id' => $_SESSION['branch'],



		);



		$m->insert('eduportal_users_auditlog_detail',$data);







		// now sending fee confirmation msg to Principle if the sms status is YES

	if($smsSerivce[0]['sms_service']=="0" && $brid[0]['double_confirmation_sms_to_principal']==1 && $totalAmount>0){





	   $collected_amount=$_REQUEST['totalAmount'];

	   $totalAmount=$_REQUEST['totalAmount'];;

	   $std_name=$_REQUEST['std_name'];

	   $number1=$_REQUEST['Owner_no'];

	    

	    $message =trim("Dear Sir, ".$std_name." has submitted the fee of (".date("M").") Total:".$totalAmount." Paid:".$collected_amount." From:".$_SESSION['branch_name']);

		

		//echo "<br>".$message;



		 $result=sendSMSFuction($number1,$message,$_SESSION['brand']);



	    $data = array('msg_sender' => $_SESSION['brand'],'msg_from' => $number1,'msg_details' => $message,'msg_status' => "Sent",'branch_id' => $_SESSION['branch'],'fee_msg' => 1);

	    $result =  $m->insert ('edu_messages_details', $data);

	 	

	}



    echo "1";

}// end if condition credit limit zero

} // sms service block 

} // ending main if



// sms sending function





function sendSMSFuction($number,$message,$from)



{



  

 $message=trim($message);





  if(substr($number,0,1)=="3"){



  $to = "92".$number;



  }else if(substr($number,0,1)=="0"){



  $to =  "92".substr($number,1);



  }



  else if(substr($number,0,2)=="92"){



  $sendmsg1 =  $number;



  }



  else{



   $to = "Invalid Number";



  }







  $username = SMSAPI_USER;



  $password = SMSAPI_PASS;



  



	if($from!="" || $from!=NULL ){



	  $from = $from;   



	}else {      



	  $from=$_SESSION['brand'];  



	}







$curl = curl_init();



$url='https://bsms.ufone.com/bsms_v8_api/sendapi-0.3.jsp?id=03348960296&message='.urlencode($message).'&shortcode=EduPortal&lang=English&mobilenum='.$to.'&password=Ufone@APi@321&groupname&messagetype=transactional';



curl_setopt_array($curl, array(

  CURLOPT_URL => $url,

  CURLOPT_RETURNTRANSFER => true,

  CURLOPT_ENCODING => '',

  CURLOPT_MAXREDIRS => 10,

  CURLOPT_TIMEOUT => 0,

  CURLOPT_FOLLOWLOCATION => true,

  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

  CURLOPT_CUSTOMREQUEST => 'GET',

  CURLOPT_HTTPHEADER => array(

    'Cookie: cookiesession1=678B2881QRSTUVXYZABCDEFGHIMN88F6'

  ),

));



$response = curl_exec($curl);



curl_close($curl);



/*$url = "http://lifetimesms.com/json";



$parameters = array("username" =>$username ,

                  "password" => $password,

                  "to" => $to,

                  "from" => $from,

                  "message" => $message

                );



	$ch = curl_init();

	$timeout  =  30;

	curl_setopt($ch, CURLOPT_URL, $url);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

	curl_setopt($ch, CURLOPT_HEADER, 0);

	curl_setopt($ch, CURLOPT_POST, 1);

	curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);

	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

	$response = curl_exec($ch);

	curl_close($ch);*/







   return $response;



}







?>