<?php
@session_start();
@ob_start();

include "header.php"; 
//$objIms->expense_alter();
?>
<script type="text/javascript">
	$(document).ready(function() {                
				$("#FORMNAME1").validate({
                    rules: {
					
                   expense_type:"required",
				  amount:"required",
					},
		messages: {
			
					 expense_type: "",
					 amount: "",
								
				},
		  });                
            });
//---auto search-----
</script>
<!-- Page wrapper  -->
        <div class="page-wrapper">
           
            <!-- Container fluid  -->
             <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row justify-content-center">
                    <div class="col-lg-6">
                        <div class="card">
						<h4>Expances <span style="float:right"><button onclick="goBack()" class="btn btn-info btn-sm">Go Back</button><script>
function goBack() {
    window.history.back();
}
</script></span></h4>
						<hr />
    <form method="post" id="FORMNAME1" enctype="multipart/form-data" >
	<?php  echo $objIms->error;?>
	
								<div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Expance From (Bank) <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                 <select name="bank" id="bank" onchange="fetch()"  class="required form-control" required>
     
      <?php 
							$sqlc="select * from tbl_bank_detail where session_login_id = '".$_SESSION['login_id']."'";
							//echo $sqlc;
							$c=mysql_query($sqlc);
							if(mysql_num_rows($c)>0){
							while($cs=mysql_fetch_array($c))
							{?>
      <option value="<?=$cs["id"];?>" >
      <?=$cs["bank_name"];?> (AC/NO:  <?=$cs["acc_no"];?>)
      </option>
      <?php }}?>
    </select>
                                            </div>
                                        </div>
										
										<div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Expense Type<span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                 <input type="text" name="expense_type" id="expense_type" class="form-control" placeholder="eg: transport etc" required>
                                            </div>
                                        </div>
										
										<div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Amount<span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                 <input name="amount" type="text" onkeyup="fetch()" onkeypress="return isNumber(event,this);" class="form-control" id="amount" required />
                                            </div>
                                        </div>
										<div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Date<span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                 <input name="a_date" type="date" id="a_date" class="form-control" required />
                                            </div>
                                        </div>
										
										<div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Purpose<span class="text-danger"></span></label>
                                            <div class="col-lg-6">
                                                  <input name="Remarks" type="text"  class="form-control" id="Remarks" />
                                            </div>
                                        </div>
	<div class="form-group row">
                                            <div class="col-lg-8 ml-auto">
                                                <input type ="submit" name="Save" value="save"  class="btn btn-info"/>
                                            </div>
                                        </div>

    
</form>
<script type="text/javascript">
	function isNumber(evt, element) {
  var charCode = (evt.which) ? evt.which : event.keyCode
  if (charCode > 31 && (charCode < 48 || charCode > 57) && !(charCode == 46 || charCode == 8))
    return false;
  else {
    var len = $(element).val().length;
    var index = $(element).val().indexOf('.');
    if (index > 0 && charCode == 46) {
      return false;
    }
    if (index > 0) {
      var CharAfterdot = (len + 1) - index;
      if (CharAfterdot > 3) {
        return false;
      }
    }

  }
  return true;
}
</script>
<?php
if(isset($_POST['Save'])){

$type = 'Expance';
$a_date= date('d-m-Y', strtotime($_REQUEST['a_date']));
$aa_date =$objIms->DateToInt($a_date);



$sql_ins="INSERT INTO  `tbl_account` (`bank_id`, `Dr_Amt`, `sales_type`, `expance_type`, `remark`,  `trans_date`,  `session_login_id`) VALUES ('".$_POST['bank']."', '".$_POST['amount']."', '$type', '".$_POST['expense_type']."', '".$_POST['Remarks']."', '$aa_date', '".$_SESSION['login_id']."');";
				$res_ins=mysql_query($sql_ins);


$sql_expens = "insert into expense_details(expense_report, amount, head, stampdate, session_login_id) values('
$type', '".$_POST['amount']."', 'Expense', '$aa_date ' , '".$_SESSION['login_id']."');";
$res_expens = mysql_query($sql_expens);

$sql_bank = "insert into tbl_bank(head, bankdet_id, credit, stampdate, session_login_id) values('Expense','".$_POST['bank']."',  '".$_POST['amount']."', '$aa_date', '".$_SESSION['login_id']."');";
$res_bank = mysql_query($sql_bank);
				?>
				<script>
				alert('Success');
				location='expance_list.php';
				</script>
				<?php
				

}
?>
<script>

function fetch(){

var bank = $('#bank').val();
var amt = $('#amount').val();

 $.ajax({
		type: "POST",
		url: "check_balance.php",
		data: "bank_id="+bank,
		 dataType: 'json', 
		cache: false,
		success: function(data)
			{
				var amount = data[0];
				
				if(eval(amt) > eval(amount)){
				alert('Dont have sufficient balance, please add amount to bank');
				$('#amount').val('');
				$('#amount').focus();
				}
				
				
			}
			});
   
	


}


</script>
 </div>

                            </div>
						
                            
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->
<?php include 'footer.php' ?>