<?php 
include ("../core/kfunctions.php");
adminall("overadmin","priaccount","secaccount");
?>

<html>
<head>
<?php kheader(); ?>
<link href="../css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="../js/scfjs.js" charset="UTF-8"></script>
<script>
function get_payment(pt,ph){
if(pt=="new"){ var phone = prompt("PLEASE ENTER THE PHONE NUMBER YOU WANT TO COLLECT THE PAYMENT FROM.");}
else if(pt=="exist"){ var phone=ph; }

if((phone=='' || phone==null) && pt=="new"){ alert("PLEASE ENTER THE PHONE NUMBER"); }
  else if(pt=="exist" || phone!=''){
 var client="Life";
  var amount=$("#receivedtotal").val();
  var podidx="pd001";
  var identifier="id001";
  var platform="Mediaduuka";

if(phone==''){alert("Please Enter the Phone Number");}
else{
  var conf=confirm("Do you want to collect SH."+amount+"/= FROM this phone number "+phone);
  if(conf==true){
      var form_data = new FormData();
form_data.append("phone", phone);
form_data.append("amount", amount);
form_data.append("platform", platform);
form_data.append("transid", podidx);
form_data.append("identifier", identifier);

  $.ajax({
   url: "submit_payment.php",
   type: "POST",
   data:  form_data,
   contentType: false,
         cache: false,
   processData:false,
   beforeSend : function(){ $("#success_txt").html("<div style='color:orange;margin:5px 0px 5px 0px;'><i> Please wait...!!! </i></div>");
   },
   success: function(data){
    var d=data.split("%^&");
    var ph=d[0];
    var tid=d[1];
    var statx=d[2];
    var msg=d[3];

   if(statx==1){ $("#success_txt").html("<div class='alert alert-success'>"+msg+"</div>"); pverify(tid,ph,amount,client,podidx,identifier); }
else{$("#success_txt").html("<div class='alert alert-danger'>Transaction Failed.</div>");}

 }
                
    });
  }
}  
return false } }
 
function pverify(tid,ph,amount,client,podidx,identifier){
var myVar = setInterval(function(){ myTimer(tid,ph,amount,client,podidx,identifier) }, 1000);

function myTimer(td,ph,amount,client,podidx,identifier) {
  var form_data = new FormData();
form_data.append("tid", td);
form_data.append("ph", ph);
form_data.append("podid", podidx);
form_data.append("amount", amount);
form_data.append("identifier", identifier);

$.ajax({
url:"get_confirm.php",
method:"POST",
data:form_data,
contentType:false,
cache:false,
processData:false,
success:function(data){ 
var d=data.split("$%^");
var statx=d[1];
var msg=d[2];
if(parseInt(statx)==2){ clearInterval(myVar); $("#success_txt").html("<div class='alert alert-success'>Successfully received "+amount+" from "+client+" with transaction ID "+tid+".</div>"); 
setTimeout(function(){location.reload();},2000);
}
else if(parseInt(statx)==1){ $("#success_txt").html(msg);}
else if(parseInt(statx)==0){ clearInterval(myVar); $("#success_txt").html(msg);}
 }

})

  
 }


setTimeout(function(){clearInterval(myVar)},90000);}


</script>


</head>
<body>

    <div id="wrapper">

        <!-- Navigation -->
        <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <!-- Brand and toggle get grouped for better mobile display -->
            <?php ktopbar(); ?>
            <!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
            <?php kleftbar(); ?>
            <!-- /.navbar-collapse -->
        </nav>

        <div id="page-wrapper">

            <div class="container-fluid">

      

                <!-- /.row -->

                

                  
<div class="col-lg-12">
<div class="panel panel-green">
<div class="panel-heading">
<h3 class="panel-title">Basic Details for <?php echo $_GET['cid'];?></h3>
</div>
<div class="panel-body">
<div class='col-lg-3'>
<?php
include ("../core/connect.php");
$fsid=$_GET["fsid"];
$cid8 = $_GET["cid"];
$result70 = $dbh->query("select * from children where cid='$cid8'");
$row70 = $result70->fetchObject();
$count70 = $result70->rowCount();

$schoolyear=$_GET["schoolyear"];
$t=$_GET["t"];


if($count70>0){
echo "
Photo &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: ".$row70->img."<br>
Name: ".$row70->fname." ".$row70->sname."

";
}else{
  echo "Sorry: There is NO match for the student";
}
?>

</div>

<div class="col-lg-4">
<?php
include ("../core/connect.php");
$cid8 = $_GET["cid"];
$result70 = $dbh->query("select * from children where cid='$cid8'");
$row70 = $result70->fetchObject();
$count70 = $result70->rowCount();

