<?php
session_start();

	include("connect.php");
	
	if(!isset($_SESSION['tulsilogin']))
	{
		header('Location: index.php');
		return;
	}
				
	
		//$_SESSION['frompage']="dashboard";

	date_default_timezone_set("Asia/Kolkata");
	$mdate= date('Y/m/d');
	$mdate2= date('Y-m-d');
	$mdate1= date('Y-m-d H:i:s');
	$mdate3= date('d M,Y');
	
	$umid=$_SESSION['umid'];
	$campusmid1=$_SESSION['campusmid1'];
											
											$eve22 = "select campusname,cmname,countryname from res_collegecampus_master,res_college_master,res_country_master where res_collegecampus_master.collegemid = res_college_master.collegemid and res_college_master.countrymid = res_country_master.countrymid and res_collegecampus_master.ccampusmid = $campusmid1";
											$re22 = mysqli_query($conn, $eve22);
											while($rt22 = mysqli_fetch_assoc($re22))
										{
											$campusname = $rt22['campusname'];
											$cmname = $rt22['cmname'];
											$countryname = $rt22['countryname'];
										}	
										
										
	
	
	
	$imids = $_SESSION['acsdid'];
	$acsdid = implode(',', $imids);

		
	  $eve = " SELECT * FROM res_inquiry_master, res_admission_master, res_course_master,res_admissioncoursesem_detail WHERE res_admission_master.imid = res_inquiry_master.imid 
  AND res_course_master.coursemid = res_admission_master.coursemid AND res_course_master.campusmid = res_admission_master.campusmid AND res_course_master.campusmid IN ( SELECT ccampusmid FROM res_commissioncampus_detail WHERE commissionmid IN ( SELECT commissionmid FROM res_commission_master WHERE termsclaimable >= res_admission_master.intaketerm ) ) AND  res_admissioncoursesem_detail.admid = res_admission_master.admid AND res_course_master.campusmid = $campusmid1  and res_admissioncoursesem_detail.acsdid in ($acsdid)";
	$re = mysqli_query($conn,$eve);
	$count = mysqli_num_rows($re);
	$total = 0;
	if(mysqli_num_rows($re) > 0)
	{
	
		 while($rt = mysqli_fetch_assoc($re))
		 {
			 
				$ctutionfees=$rt['asemfees'];
				$feesin=$rt['asemfeesin'];
				
			  $currancyvalue=1;
				 $eve1 = "select * from res_currencytoinr_master where currencyname='$feesin'";
				$re1 = mysqli_query($conn, $eve1);
				if(mysqli_num_rows($re1) > 0)
				{
					 while($rt1 = mysqli_fetch_assoc($re1))
					 {
						 $currancyvalue=$rt1['currancyvalue'];
					 }
				}
				
				$data = $ctutionfees/$currancyvalue;
	 $data = number_format($data,2);
	  $data = str_replace(',', '', $data);
				
						
  $total =  $total + (int)$ctutionfees; 										
		 }
	}
	
	
	$eve1 = "select * from res_commission_master where commissionmid in (select commissionmid from res_commissioncampus_detail where ccampusmid=$campusmid1) limit 0,1";
	$re1 = mysqli_query($conn,$eve1);
	if(mysqli_num_rows($re1) > 0)
	{
		
		 while($rt1 = mysqli_fetch_assoc($re1))
		 {
			 	 $commissionstrucher=$rt1['commissionstrucher'];
			 	$commissionmid=$rt1['commissionmid'];
				  $commissionstruchervalue=$rt1['commissionstruchervalue'];
				 $cmgstincexc=$rt1['cmgstincexc'];
				 $ctaxpercent=$rt1['ctaxpercent'];
				 $ctaxname=$rt1['ctaxname'];
				 $cmcurrency=$rt1['cmcurrency'];
				$currancyvalue1=1;
				$eve1 = "select * from res_currencytoinr_master where currencyname='$cmcurrency'";
				$re1 = mysqli_query($conn, $eve1);
				if(mysqli_num_rows($re1) > 0)
				{
					 while($rt2 = mysqli_fetch_assoc($re1))
					 {
						 $currancyvalue1=$rt2['currancyvalue'];
					 }
				}
				$comission =0;
				if($commissionstrucher== 'Slab')
				{
					$slabstrucher=$rt1['slabstrucher'];
					$data1=0;
						$eve12 = "select * from res_commissionslab_detail,res_slab_master where res_commissionslab_detail.slabmid=res_slab_master.slabmid  and res_commissionslab_detail.commissionmid = $commissionmid and res_slab_master.slabstartnumber <= $count and res_slab_master.slabendnumber >= $count limit 0,1";
					 $re12 = mysqli_query($conn,$eve12);
					if(mysqli_num_rows($re12) > 0)
					{
						
						 while($rt12 = mysqli_fetch_assoc($re12))
						 {
							 
							  $svalue = $rt12['svalue'];
							  if($slabstrucher== 'Fixed Amount'){
					
								$comission = $svalue*$count;
				}else  if($slabstrucher == 'Percentage'){
					$currancyvalue2 = $currancyvalue1/$currancyvalue;
					$total2 = $total/$currancyvalue2;
					 
					  $comission =  $total2 * ($svalue/100);
				}
							
							    
						 }
					}
					
				}else if($commissionstrucher== 'Fixed Amount'){
					
					
								$comission = $commissionstruchervalue*$count;
				}else  if($commissionstrucher == 'Percentage'){
					$currancyvalue2 = $currancyvalue1/$currancyvalue;
					$total2 = $total/$currancyvalue2;
					 
					 
					  $comission =  $total2 * ($commissionstruchervalue/100);
					    $comission = number_format($comission,2);
	  $comission = str_replace(',', '', $comission);
				}
				
				if($cmgstincexc=='Included')
				{
					$finalcomission = $comission;
				}else{
					$finalcomission = $comission + $comission*$ctaxpercent/100;
				}
				 $finalcomission;
				}
				$finalcomission = number_format($finalcomission,2);
	  $finalcomission = str_replace(',', '', $finalcomission);
	}
	
	$eve11 = "select max(invoiceno) as aa2 from res_invoice_master   ";
	$re11 = mysqli_query($conn,$eve11);
	if(mysqli_num_rows($re11) > 0)
	{
	
		 while($rt11 = mysqli_fetch_assoc($re11))
		 {
			 $invoiceno1=$rt11['aa2'];
		 }
	}else{
		 $invoiceno1=0;
	}
	 $invoiceno= $invoiceno1+1;
	
	$eve23 = "INSERT INTO res_invoice_master (invoiceno,invoicedate,invoicetype,invoiceamount,itaxincexc,itaxname,itaxpercent,invoicetotalamount,campusmid,imaddedon,imaddedby,invoicecurrency) values('$invoiceno','$mdate2','Campus Comission','$comission','$cmgstincexc','$ctaxname','$ctaxpercent','$finalcomission','$campusmid1','$mdate1',$umid,'$cmcurrency')";
	if ($conn->query($eve23) === TRUE)
	{
		$invoicemid = $conn->insert_id;
		  $eve = " SELECT * FROM res_inquiry_master, res_admission_master, res_course_master,res_admissioncoursesem_detail WHERE res_admission_master.imid = res_inquiry_master.imid 
  AND res_course_master.coursemid = res_admission_master.coursemid AND res_course_master.campusmid = res_admission_master.campusmid  AND res_course_master.campusmid = $campusmid1 AND  res_admissioncoursesem_detail.admid = res_admission_master.admid  and  res_admissioncoursesem_detail.acsdid  in ($acsdid )";
	$re = mysqli_query($conn,$eve);
	$count = mysqli_num_rows($re);
	$total = 0;
	if(mysqli_num_rows($re) > 0)
	{
	
		 while($rt = mysqli_fetch_assoc($re))
		 {
			 
				$ctutionfees=$rt['asemfees'];
				$admid=$rt['admid'];
				$asem=$rt['asem'];
				$acsdid =$rt['acsdid'];
				$imid=$rt['imid'];
				
				 $eve1 = "select * from res_currencytoinr_master where currencyname='$feesin'";
				$re1 = mysqli_query($conn, $eve1);
				if(mysqli_num_rows($re1) > 0)
				{
					 while($rt1 = mysqli_fetch_assoc($re1))
					 {
						 $currancyvalue=$rt1['currancyvalue'];
					 }
				}
				
				$data = $ctutionfees/$currancyvalue;
	 $data = number_format($data,2);
	  $data = str_replace(',', '', $data);
			
$currancyvalue2 = $currancyvalue1/$currancyvalue;
					$data2 = $ctutionfees/$currancyvalue2;
					$data2 = number_format($data2,2);
	  $data2 = str_replace(',', '', $data2);
					if($cmgstincexc=='Included')
				{
					$finaldata = $data2;
				}else{
					$itaxamount = $data2*$ctaxpercent/100;
					$itaxamount = number_format($itaxamount,2);
	  $itaxamount = str_replace(',', '', $itaxamount);
					$finaldata = $data2 + $data2*$ctaxpercent/100;
				}
				$finaldata = number_format($finaldata,2);
	  $finaldata = str_replace(',', '', $finaldata);
				 
				
					$intaketerm=$asem;
					$eve23 = "INSERT INTO res_invoice_detail (invoicemid,imid,acsdid ,intaketerm,iamount,itaxamount,itotalamount,idaddedon,idaddedby) values('$invoicemid','$imid','$acsdid ','$intaketerm','$data2','$itaxamount','$finaldata','$mdate1',$umid)";
					$re23 = mysqli_query($conn, $eve23);
									
		 }
	}
	}
	
	header("Location: invoicepdf.php?id=$invoicemid");
	
	?>