<?php
include("../db.php");
$ch=$_REQUEST["type"];

if($_GET["type"]=="getValByRecepit"){
	$data=$db->getRow("select * from receipt where id='".$_REQUEST["id"]."'");
	//$finaldetail=$data["detail"];
	$pdetails=json_decode($data["detail"], true);
	//print_r($pdetails);
	if(is_array($pdetails) && count($pdetails)>0){
		$finaldetail="";
		foreach($pdetails as $key=>$value){
			$finaldetail=$finaldetail.$key.":".$value."\n" ;
		}
	}
	$detail["month"]=$data["udate"]; 
	$detail["name"]=$finaldetail;
	$detail["value"]=$data["amount"];
	echo json_encode($detail);
}

elseif($ch=="completediscount"){
    // print_r($_POST);
    $idno = $_POST["idno"];
    $data = $_POST["discount_detail"];

    $disc_mode = $_POST['disc_mode'];
    
    $disc_type = $_POST['disc_type'];
    $disc_amt  = $_POST['disc_amt'];

    $discount_id = $db->getVal("select discount_id from discount_detail order by discount_id DESC")+1;

    foreach($data as $detail){
        $i = $detail["month"];
        // print_r($d);
        $feedetail = $detail['feedetail'];
            foreach($feedetail as $d){
                // print_r($d);
                
                $name = $d["name"];
                $type = $d["type"];
                $discountamount = $d["disamount"];
                $other_id = $d["other_id"];
                
                if($type == "tution"){
                    $ramount  = $db->getVal("select i".$i." from installment where  idno='$idno' ");
                    $discount = $db->getVal("select discount from installment where  idno='$idno' ");	
                    
                    $insAry =   array(  "i".($i)	=>  $ramount-$discountamount,
                            "discount"  =>  $discount+$discountamount,
    				);

                	$inup=$db->updateAry("installment",$insAry,"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='$idno' ");
                
                    $arySave=array(
		                "idno"              =>  $idno,
		                "discount_id"       =>  $discount_id,
						"discount_amount"   =>  $discountamount,
						"installment"       =>  "i".($i),
						"type"              =>  $type,
						"other_id"          =>  $other_id,
						"session"           =>  $_SESSION["admin"]["session"],
  	                    "school_id"         =>  $_SESSION["admin"]["school_id"],
                        "added_by"          =>  $_SESSION["admin"]["uid"],
                        "disc_mode"         =>  $disc_mode,
                        "disc_type"         =>  $disc_type,
                        "disc_amt"          =>  $disc_amt
					);
        			$db->insertAry("discount_detail",$arySave);
                    
                }
                
                if($type == "bus"){
                    $ramount=$db->getVal("select b".$i." from installment where  idno='$idno' ");
                    $discount=$db->getVal("select busdiscount from installment where  idno='$idno' ");
                    
                    $insAry=array( "b".($i)	=>$ramount-$discountamount,
        				"busdiscount"=>$discount+$discountamount				
						);

                	$inup=$db->updateAry("installment",$insAry,"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='$idno' ");

	                $arySave    =   array(
		                "idno"              =>  $idno,
		                "discount_id"       =>  $discount_id,
						"discount_amount"   =>  $discountamount, 
						"installment"       =>  "b".($i),
						"type"              =>  $type,
						"other_id"          =>  $other_id,
						"session"           =>  $_SESSION["admin"]["session"],
  	                    "school_id"         =>  $_SESSION["admin"]["school_id"],
                        "added_by"          =>  $_SESSION["admin"]["uid"],
                        "disc_mode"         =>  $disc_mode,
                        "disc_type"         =>  $disc_type,
                        "disc_amt"          =>  $disc_amt
					);
        			$db->insertAry("discount_detail",$arySave);	
                }
                
                if($type == "extra"){
                    $ramount=$db->getVal("select  fee from extrafee where  idno='$idno'  and pid='p".$i."' and  type='$other_id'  ");
                    $insAry=array( "fee"=>$ramount-$discountamount );

                	$inup=$db->updateAry("extrafee",$insAry,"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='$idno' and  type='$other_id' and pid='p".$i."' ");

                    $arySave=array(
                        "idno"              =>  $idno,
                        "discount_id"       =>  $discount_id,
                        "discount_amount"   =>  $discountamount,
                        "installment"       =>  "i".($i),
                        "type"              =>  "extra",
                        "other_id"          =>  $other_id,
                        "session"           =>  $_SESSION["admin"]["session"],
                        "school_id"         =>  $_SESSION["admin"]["school_id"],
                        "added_by"          =>  $_SESSION["admin"]["uid"],
                        "disc_mode"         =>  $disc_mode,
                        "disc_type"         =>  $disc_type,
                        "disc_amt"          =>  $disc_amt
					);
            		$db->insertAry("discount_detail",$arySave);

                }
                
                if($type == "otherfee"){
                    $arr=$db->getRow("Select class,section from student where idno='$idno' ");
            		$aryData=array(
            						"other_id"  =>  $other_id,
            						"idno"      =>  $idno,
            						"discount"  =>  $discountamount,
            						"month"     =>  $i,
            						"class"     =>  $arr["class"],
            						"section"   =>  $arr["section"],
            						"session"   =>  $_SESSION["admin"]["session"],
            		      			"school_id" =>  $_SESSION["admin"]["school_id"],
                    				"added_by"  =>  $_SESSION["admin"]["uid"]
            					 );
        			$flgIn=$db->insertAry("otherfee_discount",$aryData);
            
            		$arySave=array(
                                    "idno"              =>  $idno,
                                    "discount_id"       =>  $discount_id,
                                    "discount_amount"   =>  $discountamount,
            						"installment"       =>  "",
            						"type"              =>  "otherfee",
            						"other_id"          =>  $other_id,
            						"session"           =>  $_SESSION["admin"]["session"],
              	                    "school_id"         =>  $_SESSION["admin"]["school_id"],
            	                    "added_by"          =>  $_SESSION["admin"]["uid"],
                                    "disc_mode"         =>  $disc_mode,
                                    "disc_type"         =>  $disc_type,
                                    "disc_amt"          =>  $disc_amt
            				);
        			$db->insertAry("discount_detail",$arySave);
            	
                }

            }
        // print_r($feedetail);
        $id = $d['month'];
        // echo $name = $d['name'];

// echo "$i - ";

    }
    
    // echo count($data);
    // echo "hello from ajax";
    // $feeAry = $_POST['discount_detail'];
    // if(count($feeAry) > 0){
    //     print_r($feeAry);
    // }
}

