<!DOCTYPE html>
<html>
<head>
	<?php include('include/head.php'); ?>
	
	<!-- <title>Add Gallery </title> -->
    <style>
        .preview_box{clear: both; padding: 5px; margin-top: 10px; text-align: left;}
        .preview_box img{max-width: 150px; max-height: 150px;}
    </style>
   
</head>
<body>
	<?php include('include/header.php'); ?>
	<?php include('include/sidebar.php'); ?>
	<div class="main-container">
		<div class="pd-ltr-20 customscroll customscroll-10-p height-100-p xs-pd-20-10">
			<div class="min-height-200px">
				<div class="page-header">
					<div class="row">
						<div class="col-md-10 col-sm-12">
							<div class="title">
								<h4>Add Student Invoice</h4>
							</div>
							<nav aria-label="breadcrumb" role="navigation">
								<ol class="breadcrumb">
									<li class="breadcrumb-item"><a href="dashboard.php">Dashboard</a></li>
									<li class="breadcrumb-item"><a href="view_student_reg.php">View Student Registration</a></li>
                  <li class="breadcrumb-item"><a href="view_invoice_history.php?fld_reg_no=<?php echo $_GET['fld_reg_no'] ?>&fld_status=<?php echo $_GET['fld_status'] ?>">Invoice History</a></li>
                  <li class="breadcrumb-item active" aria-current="page">Add Student Invoice</li>
								</ol>
							</nav>
						</div>
            <div class="col-md-2 col-sm-12 text-right">
              <div class="dropdown">
                <a class="btn btn-primary" href="view_invoice_history.php?fld_reg_no=<?php echo $_GET['fld_reg_no'] ?>" role="button">
                  View Invoice<br> History
                </a>
              </div>
            </div>
					</div>
				</div>


				
        			<div class="pd-20 bg-white border-radius-4 box-shadow mb-30">
					<div class="clearfix">
						<div class="pull-left">
							<!-- <h4 class="text-blue">Add Photo</h4> -->
						</div>
						
					</div><br> 
					 <?php 
          
          ?>
					<form method="post" enctype="multipart/form-data">
            
            <?php 
              $select1=mysqli_query($connect,"select fld_invoice_id from tbl_invoice order by fld_invoice_id desc limit 1") or die(mysqli_error($connect));
              $fetch1=mysqli_fetch_array($select1);
              $invoice_id=$fetch1['fld_invoice_id'];

             ?>
             <?php
            $sel="select * from tbl_student_reg where fld_reg_no='".$_GET['fld_reg_no']."'";
            $select=mysqli_query($connect,$sel) or die (mysqli_error($connect));
            $fetch=mysqli_fetch_array($select);
            extract($fetch);  

            date_default_timezone_set('Asia/Kolkata');
            $current_date=Date("Y-m-d");  
            ?>
						<div class="form-group row">
              <label class="col-sm-12 col-md-2 col-form-label">Invoice Id <span class="text-danger">*</span> : </label>
              <div class="col-sm-12 col-md-10">
                <input type="text" name="invoice_id" class="form-control" value="<?php echo "GBI" . ++$invoice_id;?>" readonly>
              </div>
            </div>
            <div class="form-group row">
              <label class="col-sm-12 col-md-2 col-form-label">Student Registration Id <span class="text-danger">*</span> : </label>
              <div class="col-sm-12 col-md-10">
                <input type="text" name="stud_reg_id" value="<?php echo $fetch['fld_reg_no'];?>" class="form-control" readonly>
              </div>
            </div>
             <div class="form-group row">
              <label class="col-sm-12 col-md-2 col-form-label">Total Amount<span class="text-danger">*</span> : </label>
              <div class="col-sm-12 col-md-10">
                <input type="text" name="total_amount" value="<?php echo $fetch['fld_final_fees'];?>" class="form-control" readonly>
              </div>
            </div>
            <?php 
            // error_reporting(0);
              $asa="select * from tbl_invoice where fld_reg_no='".$_GET['fld_reg_no']."' order by fld_invoice_id desc limit 1";
             $query=mysqli_query($connect,$asa);
              if (mysqli_num_rows($query)==0)
               {
                $pending_amount=$fetch['fld_final_fees'];
               }
               else
               {
                $view=mysqli_fetch_array($query);
                $pending_amount=$view['fld_balance_amount'];
               }
            ?>
            <div class="form-group row">

              <label class="col-sm-12 col-md-2 col-form-label">Pending Amount<span class="text-danger">*</span> : </label>
              <div class="col-sm-12 col-md-10">
                <input type="text" id="txt1" name="pending_amount"  value="<?php echo $pending_amount;?>" class="form-control" readonly>
              </div>
            </div>
            <div class="form-group row">
              <label class="col-sm-12 col-md-2 col-form-label">Paid Amount<span class="text-danger">*</span> : </label>
              <div class="col-sm-12 col-md-10">
                <input type="text" name="paid_amount" id="txt2" class="form-control" placeholder="Enter Paid Amount" required="" onkeyup="fn1()">
              </div>
            </div>
            <div class="form-group row">
              <label class="col-sm-12 col-md-2 col-form-label">Balance Amount<span class="text-danger">*</span> : </label>
              <div class="col-sm-12 col-md-10">
                <input type="text" name="balance_amount" id="txt3" class="form-control" required="" readonly="">
              </div>
            </div>
            <div class="form-group row">
              <label class="col-sm-12 col-md-2 col-form-label">Current Date: <span class="text-danger">*</span> </label>
              <div class="col-sm-12 col-md-10">
                <input type="text" name="fld_customer_package_date" class="form-control" required="" placeholder="Package Booked Date" value="<?php echo $current_date; ?>" readonly>
              </div>
            </div>		
						<div class="form-group row">
							<div class="col-sm-12 col-md-10">
							<center><input class="btn btn-success" value="Submit" type="submit" name="submit">&nbsp;
								<input class="btn btn-danger" value="Reset" type="reset">&nbsp;
								<a href="view_invoice_history.php?fld_reg_no=<?php echo $_GET['fld_reg_no'] ?>&fld_status=<?php echo $_GET['fld_status'] ?>" class="btn btn-warning">Back</a></center>
							</div>
						</div>
					</form>
         
