<?php include("parent/callMe.php"); ?>
<?php  include("sessioncheck.php");

if(($_REQUEST['action']=='PayFamilyChallan'))
{


$branch_id=$_SESSION['branch'];
$cell_no=$_REQUEST['cell_no'];
$month=date("m");

  $sql="SELECT * FROM eduportal_monthly_challan as fee, edu_student_details as std WHERE std.cell_no='$cell_no' and fee.branch_id='$branch_id' and std.status='0' and fee.month_id='$month' and std.student_id=fee.student_id";
//print_r($sql);


$WorkAssignListings = $m->query($sql);
$counter=0;

 foreach($WorkAssignListings as $WorkAssignListVal)
      { 

     $counter++;

      $OthersTotal=0+getFeeAllOtherFee($WorkAssignListVal['student_id'],$WorkAssignListVal['challan_id'],$m);
      $totalAmount= $WorkAssignListVal['tutionfee']+$WorkAssignListVal['transportfee']+$WorkAssignListVal['balance']+$WorkAssignListVal['misc_fee']+$OthersTotal; 
//echo $WorkAssignListVal['tutionfee'];
    $start_date = strtotime($WorkAssignListVal['challan_duedate']); 
    $end_date = strtotime(date("Y-m-d"));
    $datediffrence =($end_date - $start_date)/60/60/24;


      $totalAmount1=$totalAmount-$WorkAssignListVal['disc_amount'];

$student_fine=0;
    $actual_fine=0;



  
   if($branch_details[0]['fine_type']=="0" || $branch_details[0]['fine_type']==0){ 
    $student_fine= $branch_details[0]['late_fee_fine']; } 
  else {    
    $student_fine= ($datediffrence * $branch_details[0]['late_fee_fine']);
   }

   
        // if row is first and fee is paid then show only collected fine
          if($WorkAssignListVal['paid_status']==1){
             //echo $WorkAssignListVal['collected_fine'];
             $actual_fine=$WorkAssignListVal['collected_fine'];
           
          } 
        // if row is first and fee is not paid then calculate fine
          else if(date("Y-m-d")>$WorkAssignListVal['challan_duedate'] && $WorkAssignListVal['paid_status']==0){
           // echo $student_fine; 
            $actual_fine=$student_fine;
          }      
    
// if not row is first and then show only collected fine
    else {

        // echo $WorkAssignListVal['collected_fine'];
         $actual_fine=$WorkAssignListVal['collected_fine'];
    }    



    $totalAmount1+=$actual_fine;




if ($WorkAssignListVal['paid_status']==0) {
			
	$paid_date=date("Y-m-d");

	$collected_amount=intVal($totalAmount1-$actual_fine);
	$totalAmount=$totalAmount1;
	$number=$cell_no;
	$fathName=$WorkAssignListVal['last_name'];

  
		$data = array(
		'collected_amount' => $collected_amount,
		'collected_fine' => $WorkAssignListVal['collected_fine'],
		'paid_status' => 1,
		'paid_fee_user_id' => $_SESSION['user_id'],
		'paying_time' => date("h:i:s a"),
		'paying_date'=>date('d-m-Y',strtotime($paid_date))
		);

	//	echo "<pre>"; print_r($data); echo "</pre>"."<br>";

		$m->where ("challan_id", $WorkAssignListVal['challan_id']);
		$m->where ("student_id", $WorkAssignListVal['student_id']);
		$m->where ("branch_id", $_SESSION['branch']);
		//$result = $m->update('eduportal_monthly_challan',$data);
	} 


       



  
 }  



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

		if($brid[0]['fee_recv_sms']=="1" || $brid[0]['fee_recv_sms']==1){

	    

		$message ="Dear ".$fathName.", You have submitted the fee for the month of (".getMonthName($m,$WorkAssignListings[0]['month_id']).")  

		Total:".$totalAmount." 

		Paid:".$totalAmount."

		Date:".date("d-M-Y")." 

		From:".$_SESSION['branch_name'];



			echo "<br>".$message;



		  // 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);

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

		 	

		}





}







/*


		$paid_date=date("Y-m-d");


		// 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,
		'paid_fee_user_id' => $_SESSION['user_id'],
		'paying_time' => date("h:i:s a"),
		'paying_date'=>date('d-m-Y',strtotime($paid_date))
		);

		$m->where ("challan_id", $_REQUEST['challan_id']);
		$result = $m->update('eduportal_monthly_challan',$data);


   
   $totalAmount=$_REQUEST['totalAmount'];
   $std_name=$_REQUEST['std_name'];
   $number=$_REQUEST['std_no'];
    
   // echo "Data added";
   $balanc=$totalAmount-$collected_amount;
		// now sending fee confirmation msg to parents if the sms status is YES
		if($_REQUEST['fee_recv_sms']=="1" || $_REQUEST['fee_recv_sms']==1 && $totalAmount>0){


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

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

			// sms sending function is made at the bottom 
			 $result=sendSMSFuction($number,$message,$_SESSION['brand']);
		 	
		}// ending fee recv if

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



        		// now sending fee confirmation msg to Principle if the sms status is YES
if($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 ="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";

} // ending main if

// sms sending function

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

  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'];  
	}

$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."==".$url."==".$message;


*/


?>