if($count70>0){
echo "
Gender&nbsp;&nbsp;&nbsp; : ".$row70->gender."<br>
Comments : ".$row70->comment."

";
}else{
  echo "Sorry: There is NO match for the student";
}
?>

                                </div>

                                <div class="col-lg-4">
<?php
include ("../core/connect.php");
$cid8 = $_GET["cid"];
$result70 = $dbh->query("select * from children where cid='$cid8'");
$row70 = $result70->fetchObject();
$count70 = $result70->rowCount();

if($count70>0){
echo "

Term&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ".$_GET["t"]."<br>
Class&nbsp;&nbsp;&nbsp;&nbsp;    : "; 

$schoolclass = $row70->schoolclass;
include ("../gentools/class.php");
 echo $daclass;
}else{
  echo "Sorry: There is NO match for the student";
}


$cid=$_GET['cid'];
$tid=$_GET['t'];
$fsid=$_GET['fsid'];
$schoolyear=$_GET['schoolyear'];

//useractivity("(cid ".$cid.")(term ".$tid.")(year ".$schoolyear.") transaction details","55");

if(isset($_POST["addreceived"])){
$yy=date("Y"); $fyy=substr($yy,2,2); $mm=date("m"); 
$dd=date("d"); $hi=date("h"); $mi=date("i");
$sa=date("sa"); $sa=date("s");

$transid="tr".$fyy.$mm.$dd.$hi.$mi.$sa;
$lastupdate=date('d-m-Y H:i:s');

$date_received=explode(" ",$_POST["date_received"]);
$ddate=$date_received[0];
$ttime=$date_received[1];
$date_reminder=$_POST["date_reminder"];
$moptotal=$_POST["moptotal"];
$term=$_POST["term"];

$pay_methods=array('bslip','mobile','cash','cheque','banktfer');
foreach($pay_methods as $pay_method){
  if(!empty($_POST['am_'.$pay_method])){
    if(isset($_POST['id_'.$pay_method])){$pay_id=$_POST['id_'.$pay_method];}
    else{$pay_id=$pay_method;}
    $amount=$_POST['am_'.$pay_method];
    $insert_transaction=$dbh->query("insert into $transactionstbl (cid,paymethod,transactionid,externalid,amount,ouruser) values('$cid','$pay_method','$transid','$pay_id','$amount','$rolenumber')");

  }
}

$result_scrapterm = $dbh->query("select * from scrap where item='$term' and type='term_year'");
$row_scrapterm = $result_scrapterm->fetchObject(); 

$result_childClass = $dbh->query("select * from children where cid='$cid'");
$row_childClass = $result_childClass->fetchObject(); 

$resultfsi = $dbh->query("select * from $bravostbl where cid='$cid' and fsitemid!='fullpay'");
$rowfsi = $resultfsi->fetchObject(); $countfsi = $resultfsi->rowCount();
for($x=1; $x<=$countfsi; $x++){
  if(!empty($_POST["current".$x.""])){
  $amount=$_POST["current".$x.""];
  $fsid=$_POST["fsid".$x.""];
  $fsitemid=$_POST["fsitemid".$x.""];
  
  $insert_homeland=$dbh->query("insert into $homelandtbl (cid,fsid,fsitemid,paytype,amount,ouruser,transactionid,paydate,time_received,schoolyear,schoolterm,schoolclass) values('$cid','$fsid','$fsitemid','c','$amount','$rolenumber','$transid','$ddate','$ttime','$row_scrapterm->item3','$row_scrapterm->item2','$row_childClass->schoolclass')");
  $update_bravo=$dbh->query("update $bravostbl set paid= paid + $amount , balance = balance - $amount , lastupdate='$lastupdate' where cid='$cid' and fsid='$fsid' and fsitemid='$fsitemid' and fsitemid!=''");
  $update_bravo2=$dbh->query("update $bravostbl set paid= paid + $amount , balance = balance - $amount , lastupdate='$lastupdate' where cid='$cid' and fsid='$fsid' and fsitemid=''");
  
}
$result_sump = $dbh->query("select sum(paid) from $bravostbl where cid='$cid' and fsitemid !='fullpay'");
$row_sump = $result_sump->fetch();

$result_sumb = $dbh->query("select sum(balance) from $bravostbl where cid='$cid' and fsitemid !='fullpay'");
  $row_sumb = $result_sumb->fetch();

$update_bravo_sum_total=$dbh->query("update $bravostbl set paid=$row_sump[0] , balance=$row_sumb[0] , lastupdate='$lastupdate' where cid='$cid' and fsitemid='fullpay'");
}

$insert_clo=$dbh->query("insert into $clotbl (transactionid,cid,totalpaid,ouruser,reminderdate,date_received,time_received,termid) values('$transid','$cid','$moptotal','$rolenumber','$date_reminder','$ddate','$ttime','$term')");


echo "<script>window.location='receipt.php?cid=".$cid."&t=".$tid."&schoolyear=".$schoolyear."&transactionid=".$transid."'</script>";}