elseif($ch=="resetdiscount"){
    // print_r($_POST);
    $idno = $_POST["idno"];
    $discount_id = $_POST["discount_id"];
    // echo "select * from discount_detail wehre idno='$idno' and discount_id='$discount_id' ";
    $records = $db->getRows("select * from discount_detail where idno='$idno' and discount_id='$discount_id' ");
    
    foreach($records as $row){
        
        $type = $row["type"];
        
        if($type == "tution"){

            $ins = $db->getRow("select $row[installment] as amount, discount from installment where idno='$idno' and session='".$_SESSION["admin"]["session"]."' ");
            $newamount = $ins["amount"]+$row["discount_amount"];
        	$aryData=array($row['installment'] => $newamount, "discount" => $ins['discount']-$row["discount_amount"] );

            $db->delete("discount_detail","where id='$row[id]' and session='".$_SESSION["admin"]["session"]."' and idno='$idno' and installment='$row[installment]' and type='tution'");
    
        	$db->updateAry("installment",$aryData,"where idno='$idno' and session='".$_SESSION["admin"]["session"]."' ");
    	
        }
        
        if($type == "bus"){

            $ins = $db->getRow("select $row[installment] as amount, discount from installment where idno='$idno' and session='".$_SESSION["admin"]["session"]."' ");
            $newamount = $ins["amount"]+$row["discount_amount"];
        	$aryData=array($row['installment'] => $newamount, "busdiscount" => $ins['discount']-$row["discount_amount"] );

            $db->delete("discount_detail","where id='$row[id]' and session='".$_SESSION["admin"]["session"]."' and idno='$idno' and installment='$row[installment]' and type='bus'");
    
        	$db->updateAry("installment",$aryData,"where idno='$idno' and session='".$_SESSION["admin"]["session"]."' ");
    	
        }
        
        if($type == "extra"){
            $pid = str_replace("i","p","$row[installment]");
            $amount = $db->getVal("select fee from extrafee where idno='$idno' and type='$row[other_id]' and pid='$pid' and session='".$_SESSION["admin"]["session"]."' ");
            $aryData=array("fee" => $amount+$row["discount_amount"]);
        
            $db->updateAry("extrafee",$aryData,"where idno='$idno' and type='".$row["other_id"]."' and pid='$pid' ");
            
            $db->delete("discount_detail","where id='$row[id]' and session='".$_SESSION["admin"]["session"]."' and idno='$idno' and installment='$row[installment]' and type='extra' ");

        }

        if($type == "otherfee"){
            	$db->delete("discount_detail","where idno='$idno' and id='$row[id]' ");

            	$db->delete("otherfee_discount","where idno='$idno' and other_id='$row[other_id]' and discount='$row[discount_amount]' and session='".$_SESSION["admin"]["session"]."' ");

        }
        
    }

    echo "Discount Reset Successfully";
    
}

