<?php include("sass/icon_for_datepicker_and_datatable.php");?>
              
        <div class="wrapper row-offcanvas row-offcanvas-left">
            <!-- Left side column. contains the logo and sidebar -->
            <?php include("sass/sidebar.php");?>

            <!-- Right side column. Contains the navbar and content of the page -->
               <aside class="right-side">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                       <?php echo $company_name ; ?>
                        <small>Control panel</small>
                    </h1>
                    <ol class="breadcrumb">
                        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li class="active">Dashboard</li>
                    </ol>
                </section>
                 <section class="content">
                 	 <div class="row">
                        <div class="col-md-12" id="formdiv">
                 <!---Main Content--->

	<?php
	
	
		
	
$errMsg=""; 
if(isset($_POST['submit'])) // To check 'submmited or not' by if(isset($_POST['submit'])) 
{
	$amount = $_POST['amount'];
	$ref_id = $_POST['ref_id'];
	$ledger = $_POST['ledger'];
	$payment_type = $_POST['payment_type'];
	
	$type = $_POST['type'];
	$cheque_no = $_POST['cheque_no'];
	$cheque_date = $_POST['cheque_date'];
	$bank_id = $_POST['bank_id'];
	$details = $_POST['details'];
	$date = $_POST['date'];
	
	if(!empty($cheque_date)) {
$cheque_date = date("Y-m-d", strtotime($cheque_date));
}
else {
$cheque_date = "";
}

	if(!empty($date)) {
$date = date("Y-m-d", strtotime($date));
}
else {
$date = "";
}
	
	if(empty($bank_id))
	{
		if($type=="1"){
		$deborcr = "debit";
		mysqli_query($connection,"UPDATE cash SET cash = cash + $amount WHERE branch_id  = '$branch_id_for_header_user'")
				or die(mysqli_error());
				}
				elseif($type=="2"){
				$deborcr = "credit";
				mysqli_query($connection,"UPDATE cash SET cash = cash - $amount WHERE branch_id  = '$branch_id_for_header_user'")
				or die(mysqli_error());
				
				}
		
		
	mysqli_query($connection,"INSERT INTO transaction (date, ref_id, ledger, $deborcr, hint, details, payment_type, branch_id) VALUES ('$date','$ref_id','$ledger','$amount','$ledger','$details','cash','$branch_id_for_header_user')")
		or die(mysqli_error());
		
		
		
		$errMsg="Trunsaction entered successfully";
	
}

else{


		if($type=="1"){
		$deborcr = "debit";
		//mysqli_query($connection,"UPDATE bank_name SET balance = balance + $amount WHERE bank_id = $bank_id")
				//or die(mysqli_error());
				}
				elseif($type=="2"){
				$deborcr = "credit";
				//mysqli_query($connection,"UPDATE bank_name SET balance = balance - $amount WHERE bank_id = $bank_id")
				//or die(mysqli_error());
				
				}
		
		
		mysqli_query($connection,"INSERT INTO bank_statement (amount, ref, payment_type, reason, bank_id, cheque_no, cheque_date, date, branch_id) VALUES ('$amount','$ref_id','$deborcr','$ledger','$bank_id','$cheque_no','$cheque_date','$cheque_date','$branch_id_for_header_user')")
		or die(mysqli_error()); 
		
		mysqli_query($connection,"INSERT INTO transaction (ref_id, branch_id) VALUES ('$ref_id','$branch_id_for_header_user')")
		or die(mysqli_error());
		
		$errMsg="Trunsaction entered successfully";



}


}

$sql="select MAX(RIGHT(ref_id,10)) as billing_no from transaction where ref_id like '$branch_id_for_header_user-BILL%'";
	$result=mysqli_query($connection,$sql);

	if($row=mysqli_fetch_array($result))
	{
	$billing_no=$row['billing_no'];
	}
	else
	{
	$billing_no=0;
	}
	$billing_no++;
	$billing_no="$branch_id_for_header_user-BILL".str_pad($billing_no,10,0,STR_PAD_LEFT);
