<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

session_start();
ob_start();

require_once('configuration/server_connect.php');
require_once('configuration/all_function.php');

$active_id=$_REQUEST['active_id'];
$menu_id=$_REQUEST['menu_id'];

 // Common Information

$page_title="Chart of Accounts";



?>




<!DOCTYPE html>
<html lang="en">

<!-- ======= <head> ======= -->
 <?php require_once('inc_files/inc_head.php'); ?>
<!-- ======= </head> ======= -->

<body>

  <!-- ======= Header ======= -->
  <?php require_once('inc_files/inc_header.php'); ?><!-- End Header -->

  <!-- ======= Sidebar ======= -->
   <?php  require_once('inc_files/inc_menu.php'); ?>
   <!-- End Sidebar-->
   
  

  <main id="main" class="main">

    <div class="pagetitle">
      <h1><?php echo $page_title;?></h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="home.php">Home</a></li>
          <li class="breadcrumb-item">General Ledger</li>
          <li class="breadcrumb-item active"><?php echo $page_title;?></li>
        </ol>
      </nav>
    </div><!-- End Page Title -->
    <section class="section">
      <div class="row">
	  
	
		<div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
              <h5 class="card-title"><strong><?php echo $page_title;?></strong></h5>

              <!-- No Labels Form -->
             
            <form class="row g-3" action="" method="post" name="codz" id="codz" onSubmit="return check()">
	 
                
				
				
				
				<div class="col-md-2">
                 <label for="first_name" class="form-label"><strong> Account Class</strong></label>
           <select id="acc_class" name="acc_class"  class="form-select" >
                   <option></option>
                   <?php foreign_relation($conn,'acc_class','id','class_name',$_POST['acc_class'],'1');?>
                 </select>
			    
                </div>
				
				<div class="col-md-3">
                 <label for="first_name" class="form-label"><strong>Account Sub Class</strong></label>
     		<select id="acc_sub_class" name="acc_sub_class"  class="form-select"  >
			<option></option>
				<?php foreign_relation($conn,'acc_sub_class','id','sub_class_name',$_POST['acc_sub_class'],'1 order by id');?>
        	</select>
			   
                </div>
				
				<div class="col-md-3">
                 <label for="first_name" class="form-label"><strong>Account Group </strong></label>
         <select id="ledger_group" name="ledger_group"  class="form-select" >
			<option></option>
				<?php foreign_relation($conn,'ledger_group','group_id','group_name',$_POST['ledger_group'],'1');?>
        	</select>
			    
                </div>
				
				<div class="col-md-2" style="text-align:center">
                 <label for="first_name" class="form-label"><strong>&nbsp;</strong></label><br />
     	 <button type="submit" name="run_data" id="run_data" class="btn btn-success rounded-pill btn-sm">&nbsp;<i class="bi bi-filter me-1"></i> Run Report&nbsp;&nbsp;</button>
			   
                </div>
				
			<div class="col-md-2" align="right">
				<a href="04_acc_ledger.php?active_id=104&menu_id=2"><button type="button" class="btn btn-primary btn-sm"><i class="bi bi-plus-lg me-1"></i> New Account&nbsp;&nbsp;&nbsp;</button></a>
				</div>
				
				
               
  </form>
  <br><!-- End No Labels Form -->

            </div>
          </div>
		
		</div>
		
		
	  
	  
        <div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
			<br>
			

              <!-- Table with stripped rows -->
              <table class="table table-sm" style="font-size:12px;" id="example">
                <thead>
                  <tr style="text-transform:uppercase; font-size:12px; color:#012970; border-top: 1px solid #CCCCCC;">
                    <th width="19%" scope="col">Class</th>
                    <th width="17%" scope="col">Sub Class </th>
                    <th width="24%" scope="col">GL Group </th>
                    <th width="13%" scope="col">GL Code</th>
                    <th width="27%" scope="col">GL Account</th>
                  </tr>
                </thead>
                <tbody>
		<?php
		
		$sql = "select id, class_name from acc_class where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $class_name[$info->id]=$info->class_name;
	
		}
		
		$sql = "select id, sub_class_name from acc_sub_class where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $sub_class_name[$info->id]=$info->sub_class_name;
	
		}
		
		 $sql = "select group_id, group_name from ledger_group where 1 group by group_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $group_name[$info->group_id]=$info->group_name;
	
		}
					
					if($_POST['acc_class']!='')
					$con=" and acc_class='".$_POST['acc_class']."'";
					
					if($_POST['acc_sub_class']!='')
					$con=" and acc_sub_class='".$_POST['acc_sub_class']."'";
					
					if($_POST['ledger_group']!='')
					$con=" and ledger_group_id='".$_POST['ledger_group']."'";
					
						$sql = 'select * from accounts_ledger where 1 '.$con.' order by acc_class,acc_sub_class,ledger_group_id,ledger_id';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				
                 <tr>
                   <td><?php echo $class_name[$data->acc_class]?></td>
                   <td><?php echo $sub_class_name[$data->acc_sub_class]?></td>
                   <td><?php echo $group_name[$data->ledger_group_id]?></td>
                   <td><?php echo $data->ledger_id;?></td>
     			   <td><?php echo $data->ledger_name;?></td>
 				</tr>
				  <?php }?>
                </tbody>
              </table>
			  
			
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        
      </div>
    </section>

  </main><!-- End #main -->
  
  