?>

                                </div>

</div></div></div>



<div class="col-lg-4">
<form method="post" onsubmit="return FormValidation();">

  <input type='hidden' name='cid' value='<?php echo $cid8; ?>'>
<div class="panel panel-green">
<div class="panel-heading">
<h3 class="panel-title">Modes of Payment</h3>
</div>

<div class="panel-body">
  

  <div class='input-append date form_date col-lg-12' data-date='' data-date-format='yyyy-mm-dd H:i' data-link-field='dtp_input2' data-link-format='yyyy-mm-dd H:i'>
<input size='16' type='text' name='date_received' style='cursor:pointer' placeholder='Date Received' class='form-control readonly' autocomplete='off' required>
<span class='add-on'><i class='icon-th'></i></span>
</div>
<div class='col-lg-12'>
<select name='term' class='form-control' required>
<option value=''>Term</option>
<?php
$result_scrap = $dbh->query("select * from scrap where type='term_year'");
$row_scrap = $result_scrap->fetchObject();
$count_scrap = $result_scrap->rowCount();

if($count_scrap>0){
  do{
    echo "<option value='".$row_scrap->item."'>Term ".$row_scrap->item2.",  20".$row_scrap->item3."</option>";    
  }while($row_scrap = $result_scrap->fetchObject());
}
?>
</select>
</div>
  <div class='input-append date form_date col-lg-12' data-date='' data-date-format='yyyy-mm-dd' data-link-field='dtp_input2' data-link-format='yyyy-mm-dd'>
<input size='16' type='text' name='date_reminder' style='cursor:pointer' autocomplete='off' placeholder='Reminder date' class='form-control readonly'>
<span class='add-on'><i class='icon-th'></i></span>
</div>


<br>&nbsp;</br>
<div class='col-lg-6'>
  <input type='text' class='form-control' name='am_bslip' autocomplete='off' placeholder='Bslip /=' id="am_bslip" onKeyUp="updatebox()">
</div>
<div class='col-lg-6'>
  <input type='text' class='form-control' name='id_bslip' autocomplete='off' placeholder='Bslip ID'>
</div>
<div class='col-lg-6'>
  <input type='text' class='form-control' name='am_mobile' autocomplete='off' placeholder='MM amount /=' id="am_mobile" onKeyUp="updatebox()">
</div>
<div class='col-lg-6'>
  <input type='text' class='form-control' name='id_mobile' autocomplete='off' placeholder='MM ID'>
</div><p>&nbsp</p>

<div class='col-lg-12'>
  <input type='text' class='form-control' name='am_cash' autocomplete='off' placeholder='Cash /=' id="am_cash" onKeyUp="updatebox()">
</div> <p>&nbsp</p>
<div class='col-lg-6'>
  <input type='text' class='form-control' name='am_cheque' autocomplete='off' placeholder='Cheque /=' id="am_cheque" onKeyUp="updatebox()">
</div>
<div class='col-lg-6'>
  <input type='text' class='form-control' name='id_cheque' autocomplete='off' placeholder='Cheque ID' >
</div>
<div class='col-lg-6'>
  <input type='text' class='form-control' name='am_banktfer' autocomplete='off' placeholder='BankTfer /=' id="am_banktfer" onKeyUp="updatebox()">
</div>
<div class='col-lg-6'>
  <input type='text' class='form-control' name='id_banktfer' autocomplete='off' placeholder='BankTfer ID'>
</div>

<div class='col-lg-12'>
<textarea name='moptotal' placeholder='M.O.P Total' class='form-control readonly' rows='1'  id='moptotal' style='
color: green;
font-size: 30px;
text-align: center;' required readonly></textarea>
</div>


</div>
</div></div>

<?php $resultfs = $dbh->query("select * from scrap where item='$fsid'");
$rowfs = $resultfs->fetchObject();
$countfs = $resultfs->rowCount();?>

<div class="col-lg-8">
<div class="panel panel-green">
<div class="panel-heading">
<h3 class="panel-title">Received Payments</b></h3>
</div>
<div class="panel-body">
<div class='col-lg-12'>

<table class='table table-bordered table-hover table-striped sortable'>
<thead>
<tr>
<th>Item</th>
<th>Structure</th>
<th>Paid so far</th>
<th>Balance</th>
<th>Current</th>
</tr>
</thead>
<tbody>
  <?php 
