<?php
include("../db.php");
$ch=$_REQUEST["type"];$status="";
if($ch=="enquiry_data"){
	$where="";
	if(isset($_REQUEST["class"]))$where.=" AND apply_class='".$_REQUEST["class"]."' ";
	if(isset($_REQUEST["section"]) && $_REQUEST["section"]!="undefined")$where.=" AND section='".$_REQUEST["section"]."' ";
	$sql_limit="select * from enquiry where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' $where and status=1 $where order by name";
	$aryList=$db->getRows($sql_limit);
	
	$aryPgAct=array("src"=>"Enquiry", "page_id"=>"pay_fees","action"=>"add");
	if(is_array($aryList) && count($aryList)>0)
	{
		$aryData='{ "aaData": [
		';
	    $i=0;
		foreach($aryList as $iList) 
		{
			 $aryPgAct["id"]=$iList['idno'];
			$i++;
			//$aryData.='["'.$i.'",';
			$aryData.='["'.$i.'",';
			$aryData.='"'.POST($iList['name']).'",';
			$aryData.='"'.POST($iList['apply_class']).'",';
			$aryData.='"'.POST($iList['fname']).'",';
			$aryData.='"'.POST($iList['mobile']).'",';
			$aryData.='"'.POST($iList['discount_mode']).'",';
			$aryData.='"';
			$aryPgAct['action']="add";
			$aryPgAct['page_id']="pay_fees";
			$aryData.='<a href=\''.URL_ADMIN_HOME.getQueryString($aryPgAct).'\'><button type=\'button\' class=\'btn btn-info\'>Pay Amount</button></a>';
			$aryPgAct['action']="edit";
			$aryPgAct['page_id']="enquiry";
			$aryData.='<a href=\''.URL_ADMIN_HOME.getQueryString($aryPgAct).'\'><button type=\'button\' class=\'btn btn-info\'>Edit</button></a>';
			$aryPgAct['action']="delete";
			$aryData.='<a href=\'javascript:;\' onclick=\'del(\"'.URL_ADMIN_HOME.getQueryString($aryPgAct).'\")\' class=\'confirmation\'><button type=\'button\' class=\'btn btn-danger\'>Delete</button></a>';
			
			$aryData.='"]';
			if($i!=count($aryList))$aryData.=",";
		 }
		 $aryData.="] }";	
		 echo $aryData;	
	}else{
		echo $db->getLastQuery().$db->getErMsg();
	}
}
elseif($ch=="enquiry_data1"){
	$where="";
	if(isset($_REQUEST["class"]))$where.=" AND class='".$_REQUEST["class"]."' ";
	if(isset($_REQUEST["section"]) && $_REQUEST["section"]!="undefined")$where.=" AND section='".$_REQUEST["section"]."' ";
	$sql_limit="select * from enquiry where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' $where and status=1 $where order by name";
	$aryList=$db->getRows($sql_limit);
	
	$aryPgAct=array("src"=>"Enquiry","page_id"=>"pay_fees","action"=>"add");
	if(is_array($aryList) && count($aryList)>0)
	{
		$aryData='{ "aaData": [
		';
	    $i=0;
		foreach($aryList as $iList)
		{
			 $aryPgAct["id"]=$iList['idno'];
			$i++;
			//$aryData.='["'.$i.'",';
			$aryData.='["'.$i.'",';
			$aryData.='"'.POST($iList['name']).'",';
			$aryData.='"'.POST($iList['class']).'",';
			$aryData.='"'.POST($iList['fname']).'",';
			$aryData.='"'.POST($iList['mobile']).'",';
			$aryData.='"'.POST($iList['discount_mode']).'",';
			$aryData.='"';
			$aryPgAct['action']="add";
			$aryPgAct['page_id']="pay_fees";
			$aryData.='<a href=\''.URL_ADMIN_HOME.getQueryString($aryPgAct).'\'><button type=\'button\' class=\'btn btn-info\'>Pay Amount</button></a>';
			$aryPgAct['action']="edit";
			$aryPgAct['page_id']="enquiry";
			$aryData.='<a href=\''.URL_ADMIN_HOME.getQueryString($aryPgAct).'\'><button type=\'button\' class=\'btn btn-info\'>Edit</button></a>';
			//$aryPgAct['action']="delete";
			//$aryData.='<a href=\'javascript:;\' onclick=\'del(\"'.URL_ADMIN_HOME.getQueryString($aryPgAct).'\")\' class=\'confirmation\'><button type=\'button\' class=\'btn btn-danger\'>Delete</button></a>';
			
			$aryData.='"]';
			if($i!=count($aryList))$aryData.=",";
		 }
		 $aryData.="] }";	
		 echo $aryData;	
	}else{
		echo $db->getLastQuery().$db->getErMsg();
	}
}
elseif($ch=="checkreceptNo_enquiry"){
	$data=$db->getVal("select id from enquiry_receipt where id=".$_REQUEST["id"]." and type='".$_REQUEST["paytype"]."'");  
	if($data!="")
	echo "data already exit";
}
elseif($ch=="checkreceptNo_pocket"){
	$data=$db->getVal("select id from pocket_receipt where id=".$_REQUEST["id"]." and type='".$_REQUEST["paytype"]."'");  
	if($data!="")
	echo "data already exit";
}
elseif($ch=="feeother"){
	if($_REQUEST["fee"]==""){
		echo "please enter other fee";
	}elseif($_REQUEST["given"]==""){
		echo "please enter fee";
	}elseif(!is_numeric($_REQUEST["given"])){
		echo "fee must in numeric format";
	}elseif($_REQUEST["given"]==0){
		echo "fee must greater than 0";
	}else{
		   $trn=$db->getVal("Select amount from transaction where detail like 'Enquiryfee%' and rid=".$_REQUEST["student_id"]." and session='".$_SESSION["admin"]["session"]."' and school_id='".$_SESSION["admin"]["school_id"]."' and tid='".$_REQUEST["reasion"]."'");
		   $mobile=$db->getVal("Select mobile from enquiry where idno = '".$_REQUEST["student_id"]."'");
			if(!$trn) { 
// 			deposit($_REQUEST["given"],$_REQUEST["student_id"],$_REQUEST["reasion"],"cash","Enquiryfee ".$_REQUEST["fee"]);

            $data = array(	 
                     'rid'      =>  $_REQUEST["student_id"],
					 'tid'      =>  $_REQUEST["reasion"],
					 'tfor'     =>  "cash",
					 'type'     =>  "deposit",
					 'amount'   =>  $_REQUEST["given"],
					 'status'   =>  1,
					 "school_id"=>  $_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"],
					 'session'  =>  $_SESSION["admin"]["session"],
					 'detail'   =>  "Enquiryfee ".$_REQUEST["fee"]
					 );
        	$did = $db->insertAry('transaction',$data);

			if($did > 0)
			echo trim($did);
			
// 			sms(mydetail("$mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of Rs.".$_REQUEST["given"]." is successfully deposited for ".$_REQUEST["reasion"]." in ".$LinksDetails["site_name"],"fee_submit");
			
			}
			else echo " You Can not pay with same name..! Please try again";
			
	}
}
elseif($ch=="DeleteInstallment"){
	$flg=0;$ftype="";$r="";$where="";
	$trn=$db->getVal("Select id from transaction where detail like 'Enquiryfee%' and rid=".$_REQUEST["idno"]." and session='".$_SESSION["admin"]["session"]."' and school_id='".$_SESSION["admin"]["school_id"]."' and tid='".$_REQUEST["detail"]."'");
	$r=$db->getVal("Select id from enquiry_receipt where rrid=".$trn." and session='".$_SESSION["admin"]["session"]."' and school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."'");
	$ftype=$_REQUEST["paytype"];
	//echo $_REQUEST["udate"].$_REQUEST["detail"].$_REQUEST['amount'];exit;
	if($_REQUEST['amount']!="") {
			//print_r($ref);exit;
			$arr=array("idno"    =>$_REQUEST["idno"],
				   "class"   =>$_REQUEST["class"],
				   "paytype" =>$_REQUEST["paytype"],
				   "amount"  =>$_REQUEST["amount"],
				   "install" =>$_REQUEST["install"],
				    'status'  =>1,
				   "receipt_id"=>$r,
				   "detail"  =>$_REQUEST["detail"],
				  "school_id"=>$_SESSION["admin"]["school_id"],
				  "session"  =>$_SESSION["admin"]["session"],
				  "added_by" =>$_SESSION["admin"]["uid"]
				  
		 );
		 //print_r($arr);exit;
		 $flgIn=$db->insertAry("delete_report",$arr);
		 if(!is_null($flgIn))
			{$flg=1;}
	}//echo $db->getLastQuery();
  if($flg==1){
    if($_REQUEST["paytype"]=="enquiry"){
		$detail=$_REQUEST["install"]."-otherfee";
		$where="where detail like 'Enquiryfee%' and rid=".$_REQUEST["idno"]." and session='".$_SESSION["admin"]["session"]."' and school_id='".$_SESSION["admin"]["school_id"]."' and amount='".$_REQUEST["amount"]."' and tid='".$_REQUEST["detail"]."'";
		//echo $where;exit;
		echo $res=$db->delete("transaction","$where");
	}
  }
}

elseif($ch=="getrollno"){
    $rollno = $db->getVal("SELECT fincome FROM `student` WHERE class='$_POST[cls]' ORDER BY cast(fincome as unsigned) DESC LIMIT 1");
    echo $rollno+1;
}


elseif($ch=="getenrollment"){
    $session = $_POST["session"];

        $years=explode("-",$session);
        $year = $years[0];
        $newstring = substr($year,-2);

    	$s = $db->getVal("SELECT count(idno) from enquiry WHERE reg_no!='' and session='$session' ");

    	$noo=$s+1;
    	echo trim("$noo-$newstring");

}


elseif($ch=="followup_details"){

    $idno = $_POST["idno"];

    $details = $db->getRow("SELECT * FROM enquiry where idno='$idno' ");
    ?>
        
        <table class="table" border="1">
            <tr>
                <td>Student Name</td>
                <td><?php echo $details["name"]; ?></td>
            </tr>
            <tr>
                <td>Father Name</td>
                <td><?php echo $details["fname"]; ?></td>
            </tr>
            <tr>
                <td>Mother  Name</td>
                <td><?php echo $details["mname"]; ?></td>
            </tr>
            <tr>
                <td>Address</td>
                <td><?php echo $details["address"]; ?></td>
            </tr>
            <tr>
                <td>Mobile No</td>
                <td><?php echo $details["mobile"]; ?></td>
            </tr>
            <tr>
                <td>Date Of Birth</td>
                <td><?php echo $details["dob"]; ?></td>
            </tr>
            <tr>
                <td>Nationality</td>
                <td>INDIAN</td>
            </tr>
            <tr>
                <td>Gender</td>
                <td><?php echo $details["gender"]; ?></td>
            </tr>
            <tr>
                <td>Email Id</td>
                <td><?php echo $details["email"]; ?></td>
            </tr>
            <tr>
                <td>Applying For Class</td>
                <td><?php echo $details["apply_class"]; ?></td>
            </tr>
            <tr>
                <td>Enquiry Status</td>
                <td><?php echo $details["discount_mode"]; ?></td>
            </tr>
            <tr>
                <td>Lead Source</td>
                <td><?php echo $details["lead_source"]; ?></td>
            </tr>
            <tr>
                <td>Staff</td>
                <td><?php echo $db->getVal("select name from staff where idno='$details[follow_staff]' "); ?></td>
            </tr>
        </table>
        
    <?php
    // $ary = array(
    //         "follow_date"   =>  $_POST["date"],
    //         "follow_staff"  =>  $_POST["staff"],
    //         "follow_medium" =>  $_POST["medium"],
    //         "summary"       =>  $_POST["summary"],
    //     );
    // $flgup = $db->updateAry("enquiry", $ary, " where idno='$_POST[idno]' ");
    // echo $flgup;
}

elseif($ch=="followup_update"){
    $idno = $_POST["idno"];

    $detail = $db->getRow("SELECT * FROM enquiry where idno='$idno' ");
    ?>
    
            <table border="1" cellspacing="0" cellspacing="5" style="width:100%;">
            <tr>
                <th>Medium</th>
                <td>
                    <select id="follow_medium" class="input-block-level">
                        <option value="Direct" <?php if($detail["follow_medium"]=="Direct")echo "selected"; ?> >Direct</option>
                        <option value="Phone Call" <?php if($detail["follow_medium"]=="Phone Call")echo "selected"; ?>>Phone Call</option>
                        <option value="SMS" <?php if($detail["follow_medium"]=="SMS")echo "selected"; ?>>SMS</option>
                        <option value="Mail" <?php if($detail["follow_medium"]=="Mail")echo "selected"; ?>>Mail</option>
                    </select>
                </td>
            </tr>
            
            <tr>
                <th>Summary</th>
                <td>
                    <textarea rows="4" id="summary" class="input-block-level"><?php echo $detail["summary"]; ?></textarea>
                </td>
            </tr>

            <tr>
                <th>New Follow Up Required</th>
                <td>
                    <input type="radio" name="new" onchange="showhide(this.value)" value="1" /> Yes &emsp;
                    <input type="radio" name="new" onchange="showhide(this.value)" value="0" checked /> No
                </td>
            </tr>

            <tr class="showhiderow" style="display:none">
                <th>Follow Date</th>
                <td><input type="date" id="follow_date"  class="input-block-level" value="<?php echo $detail["follow_date"]; ?>"></td>
            </tr>

            <tr class="showhiderow"  style="display:none">
                <th>Follow By</th>
                <td>
					<select id="follow_staff" class="input-block-level">
					    <option value="" style="width:500px;">--Select--</option> 
						<?php 
							$location=$db->getRows("select * from staff where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 order by name ");
							if(is_array($location) && count($location)>0){
								foreach($location as $loc){
									?>
									<option <?php if($loc["idno"]==$detail["follow_staff"])echo 'selected';?> value="<?php echo $loc["idno"] ?>">
										<?php echo $loc["name"] ?>
									</option>
									<?php
								}
							}
						?>
					</select>
                </td>
            </tr>
            <tr>
                <th>Follow Status</th>
                <td>
                    <select id="follow_status" class="input-block-level">
                        <option value="1" <?php if($detail["follow_status"]=="1")echo "selected"; ?>>Open</option>
                        <option value="0" <?php if($detail["follow_status"]=="0")echo "selected"; ?>>Closed</option>
                    </select>
                </td>
            </tr>
        </table>
        <br>
        <div style="text-align:center;">
            <input type="button" value="Update" onclick="updateNow()" />
        </div>
    
    <?php
}

elseif($ch=="update_followup"){

    $ary = array(
            "follow_date"   =>  $_POST["date"],
            "follow_staff"  =>  $_POST["staff"],
            "follow_medium" =>  $_POST["medium"],
            "summary"       =>  $_POST["summary"],
            "follow_status" =>  $_POST["status"]
        );
    $flgup = $db->updateAry("enquiry", $ary, " where idno='$_POST[idno]' ");
    echo $flgup;
}


elseif($ch=="getapplication"){
    $session = $_POST["session"];

        $years=explode("-",$session);
        $year = $years[0];
        $newstring = substr($year,-2);

    	$s = $db->getVal("SELECT count(idno) from enquiry WHERE application_no!='' and session='$session' ");

    	$noo=$s+1;
    	echo trim("$noo-$newstring");

}

?>