<script>
$(document).ready(function() {
    // Save the initial item dropdown HTML (with default item_id = 1)
    var defaultItemDropdown = $('#acc_sub_class').html();
    $('#acc_class').on('change', function() {
        var acc_class = $(this).val();
        if (acc_class === '') {
            // Restore default options when nothing is selected
            $('#acc_sub_class').html(defaultItemDropdown);
            return;
        }
        // Load filtered items from the server
        $.ajax({
            url: 'acc_sub_class_filtering_ajax.php',
            type: 'POST',
            data: { acc_class: acc_class },
            success: function(response) {
                $('#acc_sub_class').html(response);
            },
            error: function() {
                $('#acc_sub_class').html('<option value="">Failed to load items</option>');
            }
        });
    });
});
</script>
  
  
<script>
$(document).ready(function() {
    // Save the initial item dropdown HTML (with default item_id = 1)
    var defaultItemDropdown = $('#ledger_group').html();
    $('#acc_sub_class').on('change', function() {
        var acc_sub_class = $(this).val();
        if (acc_sub_class === '') {
            // Restore default options when nothing is selected
            $('#ledger_group').html(defaultItemDropdown);
            return;
        }
        // Load filtered items from the server
        $.ajax({
            url: 'group_wise_filtering_ajax.php',
            type: 'POST',
            data: { acc_sub_class: acc_sub_class },
            success: function(response) {
                $('#ledger_group').html(response);
            },
            error: function() {
                $('#ledger_group').html('<option value="">Failed to load items</option>');
            }
        });
    });
});
</script>

 <!-- ======= Footer ======= -->
 <?php require_once('inc_files/inc_footer.php'); ?>
<!-- ======= Footer ======= -->
<?php 
selected_two("#acc_class");
selected_two("#acc_sub_class");
selected_two("#ledger_group");
 ?>

<script>
var printCounter = 0;
 
new DataTable('#example', {
    caption: "&copy; Copyright FinTax Consultant Ltd. All Rights Reserved",
    layout: {
        topStart: {
            buttons: [
                'copy',
                {
                    extend: 'excel',
                    messageTop:
                        'The information in this table is copyright to FinTax Consultant Ltd.'
                },
                {
                    extend: 'pdf',
                    //messageBottom: null
					messageBottom: "Copyright FinTax Consultant Ltd. All Rights Reserved"
                },
                {
                    extend: 'print',
                    messageTop: function () {
                        printCounter++;
 
                        if (printCounter === 1) {
                            return 'This is the first time you have printed this document.';
                        }
                        else {
                            return (
                                'You have printed this document ' + printCounter + ' times'
                            );
                        }
                    },
                    messageBottom: "&copy; Copyright FinTax Consultant Ltd. All Rights Reserved"
                }
            ]
        }
    }
});
</script>

<!--<script type="text/javascript">
$(document).ready(function() {
    // Check if DataTable is already initialized
    if ($.fn.DataTable.isDataTable('#example')) {
        // If DataTable is already initialized, destroy it
        $('#example').DataTable().destroy();
    }
    
    // Reinitialize DataTable with new options
    $('#example').dataTable({
        'iDisplayLength': 50
    });
});
</script>-->


</body>

</html>