<?php

if (isset($_POST['submit'])) 
{
    extract($_POST);

      date_default_timezone_set('Asia/Kolkata');
      $current_date=Date("Y-m-d");

      if($balance_amount==0)
      {
        $dgfh=mysqli_query($connect,"update tbl_student_reg set 
                                    fld_status='Paid',
                                    fld_invoice_current_date='".$current_date."'
                            where fld_reg_no='".$_GET['fld_reg_no']."'");

        $back="view_invoice_history.php?fld_reg_no=".$_GET['fld_reg_no']."&fld_status=Paid";
      }
      else
      {
        $asd=mysqli_query($connect,"update tbl_student_reg set 
                                    fld_invoice_current_date='".$current_date."'
                            where fld_reg_no='".$_GET['fld_reg_no']."'");
        $back="view_invoice_history.php?fld_reg_no=".$_GET['fld_reg_no']."&fld_status=Balance";
      }

     
    $insert = mysqli_query($connect,"insert into tbl_invoice(fld_invoice_no,fld_reg_no, fld_final_fees, fld_pending_amount, fld_paid_amount, fld_balance_amount, fld_invoice_current_date) values('$invoice_id','".$_GET['fld_reg_no']."', '$total_amount', '$pending_amount', '$paid_amount', '$balance_amount', '$current_date')") or die(mysqli_error($connect));

   
    
    // $back="view_invoice_history.php?fld_stud_reg_id=".$_GET['fld_stud_reg_id']."&fld_status=".$_GET['fld_status']."&fld_status=Balance";   
    if($insert)
    {
      echo '<script>';
      echo " alert('Student Invoice Added Successfully..!!');";
      echo 'window.location.href = "'.$back.'"';
      echo '</script>';
    }  
    else
    {
      echo '<script>';
      echo " alert('Student Invoice Not Added');";
      // echo 'window.location.href = "shopping.php";';
      echo '</script>';
    }
  
}

?>

	</div>
		<?php include('include/footer.php'); ?>	
		</div>
	</div>
	<?php include('include/script.php'); ?>
<script type="text/javascript">
    function fn1()
{
   var a=parseInt(document.getElementById('txt1').value);
  var b=parseInt(document.getElementById('txt2').value);

  var result=parseInt(a) - parseInt(b);
  
  document.getElementById('txt3').value=result;
}
</script>
</body>
</html>
