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


$userid=$_SESSION["userid"];

$acc_name_dr=$_POST["acc_name_dr"];
$size = count($_POST['mfee']);

$i = 0;
while ($i < $size) {
$mfee= $_POST['mfee'][$i];
$rec_date= $_POST['rec_date'][$i];
$adm_no= $_POST['adm_no'][$i];
	$fee1= $_POST['fee1'][$i];
	$fee2= $_POST['fee2'][$i];
	$fee3= $_POST['fee3'][$i];
	$fee4= $_POST['fee4'][$i];
	$fee5= $_POST['fee5'][$i];
	$fee6= $_POST['fee6'][$i];
	$fine= $_POST['fine'][$i];
	
	$feerec= $_POST['feerec'][$i];
	$fee1rec= $_POST['fee1rec'][$i];
	$fee2rec= $_POST['fee2rec'][$i];
	$fee3rec= $_POST['fee3rec'][$i];
	$fee4rec= $_POST['fee4rec'][$i];
	$fee5rec= $_POST['fee5rec'][$i];
	$fee6rec= $_POST['fee6rec'][$i];
	
	$feebal= $mfee - $feerec;
	$fee1bal= $fee1 - $fee1rec;
	$fee2bal= $fee2 - $fee2rec;
	$fee3bal= $fee3 - $fee3rec;
	$fee4bal= $fee4 - $fee4rec;
	$fee5bal= $fee5 - $fee5rec;
	$fee6bal= $fee6 - $fee6rec;
	
$id = $_POST['id'][$i];

$sql2 = "SELECT * FROM issueslip where id = '$id' and  user_id='$userid'";
$result2=$conn->query($sql2);
$row2 = $result2->fetch_assoc();
$portal_id=$row2['portal_id'] ;
$std_name=$row2['std_name'] ;
$fname=$row2['fname'] ;
$class_name=$row2['class_name'] ;
$cls_sec=$row2['cls_sec'] ;
$cell_1=$row2['cell_1'] ;
$month_period=$row2['month_period'] ;
$feerecbal=$row2['feerec'] ;
$fee1recbal=$row2['fee1rec'] ;
$fee2recbal=$row2['fee2rec'] ;
$fee3recbal=$row2['fee3rec'] ;
$fee4recbal=$row2['fee4rec'] ;
$fee5recbal=$row2['fee5rec'] ;
$fee6recbal=$row2['fee6rec'] ;
$totalfeebal=$row2['totalfeebal'] ;
$totalfeerecbal=$row2['totalfeerec'] ;
$totalfeerec=$feerec + $fee1rec + $fee2rec + $fee3rec + $fee4rec + $fee5rec + $fee6rec;
$netfeerec =  $totalfeerecbal + $totalfeerec;
$netfeebal= $totalfeebal - $totalfeerec;
    $tfeerecbal= $feerecbal + $feerec;
	$tfee1recbal= $fee1recbal + $fee1rec;
	$tfee2recbal= $fee2recbal + $fee2rec;
	$tfee3recbal= $fee3recbal + $fee3rec;
	$tfee4recbal= $fee4recbal + $fee4rec;
	$tfee5recbal= $fee5recbal + $fee5rec;
	$tfee6recbal= $fee6recbal + $fee6rec;
 
	$sql1 = "UPDATE issueslip SET status='paid',rec_date='$rec_date',feerec='$tfeerecbal',fee1rec='$tfee1recbal',fee2rec='$tfee2recbal',fee3rec='$tfee3recbal',fee4rec='$tfee4recbal',fee5rec='$tfee5recbal',fee6rec='$tfee6recbal',feebal='$feebal',fee1bal='$fee1bal',fee2bal='$fee2bal',fee3bal='$fee3bal',fee4bal='$fee4bal',fee5bal='$fee5bal',fee6bal='$fee6bal',totalfeerec='$netfeerec',totalfeebal='$netfeebal' WHERE id='$id' and  user_id='$userid'";
$conn->query($sql1);



$sql = "SELECT * FROM admission where adm_no = '$adm_no' and  user_id='$userid'";
$result=$conn->query($sql);
$row = $result->fetch_assoc();

$bal=$row['bal'] ;
$feebal1=$row['feebal1'] ;
$feebal2=$row['feebal2'] ;
$feebal3=$row['feebal3'] ;
$feebal4=$row['feebal4'] ;
$feebal5=$row['feebal5'] ;
$feebal6=$row['feebal6'] ;

$rembal= $bal - $feerec;
$rem1bal= $feebal1 - $fee1rec;
	$rem2bal= $feebal2 - $fee2rec;
	$rem3bal= $feebal3 - $fee3rec;
	$rem4bal= $feebal4 - $fee4rec;
	$rem5bal= $feebal5 - $fee5rec;
	$rem6bal= $feebal6 - $fee6rec;
	
	$sql6 = "Insert into rec_fee (id,adm_no,portal_id,std_name,fname,class_name,cls_sec,cell_1,mfee,user_id,month_period,rec_date,feerec,fee1rec,fee2rec,fee3rec,fee4rec,fee5rec,fee6rec,totalfeerec,fine) values (null,'$adm_no','$portal_id','$std_name','$fname','$class_name','$cls_sec','$cell_1','$mfee','$userid','$month_period','$rec_date','$feerec','$fee1rec','$fee2rec','$fee3rec','$fee4rec','$fee5rec','$fee6rec','$totalfeerec','$fine')";
$conn->query($sql6);

	$sql4 = "UPDATE admission SET bal='$rembal',feebal1='$rem1bal',feebal2='$rem2bal',feebal3='$rem3bal',feebal4='$rem4bal',feebal5='$rem5bal',feebal6='$rem6bal' WHERE adm_no='$adm_no' and  user_id='$userid'";
$conn->query($sql4);
$sqlc = "SELECT * FROM chartofaccounts where acc_name = '$acc_name_dr' and  user_id='$userid'";
$resultc=$conn->query($sqlc);
$rowc = $resultc->fetch_assoc();
$acc_symb=$rowc['acc_symb'] ;
$acc_type=$rowc['acc_type'] ;
$sql101 = "Insert into acc_ledger (id,date,month_name,tr_no,acc_name,discription,acc_type,dr_amount,cr_amount,balance,user_id,acc_symb) values (null,'$rec_date','$month_period','$adm_no','$acc_name_dr','$std_name','$acc_type','$totalfeerec','0','0','$userid','$acc_symb')";
$conn->query($sql101);

	++$i;



// do the update and print out some info just to provide some visual feedback



} 
echo "Record Update";
?>
<a href="Fee_Receipt.php"> Back </a>