<?php 
session_start();
include 'dbc.php';

date_default_timezone_set("Asia/Karachi");
$today=date("Y-m-d");

$userid=$_SESSION["userid"];
$fee_period=$_POST["fee_period"];
$month_name_fine=$_POST["month_name_fine"];
$fine_value=$_POST["fine_value"];
$month_period=$_POST["month_period"];
$class_name=$_POST["class_name"];
$issue_date=$_POST["issue_date"];
$due_date=$_POST["due_date"];
$month_name=$_POST["month_name"];

$feedisc1=$_POST["feedisc1"];
$fee1=$_POST["fee1"];
$feedisc2=$_POST["feedisc2"];
$fee2=$_POST["fee2"];
$feedisc3=$_POST["feedisc3"];
$fee3=$_POST["fee3"];
$feedisc4=$_POST["feedisc4"];
$fee4=$_POST["fee4"];
$feedisc5=$_POST["feedisc5"];
$fee5=$_POST["fee5"];
$feedisc6=$_POST["feedisc6"];

$fee6=$_POST["fee6"];


$sqlfine = "SELECT * FROM admission where status='Active' and user_id='$userid'";
$resultfine=$conn->query($sqlfine);
while($rowfine = $resultfine->fetch_assoc())
 
  {
  $sql1fine = "update admission set fine='0' where status='Active' and user_id='$userid'";
$conn->query($sql1fine);
  }
  
 $sqlgfine = "SELECT * FROM issueslip where month_name='$month_name' and status='unpaid' and user_id='$userid'";
$resultgfine=$conn->query($sqlgfine);
while($rowgfine = $resultgfine->fetch_assoc())
 
  {
$adm_no1=$rowgfine['adm_no'];
$class_name=$rowgfine['class_name'];

   $tfine=$fine_value;
   $sql1fine1 = "update admission set fine='$tfine' where adm_no='$adm_no1' and class_name='$class_name' and user_id='$userid'";
$conn->query($sql1fine1);
	
	
	 }
  
  
  $sqlchallan = "SELECT * FROM admission where status='Active' and user_id='$userid'";
