<?php
session_start();
// ============
include("../../asset/theme/css/myfunc.php");
include("../../php_crud/my_class.php");
$obj=new my_class();
extract($_POST);

//echo "Operation=$operation<br/>id=$id<br/>discount=$discount<br/>payment_date=$monthly_fee_pay_date";
if($operation=="feepayConfirm"){
	$ret_msg = "";
	$monthly_fee_pay_date;
	$full_pay_date = date("Y-m-d",strtotime($monthly_fee_pay_date));
	$discount;
	$table_name="`_fee_master_invoice` ";
    $column="*";
    $where_cond="_id='$id'";
    $fee_master_invoice_data=$obj->View_column_details_By_Cond($table_name,$column,$where_cond);
    $date = date('Y-m-d');
	
	$_br_id=$fee_master_invoice_data["_br_id"];
    $_class_id=$fee_master_invoice_data["_class_id"];
    $_section_id=$fee_master_invoice_data["_section_id"];
    $_std_id=$fee_master_invoice_data["_std_id"];
    $_invoice_no=$fee_master_invoice_data["_invoice_no"];
    $_month=$fee_master_invoice_data["_month"];
    $_year=$fee_master_invoice_data["_year"];
    $_total_amount=$fee_master_invoice_data["_total_amount"];
    $_due_amount=$fee_master_invoice_data["_due_amount"];
    $_paid_amount=$fee_master_invoice_data["_paid_amount"];
    $_status=$fee_master_invoice_data["_status"];
    
	if($_status=="U"){
        $receive_amount_aftertransacton=$_due_amount;
		
		if($discount==null || $discount==0 || $discount==""){
			$discount=0;
		}
		else{
			
			$receive_amount_aftertransacton= $_due_amount-$discount;

			$table_name="_fee_details_invoice";
			$column="*";
			$where_cond="_invoice_no='$_invoice_no'";
			foreach ($obj->View_all_data_row_column_By_Cond($table_name,$column,$where_cond) as $value){
				$form_data=array(
					'_adjust_amount'=>$value["_amount"]
				);
				$update=$obj->Update_Data('_fee_details_invoice', $form_data, "_invoice_no='".$_invoice_no."' AND _month='$_month' AND _year='$_year' and `_fee_head`='".$value["_fee_head"]."'  ");
			}
		
			/*$form_data=array(
				'_br_id'=>$_br_id,
				'_class_id'=>$_class_id,
				'_section_id'=>$_section_id,
				'_invoice_no'=>$_invoice_no,
				'_month'=>$_month,
				'_year'=>$_year,
				'_std_id'=>$_std_id,
				'_entry_date'=>$date,
				'_fee_head'=>'Discount',
				'_amount'=>$discount,
				'_adjust_amount'=>$discount,
				'_type'=>"D"
			);
			$entry=$obj->Insert_Data('_fee_details_invoice', $form_data);*/
		}

		$form_data=array(
			'_br_id'=>$_br_id,
			'_class_id'=>$_class_id,
			'_section_id'=>$_section_id,
			'_invoice_no'=>$_invoice_no,
			'_month'=>$_month,
			'_year'=>$_year,
			'_std_id'=>$_std_id,
			'_entry_date'=>$date,
			'_receive_amount'=>$receive_amount_aftertransacton,
			'_transection_id'=>$transactionid,
			'_payment_mode'=>'0',
			'_bank_name'=>""
		);

		$entry=$obj->Insert_Data('_fee_total_receive', $form_data);

		$form_data=array(
            '_due_amount'=>'0',
            '_paid_amount'=>$receive_amount_aftertransacton,
			'_payment_date'=>$full_pay_date,
            '_last_paid_amount'=>$receive_amount_aftertransacton,
            '_last_payment_date'=>$date,
            '_discount'=>$discount,
            '_status'=>'P'
        );
        $update=$obj->Update_Data('_fee_master_invoice', $form_data, "_id=".$id);
		if($update){
			$ret_msg = "Payment has been made successfully";
		}
		else{
			$ret_msg ="Error occurred during payment submission";
		}
	}
	else{
		$ret_msg = "Unknown Request";
	}
	echo $ret_msg;
}

