<?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];
$month_name= $_POST['month_name'][$i];

$sql2 = "SELECT * FROM admission where adm_no = '$adm_no' 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'] ;



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

$bal=$row['bal'] ;


$rembal= $bal - $mfee;
	
	$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,totalfeerec) values (null,'$adm_no','$portal_id','$std_name','$fname','$class_name','$cls_sec','$cell_1','$mfee','$userid','$month_name','$rec_date','$mfee','$mfee')";
$conn->query($sql6);

	$sql4 = "UPDATE admission SET bal='$rembal' 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','$mfee','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_Advance.php"> Back </a>