?>
                 <?php
							if (!empty($errMsg))
							{echo "<div class='alert alert-success alert-dismissible' role='alert'>$errMsg";
							echo "<button type='button' class='close' data-dismiss='alert'><span aria-hidden='false'>&times;</span><span class='sr-only'>		Close</span></button>
                        </div>";}
                        ?>
                 			<div class="box box-primary">
                                <div class="box-header">
                                    <h3 class="box-title">Create Voucher</h3>
                                </div><!-- /.box-header -->
                                <!-- form start -->
                                
                            
                                <form role="form"  action="<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="post" id="login_form">
                                    <div class="box-body">
                                    	<div class="col-md-12">
                                        <div class="col-md-6">
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Amount</label>
                                            <input type="number" class="form-control" name="amount" required>
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Ref. ID</label>
                                            <input type="text" class="form-control" name="ref_id" value="<?php echo $billing_no ; ?>" required>
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Ledger</label>
											
											 <select name="ledger" class="form-control" required>
                                            <option value="">Select One</option>
												<?php  
                                                $query_bank = "select * from particulars";
                                                $result_bank = mysqli_query($connection,$query_bank);
                                                
                                               
                                                while($row_bank=mysqli_fetch_array($result_bank))
                                                {
                                                
                                                echo "<option value='" . $row_bank['particulars_id'] . "'>" . $row_bank['particulars'] . "</option>";}?> 
                                            
                                            </select>
											
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Payment Type</label>
                                            <select name="payment_type" class="form-control" required>
                                            	<option value="cash">Cash</option>
                                                <option value="bank">Bank</option>
                                             </select>
                                          </div>
                                          
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Book Type</label>
                                            <select name="type" class="form-control" required>
                                            	<option value="1">Receive</option>
                                                <option value="2">Payment</option>
                                             </select>
                                          </div>
                                        </div>
                                        
                                        
                                        
                                        <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Cheque Number</label>
                                            <input type="text" class="form-control" name="cheque_no">
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Cheque Date</label>
                                            <input type="text" class="form-control datepicker" autocomplete="off" name="cheque_date">
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Bank</label>
                                           <select name="bank_id" class="form-control">
                                            <option value="">Select One</option>
												<?php  
                                                $query_bank = "select * from bank_name";
                                                $result_bank = mysqli_query($connection,$query_bank);
                                                
                                               
                                                while($row_bank=mysqli_fetch_array($result_bank))
                                                {
                                                
                                                echo "<option value='" . $row_bank['bank_id'] . "'>" . $row_bank['bank_name'] . "</option>";}?> 
                                            
                                            </select>
                                        </div>
                                                
                                          <div class="form-group">
                                            <label for="exampleInputEmail1">Details</label>
                                            <input type="text" class="form-control" name="details" maxlength="50">
                                        </div>     
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Date</label>
                                            <input type="text" class="form-control datepicker" autocomplete="off" name="date" value="<?php echo date("d-m-Y");?>" required>
                                        </div>  
                                        </div>
									</div>
												<!-- /.box-body -->

                                    <div class="box-footer">
                                        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                                    </div>
                                </form>
                            </div>
                 
                 		</div>
                    </div>
                                          <img src="img/ajax-loader1.gif" style="display: none; margin-left:30%;" id="loading_image" width="50%">      
                 
                 
                 </section>
                 
                 
                 
                 
                 
                </aside>
                <!-- /.right-side -->
        </div><!-- ./wrapper -->

        <!-- add new calendar event modal -->
<script type="text/javascript">
	 $('#login_form').submit(function() {
	$('#formdiv').hide();
	$('#tbldiv').hide();
    $('#loading_image').show(); // show animation
    return true; // allow regular form submission
});
 </script>

        <!-- jQuery 2.0.2 -->
         <?php include("sass/footer_for_datepicker_and_datatable.php");?>

    </body>
</html>