<?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");?>
  <?php
$errMsg=""; 
if(isset($_POST['submit'])) // To check 'submmited or not' by if(isset($_POST['submit'])) 
{
	$customer_id = $_POST['customer_id'];
	$invoice_no = $_POST['invoice_no'];
	$amount_t = $_POST['amount'];
	$media = $_POST['media'];
	$bank_id = $_POST['bank_id'];
	$cheque_no = $_POST['cheque_no'];
	$cheque_date = $_POST['cheque_date'];
	$ledger = $_POST['ledger'];
	if(!empty($cheque_date)) {
$newDate = date("Y-m-d", strtotime($cheque_date));
}
else {
$newDate = "";
}
	$date = date("Y-m-d");
	
	if(empty($invoice_no)) $errMsg="Please Input Invoice No";
	if(empty($errMsg))
	{
	
	$check=mysqli_query($connection,"select * from sale_taka where invoice_no='$invoice_no' AND customer_id='$customer_id'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows>0){$errMsg="Sorry ! Please Insert New Invoice No.";}
		
		else{
		
		
	
				
				
		
		
		if(!empty($bank_id)) {

	/*mysqli_query($connection,"UPDATE bank_name SET balance = balance + $amount_t WHERE bank_id = $bank_id")
				or die(mysqli_error());
				
	mysqli_query($connection,"INSERT INTO cus_due_payment (customer_id, $media, cheque_no, cheque_date, bank_id, invoice_no, date) VALUES ('$customer_id','$amount_t','$cheque_no','$newDate','$bank_id','$invoice_no','$date')")
		or die(mysqli_error()); 
				
				*/
		
		mysqli_query($connection,"INSERT INTO bank_statement (amount, ref, payment_type, reason, customer_id, bank_id, cheque_no, cheque_date, date, branch_id) VALUES ('$amount_t','$invoice_no','debit','6','$customer_id','$bank_id','$cheque_no','$newDate','$newDate','$branch_id_for_header_user')")
		or die(mysqli_error());
		 
		/* mysqli_query($connection,"INSERT INTO transaction (date, ref_id, customer_id, ledger, debit, cheque_no, cheque_date, bank_id, hint, payment_type) VALUES ('$date','$invoice_no','$customer_id','$ledger','$amount_t','$cheque_no','$newDate','$bank_id','customer due payment','bank')")
		or die(mysqli_error());*/
		 
		}
		else {
			mysqli_query($connection,"UPDATE customer_setup SET opening_due = opening_due - $amount_t WHERE customer_id = '$customer_id'")
				or die(mysqli_error()); 
		mysqli_query($connection,"INSERT INTO cus_due_payment (customer_id, cash, invoice_no, date, branch_id) VALUES ('$customer_id','$amount_t','$invoice_no','$date','$branch_id_for_header_user')")
		or die(mysqli_error()); 
		 
		 mysqli_query($connection,"INSERT INTO transaction (date, ref_id, customer_id, ledger, debit, hint, payment_type, branch_id) VALUES ('$date','$invoice_no','$customer_id','6','$amount_t','customer due payment','cash','$branch_id_for_header_user')")
		or die(mysqli_error());
		
		mysqli_query($connection,"UPDATE cash SET cash = cash + $amount_t WHERE branch_id  = '$branch_id_for_header_user'")
				or die(mysqli_error());
		
		}
				
				
		$errMsg="Customer Due Paid";
	
}
}
}
?>
            <!-- 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">
                        
                 <!---Main Content--->
                 
                 			<?php 
							
							if (!empty($_REQUEST['customer_setup_id']))
						{$customer_setup_id = $_REQUEST['customer_setup_id'];
						}
						else {
						$customer_setup_id = 0 ;
						}
							
							$result_for_cus = mysqli_query($connection,"SELECT customer_name , opening_due FROM customer_setup WHERE customer_id  = '$customer_setup_id'");
		$test_for_cus = mysqli_fetch_array($result_for_cus);
				
				$customer_name=$test_for_cus['customer_name'];
				$opening_due=$test_for_cus['opening_due'];
				
							
							?>
                            
                           <div class="col-md-8 col-md-offset-2">
                         
                       
                        <?php
							if (!empty($errMsg))
							{echo "<div class='alert alert-warning 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">Customer Primary Due Payment</h3>
                                </div><!-- /.box-header -->
                                <!-- form start -->
                                <form role="form"  action="<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="post">
                                    <div class="box-body">
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Customer Name</label>
                                            <input type="hidden" name="customer_id" value="<?php echo $customer_setup_id ;?>" />
                                            <input type="text" class="form-control" name="customer_name" value="<?php echo $customer_name ;?>">
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Total Due</label>
                                            <input type="text" class="form-control" readonly="readonly" value="<?php echo $opening_due ;?>">
                                        </div>
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">New Invoice Number</label>
                                            <input type="text" class="form-control" name="invoice_no">
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Paid Amount</label>
                                            <input type="text" class="form-control" name="amount">
                                        </div>
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Payment Type</label>
                                             <select name="media" class="form-control">
                                             	<option value="cash">Cash</option>
                                                <option value="bank">Bank</option>
                                             </select>
                                        </div>
                                        
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Bank Name</label>
                                            <select name="bank_id" class="form-control">
                                            <option value="">Select</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">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">Details</label>
                                           <textarea class="form-control" name="ledger" rows="3" required></textarea>
                                        </div>
                                        
                                    </div><!-- /.box-body -->

                                    <div class="box-footer">
                                        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                                    </div>
                                </form>
                            </div>
                         
                         </div> 
                 
                 		
                    </div>
                 
                 
                 </section>
                </aside>
                <!-- /.right-side -->
        </div><!-- ./wrapper -->


        <!-- jQuery 2.0.2 -->
         <!-- Bootstrap -->
        <script src="js/bootstrap.min.js" type="text/javascript"></script>
        <!-- DATA TABES SCRIPT -->
        <script src="js/plugins/datatables/jquery.dataTables.js" type="text/javascript"></script>
        <script src="js/plugins/datatables/dataTables.bootstrap.js" type="text/javascript"></script>
        <!-- AdminLTE App -->
        <script src="js/AdminLTE/app.js" type="text/javascript"></script>
        <!-- AdminLTE for demo purposes -->
        <script src="js/AdminLTE/demo.js" type="text/javascript"></script>
        <!-- page script -->
        <script type="text/javascript">
            $(function() {
                $("#example1").dataTable();
                $('#example2').dataTable({
                    "bPaginate": true,
                    "bLengthChange": false,
                    "bFilter": false,
                    "bSort": true,
                    "bInfo": true,
                    "bAutoWidth": false
                });
            });
        </script>
<script>
$(function() {
$( ".datepicker" ).datepicker({ dateFormat: 'dd-mm-yy', changeYear: true });
});

</script>

    </body>
</html>