$resultfsi = $dbh->query("select * from $bravostbl where cid='$cid' and fsitemid!='fullpay'");
$rowfsi = $resultfsi->fetchObject();
$countfsi = $resultfsi->rowCount();
if($countfsi>0){ $r=1; do 
  {$y=$r++;
$resultfsiname = $dbh->query("select * from fsitems where newitemclass='$row70->schoolclass' and school_id='".$school_id."'");
$rowfsiname= $resultfsiname->fetchObject();
$countfsiame = $resultfsiname->rowCount();

$result_scrap = $dbh->query("select * from scrap where item='$rowfsi->fsitemid'");
$row_scrap= $result_scrap->fetchObject();
$count_scrap = $result_scrap->rowCount();

    echo "
<tr>
<td>"; if($count_scrap>0){echo $row_scrap->item2;}
else{echo $rowfsiname->newitemname;} echo "</td>
<td>".number_format($rowfsi->payable)."</td>
<td>".number_format($rowfsi->paid)."</td>
<td>".number_format($rowfsi->balance)."</td>";

if ($rowfsi->balance<1){$readonlycheck="readonly";} else {$readonlycheck='';}
echo "
<td>
<input type='hidden' name='fsid".$y."' value='".$rowfsi->fsid."'>
<input type='hidden' name='fsitemid".$y."' value='".$rowfsi->fsitemid."'>
<input type='text' class='form-control' id='current".$y."' autocomplete='off' name='current".$y."' placeholder='Amount /=' onKeyUp='updatereceived()'".$readonlycheck.">
</td>
</tr>";} while ($rowfsi = $resultfsi->fetchObject());}

echo"
<tr>
<td colspan='4'>Bank charges</td>
<td><input type='text' class='form-control' name='bankcharges' id='bankcharges' placeholder='Amount' onKeyUp='updatereceived()'></td>
</tr>
</tbody>
</table>";
echo "
</div><div align='right'>
<input type='hidden' name='cid' value='$cid'>
<input type='hidden' name='tid' value='$tid'>
<input type='hidden' name='schoolyear' value='$schoolyear'>
<input type='hidden' name='schoolclass' value='$schoolclass'>
<div class='col-lg-8'>
<textarea name='receivedtotal' placeholder='Received Total' class='form-control readonly' rows='1'  id='receivedtotal' style='
color: green;
font-size: 30px;
text-align: center;' required readonly></textarea>
</div>
<div class='col-lg-4'>
<textarea class='form-control' name='comment' placeholder='Additional comments'></textarea>
</div>
<input type='hidden' name='termtotake' value='$tid'><br>&nbsp</br>
<input type='submit' class='btn btn-sm btn-primary' name='addreceived' value='Submit Form.'>
<input type='reset' class='btn btn-sm btn-primary'  value='Reset Form'>
</form>


";?>
<!--
<input type='hidden' id='phone'>
<button class='btn btn-lg btn-primary' onClick="get_payment('new','')" style='font-size:16px;width:100%;background-color:green;color:#fff;'>PAY USING ANOTHER NUMBER</button>

<div class='col-lg-12' id='success_txt'><br></div>
<button class='btn btn-lg btn-orange' onClick="get_payment('exist','0705540340')" style='font-size:16px;width:100%;background-color:maroon;color:#fff;'>PAY USING THIS NUMBER : 0705540340</button>
</div>
</div>
-->

</div></div>

                <!-- /.row -->


<script>
function updatereceived(){
  <?php
$resultfsi = $dbh->query("select * from $bravostbl where cid='$cid' and fsitemid!='fullpay'"); $countfsi = $resultfsi->rowCount();
for($x=1;$x<=$countfsi;$x++){
  ?>
if(document.getElementById("current<?php echo $x; ?>").value==''){var cur<?php echo $x; ?> = 0;}
else{var cur<?php echo $x; ?> = parseInt((document.getElementById("current<?php echo $x; ?>").value).replace(/,/g,"")); }
  
  <?php }  ?>
  if(document.getElementById("bankcharges").value==''){var bankcharges = 0;}
else{var bankcharges = parseInt((document.getElementById("bankcharges").value).replace(/,/g,"")); }


  var ctotal= 0 <?php for($z=1;$z<=$countfsi;$z++){ ?> + cur<?php echo $z; ?> <?php } ?> + bankcharges

  var ctotalout=ctotal.toLocaleString();
document.getElementById("receivedtotal").value= ctotal;
}
  </script>


                 
    <!-- /#wrapper -->

    <!-- jQuery -->
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script type="text/javascript" src="../js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../js/jquery-1.8.3.min.js" charset="UTF-8"></script>
<script type="text/javascript" src="../js/bootstrap-datetimepicker.js" charset="UTF-8"></script>
<script type="text/javascript">
    $('.form_datetime').datetimepicker({
        //language:  'fr',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1
    });
    $('.form_date').datetimepicker({
        language:  'en',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 0
    });
    $('.form_time').datetimepicker({
        language:  'en',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 1,
        minView: 0,
        maxView: 1,
        forceParse: 0
    });
</script>

    <!-- Bootstrap Core JavaScript -->

    

</body>

</html>