elseif($ch=="cheque_status"){

    $b_dues["cheque_status"]= $_REQUEST["cheque_s"];
//die;check_status
    $id= $_REQUEST["id"];
	
	$inup=$db->updateAry("chequefee",$b_dues,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$id."'");
	echo "updated";
}

elseif($ch=="updatereceipt"){

    $ary = array(
            "payment_mode"  => $_REQUEST['mode'],
            "txnid"         => $_REQUEST['txnid'],
            "tdate"         => $_REQUEST['tdate'],
            "bankname"      => $_REQUEST['bankname']
        );
    $rid = $_REQUEST["rid"];
    $idno = $_REQUEST['idno'];

	$inup=$db->updateAry("receipt",$ary,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$rid."' and idno='".$idno."' ");

	foreach($_REQUEST['rrid'] as $rrid){

        $refary = array(
            "pay_type" => $_REQUEST['mode']
        );

        $inup=$db->updateAry("reff_receipt",$refary,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$rrid."' and idno='".$idno."' and session='".$_SESSION["admin"]["session"]."' ");
	}
	echo "$inup";
// 	print_r($_REQUEST['rrid']);
}

elseif($ch=="updatereceiptstatus"){

    $ary = array(
            "rec_status"  => $_REQUEST['status']
        );
    $rid = $_REQUEST["rid"];
    $idno = $_REQUEST['idno'];
    if($_REQUEST['status'] == 1){
    	$inup=$db->updateAry("receipt",$ary,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$rid."' and idno='".$idno."' ");
    	echo $inup;
    }
    else {

	$rdetail = $db->getRow("select * from receipt where id='$rid' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");

	$deletearay = array(
	            "class"         =>  $rdetail['class'],
	            "idno"          =>  $idno,
	            "receipt_id"    =>  $rid,
	            "paytype"       =>  "schoolreceipt",
	            "payment_mode"  =>  $rdetail['payment_mode'],
	            "amount"        =>  $rdetail['amount'],
	            "detail"        =>  $rdetail['detail'],
	            "status"        =>  1,
	            "session"       =>  $_SESSION["admin"]["session"],
	            "school_id"     =>  $_SESSION["admin"]["school_id"],
	            "added_by"      =>  $_SESSION["admin"]["uid"]
	    );

	    $db->insertAry("delete_report", $deletearay);

	    $rrids = explode("-", $rdetail['rrid']);

	    foreach($rrids as $rids){
            $reffdata = $db->getRow("select * from reff_receipt where id='$rids' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."'  ");

            if($reffdata['type'] == "fee"){
                $inst_id = $reffdata['inst_id'];
                $ins_name = "ip$inst_id";
                $updatefee = "";

                    $fee = $db->getRow("select i$inst_id, ip$inst_id from installment where idno='$idno' and session='".$_SESSION["admin"]["session"]."' ");

                    $newfee = $fee["ip$inst_id"]-$reffdata['amount'];
                    if($newfee == 0){
                        $newfee = "";
                    }

                    $updatefee = array(
                            "ip$inst_id"    =>  $newfee
                        );

                    $db->updateAry("installment", $updatefee, "where school_id='".$_SESSION["admin"]["school_id"]."' and idno='$idno' and session='".$_SESSION["admin"]["session"]."'");

                    $installmenttime = $db->getVal("select id from installment_time where ins_name='$ins_name' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");

                    $db->delete("installment_time", " where ins_name='$ins_name' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
                    $db->delete("transaction", " where rid='$idno' and tfor='fee' and amount='$reffdata[amount]' and  session='".$_SESSION["admin"]["session"]."' and (tid LIKE '%$ins_name%') ");
                    $db->delete("reff_receipt", "where id='$rids' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
            }

            elseif($reffdata['type'] == "busfee"){
                $inst_id = $reffdata['inst_id'];
                $ins_name = "bp$inst_id";
                $updatebusfee = "";

                $fee = $db->getRow("select b$inst_id, bp$inst_id from installment where idno='$idno' and session='".$_SESSION["admin"]["session"]."' ");
                $newfee = $fee["bp$inst_id"]-$reffdata['amount'];
                if($newfee == 0){
                    $newfee = "";
                }
                
                $updatefee = array(
                    "bp$inst_id"    =>  $newfee
                );

                $db->updateAry("installment", $updatefee, "where school_id='".$_SESSION["admin"]["school_id"]."' and idno='$idno' and session='".$_SESSION["admin"]["session"]."'");

                $installmenttime = $db->getVal("select id from installment_time where ins_name='$ins_name' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
                $db->delete("installment_time", " where ins_name='$ins_name' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
                $db->delete("transaction", " where rid='$idno' and tfor='bus' and amount='$reffdata[amount]' and  session='".$_SESSION["admin"]["session"]."' and (tid LIKE '%$ins_name%') ");
                $db->delete("reff_receipt", "where id='$rids' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
            }

            elseif(strpos($reffdata['type'], 'extrafee') !== false){
                    $inst_id = $reffdata['inst_id'];
                    $updateextrafee = 0;

                    $type = str_replace("extrafee","","$reffdata[type]");
                    $ins_name = "ext-".$type."-".$inst_id;

                    $feename = $db->getVal("select type from extrafee_type where id='$type' and  session='".$_SESSION["admin"]["session"]."' ");
                    $fee = $db->getRow("select fee, pay from extrafee where idno='$idno' and type='$type' and pid='p$inst_id' and session='".$_SESSION["admin"]["session"]."' ");
                    $newfee = $fee["pay"]-$reffdata['amount'];
                    if($newfee == 0){
                        $newfee = "";
                    }
                    
                    $updatefee = array(
                        "pay"    =>  $newfee
                    );
                $db->updateAry("extrafee",$updatefee,"where type='$type' and pid='p$inst_id' and school_id='".$_SESSION["admin"]["school_id"]."' and idno='$idno' and session='".$_SESSION["admin"]["session"]."'");

                $installmenttime = $db->getVal("select id from installment_time where ins_name='$ins_name' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");

                    $db->delete("installment_time", " where ins_name='$ins_name' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
                    $db->delete("reff_receipt", "where id='$rids' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
                    $db->delete("transaction", " where rid='$idno' and tfor='$feename' and amount='$reffdata[amount]' and  session='".$_SESSION["admin"]["session"]."' and (tid LIKE '%pay$inst_id%') ");

            }

            else{
                $inst_id = $reffdata['inst_id'];
                
                $transaction = $db->getRow("select * from transaction where rid='$idno' and tid='$inst_id' and amount='$reffdata[amount]' and  session='".$_SESSION["admin"]["session"]."' ");
                
                if(count($transaction) > 1){
                    $type = str_replace("_fee","_detail","$transaction[tfor]");
                    $ins_name = $type;

                    $db->delete("transaction", " where rid='$idno' and tid='$inst_id' and amount='$reffdata[amount]' and  session='".$_SESSION["admin"]["session"]."' ");
                    $db->delete("reff_receipt", "where id='$rids' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
                    $db->delete("installment_time", " where ins_name='$ins_name' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
                    $db->delete("installment_fine", " where id='$inst_id' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");

                }
            }

	    }

            $db->delete("receipt", "where id='$rid' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' "); 
            echo "deleted";
    }
}

elseif($ch=="updatehostelreceipt"){

    $ary = array(
            "payment_mode"  => $_REQUEST['mode'],
            "txnid"         => $_REQUEST['txnid'],
            "tdate"         => $_REQUEST['tdate'],
            "bankname"      => $_REQUEST['bankname']
        );
    $rid = $_REQUEST["rid"];
    $idno = $_REQUEST['idno'];

	$inup=$db->updateAry("hostel_receipt",$ary,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$rid."' and idno='".$idno."' ");

	foreach($_REQUEST['rrid'] as $rrid){

        $refary = array(
            "pay_type" => $_REQUEST['mode']
        );

        $inup=$db->updateAry("reff_receipt",$refary,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$rrid."' and idno='".$idno."' and session='".$_SESSION["admin"]["session"]."' ");
	}
	echo "$inup";

}

elseif($ch=="updatehostelreceiptstatus"){

    $ary = array(
            "rec_status"  => $_REQUEST['status']
        );
    $rid = $_REQUEST["rid"];
    $idno = $_REQUEST['idno'];
    if($_REQUEST['status'] == 1){
    	$inup=$db->updateAry("hostel_receipt",$ary,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$rid."' and idno='".$idno."' ");
    	echo "$inup";
    } else {
        
    	$rdetail = $db->getRow("select * from hostel_receipt where id='$rid' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");

    	$deletearay = array(
    	            "class"         =>  $rdetail['class'],
    	            "idno"          =>  $idno,
    	            "receipt_id"    =>  $rid,
    	            "paytype"       =>  "hostelreceipt",
    	            "payment_mode"  =>  $rdetail['payment_mode'],
    	            "amount"        =>  $rdetail['amount'],
    	            "detail"        =>  $rdetail['detail'],
    	            "status"        =>  1,
    	            "session"       =>  $_SESSION["admin"]["session"],
    	            "school_id"     =>  $_SESSION["admin"]["school_id"],
    	            "added_by"      =>  $_SESSION["admin"]["uid"]
    	    );
    
    	    $db->insertAry("delete_report", $deletearay);
    
    	    $rrids = explode("-", $rdetail['rrid']);
            
    	    foreach($rrids as $rids){
                $reffdata = $db->getRow("select * from reff_receipt where id='$rids' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."'  ");
    
                if($reffdata['type'] == "hostelfee"){
                    $inst_id = $reffdata['inst_id'];
                    $ins_name = "hp$inst_id";
                    $updatefee = "";
                    
                    $fee = $db->getRow("select i$inst_id, ip$inst_id from hostelinstallment where idno='$idno' and session='".$_SESSION["admin"]["session"]."' ");
                    $newfee = $fee["ip$inst_id"]-$reffdata['amount'];
                    if($newfee == 0){
                        $newfee = "";
                    }

                    $updatefee = array(
                            "ip$inst_id"    =>  $newfee
                        );
                        
                    $db->updateAry("hostelinstallment", $updatefee, "where school_id='".$_SESSION["admin"]["school_id"]."' and idno='$idno' and session='".$_SESSION["admin"]["session"]."'");
                        
                    $installmenttime = $db->getVal("select id from installment_time where ins_name='$ins_name' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");

                    $db->delete("installment_time", " where ins_name='$ins_name' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
                    $db->delete("transaction", " where rid='$idno' and tfor='hostel' and amount='$reffdata[amount]' and  session='".$_SESSION["admin"]["session"]."' and (tid LIKE '%ip$inst_id%') ");
                    $db->delete("reff_receipt", "where id='$rids' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' "); 

                }
                else{
                $inst_id = $reffdata['inst_id'];
                
                $transaction = $db->getRow("select * from transaction where rid='$idno' and tid='$inst_id' and amount='$reffdata[amount]' and  session='".$_SESSION["admin"]["session"]."' ");
                
                    if(count($transaction) > 1){
                        $type = str_replace("_fee","_detail","$transaction[tfor]");
                        $ins_name = "hostel$type";
    
                        $db->delete("transaction", " where rid='$idno' and tid='$inst_id' and amount='$reffdata[amount]' and  session='".$_SESSION["admin"]["session"]."' ");
                        $db->delete("reff_receipt", "where id='$rids' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
                        $db->delete("installment_time", " where ins_name='$ins_name' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
                        $db->delete("installment_fine", " where id='$inst_id' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
                        
                    }
                }

            }

            $db->delete("hostel_receipt", "where id='$rid' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' "); 
            echo "deleted";
            
    }

}

elseif($ch=="updateinventoryreceipt"){

    $ary = array(
            "payment_mode"  => $_REQUEST['mode'],
            "txnid"         => $_REQUEST['txnid'],
            "tdate"         => $_REQUEST['tdate'],
            "bankname"      => $_REQUEST['bankname']
        );
    $rid = $_REQUEST["rid"];
    $idno = $_REQUEST['idno'];

	$inup=$db->updateAry("inventory_receipt",$ary,"where school_id='".$_SESSION["admin"]["school_id"]."' and rid='".$rid."' and idno='".$idno."' ");

	echo "$inup";

}

elseif($ch=="updateinventoryreceiptstatus"){

    $ary = array(
            "rec_status"  => $_REQUEST['status']
        );
    $rid = $_REQUEST["rid"];
    $idno = $_REQUEST['idno'];
    if($_REQUEST['status'] == 1){
    	$inup=$db->updateAry("inventory_receipt",$ary,"where school_id='".$_SESSION["admin"]["school_id"]."' and rid='".$rid."' and idno='".$idno."' ");
    	echo "$inup";
    } else {
        
    	$rdetail = $db->getRow("select * from inventory_receipt where rid='$rid' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
        $stuclass = $db->getVal("select class from student where idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");

    	$deletearay = array(
    	            "class"         =>  $stuclass,
    	            "idno"          =>  $idno,
    	            "receipt_id"    =>  $rid,
    	            "paytype"       =>  "studentinventoryreceipt",
    	            "payment_mode"  =>  $rdetail['payment_mode'],
    	            "amount"        =>  $rdetail['total_amount'],
    	            "status"        =>  1,
    	            "session"       =>  $_SESSION["admin"]["session"],
    	            "school_id"     =>  $_SESSION["admin"]["school_id"],
    	            "added_by"      =>  $_SESSION["admin"]["uid"]
    	    );
    
    	    $db->insertAry("delete_report", $deletearay);

            $db->delete("inventory_receipt", "where rid='$rid' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' "); 
            echo "Deleted";
            // print_r($deletearay);

    }

}

elseif($ch=="updatefacultyinventoryreceipt"){

    $ary = array(
            "payment_mode"  => $_REQUEST['mode'],
            "txnid"         => $_REQUEST['txnid'],
            "tdate"         => $_REQUEST['tdate'],
            "bankname"      => $_REQUEST['bankname']
        );
    $rid = $_REQUEST["rid"];
    $idno = $_REQUEST['idno'];

	$inup=$db->updateAry("faculty_inventory_receipt",$ary,"where school_id='".$_SESSION["admin"]["school_id"]."' and rid='".$rid."' and idno='".$idno."' ");

	echo "$inup";

}

elseif($ch=="deletefacultyinventoryreceipt"){

    $ary = array(
            "rec_status"  => $_REQUEST['status']
        );
    $rid = $_REQUEST["rid"];
    $idno = $_REQUEST['idno'];
    if($_REQUEST['status'] == 1){
    	$inup=$db->updateAry("faculty_inventory_receipt",$ary,"where school_id='".$_SESSION["admin"]["school_id"]."' and rid='".$rid."' and idno='".$idno."' ");
    	echo "$inup";
    } else {
        
    	$rdetail = $db->getRow("select * from faculty_inventory_receipt where rid='$rid' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");
        // $stuclass = $db->getVal("select class from student where idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");

    	$deletearay = array(
    	            "class"         =>  "",
    	            "idno"          =>  $idno,
    	            "receipt_id"    =>  $rid,
    	            "paytype"       =>  "facultyinventoryreceipt",
    	            "payment_mode"  =>  $rdetail['payment_mode'],
    	            "amount"        =>  $rdetail['total_amount'],
    	            "status"        =>  1,
    	            "session"       =>  $_SESSION["admin"]["session"],
    	            "school_id"     =>  $_SESSION["admin"]["school_id"],
    	            "added_by"      =>  $_SESSION["admin"]["uid"]
    	    );
    
    	    $db->insertAry("delete_report", $deletearay);

            $db->delete("faculty_inventory_receipt", "where rid='$rid' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' "); 
            echo "Deleted";
            // print_r($deletearay);

    }

}

elseif($ch=="updatepocketreceipt"){

    $ary = array(
            "payment_mode"  => $_REQUEST['mode'],
            "txnid"         => $_REQUEST['txnid'],
            "tdate"         => $_REQUEST['tdate'],
            "bankname"      => $_REQUEST['bankname']
        );
    $rid = $_REQUEST["rid"];
    $idno = $_REQUEST['idno'];

	$inup=$db->updateAry("pocket_receipt",$ary,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$rid."' and idno='".$idno."' ");
	echo "$inup";
}

elseif($ch=="deletefees"){

    $id = $_REQUEST["id"];
    $idno = $_REQUEST['idno'];

	$db->delete("otherfee_structure", "where id='$id' and otype='$idno' and  session='".$_SESSION["admin"]["session"]."' "); 
            echo "Deleted";
}

elseif($ch=="updatereceiptEnquiry"){
    $ary = array(
            "payment_mode"  => $_REQUEST['mode'],
            "txnid"         => $_REQUEST['txnid'],
            "tdate"         => $_REQUEST['tdate'],
            "bankname"      => $_REQUEST['bankname']
        );
    $rid = $_REQUEST["rid"];
    $idno = $_REQUEST['idno'];

	$inup=$db->updateAry("enquiry_receipt",$ary,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$rid."' and idno='".$idno."' ");
	echo "$inup";
}

elseif($ch=="deletepocketreceipt"){

    $ary = array(
            "rec_status"  => $_REQUEST['status']
        );
    $rid = $_REQUEST["rid"];
    $idno = $_REQUEST['idno'];
    if($_REQUEST['status'] == 1){
    	$inup=$db->updateAry("pocket_receipt",$ary,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$rid."' and idno='".$idno."' ");
    	echo $inup;
    }
    else {

	$rdetail = $db->getRow("select * from pocket_receipt where id='$rid' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");

	$deletearay = array(
	            "class"         =>  $rdetail['class'],
	            "idno"          =>  $idno,
	            "receipt_id"    =>  $rid,
	            "paytype"       =>  "pocketreceipt",
	            "payment_mode"  =>  $rdetail['payment_mode'],
	            "amount"        =>  $rdetail['amount'],
	            "detail"        =>  $rdetail['detail'],
	            "status"        =>  1,
	            "session"       =>  $_SESSION["admin"]["session"],
	            "school_id"     =>  $_SESSION["admin"]["school_id"],
	            "added_by"      =>  $_SESSION["admin"]["uid"]
	    );

	    $db->insertAry("pocket_delete_report", $deletearay);

	    $rrids = explode("-", $rdetail['rrid']);
	    foreach($rrids as $rids){
            $db->delete("transaction", " where rid='$idno' and id='$rids' and  session='".$_SESSION["admin"]["session"]."' ");
	    }

            $db->delete("pocket_receipt", "where id='$rid' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' "); 
            echo "deleted";
    }
}


elseif($ch=="delete_enquiry_receipt"){

    $ary = array(
            "rec_status"  => $_REQUEST['status']
        );
    $rid = $_REQUEST["rid"];
    $idno = $_REQUEST['idno'];
    if($_REQUEST['status'] == 1){
    	$inup=$db->updateAry("enquiry_receipt",$ary,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$rid."' and idno='".$idno."' ");
    	echo $inup;
    }
    else {

	$rdetail = $db->getRow("select * from enquiry_receipt where id='$rid' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' ");

	$deletearay = array(
	            "class"         =>  $rdetail['class'],
	            "idno"          =>  $idno,
	            "receipt_id"    =>  $rid,
	            "paytype"       =>  "enquiry",
	            "payment_mode"  =>  $rdetail['payment_mode'],
	            "amount"        =>  $rdetail['amount'],
	            "detail"        =>  $rdetail['detail'],
	            "status"        =>  1,
	            "session"       =>  $_SESSION["admin"]["session"],
	            "school_id"     =>  $_SESSION["admin"]["school_id"],
	            "added_by"      =>  $_SESSION["admin"]["uid"]
	    ); 

	    $db->insertAry("delete_report", $deletearay);

	    $rrids = explode("-", $rdetail['rrid']);
	    foreach($rrids as $rids){
            $db->delete("transaction", " where rid='$idno' and id='$rids' and  session='".$_SESSION["admin"]["session"]."' ");
	    }

            $db->delete("enquiry_receipt", "where id='$rid' and idno='$idno' and  session='".$_SESSION["admin"]["session"]."' "); 
            echo "deleted";
    }
}

elseif($ch=="delete_note"){

    $id = $_REQUEST["id"];
    $idno = $_REQUEST['idno'];

	$db->delete("notes_detail", "where id='$id' and idno='$idno' "); 
            echo "Deleted";
}

?>