if($operation=="feePartialPayConfirm"){
	//echo "inside feePartialPayConfirm<br/>$id<br/>$monthly_fee_par_pay_date";
	$ret_msg = "";
	$par_pay_date = date("Y-m-d",strtotime($monthly_fee_par_pay_date));
	$partialPayAmount;
	
	$feeHeads=rtrim($_REQUEST["feeHeads"],',');
    $expfeeheads=explode(",",$feeHeads);
    $expfeeheadsarray=array();

    foreach ($expfeeheads as $value)
    {
		$expfeeheads_new=explode(":",$value);
		$expfeeheadsarray[$expfeeheads_new[0]]=$expfeeheads_new[1];
	}

	$table_name="`_fee_master_invoice` ";
    $column="*";
    $where_cond="_id='$id'";
    $fee_master_invoice_data=$obj->View_column_details_By_Cond($table_name,$column,$where_cond);
    $date = date('Y-m-d');

    $_br_id=$fee_master_invoice_data["_br_id"];
    $_class_id=$fee_master_invoice_data["_class_id"];
    $_section_id=$fee_master_invoice_data["_section_id"];
    $_std_id=$fee_master_invoice_data["_std_id"];
    $_invoice_no=$fee_master_invoice_data["_invoice_no"];
    $_month=$fee_master_invoice_data["_month"];
    $_year=$fee_master_invoice_data["_year"];
    $_total_amount=$fee_master_invoice_data["_total_amount"];
    $_due_amount=$fee_master_invoice_data["_due_amount"];
    $_paid_amount=$fee_master_invoice_data["_paid_amount"];
    $_status=$fee_master_invoice_data["_status"];
	
	if($_status!="P")
    {
        $receive_amount_aftertransacton=0;
       
        if($partialPayAmount==null || $partialPayAmount==0 || $partialPayAmount=="" ){
            $partialPayAmount=0;
            echo "<div class='alert alert-danger' role='alert'>Unknown Request</div>"; exit;
        }
		else{
            $due_amount_after= $_due_amount-$partialPayAmount;
            $_adjust_amount_dtls_after_total=0;
            foreach ($expfeeheadsarray as $key=>$value){
                $_adjust_amount_dtls_after=0;
                $table_name="`_fee_details_invoice` ";
                $column="_adjust_amount";
                $where_cond="_id='".$key."' AND _month='$_month' AND _year='$_year'";
                $fee_dtls_invoice_data=$obj->View_all_data_row_column_By_Cond($table_name,$column,$where_cond);

                $_adjust_amount_dtls=$fee_dtls_invoice_data[0]["_adjust_amount"];
                if($_adjust_amount_dtls=="" || $_adjust_amount_dtls==null)
                {
                    $_adjust_amount_dtls=0;
                }
                $_adjust_amount_dtls_after=$_adjust_amount_dtls+$value;

                $_adjust_amount_dtls_after_total=$_adjust_amount_dtls_after_total+$_adjust_amount_dtls_after;

                $form_data=array(
                    '_adjust_amount'=>$_adjust_amount_dtls_after
                );
                $update=$obj->Update_Data('_fee_details_invoice', $form_data, "_id='".$key."' AND _month='$_month' AND _year='$_year' ");

            }
        }
        if($due_amount_after=="0"){
            $status_master="P";
        }
        else{
            $status_master="PR";
        }

        $form_data1=array(
            '_br_id'=>$_br_id,
            '_inv_master_id'=>$id,
            '_invoice'=>$_invoice_no,
            '_par_amount'=>$partialPayAmount,
            '_status'=>'1',
            '_entry_date'=>$date

        );

        $entry=$obj->Insert_Data('_fee_partial_pay_table', $form_data1);



        $form_data=array(
            '_br_id'=>$_br_id,
            '_class_id'=>$_class_id,
            '_section_id'=>$_section_id,
            '_invoice_no'=>$_invoice_no,
            '_month'=>$_month,
            '_year'=>$_year,
            '_std_id'=>$_std_id,
            '_entry_date'=>$date,
            '_receive_amount'=>$partialPayAmount,
            '_transection_id'=>$transactionid,
            '_payment_mode'=>'0',
            '_bank_name'=>""
        );

        $entry=$obj->Insert_Data('_fee_total_receive', $form_data);


        $form_data=array(
            '_due_amount'=>$due_amount_after,
            '_paid_amount'=>$_adjust_amount_dtls_after_total,
			'_payment_date'=>$par_pay_date,
            '_last_paid_amount'=>$receive_amount_aftertransacton,
            '_last_payment_date'=>$date,
            '_status'=>$status_master
        );
        $update=$obj->Update_Data('_fee_master_invoice', $form_data, "_id=".$id);
		if($update){
			$ret_msg = "Payment has been made successfully";
		}
		else{
			$ret_msg ="Error occurred during payment submission";
		}
    }
    else{
		$ret_msg = "Unknown Request";
	}
	echo $ret_msg;
	
}
?>