<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
$_SESSION['cuPages']='blank_page.php';   
$cuPage='blank_page.php';    
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];     
}else{
header('Location:../index.php');
exit;    
}
$mhead='loanins';
$menuh='Installment &amp; Loan';
$phead='loaninv';
$page='Loan Invoice Create';
$dtnow = date("Y-m-d h:i:s", time());
?>
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
?>
<?php 
if(isset($_POST['save_loaninv'])){
    $loan_id = remove_junk(escape($_POST['loan_id']));
    $invno = remove_junk(escape($_POST['invno']));
    $amount = remove_junk(escape($_POST['amount']));
    $profit_amount = remove_junk(escape($_POST['profit_amount']));
    $profit_per = remove_junk(escape($_POST['profit_per']));
    $total = remove_junk(escape($_POST['total']));
    $installment = remove_junk(escape($_POST['installment']));
    $note = remove_junk(escape($_POST['note']));
	

    $sql="INSERT INTO tbl_loaninvoice(loan_id,invno,amount,profit_amount,profit_per,total,installment,note,date,uid,brid) 
    VALUES ($loan_id,'$invno','$amount','$profit_amount','$profit_per','$total','$installment','$note','$dtnow',$aid,$brid)";    
	$result = mysqli_query($con,$sql) or die(mysqli_error($con));
    $efid=mysqli_affected_rows($con);    
    if($efid>0){
    $act =remove_junk(escape('Loan Invoice No: '.$invno));    
    write_activity($aid,'LIN','New Loan Invoice has been Added',$act);
    save_msg('s','Data Successfully Saved!');    
    }else{
    save_msg('w','Data Fail to Saved!');    
    }
    echo "<script>window.location='lon_loaninvoicecreate.php'</script>";  

}
?>

<div class="content-wrapper">
<?php include('../layout/page_head.php'); ?>
<section class="content">
   <div class="row">
      <div class="col-md-9">
         <div class="box box-solid">
            <div class="box-header with-border">
               <h3 class="box-title">Loan Invoice</h3>
            </div>
            <div class="box-body">
            <?php echo display_msg();?>   
               <form action="" enctype="multipart/form-data" method="POST" accept-charset="utf-8">
                <div class="col-md-12 popup_details_div">
                     <div class="col-md-1"></div>
                     <div class="col-md-10">
                        <div class="col-md-12">
                            <div class="row">
                              <div class="col-md-6">
                                 <div class="form-group">
                                    <label>Loan ID</label>
                                    <select class="form-control" name="loan_id" id="loan_id">
                                        <option selected>Choose Please.....</option>
                                        <?php									
                                        $querys=mysqli_query($con,"SELECT * FROM tbl_loanid ORDER BY id ASC")or die(mysqli_error($con));
                                        while ($rows=mysqli_fetch_array($querys)){
                                        ?>
                                        <option value="<?php echo $rows['id'];?>"><?php echo $rows['code'];?>-<?php echo $rows['name'];?></option>
                                        <?php } ?>
                                      
                                     </select>
                                 </div>
                              </div>
                                <div class="col-md-6">
                                   <div class="form-group">
                                      <label>Loan Invoice No.</label>
                                      <input type="text" name="invno" maxlength="45" value="<?php echo gen_newinvno('tbl_loaninvoice','LON');?> "  id="invno" class="form-control" readonly />
                                   </div>
                                </div>
                            </div>
                           <div class="row">
                              <div class="col-md-3">
                                 <div class="form-group">
                                     <label>Loan Amount</label>
                                     <input type="text" name="amount"  value="" id="amount"  onchange="count_grand_total()"  class="form-control"/>
                                 </div>
                             </div>
                             <div class="col-md-3">
                                 <div class="form-group">
                                    <label>Profit Amount</label>
                                    <input type="text" name="profit_amount"  value="" id="profit_amount" value="0" onchange="count_grand_total()" class="form-control" placeholder="2000"/>
                                 </div>
                              </div>
                              <div class="col-md-3">
                                 <div class="form-group">
                                    <label>Profit %</label>
                                    <input type="text" name="profit_per"  value="" id="profit_per" value="0" onchange="count_grand_total()"  class="form-control" placeholder="10%"/>
                                 </div>
                              </div>
                           </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Total Amount</label>
                                        <input type="text" name="total"  value="" id="total" class="form-control"/>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                 <div class="form-group">
                                     <label>Installment</label>
                                     <input type="text" name="installment"  value="" id="installment" class="form-control"/>
                                 </div>
                             </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label>Note</label>
                                        <input type="text" name="note"  value="" id="note" class="form-control"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                     </div>
                     <div class="col-md-1"></div>
                  </div>
                  <div class="clearfix" ></div>
                  <div class="col-md-12 nopadding widgets_area"></div>
                  <div class="row"style="margin-top: 15px" >
                     <div class="col-md-8 ">
                     </div>
                     <div class="col-md-4 text-right" >
                        <input type="submit" name="save_loaninv" id="save" class="btn btn-flat bg-purple btn-sm" value="Save"/> <a href="lon_loaninvoice" class="btn btn-flat bg-gray  ">Close</a>
                     </div>
                  </div>
               </form>
            </div>
         </div>
      </div>
      <div class="col-md-3">
         <div class="row">
            <div class="col-md-12">
               <div class="box box-solid">
                  <div class="box-header">
                     <h3 class="box-title">History </h3>
                  </div>
                  <!-- /.box-header -->
                  <div class="box-body" >
                     {{-- history show here --}}
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>
   <?php include('../layout/quick.php');?>    

</section>
<!-- /.main content -->


<script type="text/javascript">
   function count_grand_total(){
       var amount = $('#amount').val();
       var profit_per = $('#profit_per').val();
       var profit_amount = $('#profit_amount').val();

           if(profit_per>0){
               var with_discount = parseInt(amount) + (parseInt(amount) * parseInt(profit_per)/100);
               $('#profit_amount').hide();
               $('#total').val(with_discount);
               return false;
           }else if(profit_amount >0){
               var with_amount = parseInt(amount) + parseInt(profit_amount);
               $('#profit_per').hide()
               $('#total').val(with_amount);
               return false;
           }else{
               $('#total').val(amount);
               return false;
           }

           return false;
         
   }
</script>  
</div>
<?php
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
   
</script>    
<!-- /page script -->
</html>    