$resultchallan=$conn->query($sqlchallan);
while($rowchallan = $resultchallan->fetch_assoc())
 
  {
  $curfine=0;
$netqty =0;
$qtysale=0;
$qtypur=0;
 $netqty1 =0;
$qtysale1=0;
$qtypur1=0;
 $netqty2 =0;
$qtysale2=0;
$qtypur2=0;
 $netqty3 =0;
$qtysale3=0;
$qtypur3=0;
 $netqty4 =0;
$qtysale4=0;
$qtypur4=0;
 $netqty5 =0;
$qtysale5=0;
$qtypur5=0;
 $netqty6 =0;
$qtysale6=0;
$qtypur6=0;
$bal = 0;
$feebal1 =0;
$feebal2 = 0;
$feebal3= 0;
$feebal4 = 0;
$feebal5 = 0;
$feebal6 = 0;
$netfee=0;
$feebal=0;
$totalfee=0;
  $adm_no=$rowchallan["adm_no"];
$std_rollno=$rowchallan['std_rollno'];
$app_file1=$rowchallan['app_file1'];
$std_name=$rowchallan['std_name'];
$fname=$rowchallan['fname'];
$cls_sec=$rowchallan['cls_sec'];
$class_name=$rowchallan['class_name'];
$cell_1=$rowchallan['cell_1'];
$mfee=$rowchallan['mfee'];
$barcode=$rowchallan['barcode'];
$curfine=$rowchallan['fine'];
$tatt=0;
$sqlchallan111 = "select * from issueslip where std_name='$std_name' and adm_no='$adm_no' and month_name='$month_name' and user_id='$userid'";
$resultchallan111=$conn->query($sqlchallan111);
  while($rowchallan111 = $resultchallan111->fetch_assoc())
 
  {
  $tatt = $tatt + 1;
  }
 if ($tatt < 1)
  {

  $sql2 = "select * from issueslip where std_name='$std_name' and adm_no='$adm_no' and user_id='$userid'";
$result2=$conn->query($sql2);
while($ppp1 = $result2->fetch_assoc())
{
  $qtysale= $qtysale + $ppp1["feebal"];
  $qtysale1= $qtysale1 + $ppp1["fee1bal"];
  $qtysale2= $qtysale2 + $ppp1["fee2bal"];
  $qtysale3= $qtysale3 + $ppp1["fee3bal"];
  $qtysale4= $qtysale4 + $ppp1["fee4bal"];
  $qtysale5= $qtysale5 + $ppp1["fee5bal"]; 
  $qtysale6= $qtysale6 + $ppp1["fee6bal"];  
 } 
  
$sql3 = "select * from rec_fee where std_name='$std_name' and adm_no='$adm_no' and user_id='$userid'";
$result3=$conn->query($sql3);
while($ppp3 = $result3->fetch_assoc())
{
  $qtypur= $qtypur + $ppp3["feerec"];
  $qtypur1= $qtypur1 + $ppp3["feerec1"];
  $qtypur2= $qtypur2 + $ppp3["feerec2"];
  $qtypur3= $qtypur3 + $ppp3["feerec3"];
  $qtypur4= $qtypur4 + $ppp3["feerec4"];
  $qtypur5= $qtypur5 + $ppp3["feerec5"];
  $qtypur6= $qtypur6 + $ppp3["feerec6"];
 } 
 
 
 

$bal = ($qtysale ) - ($qtypur );
$feebal1 = ($qtysale1 ) - ($qtypur1 );
$feebal2 = ($qtysale2 ) - ($qtypur2 );
$feebal3= ($qtysale3 ) - ($qtypur3 );
$feebal4 = ($qtysale4 ) - ($qtypur4 );
$feebal5 = ($qtysale5 ) - ($qtypur5 );
$feebal6 = ($qtysale6 ) - ($qtypur6 );
  
$tfeebal1=$fee1;
$tfeebal2=$fee2;
$tfeebal3=$fee3;
$tfeebal4=$fee4;
$tfeebal5=$fee5;
$tfeebal6=$fee6;

$netfee=$mfee * $fee_period;
$feebal=$netfee + $bal + $curfine;
$totalfee=$netfee + $tfeebal1 + $tfeebal2 + $tfeebal3 + $tfeebal4 + $tfeebal5 + $tfeebal6 + $curfine + $bal;

$sql = "Insert into issueslip (id,date,due_date,month_name,adm_no,portal_id,std_name,fname,class_name,cls_sec,cell_1,mfee,bal,feedisc1,fee1,feedisc2,fee2,feedisc3,fee3,feedisc4,fee4,feedisc5,fee5,feedisc6,fee6,fine,totalfee,status,user_id,month_period,feebal,fee1bal,fee2bal,fee3bal,fee4bal,fee5bal,fee6bal,totalfeebal,barcode,app_file1,status1,status2,status3,status4,status5,status6) values (null,'$issue_date','$due_date','$month_name','$adm_no','$std_rollno','$std_name','$fname','$class_name','$cls_sec','$cell_1','$netfee','$bal','$feedisc1','$fee1','$feedisc2','$fee2','$feedisc3','$fee3','$feedisc4','$fee4','$feedisc5','$fee5','$feedisc6','$fee6','$curfine','$totalfee','unpaid','$userid','$month_period','$netfee','$tfeebal1','$tfeebal2','$tfeebal3','$tfeebal4','$tfeebal5','$tfeebal6','$totalfee','$barcode','$app_file1','unpaid','unpaid','unpaid','unpaid','unpaid','unpaid')";
$conn->query($sql);
}
else
{
$sql4 = "UPDATE issueslip SET mfee='$mfee',bal='$bal',totalfee='$mfee',feebal='$mfee',totalfeebal='$mfee' WHERE std_name='$std_name' and adm_no='$adm_no' and month_name='$month_name' and user_id='$userid'";
$conn->query($sql4);
/*$sql = "Insert into issueslip (id,date,due_date,month_name,adm_no,portal_id,std_name,fname,class_name,cls_sec,cell_1,mfee,bal,feedisc1,fee1,feedisc2,fee2,feedisc3,fee3,feedisc4,fee4,feedisc5,fee5,feedisc6,fee6,fine,totalfee,status,user_id,month_period,feebal,fee1bal,fee2bal,fee3bal,fee4bal,fee5bal,fee6bal,totalfeebal,barcode,app_file1,status1,status2,status3,status4,status5,status6) values (null,'$issue_date','$due_date','$month_name','$adm_no','$std_rollno','$std_name','$fname','$class_name','$cls_sec','$cell_1','$netfee','$bal','$feedisc1','$fee1','$feedisc2','$fee2','$feedisc3','$fee3','$feedisc4','$fee4','$feedisc5','$fee5','$feedisc6','$fee6','$curfine','$totalfee','unpaid','$userid','$month_period','$netfee','$tfeebal1','$tfeebal2','$tfeebal3','$tfeebal4','$tfeebal5','$tfeebal6','$totalfee','$barcode','$app_file1','paid','paid','paid','paid','paid','paid')";
$conn->query($sql);*/
}
 }
echo "Challan Genrate Sucessfully";

?>
<a href="Open_Student_Admission_Challan.php">Back</a>