<?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="Account Group";
$page="03_acc_group.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='ledger_group';	
$unique='group_id';

$request_id=$_REQUEST['group_id'];

$ms_data=mysqli_fetch_object(mysqli_query($conn,'select * from '.$table.' where group_id="'.$request_id.'"'));

// End Common Information

if(isset($_POST['insert']))

{		

$_POST['entry_by'] = $_SESSION['user']['id'];
		 
$_POST['entry_at'] = $now=date('Y-m-d H:i:s');

$group_name = $_POST['group_name'];
$acc_sub_class = $_POST['acc_sub_class'];

$acc_class=find_a_field($conn,'acc_sub_class','acc_class','id="'.$acc_sub_class.'"');

$cy_id=mysqli_fetch_object(mysqli_query($conn,'select max(group_sl) as group_sl from '.$table.' where acc_sub_class="'.$acc_sub_class.'"'));
$group_sl_id=$cy_id->group_sl+1;
$group_sl_no = sprintf("%03d", $group_sl_id);

$group_id = $acc_sub_class.''.$group_sl_no;

  $ins_sql = 'INSERT INTO ledger_group (group_id, group_sl, group_name, acc_class, acc_sub_class, entry_by, entry_at) VALUES

("'.$group_id.'", "'.$group_sl_no.'", "'.$group_name.'", "'.$acc_class.'", "'.$acc_sub_class.'", "'.$_POST['entry_by'].'", "'.$_POST['entry_at'].'")';
	
mysqli_query($conn,$ins_sql);

header("location:".$page."");

}



if(isset($_POST['update']))

{		

$_POST['entry_by'] = $_SESSION['user']['id'];
		 
$_POST['entry_at'] = $now=date('Y-m-d H:i:s');

$group_name = $_POST['group_name'];

$group_id=$request_id;

$up_sql = 'update ledger_group set group_name="'.$group_name.'", edit_by="'.$_POST['entry_by'].'", edit_at="'.$_POST['entry_at'].'" 
 where group_id='.$group_id;
mysqli_query($conn,$up_sql);

header("location:".$page."");


}

?>


<!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-8"> 

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

              <!-- Table with stripped rows -->
              <table class="table"   id="data_table"  style="font-size:14px;">
                <thead>
                  <tr>
                    <th scope="col"><span class="card-title">Code</span></th>
                    <th scope="col"><span class="card-title">Account Group</span></th>
                    <th scope="col"><span class="card-title">Acc. Sub Class</span></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 * from '.$table.' where 1';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				
                 <tr>
                  <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->group_id;?>" style="color:#000000"><?php echo $data->group_id;?></a></td>
                  <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->group_id;?>" style="color:#000000"><?php echo $data->group_name;?></a></td>
                  <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->group_id;?>" style="color:#000000"><?php echo $sub_class_name[$data->acc_sub_class]?></a></td>
                  </tr>
				  <?php }?>
                </tbody>
              </table>
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        <div class="col-lg-4">

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

              <!-- Multi Columns Form -->
              <form class="row g-3" action="" method="post" name="form1" id="form1" onSubmit="return check()">
                <div class="col-md-12">
                  <label for="inputName5" class="form-label">Account Group   </label>
                   <input type="text" class="form-control" id="group_name" name="group_name" value="<?php if($request_id>0){ echo $ms_data->group_name; } ?>" required>
                </div>
				
				<div class="col-12">
                  <label for="inputAddress2" class="form-label">Account Sub Class</label>
                 
			  <?php if($request_id>0){?>
			<select id="acc_sub_class" name="acc_sub_class" required class="form-select" >
			
			<?php
			
	 $sel_sql='select * from acc_sub_class where id="'.$ms_data->acc_sub_class.'"';

	$sel_query = mysqli_query($conn,$sel_sql);

	while($sl_data=mysqli_fetch_object($sel_query)){ 
			
			?>

	  		<option value="<?php echo $sl_data->id;?>"><?php echo $sl_data->sub_class_name;?></option>  
        	
			<?php } ?>
        	</select>
			<?php } else {?>
			
			
			<select id="acc_sub_class" name="acc_sub_class" required class="form-select" >
			<option></option>
			<?php
			
	 $sel_sql='select * from acc_sub_class where 1 order by acc_class, id';

	$sel_query = mysqli_query($conn,$sel_sql);

	while($sl_data=mysqli_fetch_object($sel_query)){ 
			
			?>

	  		<option value="<?php echo $sl_data->id;?>"><?php echo $sl_data->sub_class_name;?></option>  
        	
			<?php } ?>
        	</select>
			
			<?php }?>
                </div>
				
          
                
                <div class="text-center">
				<?php if($request_id>0) {?>
                 <button type="submit" class="btn btn-success btn-sm" name="update" id="update" ><i class="bi bi-check-all"></i> Update &nbsp;</button>
				 <?php }else {?>
				 <button type="submit" class="btn btn-primary btn-sm" name="insert" id="insert" ><i class="bi bi-plus"></i> Save &amp; New &nbsp;</button>
				 <?php }?>
                  <button type="reset" class="btn btn-secondary btn-sm" onClick="parent.location='<?php echo $page?>'" >Reset</button>
                </div>
              </form><!-- End Multi Columns Form -->

            </div>
          </div>

          

          

        </div>
      </div>
    </section>

  </main><!-- End #main -->

  <!-- ======= Footer ======= -->
 <?php require_once('inc_files/inc_footer.php'); ?>
<!-- ======= Footer ======= -->
<?php selected_two("#acc_sub_class"); ?>
<script>
var printCounter = 0;
 
new DataTable('#data_table', {
    caption: "&copy; Copyright <?php echo $grp_data->group_name?> All Rights Reserved",
    layout: {
        topStart: {
            buttons: [
                'copy',
                {
                    extend: 'excel',
messageTop: 'For the period from <?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?>',
                },
                {
                    extend: 'pdf',
					
messageTop: 'For the period from <?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?>',			                    
                    //messageBottom: null
					messageBottom: "Copyright <?php echo $grp_data->group_name?> All Rights Reserved"
                },
                {
                    extend: 'print',
									
messageTop: 'For the period from <?php echo "<strong>" ?><?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?><?php echo "</strong>" ?>',
                    //messageTop: function () {
//                        printCounter++;
// 
//                        if (printCounter === 1) {
//					
//                            return 'This is the first time you have printed this document. ttt';
//                        }
//                        else {
//                            return (
//                                'You have printed this document ' + printCounter + ' times'
//                            );
//                        }
//                    },
                    messageBottom: "&copy; Copyright <?php echo $grp_data->group_name?> All Rights Reserved"
                }
            ]
        }
    }
});
</script>



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

</body>

</html>