<?php
include("../../db.php");
header("Access-Control-Allow-Origin: * ");
date_default_timezone_set("Asia/kolkata");

$count_month=$_REQUEST['countmonth'];
$id= $_REQUEST['idno'];
$_GET["id"]= $id;
$total=$_REQUEST['total']; // payment given by parent 
$m=$_REQUEST['month'];
$session=$_REQUEST['session'];
$clas= $_REQUEST['class'];
$pstatus = $_REQUEST['pstatus'];
$ptype = $_REQUEST['ptype'];
$txnid = $_REQUEST['txnid'];
$tdate = $_REQUEST['tdate'];
$pay_type='online';
$_SESSION["admin"]["session"]=$session;
$_POST["select_date"]=date('Y-m-d H:i:s');
$_SESSION["admin"]["uid"]='1';
$_SESSION["admin"]["school_id"]='1';

$tution=$db->getRow("SELECT * from installment  WHERE idno='$id' and session='$session'" );

for($x=1; $x<=$m; $x++){

    if(!empty($tution[i.$x])){
        
        $df=$tution["i$x"]-$tution["ip$x"];

        if($df!=0){
            if($df>=$total){
                $recept="fee-tution";
                $recData[$recept]+=$total;
                $reff_receipt=array(
                    "type"=>"fee",
                    "inst_id"=>"$x",
                    "amount"=>$total,
                    "idno"=>$_GET["id"],
                    "session"=>$_SESSION["admin"]["session"]
                );

                $insAry=array( "ip".($x)=>$total);
                $given=$total;
                $given1=$total;

                $rest_data='';
                $total= $rest_data;
            }

            if($df<$total){
                $t=$tution["i$x"]-$tution["ip$x"];
                $rest_data=$total-$df;
                $recept="fee-tution";
                // $recData[$recept]+=$tution[i.$x];
                $recData[$recept]+=$df;
                $reff_receipt=array(
                    "type"=>"fee",
                    "inst_id"=>"$x",
                    // "amount"=>$tution[i.$x],
                    "amount"=>$df,
                    "idno"=>$_GET["id"],
                    "session"=>$_SESSION["admin"]["session"]
                );

                $insAry=array( "ip".($x)=>$tution[i.$x]);
                // $given=$tution[i.$x];
                $given=$df;
                $given1=$tution[i.$x]-$tution[ip.$x];
                $total= $rest_data;
            }

            $inup=$db->query("update installment set ip$x=COALESCE(ip$x,0)+'".$given1."' where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."'");

            installment_time($_GET["id"],"ip".$x,$_POST["select_date"]);

            $reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
            $reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
            $reff_receipt["date"]=$_POST["select_date"];
            $reff_receipt["pay_type"]= $pay_type;

            $rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
            $receipt_data["Tution_Fees"]+=$given;
            $insAry=array( "ip".($x)=>$tution[i.$x]);
            deposit($given,$_GET["id"],json_encode($insAry),"fee","Student fee Payment in installment type");

        }
    }

    if(!empty($tution[b.$x])){

        $df=$tution["b$x"]-$tution["bp$x"];
        if($df!=0){
            if($df>=$total){
                $recept="fee-bus";
                $recData[$recept]+=$total;
                $reff_receipt=array(
                    "type"=>"busfee",
                    "inst_id"=>"$x",
                    "amount"=>$total,
                    "idno"=>$_GET["id"],
                    "session"=>$_SESSION["admin"]["session"]
                );

                $insAry=array( "bp".($x)=>$total);
                $given=$total;
                $given1=$total;
                $rest_data='';
                $total= $rest_data;
            }

            if($df<$total)
            {
                $t=$tution["b$x"]-$tution["bp$x"];
                $rest_data=$total-$df;
                $recept="fee-bus";
                // $recData[$recept]+=$tution[b.$x];
                $recData[$recept]+=$df;
                $reff_receipt=array(
                    "type"=>"busfee",
                    "inst_id"=>"$x",
                    // "amount"=>$tution[b.$x],
                    "amount"=>$df,
                    "idno"=>$_GET["id"],
                    "session"=>$_SESSION["admin"]["session"]
                );

                $insAry=array( "bp".($x)=>$tution[b.$x]);
                // $given=$tution[b.$x];
                $given=$df;
                $given1=$tution[b.$x]-$tution[bp.$x];
                $total= $rest_data;
            } 

            $inup=$db->query("update installment set bp$x=COALESCE(bp$x,0)+'".$given1."' where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."'");

            installment_time($_GET["id"],"bp".$x,$_POST["select_date"]);

            $reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
            $reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
            $reff_receipt["date"]=$_POST["select_date"];
            $reff_receipt["pay_type"]= $pay_type;

            $rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
            $receipt_data["Bus_Fees"]+=$given;
            $insAry=array( "bp".($x)=>$tution[b.$x]);
            deposit($given,$_GET["id"],json_encode($insAry),"fee","Student fee Payment in installment type");

        }

    }

    //extra	start
    $extrafee_list= $db->getRows("SELECT id from extrafee_type  WHERE class='$clas' and session='".$_SESSION["admin"]["session"]."' ");

    foreach($extrafee_list as $extra_list){
        $extra_list1[]=$extra_list[id];
    }

    $extralist=implode(',',$extra_list1);

    $type=$db->getRows("SELECT DISTINCT(type)  from extrafee  WHERE type in ($extralist) and  idno='$id' and session='".$_SESSION["admin"]["session"]."'");

    if(!empty($total)){
        foreach($type as $type1){

                    $extra_selected12=$db->getRow("SELECT *  from extrafee  WHERE   idno='$id' and pid='p".$x."' and type='$type1[type]' and session='".$_SESSION["admin"]["session"]."'");

                    $type_name= $db->getVal("SELECT type  from extrafee_type  WHERE id='$extra_selected12[type]' and session='".$_SESSION["admin"]["session"]."'");

                    $extra_next=$db->getRow("SELECT *  from extrafee  WHERE   idno='$id' and pid='p$x' and type='$type1[type]' and session='".$_SESSION["admin"]["session"]."'");

                    $df=$extra_selected12[fee]-$extra_selected12[pay];
                    if($df!=0){
                        if($df>=$total)
                        {
                            $monthary[]=$x;
                            $recept="fee-".$type_name;
                            $recData[$recept]+=$total;

                            $extra_inp1= array(pay=>$total);

                            $oldFee=$db->getVal("select pay from extrafee where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."' and pid='p$x' and type='".$type1["type"]."'");

                            $g=$total+$oldFee;
                            $insAry=array( "pay"	=>	$g);

                            $inup1=$db->updateAry("extrafee",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."' and pid='p$x' and type='".$type1["type"]."'");

                            // reff receipt	table insert
                            $reff_receipt=array(
                                "amount"=>$total,
                                "type"=>"extrafee".$extra_selected12[type],
                                "idno"=>$id,
                                "session"=>$_SESSION["admin"]["session"]
                            );

                            $reff_receipt["inst_id"]=$x;
                            $reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
                            $reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
                            $reff_receipt["date"]=$_POST["select_date"];
                            $reff_receipt["pay_type"]= $pay_type;
                            $rrid[]=$db->insertAry("reff_receipt",$reff_receipt);

                            // transaction table insert
                            $insAryJ=array( "pay".$x	=>	$total);

                            $extra_type=$db->getVal("SELECT type  from extrafee_type  WHERE   id='$extra_selected12[type]' ");

                            deposit($extra_selected12[fee],$id,json_encode($insAryJ),$extra_type,"Student ".$extra_type." fee Payment in installment type");

                            $receipt_data[$extra_type]+=$total;

                            $rest_data='';

                            $total= $rest_data;
                        }

                        if($df<$total)
                        {
                            if(!empty($extra_selected12[fee])){
                                $monthary[]=$x;
                                $recept="fee-".$type_name;
                                $recData[$recept]+=$extra_selected12[fee];

                                $after_extra_rest_data=$total-$df;

                                $oldFee=$db->getVal("select pay from extrafee where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."' and pid='p$x' and type='".$type1["type"]."'");
                                $g=$df+$oldFee;
                                $insAry=array( "pay"	=>	$g);

                                $inup1=$db->updateAry("extrafee",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."' and pid='p$x' and type='".$type1["type"]."'");

                                // reff receipt
                                $reff_receipt=array(
                                    "amount"=>$extra_selected12[fee],
                                    "type"=>"extrafee".$extra_selected12[type],
                                    "idno"=>$id,
                                    "session"=>$_SESSION["admin"]["session"]
                                );

                                $reff_receipt["inst_id"]=$x;
                                $reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
                                $reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
                                $reff_receipt["date"]=$_POST["select_date"];
                                $reff_receipt["pay_type"]= $pay_type;
                                $rrid[]=$db->insertAry("reff_receipt",$reff_receipt);

                                // transaction table insert

                                $insAryJ=array( "pay".$x	=>	$extra_selected12[fee]);

                                $extra_type=$db->getVal("SELECT type  from extrafee_type  WHERE   id='$extra_selected12[type]' ");

                                deposit($extra_selected12[fee],$id,json_encode($insAryJ),$extra_type,"Student ".$extra_type." fee Payment in installment type");

                                $receipt_data[$extra_type]+=$extra_selected12[fee];

                                $total=$after_extra_rest_data;
                            }
                        }

                    }

            if($e=='0') { break; exit(); }

        }

    }
    // ext end


    $aryForm=$db->getRow("SELECT * FROM student WHERE school_id='".$_SESSION["admin"]["school_id"]."' and idno='$id'");

    $other_fee_name=$db->getRows("SELECT  id,name from otherfee_structure  WHERE class='$clas' and month='$x' and (otype='$id' or otype='$aryForm[stype]' or otype='') and session='".$_SESSION["admin"]["session"]."' ");

    $other_name=$db->getRows(" SELECT @serial := @serial+1 AS serial_number, id,name from otherfee_structure,(SELECT @serial:= 0) AS s  WHERE class='$clas' and (otype='$id' or otype='$aryForm[stype]' or otype='') and session='".$_SESSION["admin"]["session"]."' " );

    $n=0;
    $a= array();
    foreach($other_name as $name){
        $n++;
        $a[]=$name[name];
    }
    foreach($other_fee_name as $other_name){
        foreach($a as $key=>$value){

            if($value==$other_name[name]){
                $n= $key+1;
            }
        }

        $total_other=$db->getVal("SELECT COALESCE(sum(fee),0) as result from otherfee_structure  WHERE class='$clas' and name='$other_name[name]'  and (otype='$id'  or otype='$aryForm[stype]' or otype='') and session='".$_SESSION["admin"]["session"]."' ");


        $total_other_discount=$db->getVal("SELECT COALESCE(sum(discount),0) as result from otherfee_discount  WHERE  idno='$id' and other_id='$other_name[id]'  and month='$x'   and session='".$_SESSION["admin"]["session"]."' ");

        $total_other_given=$db->getVal("SELECT COALESCE(sum(fine),0) as result from installment_fine  WHERE  idno='$id' and reasion ='$other_name[name]'   and session='".$_SESSION["admin"]["session"]."' ");

        $final= $total_other-$total_other_discount-$total_other_given;

        $num=$n*2;

        if(!empty($total)){
            if(!empty($final)){

                if(empty($total_other_given)){
                    $monthary[]=$x;

                    $udate=$_POST["select_date"];

                    installment_time($id,"other".$num."_detail",$_POST["select_date"]);

                    if($final>=$total){
                        $recept="fee-".$other_name[name];
                        $recData[$recept]+=$total;

                        $insAry=array(  "ins_name"	=>	$num."-otherfee",
                            "fine"		=>	$total,
                            "reasion"	=>	$other_name[name],
                            'udate'		=>  $_POST["select_date"],
                            "status"	=>	"1",
                            "idno"		=>	$id
                        );

                        $insAry["school_id"]=$_SESSION["admin"]["school_id"];
                        $insAry["added_by"]=$_SESSION["admin"]["uid"];
                        $insAry["session"]=$_SESSION["admin"]["session"];

                        $inup=$db->insertAry("installment_fine",$insAry);

                        // reff receipt start
                        $reff_receipt=array("amount"=>$total,
                            "type"=>$other_name[name],
                            "idno"=>$id,
                            "date"=>$udate,
                            "session"=>$_SESSION["admin"]["session"]
                        );
                        $reff_receipt["inst_id"]=$inup;
                        $reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
                        $reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
                        $reff_receipt["date"]=$_POST["select_date"];
                        $reff_receipt["pay_type"]= $pay_type;
                        $rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
                        // reff receipt end

                        deposit($total,$id,$inup,"other".$num."_fee","Student Other fee ".$other_name[name]);
                        $n=$other_name[name];
                        $receipt_data[$n]+=$total;
                        $rest_data='';

                        $total= $rest_data;

                    }

                    if($final<$total){

                        $recept="fee-".$other_name[name];
                        $recData[$recept]+=$final;

                        $after_other_rest_data=$total-$final;
                        $insAry=array(  "ins_name"	=>	$num."-otherfee",
                            "fine"		=>	$final,
                            "reasion"	=>	$other_name[name],
                            'udate'		=>  $_POST["select_date"],
                            "status"	=>	"1",
                            "idno"		=>	$id
                        );
                        $insAry["school_id"]=$_SESSION["admin"]["school_id"];
                        $insAry["added_by"]=$_SESSION["admin"]["uid"];
                        $insAry["session"]=$_SESSION["admin"]["session"];

                        $inup=$db->insertAry("installment_fine",$insAry);

                        // reff receipt start

                        $reff_receipt=array("amount"=>$final,
                            "type"=>$other_name[name],
                            "idno"=>$id,
                            "date"=>$udate,
                            "session"=>$_SESSION["admin"]["session"]
                        );
                        $reff_receipt["inst_id"]=$inup;
                        $reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
                        $reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
                        $reff_receipt["date"]=$_POST["select_date"];
                        $reff_receipt["pay_type"]= $pay_type;

                        $rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
                        // reff receipt end

                        deposit($final,$id,$inup,"other".$num."_fee","Student Other fee ".$other_name[name]);
                        $n=$other_name[name];
                        // $tempfinal+=$final;
                        $receipt_data[$n]+=$final;

                        $total=$after_other_rest_data;
                    }
                }
                else{
                    $monthary[]=$x;
                    if($final>=$total){

                        $recept="fee-".$other_name[name];
                        $recData[$recept]+=$total;

                        $insAry=array(  "ins_name"	=>	$num."-other",
                            "fine"		=>	$total,
                            "reasion"	=>	$other_name[name],
                            'udate'		=>  $_POST["select_date"],
                            "status"	=>	"1",
                            "idno"		=>	$id

                        );
                        $insAry["school_id"]=$_SESSION["admin"]["school_id"];
                        $insAry["added_by"]=$_SESSION["admin"]["uid"];
                        $insAry["session"]=$_SESSION["admin"]["session"];

                        $inup=$db->insertAry("installment_fine",$insAry);

                        // reff receipt start
                        $reff_receipt=array("amount"=>$total,
                            "type"=>$other_name[name],
                            "idno"=>$id,
                            "date"=>$udate,
                            "session"=>$_SESSION["admin"]["session"]
                        );
                        $reff_receipt["inst_id"]=$inup;
                        $reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
                        $reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
                        $reff_receipt["date"]=$_POST["select_date"];
                        $reff_receipt["pay_type"]= $pay_type;

                        $rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
                        // reff receipt end
                        deposit($total,$id,$inup,"other".$num."_fee","Student Other fee ".$other_name[name]);

                        $n=$other_name[name];
                        $receipt_data[$n]+=$total;

                        $rest_data='';
                        $total= $rest_data;
                    }

                    if($final<$total){
                        $recept="fee-".$other_name[name];
                        $recData[$recept]+=$final;

                        $after_other_rest_data=$total-$final;
                        $insAry=array(  "ins_name"	=>	$num."-other",
                            "fine"		=>	$final,
                            "reasion"	=>	$other_name[name],
                            'udate'		=>  $_POST["select_date"],
                            "status"	=>	"1",
                            "idno"		=>	$id
                        );
                        $insAry["school_id"]=$_SESSION["admin"]["school_id"];
                        $insAry["added_by"]=$_SESSION["admin"]["uid"];
                        $insAry["session"]=$_SESSION["admin"]["session"];

                        $inup=$db->insertAry("installment_fine",$insAry);

                        // reff receipt start
                        $reff_receipt=array("amount"=>$final,
                            "type"=>$other_name[name],
                            "idno"=>$id,
                            "date"=>$udate,
                            "session"=>$_SESSION["admin"]["session"]
                        );
                        $reff_receipt["inst_id"]=$inup;
                        $reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
                        $reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
                        $reff_receipt["date"]=$_POST["select_date"];
                        $reff_receipt["pay_type"]= $pay_type;
                        $rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
                        // reff receipt end
                        deposit($final,$id,$inup,"other".$num."_fee","Student Other fee ".$other_name[name]);
                        $n=$other_name[name];
                        $receipt_data[$n]+=$final;
                        $total=$after_other_rest_data;
                    }
                    $e =$total-$after_other_rest_data;
                }
            }
        }
    }
 
}

for($i=1; $i<=$m; $i++){
    $m1[]=$i;
}
$m2= implode(',',$m1);
$rrid7= implode('-',$rrid);
$ssp=$_REQUEST['countmonth'];

$sr=trim($ssp,"[ ]");

foreach($receipt_data as $key=>$val){
    $receipt_data[$key] = "".$val;
}

 $deta= json_encode($receipt_data);

$id=$db->getval("select max(id) from receipt where school_id='".$_SESSION["admin"]["school_id"]."' and type='fee'")+1;

$rec1= array(
    'id'=>$id,
    'idno'=>$_GET["id"],
    "udate"=>date('Y-m-d'),
    "topay"=>$deta,
    "detail"=>$deta,
    "amount"=>$_REQUEST['total'],
    "payment_mode"=>"online",
    "remaining"=>0,
    "total"=>$_REQUEST['total'],
    "Months"=>$sr,
    "session"=>$session,
    "status"=>0,
    "class"=>$clas,
    "type"=>"fee",
    "install"=>$m,
    "paytype"=>"overallFee",
    "school_id"=>1,
    "added_by"=>1,
    "rrid"=>$rrid7,
    "txnid" => $txnid,
    "tdate" => $tdate
);

$db->insertAry("receipt",$rec1);

$api_path = "https://www.savarnacloud.com/online_payment/";
$host_name = explode('.', $_SERVER['HTTP_HOST']);
if ($host_name[1] == 'savarna'){
    $api_path = "https://onlinepayment.savarna.in/";
}


if(is_array($rrid) && count($rrid)>0)
{
    echo json_encode(array('status' => 1, 'msg'=>'Data updated Successfully..!'));
    header("location:".$api_path."process.php?payment_status=$pstatus&txnid=$txnid&status=1&msg=Data updated successfully.");
} else
{
    echo json_encode(array('status' => 0, 'msg'=>'Data not found..!'));
    header("location:".$api_path."process.php?payment_status=$pstatus&txnid=$txnid&status=0&msg=Data not found.");
}